diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index e00b854..7e90ee9 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -10,7 +10,7 @@ use yii\db\{ActiveRecord, Exception, StaleObjectException}; use yii\helpers\ArrayHelper; use yii2\extensions\nestedsets\NestedSetsBehavior; -use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree}; +use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, Tree, TreeWithStrictValidation}; use function get_class; use function sprintf; @@ -1782,4 +1782,52 @@ public function testThrowExceptionWhenAppendToParentWithNullRightValue(): void $childNode->appendTo($parentNode); } + + public function testAppendToWithRunValidationParameterUsingStrictValidation(): void + { + $this->generateFixtureTree(); + + $targetNode = Tree::findOne(2); + + self::assertNotNull( + $targetNode, + 'Target node with ID \'2\' should exist before calling \'appendTo\'.', + ); + + $invalidNode = new TreeWithStrictValidation(['name' => 'x']); + + $result1 = $invalidNode->appendTo($targetNode); + $hasError1 = $invalidNode->hasErrors(); + + self::assertFalse( + $result1, + '\'appendTo()\' should return \'false\' when \'runValidation=true\' and data fails validation.', + ); + self::assertTrue( + $hasError1, + 'Node should have validation errors when \'runValidation=true\' and data is invalid.', + ); + + $invalidNode2 = new TreeWithStrictValidation(['name' => 'x']); + + $result2 = $invalidNode2->appendTo($targetNode, false); + $hasError2 = $invalidNode2->hasErrors(); + + self::assertTrue( + $result2, + '\'appendTo()\' should return \'true\' when \'runValidation=false\', even with invalid data ' . + 'that would fail validation.', + ); + self::assertFalse( + $hasError2, + 'Node should not have validation errors when \'runValidation=false\' because validation was skipped.', + ); + + $persistedNode = TreeWithStrictValidation::findOne($invalidNode2->id); + + self::assertNotNull( + $persistedNode, + 'Node should exist in database after appending to target node with validation disabled.', + ); + } } diff --git a/tests/phpstan-config.php b/tests/phpstan-config.php index 80f2b65..d25d0c1 100644 --- a/tests/phpstan-config.php +++ b/tests/phpstan-config.php @@ -4,7 +4,13 @@ use yii\db\{ActiveQuery, ActiveRecord}; use yii2\extensions\nestedsets\{NestedSetsBehavior, NestedSetsQueryBehavior}; -use yii2\extensions\nestedsets\tests\support\model\{MultipleTree, MultipleTreeQuery, Tree, TreeQuery}; +use yii2\extensions\nestedsets\tests\support\model\{ + MultipleTree, + MultipleTreeQuery, + Tree, + TreeQuery, + TreeWithStrictValidation, +}; return [ 'behaviors' => [ @@ -26,5 +32,8 @@ TreeQuery::class => [ NestedSetsQueryBehavior::class, ], + TreeWithStrictValidation::class => [ + NestedSetsBehavior::class, + ], ], ]; diff --git a/tests/support/model/TreeWithStrictValidation.php b/tests/support/model/TreeWithStrictValidation.php new file mode 100644 index 0000000..8f61831 --- /dev/null +++ b/tests/support/model/TreeWithStrictValidation.php @@ -0,0 +1,24 @@ + 'Name cannot be blank.'], + ['name', 'string', 'min' => 5, 'message' => 'Name must be at least 5 characters long.'], + ['name', 'match', 'pattern' => '/^[A-Z]/', 'message' => 'Name must start with an uppercase letter.'], + ]; + } +}