Skip to content

Commit 1598c0f

Browse files
committed
MAGETWO-62302: Fatal error when exceeding number of cookies per domain
1 parent 7201bc0 commit 1598c0f

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

lib/internal/Magento/Framework/Stdlib/Cookie/PhpCookieManager.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
namespace Magento\Framework\Stdlib\Cookie;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Exception\InputException;
1011
use Magento\Framework\Stdlib\CookieManagerInterface;
1112
use Magento\Framework\Phrase;
13+
use Magento\Framework\HTTP\Header as HttpHeader;
14+
use Psr\Log\LoggerInterface;
1215

1316
/**
1417
* CookieManager helps manage the setting, retrieving and deleting of cookies.
@@ -48,14 +51,32 @@ class PhpCookieManager implements CookieManagerInterface
4851
*/
4952
private $reader;
5053

54+
/**
55+
* @var LoggerInterface
56+
*/
57+
private $logger;
58+
59+
/**
60+
* @var HttpHeader
61+
*/
62+
protected $httpHeader;
63+
5164
/**
5265
* @param CookieScopeInterface $scope
5366
* @param CookieReaderInterface $reader
67+
* @param LoggerInterface $logger
68+
* @param HttpHeader $httpHeader
5469
*/
55-
public function __construct(CookieScopeInterface $scope, CookieReaderInterface $reader)
56-
{
70+
public function __construct(
71+
CookieScopeInterface $scope,
72+
CookieReaderInterface $reader,
73+
LoggerInterface $logger = null,
74+
HttpHeader $httpHeader = null
75+
) {
5776
$this->scope = $scope;
5877
$this->reader = $reader;
78+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
79+
$this->httpHeader = $httpHeader ?: ObjectManager::getInstance()->get(HttpHeader::class);
5980
}
6081

6182
/**
@@ -182,8 +203,9 @@ private function checkAbilityToSendCookie($name, $value)
182203
$sizeOfCookie = $this->sizeOfCookie($name, $value);
183204

184205
if ($numCookies > PhpCookieManager::MAX_NUM_COOKIES) {
185-
throw new CookieSizeLimitReachedException(
186-
new Phrase('Unable to send the cookie. Maximum number of cookies would be exceeded.')
206+
$this->logger->warning(
207+
new Phrase('Unable to send the cookie. Maximum number of cookies would be exceeded.'),
208+
array_merge($_COOKIE, ['user-agent' => $this->httpHeader->getHttpUserAgent()])
187209
);
188210
}
189211

lib/internal/Magento/Framework/Stdlib/Test/Unit/Cookie/PhpCookieManagerTest.php

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Magento\Framework\Exception\InputException;
1616
use Magento\Framework\Stdlib\Cookie\FailureToSendException;
1717
use Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException;
18+
use Magento\Framework\Phrase;
19+
use Magento\Framework\HTTP\Header as HttpHeader;
20+
use Psr\Log\LoggerInterface;
1821
// @codingStandardsIgnoreEnd
1922

2023
/**
@@ -95,6 +98,16 @@ class PhpCookieManagerTest extends \PHPUnit_Framework_TestCase
9598
*/
9699
protected $readerMock;
97100

101+
/**
102+
* @var LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
103+
*/
104+
protected $loggerMock;
105+
106+
/**
107+
* @var HttpHeader | \PHPUnit_Framework_MockObject_MockObject
108+
*/
109+
protected $httpHeaderMock;
110+
98111
/**
99112
* @var array
100113
*/
@@ -113,11 +126,18 @@ protected function setUp()
113126
->disableOriginalConstructor()
114127
->getMock();
115128
$this->readerMock = $this->getMock(\Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class);
129+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
130+
->getMockForAbstractClass();
131+
$this->httpHeaderMock = $this->getMockBuilder(HttpHeader::class)
132+
->disableOriginalConstructor()
133+
->getMock();
116134
$this->cookieManager = $this->objectManager->getObject(
117135
\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class,
118136
[
119137
'scope' => $this->scopeMock,
120138
'reader' => $this->readerMock,
139+
'logger' => $this->loggerMock,
140+
'httpHeader' => $this->httpHeaderMock
121141
]
122142
);
123143

@@ -503,11 +523,11 @@ public function testSetTooManyCookies()
503523
\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class
504524
);
505525

506-
$cookieValue = 'some_value';
526+
$userAgent = 'some_user_agent';
507527

508528
// Set self::MAX_NUM_COOKIES number of cookies in superglobal $_COOKIE.
509529
for ($i = count($_COOKIE); $i < self::MAX_NUM_COOKIES; $i++) {
510-
$_COOKIE['test_cookie_' . $i] = 'some_value';
530+
$_COOKIE['test_cookie_' . $i] = self::COOKIE_VALUE . '_' . $i;
511531
}
512532

513533
$this->scopeMock->expects($this->once())
@@ -517,19 +537,22 @@ public function testSetTooManyCookies()
517537
$this->returnValue($publicCookieMetadata)
518538
);
519539

520-
try {
521-
$this->cookieManager->setPublicCookie(
522-
self::MAX_COOKIE_SIZE_TEST_NAME,
523-
$cookieValue,
524-
$publicCookieMetadata
525-
);
526-
$this->fail('Failed to throw exception of too many cookies.');
527-
} catch (CookieSizeLimitReachedException $e) {
528-
$this->assertEquals(
529-
'Unable to send the cookie. Maximum number of cookies would be exceeded.',
530-
$e->getMessage()
540+
$this->httpHeaderMock->expects($this->any())
541+
->method('getHttpUserAgent')
542+
->willReturn($userAgent);
543+
544+
$this->loggerMock->expects($this->once())
545+
->method('warning')
546+
->with(
547+
new Phrase('Unable to send the cookie. Maximum number of cookies would be exceeded.'),
548+
array_merge($_COOKIE, ['user-agent' => $userAgent])
531549
);
532-
}
550+
551+
$this->cookieManager->setPublicCookie(
552+
self::MAX_COOKIE_SIZE_TEST_NAME,
553+
self::COOKIE_VALUE,
554+
$publicCookieMetadata
555+
);
533556
}
534557

535558
/**

0 commit comments

Comments
 (0)