Completed
Push — master ( 0bdf57...ff1c2f )
by mw
6s
created

testAddLanguageCodeToPageContentLanguageIntermediaryCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 9.0856
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace SIL\Tests;
4
5
use SIL\InterlanguageLinkParserFunction;
6
7
/**
8
 * @covers \SIL\InterlanguageLinkParserFunction
9
 * @group semantic-interlanguage-links
10
 *
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class InterlanguageLinkParserFunctionTest extends \PHPUnit_Framework_TestCase {
17
18
	private $languageLinkAnnotator;
19
	private $siteLanguageLinksParserOutputAppender;
20
	private $pageContentLanguageModifier;
21
22
	protected function setUp() {
23
		parent::setUp();
24
25
		$this->languageLinkAnnotator = $this->getMockBuilder( '\SIL\LanguageLinkAnnotator' )
26
			->disableOriginalConstructor()
27
			->getMock();
28
29
		$this->languageLinkAnnotator->expects( $this->any() )
30
			->method( 'canAddAnnotation' )
31
			->will( $this->returnValue( true ) );
32
33
		$this->siteLanguageLinksParserOutputAppender = $this->getMockBuilder( '\SIL\SiteLanguageLinksParserOutputAppender' )
34
			->disableOriginalConstructor()
35
			->getMock();
36
37
		$this->pageContentLanguageModifier = $this->getMockBuilder( '\SIL\PageContentLanguageModifier' )
38
			->disableOriginalConstructor()
39
			->getMock();
40
	}
41
42
	public function testCanConstruct() {
43
44
		$title = $this->getMockBuilder( '\Title' )
45
			->disableOriginalConstructor()
46
			->getMock();
47
48
		$this->assertInstanceOf(
49
			'\SIL\InterlanguageLinkParserFunction',
50
			new InterlanguageLinkParserFunction(
51
				$title,
52
				$this->languageLinkAnnotator,
53
				$this->siteLanguageLinksParserOutputAppender,
54
				$this->pageContentLanguageModifier
55
			)
56
		);
57
	}
58
59
	public function testTryParseThatCausesErrorMessage() {
60
61
		$title = $this->getMockBuilder( '\Title' )
62
			->disableOriginalConstructor()
63
			->getMock();
64
65
		$instance = new InterlanguageLinkParserFunction(
66
			$title,
67
			$this->languageLinkAnnotator,
68
			$this->siteLanguageLinksParserOutputAppender,
69
			$this->pageContentLanguageModifier
70
		);
71
72
		$instance->setInterlanguageLinksHideState( true );
73
74
		$this->assertInternalType(
75
			'string',
76
			$instance->parse( 'en', 'Foo' )
77
		);
78
79
		$instance->setInterlanguageLinksHideState( false );
80
81
		$this->assertInternalType(
82
			'string',
83
			$instance->parse( '%42$', 'Foo' )
84
		);
85
86
		$this->assertContains(
87
			'-error',
88
			$instance->parse( '', 'Foo' )
89
		);
90
91
		$this->assertContains(
92
			'-error',
93
			$instance->parse( 'en', '{[[:Template:Foo]]' )
94
		);
95
	}
96
97
	public function testParseToCreateErrorMessageForKnownTarget() {
98
99
		$title = \Title::newFromText( __METHOD__ );
100
101
		$this->languageLinkAnnotator->expects( $this->never() )
102
			->method( 'addAnnotationForInterlanguageLink' );
103
104
		$this->siteLanguageLinksParserOutputAppender->expects( $this->once() )
105
			->method( 'getRedirectTargetFor' )
106
			->with(	$this->equalTo( \Title::newFromText( 'Foo' ) ) )
107
			->will( $this->returnValue( $title ) );
108
109
		$this->siteLanguageLinksParserOutputAppender->expects( $this->once() )
110
			->method( 'tryAddLanguageTargetLinksToOutput' )
111
			->with(
112
				$this->anything(),
113
				$this->equalTo( $title ) )
114
			->will( $this->returnValue( 'Foo' ) );
115
116
		$instance = new InterlanguageLinkParserFunction(
117
			$title,
118
			$this->languageLinkAnnotator,
119
			$this->siteLanguageLinksParserOutputAppender,
120
			$this->pageContentLanguageModifier
121
		);
122
123
		$instance->setInterlanguageLinksHideState( false );
124
125
		$this->assertContains(
126
			'-error',
127
			$instance->parse( 'en', 'Foo' )
128
		);
129
	}
130
131
	public function testMultipleParseCalls() {
132
133
		$title = \Title::newFromText( __METHOD__ );
134
135
		$this->siteLanguageLinksParserOutputAppender->expects( $this->any() )
136
			->method( 'getRedirectTargetFor' )
137
			->will( $this->returnValue( $title ) );
138
139
		$instance = new InterlanguageLinkParserFunction(
140
			$title,
141
			$this->languageLinkAnnotator,
142
			$this->siteLanguageLinksParserOutputAppender,
143
			$this->pageContentLanguageModifier
144
		);
145
146
		$this->assertContains(
147
			'div class="sil-interlanguagelink"',
148
			$instance->parse( 'en', 'Foo' )
149
		);
150
151
		$this->assertContains(
152
			'div class="sil-interlanguagelink"',
153
			$instance->parse( 'en', 'Foo' )
154
		);
155
	}
156
157
	public function testMultipleParseCallsWithDifferentLanguagesTriggersErrorMessage() {
158
159
		$title = \Title::newFromText( __METHOD__ );
160
161
		$this->languageLinkAnnotator->expects( $this->any() )
162
			->method( 'hasDifferentLanguageAnnotation' )
163
			->will( $this->onConsecutiveCalls( false, true ) );
164
165
		$this->siteLanguageLinksParserOutputAppender->expects( $this->any() )
166
			->method( 'getRedirectTargetFor' )
167
			->will( $this->returnValue( $title ) );
168
169
		$instance = new InterlanguageLinkParserFunction(
170
			$title,
171
			$this->languageLinkAnnotator,
172
			$this->siteLanguageLinksParserOutputAppender,
173
			$this->pageContentLanguageModifier
174
		);
175
176
		$instance->parse( 'en', 'Foo' );
177
178
		$this->assertContains(
179
			'-error',
180
			$instance->parse( 'fr', 'Foo' )
181
		);
182
	}
183
184
	public function testParse() {
185
186
		$title = $this->getMockBuilder( '\Title' )
187
			->disableOriginalConstructor()
188
			->getMock();
189
190
		$this->languageLinkAnnotator->expects( $this->once() )
191
			->method( 'addAnnotationForInterlanguageLink' );
192
193
		$this->siteLanguageLinksParserOutputAppender->expects( $this->any() )
194
			->method( 'getRedirectTargetFor' )
195
			->will( $this->returnValue( $title ) );
196
197
		$instance = new InterlanguageLinkParserFunction(
198
			$title,
199
			$this->languageLinkAnnotator,
200
			$this->siteLanguageLinksParserOutputAppender,
201
			$this->pageContentLanguageModifier
202
		);
203
204
		$instance->setInterlanguageLinksHideState( false );
205
206
		$this->assertContains(
207
			'div class="sil-interlanguagelink"',
208
			$instance->parse( 'en', 'Foo' )
209
		);
210
	}
211
212
	public function testRevisionMode() {
213
214
		$title = $this->getMockBuilder( '\Title' )
215
			->disableOriginalConstructor()
216
			->getMock();
217
218
		$this->siteLanguageLinksParserOutputAppender->expects( $this->any() )
219
			->method( 'getRedirectTargetFor' )
220
			->will( $this->returnValue( $title ) );
221
222
		$instance = new InterlanguageLinkParserFunction(
223
			$title,
224
			$this->languageLinkAnnotator,
225
			$this->siteLanguageLinksParserOutputAppender,
226
			$this->pageContentLanguageModifier
227
		);
228
229
		$instance->setRevisionModeState( true );
230
231
		$this->assertEmpty(
232
			$instance->parse( 'en', 'Foo' )
233
		);
234
	}
235
236
	public function testAnnotationModeIsDisabled() {
237
238
		$title = $this->getMockBuilder( '\Title' )
239
			->disableOriginalConstructor()
240
			->getMock();
241
242
		$languageLinkAnnotator = $this->getMockBuilder( '\SIL\LanguageLinkAnnotator' )
243
			->disableOriginalConstructor()
244
			->getMock();
245
246
		$languageLinkAnnotator->expects( $this->once() )
247
			->method( 'canAddAnnotation' )
248
			->will( $this->returnValue( false ) );
249
250
		$instance = new InterlanguageLinkParserFunction(
251
			$title,
252
			$languageLinkAnnotator,
253
			$this->siteLanguageLinksParserOutputAppender,
254
			$this->pageContentLanguageModifier
255
		);
256
257
		$this->assertEmpty(
258
			$instance->parse( 'en', 'Foo' )
259
		);
260
	}
261
262
	public function testAddLanguageCodeToPageContentLanguageIntermediaryCache() {
263
264
		$title = $this->getMockBuilder( '\Title' )
265
			->disableOriginalConstructor()
266
			->getMock();
267
268
		$languageLinkAnnotator = $this->getMockBuilder( '\SIL\LanguageLinkAnnotator' )
269
			->disableOriginalConstructor()
270
			->getMock();
271
272
		$this->pageContentLanguageModifier->expects( $this->once() )
273
			->method( 'addToIntermediaryCache' );
274
275
		$instance = new InterlanguageLinkParserFunction(
276
			$title,
277
			$languageLinkAnnotator,
278
			$this->siteLanguageLinksParserOutputAppender,
279
			$this->pageContentLanguageModifier
280
		);
281
282
		$instance->setRevisionModeState( true );
283
		$instance->parse( 'en', 'Foo' );
284
	}
285
286
}
287