Skip to content

Commit 48e9331

Browse files
author
Roman Ganin
committed
Merge branch 'develop' of github.corp.ebay.com:magento2/magento2ce into MAGETWO-35665
2 parents 2af26fd + 0e589aa commit 48e9331

File tree

6 files changed

+112
-37
lines changed

6 files changed

+112
-37
lines changed

app/code/Magento/MediaStorage/Model/File/Storage/Response.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ class Response extends Http implements \Magento\Framework\App\Response\FileInter
2929
* @param CookieManagerInterface $cookieManager
3030
* @param CookieMetadataFactory $cookieMetadataFactory
3131
* @param \Magento\Framework\App\Http\Context $context
32+
* @param \Magento\Framework\Stdlib\DateTime $dateTime
3233
* @param \Magento\Framework\File\Transfer\Adapter\Http $transferAdapter
3334
*/
3435
public function __construct(
3536
CookieManagerInterface $cookieManager,
3637
CookieMetadataFactory $cookieMetadataFactory,
3738
\Magento\Framework\App\Http\Context $context,
39+
\Magento\Framework\Stdlib\DateTime $dateTime,
3840
\Magento\Framework\File\Transfer\Adapter\Http $transferAdapter
3941
) {
40-
parent::__construct($cookieManager, $cookieMetadataFactory, $context);
42+
parent::__construct($cookieManager, $cookieMetadataFactory, $context, $dateTime);
4143
$this->_transferAdapter = $transferAdapter;
4244
}
4345

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/TestCase/ControllerAbstractTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,11 @@ class ControllerAbstractTest extends \Magento\TestFramework\TestCase\AbstractCon
1717

