2009-08-11 01:34:32 +02:00
|
|
|
<?php
|
2013-06-08 10:28:15 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
/**
|
2013-06-08 10:28:15 +02:00
|
|
|
* This test class will focus on the when an search filter contains relational
|
|
|
|
* component such as has_one, has_many, many_many, the {@link SearchFilter::applyRelation($query)}
|
|
|
|
* will add the right "join" clauses to $query without the component parent
|
|
|
|
* class missing from "join" chain.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2013-06-08 10:28:15 +02:00
|
|
|
* @subpackage tests
|
2009-08-11 01:34:32 +02:00
|
|
|
*/
|
2013-06-08 10:28:15 +02:00
|
|
|
class SearchFilterApplyRelationTest extends SapphireTest {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'SearchFilterApplyRelationTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'SearchFilterApplyRelationTest_DO',
|
|
|
|
'SearchFilterApplyRelationTest_HasOneParent',
|
|
|
|
'SearchFilterApplyRelationTest_HasOneChild',
|
|
|
|
'SearchFilterApplyRelationTest_HasOneGrantChild',
|
|
|
|
'SearchFilterApplyRelationTest_HasManyParent',
|
|
|
|
'SearchFilterApplyRelationTest_HasManyChild',
|
|
|
|
'SearchFilterApplyRelationTest_HasManyGrantChild',
|
|
|
|
'SearchFilterApplyRelationTest_ManyManyParent',
|
|
|
|
'SearchFilterApplyRelationTest_ManyManyChild',
|
|
|
|
'SearchFilterApplyRelationTest_ManyManyGrantChild',
|
2014-08-15 08:53:05 +02:00
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testApplyRelationHasOne(){
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
$all = singleton("SearchFilterApplyRelationTest_DO");
|
|
|
|
$context = $all->getDefaultSearchContext();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
$filter = new ExactMatchFilter("SearchFilterApplyRelationTest_HasOneGrantChild.Title");
|
|
|
|
$context->setFilters(null);
|
|
|
|
$context->addFilter($filter);
|
|
|
|
$params = array(
|
|
|
|
"Title" => "I am has_one object",
|
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(2, $results->count());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testApplyRelationHasMany(){
|
2009-10-02 00:20:04 +02:00
|
|
|
$do1 = $this->objFromFixture('SearchFilterApplyRelationTest_DO', 'do1');
|
|
|
|
$do2 = $this->objFromFixture('SearchFilterApplyRelationTest_DO', 'do2');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
$all = singleton("SearchFilterApplyRelationTest_DO");
|
|
|
|
$context = $all->getDefaultSearchContext();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
$filter = new PartialMatchFilter("SearchFilterApplyRelationTest_HasManyGrantChildren.Title");
|
|
|
|
$context->setFilters(null);
|
|
|
|
$context->addFilter($filter);
|
|
|
|
$params = array(
|
2009-09-16 00:00:08 +02:00
|
|
|
"SearchFilterApplyRelationTest_HasManyGrantChildren__Title" => "I am has_many object1",
|
2009-08-11 01:34:32 +02:00
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(1, $results->count());
|
2009-10-02 00:20:04 +02:00
|
|
|
$this->assertEquals(array($do1->ID), $results->column('ID'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
$params = array(
|
2009-09-16 00:00:08 +02:00
|
|
|
"SearchFilterApplyRelationTest_HasManyGrantChildren__Title" => "I am has_many object3",
|
2009-08-11 01:34:32 +02:00
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(1, $results->count());
|
2009-10-02 00:20:04 +02:00
|
|
|
$this->assertEquals(array($do2->ID), $results->column('ID'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
$params = array(
|
2009-09-16 00:00:08 +02:00
|
|
|
"SearchFilterApplyRelationTest_HasManyGrantChildren__Title" => "I am has_many object",
|
2009-08-11 01:34:32 +02:00
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(2, $results->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
$params = array(
|
2009-09-16 00:00:08 +02:00
|
|
|
"SearchFilterApplyRelationTest_HasManyGrantChildren__Title" => "not exist",
|
2009-08-11 01:34:32 +02:00
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(0, $results->count());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testApplyRelationManyMany(){
|
2009-08-11 01:34:32 +02:00
|
|
|
$all = singleton("SearchFilterApplyRelationTest_DO");
|
|
|
|
$context = $all->getDefaultSearchContext();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 02:11:17 +02:00
|
|
|
$filter = new PartialMatchFilter("ManyManyGrantChildren.Title");
|
2009-08-11 01:34:32 +02:00
|
|
|
$context->setFilters(null);
|
|
|
|
$context->addFilter($filter);
|
|
|
|
$params = array(
|
2009-09-16 00:00:08 +02:00
|
|
|
"ManyManyGrantChildren__Title" => "I am many_many object1",
|
2009-08-11 01:34:32 +02:00
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(2, $results->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 01:34:32 +02:00
|
|
|
$params = array(
|
2009-09-16 00:00:08 +02:00
|
|
|
"ManyManyGrantChildren__Title" => "I am many_many object2",
|
2009-08-11 01:34:32 +02:00
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(2, $results->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 02:11:17 +02:00
|
|
|
$params = array(
|
2009-09-16 00:00:08 +02:00
|
|
|
"ManyManyGrantChildren__Title" => "I am many_many object",
|
2009-08-11 02:11:17 +02:00
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(2, $results->count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 02:11:17 +02:00
|
|
|
$params = array(
|
2009-09-16 00:00:08 +02:00
|
|
|
"ManyManyGrantChildren__Title" => "not exist",
|
2009-08-11 02:11:17 +02:00
|
|
|
);
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(0, $results->count());
|
2009-08-11 01:34:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2010-04-12 04:03:16 +02:00
|
|
|
class SearchFilterApplyRelationTest_DO extends DataObject implements TestOnly {
|
2013-06-08 10:28:15 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
'SearchFilterApplyRelationTest_HasOneGrantChild' => 'SearchFilterApplyRelationTest_HasOneGrantChild'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
'SearchFilterApplyRelationTest_HasManyGrantChildren' => 'SearchFilterApplyRelationTest_HasManyGrantChild'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2009-08-11 02:11:17 +02:00
|
|
|
'ManyManyGrantChildren' => 'SearchFilterApplyRelationTest_ManyManyGrantChild'
|
2009-08-11 01:34:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2010-04-12 04:03:16 +02:00
|
|
|
class SearchFilterApplyRelationTest_HasOneParent extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"Title" => "Varchar"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
class SearchFilterApplyRelationTest_HasOneChild extends SearchFilterApplyRelationTest_HasOneParent
|
|
|
|
implements TestOnly {
|
2009-08-11 01:34:32 +02:00
|
|
|
// This is to create an seperate Table only.
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"ChildField" => "Varchar"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
class SearchFilterApplyRelationTest_HasOneGrantChild extends SearchFilterApplyRelationTest_HasOneChild
|
|
|
|
implements TestOnly {
|
2009-08-11 01:34:32 +02:00
|
|
|
// This is to create an seperate Table only.
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"GrantChildField" => "Varchar"
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"SearchFilterApplyRelationTest_DOs" => "SearchFilterApplyRelationTest_DO"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2010-04-12 04:03:16 +02:00
|
|
|
class SearchFilterApplyRelationTest_HasManyParent extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"Title" => "Varchar"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
class SearchFilterApplyRelationTest_HasManyChild extends SearchFilterApplyRelationTest_HasManyParent
|
|
|
|
implements TestOnly {
|
2012-04-20 00:08:17 +02:00
|
|
|
// This is to create an separate Table only.
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"ChildField" => "Varchar"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-08-11 01:34:32 +02:00
|
|
|
class SearchFilterApplyRelationTest_HasManyGrantChild extends SearchFilterApplyRelationTest_HasManyChild{
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"SearchFilterApplyRelationTest_DO" => "SearchFilterApplyRelationTest_DO"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-08-11 01:34:32 +02:00
|
|
|
class SearchFilterApplyRelationTest_ManyManyParent extends DataObject implements TestOnly{
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"Title" => "Varchar"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
class SearchFilterApplyRelationTest_ManyManyChild extends SearchFilterApplyRelationTest_ManyManyParent
|
|
|
|
implements TestOnly {
|
2009-08-11 01:34:32 +02:00
|
|
|
// This is to create an seperate Table only.
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"ChildField" => "Varchar"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-06-08 10:28:15 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
class SearchFilterApplyRelationTest_ManyManyGrantChild extends SearchFilterApplyRelationTest_ManyManyChild
|
|
|
|
implements TestOnly {
|
2009-08-11 01:34:32 +02:00
|
|
|
// This is to create an seperate Table only.
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-08-11 01:34:32 +02:00
|
|
|
"GrantChildField" => "Varchar"
|
|
|
|
);
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $belongs_many_many = array(
|
2009-08-11 02:11:17 +02:00
|
|
|
"DOs" => "SearchFilterApplyRelationTest_DO"
|
2009-08-11 01:34:32 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|