2008-08-09 05:54:55 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests\Search;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\TextareaField;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
2017-07-26 06:35:10 +02:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-07-27 02:05:27 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\Filters\PartialMatchFilter;
|
2017-07-26 06:35:10 +02:00
|
|
|
use SilverStripe\ORM\Search\SearchContext;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class SearchContextTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected static $fixture_file = 'SearchContextTest.yml';
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
SearchContextTest\Person::class,
|
|
|
|
SearchContextTest\Book::class,
|
|
|
|
SearchContextTest\Company::class,
|
|
|
|
SearchContextTest\Project::class,
|
|
|
|
SearchContextTest\Deadline::class,
|
|
|
|
SearchContextTest\Action::class,
|
|
|
|
SearchContextTest\AllFilterTypes::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function testResultSetFilterReturnsExpectedCount()
|
|
|
|
{
|
|
|
|
$person = SearchContextTest\Person::singleton();
|
|
|
|
$context = $person->getDefaultSearchContext();
|
2020-04-20 19:58:09 +02:00
|
|
|
$results = $context->getResults(['Name' => '']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(5, $results->Count());
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$results = $context->getResults(['EyeColor' => 'green']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(2, $results->Count());
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$results = $context->getResults(['EyeColor' => 'green', 'HairColor' => 'black']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, $results->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSummaryIncludesDefaultFieldsIfNotDefined()
|
|
|
|
{
|
|
|
|
$person = SearchContextTest\Person::singleton();
|
|
|
|
$this->assertContains('Name', $person->summaryFields());
|
|
|
|
|
|
|
|
$book = SearchContextTest\Book::singleton();
|
|
|
|
$this->assertContains('Title', $book->summaryFields());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessDefinedSummaryFields()
|
|
|
|
{
|
|
|
|
$company = SearchContextTest\Company::singleton();
|
|
|
|
$this->assertContains('Industry', $company->summaryFields());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPartialMatchUsedByDefaultWhenNotExplicitlySet()
|
|
|
|
{
|
|
|
|
$person = SearchContextTest\Person::singleton();
|
|
|
|
$context = $person->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
"Name" => new PartialMatchFilter("Name"),
|
|
|
|
"HairColor" => new PartialMatchFilter("HairColor"),
|
|
|
|
"EyeColor" => new PartialMatchFilter("EyeColor")
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$context->getFilters()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultFiltersDefinedWhenNotSetInDataObject()
|
|
|
|
{
|
|
|
|
$book = SearchContextTest\Book::singleton();
|
|
|
|
$context = $book->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
"Title" => new PartialMatchFilter("Title")
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$context->getFilters()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserDefinedFiltersAppearInSearchContext()
|
|
|
|
{
|
|
|
|
$company = SearchContextTest\Company::singleton();
|
|
|
|
$context = $company->getDefaultSearchContext();
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
"Name" => new PartialMatchFilter("Name"),
|
|
|
|
"Industry" => new PartialMatchFilter("Industry"),
|
|
|
|
"AnnualProfit" => new PartialMatchFilter("AnnualProfit")
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
$context->getFilters()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserDefinedFieldsAppearInSearchContext()
|
|
|
|
{
|
|
|
|
$company = SearchContextTest\Company::singleton();
|
|
|
|
$context = $company->getDefaultSearchContext();
|
|
|
|
$this->assertEquals(
|
|
|
|
new FieldList(
|
2017-10-24 22:30:04 +02:00
|
|
|
(new TextField("Name", 'Name'))
|
|
|
|
->setMaxLength(255),
|
2016-12-16 05:34:21 +01:00
|
|
|
new TextareaField("Industry", 'Industry'),
|
|
|
|
new NumericField("AnnualProfit", 'The Almighty Annual Profit')
|
|
|
|
),
|
|
|
|
$context->getFields()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRelationshipObjectsLinkedInSearch()
|
|
|
|
{
|
|
|
|
$action3 = $this->objFromFixture(SearchContextTest\Action::class, 'action3');
|
|
|
|
|
2017-07-27 02:05:27 +02:00
|
|
|
$project = SearchContextTest\Project::singleton();
|
2016-12-16 05:34:21 +01:00
|
|
|
$context = $project->getDefaultSearchContext();
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
$params = ["Name" => "Blog Website", "Actions__SolutionArea" => "technical"];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-07-27 02:05:27 +02:00
|
|
|
/** @var DataList $results */
|
2016-12-16 05:34:21 +01:00
|
|
|
$results = $context->getResults($params);
|
|
|
|
|
2017-07-27 02:05:27 +02:00
|
|
|
$this->assertEquals(1, $results->count());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-07-27 02:05:27 +02:00
|
|
|
/** @var SearchContextTest\Project $project */
|
|
|
|
$project = $results->first();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertInstanceOf(SearchContextTest\Project::class, $project);
|
|
|
|
$this->assertEquals("Blog Website", $project->Name);
|
|
|
|
$this->assertEquals(2, $project->Actions()->Count());
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"Get RSS feeds working",
|
|
|
|
$project->Actions()->find('ID', $action3->ID)->Description
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanGenerateQueryUsingAllFilterTypes()
|
|
|
|
{
|
|
|
|
$all = SearchContextTest\AllFilterTypes::singleton();
|
|
|
|
$context = $all->getDefaultSearchContext();
|
2020-04-20 19:58:09 +02:00
|
|
|
$params = [
|
2016-12-16 05:34:21 +01:00
|
|
|
"ExactMatch" => "Match me exactly",
|
|
|
|
"PartialMatch" => "partially",
|
2020-04-20 19:58:09 +02:00
|
|
|
"CollectionMatch" => [
|
2016-12-16 05:34:21 +01:00
|
|
|
"ExistingCollectionValue",
|
|
|
|
"NonExistingCollectionValue",
|
|
|
|
4,
|
|
|
|
"Inline'Quotes'"
|
2020-04-20 19:58:09 +02:00
|
|
|
],
|
2016-12-16 05:34:21 +01:00
|
|
|
"StartsWith" => "12345",
|
|
|
|
"EndsWith" => "ijkl",
|
|
|
|
"Fulltext" => "two"
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(1, $results->Count());
|
|
|
|
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStartsWithFilterCaseInsensitive()
|
|
|
|
{
|
|
|
|
$all = SearchContextTest\AllFilterTypes::singleton();
|
|
|
|
$context = $all->getDefaultSearchContext();
|
2020-04-20 19:58:09 +02:00
|
|
|
$params = [
|
2016-12-16 05:34:21 +01:00
|
|
|
"StartsWith" => "12345-6789 camelcase", // spelled lowercase
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(1, $results->Count());
|
|
|
|
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEndsWithFilterCaseInsensitive()
|
|
|
|
{
|
|
|
|
$all = SearchContextTest\AllFilterTypes::singleton();
|
|
|
|
$context = $all->getDefaultSearchContext();
|
2020-04-20 19:58:09 +02:00
|
|
|
$params = [
|
2016-12-16 05:34:21 +01:00
|
|
|
"EndsWith" => "IJKL", // spelled uppercase
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$results = $context->getResults($params);
|
|
|
|
$this->assertEquals(1, $results->Count());
|
|
|
|
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
|
|
|
|
}
|
2017-07-26 06:35:10 +02:00
|
|
|
|
|
|
|
public function testSearchContextSummary()
|
|
|
|
{
|
|
|
|
$filters = [
|
|
|
|
'KeywordSearch' => PartialMatchFilter::create('KeywordSearch'),
|
|
|
|
'Country' => PartialMatchFilter::create('Country'),
|
|
|
|
'CategoryID' => PartialMatchFilter::create('CategoryID'),
|
|
|
|
'Featured' => PartialMatchFilter::create('Featured'),
|
|
|
|
'Nothing' => PartialMatchFilter::create('Nothing'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$fields = FieldList::create(
|
|
|
|
TextField::create('KeywordSearch', 'Keywords'),
|
|
|
|
TextField::create('Country', 'Country'),
|
|
|
|
DropdownField::create('CategoryID', 'Category', [
|
|
|
|
1 => 'Category one',
|
|
|
|
2 => 'Category two',
|
|
|
|
]),
|
|
|
|
CheckboxField::create('Featured', 'Featured')
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = SearchContext::create(
|
|
|
|
SearchContextTest\Person::class,
|
|
|
|
$fields,
|
|
|
|
$filters
|
|
|
|
);
|
|
|
|
|
|
|
|
$context->setSearchParams([
|
|
|
|
'KeywordSearch' => 'tester',
|
|
|
|
'Country' => null,
|
|
|
|
'CategoryID' => 2,
|
|
|
|
'Featured' => 1,
|
|
|
|
'Nothing' => 'empty',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$list = $context->getSummary();
|
|
|
|
|
|
|
|
$this->assertEquals(3, $list->count());
|
|
|
|
// KeywordSearch should be in the summary
|
|
|
|
$keyword = $list->find('Field', 'Keywords');
|
|
|
|
$this->assertNotNull($keyword);
|
|
|
|
$this->assertEquals('tester', $keyword->Value);
|
|
|
|
|
|
|
|
// Country should be skipped over
|
|
|
|
$country = $list->find('Field', 'Country');
|
|
|
|
$this->assertNull($country);
|
|
|
|
|
|
|
|
// Category should be expressed as the label
|
|
|
|
$category = $list->find('Field', 'Category');
|
|
|
|
$this->assertNotNull($category);
|
|
|
|
$this->assertEquals('Category two', $category->Value);
|
|
|
|
|
|
|
|
// Featured should have no value, since it's binary
|
|
|
|
$featured = $list->find('Field', 'Featured');
|
|
|
|
$this->assertNotNull($featured);
|
|
|
|
$this->assertNull($featured->Value);
|
|
|
|
|
|
|
|
// "Nothing" should come back null since there's no field for it
|
|
|
|
$nothing = $list->find('Field', 'Nothing');
|
|
|
|
$this->assertNull($nothing);
|
|
|
|
}
|
2008-08-09 05:54:55 +02:00
|
|
|
}
|