1818
protected function setUp()
1919
{
20-
$this->messageManager = $this->getMock('\Magento\Framework\Message\Manager', [], [], '', false);
21-
$request = new \Magento\TestFramework\Request(
22-
$this->getMock('Magento\Framework\Stdlib\Cookie\CookieReaderInterface'),
23-
$this->getMock('Magento\Framework\App\Route\ConfigInterface\Proxy', [], [], '', false),
24-
$this->getMock('Magento\Framework\App\Request\PathInfoProcessorInterface'),
25-
$this->getMock('Magento\Framework\ObjectManagerInterface')
26-
);
27-
$response = new \Magento\TestFramework\Response(
28-
$this->getMock('Magento\Framework\Stdlib\CookieManagerInterface'),
29-
$this->getMock('Magento\Framework\Stdlib\Cookie\CookieMetadataFactory', [], [], '', false),
30-
$this->getMock('Magento\Framework\App\Http\Context', [], [], '', false)
31-
);
20+
$testObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3221

22+
$this->messageManager = $this->getMock('\Magento\Framework\Message\Manager', [], [], '', false);
23+
$request = $testObjectManager->getObject('Magento\TestFramework\Request');
24+
$response = $testObjectManager->getObject('Magento\TestFramework\Response');
3325
$this->_objectManager = $this->getMock(
3426
'Magento\TestFramework\ObjectManager',
3527
['get', 'create'],

lib/internal/Magento/Framework/App/Response/Http.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,44 @@
1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
1313
use Magento\Framework\Stdlib\CookieManagerInterface;
14+
use Magento\Framework\Stdlib\DateTime;
1415

1516
class Http extends \Magento\Framework\HTTP\PhpEnvironment\Response
1617
{
17-
/**
18-
* Cookie to store page vary string
19-
*/
18+
/** Cookie to store page vary string */
2019
const COOKIE_VARY_STRING = 'X-Magento-Vary';
2120

22-
/**
23-
* @var \Magento\Framework\Stdlib\CookieManagerInterface
24-
*/
21+
/** Format for expiration timestamp headers */
22+
const EXPIRATION_TIMESTAMP_FORMAT = 'D, d M Y H:i:s T';
23+
24+
/** @var \Magento\Framework\Stdlib\CookieManagerInterface */
2525
protected $cookieManager;
2626

27-
/**
28-
* @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
29-
*/
27+
/** @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory */
3028
protected $cookieMetadataFactory;
3129

32-
/**
33-
* @var \Magento\Framework\App\Http\Context
34-
*/
30+
/** @var \Magento\Framework\App\Http\Context */
3531
protected $context;
3632

33+
/** @var DateTime */
34+
protected $dateTime;
35+
3736
/**
3837
* @param CookieManagerInterface $cookieManager
3938
* @param CookieMetadataFactory $cookieMetadataFactory
4039
* @param Context $context
40+
* @param DateTime $dateTime
4141
*/
4242
public function __construct(
4343
CookieManagerInterface $cookieManager,
4444
CookieMetadataFactory $cookieMetadataFactory,
45-
Context $context
45+
Context $context,
46+
DateTime $dateTime
4647
) {
4748
$this->cookieManager = $cookieManager;
4849
$this->cookieMetadataFactory = $cookieMetadataFactory;
4950
$this->context = $context;
51+
$this->dateTime = $dateTime;
5052
}
5153

5254
/**
@@ -97,7 +99,7 @@ public function setPublicHeaders($ttl)
9799
}
98100
$this->setHeader('pragma', 'cache', true);
99101
$this->setHeader('cache-control', 'public, max-age=' . $ttl . ', s-maxage=' . $ttl, true);
100-
$this->setHeader('expires', gmdate('D, d M Y H:i:s T', strtotime('+' . $ttl . ' seconds')), true);
102+
$this->setHeader('expires', $this->getExpirationHeader('+' . $ttl . ' seconds'), true);
101103
}
102104

103105
/**
@@ -114,7 +116,7 @@ public function setPrivateHeaders($ttl)
114116
}
115117
$this->setHeader('pragma', 'cache', true);
116118
$this->setHeader('cache-control', 'private, max-age=' . $ttl, true);
117-
$this->setHeader('expires', gmdate('D, d M Y H:i:s T', strtotime('+' . $ttl . ' seconds')), true);
119+
$this->setHeader('expires', $this->getExpirationHeader('+' . $ttl . ' seconds'), true);
118120
}
119121

120122
/**
@@ -126,7 +128,7 @@ public function setNoCacheHeaders()
126128
{
127129
$this->setHeader('pragma', 'no-cache', true);
128130
$this->setHeader('cache-control', 'no-store, no-cache, must-revalidate, max-age=0', true);
129-
$this->setHeader('expires', gmdate('D, d M Y H:i:s T', strtotime('-1 year')), true);
131+
$this->setHeader('expires', $this->getExpirationHeader('-1 year'), true);
130132
}
131133

132134
/**
@@ -160,4 +162,15 @@ public function __wakeup()
160162
$this->cookieManager = $objectManager->create('Magento\Framework\Stdlib\CookieManagerInterface');
161163
$this->cookieMetadataFactory = $objectManager->get('Magento\Framework\Stdlib\Cookie\CookieMetadataFactory');
162164
}
165+
166+
/**
167+
* Given a time input, returns the formatted header
168+
*
169+
* @param string $time
170+
* @return string
171+
*/
172+
protected function getExpirationHeader($time)
173+
{
174+
return $this->dateTime->gmDate(self::EXPIRATION_TIMESTAMP_FORMAT, $this->dateTime->strToTime($time));
175+
}
163176
}

lib/internal/Magento/Framework/App/Test/Unit/Response/HttpTest.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class HttpTest extends \PHPUnit_Framework_TestCase
1414
{
1515
/**
16-
* @var \Magento\Framework\App\Response\Http
16+
* @var Http
1717
*/
1818
protected $model;
1919

@@ -32,6 +32,9 @@ class HttpTest extends \PHPUnit_Framework_TestCase
3232
*/
3333
protected $contextMock;
3434

35+
/** @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Http\Context */
36+
protected $dateTimeMock;
37+
3538
protected function setUp()
3639
{
3740
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -41,12 +44,18 @@ protected function setUp()
4144
$this->cookieManagerMock = $this->getMock('Magento\Framework\Stdlib\CookieManagerInterface');
4245
$this->contextMock = $this->getMockBuilder('Magento\Framework\App\Http\Context')->disableOriginalConstructor()
4346
->getMock();
47+
48+
$this->dateTimeMock = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime')
49+
->disableOriginalConstructor()
50+
->getMock();
51+
4452
$this->model = $objectManager->getObject(
4553
'Magento\Framework\App\Response\Http',
4654
[
4755
'cookieManager' => $this->cookieManagerMock,
4856
'cookieMetadataFactory' => $this->cookieMetadataFactoryMock,
49-
'context' => $this->contextMock
57+
'context' => $this->contextMock,
58+
'dateTime' => $this->dateTimeMock
5059
]
5160
);
5261
$this->model->headersSentThrowsException = false;
@@ -118,14 +127,24 @@ public function testSendVaryEmptyData()
118127
public function testSetPublicHeaders()
119128
{
120129
$ttl = 120;
130+
$timestamp = 1000000;
121131
$pragma = 'cache';
122132
$cacheControl = 'max-age=' . $ttl . ', public, s-maxage=' . $ttl;
123-
$expiresResult = gmdate('D, d M Y H:i:s T', time() + $ttl);
133+
$expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
134+
135+
$this->dateTimeMock->expects($this->once())
136+
->method('strToTime')
137+
->with('+' . $ttl . ' seconds')
138+
->willReturn($timestamp);
139+
$this->dateTimeMock->expects($this->once())
140+
->method('gmDate')
141+
->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
142+
->willReturn($expiresResult);
124143

125144
$this->model->setPublicHeaders($ttl);
126145
$this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
127146
$this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
128-
$this->assertLessThanOrEqual($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
147+
$this->assertSame($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
129148
}
130149

131150
/**
@@ -146,14 +165,24 @@ public function testSetPublicHeadersWithoutTtl()
146165
public function testSetPrivateHeaders()
147166
{
148167
$ttl = 120;
168+
$timestamp = 1000000;
149169
$pragma = 'cache';
150170
$cacheControl = 'max-age=' . $ttl . ', private';
151-
$expires = gmdate('D, d M Y H:i:s T', strtotime('+' . $ttl . ' seconds'));
171+
$expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
172+
173+
$this->dateTimeMock->expects($this->once())
174+
->method('strToTime')
175+
->with('+' . $ttl . ' seconds')
176+
->willReturn($timestamp);
177+
$this->dateTimeMock->expects($this->once())
178+
->method('gmDate')
179+
->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
180+
->willReturn($expiresResult);
152181

153182
$this->model->setPrivateHeaders($ttl);
154183
$this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
155184
$this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
156-
$this->assertEquals($expires, $this->model->getHeader('Expires')->getFieldValue());
185+
$this->assertEquals($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
157186
}
158187

159188
/**
@@ -173,14 +202,24 @@ public function testSetPrivateHeadersWithoutTtl()
173202
*/
174203
public function testSetNoCacheHeaders()
175204
{
205+
$timestamp = 1000000;
176206
$pragma = 'no-cache';
177207
$cacheControl = 'max-age=0, must-revalidate, no-cache, no-store';
178-
$expires = gmdate('D, d M Y H:i:s T', strtotime('-1 year'));
208+
$expiresResult ='Thu, 01 Jan 1970 00:00:00 GMT';
209+
210+
$this->dateTimeMock->expects($this->once())
211+
->method('strToTime')
212+
->with('-1 year')
213+
->willReturn($timestamp);
214+
$this->dateTimeMock->expects($this->once())
215+
->method('gmDate')
216+
->with(Http::EXPIRATION_TIMESTAMP_FORMAT, $timestamp)
217+
->willReturn($expiresResult);
179218

180219
$this->model->setNoCacheHeaders();
181220
$this->assertEquals($pragma, $this->model->getHeader('Pragma')->getFieldValue());
182221
$this->assertEquals($cacheControl, $this->model->getHeader('Cache-Control')->getFieldValue());
183-
$this->assertEquals($expires, $this->model->getHeader('Expires')->getFieldValue());
222+
$this->assertEquals($expiresResult, $this->model->getHeader('Expires')->getFieldValue());
184223
}
185224

186225
/**

lib/internal/Magento/Framework/Stdlib/DateTime.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,31 @@ public function isEmptyDate($date)
6868
{
6969
return preg_replace('#[ 0:-]#', '', $date) === '';
7070
}
71+
72+
/**
73+
* Wrapper for native gmdate function
74+
*
75+
* @param string $format
76+
* @param int $time
77+
* @return string The given time in given format
78+
*
79+
* @codeCoverageIgnore
80+
*/
81+
public function gmDate($format, $time)
82+
{
83+
return gmdate($format, $time);
84+
}
85+
86+
/**
87+
* Wrapper for native strtotime function
88+
*
89+
* @param string $timeStr
90+
* @return int
91+
*
92+
* @codeCoverageIgnore
93+
*/
94+
public function strToTime($timeStr)
95+
{
96+
return strtotime($timeStr);
97+
}
7198
}

lib/internal/Magento/Framework/Stdlib/DateTime/DateTime.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function calculateOffset($timezone = null)
6363
* @param string $format
6464
* @param int|string $input date in current timezone
6565
* @return string
66+
*
67+
* @deprecated (MAGETWO-35555)
6668
*/
6769
public function gmtDate($format = null, $input = null)
6870
{

0 commit comments

Comments
 (0)