diff --git a/app/code/Magento/Cms/Model/Page/DataProvider.php b/app/code/Magento/Cms/Model/Page/DataProvider.php index ba5eab8dddccdb6f42445df250a7dcf50dea2479..fb9a54be74457519f80832db9a578bc9264c64f7 100644 --- a/app/code/Magento/Cms/Model/Page/DataProvider.php +++ b/app/code/Magento/Cms/Model/Page/DataProvider.php @@ -61,11 +61,10 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider if (isset($this->loadedData)) { return $this->loadedData; } - foreach ($this->collection->getAllIds() as $pageId) { - /** @var \Magento\Cms\Model\Page $page */ - $page = $this->collection->getNewEmptyItem(); - /** Load every record separately to make sure the list of associated stores is available */ - $this->loadedData[$pageId] = $page->load($pageId)->getData(); + $items = $this->collection->getItems(); + /** @var $page \Magento\Cms\Model\Page */ + foreach ($items as $page) { + $this->loadedData[$page->getId()] = $page->getData(); } $data = $this->dataPersistor->get('cms_page'); diff --git a/app/code/Magento/Cms/Model/ResourceModel/AbstractCollection.php b/app/code/Magento/Cms/Model/ResourceModel/AbstractCollection.php index 19d5cd446d8ebe73af32a3c9c6735c36983b6db4..809a9720c2a3545bc570de1ac3504a84537616dd 100644 --- a/app/code/Magento/Cms/Model/ResourceModel/AbstractCollection.php +++ b/app/code/Magento/Cms/Model/ResourceModel/AbstractCollection.php @@ -5,6 +5,8 @@ */ namespace Magento\Cms\Model\ResourceModel; +use Magento\Store\Model\Store; + /** * Abstract collection of CMS pages and blocks */ @@ -61,24 +63,30 @@ abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel $connection = $this->getConnection(); $select = $connection->select()->from(['cms_entity_store' => $this->getTable($tableName)]) ->where('cms_entity_store.' . $linkField . ' IN (?)', $linkedIds); - $result = $connection->fetchPairs($select); + $result = $connection->fetchAll($select); if ($result) { + $storesData = []; + foreach ($result as $storeData) { + $storesData[$storeData[$linkField]][] = $storeData['store_id']; + } + foreach ($this as $item) { $linkedId = $item->getData($linkField); - if (!isset($result[$linkedId])) { + if (!isset($storesData[$linkedId])) { continue; } - if ($result[$linkedId] == 0) { + $storeIdKey = array_search(Store::DEFAULT_STORE_ID, $storesData[$linkedId], true); + if ($storeIdKey !== false) { $stores = $this->storeManager->getStores(false, true); $storeId = current($stores)->getId(); $storeCode = key($stores); } else { - $storeId = $result[$linkedId]; + $storeId = current($storesData[$linkedId]); $storeCode = $this->storeManager->getStore($storeId)->getCode(); } $item->setData('_first_store_id', $storeId); $item->setData('store_code', $storeCode); - $item->setData('store_id', [$result[$linkedId]]); + $item->setData('store_id', $storesData[$linkedId]); } } } @@ -103,7 +111,7 @@ abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel /** * Add filter by store * - * @param int|array|\Magento\Store\Model\Store $store + * @param int|array|Store $store * @param bool $withAdmin * @return $this */ @@ -112,13 +120,13 @@ abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel /** * Perform adding filter by store * - * @param int|array|\Magento\Store\Model\Store $store + * @param int|array|Store $store * @param bool $withAdmin * @return void */ protected function performAddStoreFilter($store, $withAdmin = true) { - if ($store instanceof \Magento\Store\Model\Store) { + if ($store instanceof Store) { $store = [$store->getId()]; } @@ -127,7 +135,7 @@ abstract class AbstractCollection extends \Magento\Framework\Model\ResourceModel } if ($withAdmin) { - $store[] = \Magento\Store\Model\Store::DEFAULT_STORE_ID; + $store[] = Store::DEFAULT_STORE_ID; } $this->addFilter('store', ['in' => $store], 'public'); diff --git a/app/code/Magento/Cms/Model/ResourceModel/Page/Grid/Collection.php b/app/code/Magento/Cms/Model/ResourceModel/Page/Grid/Collection.php index b1bcb33b69d7ecc42375e4f07f0af2933e4e41a0..ed54e6960151f01c3e968b30e0dc34765f2a6276 100644 --- a/app/code/Magento/Cms/Model/ResourceModel/Page/Grid/Collection.php +++ b/app/code/Magento/Cms/Model/ResourceModel/Page/Grid/Collection.php @@ -5,11 +5,9 @@ */ namespace Magento\Cms\Model\ResourceModel\Page\Grid; -use Magento\Cms\Model\Page; use Magento\Framework\Api\Search\SearchResultInterface; use Magento\Framework\Api\Search\AggregationInterface; use Magento\Cms\Model\ResourceModel\Page\Collection as PageCollection; -use Magento\Framework\View\Element\UiComponent\DataProvider\Document; /** * Class Collection @@ -22,11 +20,6 @@ class Collection extends PageCollection implements SearchResultInterface */ protected $aggregations; - /** - * @var \Magento\Framework\View\Element\UiComponent\DataProvider\Document[] - */ - private $loadedData = []; - /** * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory * @param \Psr\Log\LoggerInterface $logger @@ -147,23 +140,4 @@ class Collection extends PageCollection implements SearchResultInterface { return $this; } - - /** - * {@inheritdoc} - */ - public function getItems() - { - if ($this->loadedData) { - return $this->loadedData; - } - - /** @var Document $pageDocument */ - foreach (parent::getItems() as $pageDocument) { - $this->loadedData[$pageDocument->getId()] = $pageDocument->setData( - $this->_entityFactory->create(Page::class)->load($pageDocument->getId())->getData() - ); - } - - return $this->loadedData; - } } diff --git a/app/code/Magento/Cms/Test/Unit/Model/ResourceModel/Block/CollectionTest.php b/app/code/Magento/Cms/Test/Unit/Model/ResourceModel/Block/CollectionTest.php index c637747b78459d4138d37e785680ccecbb5f4565..fd62f62a72159e3fc2f7c3a32da35b985cbbdb35 100644 --- a/app/code/Magento/Cms/Test/Unit/Model/ResourceModel/Block/CollectionTest.php +++ b/app/code/Magento/Cms/Test/Unit/Model/ResourceModel/Block/CollectionTest.php @@ -15,15 +15,34 @@ class CollectionTest extends AbstractCollectionTest */ protected $collection; + /** + * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $storeManagerMock; + + /** + * @var \Magento\Framework\Model\Entity\MetadataPool|\PHPUnit_Framework_MockObject_MockObject + */ + protected $metadataPoolMock; + protected function setUp() { parent::setUp(); + $this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') + ->getMockForAbstractClass(); + + $this->metadataPoolMock = $this->getMockBuilder('Magento\Framework\Model\Entity\MetadataPool') + ->disableOriginalConstructor() + ->getMock(); + $this->collection = $this->objectManager->getObject( 'Magento\Cms\Model\ResourceModel\Block\Collection', [ 'resource' => $this->resource, - 'connection' => $this->connection + 'connection' => $this->connection, + 'storeManager' => $this->storeManagerMock, + 'metadataPool' => $this->metadataPoolMock, ] ); } @@ -60,4 +79,60 @@ class CollectionTest extends AbstractCollectionTest $this->assertSame($this->collection, $this->collection->addFieldToFilter($field, $value)); } + + /** + * @param \Magento\Framework\DataObject $item + * @param array $storesData + * @dataProvider getItemsDataProvider + * @throws \Exception + */ + public function testAfterLoad($item, $storesData) + { + $linkField = 'row_id'; + + $expectedResult = []; + foreach ($storesData as $storeData) { + $expectedResult[$storeData[$linkField]][] = $storeData['store_id']; + } + + $entityMetadataMock = $this->getMockBuilder('Magento\Framework\Model\Entity\EntityMetadata') + ->disableOriginalConstructor() + ->getMock(); + $entityMetadataMock->expects($this->any())->method('getLinkField')->willReturn($linkField); + $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($entityMetadataMock); + + $this->select->expects($this->any())->method('from')->willReturnSelf(); + $this->connection->expects($this->any())->method('fetchAll')->willReturn($storesData); + + $storeDataMock = $this->getMockBuilder('Magento\Store\Api\Data\StoreInterface')->getMockForAbstractClass(); + $storeDataMock->expects($this->any())->method('getId')->willReturn(current($expectedResult[$item->getId()])); + $storeDataMock->expects($this->any())->method('getCode')->willReturn('some_code'); + $this->storeManagerMock->expects($this->any())->method('getStores')->willReturn([$storeDataMock]); + $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeDataMock); + + $this->collection->addItem($item); + + $this->assertEmpty($item->getStoreId()); + $this->collection->load(); + $this->assertEquals($expectedResult[$item->getId()], $item->getStoreId()); + } + + public function getItemsDataProvider() + { + return [ + [ + new \Magento\Framework\DataObject(['id' => 1, 'row_id' => 1]), + [ + ['row_id' => 1, 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID], + ], + ], + [ + new \Magento\Framework\DataObject(['id' => 2, 'row_id' => 2]), + [ + ['row_id' => 2, 'store_id' => 1], + ['row_id' => 2, 'store_id' => 2], + ], + ], + ]; + } } diff --git a/app/code/Magento/Cms/Test/Unit/Model/ResourceModel/Page/CollectionTest.php b/app/code/Magento/Cms/Test/Unit/Model/ResourceModel/Page/CollectionTest.php index 8ded27a352df708c6804e5701fc69a95ead2fab0..916e1f96c844778c83dd5a75f3b1a68fd4aa35f6 100644 --- a/app/code/Magento/Cms/Test/Unit/Model/ResourceModel/Page/CollectionTest.php +++ b/app/code/Magento/Cms/Test/Unit/Model/ResourceModel/Page/CollectionTest.php @@ -15,15 +15,34 @@ class CollectionTest extends AbstractCollectionTest */ protected $collection; + /** + * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $storeManagerMock; + + /** + * @var \Magento\Framework\Model\Entity\MetadataPool|\PHPUnit_Framework_MockObject_MockObject + */ + protected $metadataPoolMock; + protected function setUp() { parent::setUp(); + $this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') + ->getMockForAbstractClass(); + + $this->metadataPoolMock = $this->getMockBuilder('Magento\Framework\Model\Entity\MetadataPool') + ->disableOriginalConstructor() + ->getMock(); + $this->collection = $this->objectManager->getObject( 'Magento\Cms\Model\ResourceModel\Page\Collection', [ 'resource' => $this->resource, - 'connection' => $this->connection + 'connection' => $this->connection, + 'storeManager' => $this->storeManagerMock, + 'metadataPool' => $this->metadataPoolMock, ] ); } @@ -60,4 +79,60 @@ class CollectionTest extends AbstractCollectionTest $this->assertSame($this->collection, $this->collection->addFieldToFilter($field, $value)); } + + /** + * @param \Magento\Framework\DataObject $item + * @param array $storesData + * @dataProvider getItemsDataProvider + * @throws \Exception + */ + public function testAfterLoad($item, $storesData) + { + $linkField = 'row_id'; + + $expectedResult = []; + foreach ($storesData as $storeData) { + $expectedResult[$storeData[$linkField]][] = $storeData['store_id']; + } + + $entityMetadataMock = $this->getMockBuilder('Magento\Framework\Model\Entity\EntityMetadata') + ->disableOriginalConstructor() + ->getMock(); + $entityMetadataMock->expects($this->any())->method('getLinkField')->willReturn($linkField); + $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($entityMetadataMock); + + $this->select->expects($this->any())->method('from')->willReturnSelf(); + $this->connection->expects($this->any())->method('fetchAll')->willReturn($storesData); + + $storeDataMock = $this->getMockBuilder('Magento\Store\Api\Data\StoreInterface')->getMockForAbstractClass(); + $storeDataMock->expects($this->any())->method('getId')->willReturn(current($expectedResult[$item->getId()])); + $storeDataMock->expects($this->any())->method('getCode')->willReturn('some_code'); + $this->storeManagerMock->expects($this->any())->method('getStores')->willReturn([$storeDataMock]); + $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeDataMock); + + $this->collection->addItem($item); + + $this->assertEmpty($item->getStoreId()); + $this->collection->load(); + $this->assertEquals($expectedResult[$item->getId()], $item->getStoreId()); + } + + public function getItemsDataProvider() + { + return [ + [ + new \Magento\Framework\DataObject(['id' => 1, 'row_id' => 1]), + [ + ['row_id' => 1, 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID], + ], + ], + [ + new \Magento\Framework\DataObject(['id' => 2, 'row_id' => 2]), + [ + ['row_id' => 2, 'store_id' => 1], + ['row_id' => 2, 'store_id' => 2], + ], + ], + ]; + } }