@@ -735,8 +735,11 @@ public function printTree($tree) {
735735
736736 /**
737737 * Assert node correctly moved below.
738+ *
739+ * @param int $parentOffset
740+ * (optional) Optional offset for old parent.
738741 */
739- protected function assertNodeMovedBelow () {
742+ protected function assertNodeMovedBelow ($ parentOffset = 0 ) {
740743 // Check node is in new position.
741744 $ node = $ this ->nestedSet ->getNode (new NodeKey (7 , 1 ));
742745 $ this ->assertEquals (2 , $ node ->getLeft ());
@@ -756,7 +759,7 @@ protected function assertNodeMovedBelow() {
756759
757760 // Check old parent is updated.
758761 $ node = $ this ->nestedSet ->getNode (new NodeKey (3 , 1 ));
759- $ this ->assertEquals (16 , $ node ->getLeft ());
762+ $ this ->assertEquals (16 + $ parentOffset , $ node ->getLeft ());
760763 $ this ->assertEquals (21 , $ node ->getRight ());
761764 $ this ->assertEquals (1 , $ node ->getDepth ());
762765 }
@@ -789,4 +792,32 @@ protected function assertNodeMovedBefore() {
789792 $ this ->assertEquals (1 , $ node ->getDepth ());
790793 }
791794
795+ /**
796+ * Tests moving a sub-tree under a parent node.
797+ */
798+ public function testSimultaneousMoveSubTreeBelow () {
799+ $ parent = $ this ->nestedSet ->getNode (new NodeKey (1 , 1 ));
800+ $ nodeKey = new NodeKey (7 , 1 );
801+ $ node = $ this ->nestedSet ->getNode ($ nodeKey );
802+
803+ // We are planning to move node 7 below node 1, but at the same time,
804+ // let's move node 8 around a bit.
805+ $ otherNodeKey = new NodeKey (8 , 1 );
806+ $ otherNode = $ this ->nestedSet ->getNode ($ otherNodeKey );
807+
808+ // Now we move node 8 around.
809+ $ this ->nestedSet ->moveSubTreeBelow ($ parent , $ otherNode );
810+
811+ // And then we move node 7 around, but at this point our Node object has
812+ // values for left and right that are stale.
813+ $ this ->nestedSet ->moveSubTreeBelow ($ parent , $ node );
814+
815+ // Check nodes are in the correct positions.
816+ $ this ->assertNodeMovedBelow (2 );
817+ $ node = $ this ->nestedSet ->getNode (new NodeKey (8 , 1 ));
818+ $ this ->assertEquals (8 , $ node ->getLeft ());
819+ $ this ->assertEquals (9 , $ node ->getRight ());
820+ $ this ->assertEquals (1 , $ node ->getDepth ());
821+ }
822+
792823}
0 commit comments