Completed
Push — dev2 ( 774f9d...0919fb )
by Gordon
05:50
created

testQueryInSearchBoxForOneFormOnly()   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 9
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
        $this->assertEquals(200, $response->getStatusCode());
79
80
        $this->assertSelectorStartsWithOrEquals('span.count', 0, '(5)');
81
		$this->assertSelectorStartsWithOrEquals('span.count', 1, '(11)');
82
		$this->assertSelectorStartsWithOrEquals('span.count', 2, '(12)');
83
		$this->assertSelectorStartsWithOrEquals('span.count', 3, '(13)');
84
		$this->assertSelectorStartsWithOrEquals('span.count', 4, '(16)');
85
		$this->assertSelectorStartsWithOrEquals('span.count', 5, '(13)');
86
		$this->assertSelectorStartsWithOrEquals('span.count', 6, '(11)');
87
		$this->assertSelectorStartsWithOrEquals('span.count', 7, '(19)');
88
		$this->assertSelectorStartsWithOrEquals('span.count', 8, '(12)');
89
		$this->assertSelectorStartsWithOrEquals('span.count', 9, '(11)');
90
		$this->assertSelectorStartsWithOrEquals('span.count', 10, '(11)');
91
		$this->assertSelectorStartsWithOrEquals('span.count', 11, '(20)');
92
		$this->assertSelectorStartsWithOrEquals('span.count', 12, '(12)');
93
		$this->assertSelectorStartsWithOrEquals('span.count', 13, '(17)');
94
		$this->assertSelectorStartsWithOrEquals('span.count', 14, '(17)');
95
		$this->assertSelectorStartsWithOrEquals('span.count', 15, '(17)');
96
		$this->assertSelectorStartsWithOrEquals('span.count', 16, '(15)');
97
		$this->assertSelectorStartsWithOrEquals('span.count', 17, '(17)');
98
		$this->assertSelectorStartsWithOrEquals('span.count', 18, '(10)');
99
		$this->assertSelectorStartsWithOrEquals('span.count', 19, '(18)');
100
		$this->assertSelectorStartsWithOrEquals('span.count', 20, '(1)');
101
		$this->assertSelectorStartsWithOrEquals('span.count', 21, '(10)');
102
		$this->assertSelectorStartsWithOrEquals('span.count', 22, '(12)');
103
		$this->assertSelectorStartsWithOrEquals('span.count', 23, '(21)');
104
		$this->assertSelectorStartsWithOrEquals('span.count', 24, '(23)');
105
		$this->assertSelectorStartsWithOrEquals('span.count', 25, '(17)');
106
		$this->assertSelectorStartsWithOrEquals('span.count', 26, '(16)');
107
		$this->assertSelectorStartsWithOrEquals('span.count', 27, '(23)');
108
		$this->assertSelectorStartsWithOrEquals('span.count', 28, '(9)');
109
		$this->assertSelectorStartsWithOrEquals('span.count', 29, '(31)');
110
		$this->assertSelectorStartsWithOrEquals('span.count', 30, '(16)');
111
		$this->assertSelectorStartsWithOrEquals('span.count', 31, '(39)');
112
		$this->assertSelectorStartsWithOrEquals('span.count', 32, '(5)');
113
	}
