Completed
Push — dev2 ( 01a103...4a0707 )
by Gordon
20:37 queued 05:45
created

testSimilarClassDoesNotExist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 9
loc 9
ccs 0
cts 7
cp 0
rs 9.6667
cc 1
eloc 8
nc 1
nop 0
crap 2
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
406
407
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg', 0, 'Showing results for ');
408
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg a', 0, 'New ');
409
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg strong.hl', 0, 'Zealand');
410
411
		$this->assertSelectorStartsWithOrEquals('p.searchOriginalQueryMsg', 0, 'Search instead for ');
412
		$this->assertSelectorStartsWithOrEquals('p.searchOriginalQueryMsg a', 0, 'New Zealind');
413
414
		// simulate following the link to search for 'New Zealind'
415
		$url = rtrim($searchPageObj->Link(), '/');
416
		$url .= '?q=New Zealind&is=1';
417
		$response = $this->get($url);
418
		$this->assertEquals(200, $response->getStatusCode());
419
420
				// results vary slightly due to sharding, hence check for a string instead of absolute results
421 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...
422
			$this->assertSelectorContains('div.searchResult a', $ctr, 'New', true);
423
			$ctr++;
424
			$this->assertSelectorStartsWithOrEquals('div.searchResult a', $ctr, 'Similar');
425
			$ctr++;
426
		}
427
428
		//Only the word New will match, Zealind does not exist.  Hence 'New York', 'New Orelans' etc
429
		$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\'');
430
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 1, 'Similar');
431
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 2, '[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]');
432
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 3, 'Similar');
433
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 4, 'Flash Light view in new Subterranean');
434
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 5, 'Similar');
435
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 6, '[Texas and New Orleans, Southern Pacific Railroad Station, Sinton, Texas]');
436
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 7, 'Similar');
437
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 8, '[Texas and New Orleans, Southern Pacific Railroad Station, Taft, Texas]');
438
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 9, 'Similar');
439
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 10, '[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]');
440
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 11, 'Similar');
441
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 12, '[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]');
442
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 13, 'Similar');
443
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 14, 'Image taken from page 273 of \'Old and New London, etc\'');
444
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 15, 'Similar');
445
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 16, '[Texas and New Orleans, Southern Pacific, Switchman\'s Tower, San Antonio, Texas]');
446
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 17, 'Similar');
447
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 18, 'Image taken from page 143 of \'Cassell\'s Old and New Edinburgh, etc\'');
448
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 19, 'Similar');
449
450
451
452
		//no suggestions shown, the is flag prevents this
453
		$this->assertExactHTMLMatchBySelector('p.showingResultsForMsg', array());
454
		$this->assertExactHTMLMatchBySelector('p.searchOriginalQueryMsg', array());
455
456
		// reconfirm lack of suggestions when searching for 'New Zealand' with the is flag set
457
		$url = rtrim($searchPageObj->Link(), '/');
458
		$url .= '?q=New Zealand&is=1';
459
		$response = $this->get($url);
460
		$this->assertEquals(200, $response->getStatusCode());
461
		$this->assertExactHTMLMatchBySelector('p.showingResultsForMsg', array());
462
		$this->assertExactHTMLMatchBySelector('p.searchOriginalQueryMsg', array());
463
	}
464
/*
465
<p class="showingResultsForMsg">Showing results for <a href="./?q=New Zealand">New <strong class="hl">Zealand</strong></a></p>
466
<p class="searchOriginalQueryMsg">Search instead for <a href="/search-2?q=New Zealind&is=1">New Zealind</a></p>
467
468
469
 */
470
471
472 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...
473
		$this->enableHighlights();
474
		$this->autoFollowRedirection = false;
475
		$searchTerm = 'mineralogy';
476
477
		//Note pages need to be published, by default fixtures only reside in Stage
478
		$searchPageObj = $this->ElasticSearchPage2;
479
		$url = rtrim($searchPageObj->Link(), '/');
480
		$url = $url.'?q='.$searchTerm;
481
		$response = $this->get($url);
