Skip to content

Magento_Cookie: avoid using deprecated escape* methods from AbstractB… #31674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/code/Magento/Cookie/Block/RequireCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getScriptOptions()
$isRedirectCmsPage = (boolean)$this->_scopeConfig->getValue('web/browser_capabilities/cookies');
$params = [
'noCookieUrl' => $this->escapeUrl($this->getUrl('cookie/index/noCookies/')),
'triggers' => $this->escapeHtml($this->getTriggers()),
'triggers' => $this->_escaper->escapeHtml($this->getTriggers()),
'isRedirectCmsPage' => $isRedirectCmsPage
];
return json_encode($params);
Expand Down
87 changes: 36 additions & 51 deletions app/code/Magento/Cookie/Test/Unit/Block/RequireCookieTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
Expand All @@ -9,6 +10,8 @@

use Magento\Cookie\Block\RequireCookie;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Escaper;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template\Context;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -26,60 +29,44 @@ class RequireCookieTest extends TestCase
private $block;

/**
* @var MockObject|ScopeConfigInterface
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfigMock;

/**
* @var MockObject|Context
* @var Escaper|MockObject
*/
private $escaperMock;

/**
* @var UrlInterface|MockObject
*/
private $contextMock;
private $urlBuilderMock;

/**
* Setup Environment
*/
protected function setUp(): void
{
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->disableOriginalConstructor()
->setMethods(['getValue'])
->getMockForAbstractClass();

$this->contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
$this->contextMock->expects($this->any())->method('getScopeConfig')
->willReturn($this->scopeConfigMock);

$this->block = $this->getMockBuilder(RequireCookie::class)
->setMethods(['escapeHtml', 'escapeUrl', 'getUrl', 'getTriggers'])
->setConstructorArgs(
[
'context' => $this->contextMock
]
)->getMock();
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
$this->escaperMock = $this->createMock(Escaper::class);
$this->urlBuilderMock = $this->createMock(UrlInterface::class);

$contextMock = $this->createMock(Context::class);
$contextMock->method('getScopeConfig')->willReturn($this->scopeConfigMock);
$contextMock->method('getEscaper')->willReturn($this->escaperMock);
$contextMock->method('getUrlBuilder')->willReturn($this->urlBuilderMock);

$this->block = new RequireCookie($contextMock);
$this->block->setData('triggers', 'test');
}

