Skip to content

Commit fc32aa1

Browse files
committed
Unit Test to cover RequireCookie Block issue25148
1 parent 6ea1f0d commit fc32aa1

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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('{"noCookieUrl":"http:\/\/magento.com\/cookie\/index\/noCookies\/",' .
80+
'"triggers":"test","isRedirectCmsPage":true}', $this->block->getScriptOptions());
81+
}
82+
83+
/**
84+
* Test getScriptOptions() when the settings "Redirect to CMS-page if Cookies are Disabled" is "No"
85+
*/
86+
public function testGetScriptOptionsWhenRedirectToCmsIsNo()
87+
{
88+
$this->scopeConfig->expects($this->any())->method('getValue')
89+
->with('web/browser_capabilities/cookies')
90+
->willReturn('0');
91+
92+
$this->block->expects($this->any())->method('getUrl')
93+
->with('cookie/index/noCookies/')
94+
->willReturn('http://magento.com/cookie/index/noCookies/');
95+
$this->block->expects($this->any())->method('getTriggers')
96+
->willReturn('test');
97+
$this->block->expects($this->any())->method('escapeUrl')
98+
->with('http://magento.com/cookie/index/noCookies/')
99+
->willReturn('http://magento.com/cookie/index/noCookies/');
100+
$this->block->expects($this->any())->method('escapeHtml')
101+
->with('test')
102+
->willReturn('test');
103+
104+
$this->assertEquals('{"noCookieUrl":"http:\/\/magento.com\/cookie\/index\/noCookies\/",' .
105+
'"triggers":"test","isRedirectCmsPage":false}', $this->block->getScriptOptions());
106+
}
107+
}

0 commit comments

Comments
 (0)