Skip to content

Commit c84f6e8

Browse files
ENGCOM-6129: Resolve 'Redirect to CMS-page if Cookies are Disabled' is 'No' but it still redirect issue25148 #25152
- Merge Pull Request #25152 from edenduong/magento2:2.3-bugfix/no_cookie_issue25148 - Merged commits: 1. 8aafc69 2. e185622 3. 6ea1f0d 4. fc32aa1 5. 1a8bd75
2 parents 65763df + 1a8bd75 commit c84f6e8

File tree

4 files changed

+142
-6
lines changed

4 files changed

+142
-6
lines changed

app/code/Magento/Cookie/Block/RequireCookie.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
6+
declare(strict_types=1);
77
/**
88
* Frontend form key content block
99
*/
1010
namespace Magento\Cookie\Block;
1111

12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
1215
/**
16+
* Block Require Cookie
17+
*
1318
* @api
1419
* @since 100.0.2
20+
*
21+
* Class \Magento\Cookie\Block\RequireCookie
1522
*/
1623
class RequireCookie extends \Magento\Framework\View\Element\Template
1724
{
@@ -22,9 +29,11 @@ class RequireCookie extends \Magento\Framework\View\Element\Template
2229
*/
2330
public function getScriptOptions()
2431
{
32+
$isRedirectCmsPage = (boolean)$this->_scopeConfig->getValue('web/browser_capabilities/cookies');
2533
$params = [
2634
'noCookieUrl' => $this->escapeUrl($this->getUrl('cookie/index/noCookies/')),
27-
'triggers' => $this->escapeHtml($this->getTriggers())
35+
'triggers' => $this->escapeHtml($this->getTriggers()),
36+
'isRedirectCmsPage' => $isRedirectCmsPage
2837
];
2938
return json_encode($params);
3039
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Cookie\Test\Unit\Block;
10+
11+
use Magento\Cookie\Block\RequireCookie;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\View\Element\Template\Context;
14+
15+
/**
16+
* Class \Magento\Cookie\Test\Unit\Block\RequireCookieTest
17+
*/
18+
class RequireCookieTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var \PHPUnit_Framework_MockObject_MockObject|RequireCookie
22+
*/
23+
private $block;
24+
25+
/**
26+
* @var \PHPUnit_Framework_MockObject_MockObject|ScopeConfigInterface
27+
*/
28+
private $scopeConfig;
29+
30+
/**
31+
* @var \PHPUnit_Framework_MockObject_MockObject|Context
32+
*/
33+
private $context;
34+
35+
/**
36+
* Setup Environment
37+
*/
38+
protected function setUp()
39+
{
40+
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
41+
->disableOriginalConstructor()
42+
->setMethods(['getValue'])
43+
->getMockForAbstractClass();
44+
$this->context = $this->getMockBuilder(Context::class)
45+
->disableOriginalConstructor()
46+
->getMock();
47+
$this->context->expects($this->any())->method('getScopeConfig')
48+
->willReturn($this->scopeConfig);
49+
$this->block = $this->getMockBuilder(RequireCookie::class)
50+
->setMethods(['escapeHtml', 'escapeUrl', 'getUrl', 'getTriggers'])
51+
->setConstructorArgs(
52+
[
53+
'context' => $this->context
54+
]
55+
)->getMock();
56+
}
57+
58+
/**
59+
* Test getScriptOptions() when the settings "Redirect to CMS-page if Cookies are Disabled" is "Yes"
60+
*/
61+
public function testGetScriptOptionsWhenRedirectToCmsIsYes()
62+
{
63+
$this->scopeConfig->expects($this->any())->method('getValue')
64+
->with('web/browser_capabilities/cookies')
65+
->willReturn('1');
66+
67+
$this->block->expects($this->any())->method('getUrl')
68+
->with('cookie/index/noCookies/')
69+
->willReturn('http://magento.com/cookie/index/noCookies/');
70+
$this->block->expects($this->any())->method('getTriggers')
71+
->willReturn('test');
72+
$this->block->expects($this->any())->method('escapeUrl')
73+
->with('http://magento.com/cookie/index/noCookies/')
74+
->willReturn('http://magento.com/cookie/index/noCookies/');
75+
$this->block->expects($this->any())->method('escapeHtml')
76+
->with('test')
77+
->willReturn('test');
78+
79+
$this->assertEquals(
80+
'{"noCookieUrl":"http:\/\/magento.com\/cookie\/index\/noCookies\/",' .
81+
'"triggers":"test","isRedirectCmsPage":true}',
82+
$this->block->getScriptOptions()
83+
);
84+
}
85+
86+
/**
87+
* Test getScriptOptions() when the settings "Redirect to CMS-page if Cookies are Disabled" is "No"
88+
*/
89+
public function testGetScriptOptionsWhenRedirectToCmsIsNo()
90+
{
91+
$this->scopeConfig->expects($this->any())->method('getValue')
92+
->with('web/browser_capabilities/cookies')
93+
->willReturn('0');
94+
95+
$this->block->expects($this->any())->method('getUrl')
96+
->with('cookie/index/noCookies/')
97+
->willReturn('http://magento.com/cookie/index/noCookies/');
98+
$this->block->expects($this->any())->method('getTriggers')
99+
->willReturn('test');
100+
$this->block->expects($this->any())->method('escapeUrl')
101+
->with('http://magento.com/cookie/index/noCookies/')
102+
->willReturn('http://magento.com/cookie/index/noCookies/');
103+
$this->block->expects($this->any())->method('escapeHtml')
104+
->with('test')
105+
->willReturn('test');
106+
107+
$this->assertEquals(
108+
'{"noCookieUrl":"http:\/\/magento.com\/cookie\/index\/noCookies\/",' .
109+
'"triggers":"test","isRedirectCmsPage":false}',
110+
$this->block->getScriptOptions()
111+
);
112+
}
113+
}

app/code/Magento/Cookie/i18n/en_US.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@
1111
"Cookie Domain","Cookie Domain"
1212
"Use HTTP Only","Use HTTP Only"
1313
"Cookie Restriction Mode","Cookie Restriction Mode"
14+
"Cookies are disabled in your browser.","Cookies are disabled in your browser."
15+

app/code/Magento/Cookie/view/frontend/web/js/require-cookie.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@
88
*/
99
define([
1010
'jquery',
11-
'jquery-ui-modules/widget'
12-
], function ($) {
11+
'Magento_Ui/js/modal/alert',
12+
'jquery-ui-modules/widget',
13+
'mage/mage',
14+
'mage/translate'
15+
], function ($, alert) {
1316
'use strict';
1417

1518
$.widget('mage.requireCookie', {
1619
options: {
1720
event: 'click',
1821
noCookieUrl: 'enable-cookies',
19-
triggers: ['.action.login', '.action.submit']
22+
triggers: ['.action.login', '.action.submit'],
23+
isRedirectCmsPage: true
2024
},
2125

2226
/**
@@ -49,8 +53,16 @@ define([
4953
if (navigator.cookieEnabled) {
5054
return;
5155
}
56+
5257
event.preventDefault();
53-
window.location = this.options.noCookieUrl;
58+
59+
if (this.options.isRedirectCmsPage) {
60+
window.location = this.options.noCookieUrl;
61+
} else {
62+
alert({
63+
content: $.mage.__('Cookies are disabled in your browser.')
64+
});
65+
}
5466
}
5567
});
5668

0 commit comments

Comments
 (0)