Skip to content

Commit a029586

Browse files
author
Sergey Nosov
committed
Merge remote-tracking branch 'origin/MDVA-301' into 2.0.6_backlog
2 parents 257b6b5 + 6bdf4b5 commit a029586

File tree

6 files changed

+87
-10
lines changed

6 files changed

+87
-10
lines changed

app/code/Magento/Integration/Model/Oauth/Consumer.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class Consumer extends \Magento\Framework\Model\AbstractModel implements Consume
4343
*/
4444
protected $dataHelper;
4545

46+
/**
47+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
48+
*/
49+
private $_dateHelper;
50+
4651
/**
4752
* @param \Magento\Framework\Model\Context $context
4853
* @param \Magento\Framework\Registry $registry
@@ -80,6 +85,22 @@ protected function _construct()
8085
$this->_init('Magento\Integration\Model\ResourceModel\Oauth\Consumer');
8186
}
8287

88+
/**
89+
* The getter function to get the new DateTime dependency
90+
*
91+
* @return \Magento\Framework\Stdlib\DateTime\DateTime
92+
*
93+
* @deprecated
94+
*/
95+
private function getDateHelper()
96+
{
97+
if ($this->_dateHelper === null) {
98+
$this->_dateHelper = \Magento\Framework\App\ObjectManager::getInstance()
99+
->get(\Magento\Framework\Stdlib\DateTime\DateTime::class);
100+
}
101+
return $this->_dateHelper;
102+
}
103+
83104
/**
84105
* BeforeSave actions
85106
*
@@ -176,6 +197,8 @@ public function getCreatedAt()
176197
public function isValidForTokenExchange()
177198
{
178199
$expiry = $this->dataHelper->getConsumerExpirationPeriod();
179-
return $expiry > $this->getResource()->getTimeInSecondsSinceCreation($this->getId());
200+
$currentTimestamp = $this->getDateHelper()->gmtTimestamp();
201+
$updatedTimestamp = $this->getDateHelper()->gmtTimestamp($this->getUpdatedAt());
202+
return $expiry > ($currentTimestamp - $updatedTimestamp);
180203
}
181204
}

app/code/Magento/Integration/Model/OauthService.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class OauthService implements \Magento\Integration\Api\OauthServiceInterface
6161
*/
6262
protected $_tokenProvider;
6363

