Completed
Push — dev2 ( f75d77...01a103 )
by Gordon
15:27
created

ElasticSearchPageControllerTest   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 715
Duplicated Lines 18.88 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%
Metric Value
wmc 33
lcom 1
cbo 4
dl 135
loc 715
ccs 0
cts 497
cp 0
rs 9.4

22 Methods

Rating   Name   Duplication   Size   Complexity  
A testAggregationThreeSelected() 0 22 1
A testQueryIsEmpty() 0 22 1
C setUp() 0 50 7
A testAggregationNoneSelected() 0 51 1
B testAggregationOneSelected() 0 45 1
B testAggregationTwoSelected() 0 29 1
A testQueryInSearchBoxForOneFormOnly() 9 9 1
B testSearchFormAutocompleteEnabled() 0 30 1
B testTemplateOverrideExtension() 0 27 2
A testSimilarNotSearchable() 10 10 1
A testSimilarNull() 10 10 1
A testSimilarClassDoesNotExist() 9 9 1
A testSimilarSearchServerDown() 8 8 1
A testNormalSearchServerDown() 8 8 1
A testSimilarValid() 6 16 2
B testSuggestion() 6 93 2
B testSearchOnePageNoAggregation() 36 36 1
A testRedirection() 0 17 1
B testSearchOnePage() 33 33 1
B testSiteTreeSearch() 0 35 1
A testSearchSeveralPagesPage() 0 62 1
A enableHighlights() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
/**
5
 * @package comments
6
 */
7
class ElasticSearchPageControllerTest extends ElasticsearchFunctionalTestBase {
8
9
	public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml';
10
11
12
	public function setUp() {
13
		parent::setUp();
14
		$esp = new ElasticSearchPage();
15
		$esp->Title = 'Search with aggregation';
16
		$esp->Content = 'Example search page with aggregation';
17
		$esp->Identifier = 'testwithagg';
18
		$esp->IndexingOff = true;
19
		$esp->URLSegment = 'search';
20
		$esp->SiteTreeOnly = false;
21
		$esp->ClassesToSearch = 'FlickrPhotoTO';
22
		$esp->SearchHelper = 'FlickrPhotoTOElasticaSearchHelper';
23
		$esp->write();
24
25
		$esp2 = new ElasticSearchPage();
26
		$esp2->Title = 'Search without aggregation';
27
		$esp2->Content = 'Example search page';
28
		$esp2->Identifier = 'testwithoutagg';
29
		$esp2->IndexingOff = true;
30
		$esp2->URLSegment = 'search';
31
		$esp2->SiteTreeOnly = false;
32
		$esp2->ClassesToSearch = 'FlickrPhotoTO';
33
		$esp2->ContentForEmptySearch = 'Content for empty search';
34
		$esp2->write();
35
36
		#NOTE: how to edit extra extra fields programatically
37
		$extraFields = array('Searchable' => 1, 'SimilarSearchable' => 1, 'Active' => 1,
38
			'Weight' => 1);
39
		$esfs2 = $esp2->ElasticaSearchableFields();
40
		foreach($esfs2 as $sf) {
41
			if($sf->Name == 'Title' || $sf->Name == 'Description') {
42
				$esfs2->remove($sf);
43
				$esfs2->add($sf, $extraFields);
44
			}
45
		}
46
		$esp2->write();
47
48
		$esfs = $esp->ElasticaSearchableFields();
49
50
		foreach($esfs as $sf) {
51
			if($sf->Name == 'Title' || $sf->Name == 'Description') {
52
				$esfs->remove($sf);
53
				$esfs->add($sf, $extraFields);
54
			}
55
		}
56
		$esp->write();
57
		$esp->publish('Stage', 'Live');
58
		$esp2->publish('Stage', 'Live');
59
		$this->ElasticSearchPage = $esp;
60
		$this->ElasticSearchPage2 = $esp2;
61
	}
62
63
64
	public function testAggregationNoneSelected() {
65
		//SearchHelper
66
67
		$this->autoFollowRedirection = false;
68
69
		//Note pages need to be published, by default fixtures only reside in Stage
70
		$searchPageObj = $this->ElasticSearchPage;
71
72
		$url = $searchPageObj->Link();
73
		$searchPage = $this->get($searchPageObj->URLSegment);
74
		$this->assertEquals(200, $searchPage->getStatusCode());
75
		$url = rtrim($url, '/');
76
77
        $response = $this->get($url);
78
79
        $this->assertEquals(200, $response->getStatusCode());
80
81
        $this->assertSelectorStartsWithOrEquals('ul.iso span.count', 0, '(5)');
82
		$this->assertSelectorStartsWithOrEquals('ul.iso span.count', 1, '(11)');
83
		$this->assertSelectorStartsWithOrEquals('ul.iso span.count', 2, '(12)');
84
		$this->assertSelectorStartsWithOrEquals('ul.iso span.count', 3, '(13)');
85
		$this->assertSelectorStartsWithOrEquals('ul.iso span.count', 4, '(16)');
86
		$this->assertSelectorStartsWithOrEquals('ul.iso span.count', 5, '(13)');
87
		$this->assertSelectorStartsWithOrEquals('ul.iso span.count', 6, '(11)');
88
		$this->assertSelectorStartsWithOrEquals('ul.iso span.count', 7, '(19)');
89
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 0, '(12)');
90
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 1, '(11)');
91
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 2, '(11)');
92
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 3, '(20)');
93
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 4, '(12)');
94
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 5, '(17)');
95
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 6, '(17)');
96
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 0, '(17)');
97
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 1, '(15)');
98
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 2, '(17)');
99
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 3, '(10)');
100
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 4, '(18)');
101
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 5, '(1)');
102
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 6, '(10)');
103
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 7, '(12)');
104
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 0, '(21)');
105
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 1, '(23)');
106
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 2, '(17)');
107
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 3, '(16)');
108
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 4, '(23)');
109
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 0, '(9)');
110
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 1, '(31)');
111
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 2, '(16)');
112
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 3, '(39)');
113
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 4, '(5)');
114
	}
