2014-03-24 21:55:13 +01:00
|
|
|
<?php
|
|
|
|
|
2017-11-14 21:48:52 +01:00
|
|
|
namespace SilverStripe\FullTextSearch\Tests;
|
|
|
|
|
2017-12-04 21:11:51 +01:00
|
|
|
use Apache_Solr_Document;
|
2017-04-21 02:23:27 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-04-26 12:52:20 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-12-04 21:11:51 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-04-26 12:52:20 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\FullTextSearch\Search\FullTextSearch;
|
|
|
|
use SilverStripe\FullTextSearch\Search\SearchIntrospection;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Indexes\SearchIndex_Recording;
|
|
|
|
use SilverStripe\FullTextSearch\Solr\Services\Solr3Service;
|
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchVariantVersionedTest\SearchVariantVersionedTest_Item;
|
|
|
|
use SilverStripe\FullTextSearch\Tests\SolrIndexVersionedTest\SolrIndexVersionedTest_Object;
|
|
|
|
use SilverStripe\FullTextSearch\Tests\SolrIndexVersionedTest\SolrVersionedTest_Index;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateProcessor;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateImmediateProcessor;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
|
2017-12-04 21:11:51 +01:00
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariantSubsites;
|
2017-04-26 12:52:20 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariantVersioned;
|
2017-12-04 21:11:51 +01:00
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
2017-04-21 03:18:37 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2017-04-21 02:23:27 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SolrIndexVersionedTest extends SapphireTest
|
|
|
|
{
|
2017-12-04 21:11:51 +01:00
|
|
|
protected $usesDatabase = true;
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
protected $oldMode = null;
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
protected static $index = null;
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2017-12-04 21:11:51 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2017-04-26 12:52:20 +02:00
|
|
|
SearchVariantVersionedTest_Item::class,
|
2017-12-04 21:11:51 +01:00
|
|
|
SolrIndexVersionedTest_Object::class,
|
|
|
|
];
|
2015-11-21 07:19:20 +01:00
|
|
|
|
2017-11-14 05:05:30 +01:00
|
|
|
protected function setUp()
|
2015-11-21 07:19:20 +01:00
|
|
|
{
|
2017-12-04 21:11:51 +01:00
|
|
|
// Need to be set before parent::setUp() since they're executed before the tests start
|
|
|
|
Config::modify()->set(SearchVariantSubsites::class, 'enabled', false);
|
2017-11-14 05:05:30 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
parent::setUp();
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
if (self::$index === null) {
|
2017-04-26 12:52:20 +02:00
|
|
|
self::$index = singleton(SolrVersionedTest_Index::class);
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
|
|
|
|
2017-12-04 21:34:12 +01:00
|
|
|
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, [
|
2017-04-26 12:52:20 +02:00
|
|
|
'class' => SearchUpdateImmediateProcessor::class
|
2017-12-04 21:11:51 +01:00
|
|
|
]);
|
2015-11-21 07:19:20 +01:00
|
|
|
|
|
|
|
FullTextSearch::force_index_list(self::$index);
|
|
|
|
SearchUpdater::clear_dirty_indexes();
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$this->oldMode = Versioned::get_reading_mode();
|
2017-12-04 21:11:51 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2017-12-04 21:11:51 +01:00
|
|
|
protected function tearDown()
|
2015-11-21 07:19:20 +01:00
|
|
|
{
|
|
|
|
Versioned::set_reading_mode($this->oldMode);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2017-04-26 12:52:20 +02:00
|
|
|
protected function getServiceMock($setMethods = array())
|
2015-11-21 07:19:20 +01:00
|
|
|
{
|
2017-04-26 12:52:20 +02:00
|
|
|
// Setup mock
|
|
|
|
/** @var SilverStripe\FullTextSearch\Solr\Services\Solr3Service|ObjectProphecy $serviceMock */
|
|
|
|
$serviceMock = $this->getMockBuilder(Solr3Service::class)
|
|
|
|
->setMethods($setMethods)
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
self::$index->setService($serviceMock);
|
|
|
|
|
|
|
|
return $serviceMock;
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
/**
|
|
|
|
* @param DataObject $object Item being added
|
|
|
|
* @param string $stage
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getExpectedDocumentId($object, $stage)
|
2015-11-21 07:19:20 +01:00
|
|
|
{
|
2016-04-15 07:59:10 +02:00
|
|
|
$id = $object->ID;
|
2017-04-26 12:52:20 +02:00
|
|
|
$class = DataObject::getSchema()->baseDataClass($object);
|
2018-06-27 06:50:54 +02:00
|
|
|
return $id . '-' . $class . '-{' . json_encode(SearchVariantVersioned::class) . ':"' . $stage . '"}';
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2017-04-26 12:52:20 +02:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param DataObject $object Item being added
|
|
|
|
* @param string $value Value for class
|
|
|
|
* @param string $stage Stage updated
|
|
|
|
* @return Apache_Solr_Document
|
|
|
|
*/
|
|
|
|
protected function getSolrDocument($class, $object, $value, $stage)
|
2015-11-21 07:19:20 +01:00
|
|
|
{
|
2017-12-04 21:11:51 +01:00
|
|
|
$doc = new Apache_Solr_Document();
|
2017-04-26 12:52:20 +02:00
|
|
|
$doc->setField('_documentid', $this->getExpectedDocumentId($object, $stage));
|
|
|
|
$doc->setField('ClassName', $class);
|
|
|
|
$doc->setField(DataObject::getSchema()->baseDataClass($class) . '_TestText', $value);
|
|
|
|
$doc->setField('_versionedstage', $stage);
|
2017-12-04 21:11:51 +01:00
|
|
|
$doc->setField('ID', (int) $object->ID);
|
2017-04-26 12:52:20 +02:00
|
|
|
$doc->setField('ClassHierarchy', SearchIntrospection::hierarchy($class));
|
|
|
|
$doc->setFieldBoost('ID', false);
|
|
|
|
$doc->setFieldBoost('ClassHierarchy', false);
|
|
|
|
|
|
|
|
return $doc;
|
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2017-04-26 12:52:20 +02:00
|
|
|
public function testPublishing()
|
|
|
|
{
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check that write updates Stage
|
2017-12-04 21:11:51 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo'));
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->write();
|
2016-04-15 07:59:10 +02:00
|
|
|
$object = new SolrIndexVersionedTest_Object(array('TestText' => 'Bar'));
|
|
|
|
$object->write();
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2017-12-04 21:11:51 +01:00
|
|
|
$doc1 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::DRAFT);
|
|
|
|
$doc2 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::DRAFT);
|
2017-04-26 12:52:20 +02:00
|
|
|
|
|
|
|
// Ensure correct call is made to Solr
|
|
|
|
$this->getServiceMock(['addDocument', 'commit'])
|
|
|
|
->expects($this->exactly(2))
|
|
|
|
->method('addDocument')
|
|
|
|
->withConsecutive(
|
2017-12-04 21:11:51 +01:00
|
|
|
[$this->equalTo($doc1)],
|
|
|
|
[$this->equalTo($doc2)]
|
2017-04-26 12:52:20 +02:00
|
|
|
);
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check that write updates Live
|
2017-12-04 21:11:51 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo'));
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->write();
|
2017-12-04 21:11:51 +01:00
|
|
|
$item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
$object = new SolrIndexVersionedTest_Object(array('TestText' => 'Bar'));
|
|
|
|
$object->write();
|
2017-12-04 21:11:51 +01:00
|
|
|
$object->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2017-12-04 21:11:51 +01:00
|
|
|
$doc1 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::DRAFT);
|
|
|
|
$doc2 = $this->getSolrDocument(SearchVariantVersionedTest_Item::class, $item, 'Foo', Versioned::LIVE);
|
|
|
|
$doc3 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::DRAFT);
|
|
|
|
$doc4 = $this->getSolrDocument(SolrIndexVersionedTest_Object::class, $object, 'Bar', Versioned::LIVE);
|
2017-04-26 12:52:20 +02:00
|
|
|
|
|
|
|
// Ensure correct call is made to Solr
|
|
|
|
$this->getServiceMock(['addDocument', 'commit'])
|
|
|
|
->expects($this->exactly(4))
|
|
|
|
->method('addDocument')
|
|
|
|
->withConsecutive(
|
2017-12-04 21:11:51 +01:00
|
|
|
[$doc1],
|
|
|
|
[$doc2],
|
|
|
|
[$doc3],
|
|
|
|
[$doc4]
|
2017-04-26 12:52:20 +02:00
|
|
|
);
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function testDelete()
|
|
|
|
{
|
|
|
|
// Delete the live record (not the stage)
|
2017-12-04 21:11:51 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Too'));
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->write();
|
2017-12-04 21:11:51 +01:00
|
|
|
$item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2016-04-15 07:59:10 +02:00
|
|
|
$id = clone $item;
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->delete();
|
2017-04-26 12:52:20 +02:00
|
|
|
|
|
|
|
// Check that only the 'Live' version is deleted
|
|
|
|
$this->getServiceMock(['addDocument', 'commit', 'deleteById'])
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('deleteById')
|
2017-12-04 21:11:51 +01:00
|
|
|
->with($this->getExpectedDocumentId($id, Versioned::LIVE));
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Delete the stage record
|
2017-12-04 21:11:51 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2016-04-15 07:59:10 +02:00
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Too'));
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->write();
|
2017-12-04 21:11:51 +01:00
|
|
|
$item->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-04-15 07:59:10 +02:00
|
|
|
$id = clone $item;
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->delete();
|
2017-04-26 12:52:20 +02:00
|
|
|
|
|
|
|
// Check that only the 'Stage' version is deleted
|
|
|
|
$this->getServiceMock(['addDocument', 'commit', 'deleteById'])
|
|
|
|
->expects($this->exactly(1))
|
|
|
|
->method('deleteById')
|
2017-12-04 21:11:51 +01:00
|
|
|
->with($this->getExpectedDocumentId($id, Versioned::DRAFT));
|
2017-04-26 12:52:20 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
}
|
2014-03-24 21:55:13 +01:00
|
|
|
}
|