2011-05-02 06:33:05 +02:00
|
|
|
<?php
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SearchUpdaterTest_Container extends DataObject
|
|
|
|
{
|
|
|
|
private static $db = array(
|
|
|
|
'Field1' => 'Varchar',
|
|
|
|
'Field2' => 'Varchar',
|
|
|
|
'MyDate' => 'Date',
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_one = array(
|
|
|
|
'HasOneObject' => 'SearchUpdaterTest_HasOne'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_many = array(
|
|
|
|
'HasManyObjects' => 'SearchUpdaterTest_HasMany'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
|
|
|
'ManyManyObjects' => 'SearchUpdaterTest_ManyMany'
|
|
|
|
);
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 00:50:11 +02:00
|
|
|
/**
|
|
|
|
* Used to test ambiguous relationships.
|
|
|
|
*/
|
|
|
|
class SearchUpdaterTest_OtherContainer extends DataObject
|
|
|
|
{
|
|
|
|
private static $has_many = array(
|
|
|
|
'HasManyObjects' => 'SearchUpdaterTest_HasMany'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
|
|
|
'ManyManyObjects' => 'SearchUpdaterTest_ManyMany'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-06-15 23:39:37 +02:00
|
|
|
/**
|
|
|
|
* Used to test inherited ambiguous relationships.
|
|
|
|
*/
|
|
|
|
class SearchUpdaterTest_ExtendedContainer extends SearchUpdaterTest_OtherContainer
|
|
|
|
{
|
|
|
|
private static $db = array(
|
|
|
|
'SomeField' => 'Varchar',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SearchUpdaterTest_HasOne extends DataObject
|
|
|
|
{
|
|
|
|
private static $db = array(
|
|
|
|
'Field1' => 'Varchar',
|
|
|
|
'Field2' => 'Varchar'
|
|
|
|
);
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
private static $has_many = array(
|
|
|
|
'HasManyContainers' => 'SearchUpdaterTest_Container'
|
|
|
|
);
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SearchUpdaterTest_HasMany extends DataObject
|
|
|
|
{
|
|
|
|
private static $db = array(
|
|
|
|
'Field1' => 'Varchar',
|
|
|
|
'Field2' => 'Varchar'
|
|
|
|
);
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
private static $has_one = array(
|
2017-06-19 00:50:11 +02:00
|
|
|
'HasManyContainer' => 'SearchUpdaterTest_Container',
|
2017-06-15 23:39:37 +02:00
|
|
|
'HasManyOtherContainer' => 'SearchUpdaterTest_OtherContainer',
|
2015-11-21 07:19:20 +01:00
|
|
|
);
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SearchUpdaterTest_ManyMany extends DataObject
|
|
|
|
{
|
|
|
|
private static $db = array(
|
|
|
|
'Field1' => 'Varchar',
|
|
|
|
'Field2' => 'Varchar'
|
|
|
|
);
|
2014-08-21 06:58:00 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
private static $belongs_many_many = array(
|
2017-06-19 00:50:11 +02:00
|
|
|
'ManyManyContainer' => 'SearchUpdaterTest_Container',
|
2017-06-15 23:39:37 +02:00
|
|
|
'ManyManyOtherContainer' => 'SearchUpdaterTest_OtherContainer',
|
2015-11-21 07:19:20 +01:00
|
|
|
);
|
2014-08-21 06:58:00 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SearchUpdaterTest_Index extends SearchIndex_Recording
|
|
|
|
{
|
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
$this->addClass('SearchUpdaterTest_Container');
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$this->addFilterField('Field1');
|
|
|
|
$this->addFilterField('HasOneObject.Field1');
|
|
|
|
$this->addFilterField('HasManyObjects.Field1');
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class SearchUpdaterTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected $usesDatabase = true;
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
private static $index = null;
|
2016-04-15 05:46:19 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2013-09-02 00:10:14 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
if (self::$index === null) {
|
|
|
|
self::$index = singleton(get_class($this).'_Index');
|
|
|
|
} else {
|
|
|
|
self::$index->reset();
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::bind_manipulation_capture();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
Config::inst()->update('Injector', 'SearchUpdateProcessor', array(
|
|
|
|
'class' => 'SearchUpdateImmediateProcessor'
|
|
|
|
));
|
2013-07-25 04:27:09 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
FullTextSearch::force_index_list(self::$index);
|
|
|
|
SearchUpdater::clear_dirty_indexes();
|
|
|
|
}
|
2013-07-25 04:27:09 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function testBasic()
|
|
|
|
{
|
|
|
|
$item = new SearchUpdaterTest_Container();
|
|
|
|
$item->write();
|
2013-07-25 04:27:09 +02:00
|
|
|
|
2015-11-21 07:19:20 +01: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 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function testHasOneHook()
|
|
|
|
{
|
|
|
|
$hasOne = new SearchUpdaterTest_HasOne();
|
|
|
|
$hasOne->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$alternateHasOne = new SearchUpdaterTest_HasOne();
|
|
|
|
$alternateHasOne->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$container1 = new SearchUpdaterTest_Container();
|
|
|
|
$container1->HasOneObjectID = $hasOne->ID;
|
|
|
|
$container1->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$container2 = new SearchUpdaterTest_Container();
|
|
|
|
$container2->HasOneObjectID = $hasOne->ID;
|
|
|
|
$container2->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$container3 = new SearchUpdaterTest_Container();
|
|
|
|
$container3->HasOneObjectID = $alternateHasOne->ID;
|
|
|
|
$container3->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check the default "writing a document updates the document"
|
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2014-02-13 23:32:20 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$added = self::$index->getAdded(array('ID'));
|
|
|
|
// Some databases don't output $added in a consistent order; that's okay
|
|
|
|
usort($added, function ($a, $b) {return $a['ID']-$b['ID']; });
|
2014-02-13 23:32:20 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$this->assertEquals($added, array(
|
|
|
|
array('ID' => $container1->ID),
|
|
|
|
array('ID' => $container2->ID),
|
|
|
|
array('ID' => $container3->ID)
|
|
|
|
));
|
2014-02-13 23:32:20 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check writing a has_one tracks back to the origin documents
|
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
|
|
|
$hasOne->Field1 = "Updated";
|
|
|
|
$hasOne->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
$added = self::$index->getAdded(array('ID'));
|
|
|
|
// Some databases don't output $added in a consistent order; that's okay
|
|
|
|
usort($added, function ($a, $b) {return $a['ID']-$b['ID']; });
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$this->assertEquals($added, array(
|
|
|
|
array('ID' => $container1->ID),
|
|
|
|
array('ID' => $container2->ID)
|
|
|
|
));
|
2014-02-13 23:32:20 +01:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check updating an unrelated field doesn't track back
|
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
|
|
|
$hasOne->Field2 = "Updated";
|
|
|
|
$hasOne->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
$this->assertEquals(self::$index->getAdded(array('ID')), array());
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
// Check writing a has_one tracks back to the origin documents
|
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
|
|
|
$alternateHasOne->Field1= "Updated";
|
|
|
|
$alternateHasOne->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
$this->assertEquals(self::$index->getAdded(array('ID')), array(
|
|
|
|
array('ID' => $container3->ID)
|
|
|
|
));
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function testHasManyHook()
|
|
|
|
{
|
|
|
|
$container1 = new SearchUpdaterTest_Container();
|
|
|
|
$container1->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$container2 = new SearchUpdaterTest_Container();
|
|
|
|
$container2->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
//self::$index->reset();
|
|
|
|
//SearchUpdater::clear_dirty_indexes();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$hasMany1 = new SearchUpdaterTest_HasMany();
|
|
|
|
$hasMany1->HasManyContainerID = $container1->ID;
|
|
|
|
$hasMany1->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$hasMany2 = new SearchUpdaterTest_HasMany();
|
|
|
|
$hasMany2->HasManyContainerID = $container1->ID;
|
|
|
|
$hasMany2->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$this->assertEquals(self::$index->getAdded(array('ID')), array(
|
|
|
|
array('ID' => $container1->ID),
|
|
|
|
array('ID' => $container2->ID)
|
|
|
|
));
|
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
|
|
|
$hasMany1->Field1 = 'Updated';
|
|
|
|
$hasMany1->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$hasMany2->Field1 = 'Updated';
|
|
|
|
$hasMany2->write();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
SearchUpdater::flush_dirty_indexes();
|
|
|
|
$this->assertEquals(self::$index->getAdded(array('ID')), array(
|
|
|
|
array('ID' => $container1->ID)
|
|
|
|
));
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
}
|