114
115
116
	public function testAggregationOneSelected() {
117
		//SearchHelper
118
119
		$this->autoFollowRedirection = false;
120
121
		//Note pages need to be published, by default fixtures only reside in Stage
122
		$searchPageObj = $this->ElasticSearchPage;
123
124
		$url = $searchPageObj->Link();
125
		$searchPage = $this->get($searchPageObj->URLSegment);
126
		$this->assertEquals(200, $searchPage->getStatusCode());
127
		$url = rtrim($url, '/');
128
		$url .= '?ISO=400';
129
130
        $response = $this->get($url);
131
        $this->assertEquals(200, $response->getStatusCode());
132
133
        // These are less than in the no facets selected case, as expected
134
		$this->assertSelectorStartsWithOrEquals('span.count', 0, '(2)');
135
		$this->assertSelectorStartsWithOrEquals('span.count', 1, '(1)');
136
		$this->assertSelectorStartsWithOrEquals('span.count', 2, '(2)');
137
		$this->assertSelectorStartsWithOrEquals('span.count', 3, '(1)');
138
		$this->assertSelectorStartsWithOrEquals('span.count', 4, '(3)');
139
		$this->assertSelectorStartsWithOrEquals('span.count', 5, '(1)');
140
		$this->assertSelectorStartsWithOrEquals('span.count', 6, '(3)');
141
		$this->assertSelectorStartsWithOrEquals('span.count', 7, '(3)');
142
		$this->assertSelectorStartsWithOrEquals('span.count', 8, '(2)');
143
		$this->assertSelectorStartsWithOrEquals('span.count', 9, '(2)');
144
		$this->assertSelectorStartsWithOrEquals('span.count', 10, '(1)');
145
		$this->assertSelectorStartsWithOrEquals('span.count', 11, '(2)');
146
		$this->assertSelectorStartsWithOrEquals('span.count', 12, '(3)');
147
		$this->assertSelectorStartsWithOrEquals('span.count', 13, '(3)');
148
		$this->assertSelectorStartsWithOrEquals('span.count', 14, '(4)');
149
		$this->assertSelectorStartsWithOrEquals('span.count', 15, '(3)');
150
		$this->assertSelectorStartsWithOrEquals('span.count', 16, '(1)');
151
		$this->assertSelectorStartsWithOrEquals('span.count', 17, '(2)');
152
		$this->assertSelectorStartsWithOrEquals('span.count', 18, '(0)');
153
		$this->assertSelectorStartsWithOrEquals('span.count', 19, '(4)');
154
		$this->assertSelectorStartsWithOrEquals('span.count', 20, '(1)');
155
		$this->assertSelectorStartsWithOrEquals('span.count', 21, '(7)');
156
		$this->assertSelectorStartsWithOrEquals('span.count', 22, '(1)');
157
158
159
	}
160
161
162
163
	public function testAggregationTwoSelected() {
164
		//SearchHelper
165
166
		$this->autoFollowRedirection = false;
167
168
		//Note pages need to be published, by default fixtures only reside in Stage
169
		$searchPageObj = $this->ElasticSearchPage;
170
171
		$url = $searchPageObj->Link();
172
		$searchPage = $this->get($searchPageObj->URLSegment);
173
		$this->assertEquals(200, $searchPage->getStatusCode());
174
		$url = rtrim($url, '/');
175
		$url .= '?ISO=400&ShutterSpeed=2%2F250';
176
177
        $response = $this->get($url);
178
        $this->assertEquals(200, $response->getStatusCode());
179
180
        // These are less than in the one facet selected case, as expected
181
        $this->assertSelectorStartsWithOrEquals('span.count', 0, '(1)');
182
		$this->assertSelectorStartsWithOrEquals('span.count', 1, '(2)');
183
		$this->assertSelectorStartsWithOrEquals('span.count', 2, '(1)');
184
		$this->assertSelectorStartsWithOrEquals('span.count', 3, '(1)');
185
		$this->assertSelectorStartsWithOrEquals('span.count', 4, '(1)');
186
		$this->assertSelectorStartsWithOrEquals('span.count', 5, '(0)');
187
		$this->assertSelectorStartsWithOrEquals('span.count', 6, '(1)');
188
		$this->assertSelectorStartsWithOrEquals('span.count', 7, '(0)');
189
		$this->assertSelectorStartsWithOrEquals('span.count', 8, '(2)');
190
		$this->assertSelectorStartsWithOrEquals('span.count', 9, '(0)');
191
192
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
244
245
246 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...
247
		$searchPageObj = $this->ElasticSearchPage2;
248
		$url = rtrim($searchPageObj->Link(), '/');
249
		$url .= "?q=Auckland&sfid=".$searchPageObj->Identifier;
250
		$response = $this->get($url);
251
		$this->assertEquals(200, $response->getStatusCode());
252
		$this->assertAttributeHasExactValue('#ElasticSearchForm_SearchForm_q', 'q',
253
			'Auckland');
254
	}
