public function testAddFieldToFilter() { $field = 'title'; $value = 'test_filter'; $searchSql = 'sql query'; $this->connection->expects($this->any())->method('quoteIdentifier')->willReturn($searchSql); $this->connection->expects($this->any())->method('prepareSqlCondition')->willReturn($searchSql); $this->select->expects($this->once())->method('where')->with($searchSql, null, \Magento\Framework\DB\Select::TYPE_CONDITION); $this->assertSame($this->collection, $this->collection->addFieldToFilter($field, $value)); }
/** * Get data * * @return array */ public function getData() { if (isset($this->loadedData)) { return $this->loadedData; } $items = $this->collection->getItems(); /** @var \Magento\Cms\Model\Block $block */ foreach ($items as $block) { $this->loadedData[$block->getId()] = $block->getData(); } return $this->loadedData; }
public function testMassDeleteAction() { $deletedBlocksCount = 2; $collection = [$this->getBlockMock(), $this->getBlockMock()]; $this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->blockCollectionMock); $this->filterMock->expects($this->once())->method('getCollection')->with($this->blockCollectionMock)->willReturn($this->blockCollectionMock); $this->blockCollectionMock->expects($this->once())->method('getSize')->willReturn($deletedBlocksCount); $this->blockCollectionMock->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator($collection)); $this->messageManagerMock->expects($this->once())->method('addSuccess')->with(__('A total of %1 record(s) have been deleted.', $deletedBlocksCount)); $this->messageManagerMock->expects($this->never())->method('addError'); $this->resultRedirectMock->expects($this->once())->method('setPath')->with('*/*/')->willReturnSelf(); $this->assertSame($this->resultRedirectMock, $this->massDeleteController->executeInternal()); }
/** * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory * @param \Psr\Log\LoggerInterface $logger * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy * @param \Magento\Framework\Event\ManagerInterface $eventManager * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool * @param string $mainTable * @param string $eventPrefix * @param string $eventObject * @param string $resourceModel * @param string $model * @param string|null $connection * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct(\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Model\Entity\MetadataPool $metadataPool, $mainTable, $eventPrefix, $eventObject, $resourceModel, $model = 'Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\Document', $connection = null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $storeManager, $metadataPool, $connection, $resource); $this->_eventPrefix = $eventPrefix; $this->_eventObject = $eventObject; $this->_init($model, $resourceModel); $this->setMainTable($mainTable); }
/** * Get data * * @return array */ public function getData() { if (isset($this->loadedData)) { return $this->loadedData; } $items = $this->collection->getItems(); /** @var \Magento\Cms\Model\Block $block */ foreach ($items as $block) { $this->loadedData[$block->getId()] = $block->getData(); } $data = $this->dataPersistor->get('cms_block'); if (!empty($data)) { $block = $this->collection->getNewEmptyItem(); $block->setData($data); $this->loadedData[$block->getId()] = $block->getData(); } return $this->loadedData; }
/** * @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\\EntityManager\\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()); }
/** * @test */ public function testGetList() { $field = 'name'; $value = 'magento'; $condition = 'eq'; $total = 10; $currentPage = 3; $pageSize = 2; $sortField = 'id'; $criteria = $this->getMockBuilder('Magento\\Framework\\Api\\SearchCriteriaInterface')->getMock(); $filterGroup = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\FilterGroup')->getMock(); $filter = $this->getMockBuilder('Magento\\Framework\\Api\\Filter')->getMock(); $storeFilter = $this->getMockBuilder('Magento\\Framework\\Api\\Filter')->getMock(); $sortOrder = $this->getMockBuilder('Magento\\Framework\\Api\\SortOrder')->getMock(); $criteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $criteria->expects($this->once())->method('getSortOrders')->willReturn([$sortOrder]); $criteria->expects($this->once())->method('getCurrentPage')->willReturn($currentPage); $criteria->expects($this->once())->method('getPageSize')->willReturn($pageSize); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$storeFilter, $filter]); $filter->expects($this->once())->method('getConditionType')->willReturn($condition); $filter->expects($this->any())->method('getField')->willReturn($field); $filter->expects($this->once())->method('getValue')->willReturn($value); $storeFilter->expects($this->any())->method('getField')->willReturn('store_id'); $storeFilter->expects($this->once())->method('getValue')->willReturn(1); $sortOrder->expects($this->once())->method('getField')->willReturn($sortField); $sortOrder->expects($this->once())->method('getDirection')->willReturn(SortOrder::SORT_DESC); /** @var \Magento\Framework\Api\SearchCriteriaInterface $criteria */ $this->collection->addItem($this->block); $this->blockSearchResult->expects($this->once())->method('setSearchCriteria')->with($criteria)->willReturnSelf(); $this->collection->expects($this->once())->method('addFieldToFilter')->with($field, [$condition => $value])->willReturnSelf(); $this->blockSearchResult->expects($this->once())->method('setTotalCount')->with($total)->willReturnSelf(); $this->collection->expects($this->once())->method('getSize')->willReturn($total); $this->collection->expects($this->once())->method('setCurPage')->with($currentPage)->willReturnSelf(); $this->collection->expects($this->once())->method('setPageSize')->with($pageSize)->willReturnSelf(); $this->collection->expects($this->once())->method('addOrder')->with($sortField, 'DESC')->willReturnSelf(); $this->block->expects($this->once())->method('getData')->willReturn(['data']); $this->blockSearchResult->expects($this->once())->method('setItems')->with(['someData'])->willReturnSelf(); $this->dataHelper->expects($this->once())->method('populateWithArray')->with($this->blockData, ['data'], 'Magento\\Cms\\Api\\Data\\BlockInterface'); $this->dataObjectProcessor->expects($this->once())->method('buildOutputDataArray')->with($this->blockData, 'Magento\\Cms\\Api\\Data\\BlockInterface')->willReturn('someData'); $this->assertEquals($this->blockSearchResult, $this->repository->getList($criteria)); }