Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Natalia Momotenko committed Apr 27, 2016
2 parents 86aa307 + 95abd47 commit 8e2057c
Show file tree
Hide file tree
Showing 11 changed files with 567 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
namespace Magento\Catalog\Model\ResourceModel\Category\Collection;

/**
* Class Factory
* @deprecated
*/
class Factory
{
/**
Expand Down
25 changes: 24 additions & 1 deletion app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Catalog\Model\ResourceModel\Category;

use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory as CategoryFlatCollectionFactory;
use Magento\Framework\App\ObjectManager;

/**
* Category flat model
Expand Down Expand Up @@ -68,6 +70,7 @@ class Flat extends \Magento\Indexer\Model\ResourceModel\AbstractResource
* Category collection factory
*
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
* @deprecated
*/
protected $_categoryCollectionFactory;

Expand All @@ -78,6 +81,11 @@ class Flat extends \Magento\Indexer\Model\ResourceModel\AbstractResource
*/
protected $_categoryFactory;

/**
* @var CategoryFlatCollectionFactory
*/
private $categoryFlatCollectionFactory;

/**
* Class constructor
*
Expand Down Expand Up @@ -399,7 +407,7 @@ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $as
);
$parentPath = $this->getConnection()->fetchOne($select);

$collection = $this->_categoryCollectionFactory
$collection = $this->getCategoryFlatCollectionFactory()
->create()
->addNameToResult()
->addUrlRewriteToResult()
Expand Down Expand Up @@ -690,4 +698,19 @@ public function getProductsPosition($category)

return $this->getConnection()->fetchPairs($select, $bind);
}

/**
* Get instance of CategoryFlatCollectionFactory
*
* @return CategoryFlatCollectionFactory
*/
private function getCategoryFlatCollectionFactory()
{
if (!$this->categoryFlatCollectionFactory instanceof CategoryFlatCollectionFactory) {
$this->categoryFlatCollectionFactory = ObjectManager::getInstance()
->get(CategoryFlatCollectionFactory::class);
}

return $this->categoryFlatCollectionFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category;

use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\Flat\Collection;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Catalog\Model\ResourceModel\Category\Flat;
use Magento\Framework\DB\Select;
use Magento\Framework\DB\Adapter\AdapterInterface as Adapter;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;

/**
* Category flat model test
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class FlatTest extends \PHPUnit_Framework_TestCase
{
const STORE_ID = 1;
const TABLE_NAME = 'test_table';
const PARENT_PATH = '1';
const SORTED = false;
const PARENT = 1;
const RECURSION_LEVEL = 0;

/**
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
*/
private $categoryCollectionFactoryMock;

/**
* @var Collection|\PHPUnit_Framework_MockObject_MockObject
*/
private $categoryCollectionMock;

/**
* @var Flat
*/
private $model;

/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var Select|\PHPUnit_Framework_MockObject_MockObject
*/
private $selectMock;

/**
* @var Adapter|\PHPUnit_Framework_MockObject_MockObject
*/
private $connectionMock;

/**
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
*/
private $resourceMock;

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

/**
* @var Store|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeMock;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->objectManager = new ObjectManager($this);

$this->selectMock = $this->getMockBuilder(Select::class)
->disableOriginalConstructor()
->setMethods(['where', 'from'])
->getMock();
$this->selectMock->expects($this->once())
->method('where')
->willReturn($this->selectMock);
$this->selectMock->expects($this->once())
->method('from')
->willReturn($this->selectMock);
$this->connectionMock = $this->getMockBuilder(Adapter::class)
->getMockForAbstractClass();
$this->connectionMock->expects($this->once())
->method('select')
->willReturn($this->selectMock);
$this->connectionMock->expects($this->once())
->method('fetchOne')
->with($this->selectMock)
->willReturn(self::PARENT_PATH);
$this->resourceMock = $this->getMockBuilder(ResourceConnection::class)
->disableOriginalConstructor()
->setMethods(['getConnection', 'getTableName'])
->getMock();
$this->resourceMock->expects($this->any())
->method('getConnection')
->willReturn($this->connectionMock);
$this->resourceMock->expects($this->any())
->method('getTableName')
->willReturn(self::TABLE_NAME);
$this->contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->setMethods(['getResources'])
->getMock();
$this->contextMock->expects($this->any())
->method('getResources')
->willReturn($this->resourceMock);

$this->storeMock = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()
->setMethods(['getId'])
->getMock();
$this->storeMock->expects($this->any())
->method('getId')
->willReturn(self::STORE_ID);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
$this->storeManagerMock->expects($this->any())
->method('getStore')
->willReturn($this->storeMock);
}

public function testGetCategories()
{
$this->categoryCollectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->categoryCollectionMock = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->setMethods(
[
'addNameToResult',
'addUrlRewriteToResult',
'addParentPathFilter',
'addStoreFilter',
'addIsActiveFilter',
'addAttributeToFilter',
'addSortedField',
'load'
]
)
->getMock();
$this->categoryCollectionMock->expects($this->once())
->method('addNameToResult')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addUrlRewriteToResult')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addParentPathFilter')
->with(self::PARENT_PATH)
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addStoreFilter')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addIsActiveFilter')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addSortedField')
->with(self::SORTED)
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('addAttributeToFilter')
->with('include_in_menu', 1)
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionMock->expects($this->once())
->method('load')
->willReturn($this->categoryCollectionMock);
$this->categoryCollectionFactoryMock->expects($this->once())
->method('create')
->willReturn($this->categoryCollectionMock);

$this->model = $this->objectManager->getObject(
Flat::class,
[
'context' => $this->contextMock,
'storeManager' => $this->storeManagerMock,
]
);

$reflection = new \ReflectionClass(get_class($this->model));
$reflectionProperty = $reflection->getProperty('categoryFlatCollectionFactory');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->model, $this->categoryCollectionFactoryMock);

$this->assertEquals(
$this->model->getCategories(self::PARENT, self::RECURSION_LEVEL, self::SORTED, true),
$this->categoryCollectionMock
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ public function __construct(
*/
public function filter($data)
{
$inputFilter = new \Zend_Filter_Input(
['custom_theme_from' => $this->dateFilter, 'custom_theme_to' => $this->dateFilter],
[],
$data
);
$data = $inputFilter->getUnescaped();
return $data;
$filterRules = [];

foreach (['custom_theme_from', 'custom_theme_to'] as $dateField) {
if (!empty($data[$dateField])) {
$filterRules[$dateField] = $this->dateFilter;
}
}

return (new \Zend_Filter_Input($filterRules, [], $data))->getUnescaped();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Cms/Model/ResourceModel/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ protected function _beforeSave(AbstractModel $object)
* type NULL so in DB they will be empty and not some default value
*/
foreach (['custom_theme_from', 'custom_theme_to'] as $field) {
$value = !$object->getData($field) ? null : $object->getData($field);
$object->setData($field, $this->dateTime->formatDate($value));
$value = !$object->getData($field) ? null : $this->dateTime->formatDate($object->getData($field));
$object->setData($field, $value);
}

if (!$this->isValidPageIdentifier($object)) {
Expand Down
Loading

0 comments on commit 8e2057c

Please sign in to comment.