Test for ambiguous relationships

See https://github.com/silverstripe/silverstripe-fulltextsearch/pull/145
This commit is contained in:
Ingo Schommer 2017-06-19 10:50:11 +12:00 committed by Daniel Hensby
parent 2b3357565e
commit b7f4c1e5f8
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
2 changed files with 75 additions and 2 deletions

View File

@ -21,6 +21,20 @@ class SearchUpdaterTest_Container extends DataObject
);
}
/**
* 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'
);
}
class SearchUpdaterTest_HasOne extends DataObject
{
private static $db = array(
@ -41,7 +55,8 @@ class SearchUpdaterTest_HasMany extends DataObject
);
private static $has_one = array(
'HasManyContainer' => 'SearchUpdaterTest_Container'
'HasManyContainer' => 'SearchUpdaterTest_Container',
'HasManyOtherContainer' => 'SearchUpdaterTest_OtherContainer'
);
}
@ -53,7 +68,8 @@ class SearchUpdaterTest_ManyMany extends DataObject
);
private static $belongs_many_many = array(
'ManyManyContainer' => 'SearchUpdaterTest_Container'
'ManyManyContainer' => 'SearchUpdaterTest_Container',
'ManyManyOtherContainer' => 'SearchUpdaterTest_OtherContainer'
);
}

View File

@ -53,6 +53,44 @@ class SolrIndexTest extends SapphireTest
$this->assertEquals('SearchUpdaterTest_ManyMany', $data['class']);
}
public function testFieldDataAmbiguousHasMany()
{
$index = new SolrIndexTest_AmbiguousRelationIndex();
$data = $index->fieldData('HasManyObjects.Field1');
$this->assertArrayHasKey('SearchUpdaterTest_Container_HasManyObjects_Field1', $data);
$this->assertArrayHasKey('SearchUpdaterTest_OtherContainer_HasManyObjects_Field1', $data);
$dataContainer = $data['SearchUpdaterTest_Container_HasManyObjects_Field1'];
$this->assertEquals($dataContainer['origin'], 'SearchUpdaterTest_Container');
$this->assertEquals($dataContainer['base'], 'SearchUpdaterTest_Container');
$this->assertEquals($dataContainer['class'], 'SearchUpdaterTest_HasMany');
$dataOtherContainer = $data['SearchUpdaterTest_OtherContainer_HasManyObjects_Field1'];
$this->assertEquals($dataOtherContainer['origin'], 'SearchUpdaterTest_OtherContainer');
$this->assertEquals($dataOtherContainer['base'], 'SearchUpdaterTest_OtherContainer');
$this->assertEquals($dataOtherContainer['class'], 'SearchUpdaterTest_HasMany');
}
public function testFieldDataAmbiguousManyMany()
{
$index = new SolrIndexTest_AmbiguousRelationIndex();
$data = $index->fieldData('ManyManyObjects.Field1');
$this->assertArrayHasKey('SearchUpdaterTest_Container_ManyManyObjects_Field1', $data);
$this->assertArrayHasKey('SearchUpdaterTest_OtherContainer_ManyManyObjects_Field1', $data);
$dataContainer = $data['SearchUpdaterTest_Container_ManyManyObjects_Field1'];
$this->assertEquals($dataContainer['origin'], 'SearchUpdaterTest_Container');
$this->assertEquals($dataContainer['base'], 'SearchUpdaterTest_Container');
$this->assertEquals($dataContainer['class'], 'SearchUpdaterTest_ManyMany');
$dataOtherContainer = $data['SearchUpdaterTest_OtherContainer_ManyManyObjects_Field1'];
$this->assertEquals($dataOtherContainer['origin'], 'SearchUpdaterTest_OtherContainer');
$this->assertEquals($dataOtherContainer['base'], 'SearchUpdaterTest_OtherContainer');
$this->assertEquals($dataOtherContainer['class'], 'SearchUpdaterTest_ManyMany');
}
/**
* Test boosting on SearchQuery
*/
@ -359,3 +397,22 @@ class SolrIndexTest_BoostedIndex extends SolrIndex
$this->addBoostedField('Field2', null, array(), 2.1);
}
}
class SolrIndexTest_AmbiguousRelationIndex extends SolrIndex
{
protected function getStoredDefault()
{
// Override isDev defaulting to stored
return 'false';
}
public function init()
{
$this->addClass('SearchUpdaterTest_Container');
$this->addClass('SearchUpdaterTest_OtherContainer');
// These relationships exist on both classes
$this->addFilterField('HasManyObjects.Field1');
$this->addFilterField('ManyManyObjects.Field1');
}
}