Skip to content

Commit

Permalink
Fixes yiisoft#16657: Ensure widgets after run event result contains t…
Browse files Browse the repository at this point in the history
…he result of the rendered widget
  • Loading branch information
AdeAttwood authored and samdark committed Sep 23, 2018
1 parent 26700f0 commit 7dc38ff
Show file tree
Hide file tree
Showing 25 changed files with 230 additions and 31 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.16 under development
------------------------

- Bug #16657: Ensure widgets after run event result contains the result of the rendered widget (AdeAttwood)
- Enh #16151: Change of scope for method `getTableNameAndAlias()` (s1lver)
- Bug #14230: Fixed `itemsOptions` ignored in `checkBoxList` (s1lver)
- Bug #14368: Added `role` attribute for active radio list (s1lver)
Expand Down
4 changes: 2 additions & 2 deletions framework/grid/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function run()
$id = $this->options['id'];
$options = Json::htmlEncode(array_merge($this->getClientOptions(), ['filterOnFocusOut' => $this->filterOnFocusOut]));
$view->registerJs("jQuery('#$id').yiiGridView($options);");
parent::run();
return parent::run();
}

/**
Expand Down Expand Up @@ -359,7 +359,7 @@ public function renderItems()

$tableFooter = false;
$tableFooterAfterBody = false;

if ($this->showFooter) {
if ($this->placeFooterAfterBody) {
$tableFooterAfterBody = $this->renderTableFooter();
Expand Down
2 changes: 1 addition & 1 deletion framework/widgets/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function run()

$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'div');
echo Html::tag($tag, $content, $options);
return Html::tag($tag, $content, $options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion framework/widgets/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function run()
{
$block = ob_get_clean();
if ($this->renderInPlace) {
echo $block;
return $block;
}
$this->view->blocks[$this->getId()] = $block;
}
Expand Down
2 changes: 1 addition & 1 deletion framework/widgets/Breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function run()
}
$links[] = $this->renderItem($link, isset($link['url']) ? $this->itemTemplate : $this->activeItemTemplate);
}
echo Html::tag($this->tag, implode('', $links), $this->options);
return Html::tag($this->tag, implode('', $links), $this->options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion framework/widgets/ContentDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public function run()
$params = $this->params;
$params['content'] = ob_get_clean();
// render under the existing context
echo $this->view->renderFile($this->viewFile, $params);
return $this->view->renderFile($this->viewFile, $params);
}
}
2 changes: 1 addition & 1 deletion framework/widgets/DetailView.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function run()

$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'table');
echo Html::tag($tag, implode("\n", $rows), $options);
return Html::tag($tag, implode("\n", $rows), $options);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions framework/widgets/FragmentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function init()
public function run()
{
if (($content = $this->getCachedContent()) !== false) {
echo $content;
return $content;
} elseif ($this->cache instanceof CacheInterface) {
$this->getView()->popDynamicContent();

Expand All @@ -114,7 +114,7 @@ public function run()
}
$data = [$content, $this->getDynamicPlaceholders()];
$this->cache->set($this->calculateKey(), $data, $this->duration, $this->dependency);
echo $this->updateDynamicContent($content, $this->getDynamicPlaceholders());
return $this->updateDynamicContent($content, $this->getDynamicPlaceholders());
}
}

Expand Down
2 changes: 1 addition & 1 deletion framework/widgets/LinkPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function run()
if ($this->registerLinkTags) {
$this->registerLinkTags();
}
echo $this->renderPageButtons();
return $this->renderPageButtons();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion framework/widgets/LinkSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function init()
*/
public function run()
{
echo $this->renderSortLinks();
return $this->renderSortLinks();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion framework/widgets/MaskedInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function init()
public function run()
{
$this->registerClientScript();
echo $this->renderInputHtml($this->type);
return $this->renderInputHtml($this->type);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion framework/widgets/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function run()
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'ul');

echo Html::tag($tag, $this->renderItems($items), $options);
return Html::tag($tag, $this->renderItems($items), $options);
}
}

