Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate block rendering with getSortedChildren() #4337

Merged
merged 4 commits into from
Nov 8, 2024
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
4 changes: 2 additions & 2 deletions app/code/core/Mage/Core/Block/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ public function setChild($alias, $block)

$block->setParentBlock($this);
$block->setBlockAlias($alias);
$this->unsetChild($alias);
$this->_children[$alias] = $block;
return $this;
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Expand Down
103 changes: 103 additions & 0 deletions tests/unit/Mage/Core/Block/Text/ListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* OpenMage
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available at https://opensource.org/license/osl-3-0-php
*
* @category OpenMage
* @package OpenMage_Tests
* @copyright Copyright (c) 2024 The OpenMage Contributors (https://www.openmage.org)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace OpenMage\Tests\Unit\Mage\Core\Block\Text;

use Mage;
use PHPUnit\Framework\TestCase;

class ListTest extends TestCase
{
public function setUp(): void
{
Mage::app();
}

/**
* @group Mage_Core
* @group Mage_Core_Block
*/
public function testDuplicateBlockName(): void
{
$layout = Mage::getModel('core/layout');

$parentBlock = $layout->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());
}
}
Loading