silverstripe-framework/tests/php/ORM/Search/SearchContextTest.php

178 lines
5.2 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\ORM\Tests\Search;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\Filters\PartialMatchFilter;
class SearchContextTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
protected static $fixture_file = 'SearchContextTest.yml';
protected $extraDataObjects = array(
2016-10-14 03:30:05 +02:00
SearchContextTest\Person::class,
SearchContextTest\Book::class,
SearchContextTest\Company::class,
SearchContextTest\Project::class,
SearchContextTest\Deadline::class,
SearchContextTest\Action::class,
SearchContextTest\AllFilterTypes::class,
);
public function testResultSetFilterReturnsExpectedCount() {
2016-10-14 03:30:05 +02:00
$person = SearchContextTest\Person::singleton();
$context = $person->getDefaultSearchContext();
$results = $context->getResults(array('Name'=>''));
$this->assertEquals(5, $results->Count());
2014-08-15 08:53:05 +02:00
$results = $context->getResults(array('EyeColor'=>'green'));
$this->assertEquals(2, $results->Count());
2014-08-15 08:53:05 +02:00
$results = $context->getResults(array('EyeColor'=>'green', 'HairColor'=>'black'));
$this->assertEquals(1, $results->Count());
}
2014-08-15 08:53:05 +02:00
public function testSummaryIncludesDefaultFieldsIfNotDefined() {
2016-10-14 03:30:05 +02:00
$person = SearchContextTest\Person::singleton();
$this->assertContains('Name', $person->summaryFields());
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02:00
$book = SearchContextTest\Book::singleton();
$this->assertContains('Title', $book->summaryFields());
}
2014-08-15 08:53:05 +02:00
public function testAccessDefinedSummaryFields() {
2016-10-14 03:30:05 +02:00
$company = SearchContextTest\Company::singleton();
$this->assertContains('Industry', $company->summaryFields());
}
2014-08-15 08:53:05 +02:00
public function testPartialMatchUsedByDefaultWhenNotExplicitlySet() {
2016-10-14 03:30:05 +02:00
$person = SearchContextTest\Person::singleton();
$context = $person->getDefaultSearchContext();
2014-08-15 08:53:05 +02:00
$this->assertEquals(
array(
"Name" => new PartialMatchFilter("Name"),
"HairColor" => new PartialMatchFilter("HairColor"),
"EyeColor" => new PartialMatchFilter("EyeColor")
),
$context->getFilters()
);
}
2014-08-15 08:53:05 +02:00
public function testDefaultFiltersDefinedWhenNotSetInDataObject() {
2016-10-14 03:30:05 +02:00
$book = SearchContextTest\Book::singleton();
$context = $book->getDefaultSearchContext();
2014-08-15 08:53:05 +02:00
$this->assertEquals(
array(
"Title" => new PartialMatchFilter("Title")
),
$context->getFilters()
);
}
2014-08-15 08:53:05 +02:00
public function testUserDefinedFiltersAppearInSearchContext() {
2016-10-14 03:30:05 +02:00
$company = SearchContextTest\Company::singleton();
$context = $company->getDefaultSearchContext();
$this->assertEquals(
array(
"Name" => new PartialMatchFilter("Name"),
"Industry" => new PartialMatchFilter("Industry"),
"AnnualProfit" => new PartialMatchFilter("AnnualProfit")
),
$context->getFilters()
);
}
2014-08-15 08:53:05 +02:00
public function testUserDefinedFieldsAppearInSearchContext() {
2016-10-14 03:30:05 +02:00
$company = SearchContextTest\Company::singleton();
$context = $company->getDefaultSearchContext();
$fields = $context->getFields();
$this->assertEquals(
new FieldList(
new TextField("Name", 'Name'),
new TextareaField("Industry", 'Industry'),
new NumericField("AnnualProfit", 'The Almighty Annual Profit')
),
$context->getFields()
);
}
2014-08-15 08:53:05 +02:00
public function testRelationshipObjectsLinkedInSearch() {
2016-10-14 03:30:05 +02:00
$action3 = $this->objFromFixture(SearchContextTest\Action::class, 'action3');
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02:00
$project = singleton(SearchContextTest\Project::class);
$context = $project->getDefaultSearchContext();
2014-08-15 08:53:05 +02:00
$params = array("Name"=>"Blog Website", "Actions__SolutionArea"=>"technical");
2014-08-15 08:53:05 +02:00
$results = $context->getResults($params);
2014-08-15 08:53:05 +02:00
$this->assertEquals(1, $results->Count());
2014-08-15 08:53:05 +02:00
$project = $results->First();
2014-08-15 08:53:05 +02:00
2016-10-14 03:30:05 +02:00
$this->assertInstanceOf(SearchContextTest\Project::class, $project);
$this->assertEquals("Blog Website", $project->Name);
$this->assertEquals(2, $project->Actions()->Count());
2014-08-15 08:53:05 +02:00
$this->assertEquals(
2014-08-15 08:53:05 +02:00
"Get RSS feeds working",
$project->Actions()->find('ID', $action3->ID)->Description
);
}
2014-08-15 08:53:05 +02:00
public function testCanGenerateQueryUsingAllFilterTypes() {
2016-10-14 03:30:05 +02:00
$all = SearchContextTest\AllFilterTypes::singleton();
$context = $all->getDefaultSearchContext();
$params = array(
"ExactMatch" => "Match me exactly",
"PartialMatch" => "partially",
2016-10-14 03:30:05 +02:00
"CollectionMatch" => array(
"ExistingCollectionValue",
"NonExistingCollectionValue",
4,
"Inline'Quotes'"
),
"StartsWith" => "12345",
"EndsWith" => "ijkl",
"Fulltext" => "two"
);
$results = $context->getResults($params);
$this->assertEquals(1, $results->Count());
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
}
public function testStartsWithFilterCaseInsensitive() {
2016-10-14 03:30:05 +02:00
$all = SearchContextTest\AllFilterTypes::singleton();
$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);
}
public function testEndsWithFilterCaseInsensitive() {
2016-10-14 03:30:05 +02:00
$all = SearchContextTest\AllFilterTypes::singleton();
$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);
}
}