2008-08-09 05:54:55 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
class SearchContextTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'SearchContextTest.yml';
|
2008-08-11 01:29:30 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'SearchContextTest_Person',
|
|
|
|
'SearchContextTest_Book',
|
|
|
|
'SearchContextTest_Company',
|
|
|
|
'SearchContextTest_Project',
|
|
|
|
'SearchContextTest_Deadline',
|
|
|
|
'SearchContextTest_Action',
|
|
|
|
'SearchContextTest_AllFilterTypes',
|
|
|
|
);
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSummaryIncludesDefaultFieldsIfNotDefined() {
|
2008-08-09 06:06:52 +02:00
|
|
|
$person = singleton('SearchContextTest_Person');
|
2008-08-09 08:29:50 +02:00
|
|
|
$this->assertContains('Name', $person->summaryFields());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testAccessDefinedSummaryFields() {
|
2008-08-09 06:06:52 +02:00
|
|
|
$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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPartialMatchUsedByDefaultWhenNotExplicitlySet() {
|
2012-12-08 12:20:20 +01:00
|
|
|
$person = singleton('SearchContextTest_Person');
|
|
|
|
$context = $person->getDefaultSearchContext();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
"Name" => new PartialMatchFilter("Name"),
|
|
|
|
"HairColor" => new PartialMatchFilter("HairColor"),
|
|
|
|
"EyeColor" => new PartialMatchFilter("EyeColor")
|
|
|
|
),
|
|
|
|
$context->getFilters()
|
|
|
|
);
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testDefaultFiltersDefinedWhenNotSetInDataObject() {
|
2008-08-09 06:06:52 +02:00
|
|
|
$book = singleton('SearchContextTest_Book');
|
|
|
|
$context = $book->getDefaultSearchContext();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
"Title" => new PartialMatchFilter("Title")
|
|
|
|
),
|
|
|
|
$context->getFilters()
|
|
|
|
);
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUserDefinedFiltersAppearInSearchContext() {
|
2008-08-09 06:38:44 +02:00
|
|
|
$company = singleton('SearchContextTest_Company');
|
|
|
|
$context = $company->getDefaultSearchContext();
|
2012-12-08 12:20:20 +01:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$this->assertEquals(
|
2008-08-09 06:06:52 +02:00
|
|
|
array(
|
|
|
|
"Name" => new PartialMatchFilter("Name"),
|
2012-12-08 12:20:20 +01:00
|
|
|
"Industry" => new PartialMatchFilter("Industry"),
|
|
|
|
"AnnualProfit" => new PartialMatchFilter("AnnualProfit")
|
2008-08-09 06:06:52 +02:00
|
|
|
),
|
|
|
|
$context->getFilters()
|
2008-08-09 06:38:44 +02:00
|
|
|
);
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testUserDefinedFieldsAppearInSearchContext() {
|
2008-08-11 01:29:30 +02:00
|
|
|
$company = singleton('SearchContextTest_Company');
|
|
|
|
$context = $company->getDefaultSearchContext();
|
|
|
|
$fields = $context->getFields();
|
|
|
|
$this->assertEquals(
|
2011-05-11 09:51:54 +02:00
|
|
|
new FieldList(
|
2008-11-02 22:24:56 +01:00
|
|
|
new TextField("Name", 'Name'),
|
2012-12-08 12:20:20 +01:00
|
|
|
new TextareaField("Industry", 'Industry'),
|
|
|
|
new NumericField("AnnualProfit", 'The Almighty Annual Profit')
|
2008-08-11 01:29:30 +02:00
|
|
|
),
|
|
|
|
$context->getFields()
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRelationshipObjectsLinkedInSearch() {
|
2009-10-02 02:05:19 +02:00
|
|
|
$action3 = $this->objFromFixture('SearchContextTest_Action', 'action3');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:38:44 +02:00
|
|
|
$project = singleton('SearchContextTest_Project');
|
|
|
|
$context = $project->getDefaultSearchContext();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
$params = array("Name"=>"Blog Website", "Actions__SolutionArea"=>"technical");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
$results = $context->getResults($params);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
$this->assertEquals(1, $results->Count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:53:34 +02:00
|
|
|
$project = $results->First();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 12:43:22 +02:00
|
|
|
$this->assertInstanceOf('SearchContextTest_Project', $project);
|
2008-08-09 06:53:34 +02:00
|
|
|
$this->assertEquals("Blog Website", $project->Name);
|
|
|
|
$this->assertEquals(2, $project->Actions()->Count());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-02 02:05:19 +02:00
|
|
|
$this->assertEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
"Get RSS feeds working",
|
2009-10-02 02:05:19 +02:00
|
|
|
$project->Actions()->find('ID', $action3->ID)->Description
|
|
|
|
);
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCanGenerateQueryUsingAllFilterTypes() {
|
2008-08-09 06:06:52 +02:00
|
|
|
$all = singleton("SearchContextTest_AllFilterTypes");
|
|
|
|
$context = $all->getDefaultSearchContext();
|
|
|
|
$params = array(
|
2009-10-02 02:05:19 +02:00
|
|
|
"ExactMatch" => "Match me exactly",
|
2008-08-09 06:06:52 +02:00
|
|
|
"PartialMatch" => "partially",
|
2013-03-19 12:54:25 +01:00
|
|
|
"CollectionMatch" => array("ExistingCollectionValue","NonExistingCollectionValue",4,"Inline'Quotes'"),
|
2008-08-09 08:40:50 +02:00
|
|
|
"StartsWith" => "12345",
|
2009-05-24 23:27:48 +02:00
|
|
|
"EndsWith" => "ijkl",
|
|
|
|
"Fulltext" => "two"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2009-10-02 02:05:19 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(1, $results->Count());
|
|
|
|
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
|
|
|
|
}
|
2012-03-14 16:05:28 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testStartsWithFilterCaseInsensitive() {
|
2012-03-14 16:05:28 +01:00
|
|
|
$all = singleton("SearchContextTest_AllFilterTypes");
|
|
|
|
$context = $all->getDefaultSearchContext();
|
|
|
|
$params = array(
|
|
|
|
"StartsWith" => "12345-6789 camelcase", // spelled lowercase
|
|
|
|
);
|
|
|
|
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(1, $results->Count());
|
|
|
|
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testEndsWithFilterCaseInsensitive() {
|
2012-03-14 16:05:28 +01:00
|
|
|
$all = singleton("SearchContextTest_AllFilterTypes");
|
|
|
|
$context = $all->getDefaultSearchContext();
|
|
|
|
$params = array(
|
|
|
|
"EndsWith" => "IJKL", // spelled uppercase
|
|
|
|
);
|
|
|
|
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(1, $results->Count());
|
|
|
|
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
class SearchContextTest_Person extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-05-07 08:00:50 +02:00
|
|
|
"Name" => "Varchar",
|
|
|
|
"Email" => "Varchar",
|
|
|
|
"HairColor" => "Varchar",
|
|
|
|
"EyeColor" => "Varchar"
|
2008-08-09 05:54:55 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $searchable_fields = array(
|
2008-08-09 06:06:52 +02:00
|
|
|
"Name", "HairColor", "EyeColor"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SearchContextTest_Book extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-05-07 08:00:50 +02:00
|
|
|
"Title" => "Varchar",
|
|
|
|
"Summary" => "Varchar"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SearchContextTest_Company extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-05-07 08:00:50 +02:00
|
|
|
"Name" => "Varchar",
|
|
|
|
"Industry" => "Varchar",
|
2008-08-09 06:06:52 +02:00
|
|
|
"AnnualProfit" => "Int"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $summary_fields = array(
|
2008-08-09 06:06:52 +02:00
|
|
|
"Industry"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $searchable_fields = array(
|
2008-08-09 06:06:52 +02:00
|
|
|
"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
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SearchContextTest_Project extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-05-07 08:00:50 +02:00
|
|
|
"Name" => "Varchar"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
"Deadline" => "SearchContextTest_Deadline"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2008-08-09 06:06:52 +02:00
|
|
|
"Actions" => "SearchContextTest_Action"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $searchable_fields = array(
|
2008-08-09 06:06:52 +02:00
|
|
|
"Name" => "PartialMatchFilter",
|
2008-08-09 06:53:34 +02:00
|
|
|
"Actions.SolutionArea" => "ExactMatchFilter",
|
|
|
|
"Actions.Description" => "PartialMatchFilter"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SearchContextTest_Deadline extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2016-06-15 06:03:16 +02:00
|
|
|
"CompletionDate" => "Datetime"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
"Project" => "SearchContextTest_Project"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SearchContextTest_Action extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2008-08-09 06:06:52 +02:00
|
|
|
"Description" => "Text",
|
2009-05-07 08:00:50 +02:00
|
|
|
"SolutionArea" => "Varchar"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2008-08-09 06:53:34 +02:00
|
|
|
"Project" => "SearchContextTest_Project"
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SearchContextTest_AllFilterTypes extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2009-05-07 08:00:50 +02:00
|
|
|
"ExactMatch" => "Varchar",
|
|
|
|
"PartialMatch" => "Varchar",
|
|
|
|
"SubstringMatch" => "Varchar",
|
|
|
|
"CollectionMatch" => "Varchar",
|
|
|
|
"StartsWith" => "Varchar",
|
|
|
|
"EndsWith" => "Varchar",
|
2009-05-24 23:27:48 +02:00
|
|
|
"HiddenValue" => "Varchar",
|
2014-08-15 08:53:05 +02:00
|
|
|
'FulltextField' => 'Text',
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $searchable_fields = array(
|
2008-08-09 06:06:52 +02:00
|
|
|
"ExactMatch" => "ExactMatchFilter",
|
|
|
|
"PartialMatch" => "PartialMatchFilter",
|
2013-03-19 12:54:25 +01:00
|
|
|
"CollectionMatch" => "ExactMatchFilter",
|
2008-08-09 08:40:50 +02:00
|
|
|
"StartsWith" => "StartsWithFilter",
|
2009-05-24 23:27:48 +02:00
|
|
|
"EndsWith" => "EndsWithFilter",
|
|
|
|
"FulltextField" => "FulltextFilter",
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|