PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php

https://gitlab.com/reasonat/test8
PHP | 213 lines | 97 code | 22 blank | 94 comment | 12 complexity | 339e12ad0d54a0641960967e5cc7daaf MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\file\Kernel;
  3. use Drupal\file\Entity\File;
  4. use Drupal\file\FileInterface;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Drupal\user\Entity\User;
  7. /**
  8. * Base class for file unit tests that use the file_test module to test uploads and
  9. * hooks.
  10. */
  11. abstract class FileManagedUnitTestBase extends KernelTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = array('file_test', 'file', 'system', 'field', 'user');
  18. protected function setUp() {
  19. parent::setUp();
  20. // Clear out any hook calls.
  21. file_test_reset();
  22. $this->installConfig(array('system'));
  23. $this->installEntitySchema('file');
  24. $this->installEntitySchema('user');
  25. $this->installSchema('file', array('file_usage'));
  26. // Make sure that a user with uid 1 exists, self::createFile() relies on
  27. // it.
  28. $user = User::create(['uid' => 1, 'name' => $this->randomMachineName()]);
  29. $user->enforceIsNew();
  30. $user->save();
  31. \Drupal::currentUser()->setAccount($user);
  32. }
  33. /**
  34. * Assert that all of the specified hook_file_* hooks were called once, other
  35. * values result in failure.
  36. *
  37. * @param array $expected
  38. * Array with string containing with the hook name, e.g. 'load', 'save',
  39. * 'insert', etc.
  40. */
  41. function assertFileHooksCalled($expected) {
  42. \Drupal::state()->resetCache();
  43. // Determine which hooks were called.
  44. $actual = array_keys(array_filter(file_test_get_all_calls()));
  45. // Determine if there were any expected that were not called.
  46. $uncalled = array_diff($expected, $actual);
  47. if (count($uncalled)) {
  48. $this->assertTrue(FALSE, format_string('Expected hooks %expected to be called but %uncalled was not called.', array('%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled))));
  49. }
  50. else {
  51. $this->assertTrue(TRUE, format_string('All the expected hooks were called: %expected', array('%expected' => empty($expected) ? '(none)' : implode(', ', $expected))));
  52. }
  53. // Determine if there were any unexpected calls.
  54. $unexpected = array_diff($actual, $expected);
  55. if (count($unexpected)) {
  56. $this->assertTrue(FALSE, format_string('Unexpected hooks were called: %unexpected.', array('%unexpected' => empty($unexpected) ? '(none)' : implode(', ', $unexpected))));
  57. }
  58. else {
  59. $this->assertTrue(TRUE, 'No unexpected hooks were called.');
  60. }
  61. }
  62. /**
  63. * Assert that a hook_file_* hook was called a certain number of times.
  64. *
  65. * @param string $hook
  66. * String with the hook name, e.g. 'load', 'save', 'insert', etc.
  67. * @param int $expected_count
  68. * Optional integer count.
  69. * @param string $message
  70. * Optional translated string message.
  71. */
  72. function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) {
  73. $actual_count = count(file_test_get_calls($hook));
  74. if (!isset($message)) {
  75. if ($actual_count == $expected_count) {
  76. $message = format_string('hook_file_@name was called correctly.', array('@name' => $hook));
  77. }
  78. elseif ($expected_count == 0) {
  79. $message = \Drupal::translation()->formatPlural($actual_count, 'hook_file_@name was not expected to be called but was actually called once.', 'hook_file_@name was not expected to be called but was actually called @count times.', array('@name' => $hook, '@count' => $actual_count));
  80. }
  81. else {
  82. $message = format_string('hook_file_@name was expected to be called %expected times but was called %actual times.', array('@name' => $hook, '%expected' => $expected_count, '%actual' => $actual_count));
  83. }
  84. }
  85. $this->assertEqual($actual_count, $expected_count, $message);
  86. }
  87. /**
  88. * Asserts that two files have the same values (except timestamp).
  89. *
  90. * @param \Drupal\file\FileInterface $before
  91. * File object to compare.
  92. * @param \Drupal\file\FileInterface $after
  93. * File object to compare.
  94. */
  95. function assertFileUnchanged(FileInterface $before, FileInterface $after) {
  96. $this->assertEqual($before->id(), $after->id(), t('File id is the same: %file1 == %file2.', array('%file1' => $before->id(), '%file2' => $after->id())), 'File unchanged');
  97. $this->assertEqual($before->getOwner()->id(), $after->getOwner()->id(), t('File owner is the same: %file1 == %file2.', array('%file1' => $before->getOwner()->id(), '%file2' => $after->getOwner()->id())), 'File unchanged');
  98. $this->assertEqual($before->getFilename(), $after->getFilename(), t('File name is the same: %file1 == %file2.', array('%file1' => $before->getFilename(), '%file2' => $after->getFilename())), 'File unchanged');
  99. $this->assertEqual($before->getFileUri(), $after->getFileUri(), t('File path is the same: %file1 == %file2.', array('%file1' => $before->getFileUri(), '%file2' => $after->getFileUri())), 'File unchanged');
  100. $this->assertEqual($before->getMimeType(), $after->getMimeType(), t('File MIME type is the same: %file1 == %file2.', array('%file1' => $before->getMimeType(), '%file2' => $after->getMimeType())), 'File unchanged');
  101. $this->assertEqual($before->getSize(), $after->getSize(), t('File size is the same: %file1 == %file2.', array('%file1' => $before->getSize(), '%file2' => $after->getSize())), 'File unchanged');
  102. $this->assertEqual($before->isPermanent(), $after->isPermanent(), t('File status is the same: %file1 == %file2.', array('%file1' => $before->isPermanent(), '%file2' => $after->isPermanent())), 'File unchanged');
  103. }
  104. /**
  105. * Asserts that two files are not the same by comparing the fid and filepath.
  106. *
  107. * @param \Drupal\file\FileInterface $file1
  108. * File object to compare.
  109. * @param \Drupal\file\FileInterface $file2
  110. * File object to compare.
  111. */
  112. function assertDifferentFile(FileInterface $file1, FileInterface $file2) {
  113. $this->assertNotEqual($file1->id(), $file2->id(), t('Files have different ids: %file1 != %file2.', array('%file1' => $file1->id(), '%file2' => $file2->id())), 'Different file');
  114. $this->assertNotEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have different paths: %file1 != %file2.', array('%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri())), 'Different file');
  115. }
  116. /**
  117. * Asserts that two files are the same by comparing the fid and filepath.
  118. *
  119. * @param \Drupal\file\FileInterface $file1
  120. * File object to compare.
  121. * @param \Drupal\file\FileInterface $file2
  122. * File object to compare.
  123. */
  124. function assertSameFile(FileInterface $file1, FileInterface $file2) {
  125. $this->assertEqual($file1->id(), $file2->id(), t('Files have the same ids: %file1 == %file2.', array('%file1' => $file1->id(), '%file2-fid' => $file2->id())), 'Same file');
  126. $this->assertEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have the same path: %file1 == %file2.', array('%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri())), 'Same file');
  127. }
  128. /**
  129. * Create a file and save it to the files table and assert that it occurs
  130. * correctly.
  131. *
  132. * @param string $filepath
  133. * Optional string specifying the file path. If none is provided then a
  134. * randomly named file will be created in the site's files directory.
  135. * @param string $contents
  136. * Optional contents to save into the file. If a NULL value is provided an
  137. * arbitrary string will be used.
  138. * @param string $scheme
  139. * Optional string indicating the stream scheme to use. Drupal core includes
  140. * public, private, and temporary. The public wrapper is the default.
  141. * @return \Drupal\file\FileInterface
  142. * File entity.
  143. */
  144. function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {
  145. // Don't count hook invocations caused by creating the file.
  146. \Drupal::state()->set('file_test.count_hook_invocations', FALSE);
  147. $file = File::create([
  148. 'uri' => $this->createUri($filepath, $contents, $scheme),
  149. 'uid' => 1,
  150. ]);
  151. $file->save();
  152. // Write the record directly rather than using the API so we don't invoke
  153. // the hooks.
  154. $this->assertTrue($file->id() > 0, 'The file was added to the database.', 'Create test file');
  155. \Drupal::state()->set('file_test.count_hook_invocations', TRUE);
  156. return $file;
  157. }
  158. /**
  159. * Creates a file and returns its URI.
  160. *
  161. * @param string $filepath
  162. * Optional string specifying the file path. If none is provided then a
  163. * randomly named file will be created in the site's files directory.
  164. * @param string $contents
  165. * Optional contents to save into the file. If a NULL value is provided an
  166. * arbitrary string will be used.
  167. * @param string $scheme
  168. * Optional string indicating the stream scheme to use. Drupal core includes
  169. * public, private, and temporary. The public wrapper is the default.
  170. *
  171. * @return string
  172. * File URI.
  173. */
  174. function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
  175. if (!isset($filepath)) {
  176. // Prefix with non-latin characters to ensure that all file-related
  177. // tests work with international filenames.
  178. $filepath = 'Файл для тестирования ' . $this->randomMachineName();
  179. }
  180. if (!isset($scheme)) {
  181. $scheme = file_default_scheme();
  182. }
  183. $filepath = $scheme . '://' . $filepath;
  184. if (!isset($contents)) {
  185. $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
  186. }
  187. file_put_contents($filepath, $contents);
  188. $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');
  189. return $filepath;
  190. }
  191. }