Skip to content

PHPUnit: added test for Mage_Core_Model_Layout::getBlockSingleton() #4431

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

Merged
merged 11 commits into from
Jan 5, 2025
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
1 change: 0 additions & 1 deletion .phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ parameters:
- app/code/core/Mage/Admin/Model/Acl/Assert/Time.php
- app/code/core/Mage/Api/Model/Acl/Assert/Ip.php
- app/code/core/Mage/Api/Model/Acl/Assert/Time.php
- app/code/core/Mage/Adminhtml/Block/Widget/Grid/Block.php
- app/code/core/Mage/Core/Model/Mysql4/Design/Theme/Collection.php
- lib/Varien/Directory/Collection.php
- lib/Varien/Directory/Factory.php
Expand Down
30 changes: 0 additions & 30 deletions app/code/core/Mage/Adminhtml/Block/Widget/Grid/Block.php

This file was deleted.

10 changes: 6 additions & 4 deletions app/code/core/Mage/Core/Model/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,12 @@ protected function _generateAction($node, $parent)
}

/**
* @codeCoverageIgnore
* @param string $method
* @param string[] $args
* @throws Mage_Core_Exception
* @deprecated
* @see Mage_Core_Helper_Security::validateAgainstBlockMethodBlacklist()
*/
protected function validateAgainstBlacklist(Mage_Core_Block_Abstract $block, $method, array $args)
{
Expand Down Expand Up @@ -503,12 +506,12 @@ protected function _getBlockInstance($block, array $attributes = [])
}
}
if (class_exists($block, false) || mageFindClassFile($block)) {
// phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation
$block = new $block($attributes);
}
}
if (!$block instanceof Mage_Core_Block_Abstract) {
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $block));
$block = is_object($block) ? get_class($block) : $block;
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s (not instance of Mage_Core_Block_Abstract)', $block));
}
return $block;
}
Expand Down Expand Up @@ -591,7 +594,7 @@ public function getMessagesBlock()

/**
* @param string $type
* @return Mage_Core_Block_Abstract
* @return Mage_Core_Block_Abstract|object
*/
public function getBlockSingleton($type)
{
Expand All @@ -601,7 +604,6 @@ public function getBlockSingleton($type)
Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $type));
}

