Issues (248)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/QueryGeneratorTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
use \SilverStripe\Elastica\ElasticSearcher;
3
use \SilverStripe\Elastica\QueryGenerator;
4
5
/**
6
 * Test query generation
7
 * @package elastica
8
 */
9
class QueryGeneratorTest extends ElasticsearchBaseTest {
10
	public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml';
11
12
13
	public static $ignoreFixtureFileFor = array('testToQuoted*');
14
15
	public function testTextOnly() {
16
		$qg = new QueryGenerator();
17
		$qg->setQueryText('New Zealand');
18
		$qg->setFields(null);
19
		$qg->setSelectedFilters(null);
20
21
		//As the query is not empty it should not matter whether or not the show results for empty
22
		//query flag is set or not - test with true and false
23
24
		$qg->setShowResultsForEmptyQuery(false);
25
		$qs = array('query_string' => array('query' => 'New Zealand', 'lenient' => true));
26
		$expected = array(
27
			'query' => $qs,
28
			'size' => 10,
29
			'from' => 0,
30
			'suggest' => $this->getDefaultSuggest('New Zealand')
31
		);
32
33
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
34
35
		$qg->setShowResultsForEmptyQuery(true);
36
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
37
	}
38
39
40
	/**
41
	 * @param string $queryText
42
	 */
43
	private function getDefaultSuggest($queryText) {
44
		return array(
45
				'query-phrase-suggestions' => array(
46
					'phrase' => array(
47
						'field' => '_all',
48
						'size' => 4,
49
						'highlight' => array(
50
							'pre_tag' => '<strong class="hl">',
51
							'post_tag' => '</strong>'
52
						)
53
					),
54
					'text' => $queryText
55
				)
56
		);
57
	}
58
59
60
	public function testEmptyTextShowNone() {
61
		$qg = new QueryGenerator();
62
		$qg->setQueryText('');
63
		$qg->setFields(null);
64
		$qg->setSelectedFilters(null);
65
		$qg->setShowResultsForEmptyQuery(false);
66
67
		$qs = array('query_string' => array('query' => '', 'lenient' => true));
68
		$expected = array(
69
			'query' => $qs,
70
			'size' => 10,
71
			'from' => 0,
72
			'suggest' => $this->getDefaultSuggest('')
73
		);
74
75
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
76
	}
77
78
79
	public function testEmptyTextShowAll() {
80
		$qg = new QueryGenerator();
81
		$qg->setQueryText('');
82
		$qg->setFields(null);
83
		$qg->setSelectedFilters(null);
84
		$qg->setShowResultsForEmptyQuery(true);
85
86
		//In order to show all results an empty query works,
87
		//e.g. curl -XGET 'http://localhost:9200/elastica_ss_module_test_en_us/_search?pretty'
88
		$expected = array(
89
			'size' => 10,
90
			'from' => 0,
91
			'suggest' => $this->getDefaultSuggest('')
92
		);
93
94
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
95
	}
96
97
98 View Code Duplication
	public function testMultiMatchWithText() {
99
		$qg = new QueryGenerator();
100
		$qg->setQueryText('New Zealand');
101
		$fields = array('Title' => 1, 'Description' => 1);
102
		$qg->setFields($fields);
103
		$qg->setSelectedFilters(null);
104
		$qg->setClasses('FlickrPhotoTO');
105
106
		//As the query is not empty it should not matter whether or not the show results for empty
107
		//query flag is set or not - test with true and false
108
109
		$qg->setShowResultsForEmptyQuery(false);
110
		$qs = array('multi_match' => array(
111
			'fields' => array('Title','Title.*','Description','Description.*'),
112
			'type' => 'most_fields',
113
			'query' => 'New Zealand',
114
			'lenient' => true
115
			)
116
		);
117
		$expected = array(
118
			'query' => $qs,
119
			'size' => 10,
120
			'from' => 0,
121
			'suggest' => $this->getDefaultSuggest('New Zealand')
122
		);
123
124
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
125
126
		$qg->setShowResultsForEmptyQuery(true);
127
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
128
	}
129
130
131
132 View Code Duplication
	public function testMultiMatchWithNoText() {
133
		$qg = new QueryGenerator();
134
		$qg->setQueryText('');
135
		$fields = array('Title' => 1, 'Description' => 1);
136
		$qg->setFields($fields);
137
		$qg->setSelectedFilters(null);
138
		$qg->setClasses('FlickrPhotoTO');
139
140
		//As the query is not empty it should not matter whether or not the show results for empty
141
		//query flag is set or not - test with true and false
142
143
		//Case of empty query, do not show results
144
		$qg->setShowResultsForEmptyQuery(false);
145
		$qs = array(
146
			'multi_match' => array(
147
				'fields' => array('Title','Title.*','Description','Description.*'),
148
				'type' => 'most_fields',
149
				'query' => '',
150
				'lenient' => true
151
			)
152
		);
153
		$expected = array(
154
			'query' => $qs,
155
			'size' => 10,
156
			'from' => 0,
157
			'suggest' => $this->getDefaultSuggest('')
158
		);
159
160
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
161
162
163
		// Now the case of empty query and show results
164
		$qg->setShowResultsForEmptyQuery(true);
165
		unset($expected['query']);
166
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
167
	}
168
169
170
	// ---- tests with aggregations ----
171
172
173
	public function testEmptyTextShowNoResultsWithAggregations() {
174
		$this->assertFalse(false, 'This is not possible - an empty query returns 0 docs and 0 aggregations');
175
	}
176
177
178
	/*
179
	Test aggregations with and without text query
180
	 */
181
	public function testTextShowResultsWithAggregations() {
182
		$qg = new QueryGenerator();
183
		$qg->setQueryText('');
184
		$qg->setFields(null);
185
		$qg->setSelectedFilters(null);
186
		$qg->setShowResultsForEmptyQuery(true);
187
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
188
		$aggs = $this->baseAggs();
189
190
		//tests are complete
191
		$expected = array(
192
			'aggs' => $aggs,
193
			'size' => 10,
194
			'from' => 0,
195
			'sort' => array('TakenAt' => 'desc'),
196
			'suggest' => $this->getDefaultSuggest('')
197
		);
198
199
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
200
201
		$qg->setQueryText('New Zealand');
202
		unset($expected['sort']);
203
		$expected['query'] = array('query_string' => array('query' => 'New Zealand', 'lenient' => true));
204
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
205
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
206
207
		$qg->setShowResultsForEmptyQuery(false);
208
		$qg->setQueryText('New Zealand');
209
		$expected['query'] = array('query_string' => array('query' => 'New Zealand', 'lenient' => true));
210
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
211
	}
212
213
214
	/*
215
	Should generate this working query:
216
	curl -XGET 'http://localhost:9200/elastica_ss_module_test_en_us/_search?pretty' -d '
217
	{
218
	  "query": {
219
	    "filtered": {
220
	      "filter": {
221
	        "term": {
222
	          "ISO": 400
223
	        }
224
	      }
225
	    }
226
	  },
227
	  "aggs": {
228
	    "Aperture": {
229
	      "terms": {
230
	        "field": "Aperture",
231
	        "size": 0,
232
	        "order": {
233
	          "_term": "asc"
234
	        }
235
	      }
236
	    },
237
	    "ShutterSpeed": {
238
	      "terms": {
239
	        "field": "ShutterSpeed",
240
	        "size": 0,
241
	        "order": {
242
	          "_term": "asc"
243
	        }
244
	      }
245
	    },
246
	    "FocalLength35mm": {
247
	      "terms": {
248
	        "field": "FocalLength35mm",
249
	        "size": 0,
250
	        "order": {
251
	          "_term": "asc"
252
	        }
253
	      }
254
	    },
255
	    "ISO": {
256
	      "terms": {
257
	        "field": "ISO",
258
	        "size": 0,
259
	        "order": {
260
	          "_term": "asc"
261
	        }
262
	      }
263
	    },
264
	    "Aspect": {
265
	      "range": {
266
	        "field": "AspectRatio",
267
	        "ranges": [{
268
	          "from": 1.0e-7,
269
	          "to": 0.3,
270
	          "key": "Panoramic"
271
	        }, {
272
	          "from": 0.3,
273
	          "to": 0.9,
274
	          "key": "Horizontal"
275
	        }, {
276
	          "from": 0.9,
277
	          "to": 1.2,
278
	          "key": "Square"
279
	        }, {
280
	          "from": 1.2,
281
	          "to": 1.79,
282
	          "key": "Vertical"
283
	        }, {
284
	          "from": 1.79,
285
	          "to": 10000000,
286
	          "key": "Tallest"
287
	        }]
288
	      }
289
	    }
290
	  },
291
	  "size": 10,
292
	  "from": 0
293
	}
294
	'
295
	 */
296
	public function testTextOneFilterAggregate() {
297
		$qg = new QueryGenerator();
298
		$qg->setQueryText('');
299
		$qg->setFields(null);
300
		$filters = array('ISO' => 400);
301
		$qg->setSelectedFilters($filters);
302
		$qg->setShowResultsForEmptyQuery(true);
303
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
304
		$aggs = $this->baseAggs();
305
306
		//FIXME - query needs removed in this case, leave as a reminder for now until
307
		//tests are complete
308
		$expected = array(
309
			'aggs' => $aggs,
310
			'size' => 10,
311
			'from' => 0,
312
			'query' => array(
313
				'filtered' => array(
314
					'filter' => array('term' => array('ISO' => 400))
315
				)
316
			),
317
			'sort' => array('TakenAt' => 'desc'),
318
			'suggest' => $this->getDefaultSuggest('')
319
		);
320
321
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
322
323
		$qg->setQueryText('New Zealand');
324
		unset($expected['sort']); // use query text search relevance for sorting, ie default Elasticsearch
325
		$expected['query']['filtered']['query']['query_string'] = array('query' => 'New Zealand', 'lenient' => true);
326
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
327
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
328
	}
329
330
331
	public function testTextTwoFilterAggregate() {
332
		$qg = new QueryGenerator();
333
		$qg->setQueryText('');
334
		$qg->setFields(null);
335
		$filters = array('ISO' => 400, 'Aspect' => 'Square');
336
		$qg->setSelectedFilters($filters);
337
		$qg->setShowResultsForEmptyQuery(true);
338
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
339
		$aggs = $this->baseAggs();
340
341
		//FIXME - query needs removed in this case, leave as a reminder for now until
342
		//tests are complete
343
		$expected = array(
344
			'aggs' => $aggs,
345
			'size' => 10,
346
			'from' => 0,
347
			'query' => array(
348
				'filtered' => array(
349
					'filter' =>
350
					array('and' => array(
351
						0 => array( 'term' =>  array('ISO' => 400)),
352
						1 => array( 'range' => array(
353
							'AspectRatio' => array(
354
								'gte' => '0.9',
355
								'lt' => '1.2'
356
							)
357
						))
358
					)
359
				))
360
			),
361
			'sort' => array('TakenAt' => 'desc'),
362
			'suggest' => $this->getDefaultSuggest('')
363
		);
364
365
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
366
367
		$qg->setQueryText('New Zealand');
368
		unset($expected['sort']); // use query text search relevance for sorting, ie default Elasticsearch
369
		$expected['query']['filtered']['query']['query_string'] = array('query' => 'New Zealand', 'lenient' => true);
370
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
371
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
372
	}
373
374
375
	public function testTextThreeFilterAggregate() {
376
		$qg = new QueryGenerator();
377
		$qg->setQueryText('');
378
		$qg->setFields(null);
379
		$filters = array('ISO' => 400, 'Aspect' => 'Square', 'Aperture' => 5.6);
380
		$qg->setSelectedFilters($filters);
381
		$qg->setShowResultsForEmptyQuery(true);
382
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
383
		$aggs = $this->baseAggs();
384
385
		//FIXME - query needs removed in this case, leave as a reminder for now until
386
		//tests are complete
387
		$expected = array(
388
			'aggs' => $aggs,
389
			'size' => 10,
390
			'from' => 0,
391
			'query' => array(
392
				'filtered' => array('filter' =>
393
					array('and' => array(
394
						0 => array( 'term' =>  array('ISO' => 400)),
395
						1 => array( 'range' => array(
396
							'AspectRatio' => array(
397
								'gte' => '0.9',
398
								'lt' => '1.2'
399
							)
400
						)),
401
						2 => array( 'term' =>  array('Aperture' => 5.6)),
402
					)
403
				))
404
			),
405
			'sort' => array('TakenAt' => 'desc'),
406
			'suggest' => $this->getDefaultSuggest('')
407
		);
408
409
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
410
411
		$qg->setQueryText('New Zealand');
412
		unset($expected['sort']); // use query text search relevance for sorting, ie default Elasticsearch
413
		$expected['query']['filtered']['query']['query_string'] = array('query' => 'New Zealand', 'lenient' => true);
414
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
415
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
416
	}
417
418
419
	public function testMultiMatchOneFilterAggregate() {
420
		$qg = new QueryGenerator();
421
		$qg->setQueryText('');
422
		$qg->setFields(array('Title' => 2, 'Content' => 1));
423
		$filters = array('ISO' => 400);
424
		$qg->setSelectedFilters($filters);
425
		$qg->setShowResultsForEmptyQuery(true);
426
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
427
		$aggs = $this->baseAggs();
428
429
		//FIXME - query needs removed in this case, leave as a reminder for now until
430
		//tests are complete
431
		$expected = array(
432
			'aggs' => $aggs,
433
			'size' => 10,
434
			'from' => 0,
435
			'query' => array(
436
				'filtered' => array(
437
					'filter' => array('term' => array('ISO' => 400))
438
				)
439
			),
440
			'sort' => array('TakenAt' => 'desc'),
441
			'suggest' => $this->getDefaultSuggest('')
442
		);
443
444
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
445
446
		$qg->setQueryText('New Zealand');
447
		unset($expected['sort']); // use relevance of search sorting
448
449
		$expected['query']['filtered']['query']['multi_match'] = array(
450
			'query' => 'New Zealand',
451
			'lenient' => true,
452
			'fields' => array('Title^2', 'Title.*^2','Content', 'Content.*'),
453
			'type' => 'most_fields'
454
		);
455
456
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
457
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
458
	}
459
460
461
462
	public function testMultiMatchTwoFilterAggregate() {
463
		$qg = new QueryGenerator();
464
		$qg->setQueryText('');
465
		$qg->setFields(array('Title' => 2, 'Content' => 1));
466
		$filters = array('ISO' => 400, 'Aspect' => 'Square');
467
		$qg->setSelectedFilters($filters);
468
		$qg->setShowResultsForEmptyQuery(true);
469
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
470
		$aggs = $this->baseAggs();
471
472
		//FIXME - query needs removed in this case, leave as a reminder for now until
473
		//tests are complete
474
		$expected = array(
475
			'aggs' => $aggs,
476
			'size' => 10,
477
			'from' => 0,
478
			'query' => array(
479
				'filtered' => array(
480
					'filter' =>
481
					array('and' => array(
482
						0 => array( 'term' =>  array('ISO' => 400)),
483
						1 => array( 'range' => array(
484
							'AspectRatio' => array(
485
								'gte' => '0.9',
486
								'lt' => '1.2'
487
							)
488
						))
489
					)
490
				))
491
			),
492
			'sort' => array('TakenAt' => 'desc'),
493
			'suggest' => $this->getDefaultSuggest('')
494
		);
495
496
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
497
498
		$qg->setQueryText('New Zealand');
499
		unset($expected['sort']); // use relevance of search sorting
500
		$expected['query']['filtered']['query']['multi_match'] = array(
501
			'query' => 'New Zealand',
502
			'lenient' => true,
503
			'fields' => array('Title^2', 'Title.*^2','Content', 'Content.*'),
504
			'type' => 'most_fields'
505
		);
506
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
507
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
508
	}
509
510
511
512
	public function testMultiMatchThreeFilterAggregate() {
513
		$qg = new QueryGenerator();
514
		$qg->setQueryText('');
515
		$qg->setFields(array('Title' => 2, 'Content' => 1));
516
		$filters = array('ISO' => 400, 'Aspect' => 'Square', 'Aperture' => 5.6);
517
		$qg->setSelectedFilters($filters);
518
		$qg->setShowResultsForEmptyQuery(true);
519
		$qg->setQueryResultManipulator('FlickrPhotoTOElasticaSearchHelper');
520
		$aggs = $this->baseAggs();
521
522
		//FIXME - query needs removed in this case, leave as a reminder for now until
523
		//tests are complete
524
		$expected = array(
525
			'aggs' => $aggs,
526
			'size' => 10,
527
			'from' => 0,
528
			'query' => array(
529
				'filtered' => array('filter' =>
530
					array('and' => array(
531
						0 => array( 'term' =>  array('ISO' => 400)),
532
						1 => array( 'range' => array(
533
							'AspectRatio' => array(
534
								'gte' => '0.9',
535
								'lt' => '1.2'
536
							)
537
						)),
538
						2 => array( 'term' =>  array('Aperture' => 5.6)),
539
					)
540
				))
541
			),
542
			'sort' => array('TakenAt' => 'desc'),
543
			'suggest' => $this->getDefaultSuggest('')
544
		);
545
546
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
547
548
		$qg->setQueryText('New Zealand');
549
		unset($expected['sort']);
550
		$expected['query']['filtered']['query']['multi_match'] = array(
551
			'query' => 'New Zealand',
552
			'lenient' => true,
553
			'fields' => array('Title^2', 'Title.*^2','Content', 'Content.*'),
554
			'type' => 'most_fields'
555
		);
556
		$expected['suggest'] = $this->getDefaultSuggest('New Zealand');
557
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
558
	}
559
560
561
562
563
	// ---- tests for field array to elasticsearch syntax
564 View Code Duplication
	public function testConvertWeightedFieldsForElasticaUnaryStrings() {
565
		$qg = new QueryGenerator();
566
		$qg->setClasses('FlickrPhotoTO');
567
		$fields = array('Title' => 1, 'Description' => 1);
568
		$expected = array('Title', 'Title.*','Description', 'Description.*');
569
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
570
	}
571
572
573 View Code Duplication
	public function testConvertWeightedFieldsForElasticaMultipleStrings() {
574
		$qg = new QueryGenerator();
575
		$qg->setClasses('FlickrPhotoTO');
576
		$fields = array('Title' => 2, 'Description' => 1);
577
		$expected = array('Title^2', 'Title.*^2','Description', 'Description.*');
578
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
579
	}
580
581
582
	public function testConvertWeightedFieldsForElasticaTestNonString() {
583
		$qg = new QueryGenerator();
584
		$qg->setClasses('FlickrPhotoTO');
585
		$fields = array('Aperture' => 2, 'FocalLength35mm' => 1);
586
		$expected = array('Aperture^2', 'FocalLength35mm');
587
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
588
	}
589
590
591
	public function testConvertWeightedFieldsForElasticaNonExistent() {
592
		$qg = new QueryGenerator();
593
		$qg->setClasses('FlickrPhotoTO');
594
		$fields = array('Aperture' => 2, 'FocalLength35mm' => 1, 'Wibble' => 2);
595
		try {
596
			$this->assertEquals('This test should fail', $qg->convertWeightedFieldsForElastica($fields));
597
			$this->fail('An exception should have been thrown as the field Wibble does not exist');
598
		} catch (Exception $e) {
599
			$this->assertEquals('Field Wibble does not exist', $e->getMessage());
600
		}
601
602
	}
603
604
605 View Code Duplication
	public function testSearchFieldsMappingForClasses() {
606
		$qg = new QueryGenerator();
607
		$qg->setClasses('FlickrPhotoTO,Page');
608
		$fields = array('Title' => 2, 'Description' => 1);
609
		$expected = array('Title^2', 'Title.*^2','Description', 'Description.*');
610
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
611
612
		$qg->setClasses(array('FlickrPhotoTO','Page'));
0 ignored issues
show
array('FlickrPhotoTO', 'Page') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
613
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
614
615
	}
616
617
618
	public function testSearchFieldsMappingForClassesCaching() {
619
		QueryGenerator::resetCacheHitCounter();
620
		$cache = SS_Cache::factory('elasticsearch');
621
		// Previous tests may have altered this so start from a known position
622
		$cache->remove('SEARCHABLE_FIELDS_FlickrPhotoTO_Page');
623
		$qg = new QueryGenerator();
624
		$qg->setClasses('FlickrPhotoTO,Page');
625
		$fields = array('Title' => 2, 'Description' => 1);
626
		$expected = array('Title^2', 'Title.*^2','Description', 'Description.*');
627
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
628
629
		//Execute a 2nd time
630
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
631
		//Check for cache hit
632
		$this->assertEquals(1, QueryGenerator::getCacheHitCounter());
633
634
		//Execute a 3rd time
635
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
636
		//Check for cache hit
637
		$this->assertEquals(2, QueryGenerator::getCacheHitCounter());
638
	}
639
640
641 View Code Duplication
	public function testSearchFieldsMappingForSiteTree() {
642
		$qg = new QueryGenerator();
643
		$qg->setClasses(null); // select all of site tree classes
644
		$fields = array('Title' => 2, 'Content' => 1);
645
		$expected = array('Title^2', 'Title.*^2','Content', 'Content.*');
646
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
647
648
		$qg->setClasses(array('FlickrPhotoTO','Page'));
0 ignored issues
show
array('FlickrPhotoTO', 'Page') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
649
		$this->assertEquals($expected, $qg->convertWeightedFieldsForElastica($fields));
650
651
	}
652
653
654
	public function testPagination() {
655
		$qg = new QueryGenerator();
656
		$qg->setQueryText('New Zealand');
657
		$qg->setFields(null);
658
		$qg->setSelectedFilters(null);
659
		$qg->setPageLength(12);
660
		$qg->setStart(24);
661
662
		//As the query is not empty it should not matter whether or not the show results for empty
663
		//query flag is set or not - test with true and false
664
665
		$qg->setShowResultsForEmptyQuery(false);
666
		$this->assertEquals(false, $qg->getShowResultsForEmptyQuery());
667
		$qs = array('query_string' => array('query' => 'New Zealand', 'lenient' => true));
668
		$expected = array(
669
			'query' => $qs,
670
			'size' => 12,
671
			'from' => 24,
672
			'suggest' => $this->getDefaultSuggest('New Zealand')
673
		);
674
675
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
676
677
		$qg->setShowResultsForEmptyQuery(true);
678
		$this->assertEquals(true, $qg->getShowResultsForEmptyQuery());
679
		$this->assertEquals($expected, $qg->generateElasticaQuery()->toArray());
680
	}
681
682
683
	/**
684
	 * Get the basic aggregates that should be returned for the augmenter being tested
685
	 * @return array array of aggregations.  Tweak these in tests and assert as required.
686
	 */
687
	private function baseAggs() {
688
		$result = array();
689
		$result['Aperture'] = array(
690
			'terms' => array(
691
				'field' => 'Aperture',
692
				'size' => 0,
693
				'order' => array('_term' => 'asc')
694
			)
695
		);
696
		$result['ShutterSpeed'] =  array(
697
			'terms' => array(
698
				'field' => 'ShutterSpeed',
699
				'size' => 0,
700
				'order' => array('_term' => 'asc')
701
			)
702
		);
703
		$result['FocalLength35mm'] =  array(
704
			'terms' => array(
705
				'field' => 'FocalLength35mm',
706
				'size' => 0,
707
				'order' => array('_term' => 'asc')
708
			)
709
		);
710
		$result['ISO'] =  array(
711
			'terms' => array(
712
				'field' => 'ISO',
713
				'size' => 0,
714
				'order' => array('_term' => 'asc')
715
			)
716
		);
717
718
		$ranges = array();
719
		$ranges[0] = array('from' => '1.0E-7', 'to' => '0.3', 'key' => 'Panoramic');
720
		$ranges[1] = array('from' => '0.3', 'to' => '0.9', 'key' => 'Horizontal');
721
		$ranges[2] = array('from' => '0.9', 'to' => '1.2', 'key' => 'Square');
722
		$ranges[3] = array('from' => '1.2', 'to' => '1.79', 'key' => 'Vertical');
723
		$ranges[4] = array('from' => '1.79', 'to' => '10000000', 'key' => 'Tallest');
724
725
		$result['Aspect'] =  array(
726
			'range' => array(
727
				'field' => 'AspectRatio',
728
				'ranges' => $ranges
729
			)
730
		);
731
		return $result;
732
	}
733
734
735
		// ---- tests for the toQuotedCSV function ----
736
	public function testToQuotedCSVFromString() {
737
		$expected = "'Bangkok','Nonthaburi','Saraburi','Chiang Mai'";
738
		$items = 'Bangkok,Nonthaburi,Saraburi,Chiang Mai';
739
		$quoted = QueryGenerator::convertToQuotedCSV($items);
740
		$this->assertEquals($expected, $quoted);
741
	}
742
743
	public function testToQuotedCSVFromArray() {
744
		$expected = "'Bangkok','Nonthaburi','Saraburi','Chiang Mai'";
745
		$items = array('Bangkok','Nonthaburi','Saraburi','Chiang Mai');
746
		$quoted = QueryGenerator::convertToQuotedCSV($items);
747
		$this->assertEquals($expected, $quoted);
748
	}
749
750
	public function testToQuotedCSVEmptyString() {
751
		$quoted = QueryGenerator::convertToQuotedCSV('');
752
		$this->assertEquals('', $quoted);
753
	}
754
755
	public function testToQuotedCSVEmptyArray() {
756
		$quoted = QueryGenerator::convertToQuotedCSV(array());
757
		$this->assertEquals('', $quoted);
758
	}
759
760
	public function testToQuotedCSVNull() {
761
		$quoted = QueryGenerator::convertToQuotedCSV(null);
762
		$this->assertEquals('', $quoted);
763
	}
764
}
765