115
116
117
	public function testAggregationOneSelected() {
118
		//SearchHelper
119
120
		$this->autoFollowRedirection = false;
121
122
		//Note pages need to be published, by default fixtures only reside in Stage
123
		$searchPageObj = $this->ElasticSearchPage;
124
125
		$url = $searchPageObj->Link();
126
		$searchPage = $this->get($searchPageObj->URLSegment);
127
		$this->assertEquals(200, $searchPage->getStatusCode());
128
		$url = rtrim($url, '/');
129
		$url .= '?ISO=400';
130
131
        $response = $this->get($url);
132
133
        $this->assertEquals(200, $response->getStatusCode());
134
135
        // These are less than in the no facets selected case, as expected
136
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 0, '(2)');
137
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 1, '(1)');
138
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 2, '(2)');
139
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 3, '(1)');
140
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 4, '(3)');
141
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 5, '(1)');
142
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 6, '(3)');
143
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 0, '(3)');
144
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 1, '(2)');
145
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 2, '(2)');
146
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 3, '(1)');
147
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed  span.count', 4, '(2)');
148
		$this->assertSelectorStartsWithOrEquals('ul.shutter_speed span.count', 5, '(3)');
149
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 0, '(3)');
150
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 1, '(4)');
151
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 2, '(3)');
152
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 3, '(1)');
153
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 4, '(2)');
154
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 0, '(0)');
155
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 1, '(4)');
156
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 2, '(1)');
157
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 3, '(7)');
158
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 4, '(1)');
159
160
161
	}
