From cbd358ab93094a2ad0271d3600b94bb3f1899719 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Wed, 18 Oct 2023 09:36:41 +1300 Subject: [PATCH] MNT Test that we can't filter/sort eagerloaded data by relation (#10978) --- tests/php/ORM/EagerLoadedListTest.php | 89 +++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tests/php/ORM/EagerLoadedListTest.php b/tests/php/ORM/EagerLoadedListTest.php index c9b717d06..901502acd 100644 --- a/tests/php/ORM/EagerLoadedListTest.php +++ b/tests/php/ORM/EagerLoadedListTest.php @@ -953,6 +953,13 @@ class EagerLoadedListTest extends SapphireTest $this->assertFalse($subteam->canSortBy('SomethingElse')); } + public function testCannotSortByRelation() + { + $list = $this->getListWithRecords(TeamComment::class); + $this->assertFalse($list->canSortBy('Team')); + $this->assertFalse($list->canSortBy('Team.Title')); + } + public function testArrayAccess() { $list = $this->getListWithRecords(Team::class)->sort('Title'); @@ -1112,6 +1119,14 @@ class EagerLoadedListTest extends SapphireTest ); } + public function testSortByRelation() + { + $list = $this->getListWithRecords(TeamComment::class); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Cannot sort by relations on EagerLoadedList'); + $list = $list->sort('Team.Title', 'ASC'); + } + /** * @dataProvider provideSortInvalidParameters */ @@ -1324,6 +1339,26 @@ class EagerLoadedListTest extends SapphireTest $this->assertTrue($subteam->canFilterBy("SubclassDatabaseField")); } + public function testCannotFilterByRelation() + { + $list = $this->getListWithRecords(Team::class); + + $this->assertFalse($list->canFilterBy('Captain.ShirtNumber')); + $this->assertFalse($list->canFilterBy('SomethingElse.ShirtNumber')); + $this->assertFalse($list->canFilterBy('Captain.SomethingElse')); + $this->assertFalse($list->canFilterBy('Captain.FavouriteTeam.Captain.ShirtNumber')); + + // Has many + $this->assertFalse($list->canFilterBy('Fans.Name')); + $this->assertFalse($list->canFilterBy('SomethingElse.Name')); + $this->assertFalse($list->canFilterBy('Fans.SomethingElse')); + + // Many many + $this->assertFalse($list->canFilterBy('Players.FirstName')); + $this->assertFalse($list->canFilterBy('SomethingElse.FirstName')); + $this->assertFalse($list->canFilterBy('Players.SomethingElse')); + } + public function testAddfilter() { $list = $this->getListWithRecords(TeamComment::class); @@ -1441,6 +1476,60 @@ class EagerLoadedListTest extends SapphireTest $this->assertEquals(2, count($list->exclude('ID', $id) ?? [])); } + public function testFilterAnyByRelation() + { + $list = $this->getListWithRecords(Player::class); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Can't filter by column 'Teams.Title'"); + $list = $list->filterAny(['Teams.Title' => 'Team']); + } + + public function testFilterAggregate() + { + $list = $this->getListWithRecords(Team::class); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Can't filter by column 'Players.Count()'"); + $list->filter(['Players.Count()' => 2]); + } + + public function testFilterAnyAggregate() + { + $list = $this->getListWithRecords(Team::class); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Can't filter by column 'Players.Count()'"); + $list->filterAny(['Players.Count()' => 2]); + } + + public function provideCantFilterByRelation() + { + return [ + 'many_many' => [ + 'Players.FirstName', + ], + 'has_many' => [ + 'Comments.Name', + ], + 'has_one' => [ + 'FavouriteTeam.Title', + ], + 'non-existent relation' => [ + 'MascotAnimal.Name', + ] + ]; + } + + /** + * @dataProvider provideCantFilterByRelation + */ + public function testCantFilterByRelation(string $column) + { + // Many to many + $list = $this->getListWithRecords(Team::class); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Can't filter by column '$column'"); + $list->filter($column, ['Captain', 'Captain 2']); + } + /** * @dataProvider provideFilterByNull */