Skip to content

Commit 970a7cc

Browse files
committed
WIP for fix with page layouts
1 parent ef1c7fc commit 970a7cc

12 files changed

Lines changed: 168 additions & 13 deletions

File tree

Controller/Index/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function execute(): ResultInterface|ResponseInterface
5151
{
5252
$data = $this->requestDataLoader->load();
5353
$this->requestDataLoader->mergeRequestParams();
54-
$layout = $this->layoutLoader->load($data['handles'], true);
54+
$layout = $this->layoutLoader->load($data['handles'], [], true);
5555

5656
$updates = $this->getComponentUpdates($data['updates'], $layout);
5757
$updates = $this->sortUpdates($updates);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3+
<module name="LokiTest_CustomPageLayout"/>
4+
</config>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Magento\Framework\Component\ComponentRegistrar;
5+
6+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'LokiTest_CustomPageLayout', __DIR__);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
4+
<body>
5+
<referenceContainer name="loki-test-container">
6+
<block name="loki-test-block"/>
7+
</referenceContainer>
8+
</body>
9+
</page>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
3+
<update handle="1column"/>
4+
<referenceContainer name="columns">
5+
<container name="loki-test-container"/>
6+
</referenceContainer>
7+
</layout>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Test\Integration\Util\Controller;
4+
5+
use Loki\Components\Util\Controller\LayoutLoader;
6+
use Loki\Components\Util\Controller\TargetRenderer;
7+
use Magento\Framework\App\ObjectManager;
8+
use PHPUnit\Framework\TestCase;
9+
10+
abstract class AbstractTargetRendererTestCase extends TestCase
11+
{
12+
protected function getLayoutLoader(): LayoutLoader
13+
{
14+
return ObjectManager::getInstance()->get(LayoutLoader::class);
15+
}
16+
17+
protected function getTargetRenderer(): TargetRenderer
18+
{
19+
return ObjectManager::getInstance()->get(TargetRenderer::class);
20+
}
21+
}

Test/Integration/Util/Controller/LayoutLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php declare(strict_types=1);
22

3-
namespace Loki\Components\Util\Controller;
3+
namespace Loki\Components\Test\Integration\Util\Controller;
44

5+
use Loki\Components\Util\Controller\LayoutLoader;
56
use Magento\Framework\App\ObjectManager;
67
use PHPUnit\Framework\TestCase;
78

Test/Integration/Util/Controller/RequestDataLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php declare(strict_types=1);
22

3-
namespace Loki\Components\Util\Controller;
3+
namespace Loki\Components\Test\Integration\Util\Controller;
44

55
use Laminas\Http\Headers;
6+
use Loki\Components\Util\Controller\RequestDataLoader;
67
use Magento\Framework\App\ObjectManager;
78
use Magento\Framework\App\Request\Http as HttpRequest;
89
use PHPUnit\Framework\TestCase;

Test/Integration/Util/Controller/TargetRendererTest.php

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,66 @@
66
use Loki\Components\Util\Controller\TargetRenderer;
77
use Magento\Framework\App\ObjectManager;
88
use Magento\TestFramework\Fixture\AppArea;
9+
use Magento\TestFramework\Fixture\AppIsolation;
10+
use PHPUnit\Framework\Attributes\DataProvider;
911
use PHPUnit\Framework\TestCase;
12+
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsEnabled;
13+
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsRegistered;
1014