255
256
257
	/*
258
	Check the search form attributes for autocomplete info
259
	 */
260
	public function testSearchFormAutocompleteEnabled() {
261
		// prepeare autocomplete field
262
		$this->autoFollowRedirection = false;
263
264
		//Note pages need to be published, by default fixtures only reside in Stage
265
		$searchPageObj = $this->ElasticSearchPage2;
266
		$searchPageObj->ClassesToSearch = 'FlickrPhotoTO';
267
		$filter = array('ClazzName' => 'FlickrPhotoTO', 'Name' => 'Title');
268
		$sfid = SearchableField::get()->filter($filter)->first()->ID;
269
		$searchPageObj->AutoCompleteFieldID = $sfid;
270
		$pageLength = 10; // the default
271
		$searchPageObj->ResultsPerPage = $pageLength;
272
		$searchPageObj->write();
273
		$searchPageObj->publish('Stage', 'Live');
274
275
		$url = rtrim($searchPageObj->Link(), '/');
276
		$response = $this->get($url);
277
		$this->assertEquals(200, $response->getStatusCode());
278
279
		// note, need to test with strings
280
		$expected = array(
281
			'data-autocomplete' => 'true',
282
			'data-autocomplete-field' => 'Title',
283
			'data-autocomplete-field' => 'Title',
284
			'data-autocomplete-classes' => 'FlickrPhotoTO',
285
			'data-autocomplete-sitetree' => '0',
286
			'data-autocomplete-source' => '/search-2/'
287
		);
288
		$this->assertAttributesHaveExactValues('#ElasticSearchForm_SearchForm_q', $expected);
289
	}
290
291
292
	public function testTemplateOverrideExtension() {
293
294
		if(!class_exists('PageControllerTemplateOverrideExtension')) {
295
			$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...
296
		}
297
298
		Page_Controller::remove_extension('PageControllerTemplateOverrideExtension');
299
		$searchPageObj = $this->ElasticSearchPage2;
300
301
		$url = rtrim($searchPageObj->Link(), '/');
302
		$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...
303
		$this->assertEquals(0, PageControllerTemplateOverrideExtension::getTemplateOverrideCounter());
304
		Page_Controller::add_extension('PageControllerTemplateOverrideExtension');
305
		$searchPageObj = $this->ElasticSearchPage2;
306
		$url = rtrim($searchPageObj->Link(), '/');
307
		$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...
308
		$this->assertEquals(1, PageControllerTemplateOverrideExtension::getTemplateOverrideCounter());
309
310
		$url .= "/similar/FlickrPhotoTO/77";
311
		$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...
312
313
		//check the template override method was called
314
		$this->assertEquals(2, PageControllerTemplateOverrideExtension::getTemplateOverrideCounter());
315
316
		Page_Controller::remove_extension('PageControllerTemplateOverrideExtension');
317
318
	}
319
320
321 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...
322
		$searchPageObj = $this->ElasticSearchPage2;
323
		$url = rtrim($searchPageObj->Link(), '/');
324
		$url .= "/similar/Member/1";
325
		$response = $this->get($url);
326
		$this->assertEquals(200, $response->getStatusCode());
327
328
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
329
			'Class Member is either not found or not searchable');
330
	}
331
332
333 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...
334
		$searchPageObj = $this->ElasticSearchPage2;
335
		$url = rtrim($searchPageObj->Link(), '/');
336
		$url .= "/similar/Member/0";
337
		$response = $this->get($url);
338
		$this->assertEquals(200, $response->getStatusCode());
339
340
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
341
			'Class Member is either not found or not searchable');
342
	}
343
344
345 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...
346
		$searchPageObj = $this->ElasticSearchPage2;
347
		$url = rtrim($searchPageObj->Link(), '/');
348
		$url .= "/similar/asdfsadfsfd/4";
349
		$response = $this->get($url);
