silverstripe-framework/tests/search/SearchFilterApplyRelationTest.php

237 lines
6.7 KiB
PHP
Raw Normal View History

<?php
2013-06-08 10:28:15 +02:00
use SilverStripe\ORM\DataObject;
/**
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
*
* @package framework
2013-06-08 10:28:15 +02:00
* @subpackage tests
*/
2013-06-08 10:28:15 +02:00
class SearchFilterApplyRelationTest extends SapphireTest {
protected static $fixture_file = 'SearchFilterApplyRelationTest.yml';
2014-08-15 08:53:05 +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
);
public function testApplyRelationHasOne(){
2014-08-15 08:53:05 +02:00
$all = singleton("SearchFilterApplyRelationTest_DO");
$context = $all->getDefaultSearchContext();
2014-08-15 08:53:05 +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
public function testApplyRelationHasMany(){
$do1 = $this->objFromFixture('SearchFilterApplyRelationTest_DO', 'do1');
$do2 = $this->objFromFixture('SearchFilterApplyRelationTest_DO', 'do2');
2014-08-15 08:53:05 +02:00
$all = singleton("SearchFilterApplyRelationTest_DO");
$context = $all->getDefaultSearchContext();
2014-08-15 08:53:05 +02:00
$filter = new PartialMatchFilter("SearchFilterApplyRelationTest_HasManyGrantChildren.Title");
$context->setFilters(null);
$context->addFilter($filter);
$params = array(
"SearchFilterApplyRelationTest_HasManyGrantChildren__Title" => "I am has_many object1",
);
$results = $context->getResults($params);
$this->assertEquals(1, $results->count());
$this->assertEquals(array($do1->ID), $results->column('ID'));
2014-08-15 08:53:05 +02:00
$params = array(
"SearchFilterApplyRelationTest_HasManyGrantChildren__Title" => "I am has_many object3",
);
$results = $context->getResults($params);
$this->assertEquals(1, $results->count());
$this->assertEquals(array($do2->ID), $results->column('ID'));
2014-08-15 08:53:05 +02:00
$params = array(
"SearchFilterApplyRelationTest_HasManyGrantChildren__Title" => "I am has_many object",
);
$results = $context->getResults($params);
$this->assertEquals(2, $results->count());
2014-08-15 08:53:05 +02:00
$params = array(
"SearchFilterApplyRelationTest_HasManyGrantChildren__Title" => "not exist",
);
$results = $context->getResults($params);
$this->assertEquals(0, $results->count());
}
2014-08-15 08:53:05 +02:00
public function testApplyRelationManyMany(){
$all = singleton("SearchFilterApplyRelationTest_DO");
$context = $all->getDefaultSearchContext();
2014-08-15 08:53:05 +02:00
$filter = new PartialMatchFilter("ManyManyGrantChildren.Title");
$context->setFilters(null);
$context->addFilter($filter);
$params = array(
"ManyManyGrantChildren__Title" => "I am many_many object1",
);
$results = $context->getResults($params);
$this->assertEquals(2, $results->count());
2014-08-15 08:53:05 +02:00
$params = array(
"ManyManyGrantChildren__Title" => "I am many_many object2",
);
$results = $context->getResults($params);
$this->assertEquals(2, $results->count());
2014-08-15 08:53:05 +02:00
$params = array(
"ManyManyGrantChildren__Title" => "I am many_many object",
);
$results = $context->getResults($params);
$this->assertEquals(2, $results->count());
2014-08-15 08:53:05 +02:00
$params = array(
"ManyManyGrantChildren__Title" => "not exist",
);
$results = $context->getResults($params);
$this->assertEquals(0, $results->count());
}
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_DO extends DataObject implements TestOnly {
2013-06-08 10:28:15 +02:00
private static $has_one = array(
'SearchFilterApplyRelationTest_HasOneGrantChild' => 'SearchFilterApplyRelationTest_HasOneGrantChild'
);
2014-08-15 08:53:05 +02:00
private static $has_many = array(
'SearchFilterApplyRelationTest_HasManyGrantChildren' => 'SearchFilterApplyRelationTest_HasManyGrantChild'
);
2014-08-15 08:53:05 +02:00
private static $many_many = array(
'ManyManyGrantChildren' => 'SearchFilterApplyRelationTest_ManyManyGrantChild'
);
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_HasOneParent extends DataObject implements TestOnly {
private static $db = array(
"Title" => "Varchar"
);
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_HasOneChild extends SearchFilterApplyRelationTest_HasOneParent
implements TestOnly {
// This is to create an seperate Table only.
private static $db = array(
"ChildField" => "Varchar"
);
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_HasOneGrantChild extends SearchFilterApplyRelationTest_HasOneChild
implements TestOnly {
// This is to create an seperate Table only.
private static $db = array(
"GrantChildField" => "Varchar"
);
private static $has_many = array(
"SearchFilterApplyRelationTest_DOs" => "SearchFilterApplyRelationTest_DO"
);
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_HasManyParent extends DataObject implements TestOnly {
private static $db = array(
"Title" => "Varchar"
);
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_HasManyChild extends SearchFilterApplyRelationTest_HasManyParent
implements TestOnly {
// This is to create an separate Table only.
private static $db = array(
"ChildField" => "Varchar"
);
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_HasManyGrantChild extends SearchFilterApplyRelationTest_HasManyChild{
private static $has_one = array(
"SearchFilterApplyRelationTest_DO" => "SearchFilterApplyRelationTest_DO"
);
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_ManyManyParent extends DataObject implements TestOnly{
private static $db = array(
"Title" => "Varchar"
);
}
class SearchFilterApplyRelationTest_ManyManyChild extends SearchFilterApplyRelationTest_ManyManyParent
implements TestOnly {
// This is to create an seperate Table only.
private static $db = array(
"ChildField" => "Varchar"
);
}
2013-06-08 10:28:15 +02:00
/**
* @package framework
* @subpackage tests
*/
class SearchFilterApplyRelationTest_ManyManyGrantChild extends SearchFilterApplyRelationTest_ManyManyChild
implements TestOnly {
// This is to create an seperate Table only.
private static $db = array(
"GrantChildField" => "Varchar"
);
private static $belongs_many_many = array(
"DOs" => "SearchFilterApplyRelationTest_DO"
);
}