Skip to content

Commit

Permalink
Merge pull request #407 from hbugdoll/unit-tests-env-options
Browse files Browse the repository at this point in the history
Unit tests for block env options
  • Loading branch information
nadar authored Feb 6, 2024
2 parents e8afe25 + fac9cdf commit 5d59cc6
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 18 deletions.
10 changes: 2 additions & 8 deletions src/models/NavItemPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion tests/data/blocks/TestBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
}
}
7 changes: 6 additions & 1 deletion tests/data/blocks/import/TestBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
}
}
205 changes: 203 additions & 2 deletions tests/src/base/BlockPlaceholderIterationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testRenderPlaceholderIteration()
'group_id' => 1,
'class' => TestingLayoutBlock::class,
'is_disabled' => 0,
]
],
]
]);

Expand All @@ -48,7 +48,7 @@ public function testRenderPlaceholderIteration()
'nav_item_id' => 1,
'timestamp_create' => time(),
'version_alias' => 'barfoo',
]
],
]
]);

Expand Down Expand Up @@ -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('<div class="render-frontend"><box><div class="block">foo</div></box><box><div class="block">bar</div></box></div>', $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
Expand All @@ -107,6 +282,7 @@ public function name()
{
return 'Content';
}

public function config()
{
return [
Expand All @@ -115,10 +291,12 @@ public function config()
]
];
}

public function renderFrontend()
{
return '<div class="block">'.$this->getVarValue('var1').'</div>';
}

public function renderAdmin()
{
return '<div>{{ vars.content | raw }}</div>';
Expand Down Expand Up @@ -154,3 +332,26 @@ public function placeholderRenderIteration(\luya\cms\base\BlockInterface $block)
return '<box>'.$block->renderFrontend().'</box>';
}
}

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();
}
}
45 changes: 39 additions & 6 deletions tests/src/base/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand Down

0 comments on commit 5d59cc6

Please sign in to comment.