Completed
Push — master ( 97f192...39c216 )
by mw
9s
created

InterlanguageLinkParserFunctionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 11
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
21
	protected function setUp() {
22
		parent::setUp();
23
24
		$this->languageLinkAnnotator = $this->getMockBuilder( '\SIL\LanguageLinkAnnotator' )
25
			->disableOriginalConstructor()
26
			->getMock();
27
28
		$this->languageLinkAnnotator->expects( $this->any() )
29
			->method( 'canAddAnnotation' )
30
			->will( $this->returnValue( true ) );
31
32
		$this->siteLanguageLinksParserOutputAppender = $this->getMockBuilder( '\SIL\SiteLanguageLinksParserOutputAppender' )
33
			->disableOriginalConstructor()
34
			->getMock();
35
	}
36
37
	public function testCanConstruct() {
38
39
		$title = $this->getMockBuilder( '\Title' )
40
			->disableOriginalConstructor()
41
			->getMock();
42
43
		$this->assertInstanceOf(
44
			'\SIL\InterlanguageLinkParserFunction',
45
			new InterlanguageLinkParserFunction(
46
				$title,
47
				$this->languageLinkAnnotator,
48
				$this->siteLanguageLinksParserOutputAppender
49
			)
50
		);
51
	}
52
53
	public function testTryParseThatCausesErrorMessage() {
54
55
		$title = $this->getMockBuilder( '\Title' )
56
			->disableOriginalConstructor()
57
			->getMock();
58
59
		$instance = new InterlanguageLinkParserFunction(
60
			$title,
61
			$this->languageLinkAnnotator,
62
			$this->siteLanguageLinksParserOutputAppender
63
		);
64
65
		$instance->setInterlanguageLinksHideState( true );
66
67
		$this->assertInternalType(
68
			'string',
69
			$instance->parse( 'en', 'Foo' )
70
		);
71
72
		$instance->setInterlanguageLinksHideState( false );
73
74
		$this->assertInternalType(
75
			'string',
76
			$instance->parse( '%42$', 'Foo' )
77
		);
78
79
		$this->assertContains(
80
			'-error',
81
			$instance->parse( '', 'Foo' )
82
		);
83
84
		$this->assertContains(
85
			'-error',
86
			$instance->parse( 'en', '{[[:Template:Foo]]' )
87
		);
88
	}
89
90
	public function testParseToCreateErrorMessageForKnownTarget() {
91
92
		$title = \Title::newFromText( __METHOD__ );
93
94
		$this->languageLinkAnnotator->expects( $this->never() )
95
			->method( 'addAnnotationForInterlanguageLink' );
96
97
		$this->siteLanguageLinksParserOutputAppender->expects( $this->once() )
98
			->method( 'getRedirectTargetFor' )
99
			->with(	$this->equalTo( \Title::newFromText( 'Foo' ) ) )
100
			->will( $this->returnValue( $title ) );
101
102
		$this->siteLanguageLinksParserOutputAppender->expects( $this->once() )
103
			->method( 'tryAddLanguageTargetLinksToOutput' )
104
			->with(
105
				$this->anything(),
106
				$this->equalTo( $title ) )
107
			->will( $this->returnValue( 'Foo' ) );
108
109
		$instance = new InterlanguageLinkParserFunction(
110
			$title,
111
			$this->languageLinkAnnotator,
112
			$this->siteLanguageLinksParserOutputAppender
113
		);
114
115
		$instance->setInterlanguageLinksHideState( false );
116
117
		$this->assertContains(
118
			'-error',
119
			$instance->parse( 'en', 'Foo' )
120
		);
121
	}
