3 namespace Drupal\Tests\editor\Kernel;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\file\Entity\File;
7 use Drupal\filter\FilterPluginCollection;
8 use Drupal\KernelTests\KernelTestBase;
11 * Tests Editor module's file reference filter.
15 class EditorFileReferenceFilterTest extends KernelTestBase {
22 public static $modules = ['system', 'filter', 'editor', 'field', 'file', 'user'];
25 * @var \Drupal\filter\Plugin\FilterInterface[]
32 protected function setUp() {
34 $this->installConfig(['system']);
35 $this->installEntitySchema('file');
36 $this->installSchema('file', ['file_usage']);
38 $manager = $this->container->get('plugin.manager.filter');
39 $bag = new FilterPluginCollection($manager, []);
40 $this->filters = $bag->getAll();
44 * Tests the editor file reference filter.
46 public function testEditorFileReferenceFilter() {
47 $filter = $this->filters['editor_file_reference'];
49 $test = function($input) use ($filter) {
50 return $filter->process($input, 'und');
53 file_put_contents('public://llama.jpg', $this->randomMachineName());
54 $image = File::create(['uri' => 'public://llama.jpg']);
57 $uuid = $image->uuid();
58 $cache_tag = ['file:' . $id];
60 file_put_contents('public://alpaca.jpg', $this->randomMachineName());
61 $image_2 = File::create(['uri' => 'public://alpaca.jpg']);
63 $id_2 = $image_2->id();
64 $uuid_2 = $image_2->uuid();
65 $cache_tag_2 = ['file:' . $id_2];
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());
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());
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());
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());
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());
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());
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());
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());