Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / file / tests / src / Functional / FileFieldPathTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional;
4
5 use Drupal\file\Entity\File;
6
7 /**
8  * Tests that files are uploaded to proper locations.
9  *
10  * @group file
11  */
12 class FileFieldPathTest extends FileFieldTestBase {
13
14   /**
15    * Tests the normal formatter display on node display.
16    */
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');
25
26     // Create a new node.
27     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
28
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');
35     $expected_filename =
36       'public://' .
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()]));
41
42     // Change the path to contain multiple subdirectories.
43     $this->updateFileField($field_name, $type_name, ['file_directory' => 'foo/bar/baz']);
44
45     // Upload a new file into the subdirectories.
46     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
47
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()]));
53
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]']);
57
58     // Upload a new file into the token subdirectories.
59     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
60
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()]));
70   }
71
72   /**
73    * Asserts that a file is uploaded to the right location.
74    *
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.
82    */
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);
89
90     $result = preg_match('/' . preg_quote($base_path, '/') . '(_[0-9]+)?\.' . preg_quote($extension, '/') . '/', $actual_path);
91     $this->assertTrue($result, $message);
92   }
93
94 }