From 5c7554dd54c9572cddea44d3524e6f316e7e1489 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Fri, 7 Oct 2022 15:31:40 +1300 Subject: [PATCH 1/2] FIX Don't assume dataclass is in the spec. --- code/ModelAdmin.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/code/ModelAdmin.php b/code/ModelAdmin.php index c0a33681c..18a589053 100644 --- a/code/ModelAdmin.php +++ b/code/ModelAdmin.php @@ -574,6 +574,9 @@ protected function getModelTabForModelClass(string $modelClass): ?string $classes = array_reverse(ClassInfo::ancestry($modelClass)); foreach ($classes as $class) { foreach ($managed as $tab => $spec) { + if (!isset($spec['dataClass'])) { + $spec['dataClass'] = $tab; + } if ($spec['dataClass'] === $class) { return $tab; } From df9d4330d4ba78f529a4194f0d3e0ba35bc7ac83 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 11 Oct 2022 15:00:05 +1300 Subject: [PATCH 2/2] MNT Test that dataClass doesn't have to be in the spec --- tests/php/ModelAdminTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/php/ModelAdminTest.php b/tests/php/ModelAdminTest.php index 9fe6db7c4..762469069 100644 --- a/tests/php/ModelAdminTest.php +++ b/tests/php/ModelAdminTest.php @@ -367,6 +367,26 @@ public function testGetModelTabForInvalidModelClass() $reflectionMethod->invoke($admin, 'cricket-players'); } + public function testGetModelTabForModelClassNoSpec() + { + /** @var ModelAdmin $mock */ + $mock = $this->getMockBuilder(ModelAdminTest\MultiModelAdmin::class)->getMock(); + + // `ModelTabForModelClass` relies on `getManagedModels` whose output format has changed within the 4.x line. + // We need to mock `getManagedModels` to test the legacy format. + $mock->expects($this->atLeastOnce()) + ->method('getManagedModels') + ->will($this->returnValue([ + Player::class => [ + 'title' => 'Rugby Players' + ], + ])); + + $reflectionMethod = new ReflectionMethod($mock, 'getModelTabForModelClass'); + $reflectionMethod->setAccessible(true); + $this->assertSame(Player::class, $reflectionMethod->invoke($mock, Player::class)); + } + public function testIsManagedModel() { $admin = new ModelAdminTest\MultiModelAdmin();