3 namespace Drupal\Tests\file\Functional;
5 use Drupal\file\Entity\File;
8 * Tests that files are uploaded to proper locations.
12 class FileFieldPathTest extends FileFieldTestBase {
15 * Tests the normal formatter display on node display.
17 public function testUploadPath() {
18 /** @var \Drupal\node\NodeStorageInterface $node_storage */
19 $node_storage = $this->container->get('entity.manager')->getStorage('node');
20 $field_name = strtolower($this->randomMachineName());
21 $type_name = 'article';
22 $this->createFileField($field_name, 'node', $type_name);
23 /** @var \Drupal\file\FileInterface $test_file */
24 $test_file = $this->getTestFile('text');
27 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
29 // Check that the file was uploaded to the correct location.
30 $node_storage->resetCache([$nid]);
31 $node = $node_storage->load($nid);
32 /** @var \Drupal\file\FileInterface $node_file */
33 $node_file = $node->{$field_name}->entity;
34 $date_formatter = $this->container->get('date.formatter');
37 $date_formatter->format(REQUEST_TIME, 'custom', 'Y') . '-' .
38 $date_formatter->format(REQUEST_TIME, 'custom', 'm') . '/' .
39 $test_file->getFilename();
40 $this->assertPathMatch($expected_filename, $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', ['%file' => $node_file->getFileUri()]));
42 // Change the path to contain multiple subdirectories.
43 $this->updateFileField($field_name, $type_name, ['file_directory' => 'foo/bar/baz']);
45 // Upload a new file into the subdirectories.
46 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
48 // Check that the file was uploaded into the subdirectory.
49 $node_storage->resetCache([$nid]);
50 $node = $node_storage->load($nid);
51 $node_file = File::load($node->{$field_name}->target_id);
52 $this->assertPathMatch('public://foo/bar/baz/' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path.', ['%file' => $node_file->getFileUri()]));
54 // Check the path when used with tokens.
55 // Change the path to contain multiple token directories.
56 $this->updateFileField($field_name, $type_name, ['file_directory' => '[current-user:uid]/[current-user:name]']);
58 // Upload a new file into the token subdirectories.
59 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
61 // Check that the file was uploaded into the subdirectory.
62 $node_storage->resetCache([$nid]);
63 $node = $node_storage->load($nid);
64 $node_file = File::load($node->{$field_name}->target_id);
65 // Do token replacement using the same user which uploaded the file, not
66 // the user running the test case.
67 $data = ['user' => $this->adminUser];
68 $subdirectory = \Drupal::token()->replace('[user:uid]/[user:name]', $data);
69 $this->assertPathMatch('public://' . $subdirectory . '/' . $test_file->getFilename(), $node_file->getFileUri(), format_string('The file %file was uploaded to the correct path with token replacements.', ['%file' => $node_file->getFileUri()]));
73 * Asserts that a file is uploaded to the right location.
75 * @param string $expected_path
76 * The location where the file is expected to be uploaded. Duplicate file
77 * names to not need to be taken into account.
78 * @param string $actual_path
79 * Where the file was actually uploaded.
80 * @param string $message
81 * The message to display with this assertion.
83 public function assertPathMatch($expected_path, $actual_path, $message) {
84 // Strip off the extension of the expected path to allow for _0, _1, etc.
85 // suffixes when the file hits a duplicate name.
86 $pos = strrpos($expected_path, '.');
87 $base_path = substr($expected_path, 0, $pos);
88 $extension = substr($expected_path, $pos + 1);
90 $result = preg_match('/' . preg_quote($base_path, '/') . '(_[0-9]+)?\.' . preg_quote($extension, '/') . '/', $actual_path);
91 $this->assertTrue($result, $message);