MNT Update tests for searchable_fields match_any

This commit is contained in:
Guy Sartorelli 2022-06-30 13:26:35 +12:00
parent 30cd521029
commit ffcaed84f3
3 changed files with 83 additions and 6 deletions

View File

@ -262,10 +262,48 @@ class SearchContextTest extends SapphireTest
// Search should match Order's customer FirstName
$results = $context->getResults(['CustomFirstName' => 'Bill']);
$this->assertEquals(1, $results->Count());
$this->assertCount(2, $results);
$this->assertListContains([
['Name' => 'Jane'],
['Name' => 'Jack'],
], $results);
// Search should match Order's shipping address FirstName
$results = $context->getResults(['CustomFirstName' => 'Bob']);
$this->assertEquals(1, $results->Count());
$this->assertCount(2, $results);
$this->assertListContains([
['Name' => 'Jane'],
['Name' => 'Jill'],
], $results);
// Search should match Order's Name db field
$results = $context->getResults(['CustomFirstName' => 'Jane']);
$this->assertCount(1, $results);
$this->assertSame('Jane', $results->first()->Name);
// Search should not match any Order
$results = $context->getResults(['CustomFirstName' => 'NoMatches']);
$this->assertCount(0, $results);
}
public function testMatchAnySearchWithFilters()
{
$order1 = $this->objFromFixture(SearchContextTest\Order::class, 'order1');
$context = $order1->getDefaultSearchContext();
$results = $context->getResults(['ExactMatchField' => 'Bil']);
$this->assertCount(0, $results);
$results = $context->getResults(['PartialMatchField' => 'Bil']);
$this->assertCount(2, $results);
$results = $context->getResults(['ExactMatchField' => 'ob']);
$this->assertCount(0, $results);
$results = $context->getResults(['PartialMatchField' => 'ob']);
$this->assertCount(2, $results);
$results = $context->getResults(['ExactMatchField' => 'an']);
$this->assertCount(0, $results);
$results = $context->getResults(['PartialMatchField' => 'an']);
$this->assertCount(1, $results);
}
}

View File

@ -74,12 +74,29 @@ SilverStripe\ORM\Tests\Search\SearchContextTest\AllFilterTypes:
SilverStripe\ORM\Tests\Search\SearchContextTest\Customer:
customer1:
FirstName: Bill
customer2:
FirstName: Bailey
customer3:
FirstName: Billy
SilverStripe\ORM\Tests\Search\SearchContextTest\Address:
address1:
FirstName: Bob
address2:
FirstName: Barley
address3:
FirstName: Billy
SilverStripe\ORM\Tests\Search\SearchContextTest\Order:
order1:
Name: 'Jane'
Customer: =>SilverStripe\ORM\Tests\Search\SearchContextTest\Customer.customer1
ShippingAddress: =>SilverStripe\ORM\Tests\Search\SearchContextTest\Address.address1
order2:
Name: 'Jill'
Customer: =>SilverStripe\ORM\Tests\Search\SearchContextTest\Customer.customer2
ShippingAddress: =>SilverStripe\ORM\Tests\Search\SearchContextTest\Address.address1
order3:
Name: 'Jack'
Customer: =>SilverStripe\ORM\Tests\Search\SearchContextTest\Customer.customer3
ShippingAddress: =>SilverStripe\ORM\Tests\Search\SearchContextTest\Address.address3

View File

@ -10,6 +10,10 @@ class Order extends DataObject implements TestOnly
{
private static $table_name = 'SearchContextTest_Order';
private static $db = [
'Name' => 'Varchar',
];
private static $has_one = [
'Customer' => Customer::class,
'ShippingAddress' => Address::class,
@ -19,12 +23,30 @@ class Order extends DataObject implements TestOnly
'CustomFirstName' => [
'title' => 'First Name',
'field' => TextField::class,
'filter' => 'PartialMatchFilter',
'match_any' => [
// Searching with "First Name" will show Orders with matching Customer or Address names
// Searching with the "First Name" field will show Orders matching either Name, Customer.FirstName, or ShippingAddress.FirstName
'Name',
'Customer.FirstName',
'ShippingAddress.FirstName',
]
]
],
],
'PartialMatchField' => [
'field' => TextField::class,
'filter' => 'PartialMatchFilter',
'match_any' => [
'Name',
'Customer.FirstName',
'ShippingAddress.FirstName',
],
],
'ExactMatchField' => [
'field' => TextField::class,
'filter' => 'ExactMatchFilter',
'match_any' => [
'Name',
'Customer.FirstName',
'ShippingAddress.FirstName',
],
],
];
}