diff --git a/tests/php/ORM/Search/SearchContextTest.php b/tests/php/ORM/Search/SearchContextTest.php index 18010b273..c3a0a379f 100644 --- a/tests/php/ORM/Search/SearchContextTest.php +++ b/tests/php/ORM/Search/SearchContextTest.php @@ -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); } } diff --git a/tests/php/ORM/Search/SearchContextTest.yml b/tests/php/ORM/Search/SearchContextTest.yml index 5eb4d4022..2759311eb 100644 --- a/tests/php/ORM/Search/SearchContextTest.yml +++ b/tests/php/ORM/Search/SearchContextTest.yml @@ -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 diff --git a/tests/php/ORM/Search/SearchContextTest/Order.php b/tests/php/ORM/Search/SearchContextTest/Order.php index 6aae69b44..3df920181 100644 --- a/tests/php/ORM/Search/SearchContextTest/Order.php +++ b/tests/php/ORM/Search/SearchContextTest/Order.php @@ -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', + ], + ], ]; }