162
163
164
165
	public function testAggregationTwoSelected() {
166
		//SearchHelper
167
168
		$this->autoFollowRedirection = false;
169
170
		//Note pages need to be published, by default fixtures only reside in Stage
171
		$searchPageObj = $this->ElasticSearchPage;
172
173
		$url = $searchPageObj->Link();
174
		$searchPage = $this->get($searchPageObj->URLSegment);
175
		$this->assertEquals(200, $searchPage->getStatusCode());
176
		$url = rtrim($url, '/');
177
		$url .= '?ISO=400&ShutterSpeed=2%2F250';
178
179
        $response = $this->get($url);
180
        $this->assertEquals(200, $response->getStatusCode());
181
182
        // These are less than in the one facet selected case, as expected
183
        $this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 0, '(1)');
184
		$this->assertSelectorStartsWithOrEquals('ul.focal_length span.count', 1, '(2)');
185
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 0, '(1)');
186
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 1, '(1)');
187
		$this->assertSelectorStartsWithOrEquals('ul.aperture span.count', 2, '(1)');
188
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 0, '(0)');
189
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 1, '(1)');
190
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 2, '(0)');
191
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 3, '(2)');
192
		$this->assertSelectorStartsWithOrEquals('ul.aspect span.count', 4, '(0)');
193
	}
194
195
196
	public function testAggregationThreeSelected() {
197
		//SearchHelper
198
199
		$this->autoFollowRedirection = false;
200
201
		//Note pages need to be published, by default fixtures only reside in Stage
202
		$searchPageObj = $this->ElasticSearchPage;
203
204
		$url = $searchPageObj->Link();
205
		$searchPage = $this->get($searchPageObj->URLSegment);
206
		$this->assertEquals(200, $searchPage->getStatusCode());
207
		$url = rtrim($url, '/');
208
		$url .= '?ISO=400&ShutterSpeed=2%2F250&Aspect=Vertical';
209
210
        $response = $this->get($url);
211
        $this->assertEquals(200, $response->getStatusCode());
212
213
        // These are less than in the one facet selected case, as expected
214
        $this->assertSelectorStartsWithOrEquals('span.count', 0, '(2)');
215
		$this->assertSelectorStartsWithOrEquals('span.count', 1, '(1)');
216
		$this->assertSelectorStartsWithOrEquals('span.count', 2, '(1)');
217
	}
218
219
220
	public function testQueryIsEmpty() {
221
		//Note pages need to be published, by default fixtures only reside in Stage
222
		$searchPageObj = $this->ElasticSearchPage2;
223
		$url = rtrim($searchPageObj->Link(), '/');
224
		$response = $this->get($url);
225
		$this->assertEquals(200, $response->getStatusCode());
226
		$this->assertSelectorStartsWithOrEquals('div.contentForEmptySearch', 0,
227
			$searchPageObj->ContentForEmptySearch);
228
229
230
		$url .= '?q=';
231
		$response = $this->get($url);
232
		$this->assertEquals(200,
233
			$response->getStatusCode());
234
		$this->assertSelectorStartsWithOrEquals('div.contentForEmptySearch', 0,
235
			$searchPageObj->ContentForEmptySearch);
236
237
		$url .= 'farming';
238
		$response = $this->get($url);
239
		$this->assertEquals(200, $response->getStatusCode());
240
		$this->assertExactHTMLMatchBySelector('div.contentForEmptySearch', array());
241
	}
242
243 View Code Duplication
	public function testQueryInSearchBoxForOneFormOnly() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
		$searchPageObj = $this->ElasticSearchPage2;
245
		$url = rtrim($searchPageObj->Link(), '/');
246
		$url .= "?q=Auckland&sfid=".$searchPageObj->Identifier;
247
		$response = $this->get($url);
248
		$this->assertEquals(200, $response->getStatusCode());
249
		$this->assertAttributeHasExactValue('#ElasticSearchForm_SearchForm_q', 'q',
250
			'Auckland');
251
	}
252
253
254
	/*
255
	Check the search form attributes for autocomplete info
256
	 */
