Skip to content

Commit

Permalink
Merge pull request #333 from magento-api/api-sprint-61-pr
Browse files Browse the repository at this point in the history
[API] Sprint 61 + 2.0.1 patch
  • Loading branch information
Korshenko, Olexii(okorshenko) committed Jan 26, 2016
2 parents e7a0e89 + 75ea7d7 commit d3be453
Show file tree
Hide file tree
Showing 36 changed files with 496 additions and 395 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function get($sku, $editMode = false, $storeId = null, $forceReload = fal
*
* @param int $productId
* @param bool $editMode
* @param null|int $storeId
* @param int|null $storeId
* @param bool $forceReload
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Magento\Framework\App\TemplateTypesInterface;
use Magento\Framework\Mail\MessageInterface;

class TransportBuilderTest extends \Magento\Framework\Mail\Test\Unit\Template\TransportBuilderTest
class TransportBuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var string
Expand All @@ -20,20 +20,68 @@ class TransportBuilderTest extends \Magento\Framework\Mail\Test\Unit\Template\Tr
*/
protected $builder;

/**
* @var \Magento\Framework\Mail\Template\FactoryInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $templateFactoryMock;

/**
* @var \Magento\Framework\Mail\Message | \PHPUnit_Framework_MockObject_MockObject
*/
protected $messageMock;

/**
* @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $objectManagerMock;

/**
* @var \Magento\Framework\Mail\Template\SenderResolverInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $senderResolverMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $mailTransportFactoryMock;

/**
* @return void
*/
public function setUp()
{
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->templateFactoryMock = $this->getMock('Magento\Framework\Mail\Template\FactoryInterface');
$this->messageMock = $this->getMock('Magento\Framework\Mail\Message');
$this->objectManagerMock = $this->getMock('Magento\Framework\ObjectManagerInterface');
$this->senderResolverMock = $this->getMock('Magento\Framework\Mail\Template\SenderResolverInterface');
$this->mailTransportFactoryMock = $this->getMockBuilder('Magento\Framework\Mail\TransportInterfaceFactory')
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->builder = $objectManagerHelper->getObject(
$this->builderClassName,
[
'templateFactory' => $this->templateFactoryMock,
'message' => $this->messageMock,
'objectManager' => $this->objectManagerMock,
'senderResolver' => $this->senderResolverMock,
'mailTransportFactory' => $this->mailTransportFactoryMock
]
);
}

/**
* @param int $templateType
* @param string $messageType
* @param string $bodyText
* @param string $templateNamespace
* @return void
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function testGetTransport(
$templateType = TemplateTypesInterface::TYPE_HTML,
$messageType = MessageInterface::TYPE_HTML,
$bodyText = '<h1>Html message</h1>',
$templateNamespace = ''
$bodyText = '<h1>Html message</h1>'
) {
$filter = $this->getMock('Magento\Email\Model\Template\Filter', [], [], '', false);
$data = [
Expand Down
14 changes: 5 additions & 9 deletions app/code/Magento/Store/Model/Plugin/StoreCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,25 @@ public function __construct(
* Delete cookie "store" if the store (a value in the cookie) does not exist or is inactive
*
* @param \Magento\Framework\App\FrontController $subject
* @param callable $proceed
* @param \Magento\Framework\App\RequestInterface $request
* @return mixed
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundDispatch(
public function beforeDispatch(
\Magento\Framework\App\FrontController $subject,
\Closure $proceed,
\Magento\Framework\App\RequestInterface $request
) {
$defaultStore = $this->storeManager->getDefaultStoreView();
$storeCodeFromCookie = $this->storeCookieManager->getStoreCodeFromCookie();
if ($storeCodeFromCookie) {
try {
$this->storeRepository->getActiveStoreByCode($storeCodeFromCookie);
} catch (StoreIsInactiveException $e) {
$this->storeCookieManager->deleteStoreCookie($defaultStore);
$this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView());
} catch (NoSuchEntityException $e) {
$this->storeCookieManager->deleteStoreCookie($defaultStore);
$this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView());
} catch (InvalidArgumentException $e) {
$this->storeCookieManager->deleteStoreCookie($defaultStore);
$this->storeCookieManager->deleteStoreCookie($this->storeManager->getDefaultStoreView());
}
}
return $proceed($request);
}
}
5 changes: 5 additions & 0 deletions app/code/Magento/Store/Model/StoreResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface
*/
protected $scopeCode;

/*
* @var \Magento\Framework\App\RequestInterface
*/
protected $request;

/**
* @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
* @param StoreCookieManagerInterface $storeCookieManager
Expand Down
39 changes: 9 additions & 30 deletions app/code/Magento/Store/Test/Unit/Model/Plugin/StoreCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ class StoreCookieTest extends \PHPUnit_Framework_TestCase
*/
protected $storeMock;

/**
* @var \Closure
*/
protected $closureMock;

/**
* @var \Magento\Framework\App\FrontController|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -77,10 +72,6 @@ public function setUp()
->setMethods([])
->getMock();

$this->closureMock = function () {
return 'ExpectedValue';
};

$this->subjectMock = $this->getMockBuilder('Magento\Framework\App\FrontController')
->disableOriginalConstructor()
->setMethods([])
Expand All @@ -106,7 +97,7 @@ public function setUp()
);
}

public function testAroundDispatchNoSuchEntity()
public function testBeforeDispatchNoSuchEntity()
{
$storeCode = 'store';
$this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock);
Expand All @@ -115,13 +106,10 @@ public function testAroundDispatchNoSuchEntity()
->method('getActiveStoreByCode')
->willThrowException(new NoSuchEntityException);
$this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock);
$this->assertEquals(
'ExpectedValue',
$this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock)
);
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
}

public function testAroundDispatchStoreIsInactive()
public function testBeforeDispatchStoreIsInactive()
{
$storeCode = 'store';
$this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock);
Expand All @@ -130,13 +118,10 @@ public function testAroundDispatchStoreIsInactive()
->method('getActiveStoreByCode')
->willThrowException(new StoreIsInactiveException);
$this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock);
$this->assertEquals(
'ExpectedValue',
$this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock)
);
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
}

public function testAroundDispatchInvalidArgument()
public function testBeforeDispatchInvalidArgument()
{
$storeCode = 'store';
$this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock);
Expand All @@ -145,22 +130,16 @@ public function testAroundDispatchInvalidArgument()
->method('getActiveStoreByCode')
->willThrowException(new InvalidArgumentException);
$this->storeCookieManagerMock->expects($this->once())->method('deleteStoreCookie')->with($this->storeMock);
$this->assertEquals(
'ExpectedValue',
$this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock)
);
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
}

public function testAroundDispatchNoStoreCookie()
public function testBeforeDispatchNoStoreCookie()
{
$storeCode = null;
$this->storeManagerMock->expects($this->once())->method('getDefaultStoreView')->willReturn($this->storeMock);
$this->storeCookieManagerMock->expects($this->once())->method('getStoreCodeFromCookie')->willReturn($storeCode);
$this->storeManagerMock->expects($this->never())->method('getDefaultStoreView')->willReturn($this->storeMock);
$this->storeRepositoryMock->expects($this->never())->method('getActiveStoreByCode');
$this->storeCookieManagerMock->expects($this->never())->method('deleteStoreCookie')->with($this->storeMock);
$this->assertEquals(
'ExpectedValue',
$this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock)
);
$this->plugin->beforeDispatch($this->subjectMock, $this->requestMock);
}
}
5 changes: 5 additions & 0 deletions app/code/Magento/Store/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@
<plugin name="install" type="Magento\Framework\Module\Plugin\DbStatusValidator" sortOrder="40"/>
<plugin name="storeCookieValidate" type="Magento\Store\Model\Plugin\StoreCookie" sortOrder="10"/>
</type>
<type name="Magento\Store\Model\Plugin\StoreCookie">
<arguments>
<argument name="storeManager" xsi:type="object">Magento\Store\Model\StoreManagerInterface\Proxy</argument>
</arguments>
</type>
<type name="Magento\Framework\Module\Plugin\DbStatusValidator">
<arguments>
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/User/Model/ResourceModel/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ public function getOldPasswords($user, $retainLimit = 4)
$userId = (int)$user->getId();
$table = $this->getTable('admin_passwords');

// purge expired passwords, except that should retain
// purge expired passwords, except those which should be retained
$retainPasswordIds = $this->getConnection()->fetchCol(
$this->getConnection()
->select()
Expand All @@ -556,7 +556,7 @@ public function getOldPasswords($user, $retainLimit = 4)
}
$this->getConnection()->delete($table, $where);

// now get all remained passwords
// get all remaining passwords
return $this->getConnection()->fetchCol(
$this->getConnection()
->select()
Expand Down
35 changes: 31 additions & 4 deletions app/code/Magento/User/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ protected function _getValidationRulesBeforeSave()
}

/**
* Validate customer attribute values.
* For existing customer password + confirmation will be validated only when password is set
* (i.e. its change is requested)
* Validate admin user data.
*
* Existing user password confirmation will be validated only when password is set
*
* @return bool|string[]
*/
Expand All @@ -272,8 +272,35 @@ public function validate()
return $validator->getMessages();
}

return true;
return $this->validatePasswordChange();
}

/**
* Make sure admin password was changed.
*
* New password is compared to at least 4 previous passwords to prevent setting them again
*
* @return bool|string[]
*/
protected function validatePasswordChange()
{
$password = $this->getPassword();
if ($password && !$this->getForceNewPassword() && $this->getId()) {
$errorMessage = __('Sorry, but this password has already been used. Please create another.');
// Check if password is equal to the current one
if ($this->_encryptor->isValidHash($password, $this->getOrigData('password'))) {
return [$errorMessage];
}

// Check whether password was used before
$passwordHash = $this->_encryptor->getHash($password, false);
foreach ($this->getResource()->getOldPasswords($this) as $oldPasswordHash) {
if ($passwordHash === $oldPasswordHash) {
return [$errorMessage];
}
}
}
return true;
}

/**
Expand Down

This file was deleted.

Loading

0 comments on commit d3be453

Please sign in to comment.