Skip to content

Commit

Permalink
Merge remote-tracking branch 'mainline/develop' into jsunit
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirZaets committed Oct 20, 2016
2 parents 92a9cba + 8ee63a1 commit 4ab99cd
Show file tree
Hide file tree
Showing 17 changed files with 727 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Eav\Api\Data\AttributeSetInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use Magento\Framework\Api\ExtensionAttributesFactory;
Expand Down Expand Up @@ -53,11 +52,6 @@ class AddAttributeToTemplate extends \Magento\Catalog\Controller\Adminhtml\Produ
*/
protected $searchCriteriaBuilder;

/**
* @var SortOrderBuilder
*/
protected $sortOrderBuilder;

/**
* @var AttributeGroupInterfaceFactory
*/
Expand Down Expand Up @@ -115,7 +109,6 @@ public function execute()
$attributeGroupSearchCriteria = $this->getSearchCriteriaBuilder()
->addFilter('attribute_set_id', $attributeSet->getAttributeSetId())
->addFilter('attribute_group_code', $groupCode)
->addSortOrder($this->getSortOrderBuilder()->setAscendingDirection()->create())
->setPageSize(1)
->create();

Expand Down Expand Up @@ -252,18 +245,6 @@ private function getSearchCriteriaBuilder()
return $this->searchCriteriaBuilder;
}

/**
* @return SortOrderBuilder
*/
private function getSortOrderBuilder()
{
if (null === $this->sortOrderBuilder) {
$this->sortOrderBuilder = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Api\SortOrderBuilder::class);
}
return $this->sortOrderBuilder;
}

/**
* @return AttributeManagementInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class Suffix extends \Magento\Framework\App\Config\Value
*/
protected $resource;

/**
* @var \Magento\Framework\App\Config\ScopePool
*/
private $scopePool;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -75,6 +80,22 @@ public function __construct(
$this->resource = $appResource;
}

/**
* Get instance of ScopePool
*
* @return \Magento\Framework\App\Config\ScopePool
* @deprecated
*/
private function getScopePool()
{
if ($this->scopePool === null) {
$this->scopePool = \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\App\Config\ScopePool::class
);
}
return $this->scopePool;
}

/**
* Check url rewrite suffix - whether we can support it
*
Expand Down Expand Up @@ -103,6 +124,24 @@ public function afterSave()
return parent::afterSave();
}

/**
* {@inheritdoc}
*/
public function afterDeleteCommit()
{
if ($this->isValueChanged()) {
$this->updateSuffixForUrlRewrites();
if ($this->isCategorySuffixChanged()) {
$this->cacheTypeList->invalidate([
\Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER,
\Magento\Framework\App\Cache\Type\Collection::TYPE_IDENTIFIER
]);
}
}

return parent::afterDeleteCommit();
}

/**
* Check is category suffix changed
*
Expand Down Expand Up @@ -135,7 +174,12 @@ protected function updateSuffixForUrlRewrites()
}
$entities = $this->urlFinder->findAllByData($dataFilter);
$oldSuffixPattern = '~' . preg_quote($this->getOldValue()) . '$~';
$suffix = $this->getValue();
if ($this->getValue() !== null) {
$suffix = $this->getValue();
} else {
$this->getScopePool()->clean();
$suffix = $this->_config->getValue($this->getPath());
}
foreach ($entities as $urlRewrite) {
$bind = $urlRewrite->getIsAutogenerated()
? [UrlRewrite::REQUEST_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getRequestPath())]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product;

use Magento\Catalog\Controller\Adminhtml\Product\AddAttributeToTemplate;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Backend\App\Action\Context;
use Magento\Catalog\Controller\Adminhtml\Product\Builder as ProductBuilder;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\RequestInterface;
use Magento\Catalog\Api\AttributeSetRepositoryInterface;
use Magento\Eav\Api\Data\AttributeSetInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteria;
use Magento\Eav\Api\AttributeGroupRepositoryInterface;
use Magento\Eav\Api\Data\AttributeGroupSearchResultsInterface;
use Magento\Eav\Api\Data\AttributeGroupInterfaceFactory;
use Magento\Eav\Api\Data\AttributeGroupInterface;
use Magento\Framework\Controller\Result\Json;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AddAttributeToTemplateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var AddAttributeToTemplate
*/
private $controller;

/**
* @var Context|\PHPUnit_Framework_MockObject_MockObject
*/
private $contextMock;

/**
* @var ProductBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
private $productBuilderMock;

/**
* @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $resultJsonFactoryMock;

/**
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;

/**
* @var AttributeSetRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $attributeSetRepositoryMock;

/**
* @var AttributeSetInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $attributeSetInterfaceMock;

/**
* @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
*/
private $searchCriteriaBuilderMock;

/**
* @var SearchCriteria|\PHPUnit_Framework_MockObject_MockObject
*/
private $searchCriteriaMock;