257
	public function testSearchFormAutocompleteEnabled() {
258
		// prepeare autocomplete field
259
		$this->autoFollowRedirection = false;
260
261
		//Note pages need to be published, by default fixtures only reside in Stage
262
		$searchPageObj = $this->ElasticSearchPage2;
263
		$searchPageObj->ClassesToSearch = 'FlickrPhotoTO';
264
		$filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Title');
265
		$sfid = SearchableField::get()->filter($filter)->first()->ID;
266
		$searchPageObj->AutoCompleteFieldID = $sfid;
267
		$pageLength = 10; // the default
268
		$searchPageObj->ResultsPerPage = $pageLength;
269
		$searchPageObj->write();
270
		$searchPageObj->publish('Stage', 'Live');
271
272
		$url = rtrim($searchPageObj->Link(), '/');
273
		$response = $this->get($url);
274
		$this->assertEquals(200, $response->getStatusCode());
275
276
		// note, need to test with strings
277
		$expected = array(
278
			'data-autocomplete' => 'true',
279
			'data-autocomplete-field' => 'Title',
280
			'data-autocomplete-field' => 'Title',
281
			'data-autocomplete-classes' => 'FlickrPhotoTO',
282
			'data-autocomplete-sitetree' => '0',
283
			'data-autocomplete-source' => '/search-2/'
284
		);
285
		$this->assertAttributesHaveExactValues('#ElasticSearchForm_SearchForm_q', $expected);
286
	}
287
288
289
	public function testTemplateOverrideExtension() {
290
291
		if(!class_exists('PageControllerTemplateOverrideExtension')) {
292
			$this->markTestSkipped('PageControllerTemplateOverrideExtension not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<ElasticSearchPageControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
293
		}
294
295
		Page_Controller::remove_extension('PageControllerTemplateOverrideExtension');
296
		$searchPageObj = $this->ElasticSearchPage2;
297
298
		$url = rtrim($searchPageObj->Link(), '/');
299
		$response = $this->get($url);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
300
		$this->assertEquals(0, PageControllerTemplateOverrideExtension::getTemplateOverrideCounter());
301
		Page_Controller::add_extension('PageControllerTemplateOverrideExtension');
302
		$searchPageObj = $this->ElasticSearchPage2;
303
		$url = rtrim($searchPageObj->Link(), '/');
304
		$response = $this->get($url);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
305
		$this->assertEquals(1, PageControllerTemplateOverrideExtension::getTemplateOverrideCounter());
306
307
		$url .= "/similar/FlickrPhotoTO/77";
308
		$response = $this->get($url);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
309
310
		//check the template override method was called
311
		$this->assertEquals(2, PageControllerTemplateOverrideExtension::getTemplateOverrideCounter());
312
313
		Page_Controller::remove_extension('PageControllerTemplateOverrideExtension');
314
315
	}
316
317
318 View Code Duplication
	public function testSimilarNotSearchable() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
319
		$searchPageObj = $this->ElasticSearchPage2;
320
		$url = rtrim($searchPageObj->Link(), '/');
321
		$url .= "/similar/Member/1";
322
		$response = $this->get($url);
323
		$this->assertEquals(200, $response->getStatusCode());
324
325
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
326
			'Class Member is either not found or not searchable');
327
	}
328
329
330 View Code Duplication
	public function testSimilarNull() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
331
		$searchPageObj = $this->ElasticSearchPage2;
332
		$url = rtrim($searchPageObj->Link(), '/');
333
		$url .= "/similar/Member/0";
334
		$response = $this->get($url);
335
		$this->assertEquals(200, $response->getStatusCode());
336
337
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
338
			'Class Member is either not found or not searchable');
339
	}
340
341
342 View Code Duplication
	public function testSimilarClassDoesNotExist() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
343
		$searchPageObj = $this->ElasticSearchPage2;
344
		$url = rtrim($searchPageObj->Link(), '/');
345
		$url .= "/similar/asdfsadfsfd/4";
346
		$response = $this->get($url);
347
		$this->assertEquals(200, $response->getStatusCode());
348
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
349
			'Class asdfsadfsfd is either not found or not searchable');
350
	}
