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