3 namespace Drupal\Tests\file\Functional;
5 use Drupal\file\Entity\File;
6 use Drupal\file\FileInterface;
7 use Drupal\Tests\BrowserTestBase;
10 * Base class for file tests that use the file_test module to test uploads and
13 abstract class FileManagedTestBase extends BrowserTestBase {
20 public static $modules = ['file_test', 'file'];
22 protected function setUp() {
24 // Clear out any hook calls.
29 * Assert that all of the specified hook_file_* hooks were called once, other
30 * values result in failure.
32 * @param string[] $expected
33 * An array of strings containing with the hook name; for example, 'load',
34 * 'save', 'insert', etc.
36 public function assertFileHooksCalled($expected) {
37 \Drupal::state()->resetCache();
39 // Determine which hooks were called.
40 $actual = array_keys(array_filter(file_test_get_all_calls()));
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)]));
48 $this->assertTrue(TRUE, format_string('All the expected hooks were called: %expected', ['%expected' => empty($expected) ? '(none)' : implode(', ', $expected)]));
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)]));
57 $this->assertTrue(TRUE, 'No unexpected hooks were called.');
62 * Assert that a hook_file_* hook was called a certain number of times.
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.
71 public function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) {
72 $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.', ['@name' => $hook]);
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]);
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]);
85 $this->assertEqual($actual_count, $expected_count, $message);
89 * Asserts that two files have the same values (except timestamp).
91 * @param \Drupal\file\FileInterface $before
92 * File object to compare.
93 * @param \Drupal\file\FileInterface $after
94 * File object to compare.
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');
107 * Asserts that two files are not the same by comparing the fid and filepath.
109 * @param \Drupal\file\FileInterface $file1
110 * File object to compare.
111 * @param \Drupal\file\FileInterface $file2
112 * File object to compare.
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');
120 * Asserts that two files are the same by comparing the fid and filepath.
122 * @param \Drupal\file\FileInterface $file1
123 * File object to compare.
124 * @param \Drupal\file\FileInterface $file2
125 * File object to compare.
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');
133 * Create a file and save it to the files table and assert that it occurs
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
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),
156 // Write the record directly rather than using the API so we don't invoke
158 $this->assertTrue($file->id() > 0, 'The file was added to the database.', 'Create test file');
160 \Drupal::state()->set('file_test.count_hook_invocations', TRUE);
165 * Creates a file and returns its URI.
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.
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();
186 if (!isset($scheme)) {
187 $scheme = file_default_scheme();
189 $filepath = $scheme . '://' . $filepath;
191 if (!isset($contents)) {
192 $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
195 file_put_contents($filepath, $contents);
196 $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');