2011-05-02 16:33:05 +12:00
|
|
|
<?php
|
|
|
|
|
2017-11-15 09:48:52 +13:00
|
|
|
namespace SilverStripe\FullTextSearch\Tests;
|
|
|
|
|
2017-04-21 16:34:04 +12:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-04-26 22:52:20 +12:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-11-15 09:48:52 +13:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-04-21 16:34:04 +12:00
|
|
|
use SilverStripe\FullTextSearch\Search\FullTextSearch;
|
2017-11-15 09:48:52 +13:00
|
|
|
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateProcessor;
|
|
|
|
use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateImmediateProcessor;
|
2020-06-10 17:22:20 +12:00
|
|
|
use SilverStripe\FullTextSearch\Search\Services\SearchableService;
|
2017-11-15 09:48:52 +13:00
|
|
|
use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater;
|
2017-04-21 16:34:04 +12:00
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_Container;
|
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_HasOne;
|
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_HasMany;
|
|
|
|
use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_Index;
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
class SearchUpdaterTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected $usesDatabase = true;
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
private static $index = null;
|
2016-04-15 15:46:19 +12:00
|
|
|
|
2021-11-02 14:48:12 +13:00
|
|
|
protected function setUp(): void
|
2015-11-21 19:19:20 +13:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2013-09-02 10:10:14 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
if (self::$index === null) {
|
2017-04-21 16:34:04 +12:00
|
|
|
self::$index = SearchUpdaterTest_Index::singleton();
|
2015-11-21 19:19:20 +13:00
|
|
|
} else {
|
|
|
|
self::$index->reset();
|
|
|
|
}
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2017-04-26 22:52:20 +12:00
|
|
|
Config::modify()->set(Injector::class, SearchUpdateProcessor::class, array(
|
|
|
|
'class' => SearchUpdateImmediateProcessor::class
|
2015-11-21 19:19:20 +13:00
|
|
|
));
|
2013-07-25 14:27:09 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
FullTextSearch::force_index_list(self::$index);
|
|
|
|
SearchUpdater::clear_dirty_indexes();
|
|
|
|
}
|
2013-07-25 14:27:09 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
public function testBasic()
|
|
|
|
{
|
|
|
|
$item = new SearchUpdaterTest_Container();
|
|
|
|
$item->write();
|
2021-11-02 14:48:12 +13:00
|
|
|
$this->assertTrue(true);
|
2013-07-25 14:27:09 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
// TODO: Make sure changing field1 updates item.
|
|
|
|
// TODO: Get updating just field2 to not update item (maybe not possible - variants complicate)
|
|
|
|
}
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
public function testHasOneHook()
|
|
|
|
{
|
2020-06-10 17:22:20 +12:00
|
|
|
$classesToSkip = [SearchUpdaterTest_Container::class];
|
|
|
|
Config::modify()->set(SearchableService::class, 'indexing_canview_exclude_classes', $classesToSkip);
|
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$hasOne = new SearchUpdaterTest_HasOne();
|
|
|
|
$hasOne->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$alternateHasOne = new SearchUpdaterTest_HasOne();
|
|
|
|
$alternateHasOne->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$container1 = new SearchUpdaterTest_Container();
|
|
|
|
$container1->HasOneObjectID = $hasOne->ID;
|
|
|
|
$container1->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$container2 = new SearchUpdaterTest_Container();
|
|
|
|
$container2->HasOneObjectID = $hasOne->ID;
|
|
|
|
$container2->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$container3 = new SearchUpdaterTest_Container();
|
|
|
|
$container3->HasOneObjectID = $alternateHasOne->ID;
|
|
|
|
$container3->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
// Check the default "writing a document updates the document"
|
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2017-12-04 11:53:37 +13:00
|
|
|
$added = self::$index->getAdded(['ID']);
|
2015-11-21 19:19:20 +13:00
|
|
|
// Some databases don't output $added in a consistent order; that's okay
|
2017-11-15 09:48:52 +13:00
|
|
|
usort($added, function ($a, $b) {
|
|
|
|
return $a['ID']-$b['ID'];
|
|
|
|
});
|
2014-02-14 11:32:20 +13:00
|
|
|
|
2017-12-04 11:53:37 +13:00
|
|
|
$this->assertEquals([
|
|
|
|
['ID' => $container1->ID],
|
|
|
|
['ID' => $container2->ID],
|
|
|
|
['ID' => $container3->ID],
|
|
|
|
], $added);
|
2014-02-14 11:32:20 +13:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
// Check writing a has_one tracks back to the origin documents
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
self::$index->reset();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$hasOne->Field1 = "Updated";
|
|
|
|
$hasOne->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2017-12-04 11:53:37 +13:00
|
|
|
$added = self::$index->getAdded(['ID']);
|
2017-04-22 21:31:34 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
// Some databases don't output $added in a consistent order; that's okay
|
2017-11-15 09:48:52 +13:00
|
|
|
usort($added, function ($a, $b) {
|
|
|
|
return $a['ID']-$b['ID'];
|
|
|
|
});
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2017-12-04 11:53:37 +13:00
|
|
|
$this->assertEquals([
|
|
|
|
['ID' => $container1->ID],
|
|
|
|
['ID' => $container2->ID],
|
|
|
|
], $added);
|
2014-02-14 11:32:20 +13:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
// Check updating an unrelated field doesn't track back
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
self::$index->reset();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$hasOne->Field2 = "Updated";
|
|
|
|
$hasOne->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2017-12-04 11:53:37 +13:00
|
|
|
$this->assertEquals([], self::$index->getAdded(['ID']));
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
// Check writing a has_one tracks back to the origin documents
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
self::$index->reset();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$alternateHasOne->Field1= "Updated";
|
|
|
|
$alternateHasOne->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2017-12-04 11:53:37 +13:00
|
|
|
$this->assertEquals([
|
|
|
|
['ID' => $container3->ID],
|
|
|
|
], self::$index->getAdded(['ID']));
|
2015-11-21 19:19:20 +13:00
|
|
|
}
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
public function testHasManyHook()
|
|
|
|
{
|
2020-06-10 17:22:20 +12:00
|
|
|
$classesToSkip = [SearchUpdaterTest_Container::class];
|
|
|
|
Config::modify()->set(SearchableService::class, 'indexing_canview_exclude_classes', $classesToSkip);
|
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$container1 = new SearchUpdaterTest_Container();
|
|
|
|
$container1->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$container2 = new SearchUpdaterTest_Container();
|
|
|
|
$container2->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
//self::$index->reset();
|
|
|
|
//SearchUpdater::clear_dirty_indexes();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$hasMany1 = new SearchUpdaterTest_HasMany();
|
|
|
|
$hasMany1->HasManyContainerID = $container1->ID;
|
|
|
|
$hasMany1->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$hasMany2 = new SearchUpdaterTest_HasMany();
|
|
|
|
$hasMany2->HasManyContainerID = $container1->ID;
|
|
|
|
$hasMany2->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2017-12-04 11:53:37 +13:00
|
|
|
$this->assertEquals([
|
|
|
|
['ID' => $container1->ID],
|
|
|
|
['ID' => $container2->ID],
|
|
|
|
], self::$index->getAdded(['ID']));
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
self::$index->reset();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$hasMany1->Field1 = 'Updated';
|
|
|
|
$hasMany1->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
$hasMany2->Field1 = 'Updated';
|
|
|
|
$hasMany2->write();
|
2011-05-02 16:33:05 +12:00
|
|
|
|
2015-11-21 19:19:20 +13:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2017-12-04 11:53:37 +13:00
|
|
|
$this->assertEquals([
|
|
|
|
['ID' => $container1->ID],
|
|
|
|
], self::$index->getAdded(['ID']));
|
2015-11-21 19:19:20 +13:00
|
|
|
}
|
2011-05-02 16:33:05 +12:00
|
|
|
}
|