From 95fe0174c7951d6fd9e7fe9ae18488201a2959b9 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 21 Sep 2025 12:19:50 -0400 Subject: [PATCH 1/3] Fix type error when undefined fields are used If a field that does not exist in the schema (like an association property) is passed to `enumSupportsLabel` there shouldn't be an error. --- src/View/Helper/BakeHelper.php | 2 +- tests/TestCase/View/Helper/BakeHelperTest.php | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/View/Helper/BakeHelper.php b/src/View/Helper/BakeHelper.php index 41b48a9e..81813818 100644 --- a/src/View/Helper/BakeHelper.php +++ b/src/View/Helper/BakeHelper.php @@ -304,7 +304,7 @@ public function columnData(string $field, TableSchema $schema): ?array public function enumSupportsLabel(string $field, TableSchema $schema): bool { $typeName = $schema->getColumnType($field); - if (!str_starts_with($typeName, 'enum-')) { + if (!$typeName || !str_starts_with($typeName, 'enum-')) { return false; } $type = TypeFactory::build($typeName); diff --git a/tests/TestCase/View/Helper/BakeHelperTest.php b/tests/TestCase/View/Helper/BakeHelperTest.php index a0bafee6..196adcec 100644 --- a/tests/TestCase/View/Helper/BakeHelperTest.php +++ b/tests/TestCase/View/Helper/BakeHelperTest.php @@ -16,8 +16,10 @@ */ namespace Bake\Test\TestCase\View\Helper; +use Bake\Test\App\Model\Enum\BakeUserStatus; use Bake\View\BakeView; use Bake\View\Helper\BakeHelper; +use Cake\Database\Type\EnumType; use Cake\Http\Response; use Cake\Http\ServerRequest as Request; use Cake\TestSuite\TestCase; @@ -40,6 +42,7 @@ class BakeHelperTest extends TestCase 'plugin.Bake.BakeComments', 'plugin.Bake.BakeArticlesBakeTags', 'plugin.Bake.BakeTags', + 'plugin.Bake.Users', ]; /** @@ -89,12 +92,12 @@ public function testAliasExtractorFilteredHasMany() 'className' => '\Bake\Test\App\Model\Table\ArticlesTable', ]); $this->BakeHelper = $this->getMockBuilder('Bake\View\Helper\BakeHelper') - ->disableOriginalConstructor() - ->onlyMethods(['_filterHasManyAssociationsAliases']) - ->getMock(); + ->disableOriginalConstructor() + ->onlyMethods(['_filterHasManyAssociationsAliases']) + ->getMock(); $this->BakeHelper->expects($this->once()) - ->method('_filterHasManyAssociationsAliases') - ->with($table, ['ArticlesTags']); + ->method('_filterHasManyAssociationsAliases') + ->with($table, ['ArticlesTags']); $result = $this->BakeHelper->aliasExtractor($table, 'HasMany'); $this->assertEmpty($result); } @@ -107,7 +110,7 @@ public function testAliasExtractorFilteredHasMany() public function testAliasExtractorBelongsTo() { $table = $this->getTableLocator()->get('Articles', [ - 'className' => '\Bake\Test\App\Model\Table\ArticlesTable', + 'className' => '\Bake\Test\App\Model\Table\ArticlesTable', ]); $result = $this->BakeHelper->aliasExtractor($table, 'BelongsTo'); $expected = ['authors']; @@ -122,7 +125,7 @@ public function testAliasExtractorBelongsTo() public function testAliasExtractorBelongsToMany() { $table = $this->getTableLocator()->get('Articles', [ - 'className' => '\Bake\Test\App\Model\Table\ArticlesTable', + 'className' => '\Bake\Test\App\Model\Table\ArticlesTable', ]); $result = $this->BakeHelper->aliasExtractor($table, 'BelongsToMany'); $expected = ['tags']; @@ -185,4 +188,17 @@ public function testHasPlugin(): void $this->assertTrue($this->BakeHelper->hasPlugin('Bake')); $this->assertFalse($this->BakeHelper->hasPlugin('DebugKit')); } + + public function testEnumSupportsLabel(): void + { + $table = $this->getTableLocator()->get('Users', [ + 'className' => '\Bake\Test\App\Model\Table\BakeUsersTable', + ]); + $schema = $table->getSchema(); + $schema->setColumnType('status', EnumType::from(BakeUserStatus::class)); + + $this->assertFalse($this->BakeHelper->enumSupportsLabel('status', $schema)); + $this->assertFalse($this->BakeHelper->enumSupportsLabel('username', $schema)); + $this->assertFalse($this->BakeHelper->enumSupportsLabel('does_not_exist', $schema)); + } } From 3c860f44898f3c59323d88bb90650cd35a3bdd50 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 21 Sep 2025 12:24:18 -0400 Subject: [PATCH 2/3] Fix test --- tests/TestCase/View/Helper/BakeHelperTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/TestCase/View/Helper/BakeHelperTest.php b/tests/TestCase/View/Helper/BakeHelperTest.php index 196adcec..9fb54c5b 100644 --- a/tests/TestCase/View/Helper/BakeHelperTest.php +++ b/tests/TestCase/View/Helper/BakeHelperTest.php @@ -191,13 +191,11 @@ public function testHasPlugin(): void public function testEnumSupportsLabel(): void { - $table = $this->getTableLocator()->get('Users', [ - 'className' => '\Bake\Test\App\Model\Table\BakeUsersTable', - ]); + $table = $this->fetchTable('BakeUsers'); $schema = $table->getSchema(); $schema->setColumnType('status', EnumType::from(BakeUserStatus::class)); - $this->assertFalse($this->BakeHelper->enumSupportsLabel('status', $schema)); + $this->assertTrue($this->BakeHelper->enumSupportsLabel('status', $schema)); $this->assertFalse($this->BakeHelper->enumSupportsLabel('username', $schema)); $this->assertFalse($this->BakeHelper->enumSupportsLabel('does_not_exist', $schema)); } From 90a3cf8dcbebe9a2c1049fb0adbb40188b4e6dac Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 21 Sep 2025 12:25:20 -0400 Subject: [PATCH 3/3] update phpcs config --- phpcs.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/phpcs.xml b/phpcs.xml index d1748477..501f6cfd 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,6 +1,5 @@ - src/