1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Firesphere\SolrSearch\Tests; |
5
|
|
|
|
6
|
|
|
use CircleCITestIndex; |
7
|
|
|
use Firesphere\SolrSearch\Helpers\Synonyms; |
8
|
|
|
use Firesphere\SolrSearch\Indexes\BaseIndex; |
9
|
|
|
use Firesphere\SolrSearch\Queries\BaseQuery; |
10
|
|
|
use Firesphere\SolrSearch\Results\SearchResult; |
11
|
|
|
use Firesphere\SolrSearch\Stores\FileConfigStore; |
12
|
|
|
use Page; |
13
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
14
|
|
|
use SilverStripe\Control\Director; |
15
|
|
|
use SilverStripe\Control\NullHTTPRequest; |
16
|
|
|
use SilverStripe\Core\Injector\Injector; |
17
|
|
|
use SilverStripe\Dev\SapphireTest; |
18
|
|
|
use SilverStripe\ORM\ArrayList; |
19
|
|
|
use SilverStripe\ORM\PaginatedList; |
20
|
|
|
use SilverStripe\Security\DefaultAdminService; |
21
|
|
|
use SilverStripe\View\ArrayData; |
22
|
|
|
use Solarium\Component\Result\Highlighting\Highlighting; |
23
|
|
|
use Solarium\Core\Client\Client; |
24
|
|
|
|
25
|
|
|
class BaseIndexTest extends SapphireTest |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var BaseIndex |
29
|
|
|
*/ |
30
|
|
|
protected $index; |
31
|
|
|
|
32
|
|
|
public function testConstruct() |
33
|
|
|
{ |
34
|
|
|
$this->assertInstanceOf(Client::class, $this->index->getClient()); |
35
|
|
|
$this->assertCount(1, $this->index->getClasses()); |
36
|
|
|
$this->assertCount(1, $this->index->getFulltextFields()); |
37
|
|
|
$this->assertContains(SiteTree::class, $this->index->getClasses()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testInit() |
41
|
|
|
{ |
42
|
|
|
$this->assertNull($this->index->init()); |
43
|
|
|
$this->assertNotEmpty($this->index->getFulltextFields()); |
44
|
|
|
$this->assertNotEmpty($this->index->getFieldsForIndexing()); |
45
|
|
|
$expected = [ |
46
|
|
|
'Title', |
47
|
|
|
'Content', |
48
|
|
|
'Created', |
49
|
|
|
'SubsiteID' |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$this->assertEquals($expected, array_values($this->index->getFieldsForIndexing())); |
53
|
|
|
|
54
|
|
|
$index = new TestIndexTwo(); |
55
|
|
|
|
56
|
|
|
$this->assertCount(3, $index->getFulltextFields()); |
57
|
|
|
$this->assertCount(1, $index->getFacetFields()); |
58
|
|
|
$facets = $index->getFacetFields(); |
59
|
|
|
$this->assertEquals(['Field' => 'SiteTree_TestObjectID', 'Title' => 'TestObject'], $facets[TestObject::class]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testGetSynonyms() |
63
|
|
|
{ |
64
|
|
|
$this->assertEquals(Synonyms::getSynonymsAsString(), $this->index->getSynonyms()); |
65
|
|
|
|
66
|
|
|
$this->assertEmpty($this->index->getSynonyms(false)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testIndexName() |
70
|
|
|
{ |
71
|
|
|
$this->assertEquals('TestIndex', $this->index->getIndexName()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testUploadConfig() |
75
|
|
|
{ |
76
|
|
|
$config = [ |
77
|
|
|
'mode' => 'file', |
78
|
|
|
'path' => '.solr' |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** @var FileConfigStore $configStore */ |
82
|
|
|
$configStore = Injector::inst()->create(FileConfigStore::class, $config); |
83
|
|
|
|
84
|
|
|
$this->index->uploadConfig($configStore); |
85
|
|
|
|
86
|
|
|
$this->assertFileExists(Director::baseFolder() . '/.solr/TestIndex/conf/schema.xml'); |
87
|
|
|
$this->assertFileExists(Director::baseFolder() . '/.solr/TestIndex/conf/synonyms.txt'); |
88
|
|
|
$this->assertFileExists(Director::baseFolder() . '/.solr/TestIndex/conf/stopwords.txt'); |
89
|
|
|
|
90
|
|
|
$xml = file_get_contents(Director::baseFolder() . '/.solr/TestIndex/conf/schema.xml'); |
91
|
|
|
$this->assertContains( |
92
|
|
|
'<field name=\'SiteTree_Title\' type=\'string\' indexed=\'true\' stored=\'true\' multiValued=\'false\'/>', |
93
|
|
|
$xml |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$original = $configStore->getConfig(); |
97
|
|
|
$configStore->setConfig([]); |
98
|
|
|
$this->assertEquals([], $configStore->getConfig()); |
99
|
|
|
// Unhappy path, the config is not updated |
100
|
|
|
$this->assertNotEquals($original, $configStore->getConfig()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testGetFieldsForIndexing() |
104
|
|
|
{ |
105
|
|
|
$expected = [ |
106
|
|
|
'Title', |
107
|
|
|
'Content', |
108
|
|
|
'Created', |
109
|
|
|
'SubsiteID' |
110
|
|
|
]; |
111
|
|
|
$this->assertEquals($expected, array_values($this->index->getFieldsForIndexing())); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testGetSetClient() |
115
|
|
|
{ |
116
|
|
|
$client = $this->index->getClient(); |
117
|
|
|
// set client to something stupid |
118
|
|
|
$this->index->setClient('test'); |
119
|
|
|
$this->assertEquals('test', $this->index->getClient()); |
120
|
|
|
$this->index->setClient($client); |
121
|
|
|
$this->assertInstanceOf(Client::class, $this->index->getClient()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testDoSearch() |
125
|
|
|
{ |
126
|
|
|
$index = new CircleCITestIndex(); |
127
|
|
|
|
128
|
|
|
$query = new BaseQuery(); |
129
|
|
|
$query->addTerm('Home'); |
130
|
|
|
|
131
|
|
|
$result = $index->doSearch($query); |
132
|
|
|
$this->assertInstanceOf(SearchResult::class, $result); |
133
|
|
|
$this->assertEquals(1, $result->getTotalItems()); |
134
|
|
|
|
135
|
|
|
$admin = singleton(DefaultAdminService::class)->findOrCreateDefaultAdmin(); |
136
|
|
|
$this->loginAs($admin); |
137
|
|
|
// Result should be the same for now |
138
|
|
|
$result2 = $index->doSearch($query); |
139
|
|
|
$this->assertEquals($result, $result2); |
140
|
|
|
|
141
|
|
|
$query->addClass(SiteTree::class); |
142
|
|
|
|
143
|
|
|
$result3 = $index->doSearch($query); |
144
|
|
|
$request = new NullHTTPRequest(); |
145
|
|
|
$this->assertInstanceOf(PaginatedList::class, $result3->getPaginatedMatches($request)); |
146
|
|
|
$this->assertEquals($result3->getTotalItems(), $result3->getPaginatedMatches($request)->getTotalItems()); |
147
|
|
|
$this->assertInstanceOf(ArrayData::class, $result3->getFacets()); |
148
|
|
|
$this->assertInstanceOf(ArrayList::class, $result3->getSpellcheck()); |
149
|
|
|
$this->assertInstanceOf(Highlighting::class, $result3->getHighlight()); |
150
|
|
|
|
151
|
|
|
$result3->setCustomisedMatches([]); |
152
|
|
|
$this->assertInstanceOf(ArrayList::class, $result3->getMatches()); |
153
|
|
|
$this->assertCount(0, $result3->getMatches()); |
154
|
|
|
|
155
|
|
|
$index = new CircleCITestIndex(); |
156
|
|
|
$query = new BaseQuery(); |
157
|
|
|
$query->addTerm('Home', ['SiteTree_Title'], 5); |
158
|
|
|
$result4 = $index->doSearch($query); |
159
|
|
|
|
160
|
|
|
$this->assertContains('SiteTree_Title:Home^5.0', $index->getQueryFactory()->getBoostTerms()); |
161
|
|
|
$this->assertContains('Home', $index->getQueryTerms()); |
162
|
|
|
$this->assertEquals(1, $result4->getTotalItems()); |
163
|
|
|
|
164
|
|
|
$index = new CircleCITestIndex(); |
165
|
|
|
$query = new BaseQuery(); |
166
|
|
|
$query->addTerm('Home', ['SiteTree.Title'], 3); |
167
|
|
|
$result4 = $index->doSearch($query); |
168
|
|
|
|
169
|
|
|
$this->assertContains('SiteTree_Title:Home^3.0', $index->getQueryFactory()->getBoostTerms()); |
170
|
|
|
$this->assertContains('Home', $index->getQueryTerms()); |
171
|
|
|
$this->assertEquals(1, $result4->getTotalItems()); |
172
|
|
|
|
173
|
|
|
$index = new CircleCITestIndex(); |
174
|
|
|
$query = new BaseQuery(); |
175
|
|
|
$query->addTerm('Home', [], 0, true); |
176
|
|
|
$index->doSearch($query); |
177
|
|
|
|
178
|
|
|
$this->assertContains('Home~', $index->getQueryTerms()); |
179
|
|
|
|
180
|
|
|
$index = new CircleCITestIndex(); |
181
|
|
|
$query = new BaseQuery(); |
182
|
|
|
$query->addTerm('Home', [], 0, 2); |
183
|
|
|
$index->doSearch($query); |
184
|
|
|
|
185
|
|
|
$this->assertContains('Home~2', $index->getQueryTerms()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function testGetFieldsForSubsites() |
189
|
|
|
{ |
190
|
|
|
$this->assertContains('SubsiteID', $this->index->getFilterFields()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function testSetFacets() |
194
|
|
|
{ |
195
|
|
|
$this->index->addFacetField(Page::class, ['Title' => 'Title', 'Field' => 'Content']); |
196
|
|
|
|
197
|
|
|
$expected = [ |
198
|
|
|
'Page' => [ |
199
|
|
|
'Title' => 'Title', |
200
|
|
|
'Field' => 'Content' |
201
|
|
|
] |
202
|
|
|
]; |
203
|
|
|
$this->assertEquals($expected, $this->index->getFacetFields()); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testAddCopyField() |
207
|
|
|
{ |
208
|
|
|
$this->index->addCopyField('myfield', ['Content']); |
209
|
|
|
$expected = [ |
210
|
|
|
'_text' => ['*'], |
211
|
|
|
'myfield' => ['Content'] |
212
|
|
|
]; |
213
|
|
|
$this->assertEquals($expected, $this->index->getCopyFields()); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
protected function setUp() |
217
|
|
|
{ |
218
|
|
|
$this->index = Injector::inst()->get(TestIndex::class); |
219
|
|
|
|
220
|
|
|
return parent::setUp(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|