/**
* @var AttributeGroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $attributeGroupRepositoryMock;

/**
* @var AttributeGroupSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $attributeGroupSearchResultsMock;

/**
* @var AttributeGroupInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $attributeGroupInterfaceFactoryMock;

/**
* @var AttributeGroupInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $attributeGroupInterfaceMock;

/**
* @var Json|\PHPUnit_Framework_MockObject_MockObject
*/
private $jsonMock;

protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
$this->productBuilderMock = $this->getMockBuilder(ProductBuilder::class)
->disableOriginalConstructor()
->getMock();
$this->resultJsonFactoryMock = $this->getMockBuilder(JsonFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
->setMethods(['getParam', 'setParam'])
->getMockForAbstractClass();
$this->contextMock->expects($this->once())
->method('getRequest')
->willReturn($this->requestMock);
$this->attributeSetRepositoryMock = $this->getMockBuilder(AttributeSetRepositoryInterface::class)
->setMethods(['get'])
->getMockForAbstractClass();
$this->attributeSetInterfaceMock = $this->getMockBuilder(AttributeSetInterface::class)
->getMockForAbstractClass();
$this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)
->disableOriginalConstructor()
->setMethods(['addFilter', 'create', 'setPageSize'])
->getMockForAbstractClass();
$this->searchCriteriaMock = $this->getMockBuilder(SearchCriteria::class)
->disableOriginalConstructor()
->getMock();
$this->attributeGroupRepositoryMock = $this->getMockBuilder(AttributeGroupRepositoryInterface::class)
->setMethods(['getList'])
->getMockForAbstractClass();
$this->attributeGroupSearchResultsMock = $this->getMockBuilder(AttributeGroupSearchResultsInterface::class)
->setMethods(['getItems'])
->getMockForAbstractClass();
$this->attributeGroupInterfaceFactoryMock = $this->getMockBuilder(AttributeGroupInterfaceFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();
$this->attributeGroupInterfaceMock = $this->getMockBuilder(AttributeGroupInterface::class)
->setMethods(['getExtensionAttributes'])
->getMockForAbstractClass();
$this->jsonMock = $this->getMockBuilder(Json::class)
->disableOriginalConstructor()
->getMock();

$this->controller = $this->objectManager->getObject(
AddAttributeToTemplate::class,
[
'context' => $this->contextMock,
'productBuilder' => $this->productBuilderMock,
'resultJsonFactory' => $this->resultJsonFactoryMock,
]
);

$this->objectManager->setBackwardCompatibleProperty(
$this->controller,
'attributeSetRepository',
$this->attributeSetRepositoryMock
);
$this->objectManager->setBackwardCompatibleProperty(
$this->controller,
'searchCriteriaBuilder',
$this->searchCriteriaBuilderMock
);
$this->objectManager->setBackwardCompatibleProperty(
$this->controller,
'attributeGroupRepository',
$this->attributeGroupRepositoryMock
);
$this->objectManager->setBackwardCompatibleProperty(
$this->controller,
'attributeGroupFactory',
$this->attributeGroupInterfaceFactoryMock
);
}

public function testExecuteWithoutAttributeGroupItems()
{
$groupCode = 'attributes';
$groupName = 'Attributes';
$groupSortOrder = '15';
$templateId = '4';
$attributeIds = [
'selected' => ["178"],
'total' => '1'
];

$this->requestMock
->expects($this->any())
->method('getParam')
->willReturnMap(
[
['groupCode', null, $groupCode],
['groupName', null, $groupName],
['groupSortOrder', null, $groupSortOrder],
['templateId', null, $templateId],
['attributeIds', [], $attributeIds]
]
);

$this->attributeSetRepositoryMock->expects($this->once())
->method('get')
->willReturn($this->attributeSetInterfaceMock);

$this->searchCriteriaBuilderMock->expects($this->any())
->method('addFilter')
->willReturnSelf();
$this->searchCriteriaBuilderMock->expects($this->any())
->method('create')
->willReturn($this->searchCriteriaMock);
$this->searchCriteriaBuilderMock->expects($this->once())
->method('setPageSize')
->willReturnSelf();
$this->searchCriteriaBuilderMock->expects($this->never())
->method('addSortOrder')
->willReturnSelf();

$this->attributeGroupRepositoryMock->expects($this->once())
->method('getList')
->willReturn($this->attributeGroupSearchResultsMock);
$this->attributeGroupSearchResultsMock->expects($this->once())
->method('getItems')
->willReturn(null);

$this->attributeGroupInterfaceFactoryMock->expects($this->once())
->method('create')
->willReturn($this->attributeGroupInterfaceMock);
$this->attributeGroupInterfaceMock->expects($this->once())
->method('getExtensionAttributes')
->willThrowException(new LocalizedException(__('Could not get extension attributes')));

$this->resultJsonFactoryMock->expects($this->once())
->method('create')
->willReturn($this->jsonMock);
$this->jsonMock->expects($this->once())->method('setJsonData')
->willReturnSelf();

$this->controller->execute();
}
}
Loading

0 comments on commit 4ab99cd

Please sign in to comment.