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