2008-08-09 05:54:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class SearchContextTest extends SapphireTest {
|
|
|
|
static $fixture_file = 'sapphire/tests/SearchContextTest.yml';
|
|
|
|
|
|
|
|
function testResultSetFilterReturnsExpectedCount() {
|
2008-08-09 06:06:52 +02:00
|
|
|
$person = singleton('SearchContextTest_Person');
|
2008-08-09 05:54:55 +02:00
|
|
|
$context = $person->getDefaultSearchContext();
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$results = $context->getResults(array('Name'=>''));
|
2008-08-09 05:54:55 +02:00
|
|
|
$this->assertEquals(5, $results->Count());
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$results = $context->getResults(array('EyeColor'=>'green'));
|
2008-08-09 05:54:55 +02:00
|
|
|
$this->assertEquals(2, $results->Count());
|
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$results = $context->getResults(array('EyeColor'=>'green', 'HairColor'=>'black'));
|
2008-08-09 05:54:55 +02:00
|
|
|
$this->assertEquals(1, $results->Count());
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSummaryIncludesDefaultFieldsIfNotDefined() {
|
|
|
|
$person = singleton('SearchContextTest_Person');
|
|
|
|
$this->assertContains('Name', $person->summary_fields());
|
|
|
|
|
|
|
|
$book = singleton('SearchContextTest_Book');
|
|
|
|
$this->assertContains('Title', $book->summary_fields());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testAccessDefinedSummaryFields() {
|
|
|
|
$company = singleton('SearchContextTest_Company');
|
|
|
|
$this->assertContains('Industry', $company->summary_fields());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testExactMatchUsedByDefaultWhenNotExplicitlySet() {
|
|
|
|
$person = singleton('SearchContextTest_Person');
|
|
|
|
$context = $person->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
"Name" => new ExactMatchFilter("Name"),
|
|
|
|
"HairColor" => new ExactMatchFilter("HairColor"),
|
|
|
|
"EyeColor" => new ExactMatchFilter("EyeColor")
|
|
|
|
),
|
|
|
|
$context->getFilters()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDefaultFiltersDefinedWhenNotSetInDataObject() {
|
|
|
|
$book = singleton('SearchContextTest_Book');
|
|
|
|
$context = $book->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
"Title" => new ExactMatchFilter("Title")
|
|
|
|
),
|
|
|
|
$context->getFilters()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testUserDefinedFiltersAppearInSearchContext() {
|
2008-08-09 06:38:44 +02:00
|
|
|
$company = singleton('SearchContextTest_Company');
|
|
|
|
$context = $company->getDefaultSearchContext();
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$this->assertEquals(
|
2008-08-09 06:06:52 +02:00
|
|
|
array(
|
|
|
|
"Name" => new PartialMatchFilter("Name"),
|
|
|
|
"Industry" => new ExactMatchFilter("Industry"),
|
|
|
|
"AnnualProfit" => new PartialMatchFilter("AnnualProfit")
|
|
|
|
),
|
|
|
|
$context->getFilters()
|
2008-08-09 06:38:44 +02:00
|
|
|
);
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
function testRelationshipObjectsLinkedInSearch() {
|
2008-08-09 06:38:44 +02:00
|
|
|
$project = singleton('SearchContextTest_Project');
|
|
|
|
$context = $project->getDefaultSearchContext();
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$query = array("Name"=>"Blog Website");
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$results = $context->getQuery($query);
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testCanGenerateQueryUsingAllFilterTypes() {
|
|
|
|
$all = singleton("SearchContextTest_AllFilterTypes");
|
|
|
|
$context = $all->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
"ExactMatch" => "Match Me Exactly",
|
|
|
|
"PartialMatch" => "partially",
|
|
|
|
"Negation" => "undisclosed"
|
|
|
|
);
|
|
|
|
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $results->Count());
|
|
|
|
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
|
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
class SearchContextTest_Person extends DataObject implements TestOnly {
|
2008-08-09 05:54:55 +02:00
|
|
|
|
|
|
|
static $db = array(
|
|
|
|
"Name" => "Text",
|
|
|
|
"Email" => "Text",
|
|
|
|
"HairColor" => "Text",
|
|
|
|
"EyeColor" => "Text"
|
|
|
|
);
|
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
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" => "TextareaField",
|
|
|
|
"AnnualProfit" => array("NumericField" => "PartialMatchFilter")
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2008-08-09 06:38:44 +02:00
|
|
|
"SubstringMatch" => "Text",
|
|
|
|
"HiddenValue" => "Text",
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $searchable_fields = array(
|
|
|
|
"ExactMatch" => "ExactMatchFilter",
|
|
|
|
"PartialMatch" => "PartialMatchFilter",
|
2008-08-09 06:38:44 +02:00
|
|
|
"Negation" => "NegationFilter",
|
|
|
|
"SubstringMatch" => "SubstringFilter"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|