Completed
Push — master ( 9e1461...cdd4b1 )
by
unknown
13:17
created

testNoPostFilteringForNonSILProfile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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