Version 1
[yaffs-website] / web / core / modules / forum / tests / src / Unit / ForumUninstallValidatorTest.php
1 <?php
2
3 namespace Drupal\Tests\forum\Unit;
4
5 use Drupal\simpletest\AssertHelperTrait;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\forum\ForumUninstallValidator
10  * @group forum
11  */
12 class ForumUninstallValidatorTest extends UnitTestCase {
13
14   use AssertHelperTrait;
15
16   /**
17    * @var \Drupal\forum\ForumUninstallValidator|\PHPUnit_Framework_MockObject_MockObject
18    */
19   protected $forumUninstallValidator;
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     $this->forumUninstallValidator = $this->getMockBuilder('Drupal\forum\ForumUninstallValidator')
27       ->disableOriginalConstructor()
28       ->setMethods(['hasForumNodes', 'hasTermsForVocabulary', 'getForumVocabulary'])
29       ->getMock();
30     $this->forumUninstallValidator->setStringTranslation($this->getStringTranslationStub());
31   }
32
33   /**
34    * @covers ::validate
35    */
36   public function testValidateNotForum() {
37     $this->forumUninstallValidator->expects($this->never())
38       ->method('hasForumNodes');
39     $this->forumUninstallValidator->expects($this->never())
40       ->method('hasTermsForVocabulary');
41     $this->forumUninstallValidator->expects($this->never())
42       ->method('getForumVocabulary');
43
44     $module = 'not_forum';
45     $expected = [];
46     $reasons = $this->forumUninstallValidator->validate($module);
47     $this->assertSame($expected, $this->castSafeStrings($reasons));
48   }
49
50   /**
51    * @covers ::validate
52    */
53   public function testValidate() {
54     $this->forumUninstallValidator->expects($this->once())
55       ->method('hasForumNodes')
56       ->willReturn(FALSE);
57
58     $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
59     $this->forumUninstallValidator->expects($this->once())
60       ->method('getForumVocabulary')
61       ->willReturn($vocabulary);
62
63     $this->forumUninstallValidator->expects($this->once())
64       ->method('hasTermsForVocabulary')
65       ->willReturn(FALSE);
66
67     $module = 'forum';
68     $expected = [];
69     $reasons = $this->forumUninstallValidator->validate($module);
70     $this->assertSame($expected, $this->castSafeStrings($reasons));
71   }
72
73   /**
74    * @covers ::validate
75    */
76   public function testValidateHasForumNodes() {
77     $this->forumUninstallValidator->expects($this->once())
78       ->method('hasForumNodes')
79       ->willReturn(TRUE);
80
81     $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
82     $this->forumUninstallValidator->expects($this->once())
83       ->method('getForumVocabulary')
84       ->willReturn($vocabulary);
85
86     $this->forumUninstallValidator->expects($this->once())
87       ->method('hasTermsForVocabulary')
88       ->willReturn(FALSE);
89
90     $module = 'forum';
91     $expected = [
92       'To uninstall Forum, first delete all <em>Forum</em> content',
93     ];
94     $reasons = $this->forumUninstallValidator->validate($module);
95     $this->assertSame($expected, $this->castSafeStrings($reasons));
96   }
97
98   /**
99    * @covers ::validate
100    */
101   public function testValidateHasTermsForVocabularyWithNodesAccess() {
102     $this->forumUninstallValidator->expects($this->once())
103       ->method('hasForumNodes')
104       ->willReturn(TRUE);
105
106     $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
107     $vocabulary->expects($this->once())
108       ->method('label')
109       ->willReturn('Vocabulary label');
110     $vocabulary->expects($this->once())
111       ->method('url')
112       ->willReturn('/path/to/vocabulary/overview');
113     $vocabulary->expects($this->once())
114       ->method('access')
115       ->willReturn(TRUE);
116     $this->forumUninstallValidator->expects($this->once())
117       ->method('getForumVocabulary')
118       ->willReturn($vocabulary);
119
120     $this->forumUninstallValidator->expects($this->once())
121       ->method('hasTermsForVocabulary')
122       ->willReturn(TRUE);
123
124     $module = 'forum';
125     $expected = [
126       'To uninstall Forum, first delete all <em>Forum</em> content',
127       'To uninstall Forum, first delete all <a href="/path/to/vocabulary/overview"><em class="placeholder">Vocabulary label</em></a> terms',
128     ];
129     $reasons = $this->forumUninstallValidator->validate($module);
130     $this->assertSame($expected, $this->castSafeStrings($reasons));
131   }
132
133   /**
134    * @covers ::validate
135    */
136   public function testValidateHasTermsForVocabularyWithNodesNoAccess() {
137     $this->forumUninstallValidator->expects($this->once())
138       ->method('hasForumNodes')
139       ->willReturn(TRUE);
140
141     $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
142     $vocabulary->expects($this->once())
143       ->method('label')
144       ->willReturn('Vocabulary label');
145     $vocabulary->expects($this->never())
146       ->method('url');
147     $vocabulary->expects($this->once())
148       ->method('access')
149       ->willReturn(FALSE);
150     $this->forumUninstallValidator->expects($this->once())
151       ->method('getForumVocabulary')
152       ->willReturn($vocabulary);
153
154     $this->forumUninstallValidator->expects($this->once())
155       ->method('hasTermsForVocabulary')
156       ->willReturn(TRUE);
157
158     $module = 'forum';
159     $expected = [
160       'To uninstall Forum, first delete all <em>Forum</em> content',
161       'To uninstall Forum, first delete all <em class="placeholder">Vocabulary label</em> terms',
162     ];
163     $reasons = $this->forumUninstallValidator->validate($module);
164     $this->assertSame($expected, $this->castSafeStrings($reasons));
165   }
166
167   /**
168    * @covers ::validate
169    */
170   public function testValidateHasTermsForVocabularyAccess() {
171     $this->forumUninstallValidator->expects($this->once())
172       ->method('hasForumNodes')
173       ->willReturn(FALSE);
174
175     $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
176     $vocabulary->expects($this->once())
177       ->method('url')
178       ->willReturn('/path/to/vocabulary/overview');
179     $vocabulary->expects($this->once())
180       ->method('label')
181       ->willReturn('Vocabulary label');
182     $vocabulary->expects($this->once())
183       ->method('access')
184       ->willReturn(TRUE);
185     $this->forumUninstallValidator->expects($this->once())
186       ->method('getForumVocabulary')
187       ->willReturn($vocabulary);
188
189     $this->forumUninstallValidator->expects($this->once())
190       ->method('hasTermsForVocabulary')
191       ->willReturn(TRUE);
192
193     $module = 'forum';
194     $expected = [
195       'To uninstall Forum, first delete all <a href="/path/to/vocabulary/overview"><em class="placeholder">Vocabulary label</em></a> terms',
196     ];
197     $reasons = $this->forumUninstallValidator->validate($module);
198     $this->assertSame($expected, $this->castSafeStrings($reasons));
199   }
200
201   /**
202    * @covers ::validate
203    */
204   public function testValidateHasTermsForVocabularyNoAccess() {
205     $this->forumUninstallValidator->expects($this->once())
206       ->method('hasForumNodes')
207       ->willReturn(FALSE);
208
209     $vocabulary = $this->getMock('Drupal\taxonomy\VocabularyInterface');
210     $vocabulary->expects($this->once())
211       ->method('label')
212       ->willReturn('Vocabulary label');
213     $vocabulary->expects($this->never())
214       ->method('url');
215     $vocabulary->expects($this->once())
216       ->method('access')
217       ->willReturn(FALSE);
218     $this->forumUninstallValidator->expects($this->once())
219       ->method('getForumVocabulary')
220       ->willReturn($vocabulary);
221
222     $this->forumUninstallValidator->expects($this->once())
223       ->method('hasTermsForVocabulary')
224       ->willReturn(TRUE);
225
226     $module = 'forum';
227     $expected = [
228       'To uninstall Forum, first delete all <em class="placeholder">Vocabulary label</em> terms',
229     ];
230     $reasons = $this->forumUninstallValidator->validate($module);
231     $this->assertSame($expected, $this->castSafeStrings($reasons));
232   }
233
234 }