diff --git a/composer.json b/composer.json index 7a3b97f..c916edb 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,11 @@ "Shopware\\DbalNestedSet\\": "src" } }, + "autoload-dev": { + "psr-4": { + "Shopware\\DbalNestedSetTest\\": "tests" + } + }, "require": { "php": "^7.0", "doctrine/dbal": "^2.5" diff --git a/src/NestedSetWriter.php b/src/NestedSetWriter.php index 1f4bf5f..a285d68 100644 --- a/src/NestedSetWriter.php +++ b/src/NestedSetWriter.php @@ -207,9 +207,10 @@ public function moveAsLastChild(string $tableExpression, string $rootColumnName, throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as last child of itself or into a descendant'); } - $level = ($parent['level'] + 1) - $child['level']; + $level = ($parent['level'] + 1); - $this->updateNodePosition($tableExpression, $child, $parent['right'], $level); + $this->updateLevel($tableExpression, $child['id'], $level); + $this->updateNodePosition($tableExpression, $child, $parent['right'], $level - $child['level']); } /** @@ -233,9 +234,10 @@ public function moveAsFirstChild(string $tableExpression, string $rootColumnName throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as first child of itself or into a descendant'); } - $level = ($parent['level'] + 1) - $child['level']; + $level = ($parent['level'] + 1); - $this->updateNodePosition($tableExpression, $child, $parent['left'] + 1, $level); + $this->updateLevel($tableExpression, $child['id'], $level); + $this->updateNodePosition($tableExpression, $child, $parent['left'] + 1, $level - $child['level']); } /** @@ -259,9 +261,10 @@ public function moveAsPrevSibling(string $tableExpression, string $rootColumnNam throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as prev sibling of itself or into a descendant'); } - $level = $sibling['level'] - $child['level']; + $level = $sibling['level']; - $this->updateNodePosition($tableExpression, $child, $sibling['left'], $level); + $this->updateLevel($tableExpression, $child['id'], $level); + $this->updateNodePosition($tableExpression, $child, $sibling['left'], $level - $child['level']); } /** @@ -285,9 +288,10 @@ public function moveAsNextSibling(string $tableExpression, string $rootColumnNam throw new NestedSetExceptionInvalidNodeOperation('Cannot move node as next sibling of itself or into a descendant'); } - $level = $sibling['level'] - $child['level']; + $level = $sibling['level']; - $this->updateNodePosition($tableExpression, $child, $sibling['right'] + 1, $level); + $this->updateLevel($tableExpression, $child['id'], $level); + $this->updateNodePosition($tableExpression, $child, $sibling['right'] + 1, $level - $child['level']); } /** @@ -465,4 +469,18 @@ private function applyDeltaToSubtree(string $tableExpression, int $rootValue, in ]) ->execute(); } + + /** + * @param string $tableExpression + * @param int $id + * @param int $level + */ + private function updateLevel(string $tableExpression, int $id, int $level) + { + $this->connection->update( + $tableExpression, + [$this->levelCol => $level], + [$this->pkCol => $id] + ); + } } diff --git a/tests/NestedSetWriterTest.php b/tests/NestedSetWriterTest.php index b6b9ce9..f25f56d 100644 --- a/tests/NestedSetWriterTest.php +++ b/tests/NestedSetWriterTest.php @@ -162,6 +162,27 @@ public function test_move_as_last_child() $this->assertNode(11, 18, 19, 3, 1); } + public function test_move_as_last_child_with_different_levels() + { + \NestedSetBootstrap::insertDemoTree(); + + $this->writer->moveAsLastChild('tree', 'root_id', 3, 2); //make men last child of women + + \NestedSetBootstrap::validateTree(1); + + $this->assertNode(1, 1, 22, 0, 1); + $this->assertNode(3, 2, 21, 1, 1); + $this->assertNode(7, 3, 8, 2, 1); + $this->assertNode(10, 4, 5, 3, 1); + $this->assertNode(11, 6, 7, 3, 1); + $this->assertNode(8, 9, 10, 2, 1); + $this->assertNode(9, 11, 12, 2, 1); + $this->assertNode(2, 13, 20, 2, 1); + $this->assertNode(4, 14, 19, 3, 1); + $this->assertNode(5, 15, 16, 4, 1); + $this->assertNode(6, 17, 18, 4, 1); + } + public function test_move_as_last_child_throws() { \NestedSetBootstrap::insertDemoTree(); diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php index 0d4da53..b143103 100644 --- a/tests/_bootstrap.php +++ b/tests/_bootstrap.php @@ -96,11 +96,11 @@ public static function insertDemoTree(int $rootId = 1) $data = [ [1, 1, 22, 0, 'Clothing'], [2, 2, 9, 1, 'Mens'], - [ 4, 3, 8, 2, 'Suits'], - [ 5, 4, 5, 3, 'Slacks'], - [ 6, 6, 7, 3, 'Jackets'], - [ 3, 10, 21, 1, 'Women'], - [ 7, 11, 16, 2, 'Dresses'], + [4, 3, 8, 2, 'Suits'], + [5, 4, 5, 3, 'Slacks'], + [6, 6, 7, 3, 'Jackets'], + [3, 10, 21, 1, 'Women'], + [7, 11, 16, 2, 'Dresses'], [10, 12, 13, 3, 'Evening Growns'], [11, 14, 15, 3, 'Sun Dresses'], [8, 17, 18, 2, 'Skirts'],