Skip to content

Commit

Permalink
Merge pull request #417 from magento-frontend/public-prs
Browse files Browse the repository at this point in the history
Bug 
MAGETWO-58373 Set return code for SetModeCommand [GITHUB PR#4845]
MAGETWO-58372 Added 'target' attribute to the allowed attributes array for link block [GITHUB PR#1935]
MAGETWO-56552 [GITHUB PR#4733] Escape Js Quote for layout updates
MAGETWO-58369 [GITHUB PR#4733] Update Container.php [GITHUB PR#4565]
  • Loading branch information
VladimirZaets authored Sep 22, 2016
2 parents 4bb5467 + 5839e18 commit 1552780
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 32 deletions.
16 changes: 16 additions & 0 deletions app/code/Magento/Backend/Block/Widget/Form/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class Container extends \Magento\Backend\Block\Widget\Container
* @var string
*/
protected $_blockGroup = 'Magento_Backend';

/**
* @var string
*/
const PARAM_BLOCK_GROUP = 'block_group';

/**
* @var string
*/
const PARAM_MODE = 'mode';

/**
* @var string
Expand All @@ -49,6 +59,12 @@ class Container extends \Magento\Backend\Block\Widget\Container
protected function _construct()
{
parent::_construct();
if ($this->hasData(self::PARAM_BLOCK_GROUP)) {
$this->_blockGroup = $this->_getData(self::PARAM_BLOCK_GROUP);
}
if ($this->hasData(self::PARAM_MODE)) {
$this->_mode = $this->_getData(self::PARAM_MODE);
}

$this->addButton(
'back',
Expand Down
2 changes: 2 additions & 0 deletions app/code/Magento/Deploy/Console/Command/SetModeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new LocalizedException(__('Cannot switch into given mode "%1"', $toMode));
}
$output->writeln('Enabled ' . $toMode . ' mode.');

return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();

$this->escaperMock = $this->getMock(\Magento\Framework\Escaper::class, ['escapeHtml'], [], '', false);
$this->escaperMock = $this->getMock(
\Magento\Framework\Escaper::class,
['escapeHtml', 'escapeHtmlAttr'],
[],
'',
false
);
$this->escaperMock->expects($this->any())->method('escapeHtmlAttr')->willReturnArgument(0);

$this->contextMock = $this->getMockBuilder(\Magento\Backend\Block\Context::class)
->setMethods(['getEventManager', 'getScopeConfig', 'getEscaper'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,23 @@ public function testGetFormHtml()
$form->setText($expectedHtml);
$this->assertEquals($expectedHtml, $block->getFormHtml());
}

public function testPseudoConstruct()
{
/** @var $block \Magento\Backend\Block\Widget\Form\Container */
$block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\View\LayoutInterface::class
)->createBlock(
\Magento\Backend\Block\Widget\Form\Container::class,
'',
[
'data' => [
\Magento\Backend\Block\Widget\Container::PARAM_CONTROLLER => 'user',
\Magento\Backend\Block\Widget\Form\Container::PARAM_MODE => 'edit',
\Magento\Backend\Block\Widget\Form\Container::PARAM_BLOCK_GROUP => 'Magento_User'
]
]
);
$this->assertInstanceOf(\Magento\User\Block\User\Edit\Form::class, $block->getChildBlock('form'));
}
}
1 change: 1 addition & 0 deletions lib/internal/Magento/Framework/View/Element/Html/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Link extends \Magento\Framework\View\Element\Template
'title',
'charset',
'name',
'target',
'hreflang',
'rel',
'rev',
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/Magento/Framework/View/Element/Html/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function _toHtml()
'" class="' .
$this->getClass() .
'" title="' .
$this->getTitle() .
$this->escapeHtml($this->getTitle()) .
'" ' .
$this->getExtraParams() .
'>';
Expand All @@ -166,7 +166,8 @@ protected function _toHtml()
}

