Completed
Push — master ( 712a1b...ea14c4 )
by mw
03:33 queued 02:13
created

SkinTemplateOutputModifierTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SBL\Tests;
4
5
use SBL\SkinTemplateOutputModifier;
6
7
use Title;
8
9
/**
10
 * @covers \SBL\SkinTemplateOutputModifier
11
 * @group semantic-breadcrumb-links
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author mwjames
17
 */
18
class SkinTemplateOutputModifierTest extends \PHPUnit_Framework_TestCase {
19
20
	private $namespaceExaminer;
21
22
	protected function setUp() {
23
24
		$this->namespaceExaminer = $this->getMockBuilder( '\SMW\NamespaceExaminer' )
25
			->disableOriginalConstructor()
26
			->getMock();
27
	}
28
29
	public function testCanConstruct() {
30
31
		$htmlBreadcrumbLinksBuilder = $this->getMockBuilder( '\SBL\HtmlBreadcrumbLinksBuilder' )
32
			->disableOriginalConstructor()
33
			->getMock();
34
35
		$this->assertInstanceOf(
36
			SkinTemplateOutputModifier::class,
37
			new SkinTemplateOutputModifier( $htmlBreadcrumbLinksBuilder, $this->namespaceExaminer )
38
		);
39
	}
40
41
	public function testTryPrependHtmlOnUnknownTitle() {
42
43
		$this->namespaceExaminer->expects( $this->never() )
44
			->method( 'isSemanticEnabled' )
45
			->will( $this->returnValue( true ) );
46
47
		$htmlBreadcrumbLinksBuilder = $this->getMockBuilder( '\SBL\HtmlBreadcrumbLinksBuilder' )
48
			->disableOriginalConstructor()
49
			->getMock();
50
51
		$title = $this->getMockBuilder( '\Title' )
52
			->disableOriginalConstructor()
53
			->getMock();
54
55
		$title->expects( $this->once() )
56
			->method( 'isKnown' )
57
			->will( $this->returnValue( false ) );
58
59
		$output = $this->getMockBuilder( '\OutputPage' )
60
			->disableOriginalConstructor()
61
			->getMock();
62
63
		$output->expects( $this->never() )
64
			->method( 'prependHTML' );
65
66
		$output->expects( $this->atLeastOnce() )
67
			->method( 'getTitle' )
68
			->will( $this->returnValue( $title ) );
69
70
		$instance = new SkinTemplateOutputModifier(
71
			$htmlBreadcrumbLinksBuilder,
72
			$this->namespaceExaminer
73
		);
74
75
		$template = new \stdClass;
76
		$template->data = [];
77
78
		$instance->modify( $output, $template );
79
	}
80
81
	public function testTryPrependHtmlOnSpecialPage() {
82
83
		$htmlBreadcrumbLinksBuilder = $this->getMockBuilder( '\SBL\HtmlBreadcrumbLinksBuilder' )
84
			->disableOriginalConstructor()
85
			->getMock();
86
87
		$title = $this->getMockBuilder( '\Title' )
88
			->disableOriginalConstructor()
89
			->getMock();
90
91
		$title->expects( $this->once() )
92
			->method( 'isKnown' )
93
			->will( $this->returnValue( true ) );
94
95
		$title->expects( $this->once() )
96
			->method( 'isSpecialPage' )
97
			->will( $this->returnValue( true ) );
98
99
		$output = $this->getMockBuilder( '\OutputPage' )
100
			->disableOriginalConstructor()
101
			->getMock();
102
103
		$output->expects( $this->never() )
104
			->method( 'prependHTML' );
105
106
		$output->expects( $this->atLeastOnce() )
107
			->method( 'getTitle' )
108
			->will( $this->returnValue( $title ) );
109
110
		$instance = new SkinTemplateOutputModifier(
111
			$htmlBreadcrumbLinksBuilder,
112
			$this->namespaceExaminer
113
		);
114
115
		$template = new \stdClass;
116
		$template->data = [];
117
118
		$instance->modify( $output, $template );
119
	}
120
121
	public function PrependHtmlOnNonViewAction() {
122
123
		$context = new \RequestContext();
124
		$context->setRequest( new \FauxRequest( [ 'action' => 'edit' ], true ) );
125
		$context->setTitle( Title::newFromText( __METHOD__ ) );
126
127
		$htmlBreadcrumbLinksBuilder = $this->getMockBuilder( '\SBL\HtmlBreadcrumbLinksBuilder' )
128
			->disableOriginalConstructor()
129
			->getMock();
130
131
		$title = $this->getMockBuilder( '\Title' )
132
			->disableOriginalConstructor()
133
			->getMock();
134
135
		$title->expects( $this->once() )
136
			->method( 'isKnown' )
137
			->will( $this->returnValue( true ) );
138
139
		$title->expects( $this->once() )
140
			->method( 'getNamespace' )
141
			->will( $this->returnValue( NS_MAIN ) );
142
143
		$title->expects( $this->once() )
144
			->method( 'isSpecialPage' )
145
			->will( $this->returnValue( false ) );
146
147
		$output = $this->getMockBuilder( '\OutputPage' )
148
			->disableOriginalConstructor()
149
			->getMock();
150
151
		$output->expects( $this->never() )
152
			->method( 'prependHTML' );
153
154
		$output->expects( $this->once() )
155
			->method( 'getContext' )
156
			->will( $this->returnValue( $context ) );
157
158
		$output->expects( $this->atLeastOnce() )
159
			->method( 'getTitle' )
160
			->will( $this->returnValue( $title ) );
161
162
		$instance = new SkinTemplateOutputModifier(
163
			$htmlBreadcrumbLinksBuilder,
164
			$this->namespaceExaminer
165
		);
166
167
		$template = new \stdClass;
168
		$template->data = [];
169
170
		$instance->modify( $output, $template );
171
	}
172
173
	public function testTryPrependHtmlOnNOBREADCRUMBLINKS() {
174
175
		$context = new \RequestContext();
176
		$context->setRequest( new \FauxRequest( [ 'action' => 'view'  ], true ) );
177
178
		$htmlBreadcrumbLinksBuilder = $this->getMockBuilder( '\SBL\HtmlBreadcrumbLinksBuilder' )
179
			->disableOriginalConstructor()
180
			->getMock();
181
182
		$title = $this->getMockBuilder( '\Title' )
183
			->disableOriginalConstructor()
184
			->getMock();
185
186
		$title->expects( $this->once() )
187
			->method( 'isKnown' )
188
			->will( $this->returnValue( true ) );
189
190
		$title->expects( $this->once() )
191
			->method( 'getNamespace' )
192
			->will( $this->returnValue( NS_MAIN ) );
193
194
		$title->expects( $this->once() )
195
			->method( 'isSpecialPage' )
196
			->will( $this->returnValue( false ) );
197
198
		$output = $this->getMockBuilder( '\OutputPage' )
199
			->disableOriginalConstructor()
200
			->getMock();
201
202
		$output->expects( $this->never() )
203
			->method( 'prependHTML' );
204
205
		$output->expects( $this->any() )
206
			->method( 'getContext' )
207
			->will( $this->returnValue( $context ) );
208
209
		$output->expects( $this->atLeastOnce() )
210
			->method( 'getTitle' )
211
			->will( $this->returnValue( $title ) );
212
213
		$instance = new SkinTemplateOutputModifier(
214
			$htmlBreadcrumbLinksBuilder,
215
			$this->namespaceExaminer
216
		);
217
218
		$output->smwmagicwords = [ 'SBL_NOBREADCRUMBLINKS' ];
219
220
		$template = new \stdClass;
221
		$template->data = [];
222
223
		$instance->modify( $output, $template );
224
	}
225
226
	public function tesPrependHtmlForViewActionOnly() {
227
228
		$context = new \RequestContext();
229
		$context->setRequest( new \FauxRequest( [ 'action' => 'view'  ], true ) );
230
231
		$htmlBreadcrumbLinksBuilder = $this->getMockBuilder( '\SBL\HtmlBreadcrumbLinksBuilder' )
232
			->disableOriginalConstructor()
233
			->getMock();
234
235
		$htmlBreadcrumbLinksBuilder->expects( $this->once() )
236
			->method( 'buildBreadcrumbs' )
237
			->will( $this->returnSelf() );
238
239
		$language = $this->getMockBuilder( '\Language' )
240
			->disableOriginalConstructor()
241
			->getMock();
242
243
		$title = $this->getMockBuilder( '\Title' )
244
			->disableOriginalConstructor()
245
			->getMock();
246
247
		$title->expects( $this->once() )
248
			->method( 'isKnown' )
249
			->will( $this->returnValue( true ) );
250
251
		$title->expects( $this->once() )
252
			->method( 'getNamespace' )
253
			->will( $this->returnValue( NS_MAIN ) );
254
255
		$title->expects( $this->once() )
256
			->method( 'isSpecialPage' )
257
			->will( $this->returnValue( false ) );
258
259
		$title->expects( $this->once() )
260
			->method( 'getPageLanguage' )
261
			->will( $this->returnValue( $language ) );
262
263
		$output = $this->getMockBuilder( '\OutputPage' )
264
			->disableOriginalConstructor()
265
			->getMock();
266
267
		$output->expects( $this->once() )
268
			->method( 'prependHTML' );
269
270
		$output->expects( $this->once() )
271
			->method( 'getContext' )
272
			->will( $this->returnValue( $context ) );
273
274
		$output->expects( $this->atLeastOnce() )
275
			->method( 'getTitle' )
276
			->will( $this->returnValue( $title ) );
277
278
		$instance = new SkinTemplateOutputModifier(
279
			$htmlBreadcrumbLinksBuilder,
280
			$this->namespaceExaminer
281
		);
282
283
		$template = new \stdClass;
284
		$template->data = [];
285
286
		$instance->modify( $output, $template );
287
288
		$this->assertEmpty(
289
			$template->data['subtitle']
290
		);
291
	}
292
293
}
294