/** * Get or create new instance of CMS page * * @return \Magento\Cms\Model\Page */ private function _getCmsPage() { if (!$this->hasData('cms_page')) { $this->setCmsPage($this->_pageFactory->create()); } return $this->getCmsPage(); }
/** * {@inheritdoc} */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); /* * Report Event Types default data */ $eventTypeData = [['event_type_id' => \Magento\Reports\Model\Event::EVENT_PRODUCT_VIEW, 'event_name' => 'catalog_product_view'], ['event_type_id' => \Magento\Reports\Model\Event::EVENT_PRODUCT_SEND, 'event_name' => 'sendfriend_product'], ['event_type_id' => \Magento\Reports\Model\Event::EVENT_PRODUCT_COMPARE, 'event_name' => 'catalog_product_compare_add_product'], ['event_type_id' => \Magento\Reports\Model\Event::EVENT_PRODUCT_TO_CART, 'event_name' => 'checkout_cart_add_product'], ['event_type_id' => \Magento\Reports\Model\Event::EVENT_PRODUCT_TO_WISHLIST, 'event_name' => 'wishlist_add_product'], ['event_type_id' => \Magento\Reports\Model\Event::EVENT_WISHLIST_SHARE, 'event_name' => 'wishlist_share']]; foreach ($eventTypeData as $row) { $setup->getConnection()->insertForce($setup->getTable('report_event_types'), $row); } /** * Prepare database after data upgrade */ $setup->endSetup(); /** * Cms Page with 'home' identifier page modification for report pages */ /** @var $cms \Magento\Cms\Model\Page */ $cms = $this->pageFactory->create(); $cms->load('home', 'identifier'); $reportLayoutUpdate = '<!-- <referenceContainer name="right"> <action method="unsetChild"><argument name="alias" xsi:type="string">right.reports.product.viewed</argument></action> <action method="unsetChild"><argument name="alias" xsi:type="string">right.reports.product.compared</argument></action> </referenceContainer>-->'; /* * Merge and save old layout update data with report layout data */ $cms->setLayoutUpdateXml($cms->getLayoutUpdateXml() . $reportLayoutUpdate)->save(); }
/** * {@inheritdoc} */ public function run() { $this->logger->log('Installing CMS pages:'); foreach ($this->fixtures as $file) { /** @var \Magento\SampleData\Helper\Csv\Reader $csvReader */ $fileName = $this->fixtureHelper->getPath($file); $csvReader = $this->csvReaderFactory->create(['fileName' => $fileName, 'mode' => 'r']); foreach ($csvReader as $row) { $this->pageFactory->create()->load($row['identifier'], 'identifier')->addData($row)->setStores([\Magento\Store\Model\Store::DEFAULT_STORE_ID])->save(); $this->logger->logInline('.'); } } }
/** * Prepare chooser element HTML * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element Form Element * @return \Magento\Framework\Data\Form\Element\AbstractElement */ public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $uniqId = $this->mathRandom->getUniqueHash($element->getId()); $sourceUrl = $this->getUrl('cms/page_widget/chooser', ['uniq_id' => $uniqId]); $chooser = $this->getLayout()->createBlock('Magento\\Widget\\Block\\Adminhtml\\Widget\\Chooser')->setElement($element)->setConfig($this->getConfig())->setFieldsetId($this->getFieldsetId())->setSourceUrl($sourceUrl)->setUniqId($uniqId); if ($element->getValue()) { $page = $this->_pageFactory->create()->load((int) $element->getValue()); if ($page->getId()) { $chooser->setLabel($this->escapeHtml($page->getTitle())); } } $element->setData('after_element_html', $chooser->toHtml()); return $element; }
/** * Retrieve Page instance * * @return \Magento\Cms\Model\Page */ public function getPage() { if (!$this->hasData('page')) { if ($this->getPageId()) { /** @var \Magento\Cms\Model\Page $page */ $page = $this->_pageFactory->create(); $page->setStoreId($this->_storeManager->getStore()->getId())->load($this->getPageId(), 'identifier'); } else { $page = $this->_page; } $this->setData('page', $page); } return $this->getData('page'); }
/** * Validate and Match Cms Page and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return bool * * @SuppressWarnings(PHPMD.ExitExpression) */ public function match(\Magento\Framework\App\RequestInterface $request) { if (!$this->_appState->isInstalled()) { $this->_response->setRedirect($this->_url->getUrl('install'))->sendResponse(); exit; } $identifier = trim($request->getPathInfo(), '/'); $condition = new \Magento\Framework\Object(array('identifier' => $identifier, 'continue' => true)); $this->_eventManager->dispatch('cms_controller_router_match_before', array('router' => $this, 'condition' => $condition)); $identifier = $condition->getIdentifier(); if ($condition->getRedirectUrl()) { $this->_response->setRedirect($condition->getRedirectUrl()); $request->setDispatched(true); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Redirect', array('request' => $request)); } if (!$condition->getContinue()) { return null; } /** @var \Magento\Cms\Model\Page $page */ $page = $this->_pageFactory->create(); $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId()); if (!$pageId) { return null; } $request->setModuleName('cms')->setControllerName('page')->setActionName('view')->setParam('page_id', $pageId); $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier); return $this->actionFactory->create('Magento\\Framework\\App\\Action\\Forward', array('request' => $request)); }
/** * Get CMS page model instance * * @return \Magento\Cms\Model\Page */ protected function getCmsPageInstance() { if (!$this->hasData('cms_page')) { $this->setCmsPage($this->_pageFactory->create()); } return $this->getCmsPage(); }
/** * Load Page data by given Page Identity * * @param string $pageId * @return Page * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getById($pageId) { $page = $this->pageFactory->create(); $this->resource->load($page, $pageId); if (!$page->getId()) { throw new NoSuchEntityException(__('CMS Page with id "%1" does not exist.', $pageId)); } return $page; }
/** * @param array $fixtures * @throws \Exception */ public function install(array $fixtures) { foreach ($fixtures as $fileName) { $fileName = $this->fixtureManager->getFixture($fileName); if (!file_exists($fileName)) { continue; } $rows = $this->csvReader->getData($fileName); $header = array_shift($rows); foreach ($rows as $row) { $data = []; foreach ($row as $key => $value) { $data[$header[$key]] = $value; } $row = $data; $this->pageFactory->create()->load($row['identifier'], 'identifier')->addData($row)->setStores([\Magento\Store\Model\Store::DEFAULT_STORE_ID])->save(); } } }
/** * Run test deleteById method * * @return void */ public function testDeleteById() { $id = 20; $pageMock = $this->getMockForAbstractClass('Magento\\Cms\\Model\\Page', [], '', false, true, true, ['getId']); $this->pageFactoryMock->expects($this->once())->method('create')->will($this->returnValue($pageMock)); $this->resourceMock->expects($this->once())->method('load')->with($pageMock, $id); $pageMock->expects($this->once())->method('getId')->will($this->returnValue($id)); $this->resourceMock->expects($this->once())->method('delete')->with($pageMock); $this->assertTrue($this->pageRepository->deleteById($id)); }
/** * Retrieve page direct URL * * @param string $pageId * @return string */ public function getPageUrl($pageId = null) { /** @var \Magento\Cms\Model\Page $page */ $page = $this->_pageFactory->create(); if ($pageId !== null && $pageId !== $page->getId()) { $page->setStoreId($this->_storeManager->getStore()->getId()); if (!$page->load($pageId)) { return null; } } if (!$page->getId()) { return null; } return $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]); }
/** * @covers \Magento\Cms\Helper\Page::getPageUrl * @param integer|null $pageId * @param integer|null $internalPageId * @param integer $pageLoadResultIndex * @param string|null $expectedResult * * @dataProvider getPageUrlDataProvider */ public function testGetPageUrl($pageId, $internalPageId, $pageLoadResultIndex, $expectedResult) { $storeId = 321; $pageIdentifier = 111; $url = '/some/url'; $pageLoadResultCollection = [null, $this->pageMock]; $this->pageFactoryMock->expects($this->any())->method('create')->willReturn($this->pageMock); $this->pageMock->expects($this->any())->method('getId')->willReturn($internalPageId); $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($this->storeMock); $this->storeMock->expects($this->any())->method('getId')->willReturn($storeId); $this->pageMock->expects($this->any())->method('setStoreId')->with($storeId)->willReturnSelf(); $this->pageMock->expects($this->any())->method('load')->with($pageId)->willReturn($pageLoadResultCollection[$pageLoadResultIndex]); $this->pageMock->expects($this->any())->method('getIdentifier')->willReturn($pageIdentifier); $this->urlBuilderMock->expects($this->any())->method('getUrl')->with(null, ['_direct' => $pageIdentifier])->willReturn($url); $this->assertEquals($expectedResult, $this->object->getPageUrl($pageId)); }
/** * @covers \Magento\Cms\Block\Adminhtml\Block\Widget\Chooser::prepareElementHtml * * @param string $elementValue * @param integer|null $cmsPageId * * @dataProvider prepareElementHtmlDataProvider */ public function testPrepareElementHtml($elementValue, $cmsPageId) { //$elementValue = 12345; //$cmsPageId = 1; $elementId = 1; $uniqId = '126hj4h3j73hk7b347jhkl37gb34'; $sourceUrl = 'cms/page_widget/chooser/126hj4h3j73hk7b347jhkl37gb34'; $config = ['key1' => 'value1']; $fieldsetId = 2; $html = 'some html'; $title = 'some "><img src=y onerror=prompt(document.domain)>; title'; $titleEscaped = 'some "><img src=y onerror=prompt(document.domain)>; title'; $this->this->setConfig($config); $this->this->setFieldsetId($fieldsetId); $this->elementMock->expects($this->atLeastOnce())->method('getId')->willReturn($elementId); $this->mathRandomMock->expects($this->atLeastOnce())->method('getUniqueHash')->with($elementId)->willReturn($uniqId); $this->urlBuilderMock->expects($this->atLeastOnce())->method('getUrl')->with('cms/page_widget/chooser', ['uniq_id' => $uniqId])->willReturn($sourceUrl); $this->layoutMock->expects($this->atLeastOnce())->method('createBlock')->with('Magento\\Widget\\Block\\Adminhtml\\Widget\\Chooser')->willReturn($this->chooserMock); $this->chooserMock->expects($this->atLeastOnce())->method('setElement')->with($this->elementMock)->willReturnSelf(); $this->chooserMock->expects($this->atLeastOnce())->method('setConfig')->with($config)->willReturnSelf(); $this->chooserMock->expects($this->atLeastOnce())->method('setFieldsetId')->with($fieldsetId)->willReturnSelf(); $this->chooserMock->expects($this->atLeastOnce())->method('setSourceUrl')->with($sourceUrl)->willReturnSelf(); $this->chooserMock->expects($this->atLeastOnce())->method('setUniqId')->with($uniqId)->willReturnSelf(); $this->elementMock->expects($this->atLeastOnce())->method('getValue')->willReturn($elementValue); $this->pageFactoryMock->expects($this->any())->method('create')->willReturn($this->cmsPageMock); $this->cmsPageMock->expects($this->any())->method('load')->with((int) $elementValue)->willReturnSelf(); $this->cmsPageMock->expects($this->any())->method('getId')->willReturn($cmsPageId); $this->cmsPageMock->expects($this->any())->method('getTitle')->willReturn($title); $this->chooserMock->expects($this->atLeastOnce())->method('toHtml')->willReturn($html); if (!empty($elementValue) && !empty($cmsPageId)) { $this->escaper->expects($this->atLeastOnce())->method('escapeHtml')->willReturn($titleEscaped); $this->chooserMock->expects($this->atLeastOnce())->method('setLabel')->with($titleEscaped)->willReturnSelf(); } $this->elementMock->expects($this->atLeastOnce())->method('setData')->with('after_element_html', $html)->willReturnSelf(); $this->assertEquals($this->elementMock, $this->this->prepareElementHtml($this->elementMock)); }
/** * Create page * * @return \Magento\Cms\Model\Page */ public function createPage() { return $this->_pageFactory->create(); }
/** * Retrieve news collection * * @return Magentostudy_News_Model_Resource_News_Collection */ protected function _getCollection() { $collection = $this->_newsCollectionFactory->create(); return $collection; }
/** * Create page * * @return Page */ private function createPage() { return $this->pageFactory->create(); }