|
1
|
|
|
<?php |
|
2
|
|
|
use SilverStripe\Elastica\ElasticSearcher; |
|
3
|
|
|
use Elastica\Type; |
|
4
|
|
|
use SilverStripe\Elastica\ReindexTask; |
|
5
|
|
|
|
|
6
|
|
|
class TranslatableUnitTest extends ElasticsearchBaseTest { |
|
7
|
|
|
public static $fixture_file = 'elastica/tests/lotsOfPhotos.yml'; |
|
8
|
|
|
|
|
9
|
|
|
public static $ignoreFixtureFileFor = array('testResultsForEmptySearch'); |
|
10
|
|
|
|
|
11
|
|
|
public function setUpOnce() { |
|
12
|
|
|
//Add translatable if it exists |
|
13
|
|
|
if(class_exists('Translatable')) { |
|
14
|
|
|
SiteTree::add_extension('Translatable'); |
|
15
|
|
|
} |
|
16
|
|
|
parent::setUpOnce(); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function testElasticSearchForm() { |
|
20
|
|
|
if(!class_exists('Translatable')) { |
|
21
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
$form = new \ElasticSearchForm(new \Controller(), 'TestForm'); |
|
25
|
|
|
$fields = $form->Fields(); |
|
26
|
|
|
$result = array(); |
|
27
|
|
|
foreach($fields as $field) { |
|
|
|
|
|
|
28
|
|
|
$result[$field->getName()] = $field->Value(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$expected = array('q' => '', 'searchlocale' => 'en_US'); |
|
32
|
|
|
$this->assertEquals($expected, $result); |
|
33
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
public function testHighlightPassingFields() { |
|
38
|
|
|
if(!class_exists('Translatable')) { |
|
39
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$es = new ElasticSearcher(); |
|
43
|
|
|
$es->setClasses('FlickrPhotoTO'); |
|
44
|
|
|
|
|
45
|
|
|
$es->setHighlightedFields(array('Title', 'Title.standard', 'Description')); |
|
46
|
|
|
|
|
47
|
|
|
$fields = array('Title' => 1, 'Description' => 1); |
|
48
|
|
|
$query = 'New Zealand'; |
|
49
|
|
|
$paginated = $es->search($query, $fields); |
|
50
|
|
|
$ctr = 0; |
|
51
|
|
|
|
|
52
|
|
|
foreach($paginated->getList()->toArray() as $result) { |
|
53
|
|
|
$ctr++; |
|
54
|
|
|
|
|
55
|
|
|
foreach($result->SearchHighlightsByField->Description->getIterator() as $highlight) { |
|
56
|
|
|
$snippet = $highlight->Snippet; |
|
57
|
|
|
$snippet = strtolower($snippet); |
|
58
|
|
|
$wordFound = false; |
|
59
|
|
|
$lcquery = explode(' ', strtolower($query)); |
|
60
|
|
|
foreach($lcquery as $part) { |
|
61
|
|
|
$bracketed = '<strong class="hl">' . $part . '</strong>'; |
|
62
|
|
|
if(strpos($snippet, $bracketed) > 0) { |
|
63
|
|
|
$wordFound = true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
$this->assertTrue($wordFound, 'Highlight should have been found'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
public function testAutoCompleteGood() { |
|
73
|
|
|
if(!class_exists('Translatable')) { |
|
74
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$es = new ElasticSearcher(); |
|
78
|
|
|
$es->setClasses('FlickrPhotoTO'); |
|
79
|
|
|
$query = 'Lond'; |
|
80
|
|
|
$results = $es->autocomplete_search($query, 'Title'); |
|
81
|
|
|
$this->assertEquals(7, $results->getTotalItems()); |
|
82
|
|
|
foreach($results->toArray() as $result) { |
|
83
|
|
|
$this->assertTrue(strpos($result->Title, $query) > 0); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
// --------------------- |
|
91
|
|
|
|
|
92
|
|
|
// FIXME - this test is shardy unfortunately |
|
93
|
|
|
public function testMoreLikeThisSinglePhoto() { |
|
94
|
|
|
if(!class_exists('Translatable')) { |
|
95
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
|
99
|
|
|
$es = new ElasticSearcher(); |
|
100
|
|
|
$locale = \i18n::default_locale(); |
|
101
|
|
|
$es->setLocale($locale); |
|
102
|
|
|
$es->setClasses('FlickrPhotoTO'); |
|
103
|
|
|
|
|
104
|
|
|
$fields = array('Description.standard' => 1,'Title.standard' => 1); |
|
105
|
|
|
$results = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$terms = $results->getList()->MoreLikeThisTerms; |
|
108
|
|
|
|
|
109
|
|
|
$fieldNamesReturned = array_keys($terms); |
|
110
|
|
|
$fieldNames = array_keys($fields); |
|
111
|
|
|
sort($fieldNames); |
|
112
|
|
|
sort($fieldNamesReturned); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertEquals($fieldNames, $fieldNamesReturned); |
|
115
|
|
|
|
|
116
|
|
|
//FIXME - this seems anomolyous, check in more detail |
|
117
|
|
|
$expected = array('texas'); |
|
118
|
|
|
$this->assertEquals($expected, $terms['Title.standard']); |
|
119
|
|
|
|
|
120
|
|
|
$expected = array('collection', 'company', 'degolyer', 'everett', 'file', 'high', |
|
121
|
|
|
'information', 'new', 'orleans', 'pacific', 'photographs', 'railroad', 'resolution', |
|
122
|
|
|
'see', 'southern', 'texas', 'view'); |
|
123
|
|
|
|
|
124
|
|
|
$actual = $terms['Description.standard']; |
|
125
|
|
|
sort($expected); |
|
126
|
|
|
sort($actual); |
|
127
|
|
|
$this->assertEquals($expected, $actual); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/* |
|
134
|
|
|
test blank fields |
|
135
|
|
|
test fields with no weighting (ie not associative) |
|
136
|
|
|
|
|
137
|
|
|
*/ |
|
138
|
|
|
|
|
139
|
|
|
public function testSimilarGood() { |
|
140
|
|
|
if(!class_exists('Translatable')) { |
|
141
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
|
145
|
|
|
$es = new ElasticSearcher(); |
|
146
|
|
|
$es->setClasses('FlickrPhotoTO'); |
|
147
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
|
148
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals(32, $paginated->getTotalItems()); |
|
151
|
|
|
$results = $paginated->getList()->toArray(); |
|
152
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Stockdale, Texas]", $results[0]->Title); |
|
153
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Taft, Texas]", $results[1]->Title); |
|
154
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Railroad Station, Sierra Blanca, Texas]", $results[2]->Title); |
|
155
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Freight Station, Waxahachie, Texas]", $results[3]->Title); |
|
156
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Passenger Station, Waxahachie, Texas]", $results[4]->Title); |
|
157
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Tower No. 63, Mexia, Texas]", $results[5]->Title); |
|
158
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Eakin Street Yard Office, Dallas, Texas]", $results[6]->Title); |
|
159
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific Locomotive Scrap Line, Englewood Yards, Houston, Texas]", $results[7]->Title); |
|
160
|
|
|
$this->assertEquals("[Texas and New Orleans, Southern Pacific, Switchman's Tower, San Antonio, Texas]", $results[8]->Title); |
|
161
|
|
|
$this->assertEquals("Flash Light view in new Subterranean", $results[9]->Title); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/* |
|
166
|
|
|
FIXME - this is not working, not sure why. Trying to complete coverage of ReindexTask |
|
167
|
|
|
*/ |
|
168
|
|
|
public function testBulkIndexing() { |
|
169
|
|
|
if(!class_exists('Translatable')) { |
|
170
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
//Reset the index, so that nothing has been indexed |
|
174
|
|
|
$this->service->reset(); |
|
175
|
|
|
|
|
176
|
|
|
//Number of requests indexing wise made to Elasticsearch server |
|
177
|
|
|
$reqs = $this->service->getIndexingRequestCtr(); |
|
178
|
|
|
|
|
179
|
|
|
$task = new ReindexTask($this->service); |
|
180
|
|
|
|
|
181
|
|
|
// null request is fine as no parameters used |
|
182
|
|
|
$task->run(null); |
|
183
|
|
|
|
|
184
|
|
|
//Check that the number of indexing requests has increased by 2 |
|
185
|
|
|
$deltaReqs = $this->service->getIndexingRequestCtr() - $reqs; |
|
186
|
|
|
//One call is made for each of Page and FlickrPhotoTO |
|
187
|
|
|
$this->assertEquals(2, $deltaReqs); |
|
188
|
|
|
|
|
189
|
|
|
// default installed pages plus 100 FlickrPhotoTOs |
|
190
|
|
|
$this->checkNumberOfIndexedDocuments(103); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
// if this is not set to unbounded, zero, a conditional is triggered to add max doc freq to the request |
|
195
|
|
|
public function testSimilarChangeMaxDocFreq() { |
|
196
|
|
|
if(!class_exists('Translatable')) { |
|
197
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
|
201
|
|
|
$es = new ElasticSearcher(); |
|
202
|
|
|
$es->setMaxDocFreq(4); |
|
203
|
|
|
$es->setClasses('FlickrPhotoTO'); |
|
204
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
|
205
|
|
|
$paginated = $es->moreLikeThis($fp, $fields, true); |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
$this->assertEquals(14, $paginated->getTotalItems()); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
public function testSimilarNullFields() { |
|
212
|
|
|
if(!class_exists('Translatable')) { |
|
213
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$fp = $this->objFromFixture('FlickrPhotoTO', 'photo0076'); |
|
217
|
|
|
$es = new ElasticSearcher(); |
|
218
|
|
|
$es->setClasses('FlickrPhotoTO'); |
|
219
|
|
|
try { |
|
220
|
|
|
$es->moreLikeThis($fp, null, true); |
|
|
|
|
|
|
221
|
|
|
$this->fail('More like this search should have failed'); |
|
|
|
|
|
|
222
|
|
|
} catch (InvalidArgumentException $e) { |
|
223
|
|
|
$this->assertEquals('Fields cannot be null', $e->getMessage()); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testSimilarNullItem() { |
|
228
|
|
|
if(!class_exists('Translatable')) { |
|
229
|
|
|
$this->markTestSkipped('Translatable not installed'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
$es = new ElasticSearcher(); |
|
233
|
|
|
$es->setClasses('FlickrPhotoTO'); |
|
234
|
|
|
$fields = array('Title.standard' => 1, 'Description.standard' => 1); |
|
235
|
|
|
|
|
236
|
|
|
try { |
|
237
|
|
|
$es->moreLikeThis(null, $fields, true); |
|
|
|
|
|
|
238
|
|
|
$this->fail('Search should have failed'); |
|
|
|
|
|
|
239
|
|
|
} catch (InvalidArgumentException $e) { |
|
240
|
|
|
$this->assertEquals('A searchable item cannot be null', $e->getMessage()); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.