More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / file / src / Tests / FileFieldPathTest.php
1 <?php
2
3 namespace Drupal\file\Tests;
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    * Tests the normal formatter display on node display.
15    */
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');
24
25     // Create a new node.
26     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
27
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');
34     $expected_filename =
35       'public://' .
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()]));
40
41     // Change the path to contain multiple subdirectories.
42     $this->updateFileField($field_name, $type_name, ['file_directory' => 'foo/bar/baz']);
43
44     // Upload a new file into the subdirectories.
45     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
46
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()]));
52
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]']);
56
57     // Upload a new file into the token subdirectories.
58     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
59
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()]));
69   }
70
71   /**
72    * Asserts that a file is uploaded to the right location.
73    *
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.
81    */
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);
88
89     $result = preg_match('/' . preg_quote($base_path, '/') . '(_[0-9]+)?\.' . preg_quote($extension, '/') . '/', $actual_path);
90     $this->assertTrue($result, $message);
91   }
92
93 }