/** * Run test toOptionArray method * * @return void */ public function testToOptionArray() { $pageCollectionMock = $this->getMock('Magento\\Cms\\Model\\ResourceModel\\Page\\Collection', [], [], '', false); $this->collectionFactory->expects($this->once())->method('create')->will($this->returnValue($pageCollectionMock)); $pageCollectionMock->expects($this->once())->method('toOptionIdArray')->will($this->returnValue('return-value')); $this->assertEquals('return-value', $this->page->toOptionArray()); }
/** * To option array * * @return array */ public function toOptionArray() { if (!$this->options) { $this->options = $this->collectionFactory->create()->toOptionIdArray(); } return $this->options; }
/** * @param string $name * @param string $primaryFieldName * @param string $requestFieldName * @param CollectionFactory $pageCollectionFactory * @param DataPersistorInterface $dataPersistor * @param array $meta * @param array $data */ public function __construct($name, $primaryFieldName, $requestFieldName, CollectionFactory $pageCollectionFactory, DataPersistorInterface $dataPersistor, array $meta = [], array $data = []) { $this->collection = $pageCollectionFactory->create(); $this->dataPersistor = $dataPersistor; parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); $this->meta = $this->prepareMeta($this->meta); }
/** * Prepare collection * * @return \Magento\Backend\Block\Widget\Grid */ protected function _prepareCollection() { $collection = $this->_collectionFactory->create(); /* @var $collection \Magento\Cms\Model\ResourceModel\Page\Collection */ $collection->setFirstStoreFlag(true); $this->setCollection($collection); return parent::_prepareCollection(); }
/** * Execute action * * @return \Magento\Backend\Model\View\Result\Redirect * @throws \Magento\Framework\Exception\LocalizedException|\Exception */ public function execute() { $collection = $this->filter->getCollection($this->collectionFactory->create()); $collectionSize = $collection->getSize(); foreach ($collection as $page) { $page->delete(); } $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $collectionSize)); /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); return $resultRedirect->setPath('*/*/'); }
/** * Execute action * * @return \Magento\Backend\Model\View\Result\Redirect * @throws \Magento\Framework\Exception\LocalizedException|\Exception */ public function executeInternal() { $collection = $this->filter->getCollection($this->collectionFactory->create()); foreach ($collection as $item) { $item->setIsActive(true); $item->save(); } $this->messageManager->addSuccess(__('A total of %1 record(s) have been enabled.', $collection->getSize())); /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); return $resultRedirect->setPath('*/*/'); }
public function testMassEnableAction() { $enabledPagesCount = 2; $collection = [$this->getPageMock(), $this->getPageMock()]; $this->collectionFactoryMock->expects($this->once())->method('create')->willReturn($this->pageCollectionMock); $this->filterMock->expects($this->once())->method('getCollection')->with($this->pageCollectionMock)->willReturn($this->pageCollectionMock); $this->pageCollectionMock->expects($this->once())->method('getSize')->willReturn($enabledPagesCount); $this->pageCollectionMock->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 enabled.', $enabledPagesCount)); $this->messageManagerMock->expects($this->never())->method('addError'); $this->resultRedirectMock->expects($this->once())->method('setPath')->with('*/*/')->willReturnSelf(); $this->assertSame($this->resultRedirectMock, $this->massEnableController->execute()); }
/** * Load Page data collection by given search criteria * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @param \Magento\Framework\Api\SearchCriteriaInterface $criteria * @return \Magento\Cms\Model\ResourceModel\Page\Collection */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $criteria) { $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($criteria); $collection = $this->pageCollectionFactory->create(); foreach ($criteria->getFilterGroups() as $filterGroup) { foreach ($filterGroup->getFilters() as $filter) { if ($filter->getField() === 'store_id') { $collection->addStoreFilter($filter->getValue(), false); continue; } $condition = $filter->getConditionType() ?: 'eq'; $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]); } } $searchResults->setTotalCount($collection->getSize()); $sortOrders = $criteria->getSortOrders(); if ($sortOrders) { /** @var SortOrder $sortOrder */ foreach ($sortOrders as $sortOrder) { $collection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SortOrder::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($criteria->getCurrentPage()); $collection->setPageSize($criteria->getPageSize()); $pages = []; /** @var Page $pageModel */ foreach ($collection as $pageModel) { $pageData = $this->dataPageFactory->create(); $this->dataObjectHelper->populateWithArray($pageData, $pageModel->getData(), 'Magento\\Cms\\Api\\Data\\PageInterface'); $pages[] = $this->dataObjectProcessor->buildOutputDataArray($pageData, 'Magento\\Cms\\Api\\Data\\PageInterface'); } $searchResults->setItems($pages); return $searchResults; }
/** * {@inheritdoc} */ public function getSearchableEntities($storeId, $entityIds = null, $lastEntityId = null, $limit = 100) { $collection = $this->collectionFactory->create()->addStoreFilter($storeId)->addFieldToFilter('is_active', 1); $ignored = $this->getModel()->getProperty('ignored_pages'); if (is_array($ignored) && count($ignored)) { $collection->addFieldToFilter('identifier', ['nin' => $ignored]); } if ($entityIds) { $collection->addFieldToFilter('page_id', $entityIds); } $collection->addFieldToFilter('page_id', ['gt' => $lastEntityId])->setPageSize($limit)->setOrder('page_id'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); try { /** @var \Magento\Store\Model\App\Emulation $emulation */ $emulation = $objectManager->create('Magento\\Store\\Model\\App\\Emulation'); $emulation->startEnvironmentEmulation($storeId, true); /** @var \Magento\Cms\Model\Page $page */ foreach ($collection as $page) { $template = $this->emailTemplateFactory->create(); $template->emulateDesign($storeId); $template->setTemplateText($page->getContent())->setIsPlain(false); $template->setTemplateFilter($this->cmsFilterProvider->getPageFilter()); $html = $template->getProcessedTemplate([]); $page->setContent($page->getContent() . $html); } $emulation->stopEnvironmentEmulation(); } catch (\Exception $e) { } return $collection; }
/** * Constructor * * @param string $name * @param string $primaryFieldName * @param string $requestFieldName * @param CollectionFactory $pageCollectionFactory * @param FilterPool $filterPool * @param array $meta * @param array $data */ public function __construct($name, $primaryFieldName, $requestFieldName, CollectionFactory $pageCollectionFactory, FilterPool $filterPool, array $meta = [], array $data = []) { parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); $this->collection = $pageCollectionFactory->create(); $this->filterPool = $filterPool; }