c68ae48e6e333accb658ebfcbdad4cb6e0459c28
[yaffs-website] / web / core / modules / editor / tests / src / Kernel / EditorFileReferenceFilterTest.php
1 <?php
2
3 namespace Drupal\Tests\editor\Kernel;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\file\Entity\File;
7 use Drupal\filter\FilterPluginCollection;
8 use Drupal\KernelTests\KernelTestBase;
9
10 /**
11  * Tests Editor module's file reference filter.
12  *
13  * @group editor
14  */
15 class EditorFileReferenceFilterTest extends KernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['system', 'filter', 'editor', 'field', 'file', 'user'];
23
24   /**
25    * @var \Drupal\filter\Plugin\FilterInterface[]
26    */
27   protected $filters;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34     $this->installConfig(['system']);
35     $this->installEntitySchema('file');
36     $this->installSchema('file', ['file_usage']);
37
38     $manager = $this->container->get('plugin.manager.filter');
39     $bag = new FilterPluginCollection($manager, []);
40     $this->filters = $bag->getAll();
41   }
42
43   /**
44    * Tests the editor file reference filter.
45    */
46   public function testEditorFileReferenceFilter() {
47     $filter = $this->filters['editor_file_reference'];
48
49     $test = function($input) use ($filter) {
50       return $filter->process($input, 'und');
51     };
52
53     file_put_contents('public://llama.jpg', $this->randomMachineName());
54     $image = File::create(['uri' => 'public://llama.jpg']);
55     $image->save();
56     $id = $image->id();
57     $uuid = $image->uuid();
58     $cache_tag = ['file:' . $id];
59
60     file_put_contents('public://alpaca.jpg', $this->randomMachineName());
61     $image_2 = File::create(['uri' => 'public://alpaca.jpg']);
62     $image_2->save();
63     $id_2 = $image_2->id();
64     $uuid_2 = $image_2->uuid();
65     $cache_tag_2 = ['file:' . $id_2];
66
67     $this->pass('No data-entity-type and no data-entity-uuid attribute.');
68     $input = '<img src="llama.jpg" />';
69     $output = $test($input);
70     $this->assertIdentical($input, $output->getProcessedText());
71
72     $this->pass('A non-file data-entity-type attribute value.');
73     $input = '<img src="llama.jpg" data-entity-type="invalid-entity-type-value" data-entity-uuid="' . $uuid . '" />';
74     $output = $test($input);
75     $this->assertIdentical($input, $output->getProcessedText());
76
77     $this->pass('One data-entity-uuid attribute.');
78     $input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
79     $expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
80     $output = $test($input);
81     $this->assertIdentical($expected_output, $output->getProcessedText());
82     $this->assertEqual($cache_tag, $output->getCacheTags());
83
84     $this->pass('One data-entity-uuid attribute with odd capitalization.');
85     $input = '<img src="llama.jpg" data-entity-type="file" DATA-entity-UUID =   "' . $uuid . '" />';
86     $expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
87     $output = $test($input);
88     $this->assertIdentical($expected_output, $output->getProcessedText());
89     $this->assertEqual($cache_tag, $output->getCacheTags());
90
91     $this->pass('One data-entity-uuid attribute on a non-image tag.');
92     $input = '<video src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
93     $expected_output = '<video src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '"></video>';
94     $output = $test($input);
95     $this->assertIdentical($expected_output, $output->getProcessedText());
96     $this->assertEqual($cache_tag, $output->getCacheTags());
97
98     $this->pass('One data-entity-uuid attribute with an invalid value.');
99     $input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="invalid-' . $uuid . '" />';
100     $output = $test($input);
101     $this->assertIdentical($input, $output->getProcessedText());
102     $this->assertEqual([], $output->getCacheTags());
103
104     $this->pass('Two different data-entity-uuid attributes.');
105     $input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
106     $input .= '<img src="alpaca.jpg" data-entity-type="file" data-entity-uuid="' . $uuid_2 . '" />';
107     $expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
108     $expected_output .= '<img src="/' . $this->siteDirectory . '/files/alpaca.jpg" data-entity-type="file" data-entity-uuid="' . $uuid_2 . '" />';
109     $output = $test($input);
110     $this->assertIdentical($expected_output, $output->getProcessedText());
111     $this->assertEqual(Cache::mergeTags($cache_tag, $cache_tag_2), $output->getCacheTags());
112
113     $this->pass('Two identical  data-entity-uuid attributes.');
114     $input = '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
115     $input .= '<img src="llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
116     $expected_output = '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
117     $expected_output .= '<img src="/' . $this->siteDirectory . '/files/llama.jpg" data-entity-type="file" data-entity-uuid="' . $uuid . '" />';
118     $output = $test($input);
119     $this->assertIdentical($expected_output, $output->getProcessedText());
120     $this->assertEqual($cache_tag, $output->getCacheTags());
121   }
122
123 }