From 94fdca7b7a46eaf032c8314dc81428ba8aec1126 Mon Sep 17 00:00:00 2001 From: nmalevanec <mikola.malevanec@transoftgroup.com> Date: Thu, 11 Jan 2018 18:12:31 +0200 Subject: [PATCH] magento/magento2#12147: The function "isUsingStaticUrlsAllowed" (configuration setting "cms/wysiwyg/use_static_urls_in_catalog") doesn't have any effect with the WYSIWYG editor image insertion --- ...gCheckIsUsingStaticUrlsAllowedObserver.php | 2 +- ...ckIsUsingStaticUrlsAllowedObserverTest.php | 104 ------------------ .../Adminhtml/Wysiwyg/Images/OnInsert.php | 2 +- .../Adminhtml/Wysiwyg/Images/OnInsertTest.php | 65 +++++++++++ 4 files changed, 67 insertions(+), 106 deletions(-) delete mode 100644 app/code/Magento/Catalog/Test/Unit/Observer/CatalogCheckIsUsingStaticUrlsAllowedObserverTest.php create mode 100644 dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsertTest.php diff --git a/app/code/Magento/Catalog/Observer/CatalogCheckIsUsingStaticUrlsAllowedObserver.php b/app/code/Magento/Catalog/Observer/CatalogCheckIsUsingStaticUrlsAllowedObserver.php index cb05f1b14e7..7fe428dffa6 100644 --- a/app/code/Magento/Catalog/Observer/CatalogCheckIsUsingStaticUrlsAllowedObserver.php +++ b/app/code/Magento/Catalog/Observer/CatalogCheckIsUsingStaticUrlsAllowedObserver.php @@ -32,7 +32,7 @@ class CatalogCheckIsUsingStaticUrlsAllowedObserver implements ObserverInterface */ public function execute(\Magento\Framework\Event\Observer $observer) { - $storeId = (int)$observer->getEvent()->getData('store_id'); + $storeId = $observer->getEvent()->getData('store_id'); $result = $observer->getEvent()->getData('result'); $result->isAllowed = $this->catalogData->setStoreId($storeId)->isUsingStaticUrlsAllowed(); } diff --git a/app/code/Magento/Catalog/Test/Unit/Observer/CatalogCheckIsUsingStaticUrlsAllowedObserverTest.php b/app/code/Magento/Catalog/Test/Unit/Observer/CatalogCheckIsUsingStaticUrlsAllowedObserverTest.php deleted file mode 100644 index c6b41b8d76e..00000000000 --- a/app/code/Magento/Catalog/Test/Unit/Observer/CatalogCheckIsUsingStaticUrlsAllowedObserverTest.php +++ /dev/null @@ -1,104 +0,0 @@ -<?php -/** - * Copyright © Magento, Inc. All rights reserved. - * See COPYING.txt for license details. - */ - -namespace Magento\Catalog\Test\Unit\Observer; - -use Magento\Catalog\Helper\Data; -use Magento\Catalog\Observer\CatalogCheckIsUsingStaticUrlsAllowedObserver; -use Magento\Framework\Event; -use Magento\Framework\Event\Observer; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; -use PHPUnit\Framework\TestCase; - -/** - * Provide tests for CatalogCheckIsUsingStaticUrlsAllowedObserver observer. - */ -class CatalogCheckIsUsingStaticUrlsAllowedObserverTest extends TestCase -{ - /** - * Test subject. - * - * @var CatalogCheckIsUsingStaticUrlsAllowedObserver - */ - private $model; - - /** - * @var Data|\PHPUnit_Framework_MockObject_MockObject - */ - private $catalogData; - - /** - * @inheritdoc - */ - protected function setUp() - { - $objectManager = new ObjectManager($this); - $this->catalogData = $this->getMockBuilder(Data::class) - ->disableOriginalConstructor() - ->getMock(); - $this->model = $objectManager->getObject( - CatalogCheckIsUsingStaticUrlsAllowedObserver::class, - ['catalogData' => $this->catalogData] - ); - } - - /** - * Test observer can correctly handle non integer store id values. - * - * @dataProvider executeDataProvider - * @param string|int $storeId - * @return void - */ - public function testExecute($storeId) - { - $result = new \stdClass(); - /** @var Observer|\PHPUnit_Framework_MockObject_MockObject $observer */ - $observer = $this->getMockBuilder(Observer::class) - ->disableOriginalConstructor() - ->getMock(); - $event = $this->getMockBuilder(Event::class) - ->disableOriginalConstructor() - ->getMock(); - $event->expects($this->exactly(2)) - ->method('getData') - ->withConsecutive( - $this->identicalTo('store_id'), - $this->identicalTo('result') - )->willReturnOnConsecutiveCalls( - $storeId, - $result - ); - $observer->expects($this->exactly(2)) - ->method('getEvent') - ->willReturn($event); - $this->catalogData->expects($this->once()) - ->method('setStoreId') - ->with(0) - ->willReturnSelf(); - $this->catalogData->expects($this->once()) - ->method('isUsingStaticUrlsAllowed') - ->willReturn(true); - $this->model->execute($observer); - $this->assertTrue($result->isAllowed); - } - - /** - * Provide test data for testExecute(). - * - * @return array - */ - public function executeDataProvider() - { - return [ - [ - 'store_id' => 0, - ], - [ - 'store_id' => '' - ] - ]; - } -} diff --git a/app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsert.php b/app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsert.php index 2daaf39d58d..dda3940cd9b 100644 --- a/app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsert.php +++ b/app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsert.php @@ -35,7 +35,7 @@ class OnInsert extends \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images public function execute() { $helper = $this->_objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class); - $storeId = $this->getRequest()->getParam('store'); + $storeId = (int)$this->getRequest()->getParam('store'); $filename = $this->getRequest()->getParam('filename'); $filename = $helper->idDecode($filename); diff --git a/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsertTest.php b/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsertTest.php new file mode 100644 index 00000000000..fe7788e12d1 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsertTest.php @@ -0,0 +1,65 @@ +<?php +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images; + +use Magento\Cms\Helper\Wysiwyg\Images; +use Magento\Framework\Data\Form\FormKey; +use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\TestCase\AbstractBackendController; + +/** + * Provide tests for OnInsert controller. + * @magentoAppArea adminhtml + */ +class OnInsertTest extends AbstractBackendController +{ + /** + * Test OnIsert with turned on static urls in catalog. + * + * @magentoConfigFixture admin_store cms/wysiwyg/use_static_urls_in_catalog 1 + * @return void + */ + public function testExecuteWhithStaticUrls() + { + $this->prepareRequest(); + $this->dispatch('backend/cms/wysiwyg_images/onInsert'); + $this->assertRegExp('/pub\/media\/wysiwyg\/testFilename/', $this->getResponse()->getBody()); + } + + /** + * Test OnIsert with turned off static urls in catalog. + * + * @magentoConfigFixture admin_store cms/wysiwyg/use_static_urls_in_catalog 0 + * @return void + */ + public function testExecuteWhithoutStaticUrls() + { + $this->prepareRequest(); + $this->dispatch('backend/cms/wysiwyg_images/onInsert'); + $this->assertRegExp('/cms\/wysiwyg\/directive\/___directive/', $this->getResponse()->getBody()); + } + + /** + * Set necessary post data into request. + * + * @return void + */ + private function prepareRequest() + { + $this->getRequest()->setParams( + [ + 'key' => 'testKey', + 'isAjax' => 'true', + 'filename' => Bootstrap::getObjectManager()->get(Images::class)->idEncode('testFilename'), + 'node' => 'root', + 'store' => '', + 'as_is' => '0', + 'form_key' => Bootstrap::getObjectManager()->get(FormKey::class)->getFormKey(), + ] + ); + } +} -- GitLab