351
352
353 View Code Duplication
	public function testSimilarSearchServerDown() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
354
		$searchPageObj = $this->ElasticSearchPage2;
355
		$url = rtrim($searchPageObj->Link(), '/');
356
		$url .= "/similar/FlickrPhotoTO/77?ServerDown=1";
357
		$response = $this->get($url);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
358
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
359
			'Unable to connect to search server');
360
	}
361
362
363 View Code Duplication
	public function testNormalSearchServerDown() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
364
		$searchPageObj = $this->ElasticSearchPage2;
365
		$url = rtrim($searchPageObj->Link(), '/');
366
		$url .= "?q=Zealand&ServerDown=1";
367
		$response = $this->get($url);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
368
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
369
			'Unable to connect to search server');
370
	}
371
372
373
	public function testSimilarValid() {
374
		$searchPageObj = $this->ElasticSearchPage2;
375
		$url = rtrim($searchPageObj->Link(), '/');
376
		$url .= "/similar/FlickrPhotoTO/77";
377
		$response = $this->get($url);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
378
		$ctr = 0;
379
380
		// results vary slightly due to sharding, hence check for a string instead of absolute results
381 View Code Duplication
		while ($ctr < 18) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
382
			$this->assertSelectorContains('div.searchResult a', $ctr, 'New Orleans, Southern Pacific');
383
			$ctr++;
384
			$this->assertSelectorStartsWithOrEquals('div.searchResult a', $ctr, 'Similar');
385
			$ctr++;
386
		}
387
388
	}
389
390
391
	/*
392
	Search for New Zealind and get search results for New Zealand.  Show option to click on
393
	actual search of New Zealind
394
	 */
395
	public function testSuggestion() {
396
		$searchPageObj = $this->ElasticSearchPage2;
397
		$url = rtrim($searchPageObj->Link(), '/');
398
		$url .= "?q=New%20Zealind&TestMode=true";
399
		$response = $this->get($url);
400
		error_log(print_r($response,1));
401
		$this->assertEquals(200, $response->getStatusCode());
402
403
		$ctr = 0;
404
405
		// results vary slightly due to sharding, hence check for a string instead of absolute results
406 View Code Duplication
		while ($ctr < 18) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
407
			$this->assertSelectorContains('div.searchResult a', $ctr, 'New');
408
			$ctr++;
409
			$this->assertSelectorStartsWithOrEquals('div.searchResult a', $ctr, 'Similar');
410
			$ctr++;
411
		}
412
413
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg', 0, 'Showing results for ');
414
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg a', 0, 'New ');
415
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg strong.hl', 0, 'Zealand');
416
417
		$this->assertSelectorStartsWithOrEquals('p.searchOriginalQueryMsg', 0, 'Search instead for ');
418
		$this->assertSelectorStartsWithOrEquals('p.searchOriginalQueryMsg a', 0, 'New Zealind');
419
420
421
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 0, 'New ');
422
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 1, 'New Zealind');
423
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 2, 'Douglas DC-10-30 cn 47847 ZK-NZM Air New Zealand Feb74 [RJF]');
424
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 3, 'Similar');
425
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 4, 'Image taken from page 555 of \'New Zealand, its physical geography, geology and natural history, with special reference to the results of Government Expeditions in the provinces of Auckland and Nelson ... Translated from the German original ... by E. Saute');
426
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 5, 'Similar');
427
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 6, 'Image taken from page 59 of \'Illustrated Handbook to Plymouth, Stonehouse & Devonport, with a new map ... New and enlarged edition\'');
428
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 7, 'Similar');
429
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 8, '[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]');
430
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 9, 'Similar');
431
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 10, '[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]');
432
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 11, 'Similar');
433
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 12, '[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]');
434
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 13, 'Similar');
435
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 14, 'Image taken from page 273 of \'Old and New London, etc\'');
436
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 15, 'Similar');
437
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 16, 'Flash Light view in new Subterranean');
438
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 17, 'Similar');
439
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 18, 'Gen. Pancho Villa Raid on Columbus, New Mexico, March 9th, at 4 A.M., 1916.');
440
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 19, 'Similar');
441
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 20, '[Texas and New Orleans, Southern Pacific Locomotive Scrap Line, Englewood Yards, Houston, Texas]');
442
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 21, 'Similar');
443
444
445
446
		// simulate following the link to search for 'New Zealind'