64+
/**
65+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
66+
*/
67+
private $_dateHelper;
68+
6469
/**
6570
* Initialize dependencies.
6671
*
@@ -93,6 +98,22 @@ public function __construct(
9398
$this->_tokenProvider = $tokenProvider;
9499
}
95100

101+
/**
102+
* The getter function to get the new DateTime dependency
103+
*
104+
* @return \Magento\Framework\Stdlib\DateTime\DateTime
105+
*
106+
* @deprecated
107+
*/
108+
private function getDateHelper()
109+
{
110+
if ($this->_dateHelper === null) {
111+
$this->_dateHelper = \Magento\Framework\App\ObjectManager::getInstance()
112+
->get(\Magento\Framework\Stdlib\DateTime\DateTime::class);
113+
}
114+
return $this->_dateHelper;
115+
}
116+
96117
/**
97118
* {@inheritdoc}
98119
*/
@@ -193,6 +214,8 @@ public function postToConsumer($consumerId, $endpointUrl)
193214
{
194215
try {
195216
$consumer = $this->_consumerFactory->create()->load($consumerId);
217+
$consumer->setUpdatedAt($this->getDateHelper()->gmtDate());
218+
$consumer->save();
196219
if (!$consumer->getId()) {
197220
throw new \Magento\Framework\Oauth\Exception(
198221
__('A consumer with ID %1 does not exist', $consumerId)

app/code/Magento/Integration/Model/ResourceModel/Oauth/Consumer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
4545
/**
4646
* Compute time in seconds since consumer was created.
4747
*
48+
* @deprecated
49+
*
4850
* @param int $consumerId - The consumer id
4951
* @return int - time lapsed in seconds
5052
*/

app/code/Magento/Integration/Test/Unit/Helper/Oauth/ConsumerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
namespace Magento\Integration\Test\Unit\Helper\Oauth;
77

8+
/**
9+
* Test for \Magento\Integration\Model\Oauth\Consumer
10+
*
11+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
12+
*/
813
class ConsumerTest extends \PHPUnit_Framework_TestCase
914
{
1015
/** @var \Magento\Store\Model\StoreManagerInterface */
@@ -152,6 +157,16 @@ public function testPostToConsumer()
152157
)->will(
153158
$this->returnSelf()
154159
);
160+
161+
$dateHelperMock = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime\DateTime')
162+
->disableOriginalConstructor()
163+
->getMock();
164+
$dateHelperMock->expects($this->any())->method('gmtDate');
165+
166+
$dateHelper = new \ReflectionProperty('Magento\Integration\Model\OauthService', '_dateHelper');
167+
$dateHelper->setAccessible(true);
168+
$dateHelper->setValue($this->_oauthService, $dateHelperMock);
169+
155170
$this->_consumerMock->expects($this->once())->method('getId')->will($this->returnValue($consumerId));
156171
$this->_consumerMock->expects($this->once())->method('getData')->will($this->returnValue($consumerData));
157172
$this->_httpClientMock->expects(

app/code/Magento/Integration/Test/Unit/Model/Oauth/ConsumerTest.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function setUp()
110110

111111
$this->resourceMock = $this->getMock(
112112
'Magento\Integration\Model\ResourceModel\Oauth\Consumer',
113-
['getTimeInSecondsSinceCreation', 'getIdFieldName', 'selectByCompositeKey', 'deleteOldEntries'],
113+
['getIdFieldName', 'selectByCompositeKey', 'deleteOldEntries'],
114114
[],
115115
'',
116116
false,
@@ -215,21 +215,33 @@ public function testValidateInvalidConsumerSecret()
215215

216216
public function testGetConsumerExpirationPeriodValid()
217217
{
218-
$this->resourceMock->expects($this->once())
219-
->method('getTimeInSecondsSinceCreation')
220-
->will($this->returnValue(30));
218+
$dateHelperMock = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime\DateTime')
219+
->disableOriginalConstructor()
220+
->getMock();
221+
$dateHelperMock->expects($this->at(0))->method('gmtTimestamp')->willReturn(time());
222+
$dateHelperMock->expects($this->at(1))->method('gmtTimestamp')->willReturn(time() - 100);
221223

222-
$this->consumerModel->setCreatedAt(time());
224+
$dateHelper = new \ReflectionProperty('Magento\Integration\Model\Oauth\Consumer', '_dateHelper');
225+
$dateHelper->setAccessible(true);
226+
$dateHelper->setValue($this->consumerModel, $dateHelperMock);
227+
228+
$this->consumerModel->setUpdatedAt(time());
223229
$this->assertTrue($this->consumerModel->isValidForTokenExchange());
224230
}
225231

226232
public function testGetConsumerExpirationPeriodExpired()
227233
{
228-
$this->resourceMock->expects($this->once())
229-
->method('getTimeInSecondsSinceCreation')
230-
->will($this->returnValue(400));
234+
$dateHelperMock = $this->getMockBuilder('Magento\Framework\Stdlib\DateTime\DateTime')
235+
->disableOriginalConstructor()
236+
->getMock();
237+
$dateHelperMock->expects($this->at(0))->method('gmtTimestamp')->willReturn(time());
238+
$dateHelperMock->expects($this->at(1))->method('gmtTimestamp')->willReturn(time() - 1000);
239+
240+
$dateHelper = new \ReflectionProperty('Magento\Integration\Model\Oauth\Consumer', '_dateHelper');
241+
$dateHelper->setAccessible(true);
242+
$dateHelper->setValue($this->consumerModel, $dateHelperMock);
231243

232-
$this->consumerModel->setCreatedAt(time());
244+
$this->consumerModel->setUpdatedAt(time());
233245
$this->assertFalse($this->consumerModel->isValidForTokenExchange());
234246
}
235247
}

dev/tests/api-functional/testsuite/Magento/Webapi/Authentication/RestTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public function testGetRequestToken()
9090
public function testGetRequestTokenExpiredConsumer()
9191
{
9292
$this::consumerFixture('2012-01-01 00:00:00');
93+
$this::$_consumer->setUpdatedAt('2012-01-01 00:00:00');
94+
$this::$_consumer->save();
9395
/** @var $oAuthClient \Magento\TestFramework\Authentication\Rest\OauthClient */
9496
$oAuthClient = $this->_getOauthClient(self::$_consumerKey, self::$_consumerSecret);
9597
$oAuthClient->requestRequestToken();

0 commit comments

Comments
 (0)