diff --git a/app/code/core/Mage/Core/Block/Abstract.php b/app/code/core/Mage/Core/Block/Abstract.php index adf82c0834a..f1b433066bf 100644 --- a/app/code/core/Mage/Core/Block/Abstract.php +++ b/app/code/core/Mage/Core/Block/Abstract.php @@ -464,6 +464,7 @@ public function setChild($alias, $block) $block->setParentBlock($this); $block->setBlockAlias($alias); + $this->unsetChild($alias); $this->_children[$alias] = $block; return $this; } @@ -482,7 +483,7 @@ public function unsetChild($alias) unset($this->_children[$alias]); $key = array_search($name, $this->_sortedChildren); if ($key !== false) { - unset($this->_sortedChildren[$key]); + array_splice($this->_sortedChildren, $key, 1); } } @@ -741,7 +742,6 @@ public function insert($block, $siblingName = '', $after = false, $alias = '') */ public function sortChildren($force = false) { - $this->_sortedChildren = array_values($this->_sortedChildren); // reset indexes which might have gaps after unsetting blocks foreach ($this->_sortInstructions as $name => $list) { list($siblingName, $after, $exists) = $list; if ($exists && !$force) { diff --git a/tests/unit/Mage/Core/Block/Text/ListTest.php b/tests/unit/Mage/Core/Block/Text/ListTest.php new file mode 100644 index 00000000000..e46f856e4f6 --- /dev/null +++ b/tests/unit/Mage/Core/Block/Text/ListTest.php @@ -0,0 +1,103 @@ +createBlock('core/text_list', 'parent'); + + $childBlockA = $layout->createBlock('core/text', 'child_a')->setText('A1'); + $parentBlock->append($childBlockA); + + $childBlockA = $layout->createBlock('core/text', 'child_a')->setText('A2'); + $parentBlock->append($childBlockA); + + $this->assertSame('A2', $parentBlock->toHtml()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Block + */ + public function testDuplicateBlockNameOrdering(): void + { + $layout = Mage::getModel('core/layout'); + + $parentBlock = $layout->createBlock('core/text_list', 'parent'); + + $childBlockA = $layout->createBlock('core/text', 'child_a')->setText('A'); + $parentBlock->append($childBlockA); + + $childBlockB = $layout->createBlock('core/text', 'child_b')->setText('B'); + $parentBlock->append($childBlockB); + + $childBlockC = $layout->createBlock('core/text', 'child_c')->setText('C'); + $parentBlock->append($childBlockC); + + $parentBlock->unsetChild('child_b'); + + $childBlockB = $layout->createBlock('core/text', 'child_b')->setText('B'); + $parentBlock->insert($childBlockB, 'child_c', false); + + $this->assertSame('ABC', $parentBlock->toHtml()); + } + + /** + * @group Mage_Core + * @group Mage_Core_Block + */ + public function testUniqueBlockNameOrdering(): void + { + $layout = Mage::getModel('core/layout'); + + $parentBlock = $layout->createBlock('core/text_list', 'parent'); + + $childBlockD = $layout->createBlock('core/text', 'child_d')->setText('D'); + $parentBlock->insert($childBlockD, 'child_c', true); + + $childBlockC = $layout->createBlock('core/text', 'child_c')->setText('C'); + $parentBlock->insert($childBlockC, 'child_b', true); + + $childBlockA = $layout->createBlock('core/text', 'child_a')->setText('A'); + $parentBlock->insert($childBlockC, 'child_b', false); + + $childBlockB = $layout->createBlock('core/text', 'child_b')->setText('B'); + $parentBlock->insert($childBlockC, 'child_a', true); + + $parentBlock->unsetChild('child_a'); + $parentBlock->unsetChild('child_b'); + + $this->assertSame('CD', $parentBlock->toHtml()); + } +}