silverstripe-framework/tests/SearchContextTest.php
Ingo Schommer 8d0166e298 (merged from branches/roa. use "svn log -c <changeset> -g <module-svn-path>" for detailed commit message)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@60265 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-08-10 23:29:30 +00:00

248 lines
6.2 KiB
PHP

<?php
class SearchContextTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/SearchContextTest.yml';
function testResultSetFilterReturnsExpectedCount() {
$person = singleton('SearchContextTest_Person');
$context = $person->getDefaultSearchContext();
$results = $context->getResults(array('Name'=>''));
$this->assertEquals(5, $results->Count());
$results = $context->getResults(array('EyeColor'=>'green'));
$this->assertEquals(2, $results->Count());
$results = $context->getResults(array('EyeColor'=>'green', 'HairColor'=>'black'));
$this->assertEquals(1, $results->Count());
}
function testSummaryIncludesDefaultFieldsIfNotDefined() {
$person = singleton('SearchContextTest_Person');
$this->assertContains('Name', $person->summaryFields());
$book = singleton('SearchContextTest_Book');
$this->assertContains('Title', $book->summaryFields());
}
function testAccessDefinedSummaryFields() {
$company = singleton('SearchContextTest_Company');
$this->assertContains('Industry', $company->summaryFields());
}
function testPartialMatchUsedByDefaultWhenNotExplicitlySet() {
$person = singleton('SearchContextTest_Person');
$context = $person->getDefaultSearchContext();
$this->assertEquals(
array(
"Name" => new PartialMatchFilter("Name"),
"HairColor" => new PartialMatchFilter("HairColor"),
"EyeColor" => new PartialMatchFilter("EyeColor")
),
$context->getFilters()
);
}
function testDefaultFiltersDefinedWhenNotSetInDataObject() {
$book = singleton('SearchContextTest_Book');
$context = $book->getDefaultSearchContext();
$this->assertEquals(
array(
"Title" => new PartialMatchFilter("Title")
),
$context->getFilters()
);
}
function testUserDefinedFiltersAppearInSearchContext() {
$company = singleton('SearchContextTest_Company');
$context = $company->getDefaultSearchContext();
$this->assertEquals(
array(
"Name" => new PartialMatchFilter("Name"),
"Industry" => new PartialMatchFilter("Industry"),
"AnnualProfit" => new PartialMatchFilter("AnnualProfit")
),
$context->getFilters()
);
}
function testUserDefinedFieldsAppearInSearchContext() {
$company = singleton('SearchContextTest_Company');
$context = $company->getDefaultSearchContext();
$fields = $context->getFields();
$this->assertEquals(
new FieldSet(
new TextField("Name", 'Name'),
new TextareaField("Industry", 'Industry'),
new NumericField("AnnualProfit", 'The Almighty Annual Profit')
),
$context->getFields()
);
}
function testRelationshipObjectsLinkedInSearch() {
$project = singleton('SearchContextTest_Project');
$context = $project->getDefaultSearchContext();
$params = array("Name"=>"Blog Website", "Actions__SolutionArea"=>"technical");
$results = $context->getResults($params);
$this->assertEquals(1, $results->Count());
$project = $results->First();
$this->assertType('SearchContextTest_Project', $project);
$this->assertEquals("Blog Website", $project->Name);
$this->assertEquals(2, $project->Actions()->Count());
$this->assertEquals("Get RSS feeds working", $project->Actions()->First()->Description);
}
function testCanGenerateQueryUsingAllFilterTypes() {
$all = singleton("SearchContextTest_AllFilterTypes");
$context = $all->getDefaultSearchContext();
$params = array(
"ExactMatch" => "Match Me Exactly",
"PartialMatch" => "partially",
"Negation" => "undisclosed",
"CollectionMatch" => "ExistingCollectionValue,NonExistingCollectionValue,4,Inline'Quotes'",
"StartsWith" => "12345",
"EndsWith" => "ijkl"
);
$results = $context->getResults($params);
$this->assertEquals(1, $results->Count());
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
}
}
class SearchContextTest_Person extends DataObject implements TestOnly {
static $db = array(
"Name" => "Text",
"Email" => "Text",
"HairColor" => "Text",
"EyeColor" => "Text"
);
static $searchable_fields = array(
"Name", "HairColor", "EyeColor"
);
}
class SearchContextTest_Book extends DataObject implements TestOnly {
static $db = array(
"Title" => "Text",
"Summary" => "Text"
);
}
class SearchContextTest_Company extends DataObject implements TestOnly {
static $db = array(
"Name" => "Text",
"Industry" => "Text",
"AnnualProfit" => "Int"
);
static $summary_fields = array(
"Industry"
);
static $searchable_fields = array(
"Name" => "PartialMatchFilter",
"Industry" => array(
'field' => "TextareaField"
),
"AnnualProfit" => array(
'field' => "NumericField",
'filter' => "PartialMatchFilter",
'title' => 'The Almighty Annual Profit'
)
);
}
class SearchContextTest_Project extends DataObject implements TestOnly {
static $db = array(
"Name" => "Text"
);
static $has_one = array(
"Deadline" => "SearchContextTest_Deadline"
);
static $has_many = array(
"Actions" => "SearchContextTest_Action"
);
static $searchable_fields = array(
"Name" => "PartialMatchFilter",
"Actions.SolutionArea" => "ExactMatchFilter",
"Actions.Description" => "PartialMatchFilter"
);
}
class SearchContextTest_Deadline extends DataObject implements TestOnly {
static $db = array(
"CompletionDate" => "Datetime"
);
static $has_one = array(
"Project" => "SearchContextTest_Project"
);
}
class SearchContextTest_Action extends DataObject implements TestOnly {
static $db = array(
"Description" => "Text",
"SolutionArea" => "Text"
);
static $has_one = array(
"Project" => "SearchContextTest_Project"
);
}
class SearchContextTest_AllFilterTypes extends DataObject implements TestOnly {
static $db = array(
"ExactMatch" => "Text",
"PartialMatch" => "Text",
"Negation" => "Text",
"SubstringMatch" => "Text",
"CollectionMatch" => "Text",
"StartsWith" => "Text",
"EndsWith" => "Text",
"HiddenValue" => "Text"
);
static $searchable_fields = array(
"ExactMatch" => "ExactMatchFilter",
"PartialMatch" => "PartialMatchFilter",
"Negation" => "NegationFilter",
"SubstringMatch" => "SubstringFilter",
"CollectionMatch" => "ExactMatchMultiFilter",
"StartsWith" => "StartsWithFilter",
"EndsWith" => "EndsWithFilter"
);
}
?>