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
15 changes: 8 additions & 7 deletions src/Storage/DbalNestedSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function getTree() {
$tree = [];
$stmt = $this->connection->executeQuery('SELECT id, revision_id, left_pos, right_pos, depth FROM ' . $this->tableName . ' ORDER BY left_pos');
while ($row = $stmt->fetch()) {
$tree[] = new Node($row['id'], $row['revision_id'], $row['left_pos'], $row['right_pos'], $row['depth']);
$tree[] = new Node(new NodeKey($row['id'], $row['revision_id']), $row['left_pos'], $row['right_pos'], $row['depth']);
}
return $tree;
}
Expand Down Expand Up @@ -308,23 +308,23 @@ public function moveSubTreeToRoot(Node $node) {
*/
public function moveSubTreeBelow(Node $target, Node $node) {
$newLeftPosition = $target->getLeft() + 1;
$this->moveSubTreeToPosition($newLeftPosition, $node);
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth() + 1);
}

/**
* {@inheritdoc}
*/
public function moveSubTreeBefore(Node $target, Node $node) {
$newLeftPosition = $target->getLeft();
$this->moveSubTreeToPosition($newLeftPosition, $node);
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
}

/**
* {@inheritdoc}
*/
public function moveSubTreeAfter(Node $target, Node $node) {
$newLeftPosition = $target->getRight() + 1;
$this->moveSubTreeToPosition($newLeftPosition, $node);
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
}

/**
Expand All @@ -334,11 +334,13 @@ public function moveSubTreeAfter(Node $target, Node $node) {
* The new left position.
* @param \PNX\NestedSet\Node $node
* The node to move.
* @param int $newDepth
* Depth of new position.
*
* @throws \Exception
* If a transaction error occurs.
*/
protected function moveSubTreeToPosition($newLeftPosition, Node $node) {
protected function moveSubTreeToPosition($newLeftPosition, Node $node, $newDepth) {
try {
// Calculate position adjustment variables.
$width = $node->getRight() - $node->getLeft() + 1;
Expand All @@ -348,8 +350,7 @@ protected function moveSubTreeToPosition($newLeftPosition, Node $node) {
$this->connection->beginTransaction();

// Calculate depth difference.
$newNode = $this->getNodeAtPosition($newLeftPosition);
$depthDiff = $newNode->getDepth() - $node->getDepth();
$depthDiff = $newDepth - $node->getDepth();

// Backwards movement must account for new space.
if ($distance < 0) {
Expand Down
82 changes: 82 additions & 0 deletions tests/Functional/DbalNestedSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,81 @@ public function testMoveSubTreeBelow() {

}

/**
* Tests moving a sub-tree under a brand new parent node.
*/
public function testMoveSubTreeBelowEndParentNode() {

$parent = $this->nestedSet->getNode(new NodeKey(1, 1));
$nodeKey = new NodeKey(7, 1);
$node = $this->nestedSet->getNode($nodeKey);

$newRoot = $this->nestedSet->addRootNode(new NodeKey(12, 1));
$this->nestedSet->moveSubTreeBelow($newRoot, $node);

// Check node is in new position.
$node = $this->nestedSet->getNode($nodeKey);
$this->assertEquals(18, $node->getLeft());
$this->assertEquals(23, $node->getRight());
$this->assertEquals(1, $node->getDepth());

// Check children are in new position.
$node = $this->nestedSet->getNode(new NodeKey(10, 1));
$this->assertEquals(19, $node->getLeft());
$this->assertEquals(20, $node->getRight());
$this->assertEquals(2, $node->getDepth());

$node = $this->nestedSet->getNode(new NodeKey(11, 1));
$this->assertEquals(21, $node->getLeft());
$this->assertEquals(22, $node->getRight());
$this->assertEquals(2, $node->getDepth());

// Check old parent is updated.
$node = $this->nestedSet->getNode(new NodeKey(3, 1));
$this->assertEquals(10, $node->getLeft());
$this->assertEquals(15, $node->getRight());
$this->assertEquals(1, $node->getDepth());

}

/**
* Tests moving a sub-tree under a brand new child node.
*/
public function testMoveSubTreeBelowEndChildNode() {

$parent = $this->nestedSet->getNode(new NodeKey(1, 1));
$nodeKey = new NodeKey(7, 1);
$node = $this->nestedSet->getNode($nodeKey);

$newRoot = $this->nestedSet->addRootNode(new NodeKey(12, 1));
$newChild = $this->nestedSet->addNodeBelow($newRoot, new NodeKey(13, 1));
$this->nestedSet->moveSubTreeBelow($newChild, $node);

// Check node is in new position.
$node = $this->nestedSet->getNode($nodeKey);
$this->assertEquals(19, $node->getLeft());
$this->assertEquals(24, $node->getRight());
$this->assertEquals(2, $node->getDepth());

// Check children are in new position.
$node = $this->nestedSet->getNode(new NodeKey(10, 1));
$this->assertEquals(20, $node->getLeft());
$this->assertEquals(21, $node->getRight());
$this->assertEquals(3, $node->getDepth());

$node = $this->nestedSet->getNode(new NodeKey(11, 1));
$this->assertEquals(22, $node->getLeft());
$this->assertEquals(23, $node->getRight());
$this->assertEquals(3, $node->getDepth());

// Check old parent is updated.
$node = $this->nestedSet->getNode(new NodeKey(3, 1));
$this->assertEquals(10, $node->getLeft());
$this->assertEquals(15, $node->getRight());
$this->assertEquals(1, $node->getDepth());

}

/**
* Tests moving a sub-tree to the root.
*/
Expand Down Expand Up @@ -461,6 +536,13 @@ public function testValidateTableInvalidFirstChars() {
$this->nestedSet = new DbalNestedSet($this->connection, "1abc");
}

/**
* Test get tree works.
*/
public function testGetTree() {
$this->assertCount(11, $this->nestedSet->getTree());
}

/**
* Loads the test data.
*/
Expand Down