Skip to content

Commit b0cd906

Browse files
authored
Merge pull request #15 from previousnext/fix-get-tree
Fixes as follows:
2 parents 93d68da + c98f146 commit b0cd906

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

src/Storage/DbalNestedSet.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function getTree() {
211211
$tree = [];
212212
$stmt = $this->connection->executeQuery('SELECT id, revision_id, left_pos, right_pos, depth FROM ' . $this->tableName . ' ORDER BY left_pos');
213213
while ($row = $stmt->fetch()) {
214-
$tree[] = new Node($row['id'], $row['revision_id'], $row['left_pos'], $row['right_pos'], $row['depth']);
214+
$tree[] = new Node(new NodeKey($row['id'], $row['revision_id']), $row['left_pos'], $row['right_pos'], $row['depth']);
215215
}
216216
return $tree;
217217
}
@@ -308,23 +308,23 @@ public function moveSubTreeToRoot(Node $node) {
308308
*/
309309
public function moveSubTreeBelow(Node $target, Node $node) {
310310
$newLeftPosition = $target->getLeft() + 1;
311-
$this->moveSubTreeToPosition($newLeftPosition, $node);
311+
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth() + 1);
312312
}
313313

314314
/**
315315
* {@inheritdoc}
316316
*/
317317
public function moveSubTreeBefore(Node $target, Node $node) {
318318
$newLeftPosition = $target->getLeft();
319-
$this->moveSubTreeToPosition($newLeftPosition, $node);
319+
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
320320
}
321321

322322
/**
323323
* {@inheritdoc}
324324
*/
325325
public function moveSubTreeAfter(Node $target, Node $node) {
326326
$newLeftPosition = $target->getRight() + 1;
327-
$this->moveSubTreeToPosition($newLeftPosition, $node);
327+
$this->moveSubTreeToPosition($newLeftPosition, $node, $target->getDepth());
328328
}
329329

330330
/**
@@ -334,11 +334,13 @@ public function moveSubTreeAfter(Node $target, Node $node) {
334334
* The new left position.
335335
* @param \PNX\NestedSet\Node $node
336336
* The node to move.
337+
* @param int $newDepth
338+
* Depth of new position.
337339
*
338340
* @throws \Exception
339341
* If a transaction error occurs.
340342
*/
341-
protected function moveSubTreeToPosition($newLeftPosition, Node $node) {
343+
protected function moveSubTreeToPosition($newLeftPosition, Node $node, $newDepth) {
342344
try {
343345
// Calculate position adjustment variables.
344346
$width = $node->getRight() - $node->getLeft() + 1;
@@ -348,8 +350,7 @@ protected function moveSubTreeToPosition($newLeftPosition, Node $node) {
348350
$this->connection->beginTransaction();
349351

350352
// Calculate depth difference.
351-
$newNode = $this->getNodeAtPosition($newLeftPosition);
352-
$depthDiff = $newNode->getDepth() - $node->getDepth();
353+
$depthDiff = $newDepth - $node->getDepth();
353354

354355
// Backwards movement must account for new space.
355356
if ($distance < 0) {

tests/Functional/DbalNestedSetTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,81 @@ public function testMoveSubTreeBelow() {
346346

347347
}
348348

349+
/**
350+
* Tests moving a sub-tree under a brand new parent node.
351+
*/
352+
public function testMoveSubTreeBelowEndParentNode() {
353+
354+
$parent = $this->nestedSet->getNode(new NodeKey(1, 1));
355+
$nodeKey = new NodeKey(7, 1);
356+
$node = $this->nestedSet->getNode($nodeKey);
357+
358+
$newRoot = $this->nestedSet->addRootNode(new NodeKey(12, 1));
359+
$this->nestedSet->moveSubTreeBelow($newRoot, $node);
360+
361+
// Check node is in new position.
362+
$node = $this->nestedSet->getNode($nodeKey);
363+
$this->assertEquals(18, $node->getLeft());
364+
$this->assertEquals(23, $node->getRight());
365+
$this->assertEquals(1, $node->getDepth());
366+
367+
// Check children are in new position.
368+
$node = $this->nestedSet->getNode(new NodeKey(10, 1));
369+
$this->assertEquals(19, $node->getLeft());
370+
$this->assertEquals(20, $node->getRight());
371+
$this->assertEquals(2, $node->getDepth());
372+
373+
$node = $this->nestedSet->getNode(new NodeKey(11, 1));
374+
$this->assertEquals(21, $node->getLeft());
375+
$this->assertEquals(22, $node->getRight());
376+
$this->assertEquals(2, $node->getDepth());
377+
378+
// Check old parent is updated.
379+
$node = $this->nestedSet->getNode(new NodeKey(3, 1));
380+
$this->assertEquals(10, $node->getLeft());
381+
$this->assertEquals(15, $node->getRight());
382+
$this->assertEquals(1, $node->getDepth());
383+
384+
}
385+
386+
/**
387+
* Tests moving a sub-tree under a brand new child node.
388+
*/
389+
public function testMoveSubTreeBelowEndChildNode() {
390+
391+
$parent = $this->nestedSet->getNode(new NodeKey(1, 1));
392+
$nodeKey = new NodeKey(7, 1);
393+
$node = $this->nestedSet->getNode($nodeKey);
394+
395+
$newRoot = $this->nestedSet->addRootNode(new NodeKey(12, 1));
396+
$newChild = $this->nestedSet->addNodeBelow($newRoot, new NodeKey(13, 1));
397+
$this->nestedSet->moveSubTreeBelow($newChild, $node);
398+
399+
// Check node is in new position.
400+
$node = $this->nestedSet->getNode($nodeKey);
401+
$this->assertEquals(19, $node->getLeft());
402+
$this->assertEquals(24, $node->getRight());
403+
$this->assertEquals(2, $node->getDepth());
404+
405+
// Check children are in new position.
406+
$node = $this->nestedSet->getNode(new NodeKey(10, 1));
407+
$this->assertEquals(20, $node->getLeft());
408+
$this->assertEquals(21, $node->getRight());
409+
$this->assertEquals(3, $node->getDepth());
410+
411+
$node = $this->nestedSet->getNode(new NodeKey(11, 1));
412+
$this->assertEquals(22, $node->getLeft());
413+
$this->assertEquals(23, $node->getRight());
414+
$this->assertEquals(3, $node->getDepth());
415+
416+
// Check old parent is updated.
417+
$node = $this->nestedSet->getNode(new NodeKey(3, 1));
418+
$this->assertEquals(10, $node->getLeft());
419+
$this->assertEquals(15, $node->getRight());
420+
$this->assertEquals(1, $node->getDepth());
421+
422+
}
423+
349424
/**
350425
* Tests moving a sub-tree to the root.
351426
*/
@@ -461,6 +536,13 @@ public function testValidateTableInvalidFirstChars() {
461536
$this->nestedSet = new DbalNestedSet($this->connection, "1abc");
462537
}
463538

539+
/**
540+
* Test get tree works.
541+
*/
542+
public function testGetTree() {
543+
$this->assertCount(11, $this->nestedSet->getTree());
544+
}
545+
464546
/**
465547
* Loads the test data.
466548
*/

0 commit comments

Comments
 (0)