447
		$url = rtrim($searchPageObj->Link(), '/');
448
		$url .= '?q=New Zealind&is=1';
449
		$response = $this->get($url);
450
		$this->assertEquals(200, $response->getStatusCode());
451
452
		//Only the word New will match, Zealind does not exist.  Hence 'New York', 'New Orelans' etc
453
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 0, 'Image taken from page 59 of \'Illustrated Handbook to Plymouth, Stonehouse & Devonport, with a new map ... New and enlarged edition\'');
454
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 1, 'Similar');
455
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 2, '[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]');
456
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 3, 'Similar');
457
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 4, 'Flash Light view in new Subterranean');
458
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 5, 'Similar');
459
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 6, '[Texas and New Orleans, Southern Pacific Railroad Station, Sinton, Texas]');
460
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 7, 'Similar');
461
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 8, '[Texas and New Orleans, Southern Pacific Railroad Station, Taft, Texas]');
462
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 9, 'Similar');
463
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 10, '[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]');
464
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 11, 'Similar');
465
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 12, '[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]');
466
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 13, 'Similar');
467
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 14, 'Image taken from page 273 of \'Old and New London, etc\'');
468
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 15, 'Similar');
469
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 16, '[Texas and New Orleans, Southern Pacific, Switchman\'s Tower, San Antonio, Texas]');
470
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 17, 'Similar');
471
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 18, 'Image taken from page 143 of \'Cassell\'s Old and New Edinburgh, etc\'');
472
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 19, 'Similar');
473
474
475
476
		//no suggestions shown, the is flag prevents this
477
		$this->assertExactHTMLMatchBySelector('p.showingResultsForMsg', array());
478
		$this->assertExactHTMLMatchBySelector('p.searchOriginalQueryMsg', array());
479
480
		// reconfirm lack of suggestions when searching for 'New Zealand' with the is flag set
481
		$url = rtrim($searchPageObj->Link(), '/');
482
		$url .= '?q=New Zealand&is=1';
483
		$response = $this->get($url);
484
		$this->assertEquals(200, $response->getStatusCode());
485
		$this->assertExactHTMLMatchBySelector('p.showingResultsForMsg', array());
486
		$this->assertExactHTMLMatchBySelector('p.searchOriginalQueryMsg', array());
487
	}
488
/*
489
<p class="showingResultsForMsg">Showing results for <a href="./?q=New Zealand">New <strong class="hl">Zealand</strong></a></p>
490
<p class="searchOriginalQueryMsg">Search instead for <a href="/search-2?q=New Zealind&is=1">New Zealind</a></p>
491
492
493
 */
494
495
496 View Code Duplication
	public function testSearchOnePageNoAggregation() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
497
		$this->enableHighlights();
498
		$this->autoFollowRedirection = false;
499
		$searchTerm = 'mineralogy';
500
501
		//Note pages need to be published, by default fixtures only reside in Stage
502
		$searchPageObj = $this->ElasticSearchPage2;
503
		$url = rtrim($searchPageObj->Link(), '/');
504
		$url = $url.'?q='.$searchTerm;
505
		$response = $this->get($url);
506
		$this->assertEquals(200, $response->getStatusCode());
507
508
		//There are 3 results for mineralogy
509
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
510
			"Page 1 of 1  (3 results found");
511
512
513
		//Check all the result highlights for mineralogy matches
514
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'mineralogy');
515
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Mineralogy');
516
		$this->assertSelectorStartsWithOrEquals('strong.hl', 3, 'mineralogy');