350
		$this->assertEquals(200, $response->getStatusCode());
351
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
352
			'Class asdfsadfsfd is either not found or not searchable');
353
	}
354
355
356 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...
357
		$searchPageObj = $this->ElasticSearchPage2;
358
		$url = rtrim($searchPageObj->Link(), '/');
359
		$url .= "/similar/FlickrPhotoTO/77?ServerDown=1";
360
		$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...
361
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
362
			'Unable to connect to search server');
363
	}
364
365
366 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...
367
		$searchPageObj = $this->ElasticSearchPage2;
368
		$url = rtrim($searchPageObj->Link(), '/');
369
		$url .= "?q=Zealand&ServerDown=1";
370
		$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...
371
		$this->assertSelectorStartsWithOrEquals('div.error', 0,
372
			'Unable to connect to search server');
373
	}
374
375
376
	public function testSimilarValid() {
377
		$searchPageObj = $this->ElasticSearchPage2;
378
		$url = rtrim($searchPageObj->Link(), '/');
379
		$url .= "/similar/FlickrPhotoTO/77";
380
		$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...
381
382
		//Title of the original is "[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]"
383
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 0, '[ and New Orleans, Southern Pacific Railroad Station, Sinton, ]');
384
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 1, 'Similar');
385
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 2, '[ and New Orleans, Southern Pacific Railroad Station, Taft, ]');
386
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 3, 'Similar');
387
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 4, '[ and New Orleans, Southern Pacific Passenger Station, Waxahachie, ]');
388
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 5, 'Similar');
389
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 6, '[ and New Orleans, Southern Pacific, Tower No. 63, Mexia, ]');
390
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 7, 'Similar');
391
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 8, '[ and New Orleans, Southern Pacific Locomotive Scrap Line, Englewood Yards, Houston, ]');
392
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 9, 'Similar');
393
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 10, '[ and New Orleans, Southern Pacific Railroad Station, Stockdale, ]');
394
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 11, 'Similar');
395
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 12, '[ and New Orleans, Southern Pacific Freight Station, Waxahachie, ]');
396
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 13, 'Similar');
397
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 14, '[ and New Orleans, Southern Pacific, Eakin Street Yard Office, Dallas, ]');
398
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 15, 'Similar');
399
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 16, '[ and New Orleans, Southern Pacific, Switchman\'s Tower, San Antonio, ]');
400
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 17, 'Similar');
401
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 18, 'Villa Deserters Conducted to 11th Inf. Headquarters.');
402
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 19, 'Similar');
403
	}
404
405
406
	/*
407
	Search for New Zealind and get search results for New Zealand.  Show option to click on
408
	actual search of New Zealind
409
	 */
410
	public function testSuggestion() {
411
		$searchPageObj = $this->ElasticSearchPage2;
412
		$url = rtrim($searchPageObj->Link(), '/');
413
		$url .= "?q=New%20Zealind&TestMode=true";
414
		$response = $this->get($url);
415
		$this->assertEquals(200, $response->getStatusCode());
416
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg', 0, 'Showing results for ');
417
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg a', 0, 'New ');
418
		$this->assertSelectorStartsWithOrEquals('p.showingResultsForMsg strong.hl', 0, 'Zealand');
419
420
		$this->assertSelectorStartsWithOrEquals('p.searchOriginalQueryMsg', 0, 'Search instead for ');
421
		$this->assertSelectorStartsWithOrEquals('p.searchOriginalQueryMsg a', 0, 'New Zealind');
422
423
424
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 0, 'New ');
425
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 1, 'New Zealind');
426
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 2, 'Douglas DC-10-30 cn 47847 ZK-NZM Air New Zealand Feb74 [RJF]');
427
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 3, 'Similar');
428
		$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');
429
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 5, 'Similar');
430
		$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\'');
