Skip to content

Commit

Permalink
Fix regression bug for duplicate block rendering with getSortedChildr…
Browse files Browse the repository at this point in the history
…en() (#4480)

* Fix regression bug for duplicate block rendering with getSortedChildren()

* Fix code formatting via php-cs

---------

Co-authored-by: Sven Reichel <[email protected]>
  • Loading branch information
bucha and sreichel authored Jan 14, 2025
1 parent 1dabe2d commit ddb0149
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/code/core/Mage/Core/Block/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ public function setChild($alias, $block)

$block->setParentBlock($this);
$block->setBlockAlias($alias);
$this->unsetChild($alias);
$this->_children[$alias] = $block;
return $this;
}
Expand Down Expand Up @@ -705,6 +704,11 @@ public function insert($block, $siblingName = '', $after = false, $alias = '')
$this->setChild($name, $block);
}

$existingKey = array_search($name, $this->_sortedChildren);
if ($existingKey !== false) {
array_splice($this->_sortedChildren, $existingKey, 1);
}

if ($siblingName === '') {
if ($after) {
$this->_sortedChildren[] = $name;
Expand Down
23 changes: 21 additions & 2 deletions tests/unit/Mage/Core/Block/Text/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,33 @@ public function testUniqueBlockNameOrdering(): void
$parentBlock->insert($childBlockC, 'child_b', true);

$childBlockA = $layout->createBlock('core/text', 'child_a')->setText('A');
$parentBlock->insert($childBlockC, 'child_b', false);
$parentBlock->insert($childBlockA, 'child_b', false);

$childBlockB = $layout->createBlock('core/text', 'child_b')->setText('B');
$parentBlock->insert($childBlockC, 'child_a', true);
$parentBlock->insert($childBlockB, 'child_a', true);

$parentBlock->unsetChild('child_a');
$parentBlock->unsetChild('child_b');

$this->assertSame('CD', $parentBlock->toHtml());
}

public function testSortInstructionsAfterReplaceChild()
{
$layout = Mage::getModel('core/layout');

$parentBlock = $layout->createBlock('core/text_list', 'parent');

$childBlockA = $layout->createBlock('core/text', 'target_block')->setText('A');
$parentBlock->insert($childBlockA, '', false, 'child');

$childBlockB = $layout->createBlock('core/text', 'target_block')->setText('B');

// Replacing the block but keeping its order within the parent
$layout->unsetBlock('target_block');
$layout->setBlock('target_block', $childBlockB);
$parentBlock->setChild('child', $childBlockB);

$this->assertSame('B', $parentBlock->toHtml());
}
}

0 comments on commit ddb0149

Please sign in to comment.