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
55 changes: 55 additions & 0 deletions tests/NestedSetsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2271,4 +2271,59 @@ public function testProtectedMoveNodeAsRootRemainsAccessibleToSubclasses(): void
'\'moveNodeAsRoot\' method should remain protected to allow subclass customization.',
);
}

public function testChildrenMethodRequiresOrderByForCorrectTreeTraversal(): void
{
$this->createDatabase();

$command = $this->getDb()->createCommand();
$xml = $this->loadFixtureXML('test-disorder.xml');

$children = $xml->children();

self::assertNotNull(
$children,
'XML children should not be \'null\'.',
);

foreach ($children as $element => $treeElement) {
if ($element === 'tree') {
$command->insert(
'tree',
[
'name' => (string) $treeElement['name'],
'lft' => (int) $treeElement['lft'],
'rgt' => (int) $treeElement['rgt'],
'depth' => (int) $treeElement['depth'],
],
)->execute();
}
}

$root = Tree::findOne(1);

self::assertNotNull(
$root,
'Root node with ID \'1\' should exist in the database.',
);

$childrenList = $root->children()->all();
$expectedOrder = ['Child B', 'Child C', 'Child A'];

self::assertCount(
3,
$childrenList,
'Children list should contain exactly 3 elements.',
);

foreach ($childrenList as $index => $child) {
if (isset($expectedOrder[$index])) {
self::assertEquals(
$expectedOrder[$index],
$child->getAttribute('name'),
"Child at index {$index} should be {$expectedOrder[$index]} in correct \'lft\' order.",
);
}
}
}
}
17 changes: 17 additions & 0 deletions tests/support/data/test-disorder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<!-- Root node -->
<tree id="1" lft="1" rgt="8" depth="0" name="Root"/>

<!-- Children with INTENTIONALLY DISORDERED lft values to test ORDER BY dependency -->
<!-- Normal logical order would be: Child A(lft=2), Child B(lft=4), Child C(lft=6) -->
<!-- But we arrange them with swapped lft values to test if ORDER BY clause works -->
<!-- This child should be LAST in tree order (lft=6) but is inserted FIRST in XML -->
<tree id="2" lft="6" rgt="7" depth="1" name="Child A"/>

<!-- This child should be FIRST in tree order (lft=2) but is inserted SECOND in XML -->
<tree id="3" lft="2" rgt="3" depth="1" name="Child B"/>

<!-- This child should be MIDDLE in tree order (lft=4) but is inserted THIRD in XML -->
<tree id="4" lft="4" rgt="5" depth="1" name="Child C"/>
</dataset>
Loading