2008-08-09 05:54:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class SearchContextTest extends SapphireTest {
|
|
|
|
|
2008-08-11 01:29:30 +02:00
|
|
|
static $fixture_file = 'sapphire/tests/SearchContextTest.yml';
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
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');
|
2008-08-09 08:29:50 +02:00
|
|
|
$this->assertContains('Name', $person->summaryFields());
|
2008-08-09 06:06:52 +02:00
|
|
|
|
|
|
|
$book = singleton('SearchContextTest_Book');
|
2008-08-09 08:29:50 +02:00
|
|
|
$this->assertContains('Title', $book->summaryFields());
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testAccessDefinedSummaryFields() {
|
|
|
|
$company = singleton('SearchContextTest_Company');
|
2008-08-09 08:29:50 +02:00
|
|
|
$this->assertContains('Industry', $company->summaryFields());
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 07:57:44 +02:00
|
|
|
function testPartialMatchUsedByDefaultWhenNotExplicitlySet() {
|
2008-08-09 06:06:52 +02:00
|
|
|
$person = singleton('SearchContextTest_Person');
|
|
|
|
$context = $person->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2008-08-09 07:57:44 +02:00
|
|
|
"Name" => new PartialMatchFilter("Name"),
|
|
|
|
"HairColor" => new PartialMatchFilter("HairColor"),
|
|
|
|
"EyeColor" => new PartialMatchFilter("EyeColor")
|
2008-08-09 06:06:52 +02:00
|
|
|
),
|
|
|
|
$context->getFilters()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDefaultFiltersDefinedWhenNotSetInDataObject() {
|
|
|
|
$book = singleton('SearchContextTest_Book');
|
|
|
|
$context = $book->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2008-08-09 07:57:44 +02:00
|
|
|
"Title" => new PartialMatchFilter("Title")
|
2008-08-09 06:06:52 +02:00
|
|
|
),
|
|
|
|
$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"),
|
2008-08-09 07:57:44 +02:00
|
|
|
"Industry" => new PartialMatchFilter("Industry"),
|
2008-08-09 06:06:52 +02:00
|
|
|
"AnnualProfit" => new PartialMatchFilter("AnnualProfit")
|
|
|
|
),
|
|
|
|
$context->getFilters()
|
2008-08-09 06:38:44 +02:00
|
|
|
);
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
2008-08-11 01:29:30 +02:00
|
|
|
|
|
|
|
function testUserDefinedFieldsAppearInSearchContext() {
|
|
|
|
$company = singleton('SearchContextTest_Company');
|
|
|
|
$context = $company->getDefaultSearchContext();
|
|
|
|
$fields = $context->getFields();
|
|
|
|
$this->assertEquals(
|
|
|
|
new FieldSet(
|
2008-10-30 23:02:20 +01:00
|
|
|
new TextareaField("Name", 'Name'),
|
2008-08-11 01:29:30 +02:00
|
|
|
new TextareaField("Industry", 'Industry'),
|
|
|
|
new NumericField("AnnualProfit", 'The Almighty Annual Profit')
|
|
|
|
),
|
|
|
|
$context->getFields()
|
|
|
|
);
|
|
|
|
}
|
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:53:34 +02:00
|
|
|
$params = array("Name"=>"Blog Website", "Actions__SolutionArea"=>"technical");
|
2008-08-09 06:06:52 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
$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);
|
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",
|
2008-08-09 08:18:32 +02:00
|
|
|
"Negation" => "undisclosed",
|
|
|
|
"CollectionMatch" => "ExistingCollectionValue,NonExistingCollectionValue,4,Inline'Quotes'",
|
2008-08-09 08:40:50 +02:00
|
|
|
"StartsWith" => "12345",
|
|
|
|
"EndsWith" => "ijkl"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$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",
|
2008-08-11 01:29:30 +02:00
|
|
|
"Industry" => array(
|
|
|
|
'field' => "TextareaField"
|
|
|
|
),
|
|
|
|
"AnnualProfit" => array(
|
|
|
|
'field' => "NumericField",
|
|
|
|
'filter' => "PartialMatchFilter",
|
|
|
|
'title' => 'The Almighty Annual Profit'
|
|
|
|
)
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2008-08-09 06:53:34 +02:00
|
|
|
"Actions.SolutionArea" => "ExactMatchFilter",
|
|
|
|
"Actions.Description" => "PartialMatchFilter"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchContextTest_Deadline extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
static $db = array(
|
2008-10-16 10:59:40 +02:00
|
|
|
"CompletionDate" => "SSDatetime"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
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(
|
2008-08-09 06:53:34 +02:00
|
|
|
"Project" => "SearchContextTest_Project"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2008-08-09 08:18:32 +02:00
|
|
|
"CollectionMatch" => "Text",
|
2008-08-09 08:40:50 +02:00
|
|
|
"StartsWith" => "Text",
|
|
|
|
"EndsWith" => "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",
|
2008-08-09 08:18:32 +02:00
|
|
|
"SubstringMatch" => "SubstringFilter",
|
2008-08-11 01:29:30 +02:00
|
|
|
"CollectionMatch" => "ExactMatchMultiFilter",
|
2008-08-09 08:40:50 +02:00
|
|
|
"StartsWith" => "StartsWithFilter",
|
|
|
|
"EndsWith" => "EndsWithFilter"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
2008-10-30 23:02:20 +01:00
|
|
|
?>
|