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