3 namespace Drupal\file\Tests;
5 use Drupal\file\Entity\File;
6 use Drupal\file\FileInterface;
7 use Drupal\simpletest\WebTestBase;
10 * Base class for file tests that use the file_test module to test uploads and
13 * @deprecated Scheduled for removal in Drupal 9.0.0.
14 * Use \Drupal\Tests\file\Functional\FileManagedTestBase instead.
16 abstract class FileManagedTestBase extends WebTestBase {
23 public static $modules = ['file_test', 'file'];
25 protected function setUp() {
27 // Clear out any hook calls.
32 * Assert that all of the specified hook_file_* hooks were called once, other
33 * values result in failure.
35 * @param string[] $expected
36 * An array of strings containing with the hook name; for example, 'load',
37 * 'save', 'insert', etc.
39 public function assertFileHooksCalled($expected) {
40 \Drupal::state()->resetCache();
42 // Determine which hooks were called.
43 $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.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)]));
51 $this->assertTrue(TRUE, format_string('All the expected hooks were called: %expected', ['%expected' => empty($expected) ? '(none)' : implode(', ', $expected)]));
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)]));
60 $this->assertTrue(TRUE, 'No unexpected hooks were called.');
65 * Assert that a hook_file_* hook was called a certain number of times.
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.
74 public function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) {
75 $actual_count = count(file_test_get_calls($hook));
77 if (!isset($message)) {
78 if ($actual_count == $expected_count) {
79 $message = format_string('hook_file_@name was called correctly.', ['@name' => $hook]);
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]);
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]);
88 $this->assertEqual($actual_count, $expected_count, $message);
92 * Asserts that two files have the same values (except timestamp).
94 * @param \Drupal\file\FileInterface $before
95 * File object to compare.
96 * @param \Drupal\file\FileInterface $after
97 * File object to compare.
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');
110 * Asserts that two files are not the same by comparing the fid and filepath.
112 * @param \Drupal\file\FileInterface $file1
113 * File object to compare.
114 * @param \Drupal\file\FileInterface $file2
115 * File object to compare.
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');
123 * Asserts that two files are the same by comparing the fid and filepath.
125 * @param \Drupal\file\FileInterface $file1
126 * File object to compare.
127 * @param \Drupal\file\FileInterface $file2
128 * File object to compare.
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');
136 * Create a file and save it to the files table and assert that it occurs
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
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),
159 // Write the record directly rather than using the API so we don't invoke
161 $this->assertTrue($file->id() > 0, 'The file was added to the database.', 'Create test file');
163 \Drupal::state()->set('file_test.count_hook_invocations', TRUE);
168 * Creates a file and returns its URI.
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.
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();
189 if (!isset($scheme)) {
190 $scheme = file_default_scheme();
192 $filepath = $scheme . '://' . $filepath;
194 if (!isset($contents)) {
195 $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
198 file_put_contents($filepath, $contents);
199 $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');