Skip to content

Commit

Permalink
Merge remote-tracking branch 'tango-ce/MAGETWO-42096-MB' into happy_m…
Browse files Browse the repository at this point in the history
…erchant
  • Loading branch information
Yuri Kovsher committed Sep 2, 2015
2 parents 1ebc50b + fa7041b commit f2e3fff
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 16 deletions.
15 changes: 13 additions & 2 deletions app/code/Magento/Widget/Model/Template/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public function __construct(
}

/**
* Generate widget
* General method for generate widget
*
* @param string[] $construction
* @return string
*/
public function widgetDirective($construction)
public function generateWidget($construction)
{
$params = $this->getParameters($construction[2]);

Expand Down Expand Up @@ -114,6 +114,17 @@ public function widgetDirective($construction)
return $widget->toHtml();
}

/**
* Generate widget
*
* @param string[] $construction
* @return string
*/
public function widgetDirective($construction)
{
return $this->generateWidget($construction);
}

/**
* Retrieve media file URL directive
*
Expand Down
20 changes: 20 additions & 0 deletions app/code/Magento/Widget/Model/Template/FilterEmulate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Widget\Model\Template;

class FilterEmulate extends Filter
{
/**
* Generate widget with emulation frontend area
*
* @param string[] $construction
* @return string
*/
public function widgetDirective($construction)
{
return $this->_appState->emulateAreaCode('frontend', [$this, 'generateWidget'], [$construction]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Widget\Test\Unit\Model\Template;

use \Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

class FilterEmulateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectManagerHelper
*/
protected $objectManagerHelper;

/**
* @var \Magento\Widget\Model\Template\FilterEmulate
*/
protected $filterEmulate;

/**
* @var \Magento\Framework\App\State|\PHPUnit_Framework_MockObject_MockObject
*/
protected $appStateMock;

/**
* @return void
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->appStateMock = $this->getMock('Magento\Framework\App\State', [], [], '', false);

$this->filterEmulate = $this->objectManagerHelper->getObject(
'Magento\Widget\Model\Template\FilterEmulate',
['appState' => $this->appStateMock]
);
}

/**
* @return void
*/
public function testWidgetDirective()
{
$result = 'some text';
$construction = [
'{{widget type="Widget\\Link" anchor_text="Test" template="block.phtml" id_path="p/1"}}',
'widget',
' type="" anchor_text="Test" template="block.phtml" id_path="p/1"'
];

$this->appStateMock->expects($this->once())
->method('emulateAreaCode')
->with('frontend', [$this->filterEmulate, 'generateWidget'], [$construction])
->willReturn($result);
$this->assertSame($result, $this->filterEmulate->widgetDirective($construction));
}
}
265 changes: 251 additions & 14 deletions app/code/Magento/Widget/Test/Unit/Model/Template/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,269 @@
*/
namespace Magento\Widget\Test\Unit\Model\Template;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Store\Model\StoreManagerInterface;

class FilterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Widget\Model\Template\Filter
*/
protected $filter;

/**
* @var ObjectManagerHelper
*/
protected $objectManagerHelper;

/**
* @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeMock;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeManagerMock;

/**
* @var \Magento\Widget\Model\Resource\Widget|\PHPUnit_Framework_MockObject_MockObject
*/
protected $widgetResourceMock;

/**
* @var \Magento\Widget\Model\Widget|\PHPUnit_Framework_MockObject_MockObject
*/
protected $widgetMock;

/**
* @var \Magento\Framework\View\LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $layoutMock;

/**
* @return void
*/
protected function setUp()
{
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
$this->widgetResourceMock = $this->getMock('Magento\Widget\Model\Resource\Widget', [], [], '', false);
$this->widgetMock = $this->getMock('Magento\Widget\Model\Widget', [], [], '', false);
$this->layoutMock = $this->getMock('Magento\Framework\View\LayoutInterface');

$this->filter = $this->objectManagerHelper->getObject(
'Magento\Widget\Model\Template\Filter',
[
'storeManager' => $this->storeManagerMock,
'widgetResource' => $this->widgetResourceMock,
'widget' => $this->widgetMock,
'layout' => $this->layoutMock
]
);
}

/**
* @param array $construction
* @param string $name
* @param string $type
* @param int $preConfigId
* @param array $params
* @param array $preconfigure
* @param string $widgetXml
* @param \Magento\Widget\Block\BlockInterface|null $widgetBlock
* @param string $expectedResult
* @return void
* @dataProvider generateWidgetDataProvider
*/
public function testGenerateWidget(
$construction,
$name,
$type,
$preConfigId,
$params,
$preconfigure,
$widgetXml,
$widgetBlock,
$expectedResult
) {
$this->generalForGenerateWidget($name, $type, $preConfigId, $params, $preconfigure, $widgetXml, $widgetBlock);
$this->assertSame($expectedResult, $this->filter->generateWidget($construction));
}

/**
* @param array $construction
* @param string $name
* @param string $type
* @param int $preConfigId
* @param array $params
* @param array $preconfigure
* @param string $widgetXml
* @param \Magento\Widget\Block\BlockInterface|null $widgetBlock
* @param string $expectedResult
* @return void
* @dataProvider generateWidgetDataProvider
*/
public function testWidgetDirective(
$construction,
$name,
$type,
$preConfigId,
$params,
$preconfigure,
$widgetXml,
$widgetBlock,
$expectedResult
) {
$this->generalForGenerateWidget($name, $type, $preConfigId, $params, $preconfigure, $widgetXml, $widgetBlock);
$this->assertSame($expectedResult, $this->filter->widgetDirective($construction));
}

/**
* @return array
*/
public function generateWidgetDataProvider()
{
return [
[
'construction' => [
'{{widget type="Widget\\Link" anchor_text="Test" template="block.phtml" id_path="p/1"}}',
'widget',
' type="" anchor_text="Test" template="block.phtml" id_path="p/1"'
],
'name' => null,
'type' => 'Widget\Link',
'preConfigId' => null,
'params' => ['id' => ''],
'preconfigure' => [],
'widgetXml' => '',
'widgetBlock' => null,
'expectedResult' => ''
],
[
'construction' => [
'{{widget type="Widget\\Link" anchor_text="Test" template="block.phtml" id_path="p/1"}}',
'widget',
' type="" id="1" anchor_text="Test" template="block.phtml" id_path="p/1"'
],
'name' => null,
'type' => null,
'preConfigId' => 1,
'params' => ['id' => '1'],
'preconfigure' => ['widget_type' => '', 'parameters' => ''],
'widgetXml' => null,
'widgetBlock' => null,
'expectedResult' => ''
],
[
'construction' => [
'{{widget type="Widget\\Link" anchor_text="Test" template="block.phtml" id_path="p/1"}}',
'widget',
' type="" name="testName" id="1" anchor_text="Test" template="block.phtml" id_path="p/1"'
],
'name' => 'testName',
'type' => 'Widget\Link',
'preConfigId' => 1,
'params' => ['id' => '1'],
'preconfigure' => ['widget_type' => "Widget\\Link", 'parameters' => ['id' => '1']],
'widgetXml' => 'some xml',
'widgetBlock' => $this->getBlockMock('widget text'),
'expectedResult' => 'widget text'
],
[
'construction' => [
'{{widget type="Widget\\Link" anchor_text="Test" template="block.phtml" id_path="p/1"}}',
'widget',
' type="Widget\\Link" name="testName" anchor_text="Test" template="block.phtml" id_path="p/1"'
],
'name' => 'testName',
'type' => 'Widget\Link',
'preConfigId' => null,
'params' => [
'type' => 'Widget\Link',
'name' => 'testName',
'anchor_text' => 'Test',
'template' => 'block.phtml',
'id_path' => 'p/1'
],
'preconfigure' => [],
'widgetXml' => 'some xml',
'widgetBlock' => $this->getBlockMock('widget text'),
'expectedResult' => 'widget text'
],
];
}

/**
* @param string $name
* @param string $type
* @param int $preConfigId
* @param array $params
* @param array $preconfigure
* @param string $widgetXml
* @param \Magento\Widget\Block\BlockInterface|null $widgetBlock
* @return void
* @dataProvider generateWidgetDataProvider
*/
protected function generalForGenerateWidget(
$name,
$type,
$preConfigId,
$params,
$preconfigure,
$widgetXml,
$widgetBlock
) {
$this->widgetResourceMock->expects($this->any())
->method('loadPreconfiguredWidget')
->with($preConfigId)
->willReturn($preconfigure);
$this->widgetMock->expects($this->any())
->method('getWidgetByClassType')
->with($type)
->willReturn($widgetXml);
$this->layoutMock->expects($this->any())
->method('createBlock')
->with($type, $name, ['data' => $params])
->willReturn($widgetBlock);
}

/**
* @param string $returnedResult
* @return \Magento\Widget\Block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected function getBlockMock($returnedResult = '')
{
/** @var \Magento\Widget\Block\BlockInterface|\PHPUnit_Framework_MockObject_MockObject $blockMock */
$blockMock = $this->getMockBuilder('Magento\Widget\Block\BlockInterface')
->setMethods(['toHtml'])
->getMockForAbstractClass();
$blockMock->expects($this->any())
->method('toHtml')
->willReturn($returnedResult);

return $blockMock;
}

/**
* @return void
*/
public function testMediaDirective()
{
$image = 'wysiwyg/VB.png';
$construction = ['{{media url="' . $image . '"}}', 'media', ' url="' . $image . '"'];
$baseUrl = 'http://localhost/pub/media/';

/** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject $storeMock */
$storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
$storeMock->expects($this->once())->method('getBaseUrl')->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
$this->storeMock->expects($this->once())
->method('getBaseUrl')
->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
->willReturn($baseUrl);
$this->storeManagerMock->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);

/** @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject $storeManagerMock */
$storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface', [], [], '', false);
$storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock));

/** @var \Magento\Widget\Model\Template\Filter $filter */
$filter = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
->getObject(
'Magento\Widget\Model\Template\Filter',
['storeManager' => $storeManagerMock]
);
$result = $filter->mediaDirective($construction);
$result = $this->filter->mediaDirective($construction);
$this->assertEquals($baseUrl . $image, $result);
}
}
Loading

0 comments on commit f2e3fff

Please sign in to comment.