SolrIndexVersionedTest::testDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 36
rs 9.552
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Subsites\Model\Subsite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
        Config::modify()->set(Injector::class, SearchUpdateProcessor::class, [
50
            'class' => SearchUpdateImmediateProcessor::class
51
        ]);
52
53
        FullTextSearch::force_index_list(self::$index);
54
        SearchUpdater::clear_dirty_indexes();
55
56
        $this->oldMode = Versioned::get_reading_mode();
57
        Versioned::set_stage(Versioned::DRAFT);
58
    }
59
60
    protected function tearDown()
61
    {
62
        Versioned::set_reading_mode($this->oldMode);
63
        parent::tearDown();
64
    }
65
66
    protected function getServiceMock($setMethods = array())
67
    {
68
        // Setup mock
69
        /** @var SilverStripe\FullTextSearch\Solr\Services\Solr3Service|ObjectProphecy $serviceMock */
70
        $serviceMock = $this->getMockBuilder(Solr3Service::class)
71
            ->setMethods($setMethods)
72
            ->getMock();
73
74
        self::$index->setService($serviceMock);
75
76
        return $serviceMock;
77
    }
78
79
    /**
80
     * @param DataObject $object Item being added
81
     * @param string $stage
82
     * @return string
83
     */
84
    protected function getExpectedDocumentId($object, $stage)
85
    {
86
        $id = $object->ID;
87
        $class = DataObject::getSchema()->baseDataClass($object);
88
        return $id . '-' . $class . '-{' . json_encode(SearchVariantVersioned::class) . ':"' . $stage . '"}';
89
    }
90
91
    /**
92
     * @param string $class
93
     * @param DataObject $object Item being added
94
     * @param string $value Value for class
95
     * @param string $stage Stage updated
96
     * @return Apache_Solr_Document
97
     */
98
    protected function getSolrDocument($class, $object, $value, $stage)
99
    {
100
        $doc = new Apache_Solr_Document();
101
        $doc->setField('_documentid', $this->getExpectedDocumentId($object, $stage));
102
        $doc->setField('ClassName', $class);
103
        $doc->setField(DataObject::getSchema()->baseDataClass($class) . '_TestText', $value);
104
        $doc->setField('_versionedstage', $stage);
105
        $doc->setField('ID', (int) $object->ID);
106
        $doc->setField('ClassHierarchy', SearchIntrospection::hierarchy($class));
107
        $doc->setFieldBoost('ID', false);
108
        $doc->setFieldBoost('ClassHierarchy', false);
109
110
        return $doc;
111
    }
112
113
    public function testPublishing()
114
    {
115
        // Check that write updates Stage
116
        Versioned::set_stage(Versioned::DRAFT);
117
118
        $item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo'));
119
        $item->write();
120
        $object = new SolrIndexVersionedTest_Object(array('TestText' => 'Bar'));
121
        $object->write();
122
123
        $doc1 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::DRAFT);
124
        $doc2 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::DRAFT);
125
126
        // Ensure correct call is made to Solr
127
        $this->getServiceMock(['addDocument', 'commit'])
128
            ->expects($this->exactly(2))
129
            ->method('addDocument')
130
            ->withConsecutive(
131
                [$this->equalTo($doc1)],
132
                [$this->equalTo($doc2)]
133
            );
134
135
        SearchUpdater::flush_dirty_indexes();
136
137
        // Check that write updates Live
138
        Versioned::set_stage(Versioned::DRAFT);
139
140
        $item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo'));
141
        $item->write();
142
        $item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
0 ignored issues
show
Bug introduced by
The method copyVersionToStage() does not exist on SilverStripe\FullTextSea...riantVersionedTest_Item. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        $item->/** @scrutinizer ignore-call */ 
143
               copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
Loading history...
143
144
        $object = new SolrIndexVersionedTest_Object(array('TestText' => 'Bar'));
145
        $object->write();
146
        $object->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
0 ignored issues
show
Bug introduced by
The method copyVersionToStage() does not exist on SilverStripe\FullTextSea...dexVersionedTest_Object. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
        $object->/** @scrutinizer ignore-call */ 
147
                 copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
Loading history...
147
148
        $doc1 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::DRAFT);
149
        $doc2 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::LIVE);
150
        $doc3 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::DRAFT);
151
        $doc4 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::LIVE);
152
153
        // Ensure correct call is made to Solr
154
        $this->getServiceMock(['addDocument', 'commit'])
155
            ->expects($this->exactly(4))
156
            ->method('addDocument')
157
            ->withConsecutive(
158
                [$doc1],
159
                [$doc2],
160
                [$doc3],
161
                [$doc4]
162
            );
163
164
        SearchUpdater::flush_dirty_indexes();
165
    }
166
167
    public function testDelete()
168
    {
169
        // Delete the live record (not the stage)
170
        Versioned::set_stage(Versioned::DRAFT);
171
172
        $item = new SearchVariantVersionedTest_Item(array('TestText' => 'Too'));
173
        $item->write();
174
        $item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
175
        Versioned::set_stage(Versioned::LIVE);
176
        $id = clone $item;
177
        $item->delete();
178
179
        // Check that only the 'Live' version is deleted
180
        $this->getServiceMock(['addDocument', 'commit', 'deleteById'])
181
            ->expects($this->exactly(1))
182
            ->method('deleteById')
183
            ->with($this->getExpectedDocumentId($id, Versioned::LIVE));
184
185
        SearchUpdater::flush_dirty_indexes();
186
187
        // Delete the stage record
188
        Versioned::set_stage(Versioned::DRAFT);
189
190
        $item = new SearchVariantVersionedTest_Item(array('TestText' => 'Too'));
191
        $item->write();
192
        $item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
193
        $id = clone $item;
194
        $item->delete();
195
196
        // Check that only the 'Stage' version is deleted
197
        $this->getServiceMock(['addDocument', 'commit', 'deleteById'])
198
            ->expects($this->exactly(1))
199
            ->method('deleteById')
200
            ->with($this->getExpectedDocumentId($id, Versioned::DRAFT));
201
202
        SearchUpdater::flush_dirty_indexes();
203
    }
204
}
205