2011-05-02 06:33:05 +02:00
|
|
|
<?php
|
|
|
|
|
2017-11-14 21:48:52 +01:00
|
|
|
namespace SilverStripe\FullTextSearch\Tests;
|
|
|
|
|
2017-04-26 12:52:20 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-05-01 10:18:59 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2017-11-14 21:48:52 +01:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-04-26 12:52:20 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\FullTextSearch;
|
2017-04-21 03:32:39 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\Indexes\SearchIndex_Recording;
|
2020-06-10 07:22:20 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\Services\SearchableService;
|
2017-05-01 10:18:59 +02:00
|
|
|
use SilverStripe\FullTextSearch\Search\Variants\SearchVariantVersioned;
|
2017-04-26 12:52:20 +02:00
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchVariantVersionedTest\SearchVariantVersionedTest_Index;
|
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchVariantVersionedTest\SearchVariantVersionedTest_Item;
|
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchVariantVersionedTest\SearchVariantVersionedTest_IndexNoStage;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateProcessor;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateImmediateProcessor;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
|
2017-04-21 02:23:27 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SearchVariantVersionedTest extends SapphireTest
|
|
|
|
{
|
2016-04-15 05:46:19 +02:00
|
|
|
/**
|
|
|
|
* @var SearchVariantVersionedTest_Index
|
|
|
|
*/
|
2015-11-21 07:19:20 +01:00
|
|
|
private static $index = null;
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2017-04-26 12:52:20 +02:00
|
|
|
protected static $extra_dataobjects = array(
|
|
|
|
SearchVariantVersionedTest_Item::class
|
2015-11-21 07:19:20 +01:00
|
|
|
);
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2017-11-14 05:05:30 +01:00
|
|
|
protected function setUp()
|
2015-11-21 07:19:20 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2015-06-29 07:09:24 +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(SearchVariantVersionedTest_Index::class);
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2012-07-19 03:38:43 +02:00
|
|
|
|
2017-11-14 05:05:30 +01:00
|
|
|
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, array(
|
2017-04-26 12:52:20 +02:00
|
|
|
'class' => SearchUpdateImmediateProcessor::class
|
2015-11-21 07:19:20 +01:00
|
|
|
));
|
2014-03-24 21:55:13 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
FullTextSearch::force_index_list(self::$index);
|
|
|
|
SearchUpdater::clear_dirty_indexes();
|
|
|
|
}
|
2014-03-24 21:55:13 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function testPublishing()
|
|
|
|
{
|
|
|
|
// Check that write updates Stage
|
2020-06-10 07:22:20 +02:00
|
|
|
$classesToSkip = [SearchVariantVersionedTest_Item::class];
|
|
|
|
Config::modify()->set(SearchableService::class, 'indexing_canview_exclude_classes', $classesToSkip);
|
|
|
|
Config::modify()->set(SearchableService::class, 'variant_state_draft_excluded', false);
|
2014-03-24 21:55:13 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo'));
|
|
|
|
$item->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2017-04-26 12:52:20 +02:00
|
|
|
$this->assertEquals(array(
|
2015-11-21 07:19:20 +01:00
|
|
|
array('ID' => $item->ID, '_versionedstage' => 'Stage')
|
2017-04-26 12:52:20 +02:00
|
|
|
), self::$index->getAdded(array('ID', '_versionedstage')));
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check that publish updates Live
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
self::$index->reset();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2017-04-26 12:52:20 +02:00
|
|
|
$item->copyVersionToStage('Stage', 'Live');
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2017-04-26 12:52:20 +02:00
|
|
|
$this->assertEquals(array(
|
|
|
|
array('ID' => $item->ID, '_versionedstage' => 'Stage'),
|
2015-11-21 07:19:20 +01:00
|
|
|
array('ID' => $item->ID, '_versionedstage' => 'Live')
|
2017-04-26 12:52:20 +02:00
|
|
|
), self::$index->getAdded(array('ID', '_versionedstage')));
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Just update a SiteTree field, and check it updates Stage
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
self::$index->reset();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$item->Title = "Pow!";
|
|
|
|
$item->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2016-04-15 05:46:19 +02:00
|
|
|
|
|
|
|
$expected = array(array(
|
|
|
|
'ID' => $item->ID,
|
|
|
|
'_versionedstage' => 'Stage'
|
2015-11-21 07:19:20 +01:00
|
|
|
));
|
2016-04-15 05:46:19 +02:00
|
|
|
$added = self::$index->getAdded(array('ID', '_versionedstage'));
|
|
|
|
$this->assertEquals($expected, $added);
|
2017-03-08 23:40:13 +01:00
|
|
|
|
|
|
|
// Test unpublish
|
|
|
|
|
|
|
|
self::$index->reset();
|
|
|
|
|
|
|
|
$item->deleteFromStage('Live');
|
|
|
|
|
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
|
2017-03-09 11:32:56 +01:00
|
|
|
$this->assertCount(1, self::$index->deleted);
|
2017-03-08 23:40:13 +01:00
|
|
|
$this->assertEquals(
|
2017-05-01 10:18:59 +02:00
|
|
|
SiteTree::class,
|
2017-03-08 23:40:13 +01:00
|
|
|
self::$index->deleted[0]['base']
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$item->ID,
|
|
|
|
self::$index->deleted[0]['id']
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
'Live',
|
2017-05-01 10:18:59 +02:00
|
|
|
self::$index->deleted[0]['state'][SearchVariantVersioned::class]
|
2017-03-08 23:40:13 +01:00
|
|
|
);
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2012-08-28 23:21:51 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function testExcludeVariantState()
|
|
|
|
{
|
2017-04-26 12:52:20 +02:00
|
|
|
$index = singleton(SearchVariantVersionedTest_IndexNoStage::class);
|
2015-11-21 07:19:20 +01:00
|
|
|
FullTextSearch::force_index_list($index);
|
2012-08-28 23:21:51 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check that write doesn't update stage
|
|
|
|
$item = new SearchVariantVersionedTest_Item(array('TestText' => 'Foo'));
|
|
|
|
$item->write();
|
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2017-04-26 12:52:20 +02:00
|
|
|
$this->assertEquals(array(), $index->getAdded(array('ID', '_versionedstage')));
|
2012-08-28 23:21:51 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check that publish updates Live
|
|
|
|
$index->reset();
|
2015-06-29 07:09:24 +02:00
|
|
|
|
2017-04-26 12:52:20 +02:00
|
|
|
$item->copyVersionToStage('Stage', 'Live');
|
2015-06-29 07:09:24 +02:00
|
|
|
|
2017-04-26 12:52:20 +02:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
$this->assertEquals(array(
|
|
|
|
array('ID' => $item->ID, '_versionedstage' => 'Live')
|
|
|
|
), $index->getAdded(array('ID', '_versionedstage')));
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|
2017-12-04 21:11:51 +01:00
|
|
|
|
|
|
|
public function testCanBeDisabledViaConfig()
|
|
|
|
{
|
|
|
|
$variant = new SearchVariantVersioned;
|
|
|
|
|
|
|
|
Config::modify()->set(SearchVariantVersioned::class, 'enabled', true);
|
|
|
|
$this->assertTrue($variant->appliesToEnvironment());
|
|
|
|
|
|
|
|
Config::modify()->set(SearchVariantVersioned::class, 'enabled', false);
|
|
|
|
$this->assertFalse($variant->appliesToEnvironment());
|
|
|
|
}
|
2015-11-21 07:19:20 +01:00
|
|
|
}
|