More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / file / src / Tests / FileTokenReplaceTest.php
1 <?php
2
3 namespace Drupal\file\Tests;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Render\BubbleableMetadata;
7 use Drupal\file\Entity\File;
8
9 /**
10  * Generates text using placeholders for dummy content to check file token
11  * replacement.
12  *
13  * @group file
14  */
15 class FileTokenReplaceTest extends FileFieldTestBase {
16   /**
17    * Creates a file, then tests the tokens generated from it.
18    */
19   public function testFileTokenReplacement() {
20     $node_storage = $this->container->get('entity.manager')->getStorage('node');
21     $token_service = \Drupal::token();
22     $language_interface = \Drupal::languageManager()->getCurrentLanguage();
23
24     // Create file field.
25     $type_name = 'article';
26     $field_name = 'field_' . strtolower($this->randomMachineName());
27     $this->createFileField($field_name, 'node', $type_name);
28
29     $test_file = $this->getTestFile('text');
30     // Coping a file to test uploads with non-latin filenames.
31     $filename = drupal_dirname($test_file->getFileUri()) . '/текстовый файл.txt';
32     $test_file = file_copy($test_file, $filename);
33
34     // Create a new node with the uploaded file.
35     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
36
37     // Load the node and the file.
38     $node_storage->resetCache([$nid]);
39     $node = $node_storage->load($nid);
40     $file = File::load($node->{$field_name}->target_id);
41
42     // Generate and test sanitized tokens.
43     $tests = [];
44     $tests['[file:fid]'] = $file->id();
45     $tests['[file:name]'] = Html::escape($file->getFilename());
46     $tests['[file:path]'] = Html::escape($file->getFileUri());
47     $tests['[file:mime]'] = Html::escape($file->getMimeType());
48     $tests['[file:size]'] = format_size($file->getSize());
49     $tests['[file:url]'] = Html::escape(file_create_url($file->getFileUri()));
50     $tests['[file:created]'] = format_date($file->getCreatedTime(), 'medium', '', NULL, $language_interface->getId());
51     $tests['[file:created:short]'] = format_date($file->getCreatedTime(), 'short', '', NULL, $language_interface->getId());
52     $tests['[file:changed]'] = format_date($file->getChangedTime(), 'medium', '', NULL, $language_interface->getId());
53     $tests['[file:changed:short]'] = format_date($file->getChangedTime(), 'short', '', NULL, $language_interface->getId());
54     $tests['[file:owner]'] = Html::escape($this->adminUser->getDisplayName());
55     $tests['[file:owner:uid]'] = $file->getOwnerId();
56
57     $base_bubbleable_metadata = BubbleableMetadata::createFromObject($file);
58     $metadata_tests = [];
59     $metadata_tests['[file:fid]'] = $base_bubbleable_metadata;
60     $metadata_tests['[file:name]'] = $base_bubbleable_metadata;
61     $metadata_tests['[file:path]'] = $base_bubbleable_metadata;
62     $metadata_tests['[file:mime]'] = $base_bubbleable_metadata;
63     $metadata_tests['[file:size]'] = $base_bubbleable_metadata;
64     $bubbleable_metadata = clone $base_bubbleable_metadata;
65     $metadata_tests['[file:url]'] = $bubbleable_metadata->addCacheContexts(['url.site']);
66     $bubbleable_metadata = clone $base_bubbleable_metadata;
67     $metadata_tests['[file:created]'] = $bubbleable_metadata->addCacheTags(['rendered']);
68     $metadata_tests['[file:created:short]'] = $bubbleable_metadata;
69     $metadata_tests['[file:changed]'] = $bubbleable_metadata;
70     $metadata_tests['[file:changed:short]'] = $bubbleable_metadata;
71     $bubbleable_metadata = clone $base_bubbleable_metadata;
72     $metadata_tests['[file:owner]'] = $bubbleable_metadata->addCacheTags(['user:2']);
73     $metadata_tests['[file:owner:uid]'] = $bubbleable_metadata;
74
75     // Test to make sure that we generated something for each token.
76     $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
77
78     foreach ($tests as $input => $expected) {
79       $bubbleable_metadata = new BubbleableMetadata();
80       $output = $token_service->replace($input, ['file' => $file], ['langcode' => $language_interface->getId()], $bubbleable_metadata);
81       $this->assertEqual($output, $expected, format_string('Sanitized file token %token replaced.', ['%token' => $input]));
82       $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
83     }
84
85     // Generate and test unsanitized tokens.
86     $tests['[file:name]'] = $file->getFilename();
87     $tests['[file:path]'] = $file->getFileUri();
88     $tests['[file:mime]'] = $file->getMimeType();
89     $tests['[file:size]'] = format_size($file->getSize());
90
91     foreach ($tests as $input => $expected) {
92       $output = $token_service->replace($input, ['file' => $file], ['langcode' => $language_interface->getId(), 'sanitize' => FALSE]);
93       $this->assertEqual($output, $expected, format_string('Unsanitized file token %token replaced.', ['%token' => $input]));
94     }
95   }
96
97 }