|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\FullTextSearch\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Apache_Solr_Document; |
|
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
use SilverStripe\FullTextSearch\Search\FullTextSearch; |
|
11
|
|
|
use SilverStripe\FullTextSearch\Search\SearchIntrospection; |
|
12
|
|
|
use SilverStripe\FullTextSearch\Search\Indexes\SearchIndex_Recording; |
|
13
|
|
|
use SilverStripe\FullTextSearch\Solr\Services\Solr3Service; |
|
14
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchVariantVersionedTest\SearchVariantVersionedTest_Item; |
|
15
|
|
|
use SilverStripe\FullTextSearch\Tests\SolrIndexVersionedTest\SolrIndexVersionedTest_Object; |
|
16
|
|
|
use SilverStripe\FullTextSearch\Tests\SolrIndexVersionedTest\SolrVersionedTest_Index; |
|
17
|
|
|
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateProcessor; |
|
18
|
|
|
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateImmediateProcessor; |
|
19
|
|
|
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater; |
|
20
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariantSubsites; |
|
21
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariantVersioned; |
|
22
|
|
|
use SilverStripe\Subsites\Model\Subsite; |
|
23
|
|
|
use SilverStripe\Versioned\Versioned; |
|
24
|
|
|
|
|
25
|
|
|
class SolrIndexVersionedTest extends SapphireTest |
|
26
|
|
|
{ |
|
27
|
|
|
protected $usesDatabase = true; |
|
28
|
|
|
|
|
29
|
|
|
protected $oldMode = null; |
|
30
|
|
|
|
|
31
|
|
|
protected static $index = null; |
|
32
|
|
|
|
|
33
|
|
|
protected static $extra_dataobjects = [ |
|
34
|
|
|
SearchVariantVersionedTest_Item::class, |
|
35
|
|
|
SolrIndexVersionedTest_Object::class, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
protected function setUp() |
|
39
|
|
|
{ |
|
40
|
|
|
// Need to be set before parent::setUp() since they're executed before the tests start |
|
41
|
|
|
Config::modify()->set(SearchVariantSubsites::class, 'enabled', false); |
|
42
|
|
|
|
|
43
|
|
|
parent::setUp(); |
|
44
|
|
|
|
|
45
|
|
|
if (self::$index === null) { |
|
46
|
|
|
self::$index = singleton(SolrVersionedTest_Index::class); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
SearchUpdater::bind_manipulation_capture(); |
|
50
|
|
|
|
|
51
|
|
|
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, [ |
|
52
|
|
|
'class' => SearchUpdateImmediateProcessor::class |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
FullTextSearch::force_index_list(self::$index); |
|
56
|
|
|
SearchUpdater::clear_dirty_indexes(); |
|
57
|
|
|
|
|
58
|
|
|
$this->oldMode = Versioned::get_reading_mode(); |
|
59
|
|
|
Versioned::set_stage(Versioned::DRAFT); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function tearDown() |
|
63
|
|
|
{ |
|
64
|
|
|
Versioned::set_reading_mode($this->oldMode); |
|
65
|
|
|
parent::tearDown(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getServiceMock($setMethods = array()) |
|
69
|
|
|
{ |
|
70
|
|
|
// Setup mock |
|
71
|
|
|
/** @var SilverStripe\FullTextSearch\Solr\Services\Solr3Service|ObjectProphecy $serviceMock */ |
|
72
|
|
|
$serviceMock = $this->getMockBuilder(Solr3Service::class) |
|
73
|
|
|
->setMethods($setMethods) |
|
74
|
|
|
->getMock(); |
|
75
|
|
|
|
|
76
|
|
|
self::$index->setService($serviceMock); |
|
77
|
|
|
|
|
78
|
|
|
return $serviceMock; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param DataObject $object Item being added |
|
83
|
|
|
* @param string $stage |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getExpectedDocumentId($object, $stage) |
|
87
|
|
|
{ |
|
88
|
|
|
$id = $object->ID; |
|
89
|
|
|
$class = DataObject::getSchema()->baseDataClass($object); |
|
90
|
|
|
return $id.'-'.$class.'-{'.json_encode(SearchVariantVersioned::class).':"'.$stage.'"}'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $class |
|
95
|
|
|
* @param DataObject $object Item being added |
|
96
|
|
|
* @param string $value Value for class |
|
97
|
|
|
* @param string $stage Stage updated |
|
98
|
|
|
* @return Apache_Solr_Document |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getSolrDocument($class, $object, $value, $stage) |
|
101
|
|
|
{ |
|
102
|
|
|
$doc = new Apache_Solr_Document(); |
|
103
|
|
|
$doc->setField('_documentid', $this->getExpectedDocumentId($object, $stage)); |
|
104
|
|
|
$doc->setField('ClassName', $class); |
|
105
|
|
|
$doc->setField(DataObject::getSchema()->baseDataClass($class) . '_TestText', $value); |
|
106
|
|
|
$doc->setField('_versionedstage', $stage); |
|
107
|
|
|
$doc->setField('ID', (int) $object->ID); |
|
108
|
|
|
$doc->setField('ClassHierarchy', SearchIntrospection::hierarchy($class)); |
|
109
|
|
|
$doc->setFieldBoost('ID', false); |
|
110
|
|
|
$doc->setFieldBoost('ClassHierarchy', false); |
|
111
|
|
|
|
|
112
|
|
|
return $doc; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testPublishing() |
|
116
|
|
|
{ |
|
117
|
|
|
// Check that write updates Stage |
|
118
|
|
|
Versioned::set_stage(Versioned::DRAFT); |
|
119
|
|
|
|
|
120
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo')); |
|
121
|
|
|
$item->write(); |
|
122
|
|
|
$object = new SolrIndexVersionedTest_Object(array('TestText' => 'Bar')); |
|
123
|
|
|
$object->write(); |
|
124
|
|
|
|
|
125
|
|
|
$doc1 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::DRAFT); |
|
126
|
|
|
$doc2 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::DRAFT); |
|
127
|
|
|
|
|
128
|
|
|
// Ensure correct call is made to Solr |
|
129
|
|
|
$this->getServiceMock(['addDocument', 'commit']) |
|
130
|
|
|
->expects($this->exactly(2)) |
|
131
|
|
|
->method('addDocument') |
|
132
|
|
|
->withConsecutive( |
|
133
|
|
|
[$this->equalTo($doc1)], |
|
134
|
|
|
[$this->equalTo($doc2)] |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
SearchUpdater::flush_dirty_indexes(); |
|
138
|
|
|
|
|
139
|
|
|
// Check that write updates Live |
|
140
|
|
|
Versioned::set_stage(Versioned::DRAFT); |
|
141
|
|
|
|
|
142
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo')); |
|
143
|
|
|
$item->write(); |
|
144
|
|
|
$item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
$object = new SolrIndexVersionedTest_Object(array('TestText' => 'Bar')); |
|
147
|
|
|
$object->write(); |
|
148
|
|
|
$object->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
$doc1 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::DRAFT); |
|
151
|
|
|
$doc2 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::LIVE); |
|
152
|
|
|
$doc3 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::DRAFT); |
|
153
|
|
|
$doc4 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::LIVE); |
|
154
|
|
|
|
|
155
|
|
|
// Ensure correct call is made to Solr |
|
156
|
|
|
$this->getServiceMock(['addDocument', 'commit']) |
|
157
|
|
|
->expects($this->exactly(4)) |
|
158
|
|
|
->method('addDocument') |
|
159
|
|
|
->withConsecutive( |
|
160
|
|
|
[$doc1], |
|
161
|
|
|
[$doc2], |
|
162
|
|
|
[$doc3], |
|
163
|
|
|
[$doc4] |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
|
|
SearchUpdater::flush_dirty_indexes(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function testDelete() |
|
170
|
|
|
{ |
|
171
|
|
|
// Delete the live record (not the stage) |
|
172
|
|
|
Versioned::set_stage(Versioned::DRAFT); |
|
173
|
|
|
|
|
174
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Too')); |
|
175
|
|
|
$item->write(); |
|
176
|
|
|
$item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
|
|
|
|
|
|
177
|
|
|
Versioned::set_stage(Versioned::LIVE); |
|
178
|
|
|
$id = clone $item; |
|
179
|
|
|
$item->delete(); |
|
180
|
|
|
|
|
181
|
|
|
// Check that only the 'Live' version is deleted |
|
182
|
|
|
$this->getServiceMock(['addDocument', 'commit', 'deleteById']) |
|
183
|
|
|
->expects($this->exactly(1)) |
|
184
|
|
|
->method('deleteById') |
|
185
|
|
|
->with($this->getExpectedDocumentId($id, Versioned::LIVE)); |
|
186
|
|
|
|
|
187
|
|
|
SearchUpdater::flush_dirty_indexes(); |
|
188
|
|
|
|
|
189
|
|
|
// Delete the stage record |
|
190
|
|
|
Versioned::set_stage(Versioned::DRAFT); |
|
191
|
|
|
|
|
192
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Too')); |
|
193
|
|
|
$item->write(); |
|
194
|
|
|
$item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE); |
|
|
|
|
|
|
195
|
|
|
$id = clone $item; |
|
196
|
|
|
$item->delete(); |
|
197
|
|
|
|
|
198
|
|
|
// Check that only the 'Stage' version is deleted |
|
199
|
|
|
$this->getServiceMock(['addDocument', 'commit', 'deleteById']) |
|
200
|
|
|
->expects($this->exactly(1)) |
|
201
|
|
|
->method('deleteById') |
|
202
|
|
|
->with($this->getExpectedDocumentId($id, Versioned::DRAFT)); |
|
203
|
|
|
|
|
204
|
|
|
SearchUpdater::flush_dirty_indexes(); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: