Skip to content

Commit 369c963

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-62302' into develop-pr2
2 parents ebec6d7 + 0666772 commit 369c963

File tree

2 files changed

+69
-18
lines changed

2 files changed

+69
-18
lines changed

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

Lines changed: 30 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,36 @@ class PhpCookieManager implements CookieManagerInterface
4851
*/
4952
private $reader;
5053

54+
/**
55+
* Logger for warning details.
56+
*
57+
* @var LoggerInterface
58+
*/
59+
private $logger;
60+
61+
/**
62+
* Object that provides access to HTTP headers.
63+
*
64+
* @var HttpHeader
65+
*/
66+
private $httpHeader;
67+
5168
/**
5269
* @param CookieScopeInterface $scope
5370
* @param CookieReaderInterface $reader
71+
* @param LoggerInterface $logger
72+
* @param HttpHeader $httpHeader
5473
*/
55-
public function __construct(CookieScopeInterface $scope, CookieReaderInterface $reader)
56-
{
74+
public function __construct(
75+
CookieScopeInterface $scope,
76+
CookieReaderInterface $reader,
77+
LoggerInterface $logger = null,
78+
HttpHeader $httpHeader = null
79+
) {
5780
$this->scope = $scope;
5881
$this->reader = $reader;
82+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
83+
$this->httpHeader = $httpHeader ?: ObjectManager::getInstance()->get(HttpHeader::class);
5984
}
6085

6186
/**
@@ -182,8 +207,9 @@ private function checkAbilityToSendCookie($name, $value)
182207
$sizeOfCookie = $this->sizeOfCookie($name, $value);
183208

184209
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.')
210+
$this->logger->warning(
211+
new Phrase('Unable to send the cookie. Maximum number of cookies would be exceeded.'),
212+
array_merge($_COOKIE, ['user-agent' => $this->httpHeader->getHttpUserAgent()])
187213
);
188214
}
189215

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
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
/**
2124
* Test PhpCookieManager
25+
*
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2227
*/
2328
class PhpCookieManagerTest extends \PHPUnit_Framework_TestCase
2429
{
@@ -95,6 +100,16 @@ class PhpCookieManagerTest extends \PHPUnit_Framework_TestCase
95100
*/
96101
protected $readerMock;
97102

103+
/**
104+
* @var LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
105+
*/
106+
protected $loggerMock;
107+
108+
/**
109+
* @var HttpHeader | \PHPUnit_Framework_MockObject_MockObject
110+
*/
111+
protected $httpHeaderMock;
112+
98113
/**
99114
* @var array
100115
*/
@@ -113,11 +128,18 @@ protected function setUp()
113128
->disableOriginalConstructor()
114129
->getMock();
115130
$this->readerMock = $this->getMock(\Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class);
131+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
132+
->getMockForAbstractClass();
133+
$this->httpHeaderMock = $this->getMockBuilder(HttpHeader::class)
134+
->disableOriginalConstructor()
135+
->getMock();
116136
$this->cookieManager = $this->objectManager->getObject(
117137
\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class,
118138
[
119139
'scope' => $this->scopeMock,
120140
'reader' => $this->readerMock,
141+
'logger' => $this->loggerMock,
142+
'httpHeader' => $this->httpHeaderMock
121143
]
122144
);
123145

@@ -503,11 +525,11 @@ public function testSetTooManyCookies()
503525
\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class
504526
);
505527

506-
$cookieValue = 'some_value';
528+
$userAgent = 'some_user_agent';
507529

508530
// Set self::MAX_NUM_COOKIES number of cookies in superglobal $_COOKIE.
509531
for ($i = count($_COOKIE); $i < self::MAX_NUM_COOKIES; $i++) {
510-
$_COOKIE['test_cookie_' . $i] = 'some_value';
532+
$_COOKIE['test_cookie_' . $i] = self::COOKIE_VALUE . '_' . $i;
511533
}
512534

513535
$this->scopeMock->expects($this->once())
@@ -517,19 +539,22 @@ public function testSetTooManyCookies()
517539
$this->returnValue($publicCookieMetadata)
518540
);
519541

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()
542+
$this->httpHeaderMock->expects($this->any())
543+
->method('getHttpUserAgent')
544+
->willReturn($userAgent);
545+
546+
$this->loggerMock->expects($this->once())
547+
->method('warning')
548+
->with(
549+
new Phrase('Unable to send the cookie. Maximum number of cookies would be exceeded.'),
550+
array_merge($_COOKIE, ['user-agent' => $userAgent])
531551
);
532-
}
552+
553+
$this->cookieManager->setPublicCookie(
554+
self::MAX_COOKIE_SIZE_TEST_NAME,
555+
self::COOKIE_VALUE,
556+
$publicCookieMetadata
557+
);
533558
}
534559

535560
/**

0 commit comments

Comments
 (0)