Expand Down
6 changes: 3 additions & 3 deletions framework/widgets/Pjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ public function init()
$view->head();
$view->beginBody();
if ($view->title !== null) {
echo Html::tag('title', Html::encode($view->title));
return Html::tag('title', Html::encode($view->title));
}
} else {
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'div');
echo Html::beginTag($tag, array_merge([
return Html::beginTag($tag, array_merge([
'data-pjax-container' => '',
'data-pjax-push-state' => $this->enablePushState,
'data-pjax-replace-state' => $this->enableReplaceState,
Expand All @@ -148,7 +148,7 @@ public function init()
public function run()
{
if (!$this->requiresPjax()) {
echo Html::endTag(ArrayHelper::remove($this->options, 'tag', 'div'));
return Html::endTag(ArrayHelper::remove($this->options, 'tag', 'div'));
$this->registerClientScript();

return;
Expand Down
4 changes: 2 additions & 2 deletions framework/widgets/Spaceless.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public function init()

/**
* Marks the end of content to be cleaned from whitespace characters between HTML tags.
* Stops capturing an output and echoes cleaned result.
* Stops capturing an output and returns cleaned result.
*/
public function run()
{
echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));
return trim(preg_replace('/>\s+</', '><', ob_get_clean()));
}
}
2 changes: 1 addition & 1 deletion tests/framework/grid/RadiobuttonColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testMultipleInGrid()
],
]);
ob_start();
$grid->run();
echo $grid->run();
$actual = ob_get_clean();
$this->assertEqualsWithoutLE(<<<'HTML'
<div id="radio-gridview"><div class="summary">Showing <b>1-2</b> of <b>2</b> items.</div>
Expand Down
20 changes: 20 additions & 0 deletions tests/framework/widgets/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,24 @@ public function testShouldTriggerInitEvent()

$this->assertTrue($initTriggered);
}

public function testAfterRunResultNotEmpty()
{
$result = null;

ob_start();
Block::begin([
'renderInPlace' => true,
'on afterRun' => function($event) use (&$result) {
$result = $event->result;
},
]);

echo 'The Block';

Block::end();
ob_end_clean();

$this->assertEquals('The Block', $result);
}
}
24 changes: 20 additions & 4 deletions tests/framework/widgets/BreadcrumbsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testHomeLinkNull()
. '</ul>';

ob_start();
$this->breadcrumbs->run();
echo $this->breadcrumbs->run();
$actualHtml = ob_get_contents();
ob_end_clean();

Expand All @@ -63,7 +63,7 @@ public function testHomeLinkFalse()
. '</ul>';

ob_start();
$this->breadcrumbs->run();
echo $this->breadcrumbs->run();
$actualHtml = ob_get_contents();
ob_end_clean();

Expand All @@ -82,7 +82,7 @@ public function testHomeLink()
. '</ul>';

ob_start();
$this->breadcrumbs->run();
echo $this->breadcrumbs->run();
$actualHtml = ob_get_contents();
ob_end_clean();

Expand Down Expand Up @@ -180,13 +180,29 @@ public function testTag()
. "http://my.example.com/yii2/link/page\n";

ob_start();
$this->breadcrumbs->run();
echo $this->breadcrumbs->run();
$actualHtml = ob_get_contents();
ob_end_clean();

$this->assertEquals($expectedHtml, $actualHtml);
}

public function testAfterRunResultNotEmpty()
{
$result = null;
Breadcrumbs::widget([
'links' => [
['label' => 'Sample Post', 'url' => 'post/edit'],
'Edit',
],
'on afterRun' => function ($event) use (&$result) {
$result = $event->result;
},
]);

$this->assertNotNull($result);
}

/**
* Helper methods.
* @param string $class
Expand Down
21 changes: 21 additions & 0 deletions tests/framework/widgets/ContentDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,25 @@ public function testShouldTriggerInitEvent()

$this->assertTrue($initTriggered);
}

public function testAfterRunResultNotEmpty()
{
$result = null;

ob_start();
ContentDecorator::begin([
'viewFile' => '@yiiunit/data/views/layout.php',
'on afterRun' => function ($event) use (&$result) {
$result = $event->result;
},
]);

echo 'The Content';

ContentDecorator::end();
ob_end_clean();

$this->assertContains('The Content', $result);
$this->assertContains('<title>Test</title>', $result);
}
}
19 changes: 19 additions & 0 deletions tests/framework/widgets/DetailViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,25 @@ public function testShouldTriggerInitEvent()

$this->assertTrue($initTriggered);
}

public function testAfterRunResultNotEmpty()
{
$result = null;

$model = new ModelMock();
$model->id = 1;

ob_start();
DetailView::widget([
'model' => $model,
'on afterRun' => function ($event) use (&$result) {
$result = $event->result;
},
]);
ob_end_clean();

$this->assertNotNull($result);
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/framework/widgets/LinkPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,20 @@ public function testShouldTriggerInitEvent()

$this->assertTrue($initTriggered);
}

public function testAfterRunResultNotEmpty()
{
$result = null;

ob_start();
LinkPager::widget([
'pagination' => $this->getPagination(1),
'on afterRun' => function ($event) use (&$result) {
$result = $event->result;
},
]);
ob_end_clean();

$this->assertNotNull($result);
}
}
28 changes: 28 additions & 0 deletions tests/framework/widgets/LinkSorterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,32 @@ public function testShouldTriggerInitEvent()

$this->assertTrue($initTriggered);
}

public function testAfterRunResultNotEmpty()
{
$result = null;
$dataProvider = new ActiveDataProvider([
'query' => Order::find(),
'models' => [new Order()],
'totalCount' => 1,
'sort' => [
'attributes' => ['total'],
'route' => 'site/index',
],
]);

ob_start();
echo ListView::widget([
'dataProvider' => $dataProvider,
'layout' => '{sorter}',
'sorter' => [
'on afterRun' => function ($event) use (&$result) {
$result = $event->result;
},
]
]);
ob_end_clean();

$this->assertNotNull($result);
}
}
Loading

0 comments on commit 7dc38ff

Please sign in to comment.