PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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