3 namespace Drupal\Tests\filter\Unit;
5 use Drupal\simpletest\AssertHelperTrait;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\filter\FilterUninstallValidator
12 class FilterUninstallValidatorTest extends UnitTestCase {
14 use AssertHelperTrait;
17 * @var \Drupal\filter\FilterUninstallValidator|\PHPUnit_Framework_MockObject_MockObject
19 protected $filterUninstallValidator;
24 protected function setUp() {
26 $this->filterUninstallValidator = $this->getMockBuilder('Drupal\filter\FilterUninstallValidator')
27 ->disableOriginalConstructor()
28 ->setMethods(['getFilterDefinitionsByProvider', 'getEnabledFilterFormats'])
30 $this->filterUninstallValidator->setStringTranslation($this->getStringTranslationStub());
36 public function testValidateNoPlugins() {
37 $this->filterUninstallValidator->expects($this->once())
38 ->method('getFilterDefinitionsByProvider')
40 $this->filterUninstallValidator->expects($this->never())
41 ->method('getEnabledFilterFormats');
43 $module = $this->randomMachineName();
45 $reasons = $this->filterUninstallValidator->validate($module);
46 $this->assertSame($expected, $this->castSafeStrings($reasons));
52 public function testValidateNoFormats() {
53 $this->filterUninstallValidator->expects($this->once())
54 ->method('getFilterDefinitionsByProvider')
56 'test_filter_plugin' => [
57 'id' => 'test_filter_plugin',
58 'provider' => 'filter_test',
61 $this->filterUninstallValidator->expects($this->once())
62 ->method('getEnabledFilterFormats')
65 $module = $this->randomMachineName();
67 $reasons = $this->filterUninstallValidator->validate($module);
68 $this->assertSame($expected, $this->castSafeStrings($reasons));
74 public function testValidateNoMatchingFormats() {
75 $this->filterUninstallValidator->expects($this->once())
76 ->method('getFilterDefinitionsByProvider')
78 'test_filter_plugin1' => [
79 'id' => 'test_filter_plugin1',
80 'provider' => 'filter_test',
82 'test_filter_plugin2' => [
83 'id' => 'test_filter_plugin2',
84 'provider' => 'filter_test',
86 'test_filter_plugin3' => [
87 'id' => 'test_filter_plugin3',
88 'provider' => 'filter_test',
90 'test_filter_plugin4' => [
91 'id' => 'test_filter_plugin4',
92 'provider' => 'filter_test',
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']]);
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()
104 $filter_plugin_collection1->expects($this->exactly(3))
107 ['test_filter_plugin1', FALSE],
108 ['test_filter_plugin2', TRUE],
109 ['test_filter_plugin3', TRUE],
110 ['test_filter_plugin4', TRUE],
112 $filter_plugin_collection1->expects($this->exactly(2))
115 ['test_filter_plugin2', $filter_plugin_disabled],
116 ['test_filter_plugin3', $filter_plugin_enabled],
117 ['test_filter_plugin4', $filter_plugin_enabled],
120 $filter_format1 = $this->getMock('Drupal\filter\FilterFormatInterface');
121 $filter_format1->expects($this->once())
123 ->willReturn($filter_plugin_collection1);
124 $filter_format1->expects($this->once())
126 ->willReturn('Filter Format 1 Label');
128 // The second filter format only has one matching and enabled filter.
129 $filter_plugin_collection2 = $this->getMockBuilder('Drupal\filter\FilterPluginCollection')
130 ->disableOriginalConstructor()
132 $filter_plugin_collection2->expects($this->exactly(4))
135 ['test_filter_plugin1', FALSE],
136 ['test_filter_plugin2', FALSE],
137 ['test_filter_plugin3', FALSE],
138 ['test_filter_plugin4', TRUE],
140 $filter_plugin_collection2->expects($this->exactly(1))
142 ->with('test_filter_plugin4')
143 ->willReturn($filter_plugin_enabled);
145 $filter_format2 = $this->getMock('Drupal\filter\FilterFormatInterface');
146 $filter_format2->expects($this->once())
148 ->willReturn($filter_plugin_collection2);
149 $filter_format2->expects($this->once())
151 ->willReturn('Filter Format 2 Label');
152 $this->filterUninstallValidator->expects($this->once())
153 ->method('getEnabledFilterFormats')
155 'test_filter_format1' => $filter_format1,
156 'test_filter_format2' => $filter_format2,
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>'
162 $reasons = $this->filterUninstallValidator->validate($this->randomMachineName());
163 $this->assertSame($expected, $this->castSafeStrings($reasons));