From 283499cf61475c9a3a67d7dd214ccf7235e31086 Mon Sep 17 00:00:00 2001 From: rhertogh Date: Sun, 3 Mar 2024 09:32:21 +0100 Subject: [PATCH] Fix #20122: Fixed parsing of boolean keywords (e.g. used in SQLite) in `\yii\db\ColumnSchema::typecast()` --- framework/CHANGELOG.md | 1 + framework/db/ColumnSchema.php | 2 +- tests/framework/db/SchemaTest.php | 33 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index a4dd74dfcb8..9567e04fd97 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -14,6 +14,7 @@ Yii Framework 2 Change Log - Bug #13920: Fixed erroneous validation for specific cases (tim-fischer-maschinensucher) - Bug #19927: Fixed `console\controllers\MessageController` when saving translations to database: fixed FK error when adding new string and language at the same time, checking/regenerating all missing messages and dropping messages for unused languages (atrandafir) - Bug #20002: Fixed superfluous query on HEAD request in serializer (xicond) +- Bug #20122: Fixed parsing of boolean keywords (e.g. used in SQLite) in `\yii\db\ColumnSchema::typecast()` (rhertogh) - Enh #12743: Added new methods `BaseActiveRecord::loadRelations()` and `BaseActiveRecord::loadRelationsFor()` to eager load related models for existing primary model instances (PowerGamer1) - Enh #20030: Improve performance of handling `ErrorHandler::$memoryReserveSize` (antonshevelev, rob006) - Enh #20042: Add empty array check to `ActiveQueryTrait::findWith()` (renkas) diff --git a/framework/db/ColumnSchema.php b/framework/db/ColumnSchema.php index 74e1ddea84b..1d01cec2707 100644 --- a/framework/db/ColumnSchema.php +++ b/framework/db/ColumnSchema.php @@ -174,7 +174,7 @@ protected function typecast($value) case 'boolean': // treating a 0 bit value as false too // https://github.com/yiisoft/yii2/issues/9006 - return (bool) $value && $value !== "\0"; + return (bool) $value && $value !== "\0" && strtolower($value) !== 'false'; case 'double': return (float) $value; } diff --git a/tests/framework/db/SchemaTest.php b/tests/framework/db/SchemaTest.php index af02b2170b6..dd05f78d9c2 100644 --- a/tests/framework/db/SchemaTest.php +++ b/tests/framework/db/SchemaTest.php @@ -545,6 +545,39 @@ public function testColumnSchemaDbTypecastWithEmptyCharType() $this->assertSame('', $columnSchema->dbTypecast('')); } + /** + * @dataProvider columnSchemaDbTypecastBooleanPhpTypeProvider + * @param mixed $value + * @param bool $expected + */ + public function testColumnSchemaDbTypecastBooleanPhpType($value, $expected) + { + $columnSchema = new ColumnSchema(['phpType' => Schema::TYPE_BOOLEAN]); + $this->assertSame($expected, $columnSchema->dbTypecast($value)); + } + + public function columnSchemaDbTypecastBooleanPhpTypeProvider() + { + return [ + [1, true], + [0, false], + ['1', true], + ['0', false], + + // https://github.com/yiisoft/yii2/issues/9006 + ["\1", true], + ["\0", false], + + // https://github.com/yiisoft/yii2/pull/20122 + ['TRUE', true], + ['FALSE', false], + ['true', true], + ['false', false], + ['True', true], + ['False', false], + ]; + } + public function testFindUniqueIndexes() { if ($this->driverName === 'sqlsrv') {