// phpcs:ignore Ecg.Classes.ObjectInstantiation.DirectInstantiation
$helper = new $className();
if ($helper) {
if ($helper instanceof Mage_Core_Block_Abstract) {
Expand Down
1 change: 1 addition & 0 deletions app/locale/en_US/Mage_Core.csv
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"Invalid URL scheme.","Invalid URL scheme."
"Invalid base url type","Invalid base url type"
"Invalid block type: %s","Invalid block type: %s"
"Invalid block type: %s (not instance of Mage_Core_Block_Abstract)","Invalid block type: %s (not instance of Mage_Core_Block_Abstract)"
"Invalid block: %s","Invalid block: %s"
"Invalid connection","Invalid connection"
"Invalid date","Invalid date"
Expand Down
132 changes: 132 additions & 0 deletions tests/unit/Mage/Core/Model/LayoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?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\Model;

use Generator;
use Mage;
use Mage_Core_Model_Layout;
use OpenMage\Tests\Unit\Traits\PhpStormMetaData\BlocksTrait;
use PHPUnit\Framework\TestCase;

class LayoutTest extends TestCase
{
use BlocksTrait;

public Mage_Core_Model_Layout $subject;

public function setUp(): void
{
Mage::app();
$this->subject = Mage::getModel('core/layout');
}

/**
* @dataProvider provideCreateBlock
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testCreateBlock($expectedResult, bool $willReturnBlock, string $type, ?string $name, array $attributes): void
{
$result = $this->subject->createBlock($type, $name, $attributes);

if ($willReturnBlock) {
$this->assertInstanceOf($expectedResult, $result);
} else {
$this->assertFalse($result);
}
}

public function provideCreateBlock(): Generator
{
yield 'instance of Mage_Core_Block_Abstract' => [
\Mage_Cms_Block_Block::class,
true,
'cms/block',
null,
[],
];
yield 'not instance of Mage_Core_Block_Abstract' => [
false,
false,
'rule/conditions',
null,
[],
];
}

/**
* @covers Mage_Core_Model_Layout::getBlockSingleton()
* @dataProvider provideGetBlockSingleton
* @group Mage_Core
* @group Mage_Core_Model
* @group pr4411
*/
public function testGetBlockSingleton($expectedResult, bool $isAbstractBlock, string $type): void
{
$result = $this->subject->getBlockSingleton($type);

$this->assertInstanceOf($expectedResult, $result);

if ($isAbstractBlock) {
$this->assertInstanceOf(\Mage_Core_Block_Abstract::class, $result);
} else {
$this->assertNotInstanceOf(\Mage_Core_Block_Abstract::class, $result);
}
}

public function provideGetBlockSingleton(): Generator
{
$notInstanceOfMageCoreBlockAbstract = $this->getBlockClassesNotInstanceOfMageCoreBlockAbstract();

$ignoredClasses = array_merge(
$this->getAbstractBlockClasses(),
$this->getBlockClassesToMock(),
$this->getBlockClassesWithErrors(),
$this->getBlockClassesWithSessions(),
);

#$allBlocks = $this->getAllBlockClasses();
$allBlocks = [
'adminhtml/api_buttons' => \Mage_Adminhtml_Block_Api_Buttons::class,
'adminhtml/catalog_category_helper_pricestep' => \Mage_Adminhtml_Block_Catalog_Category_Helper_Pricestep::class,
];

foreach ($allBlocks as $alias => $className) {
if (!in_array($className, $ignoredClasses)) {
yield $className => [
$className,
!in_array($className, $notInstanceOfMageCoreBlockAbstract),
$alias,
];
}
}
}

/**
* @covers Mage_Core_Model_Layout::getBlockSingleton()
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetBlockSingletonError(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("Class 'Mage_Invalid_Block_Type' not found");

$this->subject->getBlockSingleton('invalid/type');
}
}
14 changes: 14 additions & 0 deletions tests/unit/Mage/Core/Model/WebsiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function setUp(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testLoad(): void
{
Expand All @@ -46,6 +47,7 @@ public function testLoad(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testLoadConfig(): void
{
Expand All @@ -55,6 +57,7 @@ public function testLoadConfig(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetStoreCollection(): void
{
Expand All @@ -63,6 +66,7 @@ public function testGetStoreCollection(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetGroupCollection(): void
{
Expand All @@ -71,6 +75,7 @@ public function testGetGroupCollection(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetStores(): void
{
Expand All @@ -79,6 +84,7 @@ public function testGetStores(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetStoreIds(): void
{
Expand All @@ -87,6 +93,7 @@ public function testGetStoreIds(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetStoreCodes(): void
{
Expand All @@ -95,6 +102,7 @@ public function testGetStoreCodes(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetStoresCount(): void
{
Expand All @@ -103,6 +111,7 @@ public function testGetStoresCount(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetGroups(): void
{
Expand All @@ -111,6 +120,7 @@ public function testGetGroups(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetGroupIds(): void
{
Expand All @@ -119,6 +129,7 @@ public function testGetGroupIds(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetGroupsCount(): void
{
Expand All @@ -127,6 +138,7 @@ public function testGetGroupsCount(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testGetBaseCurrency(): void
{
Expand All @@ -136,6 +148,7 @@ public function testGetBaseCurrency(): void

// /**
// * @group Mage_Core
// * @group Mage_Core_Model
// */
// public function testGetDefaultStore(): void
// {
Expand All @@ -154,6 +167,7 @@ public function testGetDefaultStoresSelect(): void

/**
* @group Mage_Core
* @group Mage_Core_Model
*/
public function testIsReadOnly(): void
{
Expand Down
Loading
Loading