3 namespace Drupal\file\Tests;
5 @trigger_error('The ' . __NAMESPACE__ . '\FileManagedTestBase is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\file\Functional\FileManagedTestBase. See https://www.drupal.org/node/2969361.', E_USER_DEPRECATED);
7 use Drupal\file\Entity\File;
8 use Drupal\file\FileInterface;
9 use Drupal\simpletest\WebTestBase;
12 * Base class for file tests that use the file_test module to test uploads and
15 * @deprecated Scheduled for removal in Drupal 9.0.0.
16 * Use \Drupal\Tests\file\Functional\FileManagedTestBase instead.
18 abstract class FileManagedTestBase extends WebTestBase {
25 public static $modules = ['file_test', 'file'];
27 protected function setUp() {
29 // Clear out any hook calls.
34 * Assert that all of the specified hook_file_* hooks were called once, other
35 * values result in failure.
37 * @param string[] $expected
38 * An array of strings containing with the hook name; for example, 'load',
39 * 'save', 'insert', etc.
41 public function assertFileHooksCalled($expected) {
42 \Drupal::state()->resetCache();
44 // Determine which hooks were called.
45 $actual = array_keys(array_filter(file_test_get_all_calls()));
47 // Determine if there were any expected that were not called.
48 $uncalled = array_diff($expected, $actual);
49 if (count($uncalled)) {
50 $this->assertTrue(FALSE, format_string('Expected hooks %expected to be called but %uncalled was not called.', ['%expected' => implode(', ', $expected), '%uncalled' => implode(', ', $uncalled)]));
53 $this->assertTrue(TRUE, format_string('All the expected hooks were called: %expected', ['%expected' => empty($expected) ? '(none)' : implode(', ', $expected)]));
56 // Determine if there were any unexpected calls.
57 $unexpected = array_diff($actual, $expected);
58 if (count($unexpected)) {
59 $this->assertTrue(FALSE, format_string('Unexpected hooks were called: %unexpected.', ['%unexpected' => empty($unexpected) ? '(none)' : implode(', ', $unexpected)]));
62 $this->assertTrue(TRUE, 'No unexpected hooks were called.');
67 * Assert that a hook_file_* hook was called a certain number of times.
70 * String with the hook name; for instance, 'load', 'save', 'insert', etc.
71 * @param int $expected_count
72 * Optional integer count.
73 * @param string|null $message
74 * Optional translated string message.
76 public function assertFileHookCalled($hook, $expected_count = 1, $message = NULL) {
77 $actual_count = count(file_test_get_calls($hook));
79 if (!isset($message)) {
80 if ($actual_count == $expected_count) {
81 $message = format_string('hook_file_@name was called correctly.', ['@name' => $hook]);
83 elseif ($expected_count == 0) {
84 $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]);
87 $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]);
90 $this->assertEqual($actual_count, $expected_count, $message);
94 * Asserts that two files have the same values (except timestamp).
96 * @param \Drupal\file\FileInterface $before
97 * File object to compare.
98 * @param \Drupal\file\FileInterface $after
99 * File object to compare.
101 public function assertFileUnchanged(FileInterface $before, FileInterface $after) {
102 $this->assertEqual($before->id(), $after->id(), t('File id is the same: %file1 == %file2.', ['%file1' => $before->id(), '%file2' => $after->id()]), 'File unchanged');
103 $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');
104 $this->assertEqual($before->getFilename(), $after->getFilename(), t('File name is the same: %file1 == %file2.', ['%file1' => $before->getFilename(), '%file2' => $after->getFilename()]), 'File unchanged');
105 $this->assertEqual($before->getFileUri(), $after->getFileUri(), t('File path is the same: %file1 == %file2.', ['%file1' => $before->getFileUri(), '%file2' => $after->getFileUri()]), 'File unchanged');
106 $this->assertEqual($before->getMimeType(), $after->getMimeType(), t('File MIME type is the same: %file1 == %file2.', ['%file1' => $before->getMimeType(), '%file2' => $after->getMimeType()]), 'File unchanged');
107 $this->assertEqual($before->getSize(), $after->getSize(), t('File size is the same: %file1 == %file2.', ['%file1' => $before->getSize(), '%file2' => $after->getSize()]), 'File unchanged');
108 $this->assertEqual($before->isPermanent(), $after->isPermanent(), t('File status is the same: %file1 == %file2.', ['%file1' => $before->isPermanent(), '%file2' => $after->isPermanent()]), 'File unchanged');
112 * Asserts that two files are not the same by comparing the fid and filepath.
114 * @param \Drupal\file\FileInterface $file1
115 * File object to compare.
116 * @param \Drupal\file\FileInterface $file2
117 * File object to compare.
119 public function assertDifferentFile(FileInterface $file1, FileInterface $file2) {
120 $this->assertNotEqual($file1->id(), $file2->id(), t('Files have different ids: %file1 != %file2.', ['%file1' => $file1->id(), '%file2' => $file2->id()]), 'Different file');
121 $this->assertNotEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have different paths: %file1 != %file2.', ['%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri()]), 'Different file');
125 * Asserts that two files are the same by comparing the fid and filepath.
127 * @param \Drupal\file\FileInterface $file1
128 * File object to compare.
129 * @param \Drupal\file\FileInterface $file2
130 * File object to compare.
132 public function assertSameFile(FileInterface $file1, FileInterface $file2) {
133 $this->assertEqual($file1->id(), $file2->id(), t('Files have the same ids: %file1 == %file2.', ['%file1' => $file1->id(), '%file2-fid' => $file2->id()]), 'Same file');
134 $this->assertEqual($file1->getFileUri(), $file2->getFileUri(), t('Files have the same path: %file1 == %file2.', ['%file1' => $file1->getFileUri(), '%file2' => $file2->getFileUri()]), 'Same file');
138 * Create a file and save it to the files table and assert that it occurs
141 * @param string $filepath
142 * Optional string specifying the file path. If none is provided then a
143 * randomly named file will be created in the site's files directory.
144 * @param string $contents
145 * Optional contents to save into the file. If a NULL value is provided an
146 * arbitrary string will be used.
147 * @param string $scheme
148 * Optional string indicating the stream scheme to use. Drupal core includes
149 * public, private, and temporary. The public wrapper is the default.
150 * @return \Drupal\file\FileInterface
153 public function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {
154 // Don't count hook invocations caused by creating the file.
155 \Drupal::state()->set('file_test.count_hook_invocations', FALSE);
156 $file = File::create([
157 'uri' => $this->createUri($filepath, $contents, $scheme),
161 // Write the record directly rather than using the API so we don't invoke
163 $this->assertTrue($file->id() > 0, 'The file was added to the database.', 'Create test file');
165 \Drupal::state()->set('file_test.count_hook_invocations', TRUE);
170 * Creates a file and returns its URI.
172 * @param string $filepath
173 * Optional string specifying the file path. If none is provided then a
174 * randomly named file will be created in the site's files directory.
175 * @param string $contents
176 * Optional contents to save into the file. If a NULL value is provided an
177 * arbitrary string will be used.
178 * @param string $scheme
179 * Optional string indicating the stream scheme to use. Drupal core includes
180 * public, private, and temporary. The public wrapper is the default.
185 public function createUri($filepath = NULL, $contents = NULL, $scheme = NULL) {
186 if (!isset($filepath)) {
187 // Prefix with non-latin characters to ensure that all file-related
188 // tests work with international filenames.
189 $filepath = 'Файл для тестирования ' . $this->randomMachineName();
191 if (!isset($scheme)) {
192 $scheme = file_default_scheme();
194 $filepath = $scheme . '://' . $filepath;
196 if (!isset($contents)) {
197 $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
200 file_put_contents($filepath, $contents);
201 $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');