1115
#[AppArea('frontend')]
1216
class TargetRendererTest extends TestCase
1317
{
14-
public function testRender(): void
18+
use AssertModuleIsRegistered;
19+
use AssertModuleIsEnabled;
20+
21+
#[DataProvider('getTestData')]
22+
#[AppIsolation(true)]
23+
public function testRenderWithoutIsolation(string $blockName, array $handles, array $pageHandles): void
1524
{
16-
$om = ObjectManager::getInstance();
17-
$layoutLoader = $om->get(LayoutLoader::class);
18-
$layout = $layoutLoader->load(['loki_components'], true);
25+
$layout = $this->getLayoutLoader()->load($handles, $pageHandles);
26+
27+
$allBlockNames = array_keys($layout->getAllBlocks());
28+
$this->assertNotEmpty($allBlockNames);
29+
30+
$debugMsg = 'Blocks: '.implode(', ', $allBlockNames);
31+
$this->assertContains($blockName, $allBlockNames, $debugMsg);
32+
33+
$htmlParts = $this->getTargetRenderer()->render($layout, [$blockName]);
34+
$this->assertNotEmpty($htmlParts);
35+
}
36+
37+
#[DataProvider('getTestData')]
38+
#[AppIsolation(true)]
39+
public function testRenderWithIsolation(string $blockName, array $handles, array $pageHandles): void
40+
{
41+
$layout = $this->getLayoutLoader()->load($handles, $pageHandles, true);
42+
43+
$allBlockNames = array_keys($layout->getAllBlocks());
44+
$this->assertNotEmpty($allBlockNames);
1945

20-
/** @var TargetRenderer $targetRenderer */
21-
$targetRenderer = $om->get(TargetRenderer::class);
22-
$htmlParts = $targetRenderer->render($layout, ['loki-messages'], true);
46+
$debugMsg = 'Blocks: '.implode(', ', $allBlockNames);
47+
$this->assertContains($blockName, $allBlockNames, $debugMsg);
2348

49+
$htmlParts = $this->getTargetRenderer()->render($layout, [$blockName], true);
2450
$this->assertNotEmpty($htmlParts);
2551
}
52+
53+
public static function getTestData(): array
54+
{
55+
return [
56+
['loki-components.utils.local-messages', ['loki_messages'], []],
57+
['loki.messages', ['default', 'loki_messages'], []],
58+
['footer_links', ['default'], []],
59+
];
60+
}
61+
62+
private function getLayoutLoader(): LayoutLoader
63+
{
64+
return ObjectManager::getInstance()->get(LayoutLoader::class);
65+
}
66+
67+
private function getTargetRenderer(): TargetRenderer
68+
{
69+
return ObjectManager::getInstance()->get(TargetRenderer::class);
70+
}
2671
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\Components\Test\Integration\Util\Controller;
4+
5+
use Magento\Framework\Module\ModuleList;
6+
use Magento\TestFramework\Fixture\AppArea;
7+
use Magento\TestFramework\Fixture\AppIsolation;
8+
use Magento\TestFramework\Fixture\ComponentsDir;
9+
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsEnabled;
10+
use Yireo\IntegrationTestHelper\Test\Integration\Traits\AssertModuleIsRegistered;
11+
use Magento\Framework\Module\Status as ModuleStatus;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\Framework\Module\Manager as ModuleManager;
14+
use Magento\Framework\Module\ModuleList\Loader as ModuleListLoader;
15+
16+
#[AppArea('frontend')]
17+
#[ComponentsDir('../../../../vendor/loki/magento2-components/Test/Integration/TestModules')]
18+
class TargetRendererWithCustomPageLayoutTest extends AbstractTargetRendererTestCase
19+
{
20+
use AssertModuleIsRegistered;
21+
use AssertModuleIsEnabled;
22+
23+
#[AppIsolation(true)]
24+
public function testCustomPageLayout(): void
25+
{
26+
//$this->enableModule('LokiTest_CustomPageLayout');
27+
28+
$handles = ['default'];
29+
$pageHandles = ['loki_test'];
30+
$blockName = 'loki-test-block';
31+
$layout = $this->getLayoutLoader()->load($handles, $pageHandles, false);
32+
33+
$allBlockNames = array_keys($layout->getAllBlocks());
34+
$this->assertNotEmpty($allBlockNames);
35+
36+
$debugMsg = 'Blocks: '.implode(', ', $allBlockNames);
37+
$this->assertContains($blockName, $allBlockNames, $debugMsg);
38+
39+
$htmlParts = $this->getTargetRenderer()->render($layout, [$blockName], false);
40+
$this->assertNotEmpty($htmlParts);
41+
}
42+
43+
private function enableModule(string $moduleName = ''): void
44+
{
45+
$this->assertModuleIsRegistered($moduleName);
46+
47+
$objectManager = Bootstrap::getObjectManager();
48+
$moduleStatus = $objectManager->get(ModuleStatus::class);
49+
50+
$moduleStatus->setIsEnabled(true, [
51+
$moduleName,
52+
]);
53+
54+
Bootstrap::getInstance()->reinitialize();
55+
56+
$this->assertModuleIsEnabled($moduleName);
57+
$this->assertModuleIsEnabled('Magento_Theme');
58+
}
59+
}

0 commit comments

Comments
 (0)