431
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 7, 'Similar');
432
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 8, '[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]');
433
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 9, 'Similar');
434
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 10, '[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]');
435
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 11, 'Similar');
436
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 12, '[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]');
437
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 13, 'Similar');
438
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 14, 'Image taken from page 273 of \'Old and New London, etc\'');
439
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 15, 'Similar');
440
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 16, 'Flash Light view in new Subterranean');
441
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 17, 'Similar');
442
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 18, 'Gen. Pancho Villa Raid on Columbus, New Mexico, March 9th, at 4 A.M., 1916.');
443
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 19, 'Similar');
444
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 20, '[Texas and New Orleans, Southern Pacific Locomotive Scrap Line, Englewood Yards, Houston, Texas]');
445
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 21, 'Similar');
446
447
448
449
		// simulate following the link to search for 'New Zealind'
450
		$url = rtrim($searchPageObj->Link(), '/');
451
		$url .= '?q=New Zealind&is=1';
452
		$response = $this->get($url);
453
		$this->assertEquals(200, $response->getStatusCode());
454
455
		//Only the word New will match, Zealind does not exist.  Hence 'New York', 'New Orelans' etc
456
		$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\'');
457
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 1, 'Similar');
458
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 2, '[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]');
459
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 3, 'Similar');
460
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 4, 'Flash Light view in new Subterranean');
461
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 5, 'Similar');
462
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 6, '[Texas and New Orleans, Southern Pacific Railroad Station, Sinton, Texas]');
463
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 7, 'Similar');
464
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 8, '[Texas and New Orleans, Southern Pacific Railroad Station, Taft, Texas]');
465
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 9, 'Similar');
466
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 10, '[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]');
467
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 11, 'Similar');
468
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 12, '[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]');
469
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 13, 'Similar');
470
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 14, 'Image taken from page 273 of \'Old and New London, etc\'');
471
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 15, 'Similar');
472
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 16, '[Texas and New Orleans, Southern Pacific, Switchman\'s Tower, San Antonio, Texas]');
473
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 17, 'Similar');
474
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 18, 'Image taken from page 143 of \'Cassell\'s Old and New Edinburgh, etc\'');
475
		$this->assertSelectorStartsWithOrEquals('div.searchResult a', 19, 'Similar');
476
477
478
479
		//no suggestions shown, the is flag prevents this
480
		$this->assertExactHTMLMatchBySelector('p.showingResultsForMsg', array());
481
		$this->assertExactHTMLMatchBySelector('p.searchOriginalQueryMsg', array());
482
483
		// reconfirm lack of suggestions when searching for 'New Zealand' with the is flag set
484
		$url = rtrim($searchPageObj->Link(), '/');
485
		$url .= '?q=New Zealand&is=1';
486
		$response = $this->get($url);
487
		$this->assertEquals(200, $response->getStatusCode());
488
		$this->assertExactHTMLMatchBySelector('p.showingResultsForMsg', array());
489
		$this->assertExactHTMLMatchBySelector('p.searchOriginalQueryMsg', array());
490
	}
491
/*
492
<p class="showingResultsForMsg">Showing results for <a href="./?q=New Zealand">New <strong class="hl">Zealand</strong></a></p>
493
<p class="searchOriginalQueryMsg">Search instead for <a href="/search-2?q=New Zealind&is=1">New Zealind</a></p>
494
495
496
 */
497
498
499 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...
500
		$this->enableHighlights();
501
		$this->autoFollowRedirection = false;
502
		$searchTerm = 'mineralogy';
503
504
		//Note pages need to be published, by default fixtures only reside in Stage
505
		$searchPageObj = $this->ElasticSearchPage2;
506
		$url = rtrim($searchPageObj->Link(), '/');
507
		$url = $url.'?q='.$searchTerm;
508
		$response = $this->get($url);
509
		$this->assertEquals(200, $response->getStatusCode());
510
511
		//There are 3 results for mineralogy
512
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
513
			"Page 1 of 1  (3 results found");
514
515
516
		//Check all the result highlights for mineralogy matches
517
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'mineralogy');
518
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Mineralogy');
519
		$this->assertSelectorStartsWithOrEquals('strong.hl', 3, 'mineralogy');
520
		$this->assertSelectorStartsWithOrEquals('strong.hl', 4, 'Mineralogy');
