Completed
Push — master ( bd7180...598e28 )
by mw
37:32 queued 19:31
created

SearchResultModifierTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SIL\Tests\Search;
4
5
use SIL\Search\SearchResultModifier;
6
use SMW\DIProperty;
7
8
/**
9
 * @covers \SIL\Search\SearchResultModifier
10
 *
11
 * @group semantic-interlanguage-links
12
 *
13
 * @license GNU GPL v2+
14
 * @since 1.0
15
 *
16
 * @author mwjames
17
 */
18
class SearchResultModifierTest extends \PHPUnit_Framework_TestCase {
19
20
	protected function setUp() : void {
21
		$this->markTestSkipped( 'Use of SearchEngine::defaultNamespaces was deprecated in MediaWiki 1.27.' );
22
	}
23
24
	public function testCanConstruct() {
25
26
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
27
			->disableOriginalConstructor()
28
			->getMock();
29
30
		$this->assertInstanceOf(
31
			'\SIL\Search\SearchResultModifier',
32
			new SearchResultModifier( $languageResultMatchFinder )
33
		);
34
	}
35
36
	public function testAddSearchProfile() {
37
38
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
39
			->disableOriginalConstructor()
40
			->getMock();
41
42
		$instance = new SearchResultModifier( $languageResultMatchFinder );
43
44
		$profiles = [];
45
46
		$instance->addSearchProfile( $profiles );
47
48
		$this->assertArrayHasKey(
49
			'sil',
50
			$profiles
51
		);
52
53
		$this->assertInternalType(
54
			'array',
55
			$profiles['sil']['namespaces']
56
		);
57
	}
58
59
	public function testAddSearchFormForSILProfile() {
60
61
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
62
			->disableOriginalConstructor()
63
			->getMock();
64
65
		$instance = new SearchResultModifier( $languageResultMatchFinder );
66
67
		$request = $this->getMockBuilder( '\WebRequest' )
68
			->disableOriginalConstructor()
69
			->getMock();
70
71
		$request->expects( $this->once() )
72
			->method( 'getVal' )
73
			->with( $this->equalTo( 'languagefilter' ) )
74
			->will( $this->returnValue( 'vi' ) );
75
76
		$context = $this->getMockBuilder( '\IContextSource' )
77
			->disableOriginalConstructor()
78
			->getMockForAbstractClass();
79
80
		$context->expects( $this->once() )
81
			->method( 'getRequest' )
82
			->will( $this->returnValue( $request ) );
83
84
		$specialSearch = $this->getMockBuilder( '\SpecialSearch' )
85
			->disableOriginalConstructor()
86
			->getMock();
87
88
		$specialSearch->expects( $this->once() )
89
			->method( 'getContext' )
90
			->will( $this->returnValue( $context ) );
91
92
		$specialSearch->expects( $this->once() )
93
			->method( 'setExtraParam' )
94
			->with(
95
				$this->equalTo( 'languagefilter' ),
96
				$this->equalTo( 'vi' ) );
97
98
		$form = '';
99
		$opts = [ 'Foo' => 'Bar' ];
100
101
		$this->assertFalse(
102
			$instance->addSearchProfileForm( $specialSearch, 'sil', $form, $opts )
103
		);
104
105
		$this->assertContains(
106
			'languagefilter',
107
			$form
108
		);
109
110
		$this->assertContains(
111
			'Foo',
112
			$form
113
		);
114
	}
115
116
	public function testNoSearchFormForNonSILProfile() {
117
118
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
119
			->disableOriginalConstructor()
120
			->getMock();
121
122
		$instance = new SearchResultModifier( $languageResultMatchFinder );
123
124
		$specialSearch = $this->getMockBuilder( '\SpecialSearch' )
125
			->disableOriginalConstructor()
126
			->getMock();
127
128
		$form = '';
129
		$opts = [];
130
131
		$this->assertTrue(
132
			$instance->addSearchProfileForm( $specialSearch, 'foo', $form, $opts )
133
		);
134
	}
135
136
	public function testAddLanguageFilterToPowerBox() {
137
138
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
139
			->disableOriginalConstructor()
140
			->getMock();
141
142
		$instance = new SearchResultModifier( $languageResultMatchFinder );
143
144
		$request = $this->getMockBuilder( '\WebRequest' )
145
			->disableOriginalConstructor()
146
			->getMock();
147
148
		$request->expects( $this->once() )
149
			->method( 'getVal' )
150
			->with( $this->equalTo( 'languagefilter' ) )
151
			->will( $this->returnValue( 'en' ) );
152
153
		$this->assertTrue(
154
			$instance->addLanguageFilterToPowerBox( $request, $showSections )
155
		);
156
157
		$this->assertArrayHasKey(
158
			'sil-languagefilter',
159
			$showSections
160
		);
161
	}
162
163
	public function testNoPostFilteringForNonSILProfile() {
164
165
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
166
			->disableOriginalConstructor()
167
			->getMock();
168
169
		$instance = new SearchResultModifier( $languageResultMatchFinder );
170
171
		$request = $this->getMockBuilder( '\WebRequest' )
172
			->disableOriginalConstructor()
173
			->getMock();
174
175
		$request->expects( $this->once() )
176
			->method( 'getVal' )
177
			->with( $this->equalTo( 'profile' ) )
178
			->will( $this->returnValue( 'foo' ) );
179
180
		$titleMatches = false;
181
		$textMatches = false;
182
183
		$this->assertFalse(
184
			$instance->applyLanguageFilterToResultMatches( $request, $titleMatches, $textMatches )
185
		);
186
	}
187
188
	/**
189
	 * @dataProvider validProfileProvider
190
	 */
191
	public function testTryPostFilteringByValidProfileForValidLanguageCode( $profile ) {
192
193
		$titleMatches = $this->getMockBuilder( '\SearchResultSet' )
194
			->disableOriginalConstructor()
195
			->getMock();
196
197
		$textMatches = $this->getMockBuilder( '\SearchResultSet' )
198
			->disableOriginalConstructor()
199
			->getMock();
200
201
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
202
			->disableOriginalConstructor()
203
			->getMock();
204
205
		$languageResultMatchFinder->expects( $this->at( 0 ) )
206
			->method( 'matchResultsToLanguage' )
207
			->with(
208
				$this->equalTo( $titleMatches ),
209
				$this->equalTo( 'zh-Hans' ) );
210
211
		$languageResultMatchFinder->expects( $this->at( 1 ) )
212
			->method( 'matchResultsToLanguage' )
213
			->with(
214
				$this->equalTo( $textMatches ),
215
				$this->equalTo( 'zh-Hans' ) );
216
217
		$instance = new SearchResultModifier( $languageResultMatchFinder );
218
219
		$request = $this->getMockBuilder( '\WebRequest' )
220
			->disableOriginalConstructor()
221
			->getMock();
222
223
		$request->expects( $this->at( 0 ) )
224
			->method( 'getVal' )
225
			->with( $this->equalTo( 'profile' ) )
226
			->will( $this->returnValue( $profile ) );
227
228
		$request->expects( $this->at( 1 ) )
229
			->method( 'getVal' )
230
			->with( $this->equalTo( 'languagefilter' ) )
231
			->will( $this->returnValue( 'zh-hans' ) );
232
233
		$this->assertTrue(
234
			$instance->applyLanguageFilterToResultMatches( $request, $titleMatches, $textMatches )
235
		);
236
	}
237
238
	public function testCreateHtmlLanguageFilterSelector() {
239
240
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
241
			->disableOriginalConstructor()
242
			->getMock();
243
244
		$instance = new SearchResultModifier( $languageResultMatchFinder );
245
246
		$this->assertInternalType(
247
			'string',
248
			$instance->createHtmlLanguageFilterSelector( 'en' )
249
		);
250
	}
251
252
	/**
253
	 * @dataProvider invalidLanguageCodeProvider
254
	 */
255
	public function testTryPostFilteringForSILProfileByInvalidLanguageCode( $invalidLanguageCode ) {
256
257
		$languageResultMatchFinder = $this->getMockBuilder( '\SIL\Search\LanguageResultMatchFinder' )
258
			->disableOriginalConstructor()
259
			->getMock();
260
261
		$instance = new SearchResultModifier( $languageResultMatchFinder );
262
263
		$request = $this->getMockBuilder( '\WebRequest' )
264
			->disableOriginalConstructor()
265
			->getMock();
266
267
		$request->expects( $this->at( 0 ) )
268
			->method( 'getVal' )
269
			->with( $this->equalTo( 'profile' ) )
270
			->will( $this->returnValue( 'sil' ) );
271
272
		$request->expects( $this->at( 1 ) )
273
			->method( 'getVal' )
274
			->with( $this->equalTo( 'languagefilter' ) )
275
			->will( $this->returnValue( $invalidLanguageCode ) );
276
277
		$titleMatches = false;
278
		$textMatches = false;
279
280
		$this->assertFalse(
281
			$instance->applyLanguageFilterToResultMatches( $request, $titleMatches, $textMatches )
282
		);
283
	}
284
285
	public function invalidLanguageCodeProvider() {
286
287
		$provider = [
288
			[ null ],
289
			[ '' ],
290
			[ false ],
291
			[ '-' ]
292
		];
293
294
		return $provider;
295
	}
296
297
	public function validProfileProvider() {
298
299
		$provider = [
300
			[ 'sil' ],
301
			[ 'advanced' ]
302
		];
303
304
		return $provider;
305
	}
306
307
}
308