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