/**
* Test getScriptOptions() when the settings "Redirect to CMS-page if Cookies are Disabled" is "Yes"
*/
public function testGetScriptOptionsWhenRedirectToCmsIsYes(): void
{
$this->scopeConfigMock->expects($this->any())->method('getValue')
->with('web/browser_capabilities/cookies')
->willReturn('1');

$this->block->expects($this->any())->method('getUrl')
->with('cookie/index/noCookies/')
->willReturn(self::STUB_NOCOOKIES_URL);
$this->block->expects($this->any())->method('getTriggers')
->willReturn('test');
$this->block->expects($this->any())->method('escapeUrl')
->with(self::STUB_NOCOOKIES_URL)
->willReturn(self::STUB_NOCOOKIES_URL);
$this->block->expects($this->any())->method('escapeHtml')
->with('test')
->willReturn('test');
$this->setupMocks(1);

$this->assertEquals(
'{"noCookieUrl":"http:\/\/magento.com\/cookie\/index\/noCookies\/",' .
Expand All @@ -93,26 +80,24 @@ public function testGetScriptOptionsWhenRedirectToCmsIsYes(): void
*/
public function testGetScriptOptionsWhenRedirectToCmsIsNo(): void
{
$this->scopeConfigMock->expects($this->any())->method('getValue')
->with('web/browser_capabilities/cookies')
->willReturn('0');

$this->block->expects($this->any())->method('getUrl')
->with('cookie/index/noCookies/')
->willReturn(self::STUB_NOCOOKIES_URL);
$this->block->expects($this->any())->method('getTriggers')
->willReturn('test');
$this->block->expects($this->any())->method('escapeUrl')
->with(self::STUB_NOCOOKIES_URL)
->willReturn(self::STUB_NOCOOKIES_URL);
$this->block->expects($this->any())->method('escapeHtml')
->with('test')
->willReturn('test');
$this->setupMocks(0);

$this->assertEquals(
'{"noCookieUrl":"http:\/\/magento.com\/cookie\/index\/noCookies\/",' .
'"triggers":"test","isRedirectCmsPage":false}',
$this->block->getScriptOptions()
);
}

/**
* @param int $isEnabled
* @return void
*/
private function setupMocks(int $isEnabled): void
{
$this->scopeConfigMock->method('getValue')->with('web/browser_capabilities/cookies')->willReturn($isEnabled);
$this->escaperMock->method('escapeHtml')->with('test')->willReturn('test');
$this->escaperMock->method('escapeUrl')->with(self::STUB_NOCOOKIES_URL)->willReturn(self::STUB_NOCOOKIES_URL);
$this->urlBuilderMock->method('getUrl')->with('cookie/index/noCookies/', [])->willReturn(self::STUB_NOCOOKIES_URL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Cookie settings initialization script
*
* @var $block \Magento\Framework\View\Element\Js\Cookie
* @var \Magento\Framework\View\Element\Js\Cookie $block
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
*/
$isCookieSecure = $block->getSessionConfig()->getCookieSecure() ? 'true' : 'false';
Expand Down
21 changes: 11 additions & 10 deletions app/code/Magento/Cookie/view/frontend/templates/html/notices.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* See COPYING.txt for license details.
*/

/** @var \Magento\Cookie\Block\Html\Notices $block */
/** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */
?>
<?php
/** @var \Magento\Cookie\Helper\Cookie $cookieHelper */
/**
* @var \Magento\Cookie\Block\Html\Notices $block
* @var \Magento\Framework\Escaper $escaper
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
* @var \Magento\Cookie\Helper\Cookie $cookieHelper
*/
$cookieHelper = $block->getData('cookieHelper');
if ($cookieHelper->isCookieRestrictionModeEnabled()): ?>
<div role="alertdialog"
Expand All @@ -17,16 +18,16 @@ if ($cookieHelper->isCookieRestrictionModeEnabled()): ?>
id="notice-cookie-block">
<div role="document" class="content" tabindex="0">
<p>
<strong><?= $block->escapeHtml(__('We use cookies to make your experience better.')) ?></strong>
<span><?= $block->escapeHtml(__(
<strong><?= $escaper->escapeHtml(__('We use cookies to make your experience better.')) ?></strong>
<span><?= $escaper->escapeHtml(__(
'To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies.'
)) ?>
</span>
<?= $block->escapeHtml(__('<a href="%1">Learn more</a>.', $block->getPrivacyPolicyLink()), ['a']) ?>
<?= $escaper->escapeHtml(__('<a href="%1">Learn more</a>.', $block->getPrivacyPolicyLink()), ['a']) ?>
</p>
<div class="actions">
<button id="btn-cookie-allow" class="action allow primary">
<span><?= $block->escapeHtml(__('Allow Cookies')) ?></span>
<span><?= $escaper->escapeHtml(__('Allow Cookies')) ?></span>
</button>
</div>
</div>
Expand All @@ -40,7 +41,7 @@ if ($cookieHelper->isCookieRestrictionModeEnabled()): ?>
"cookieName": "<?= /* @noEscape */ \Magento\Cookie\Helper\Cookie::IS_USER_ALLOWED_SAVE_COOKIE ?>",
"cookieValue": <?= /* @noEscape */ $cookieHelper->getAcceptedSaveCookiesWebsiteIds() ?>,
"cookieLifetime": <?= /* @noEscape */ $cookieHelper->getCookieRestrictionLifetime() ?>,
"noCookiesUrl": "<?= $block->escapeJs($block->getUrl('cookie/index/noCookies')) ?>"
"noCookiesUrl": "<?= $escaper->escapeJs($block->getUrl('cookie/index/noCookies')) ?>"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* See COPYING.txt for license details.
*/

/**
* @var \Magento\Cookie\Block\RequireCookie $block
*/
?>
<?php /** @var \Magento\Cookie\Block\RequireCookie $block */ ?>
<script type="text/x-magento-init">
{
"body": {
Expand Down