122
123
	public function testMultipleParseCalls() {
124
125
		$title = \Title::newFromText( __METHOD__ );
126
127
		$this->siteLanguageLinksParserOutputAppender->expects( $this->any() )
128
			->method( 'getRedirectTargetFor' )
129
			->will( $this->returnValue( $title ) );
130
131
		$instance = new InterlanguageLinkParserFunction(
132
			$title,
133
			$this->languageLinkAnnotator,
134
			$this->siteLanguageLinksParserOutputAppender
135
		);
136
137
		$this->assertContains(
138
			'div class="sil-interlanguagelink"',
139
			$instance->parse( 'en', 'Foo' )
140
		);
141
142
		$this->assertContains(
143
			'div class="sil-interlanguagelink"',
144
			$instance->parse( 'en', 'Foo' )
145
		);
146
	}
147
148
	public function testMultipleParseCallsWithDifferentLanguagesTriggersErrorMessage() {
149
150
		$title = \Title::newFromText( __METHOD__ );
151
152
		$this->languageLinkAnnotator->expects( $this->any() )
153
			->method( 'hasDifferentLanguageAnnotation' )
154
			->will( $this->onConsecutiveCalls( false, true ) );
155
156
		$this->siteLanguageLinksParserOutputAppender->expects( $this->any() )
157
			->method( 'getRedirectTargetFor' )
158
			->will( $this->returnValue( $title ) );
159
160
		$instance = new InterlanguageLinkParserFunction(
161
			$title,
162
			$this->languageLinkAnnotator,
163
			$this->siteLanguageLinksParserOutputAppender
164
		);
165
166
		$instance->parse( 'en', 'Foo' );
167
168
		$this->assertContains(
169
			'-error',
170
			$instance->parse( 'fr', 'Foo' )
171
		);
172
	}
173
174
	public function testParse() {
175
176
		$title = $this->getMockBuilder( '\Title' )
177
			->disableOriginalConstructor()
178
			->getMock();
179
180
		$this->languageLinkAnnotator->expects( $this->once() )
181
			->method( 'addAnnotationForInterlanguageLink' );
182
183
		$this->siteLanguageLinksParserOutputAppender->expects( $this->any() )
184
			->method( 'getRedirectTargetFor' )
185
			->will( $this->returnValue( $title ) );
186
187
		$instance = new InterlanguageLinkParserFunction(
188
			$title,
189
			$this->languageLinkAnnotator,
190
			$this->siteLanguageLinksParserOutputAppender
191
		);
192
193
		$instance->setInterlanguageLinksHideState( false );
194
195
		$this->assertContains(
196
			'div class="sil-interlanguagelink"',
197
			$instance->parse( 'en', 'Foo' )
198
		);
199
	}
200
201
	public function testRevisionMode() {
202
203
		$title = $this->getMockBuilder( '\Title' )
204
			->disableOriginalConstructor()
205
			->getMock();
206
207
		$this->siteLanguageLinksParserOutputAppender->expects( $this->any() )
208
			->method( 'getRedirectTargetFor' )
209
			->will( $this->returnValue( $title ) );
210
211
		$instance = new InterlanguageLinkParserFunction(
212
			$title,
213
			$this->languageLinkAnnotator,
214
			$this->siteLanguageLinksParserOutputAppender
215
		);
216
217
		$instance->setRevisionModeState( true );
218
219
		$this->assertEmpty(
220
			$instance->parse( 'en', 'Foo' )
221
		);
222
	}
223
224
	public function testAnnotationModeIsDisabled() {
225
226
		$title = $this->getMockBuilder( '\Title' )
227
			->disableOriginalConstructor()
228
			->getMock();
229
230
		$languageLinkAnnotator = $this->getMockBuilder( '\SIL\LanguageLinkAnnotator' )
231
			->disableOriginalConstructor()
232
			->getMock();
233
234
		$languageLinkAnnotator->expects( $this->once() )
235
			->method( 'canAddAnnotation' )
236
			->will( $this->returnValue( false ) );
237
238
		$instance = new InterlanguageLinkParserFunction(
239
			$title,
240
			$languageLinkAnnotator,
241
			$this->siteLanguageLinksParserOutputAppender
242
		);
243
244
		$this->assertEmpty(
245
			$instance->parse( 'en', 'Foo' )
246
		);
247
	}
248
249
}
250