85de742eb648960dd27c9ee32f28824e6b9c47c1
[yaffs-website] / web / core / modules / filter / tests / src / Unit / FilterUninstallValidatorTest.php
1 <?php
2
3 namespace Drupal\Tests\filter\Unit;
4
5 use Drupal\simpletest\AssertHelperTrait;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\filter\FilterUninstallValidator
10  * @group filter
11  */
12 class FilterUninstallValidatorTest extends UnitTestCase {
13
14   use AssertHelperTrait;
15
16   /**
17    * @var \Drupal\filter\FilterUninstallValidator|\PHPUnit_Framework_MockObject_MockObject
18    */
19   protected $filterUninstallValidator;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     $this->filterUninstallValidator = $this->getMockBuilder('Drupal\filter\FilterUninstallValidator')
27       ->disableOriginalConstructor()
28       ->setMethods(['getFilterDefinitionsByProvider', 'getEnabledFilterFormats'])
29       ->getMock();
30     $this->filterUninstallValidator->setStringTranslation($this->getStringTranslationStub());
31   }
32
33   /**
34    * @covers ::validate
35    */
36   public function testValidateNoPlugins() {
37     $this->filterUninstallValidator->expects($this->once())
38       ->method('getFilterDefinitionsByProvider')
39       ->willReturn([]);
40     $this->filterUninstallValidator->expects($this->never())
41       ->method('getEnabledFilterFormats');
42
43     $module = $this->randomMachineName();
44     $expected = [];
45     $reasons = $this->filterUninstallValidator->validate($module);
46     $this->assertSame($expected, $this->castSafeStrings($reasons));
47   }
48
49   /**
50    * @covers ::validate
51    */
52   public function testValidateNoFormats() {
53     $this->filterUninstallValidator->expects($this->once())
54       ->method('getFilterDefinitionsByProvider')
55       ->willReturn([
56         'test_filter_plugin' => [
57           'id' => 'test_filter_plugin',
58           'provider' => 'filter_test',
59         ],
60       ]);
61     $this->filterUninstallValidator->expects($this->once())
62       ->method('getEnabledFilterFormats')
63       ->willReturn([]);
64
65     $module = $this->randomMachineName();
66     $expected = [];
67     $reasons = $this->filterUninstallValidator->validate($module);
68     $this->assertSame($expected, $this->castSafeStrings($reasons));
69   }
70
71   /**
72    * @covers ::validate
73    */
74   public function testValidateNoMatchingFormats() {
75     $this->filterUninstallValidator->expects($this->once())
76       ->method('getFilterDefinitionsByProvider')
77       ->willReturn([
78         'test_filter_plugin1' => [
79           'id' => 'test_filter_plugin1',
80           'provider' => 'filter_test',
81         ],
82         'test_filter_plugin2' => [
83           'id' => 'test_filter_plugin2',
84           'provider' => 'filter_test',
85         ],
86         'test_filter_plugin3' => [
87           'id' => 'test_filter_plugin3',
88           'provider' => 'filter_test',
89         ],
90         'test_filter_plugin4' => [
91           'id' => 'test_filter_plugin4',
92           'provider' => 'filter_test',
93         ],
94       ]);
95
96     $filter_plugin_enabled = $this->getMockForAbstractClass('Drupal\filter\Plugin\FilterBase', [['status' => TRUE], '', ['provider' => 'filter_test']]);
97     $filter_plugin_disabled = $this->getMockForAbstractClass('Drupal\filter\Plugin\FilterBase', [['status' => FALSE], '', ['provider' => 'filter_test']]);
98
99     // The first format has 2 matching and enabled filters, but the loop breaks
100     // after finding the first one.
101     $filter_plugin_collection1 = $this->getMockBuilder('Drupal\filter\FilterPluginCollection')
102       ->disableOriginalConstructor()
103       ->getMock();
104     $filter_plugin_collection1->expects($this->exactly(3))
105       ->method('has')
106       ->willReturnMap([
107         ['test_filter_plugin1', FALSE],
108         ['test_filter_plugin2', TRUE],
109         ['test_filter_plugin3', TRUE],
110         ['test_filter_plugin4', TRUE],
111       ]);
112     $filter_plugin_collection1->expects($this->exactly(2))
113       ->method('get')
114       ->willReturnMap([
115         ['test_filter_plugin2', $filter_plugin_disabled],
116         ['test_filter_plugin3', $filter_plugin_enabled],
117         ['test_filter_plugin4', $filter_plugin_enabled],
118       ]);
119
120     $filter_format1 = $this->getMock('Drupal\filter\FilterFormatInterface');
121     $filter_format1->expects($this->once())
122       ->method('filters')
123       ->willReturn($filter_plugin_collection1);
124     $filter_format1->expects($this->once())
125       ->method('label')
126       ->willReturn('Filter Format 1 Label');
127
128     // The second filter format only has one matching and enabled filter.
129     $filter_plugin_collection2 = $this->getMockBuilder('Drupal\filter\FilterPluginCollection')
130       ->disableOriginalConstructor()
131       ->getMock();
132     $filter_plugin_collection2->expects($this->exactly(4))
133       ->method('has')
134       ->willReturnMap([
135         ['test_filter_plugin1', FALSE],
136         ['test_filter_plugin2', FALSE],
137         ['test_filter_plugin3', FALSE],
138         ['test_filter_plugin4', TRUE],
139       ]);
140     $filter_plugin_collection2->expects($this->exactly(1))
141       ->method('get')
142       ->with('test_filter_plugin4')
143       ->willReturn($filter_plugin_enabled);
144
145     $filter_format2 = $this->getMock('Drupal\filter\FilterFormatInterface');
146     $filter_format2->expects($this->once())
147       ->method('filters')
148       ->willReturn($filter_plugin_collection2);
149     $filter_format2->expects($this->once())
150       ->method('label')
151       ->willReturn('Filter Format 2 Label');
152     $this->filterUninstallValidator->expects($this->once())
153       ->method('getEnabledFilterFormats')
154       ->willReturn([
155         'test_filter_format1' => $filter_format1,
156         'test_filter_format2' => $filter_format2,
157       ]);
158
159     $expected = [
160       'Provides a filter plugin that is in use in the following filter formats: <em class="placeholder">Filter Format 1 Label, Filter Format 2 Label</em>'
161     ];
162     $reasons = $this->filterUninstallValidator->validate($this->randomMachineName());
163     $this->assertSame($expected, $this->castSafeStrings($reasons));
164   }
165
166 }