482
		$this->assertEquals(200, $response->getStatusCode());
483
484
		//There are 3 results for mineralogy
485
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
486
			"Page 1 of 1  (3 results found");
487
488
489
		//Check all the result highlights for mineralogy matches
490
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'mineralogy');
491
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Mineralogy');
492
		$this->assertSelectorStartsWithOrEquals('strong.hl', 3, 'mineralogy');
493
		$this->assertSelectorStartsWithOrEquals('strong.hl', 4, 'Mineralogy');
494
		$this->assertSelectorStartsWithOrEquals('strong.hl', 5, 'mineralogy');
495
496
497
		// Check the start text of the 3 results
498
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 0,
499
			"Image taken from page 273 of");
500
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 1,
501
			'Image taken from page 69 of');
502
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 2,
503
			'Image taken from page 142 of');
504
505
		// No span.count means no aggregations
506
		$this->assertExactHTMLMatchBySelector('span.count', array());
507
	}
508
509
510
	/* When a search is posted, should redirect to the same URL with the search term attached.  This
511
	means that searches can be bookmarked if so required */
512
	public function testRedirection() {
513
		$this->autoFollowRedirection = false;
514
515
		//Note pages need to be published, by default fixtures only reside in Stage
516
		$searchPageObj = $this->ElasticSearchPage;
517
		$url = $searchPageObj->Link();
518
		$searchPage = $this->get($searchPageObj->URLSegment);
519
		$this->assertEquals(200, $searchPage->getStatusCode());
520
521
        $response = $this->submitForm("ElasticSearchForm_SearchForm", null, array(
522
            'q' => 'New Zealand'
523
        ));
524
525
		$url = rtrim($url, '/');
526
        $this->assertEquals(302, $response->getStatusCode());
527
        $this->assertEquals($url . '?q=New Zealand&sfid=testwithagg', $response->getHeader('Location'));
528
	}
529
530
531
	/*
532
	Add test for redirection of /search/?q=XXX to /search?q=XXX
533
	 */
534
535
536
	/*
537
	Test a search for an uncommon term, no pagination here
538
	 */
539 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...
540
		$this->enableHighlights();
541
		$this->autoFollowRedirection = false;
542
		$searchTerm = 'mineralogy';
543
544
		//Note pages need to be published, by default fixtures only reside in Stage
545
		$searchPageObj = $this->ElasticSearchPage;
546
		$url = rtrim($searchPageObj->Link(), '/');
547
		$url = $url.'?q='.$searchTerm;
548
		$response = $this->get($url);
549
		$this->assertEquals(200, $response->getStatusCode());
550
551
		//There are 3 results for mineralogy
552
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
553
			"Page 1 of 1  (3 results found");
554
555
556
		//Check all the result highlights for mineralogy matches
557
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'mineralogy');
558
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Mineralogy');
559
		$this->assertSelectorStartsWithOrEquals('strong.hl', 3, 'mineralogy');
560
		$this->assertSelectorStartsWithOrEquals('strong.hl', 4, 'Mineralogy');
561
		$this->assertSelectorStartsWithOrEquals('strong.hl', 5, 'mineralogy');
562
563
564
		// Check the start text of the 3 results
565
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 0,
566
			"Image taken from page 273 of");
567
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 1,
568
			'Image taken from page 69 of');
569
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 2,
570
			'Image taken from page 142 of');
571
	}
572
573
574
575
	/*
576
	Test a search for a common term, in order to induce pagination
577
	 */
578
	public function testSiteTreeSearch() {
579
		$this->enableHighlights();
580
		$this->autoFollowRedirection = false;
581
582
		//One of the default pages
583
		$searchTerm = 'Contact Us';
584
585
		//Note pages need to be published, by default fixtures only reside in Stage
586
		$searchPageObj = $this->ElasticSearchPage;
587
		$searchPageObj->SiteTreeOnly = true;
588
		$searchPageObj->write();
589
		$searchPageObj->publish('Stage', 'Live');
590
591
592
		$pageLength = 10; // the default
593
		$searchPageObj->ResultsPerPage = $pageLength;
594
		$url = rtrim($searchPageObj->Link(), '/');
595
		$url = $url . '?q=' . $searchTerm;
596
		$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...
597
		$response = $this->get($url);
598
		$this->assertEquals(200, $response->getStatusCode());
599
600
		//There are 2 results for 'Contact Us', as 'About Us' has an Us in the title.
601
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
602
			"Page 1 of 1  (2 results found in");