521
		$this->assertSelectorStartsWithOrEquals('strong.hl', 5, 'mineralogy');
522
523
524
		// Check the start text of the 3 results
525
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 0,
526
			"Image taken from page 273 of");
527
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 1,
528
			'Image taken from page 69 of');
529
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 2,
530
			'Image taken from page 142 of');
531
532
		// No span.count means no aggregations
533
		$this->assertExactHTMLMatchBySelector('span.count', array());
534
	}
535
536
537
	/* When a search is posted, should redirect to the same URL with the search term attached.  This
538
	means that searches can be bookmarked if so required */
539
	public function testRedirection() {
540
		$this->autoFollowRedirection = false;
541
542
		//Note pages need to be published, by default fixtures only reside in Stage
543
		$searchPageObj = $this->ElasticSearchPage;
544
		$url = $searchPageObj->Link();
545
		$searchPage = $this->get($searchPageObj->URLSegment);
546
		$this->assertEquals(200, $searchPage->getStatusCode());
547
548
        $response = $this->submitForm("ElasticSearchForm_SearchForm", null, array(
549
            'q' => 'New Zealand'
550
        ));
551
552
		$url = rtrim($url, '/');
553
        $this->assertEquals(302, $response->getStatusCode());
554
        $this->assertEquals($url . '?q=New Zealand&sfid=testwithagg', $response->getHeader('Location'));
555
	}
556
557
558
	/*
559
	Add test for redirection of /search/?q=XXX to /search?q=XXX
560
	 */
561
562
563
	/*
564
	Test a search for an uncommon term, no pagination here
565
	 */
566 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...
567
		$this->enableHighlights();
568
		$this->autoFollowRedirection = false;
569
		$searchTerm = 'mineralogy';
570
571
		//Note pages need to be published, by default fixtures only reside in Stage
572
		$searchPageObj = $this->ElasticSearchPage;
573
		$url = rtrim($searchPageObj->Link(), '/');
574
		$url = $url.'?q='.$searchTerm;
575
		$response = $this->get($url);
576
		$this->assertEquals(200, $response->getStatusCode());
577
578
		//There are 3 results for mineralogy
579
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
580
			"Page 1 of 1  (3 results found");
581
582
583
		//Check all the result highlights for mineralogy matches
584
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'mineralogy');
585
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Mineralogy');
586
		$this->assertSelectorStartsWithOrEquals('strong.hl', 3, 'mineralogy');
587
		$this->assertSelectorStartsWithOrEquals('strong.hl', 4, 'Mineralogy');
588
		$this->assertSelectorStartsWithOrEquals('strong.hl', 5, 'mineralogy');
589
590
591
		// Check the start text of the 3 results
592
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 0,
593
			"Image taken from page 273 of");
594
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 1,
595
			'Image taken from page 69 of');
596
		$this->assertSelectorStartsWithOrEquals('div.searchResult h4 a', 2,
597
			'Image taken from page 142 of');
598
	}
599
600
601
602
	/*
603
	Test a search for a common term, in order to induce pagination
604
	 */
605
	public function testSiteTreeSearch() {
606
		$this->enableHighlights();
607
		$this->autoFollowRedirection = false;
608
609
		//One of the default pages
610
		$searchTerm = 'Contact Us';
611
612
		//Note pages need to be published, by default fixtures only reside in Stage
613
		$searchPageObj = $this->ElasticSearchPage;
614
		$searchPageObj->SiteTreeOnly = true;
615
		$searchPageObj->write();
616
		$searchPageObj->publish('Stage', 'Live');
617
618
619
		$pageLength = 10; // the default
620
		$searchPageObj->ResultsPerPage = $pageLength;
621
		$url = rtrim($searchPageObj->Link(), '/');
622
		$url = $url . '?q=' . $searchTerm;
623
		$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...
624
		$response = $this->get($url);
625
		$this->assertEquals(200, $response->getStatusCode());
626
627
		//There are 2 results for 'Contact Us', as 'About Us' has an Us in the title.
628
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
629
			"Page 1 of 1  (2 results found in");
