Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/MAGETWO-62302' into develop-pr2
Browse files Browse the repository at this point in the history
  • Loading branch information
RomaKis committed May 24, 2017
2 parents ebec6d7 + 0666772 commit 369c963
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
34 changes: 30 additions & 4 deletions lib/internal/Magento/Framework/Stdlib/Cookie/PhpCookieManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

namespace Magento\Framework\Stdlib\Cookie;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Phrase;
use Magento\Framework\HTTP\Header as HttpHeader;
use Psr\Log\LoggerInterface;

/**
* CookieManager helps manage the setting, retrieving and deleting of cookies.
Expand Down Expand Up @@ -48,14 +51,36 @@ class PhpCookieManager implements CookieManagerInterface
*/
private $reader;

/**
* Logger for warning details.
*
* @var LoggerInterface
*/
private $logger;

/**
* Object that provides access to HTTP headers.
*
* @var HttpHeader
*/
private $httpHeader;

/**
* @param CookieScopeInterface $scope
* @param CookieReaderInterface $reader
* @param LoggerInterface $logger
* @param HttpHeader $httpHeader
*/
public function __construct(CookieScopeInterface $scope, CookieReaderInterface $reader)
{
public function __construct(
CookieScopeInterface $scope,
CookieReaderInterface $reader,
LoggerInterface $logger = null,
HttpHeader $httpHeader = null
) {
$this->scope = $scope;
$this->reader = $reader;
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
$this->httpHeader = $httpHeader ?: ObjectManager::getInstance()->get(HttpHeader::class);
}

/**
Expand Down Expand Up @@ -182,8 +207,9 @@ private function checkAbilityToSendCookie($name, $value)
$sizeOfCookie = $this->sizeOfCookie($name, $value);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
use Magento\Framework\Exception\InputException;
use Magento\Framework\Stdlib\Cookie\FailureToSendException;
use Magento\Framework\Stdlib\Cookie\CookieSizeLimitReachedException;
use Magento\Framework\Phrase;
use Magento\Framework\HTTP\Header as HttpHeader;
use Psr\Log\LoggerInterface;
// @codingStandardsIgnoreEnd

/**
* Test PhpCookieManager
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class PhpCookieManagerTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -95,6 +100,16 @@ class PhpCookieManagerTest extends \PHPUnit_Framework_TestCase
*/
protected $readerMock;

/**
* @var LoggerInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $loggerMock;

/**
* @var HttpHeader | \PHPUnit_Framework_MockObject_MockObject
*/
protected $httpHeaderMock;

/**
* @var array
*/
Expand All @@ -113,11 +128,18 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();
$this->readerMock = $this->getMock(\Magento\Framework\Stdlib\Cookie\CookieReaderInterface::class);
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
->getMockForAbstractClass();
$this->httpHeaderMock = $this->getMockBuilder(HttpHeader::class)
->disableOriginalConstructor()
->getMock();
$this->cookieManager = $this->objectManager->getObject(
\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class,
[
'scope' => $this->scopeMock,
'reader' => $this->readerMock,
'logger' => $this->loggerMock,
'httpHeader' => $this->httpHeaderMock
]
);

Expand Down Expand Up @@ -503,11 +525,11 @@ public function testSetTooManyCookies()
\Magento\Framework\Stdlib\Cookie\PublicCookieMetadata::class
);

$cookieValue = 'some_value';
$userAgent = 'some_user_agent';

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

$this->scopeMock->expects($this->once())
Expand All @@ -517,19 +539,22 @@ public function testSetTooManyCookies()
$this->returnValue($publicCookieMetadata)
);

try {
$this->cookieManager->setPublicCookie(
self::MAX_COOKIE_SIZE_TEST_NAME,
$cookieValue,
$publicCookieMetadata
);
$this->fail('Failed to throw exception of too many cookies.');
} catch (CookieSizeLimitReachedException $e) {
$this->assertEquals(
'Unable to send the cookie. Maximum number of cookies would be exceeded.',
$e->getMessage()
$this->httpHeaderMock->expects($this->any())
->method('getHttpUserAgent')
->willReturn($userAgent);

$this->loggerMock->expects($this->once())
->method('warning')
->with(
new Phrase('Unable to send the cookie. Maximum number of cookies would be exceeded.'),
array_merge($_COOKIE, ['user-agent' => $userAgent])
);
}

$this->cookieManager->setPublicCookie(
self::MAX_COOKIE_SIZE_TEST_NAME,
self::COOKIE_VALUE,
$publicCookieMetadata
);
}

/**
Expand Down

0 comments on commit 369c963

Please sign in to comment.