603
604
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
605
		//the apparently erroneous addition of 1 to the required 2
606
		$this->assertNumberOfNodes('div.searchResult', 3);
607
608
		$this->assertSelectorStartsWithOrEquals('strong.hl', 0, 'Contact'); // CONTACT US
609
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'Us'); // Contact US
610
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Us'); // About US
611
612
	}
613
614
615
616
	/*
617
	Test a search for a common term, in order to induce pagination
618
	 */
619
	public function testSearchSeveralPagesPage() {
620
		$this->enableHighlights();
621
		$this->autoFollowRedirection = false;
622
		$searchTerm = 'railroad';
623
624
		//Note pages need to be published, by default fixtures only reside in Stage
625
		$searchPageObj = $this->ElasticSearchPage;
626
		$pageLength = 10; // the default
627
		$searchPageObj->ResultsPerPage = $pageLength;
628
		$url = rtrim($searchPageObj->Link(), '/');
629
		$url = $url . '?q=' . $searchTerm;
630
		$firstPageURL = $url;
631
		$response = $this->get($url);
632
		$this->assertEquals(200, $response->getStatusCode());
633
634
		//There are 3 results for mineralogy
635
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
636
			"Page 1 of 2  (11 results found in");
637
638
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
639
		//the apparently erroneous addition of 1 to the required 10
640
		$this->assertNumberOfNodes('div.searchResult', 11);
641
642
		//Check for a couple of highlighed 'Railroad' terms
643
		$this->assertSelectorStartsWithOrEquals('strong.hl', 0, 'Railroad');
644
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'Railroad');
645
646
		$this->assertSelectorStartsWithOrEquals('div.pagination a', 0, '2');
647
		$this->assertSelectorStartsWithOrEquals('div.pagination a.next', 0, '→');
648
649
		$resultsP1 = $this->collateSearchResults();
650
651
		$page2url = $url . '&start=' . $pageLength;
652
653
		//Check pagination on page 2
654
		$response2 = $this->get($page2url);
655
		$this->assertEquals(200, $response2->getStatusCode());
656
657
		//FIXME pluralisation probably needs fixed here, change test later acoordingly
658
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
659
			"Page 2 of 2  (11 results found in");
660
661
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
662
		//the apparently erroneous addition of 1 to the required 1
663
		$this->assertNumberOfNodes('div.searchResult', 2);
664
665
		$this->assertSelectorStartsWithOrEquals('div.pagination a', 1, '1');
666
		$this->assertSelectorStartsWithOrEquals('div.pagination a.prev', 0, '←');
667
668
		$resultsP2 = $this->collateSearchResults();
669
670
		$resultsFrom2Pages = array_merge($resultsP1, $resultsP2);
671
672
		//there are 11 results in total
673
		$this->assertEquals(11, sizeof($resultsFrom2Pages));
674
675
		//increase the number of results and assert that they are the same as per pages 1,2 joined
676
		$searchPageObj->ResultsPerPage = 20;
677
		$searchPageObj->write();
678
		$searchPageObj->publish('Stage','Live');
679
		$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...
680
	}
681
682
683
	private function enableHighlights() {
684
		foreach (SearchableField::get()->filter('Name', 'Title') as $sf) {
685
			$sf->ShowHighlights = true;
686
			$sf->write();
687
		}
688
689
		foreach (SearchableField::get()->filter('Name', 'Content') as $sf) {
690
			$sf->ShowHighlights = true;
691
			$sf->write();
692
		}
693
694
		//FIXME - do this with ORM
695
		//$sql = "UPDATE ElasticSearchPage_ElasticaSearchableFields SET "
696
	}
697
}
698