630
631
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
632
		//the apparently erroneous addition of 1 to the required 2
633
		$this->assertNumberOfNodes('div.searchResult', 3);
634
635
		$this->assertSelectorStartsWithOrEquals('strong.hl', 0, 'Contact'); // CONTACT US
636
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'Us'); // Contact US
637
		$this->assertSelectorStartsWithOrEquals('strong.hl', 2, 'Us'); // About US
638
639
	}
640
641
642
643
	/*
644
	Test a search for a common term, in order to induce pagination
645
	 */
646
	public function testSearchSeveralPagesPage() {
647
		$this->enableHighlights();
648
		$this->autoFollowRedirection = false;
649
		$searchTerm = 'railroad';
650
651
		//Note pages need to be published, by default fixtures only reside in Stage
652
		$searchPageObj = $this->ElasticSearchPage;
653
		$pageLength = 10; // the default
654
		$searchPageObj->ResultsPerPage = $pageLength;
655
		$url = rtrim($searchPageObj->Link(), '/');
656
		$url = $url . '?q=' . $searchTerm;
657
		$firstPageURL = $url;
658
		$response = $this->get($url);
659
		$this->assertEquals(200, $response->getStatusCode());
660
661
		//There are 3 results for mineralogy
662
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
663
			"Page 1 of 2  (11 results found in");
664
665
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
666
		//the apparently erroneous addition of 1 to the required 10
667
		$this->assertNumberOfNodes('div.searchResult', 11);
668
669
		//Check for a couple of highlighed 'Railroad' terms
670
		$this->assertSelectorStartsWithOrEquals('strong.hl', 0, 'Railroad');
671
		$this->assertSelectorStartsWithOrEquals('strong.hl', 1, 'Railroad');
672
673
		$this->assertSelectorStartsWithOrEquals('div.pagination a', 0, '2');
674
		$this->assertSelectorStartsWithOrEquals('div.pagination a.next', 0, '→');
675
676
		$resultsP1 = $this->collateSearchResults();
677
678
		$page2url = $url . '&start=' . $pageLength;
679
680
		//Check pagination on page 2
681
		$response2 = $this->get($page2url);
682
		$this->assertEquals(200, $response2->getStatusCode());
683
684
		//FIXME pluralisation probably needs fixed here, change test later acoordingly
685
		$this->assertSelectorStartsWithOrEquals('div.resultsFound', 0,
686
			"Page 2 of 2  (11 results found in");
687
688
		//The classname 'searchResults' appears to be matching the contained 'searchResult', hence
689
		//the apparently erroneous addition of 1 to the required 1
690
		$this->assertNumberOfNodes('div.searchResult', 2);
691
692
		$this->assertSelectorStartsWithOrEquals('div.pagination a', 1, '1');
693
		$this->assertSelectorStartsWithOrEquals('div.pagination a.prev', 0, '←');
694
695
		$resultsP2 = $this->collateSearchResults();
696
697
		$resultsFrom2Pages = array_merge($resultsP1, $resultsP2);
698
699
		//there are 11 results in total
700
		$this->assertEquals(11, sizeof($resultsFrom2Pages));
701
702
		//increase the number of results and assert that they are the same as per pages 1,2 joined
703
		$searchPageObj->ResultsPerPage = 20;
704
		$searchPageObj->write();
705
		$searchPageObj->publish('Stage','Live');
706
		$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...
707
	}
708
709
710
	private function enableHighlights() {
711
		foreach (SearchableField::get()->filter('Name', 'Title') as $sf) {
712
			$sf->ShowHighlights = true;
713
			$sf->write();
714
		}
715
716
		foreach (SearchableField::get()->filter('Name', 'Content') as $sf) {
717
			$sf->ShowHighlights = true;
718
			$sf->write();
719
		}
720
721
		//FIXME - do this with ORM
722
		//$sql = "UPDATE ElasticSearchPage_ElasticaSearchableFields SET "
723
	}
724
}
725