2014-03-24 21:55:13 +01:00
|
|
|
<?php
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
if (class_exists('Phockito')) {
|
2015-11-27 00:38:54 +01:00
|
|
|
Phockito::include_hamcrest(false);
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class SolrIndexVersionedTest extends SapphireTest
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
protected $extraDataObjects = array(
|
2016-04-15 07:59:10 +02:00
|
|
|
'SearchVariantVersionedTest_Item',
|
|
|
|
'SolrIndexVersionedTest_Object',
|
2015-11-21 07:19:20 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
if (!class_exists('Phockito')) {
|
|
|
|
$this->skipTest = true;
|
2016-04-15 07:59:10 +02:00
|
|
|
$this->markTestSkipped("These tests need the Phockito module installed to run");
|
|
|
|
return;
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check versioned available
|
|
|
|
if (!class_exists('Versioned')) {
|
|
|
|
$this->skipTest = true;
|
2016-04-15 07:59:10 +02:00
|
|
|
$this->markTestSkipped('The versioned decorator is not installed');
|
|
|
|
return;
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self::$index === null) {
|
|
|
|
self::$index = singleton('SolrVersionedTest_Index');
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchUpdater::bind_manipulation_capture();
|
|
|
|
|
|
|
|
Config::inst()->update('Injector', 'SearchUpdateProcessor', array(
|
|
|
|
'class' => 'SearchUpdateImmediateProcessor'
|
|
|
|
));
|
|
|
|
|
|
|
|
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();
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
Versioned::set_reading_mode($this->oldMode);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getServiceMock()
|
|
|
|
{
|
|
|
|
return Phockito::mock('Solr3Service');
|
|
|
|
}
|
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;
|
|
|
|
$class = ClassInfo::baseDataClass($object);
|
2015-11-21 07:19:20 +01:00
|
|
|
// Prevent subsites from breaking tests
|
2016-04-15 07:59:10 +02:00
|
|
|
$subsites = '';
|
|
|
|
if(class_exists('Subsite') && $object->hasOne('Subsite')) {
|
|
|
|
$subsites = '"SearchVariantSubsites":"0",';
|
|
|
|
}
|
|
|
|
return $id.'-'.$class.'-{'.$subsites.'"SearchVariantVersioned":"'.$stage.'"}';
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function testPublishing()
|
|
|
|
{
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Setup mocks
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
|
|
self::$index->setService($serviceMock);
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check that write updates Stage
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
Phockito::reset($serviceMock);
|
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();
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2016-04-15 07:59:10 +02:00
|
|
|
$doc1 = new SolrDocumentMatcher(array(
|
|
|
|
'_documentid' => $this->getExpectedDocumentId($item, 'Stage'),
|
|
|
|
'ClassName' => 'SearchVariantVersionedTest_Item',
|
|
|
|
'SearchVariantVersionedTest_Item_TestText' => 'Foo',
|
|
|
|
'_versionedstage' => 'Stage'
|
2015-11-21 07:19:20 +01:00
|
|
|
));
|
2016-04-15 07:59:10 +02:00
|
|
|
$doc2 = new SolrDocumentMatcher(array(
|
|
|
|
'_documentid' => $this->getExpectedDocumentId($object, 'Stage'),
|
|
|
|
'ClassName' => 'SolrIndexVersionedTest_Object',
|
|
|
|
'SolrIndexVersionedTest_Object_TestText' => 'Bar',
|
|
|
|
'_versionedstage' => 'Stage'
|
|
|
|
));
|
|
|
|
Phockito::verify($serviceMock)->addDocument($doc1);
|
|
|
|
Phockito::verify($serviceMock)->addDocument($doc2);
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check that write updates Live
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
Phockito::reset($serviceMock);
|
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();
|
|
|
|
$item->publish('Stage', 'Live');
|
2016-04-15 07:59:10 +02:00
|
|
|
$object = new SolrIndexVersionedTest_Object(array('TestText' => 'Bar'));
|
|
|
|
$object->write();
|
|
|
|
$object->publish('Stage', 'Live');
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
$doc = new SolrDocumentMatcher(array(
|
2016-04-15 07:59:10 +02:00
|
|
|
'_documentid' => $this->getExpectedDocumentId($item, 'Live'),
|
|
|
|
'ClassName' => 'SearchVariantVersionedTest_Item',
|
|
|
|
'SearchVariantVersionedTest_Item_TestText' => 'Foo',
|
|
|
|
'_versionedstage' => 'Live'
|
|
|
|
));
|
|
|
|
$doc2 = new SolrDocumentMatcher(array(
|
|
|
|
'_documentid' => $this->getExpectedDocumentId($object, 'Live'),
|
|
|
|
'ClassName' => 'SolrIndexVersionedTest_Object',
|
|
|
|
'SolrIndexVersionedTest_Object_TestText' => 'Bar',
|
|
|
|
'_versionedstage' => 'Live'
|
2015-11-21 07:19:20 +01:00
|
|
|
));
|
|
|
|
Phockito::verify($serviceMock)->addDocument($doc);
|
2016-04-15 07:59:10 +02:00
|
|
|
Phockito::verify($serviceMock)->addDocument($doc2);
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function testDelete()
|
|
|
|
{
|
|
|
|
// Setup mocks
|
|
|
|
$serviceMock = $this->getServiceMock();
|
|
|
|
self::$index->setService($serviceMock);
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Delete the live record (not the stage)
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
Phockito::reset($serviceMock);
|
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();
|
|
|
|
$item->publish('Stage', 'Live');
|
|
|
|
Versioned::reading_stage('Live');
|
2016-04-15 07:59:10 +02:00
|
|
|
$id = clone $item;
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->delete();
|
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
Phockito::verify($serviceMock, 1)
|
|
|
|
->deleteById($this->getExpectedDocumentId($id, 'Live'));
|
|
|
|
Phockito::verify($serviceMock, 0)
|
|
|
|
->deleteById($this->getExpectedDocumentId($id, 'Stage'));
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Delete the stage record
|
|
|
|
Versioned::reading_stage('Stage');
|
|
|
|
Phockito::reset($serviceMock);
|
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();
|
|
|
|
$item->publish('Stage', 'Live');
|
2016-04-15 07:59:10 +02:00
|
|
|
$id = clone $item;
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->delete();
|
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
Phockito::verify($serviceMock, 1)
|
|
|
|
->deleteById($this->getExpectedDocumentId($id, 'Stage'));
|
|
|
|
Phockito::verify($serviceMock, 0)
|
|
|
|
->deleteById($this->getExpectedDocumentId($id, 'Live'));
|
|
|
|
}
|
2014-03-24 21:55:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SolrVersionedTest_Index extends SolrIndex
|
|
|
|
{
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->addClass('SearchVariantVersionedTest_Item');
|
2016-04-15 07:59:10 +02:00
|
|
|
$this->addClass('SolrIndexVersionedTest_Object');
|
2015-11-21 07:19:20 +01:00
|
|
|
$this->addFilterField('TestText');
|
2016-04-15 07:59:10 +02:00
|
|
|
$this->addFulltextField('Content');
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2014-03-24 21:55:13 +01:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
if (!class_exists('Phockito')) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-24 21:55:13 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SolrDocumentMatcher extends Hamcrest_BaseMatcher
|
|
|
|
{
|
|
|
|
protected $properties;
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function __construct($properties)
|
|
|
|
{
|
|
|
|
$this->properties = $properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function describeTo(\Hamcrest_Description $description)
|
|
|
|
{
|
|
|
|
$description->appendText('Apache_Solr_Document with properties '.var_export($this->properties, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function matches($item)
|
|
|
|
{
|
|
|
|
if (! ($item instanceof Apache_Solr_Document)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
foreach ($this->properties as $key => $value) {
|
|
|
|
if ($item->{$key} != $value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-03-24 21:55:13 +01:00
|
|
|
}
|
2016-04-15 07:59:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Non-sitetree versioned dataobject
|
|
|
|
*/
|
|
|
|
class SolrIndexVersionedTest_Object extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
private static $extensions = array(
|
|
|
|
'Versioned'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Title' => 'Varchar',
|
|
|
|
'Content' => 'Text',
|
|
|
|
'TestText' => 'Varchar',
|
|
|
|
);
|
|
|
|
}
|