From 914f6bac1a08c501ef0374f05aef1da43fd1a957 Mon Sep 17 00:00:00 2001 From: Alexander Volle Date: Sat, 22 Mar 2025 13:54:42 +0100 Subject: [PATCH 1/4] Fix display field replacement not working when overriding `defaultTable` in controller --- src/Controller/Component/TitleComponent.php | 2 +- .../TitleComponentIntegrationTest.php | 15 +++++++++ .../Controller/LocationsAliasesController.php | 33 +++++++++++++++++++ .../templates/LocationsAliases/view.php | 0 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/test_app/TestApp/Controller/LocationsAliasesController.php create mode 100644 tests/test_app/templates/LocationsAliases/view.php diff --git a/src/Controller/Component/TitleComponent.php b/src/Controller/Component/TitleComponent.php index 175df35..630c5ab 100644 --- a/src/Controller/Component/TitleComponent.php +++ b/src/Controller/Component/TitleComponent.php @@ -105,7 +105,7 @@ protected function getPrefix(): string */ protected function getDisplayFieldValue(): string { - $model = $this->getController()->getName(); + $model = $this->getController()->fetchTable()->getAlias(); $entityVar = $this->getEntityVar($model); $entity = $this->getEntity($entityVar); if (is_null($entity)) { diff --git a/tests/TestCase/Controller/Component/TitleComponentIntegrationTest.php b/tests/TestCase/Controller/Component/TitleComponentIntegrationTest.php index 011aa53..3fab1b6 100644 --- a/tests/TestCase/Controller/Component/TitleComponentIntegrationTest.php +++ b/tests/TestCase/Controller/Component/TitleComponentIntegrationTest.php @@ -120,6 +120,21 @@ public function testTitleEntityIsMultipleWords(): void $this->assertTitle($expected); } + /** + * Test testTitleWithNonDefaultTableName method + * + * If you have a controller that changes the `defaultTable` property, then this test will assert that works OK + * + * @return void + * @covers \Avolle\Title\Controller\Component\TitleComponent::formatTitle() + */ + public function testTitleWithNonDefaultTableName(): void + { + $expected = 'Locations Aliases - View - Ă…lesund'; + $this->get(['controller' => 'LocationsAliases', 'action' => 'view', 1]); + $this->assertTitle($expected); + } + /** * Assert that title is set as expected * Will show actual title if assertion is wrong diff --git a/tests/test_app/TestApp/Controller/LocationsAliasesController.php b/tests/test_app/TestApp/Controller/LocationsAliasesController.php new file mode 100644 index 0000000..4859340 --- /dev/null +++ b/tests/test_app/TestApp/Controller/LocationsAliasesController.php @@ -0,0 +1,33 @@ +Locations->get($id); + + $this->set('location', $location); + } +} diff --git a/tests/test_app/templates/LocationsAliases/view.php b/tests/test_app/templates/LocationsAliases/view.php new file mode 100644 index 0000000..e69de29 From 434a27804c58d0657d3779b06481e97ff7a82e73 Mon Sep 17 00:00:00 2001 From: Alexander Volle Date: Sat, 22 Mar 2025 13:55:43 +0100 Subject: [PATCH 2/4] Use `IntegrationTestTrait::_getBodyAsString()` to quieten Stan --- .../Controller/Component/TitleComponentIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestCase/Controller/Component/TitleComponentIntegrationTest.php b/tests/TestCase/Controller/Component/TitleComponentIntegrationTest.php index 3fab1b6..d4ad224 100644 --- a/tests/TestCase/Controller/Component/TitleComponentIntegrationTest.php +++ b/tests/TestCase/Controller/Component/TitleComponentIntegrationTest.php @@ -144,7 +144,7 @@ public function testTitleWithNonDefaultTableName(): void */ protected function assertTitle(string $expected): void { - preg_match('/.*?<\/title>/', (string)$this->_response, $matches); + preg_match('/<title>.*?<\/title>/', $this->_getBodyAsString(), $matches); $this->assertResponseContains($expected, 'Actual title: ' . ($matches[0] ?? 'No title found')); } } From 8b22026cbf35458ecbf4923170265095d792e4a4 Mon Sep 17 00:00:00 2001 From: Alexander Volle <alexander.volle@hotmail.com> Date: Sat, 22 Mar 2025 14:05:30 +0100 Subject: [PATCH 3/4] Further quieten Stan --- phpstan.neon | 2 -- src/Controller/Component/TitleComponent.php | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 058cb4e..1b4093b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,5 @@ parameters: level: 7 - checkGenericClassInNonGenericObjectType: false - checkMissingIterableValueType: false treatPhpDocTypesAsCertain: false paths: - src diff --git a/src/Controller/Component/TitleComponent.php b/src/Controller/Component/TitleComponent.php index 630c5ab..dcbb5fe 100644 --- a/src/Controller/Component/TitleComponent.php +++ b/src/Controller/Component/TitleComponent.php @@ -42,7 +42,7 @@ class TitleComponent extends Component * If title not already set in the action, create and set a title based on controller, controller action and * controller related model class' display field (if action is view and option showDisplayFieldOnView is true * - * @param \Cake\Event\Event $event Event + * @param \Cake\Event\Event<\Cake\Controller\Controller> $event Event * @return void * @noinspection PhpUnusedParameterInspection */ From 80898265ab22ca0e629222824baeec4fe441e536 Mon Sep 17 00:00:00 2001 From: Alexander Volle <alexander.volle@hotmail.com> Date: Sat, 22 Mar 2025 14:05:41 +0100 Subject: [PATCH 4/4] Quieten codesniffer --- src/Controller/Component/TitleComponent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/Component/TitleComponent.php b/src/Controller/Component/TitleComponent.php index dcbb5fe..1a12b74 100644 --- a/src/Controller/Component/TitleComponent.php +++ b/src/Controller/Component/TitleComponent.php @@ -117,7 +117,7 @@ protected function getDisplayFieldValue(): string return ''; } if (is_array($displayField)) { - return implode(' - ', array_map(fn ($field) => (string)$entity->get($field), $displayField)); + return implode(' - ', array_map(fn($field) => (string)$entity->get($field), $displayField)); } return (string)$entity->get($displayField);