if (is_array($value)) {
$html .= '<optgroup label="' . $label . '" data-optgroup-name="' . $optgroupName . '">';
$html .= '<optgroup label="' . $this->escapeHtml($label)
. '" data-optgroup-name="' . $this->escapeHtml($optgroupName) . '">';
foreach ($value as $keyGroup => $optionGroup) {
if (!is_array($optionGroup)) {
$optionGroup = ['value' => $keyGroup, 'label' => $optionGroup];
Expand Down Expand Up @@ -204,10 +205,10 @@ protected function _optionToHtml($option, $selected = false)
foreach ($option['params'] as $key => $value) {
if (is_array($value)) {
foreach ($value as $keyMulti => $valueMulti) {
$params .= sprintf(' %s="%s" ', $keyMulti, $valueMulti);
$params .= sprintf(' %s="%s" ', $keyMulti, $this->escapeHtml($valueMulti));
}
} else {
$params .= sprintf(' %s="%s" ', $key, $value);
$params .= sprintf(' %s="%s" ', $key, $this->escapeHtml($value));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Framework\View\Test\Unit\Element\Html;

use \Magento\Framework\View\Element\Html\Select;
use Magento\Framework\Escaper;

class SelectTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -14,25 +15,27 @@ class SelectTest extends \PHPUnit_Framework_TestCase
*/
protected $select;

/**
* @var Escaper|\PHPUnit_Framework_MockObject_MockObject
*/
protected $escaper;

protected function setUp()
{
$eventManager = $this->getMock(\Magento\Framework\Event\ManagerInterface::class);

$scopeConfig = $this->getMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);

$escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
$this->escaper = $this->getMockBuilder(\Magento\Framework\Escaper::class)
->disableOriginalConstructor()
->getMock();
$escaper->expects($this->any())
->method('escapeHtml')
->will($this->returnArgument(0));

$context = $this->getMockBuilder(\Magento\Framework\View\Element\Context::class)
->disableOriginalConstructor()
->getMock();
$context->expects($this->once())
->method('getEscaper')
->will($this->returnValue($escaper));
->will($this->returnValue($this->escaper));
$context->expects($this->once())
->method('getEventManager')
->will($this->returnValue($eventManager));
Expand Down Expand Up @@ -92,8 +95,52 @@ public function testGetSetTitle()
$this->assertEquals($selectTitle, $this->select->getTitle());
}

public function testGetHtmlJs()
{
$this->escaper->expects($this->any())
->method('escapeHtml')
->will($this->returnArgument(0));
$this->escaper->expects($this->any())
->method('escapeHtmlAttr')
->will($this->returnArgument(0));

$selectId = 'testId';
$selectClass = 'testClass';
$selectTitle = 'testTitle';
$selectName = 'testName';

$options = [
'testValue' => 'testLabel',
'selectedValue' => 'selectedLabel',
];
$selectedValue = 'selectedValue';

$this->select->setId($selectId);
$this->select->setClass($selectClass);
$this->select->setTitle($selectTitle);
$this->select->setName($selectName);
$this->select->setOptions($options);
$this->select->setValue($selectedValue);

$result = '<select name="testName" id="testId" class="testClass" title="testTitle" >'
. '<option value="testValue" <%= option_extra_attrs.option_4016862802 %> >testLabel</option>'
. '<option value="selectedValue" selected="selected" <%= option_extra_attrs.option_662265145 %> >'
. 'selectedLabel</option>'
. '</select>';

$this->select->setIsRenderToJsTemplate(true);
$this->assertEquals($result, $this->select->getHtml());
}

public function testGetHtml()
{
$this->escaper->expects($this->any())
->method('escapeHtml')
->will($this->returnArgument(0));
$this->escaper->expects($this->any())
->method('escapeHtmlAttr')
->will($this->returnArgument(0));

$selectId = 'testId';
$selectClass = 'testClass';
$selectTitle = 'testTitle';
Expand Down Expand Up @@ -137,33 +184,112 @@ public function testGetHtml()
$this->assertEquals($result, $this->select->getHtml());
}

public function testGetHtmlJs()
public function testGetHtmlEscapes()
{
$selectId = 'testId';
$selectClass = 'testClass';
$selectTitle = 'testTitle';
$selectName = 'testName';
$this->escaper->expects($this->any())
->method('escapeHtml')
->will($this->returnValue('ESCAPED'));
$this->escaper->expects($this->any())
->method('escapeHtmlAttr')
->will($this->returnValue('ESCAPED_ATTR'));

$options = [
'testValue' => 'testLabel',
'selectedValue' => 'selectedLabel',
$optionsSets = [
$this->getOptionsWithSingleQuotes(),
$this->getOptionsWithDoubleQuotes()
];
$selectedValue = 'selectedValue';

$this->select->setId($selectId);
$this->select->setClass($selectClass);
$this->select->setTitle($selectTitle);
$this->select->setName($selectName);
$this->select->setOptions($options);
$this->select->setValue($selectedValue);

$result = '<select name="testName" id="testId" class="testClass" title="testTitle" >'
. '<option value="testValue" <%= option_extra_attrs.option_4016862802 %> >testLabel</option>'
. '<option value="selectedValue" selected="selected" <%= option_extra_attrs.option_662265145 %> >'
. 'selectedLabel</option>'
$expectedResult = '<select name="test[name]" id="testId" class="test class" title="ESCAPED" >'
. '<option value="ESCAPED" paramKey="ESCAPED" >ESCAPED</option>'
. '<option value="ESCAPED" selected="selected" >ESCAPED</option>'
. '<optgroup label="ESCAPED" data-optgroup-name="ESCAPED">'
. '<option value="ESCAPED" >ESCAPED</option>'
. '<option value="ESCAPED" selected="selected" >ESCAPED</option>'
. '</optgroup>'
. '</select>';

$this->select->setIsRenderToJsTemplate(true);
$this->assertEquals($result, $this->select->getHtml());
foreach ($optionsSets as $inOptions) {
$this->select->setId($inOptions['id']);
$this->select->setClass($inOptions['class']);
$this->select->setTitle($inOptions['title']);
$this->select->setName($inOptions['name']);

foreach ($inOptions['options'] as $option) {
$this->select->addOption($option['value'], $option['label'], $option['params']);
}
$this->select->setValue($inOptions['values']);

$this->assertEquals($expectedResult, $this->select->getHtml());

// reset
$this->select->setOptions([]);
}
}

/**
* @return array
*/
private function getOptionsWithSingleQuotes()
{
return [
'id' => "testId",
'name' => "test[name]",
'class' => "test class",
'title' => "test'Title",
'options' => [
'regular' => [
'value' => 'testValue',
'label' => "test'Label",
'params' => ['paramKey' => "param'Value"]
],
'selected' => [
'value' => 'selectedValue',
'label' => "selected'Label",
'params' => []
],
'optgroup' => [
'value' => [
'groupElementValue' => "GroupElement'Label",
'selectedGroupElementValue' => "SelectedGroupElement'Label"
],
'label' => "group'Label",
'params' => []
]
],
'values' => ['selectedValue', 'selectedGroupElementValue']
];
}

/**
* @return array
*/
private function getOptionsWithDoubleQuotes()
{
return [
'id' => 'testId',
'name' => 'test[name]',
'class' => 'test class',
'title' => 'test"Title',
'options' => [
'regular' => [
'value' => 'testValue',
'label' => 'test"Label',
'params' => ['paramKey' => 'param"Value']
],
'selected' => [
'value' => 'selectedValue',
'label' => 'selected"Label',
'params' => []
],
'optgroup' => [
'value' => [
'groupElementValue' => 'GroupElement"Label',
'selectedGroupElementValue' => 'SelectedGroupElement"Label'
],
'label' => 'group"Label',
'params' => []
]
],
'values' => ['selectedValue', 'selectedGroupElementValue']
];
}
}

0 comments on commit 1552780

Please sign in to comment.