517
		$this->assertSelectorStartsWithOrEquals('strong.hl', 4, 'Mineralogy');
518
		$this->assertSelectorStartsWithOrEquals('strong.hl', 5, 'mineralogy');
519
520
521
		// Check the start text of the 3 results
522
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 0,
523
			"Image taken from page 273 of");
524
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 1,
525
			'Image taken from page 69 of');
526
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 2,
527
			'Image taken from page 142 of');
528
529
		// No span.count means no aggregations
530
		$this->assertExactHTMLMatchBySelector('span.count', array());
531
	}
532
533
534
	/* When a search is posted, should redirect to the same URL with the search term attached.  This
535
	means that searches can be bookmarked if so required */
536
	public function testRedirection() {
537
		$this->autoFollowRedirection = false;
538
539
		//Note pages need to be published, by default fixtures only reside in Stage
540
		$searchPageObj = $this->ElasticSearchPage;
541
		$url = $searchPageObj->Link();
542
		$searchPage = $this->get($searchPageObj->URLSegment);
543
		$this->assertEquals(200, $searchPage->getStatusCode());
544
545
        $response = $this->submitForm("ElasticSearchForm_SearchForm", null, array(
546
            'q' => 'New Zealand'
547
        ));
548
549
		$url = rtrim($url, '/');
550
        $this->assertEquals(302, $response->getStatusCode());
551
        $this->assertEquals($url . '?q=New Zealand&sfid=testwithagg', $response->getHeader('Location'));
552
	}
553
554
555
	/*
556
	Add test for redirection of /search/?q=XXX to /search?q=XXX
557
	 */
558
559
560
	/*
561
	Test a search for an uncommon term, no pagination here
562
	 */
563 View Code Duplication
	public function testSearchOnePage() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
564
		$this->enableHighlights();
565
		$this->autoFollowRedirection = false;
566
		$searchTerm = 'mineralogy';
567
568
		//Note pages need to be published, by default fixtures only reside in Stage
569
		$searchPageObj = $this->ElasticSearchPage;
570
		$url = rtrim($searchPageObj->Link(), '/');
571
		$url = $url.'?q='.$searchTerm;
572
		$response = $this->get($url);
573
		$this->assertEquals(200, $response->getStatusCode());
574
575
		//There are 3 results for mineralogy
576
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
577
			"Page 1 of 1  (3 results found");
578
579
580
		//Check all the result highlights for mineralogy matches
581
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'mineralogy');
582
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Mineralogy');
583
		$this->assertSelectorStartsWithOrEquals('strong.hl', 3, 'mineralogy');
584
		$this->assertSelectorStartsWithOrEquals('strong.hl', 4, 'Mineralogy');
585
		$this->assertSelectorStartsWithOrEquals('strong.hl', 5, 'mineralogy');
586
587
588
		// Check the start text of the 3 results
589
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 0,
590
			"Image taken from page 273 of");
591
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 1,
592
			'Image taken from page 69 of');
593
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 2,
594
			'Image taken from page 142 of');
595
	}
596
597
598
599
	/*
600
	Test a search for a common term, in order to induce pagination
601
	 */
602
	public function testSiteTreeSearch() {
603
		$this->enableHighlights();
604
		$this->autoFollowRedirection = false;
605
606
		//One of the default pages
607
		$searchTerm = 'Contact Us';
608
609
		//Note pages need to be published, by default fixtures only reside in Stage
610
		$searchPageObj = $this->ElasticSearchPage;
611
		$searchPageObj->SiteTreeOnly = true;
612
		$searchPageObj->write();
613
		$searchPageObj->publish('Stage', 'Live');
614
615
616
		$pageLength = 10; // the default
617
		$searchPageObj->ResultsPerPage = $pageLength;
618
		$url = rtrim($searchPageObj->Link(), '/');
619
		$url = $url . '?q=' . $searchTerm;
620
		$firstPageURL = $url;
0 ignored issues
show
Unused Code introduced by
$firstPageURL is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
621
		$response = $this->get($url);
622
		$this->assertEquals(200, $response->getStatusCode());
623
624
		//There are 2 results for 'Contact Us', as 'About Us' has an Us in the title.
625
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
626
			"Page 1 of 1  (2 results found in");
