Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion tests/NestedSetsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.',
);
}
}
11 changes: 10 additions & 1 deletion tests/phpstan-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -26,5 +32,8 @@
TreeQuery::class => [
NestedSetsQueryBehavior::class,
],
TreeWithStrictValidation::class => [
NestedSetsBehavior::class,
],
],
];
24 changes: 24 additions & 0 deletions tests/support/model/TreeWithStrictValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace yii2\extensions\nestedsets\tests\support\model;

/**
* @property int $id
* @property int $lft
* @property int $rgt
* @property int $depth
* @property string $name
*/
final class TreeWithStrictValidation extends Tree
{
public function rules(): array
{
return [
['name', 'required', 'message' => '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.'],
];
}
}