1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SilverStripe\Elastica\ReindexTask; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @package comments |
7
|
|
|
*/ |
8
|
|
|
class AutocompleteControllerTest extends ElasticsearchFunctionalTestBase { |
9
|
|
|
|
10
|
|
|
public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml'; |
11
|
|
|
|
12
|
|
|
|
13
|
|
View Code Duplication |
public function setup() { |
|
|
|
|
14
|
|
|
parent::setup(); |
15
|
|
|
$config = Config::inst()->get('FlickrPhotoTO', 'searchable_fields'); |
|
|
|
|
16
|
|
|
|
17
|
|
|
\Config::inst()->update('FlickrPhotoTO', 'searchable_autocomplete', array('Title')); |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
// Delete and assert that it does not exist |
21
|
|
|
$sql = "SELECT ID,Name,ClazzName from SearchableField"; |
22
|
|
|
$records = DB::query($sql); |
|
|
|
|
23
|
|
|
|
24
|
|
|
$filter = array('Name' => 'Title', 'ClazzName' => 'FlickrPhotoTO'); |
25
|
|
|
$sf = SearchableField::get()->filter($filter)->first(); |
26
|
|
|
$sql = "UPDATE ElasticSearchPage_ElasticaSearchableFields SET Searchable=1,". |
27
|
|
|
"EnableAutocomplete=1 where SearchableFieldID=".$sf->ID; |
28
|
|
|
|
29
|
|
|
DB::query($sql); |
30
|
|
|
|
31
|
|
|
$task = new ReindexTask($this->service); |
32
|
|
|
// null request is fine as no parameters used |
33
|
|
|
$task->run(null); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function testDataObject() { |
39
|
|
|
$url = 'autocomplete/search?field=Title&classes=FlickrPhotoTO&query=the'; |
40
|
|
|
$response = $this->get($url); |
41
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
42
|
|
|
$body = $response->getBody(); |
43
|
|
|
|
44
|
|
|
$result = json_decode($body); |
45
|
|
|
|
46
|
|
|
$this->assertEquals('the', $result->Query); |
47
|
|
|
$lquery = strtolower($result->Query); |
48
|
|
|
foreach ($result->suggestions as $suggestion) { |
49
|
|
|
$value = $suggestion->value; |
50
|
|
|
$value = strtolower($value); |
51
|
|
|
$this->assertContains($lquery, $value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// make sure there were actually some results |
55
|
|
|
$this->assertEquals(10, sizeof($result->suggestions)); |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
//search for different capitlisation, should produce the same result |
59
|
|
|
$url = 'autocomplete/search?field=Title&classes=FlickrPhotoTO&query=ThE'; |
60
|
|
|
$response = $this->get($url); |
61
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
62
|
|
|
$body = $response->getBody(); |
63
|
|
|
$result2 = json_decode($body); |
64
|
|
|
$this->assertEquals($result->suggestions, $result2->suggestions); |
65
|
|
|
|
66
|
|
|
//append a space should produce the same result |
67
|
|
|
$url = 'autocomplete/search?field=Title&classes=FlickrPhotoTO&query=ThE%20'; |
68
|
|
|
$response = $this->get($url); |
69
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
70
|
|
|
$body = $response->getBody(); |
71
|
|
|
$result3 = json_decode($body); |
72
|
|
|
$this->assertEquals($result2->suggestions, $result3->suggestions); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
// test a non existent class, for now return blanks so as to avoid extra overhead as this |
76
|
|
|
// method is called often |
77
|
|
|
$url = 'autocomplete/search?field=FieldThatDoesNotExist&classes=FlickrPhotoTO&query=the'; |
78
|
|
|
$response = $this->get($url); |
79
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
80
|
|
|
$body = $response->getBody(); |
81
|
|
|
$result4 = json_decode($body); |
82
|
|
|
$this->assertEquals(0, sizeof($result4->suggestions)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.