627
628
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
629
		//the apparently erroneous addition of 1 to the required 2
630
		$this->assertNumberOfNodes('div.searchResult', 3);
631
632
		$this->assertSelectorStartsWithOrEquals('strong.hl', 0, 'Contact'); // CONTACT US
633
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'Us'); // Contact US
634
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Us'); // About US
635
636
	}
637
638
639
640
	/*
641
	Test a search for a common term, in order to induce pagination
642
	 */
643
	public function testSearchSeveralPagesPage() {
644
		$this->enableHighlights();
645
		$this->autoFollowRedirection = false;
646
		$searchTerm = 'railroad';
647
648
		//Note pages need to be published, by default fixtures only reside in Stage
649
		$searchPageObj = $this->ElasticSearchPage;
650
		$pageLength = 10; // the default
651
		$searchPageObj->ResultsPerPage = $pageLength;
652
		$url = rtrim($searchPageObj->Link(), '/');
653
		$url = $url . '?q=' . $searchTerm;
654
		$firstPageURL = $url;
655
		$response = $this->get($url);
656
		$this->assertEquals(200, $response->getStatusCode());
657
658
		//There are 3 results for mineralogy
659
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
660
			"Page 1 of 2  (11 results found in");
661
662
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
663
		//the apparently erroneous addition of 1 to the required 10
664
		$this->assertNumberOfNodes('div.searchResult', 11);
665
666
		//Check for a couple of highlighed 'Railroad' terms
667
		$this->assertSelectorStartsWithOrEquals('strong.hl', 0, 'Railroad');
668
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'Railroad');
669
670
		$this->assertSelectorStartsWithOrEquals('div.pagination a', 0, '2');
671
		$this->assertSelectorStartsWithOrEquals('div.pagination a.next', 0, '→');
672
673
		$resultsP1 = $this->collateSearchResults();
674
675
		$page2url = $url . '&start=' . $pageLength;
676
677
		//Check pagination on page 2
678
		$response2 = $this->get($page2url);
679
		$this->assertEquals(200, $response2->getStatusCode());
680
681
		//FIXME pluralisation probably needs fixed here, change test later acoordingly
682
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
683
			"Page 2 of 2  (11 results found in");
684
685
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
686
		//the apparently erroneous addition of 1 to the required 1
687
		$this->assertNumberOfNodes('div.searchResult', 2);
688
689
		$this->assertSelectorStartsWithOrEquals('div.pagination a', 1, '1');
690
		$this->assertSelectorStartsWithOrEquals('div.pagination a.prev', 0, '←');
691
692
		$resultsP2 = $this->collateSearchResults();
693
694
		$resultsFrom2Pages = array_merge($resultsP1, $resultsP2);
695
696
		//there are 11 results in total
697
		$this->assertEquals(11, sizeof($resultsFrom2Pages));
698
699
		//increase the number of results and assert that they are the same as per pages 1,2 joined
700
		$searchPageObj->ResultsPerPage = 20;
701
		$searchPageObj->write();
702
		$searchPageObj->publish('Stage','Live');
703
		$response3 = $this->get($firstPageURL);
0 ignored issues
show
Unused Code introduced by
$response3 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
704
	}
705
706
707
	private function enableHighlights() {
708
		foreach (SearchableField::get()->filter('Name', 'Title') as $sf) {
709
			$sf->ShowHighlights = true;
710
			$sf->write();
711
		}
712
713
		foreach (SearchableField::get()->filter('Name', 'Content') as $sf) {
714
			$sf->ShowHighlights = true;
715
			$sf->write();
716
		}
717
718
		//FIXME - do this with ORM
719
		//$sql = "UPDATE ElasticSearchPage_ElasticaSearchableFields SET "
720
	}
721
}
722