From 4f5d83fecad04dd1392ac394bf8cf6894002f9b9 Mon Sep 17 00:00:00 2001 From: Hendrik Bugdoll Date: Mon, 5 Feb 2024 18:15:12 +0100 Subject: [PATCH 1/2] Added new unit tests for block envOptions --- tests/data/blocks/TestBlock.php | 7 +- tests/data/blocks/import/TestBlock.php | 7 +- .../base/BlockPlaceholderIterationTest.php | 205 +++++++++++++++++- tests/src/base/BlockTest.php | 45 +++- 4 files changed, 254 insertions(+), 10 deletions(-) diff --git a/tests/data/blocks/TestBlock.php b/tests/data/blocks/TestBlock.php index 3e3d458e..857cda32 100644 --- a/tests/data/blocks/TestBlock.php +++ b/tests/data/blocks/TestBlock.php @@ -33,6 +33,11 @@ public function config() public function admin() { - return [$this->getVarValue('var1'), $this->getVarValue('var2')]; + return [ + $this->getVarValue('var1'), + $this->getVarValue('var2'), + $this->getCfgValue('cfg1'), + $this->getEnvOptions(), + ]; } } diff --git a/tests/data/blocks/import/TestBlock.php b/tests/data/blocks/import/TestBlock.php index 0cad668b..84b220b1 100644 --- a/tests/data/blocks/import/TestBlock.php +++ b/tests/data/blocks/import/TestBlock.php @@ -33,6 +33,11 @@ public function config() public function admin() { - return [$this->getVarValue('var1'), $this->getVarValue('var2')]; + return [ + $this->getVarValue('var1'), + $this->getVarValue('var2'), + $this->getCfgValue('cfg1'), + $this->getEnvOptions(), + ]; } } diff --git a/tests/src/base/BlockPlaceholderIterationTest.php b/tests/src/base/BlockPlaceholderIterationTest.php index b49ee288..adfc7530 100644 --- a/tests/src/base/BlockPlaceholderIterationTest.php +++ b/tests/src/base/BlockPlaceholderIterationTest.php @@ -35,7 +35,7 @@ public function testRenderPlaceholderIteration() 'group_id' => 1, 'class' => TestingLayoutBlock::class, 'is_disabled' => 0, - ] + ], ] ]); @@ -48,7 +48,7 @@ public function testRenderPlaceholderIteration() 'nav_item_id' => 1, 'timestamp_create' => time(), 'version_alias' => 'barfoo', - ] + ], ] ]); @@ -93,12 +93,187 @@ public function testRenderPlaceholderIteration() $block1 = $blockFixture->getModel('block1'); $block2 = $blockFixture->getModel('block2'); + $block3 = $blockFixture->getModel('block3'); $page = $pageFixture->getModel('page1'); $blockItem1 = $blockItemFixture->getModel('item1'); $blockItem2 = $blockItemFixture->getModel('item2'); $this->assertSame('
foo
bar
', $page->renderPlaceholder('content')); } + + public function testEnvOptionsPlaceholderIteration() + { + $this->app->setComponents([ + 'db' => [ + 'class' => 'yii\db\Connection', + 'dsn' => 'sqlite::memory:', + ] + ]); + + $blockFixture = new NgRestModelFixture([ + 'modelClass' => Block::class, + 'fixtureData' => [ + 'block1' => [ + 'id' => 1, + 'group_id' => 1, + 'class' => TestingEnvOptionsBlock::class, + 'is_disabled' => 0, + ], + ] + ]); + + $pageFixture = new ActiveRecordFixture([ + 'modelClass' => NavItemPage::class, + 'fixtureData' => [ + 'page1' => [ + 'id' => 1, + 'layout_id' => 1, + 'nav_item_id' => 1, + 'timestamp_create' => time(), + 'version_alias' => 'barfoo', + ], + ] + ]); + + $blockItemFixture = new NgRestModelFixture([ + 'modelClass' => NavItemPageBlockItem::class, + 'fixtureData' => [ + 'item1' => [ + 'id' => 1, + 'block_id' => 1, + 'placeholder_var' => 'content', + 'nav_item_page_id' => 1, + 'prev_id' => 0, + 'json_config_values' => '{}', + 'json_config_cfg_values' => '{}', + 'variation' => '', + 'is_hidden' => 0, + ], + 'item2' => [ + 'id' => 2, + 'block_id' => 1, + 'placeholder_var' => 'content', + 'nav_item_page_id' => 1, + 'prev_id' => 0, + 'json_config_values' => '{}', + 'json_config_cfg_values' => '{}', + 'variation' => '', + 'is_hidden' => 0, + ], + 'item3' => [ + 'id' => 3, + 'block_id' => 1, + 'placeholder_var' => 'content', + 'nav_item_page_id' => 1, + 'prev_id' => 0, + 'json_config_values' => '{}', + 'json_config_cfg_values' => '{}', + 'variation' => '', + 'is_hidden' => 0, + ], + ] + ]); + + $block1 = $blockFixture->getModel('block1'); + $page = $pageFixture->getModel('page1'); + $blockItem1 = $blockItemFixture->getModel('item1'); + $blockItem2 = $blockItemFixture->getModel('item2'); + $blockItem3 = $blockItemFixture->getModel('item3'); + + // admin: + + $adminBlockItems = NavItemPage::getPlaceholder('content', 0, $page); + + $envOptions1 = $adminBlockItems[0]['twig_admin']; + $envOptions2 = $adminBlockItems[1]['twig_admin']; + $envOptions3 = $adminBlockItems[2]['twig_admin']; + + $this->assertSame(1, $envOptions1['id']); + $this->assertSame(1, $envOptions1['blockId']); + $this->assertSame('admin', $envOptions1['context']); + //@TODO assertInstanceOf() for pageObject + $this->assertNotEquals(false, $envOptions1['pageObject']); + $this->assertSame(1, $envOptions1['index']); + $this->assertSame(3, $envOptions1['itemsCount']); + $this->assertTrue($envOptions1['isFirst']); + $this->assertFalse($envOptions1['isLast']); + $this->assertFalse($envOptions1['isPrevEqual']); + $this->assertTrue($envOptions1['isNextEqual']); + $this->assertSame(1, $envOptions1['equalIndex']); + + $this->assertSame(2, $envOptions2['id']); + $this->assertSame(1, $envOptions2['blockId']); + $this->assertSame('admin', $envOptions2['context']); + //@TODO assertInstanceOf() for pageObject + $this->assertNotEquals(false, $envOptions2['pageObject']); + $this->assertSame(2, $envOptions2['index']); + $this->assertSame(3, $envOptions2['itemsCount']); + $this->assertFalse($envOptions2['isFirst']); + $this->assertFalse($envOptions2['isLast']); + $this->assertTrue($envOptions2['isPrevEqual']); + $this->assertTrue($envOptions2['isNextEqual']); + $this->assertSame(2, $envOptions2['equalIndex']); + + $this->assertSame(3, $envOptions3['id']); + $this->assertSame(1, $envOptions3['blockId']); + $this->assertSame('admin', $envOptions3['context']); + //@TODO assertInstanceOf() for pageObject + $this->assertNotEquals(false, $envOptions2['pageObject']); + $this->assertSame(3, $envOptions3['index']); + $this->assertSame(3, $envOptions3['itemsCount']); + $this->assertFalse($envOptions3['isFirst']); + $this->assertTrue($envOptions3['isLast']); + $this->assertTrue($envOptions3['isPrevEqual']); + $this->assertFalse($envOptions3['isNextEqual']); + $this->assertSame(3, $envOptions3['equalIndex']); + + // frontend: + + $frontendEnvOptions = json_decode($page->renderPlaceholder('content'), true); + + $envOptions1 = $frontendEnvOptions[0]; + $envOptions2 = $frontendEnvOptions[1]; + $envOptions3 = $frontendEnvOptions[2]; + + $this->assertSame(1, $envOptions1['id']); + $this->assertSame(1, $envOptions1['blockId']); + $this->assertSame('frontend', $envOptions1['context']); + //@TODO assertInstanceOf() for pageObject + $this->assertNotEquals(false, $envOptions1['pageObject']); + $this->assertSame(1, $envOptions1['index']); + $this->assertSame(3, $envOptions1['itemsCount']); + $this->assertTrue($envOptions1['isFirst']); + $this->assertFalse($envOptions1['isLast']); + $this->assertFalse($envOptions1['isPrevEqual']); + $this->assertTrue($envOptions1['isNextEqual']); + $this->assertSame(1, $envOptions1['equalIndex']); + + $this->assertSame(2, $envOptions2['id']); + $this->assertSame(1, $envOptions2['blockId']); + $this->assertSame('frontend', $envOptions2['context']); + //@TODO assertInstanceOf() for pageObject + $this->assertNotEquals(false, $envOptions2['pageObject']); + $this->assertSame(2, $envOptions2['index']); + $this->assertSame(3, $envOptions2['itemsCount']); + $this->assertFalse($envOptions2['isFirst']); + $this->assertFalse($envOptions2['isLast']); + $this->assertTrue($envOptions2['isPrevEqual']); + $this->assertTrue($envOptions2['isNextEqual']); + $this->assertSame(2, $envOptions2['equalIndex']); + + $this->assertSame(3, $envOptions3['id']); + $this->assertSame(1, $envOptions3['blockId']); + $this->assertSame('frontend', $envOptions3['context']); + //@TODO assertInstanceOf() for pageObject + $this->assertNotEquals(false, $envOptions3['pageObject']); + $this->assertSame(3, $envOptions3['index']); + $this->assertSame(3, $envOptions3['itemsCount']); + $this->assertFalse($envOptions3['isFirst']); + $this->assertTrue($envOptions3['isLast']); + $this->assertTrue($envOptions3['isPrevEqual']); + $this->assertFalse($envOptions3['isNextEqual']); + $this->assertSame(3, $envOptions3['equalIndex']); + } } class TestingTextBlock extends InternalBaseBlock @@ -107,6 +282,7 @@ public function name() { return 'Content'; } + public function config() { return [ @@ -115,10 +291,12 @@ public function config() ] ]; } + public function renderFrontend() { return '
'.$this->getVarValue('var1').'
'; } + public function renderAdmin() { return '
{{ vars.content | raw }}
'; @@ -154,3 +332,26 @@ public function placeholderRenderIteration(\luya\cms\base\BlockInterface $block) return ''.$block->renderFrontend().''; } } + +class TestingEnvOptionsBlock extends InternalBaseBlock +{ + public function name() + { + return 'EnvOptions'; + } + + public function config() + { + return []; + } + + public function renderFrontend() + { + return (($this->getEnvOption('isFirst')) ? '[' : '') . json_encode($this->getEnvOptions(), JSON_NUMERIC_CHECK) . (($this->getEnvOption('isLast')) ? ']' : ','); + } + + public function renderAdmin() + { + return $this->getEnvOptions(); + } +} diff --git a/tests/src/base/BlockTest.php b/tests/src/base/BlockTest.php index 0a909ab6..99856b15 100644 --- a/tests/src/base/BlockTest.php +++ b/tests/src/base/BlockTest.php @@ -5,6 +5,7 @@ use cmstests\CmsFrontendTestCase; use cmstests\data\blocks\TestBlock; use luya\cms\base\PhpBlock; +use yii\db\ActiveRecord; class GetterSetter extends PhpBlock { @@ -39,9 +40,8 @@ public function testBlockSetup() { $block = new TestBlock(); - $this->assertEquals(false, $block->isAdminContext()); - $this->assertEquals(false, $block->isFrontendContext()); - + $this->assertFalse($block->isAdminContext()); + $this->assertFalse($block->isFrontendContext()); foreach ($block->getConfigVarsExport() as $var) { $this->assertArrayHasKey('id', $var); @@ -68,14 +68,47 @@ public function testBlockValues() { $block = new TestBlock(); - $block->setEnvOption('blockId', 1); - $block->setEnvOption('context', 'admin'); - $block->setVarValues(['var1' => 'content var 1', 'var2' => 'content var 2']); $block->setCfgValues(['cfg1' => 'content cfg 1']); $this->assertEquals('content var 1', $block->admin()[0]); $this->assertEquals('content var 2', $block->admin()[1]); + $this->assertEquals('content cfg 1', $block->admin()[2]); + } + + public function testBlockEnvOptions() + { + $block = new TestBlock(); + $pageObject = new ActiveRecord(); // @TODO specify class + + $block->setEnvOption('id', 1); + $block->setEnvOption('blockId', 1); + $block->setEnvOption('context', 'admin'); + $block->setEnvOption('pageObject', $pageObject); + $block->setEnvOption('index', 1); + $block->setEnvOption('itemsCount', 1); + $block->setEnvOption('isFirst', true); + $block->setEnvOption('isLast', true); + $block->setEnvOption('isPrevEqual', false); + $block->setEnvOption('isNextEqual', false); + $block->setEnvOption('equalIndex', 1); + + $this->assertTrue($block->isAdminContext()); + $this->assertFalse($block->isFrontendContext()); + + $envOptions = $block->admin()[3]; + $this->assertSame(1, $envOptions['id']); + $this->assertSame(1, $envOptions['blockId']); + $this->assertSame('admin', $envOptions['context']); + // @TODO assertInstanceOf() for pageObject + $this->assertNotEquals(false, $envOptions['pageObject']); + $this->assertSame(1, $envOptions['index']); + $this->assertSame(1, $envOptions['itemsCount']); + $this->assertTrue($envOptions['isFirst']); + $this->assertTrue($envOptions['isLast']); + $this->assertFalse($envOptions['isPrevEqual']); + $this->assertFalse($envOptions['isNextEqual']); + $this->assertSame(1, $envOptions['equalIndex']); } public function testGetterSetter() From fac9cdfe178c67a95728a1c201cb945110d01da3 Mon Sep 17 00:00:00 2001 From: Hendrik Bugdoll Date: Mon, 5 Feb 2024 18:17:54 +0100 Subject: [PATCH 2/2] Fixed equalIndex iteration - Fixed recently introduced equalIndex iteration in admin context - Reduced lines of code in frontend context --- src/models/NavItemPage.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/models/NavItemPage.php b/src/models/NavItemPage.php index 096159fb..c7adca9a 100644 --- a/src/models/NavItemPage.php +++ b/src/models/NavItemPage.php @@ -298,13 +298,7 @@ private function renderPlaceholderRecursive($navItemPageId, $placeholderVar, $pr $prevIsEqual = array_key_exists($prev, $placeholders) && $placeholder['block_id'] == $placeholders[$prev]['block_id']; $blockObject->setEnvOption('isPrevEqual', $prevIsEqual); $blockObject->setEnvOption('isNextEqual', array_key_exists($next, $placeholders) && $placeholder['block_id'] == $placeholders[$next]['block_id']); - - if (!$prevIsEqual) { - $equalIndex = 1; - } else { - $equalIndex++; - } - $blockObject->setEnvOption('equalIndex', $equalIndex); + $blockObject->setEnvOption('equalIndex', (!$prevIsEqual) ? $equalIndex = 1 : ++$equalIndex); // render sub placeholders and set into object $insertedHolders = []; @@ -449,7 +443,7 @@ public static function getPlaceholder($placeholderVar, $prevId, NavItemPage $nav 'isLast' => ($i == $blocksCount), 'isPrevEqual' => $prevIsEqual, 'isNextEqual' => array_key_exists($next, $nav_item_page_block_item_data) && $blockItem['block_id'] == $nav_item_page_block_item_data[$next]['block_id'], - 'equalIndex' => (!$prevIsEqual) ? ($equalIndex = 1) : ($equalIndex++) + 'equalIndex' => (!$prevIsEqual) ? $equalIndex = 1 : ++$equalIndex ]; $item = self::getBlockItem($blockItem, $navItemPage, $envOptions);