Skip to content

Extracted logic from wysiwyg OnInsert controller to a model #29677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;

class OnInsert extends \Magento\Cms\Controller\Adminhtml\Wysiwyg\Images
use Magento\Backend\App\Action\Context;
use Magento\Cms\Controller\Adminhtml\Wysiwyg\Images;
use Magento\Cms\Model\Wysiwyg\Images\GetInsertImageContent;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Registry;

class OnInsert extends Images implements HttpPostActionInterface
{
/**
* @var \Magento\Framework\Controller\Result\RawFactory
* @var RawFactory
*/
protected $resultRawFactory;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
* @var GetInsertImageContent
*/
private $getInsertImageContent;

/**
* @param Context $context
* @param Registry $coreRegistry
* @param RawFactory $resultRawFactory
* @param GetInsertImageContent $getInsertImageContent
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
Context $context,
Registry $coreRegistry,
RawFactory $resultRawFactory,
?GetInsertImageContent $getInsertImageContent = null
) {
$this->resultRawFactory = $resultRawFactory;
parent::__construct($context, $coreRegistry);
$this->getInsertImageContent = $getInsertImageContent ?: $this->_objectManager
->get(GetInsertImageContent::class);
}

/**
* Fire when select image
* Return a content (just a link or an html block) for inserting image to the content
*
* @return \Magento\Framework\Controller\ResultInterface
* @return ResultInterface
*/
public function execute()
{
$imagesHelper = $this->_objectManager->get(\Magento\Cms\Helper\Wysiwyg\Images::class);
$request = $this->getRequest();

$storeId = $request->getParam('store');

$filename = $request->getParam('filename');
$filename = $imagesHelper->idDecode($filename);

$asIs = $request->getParam('as_is');

$forceStaticPath = $request->getParam('force_static_path');

$this->_objectManager->get(\Magento\Catalog\Helper\Data::class)->setStoreId($storeId);
$imagesHelper->setStoreId($storeId);

if ($forceStaticPath) {
$image = parse_url($imagesHelper->getCurrentUrl() . $filename, PHP_URL_PATH);
} else {
$image = $imagesHelper->getImageHtmlDeclaration($filename, $asIs);
}

/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create();
return $resultRaw->setContents($image);
$data = $this->getRequest()->getParams();
return $this->resultRawFactory->create()->setContents(
$this->getInsertImageContent->execute(
$data['filename'],
$data['force_static_path'],
$data['as_is'],
isset($data['store']) ? (int) $data['store'] : null
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Cms\Model\Wysiwyg\Images;

use Magento\Catalog\Helper\Data as CatalogHelper;
use Magento\Cms\Helper\Wysiwyg\Images as ImagesHelper;

class GetInsertImageContent
{
/**
* @var ImagesHelper
*/
private $imagesHelper;

/**
* @var CatalogHelper
*/
private $catalogHelper;

/**
* @param ImagesHelper $imagesHelper
* @param CatalogHelper $catalogHelper
*/
public function __construct(ImagesHelper $imagesHelper, CatalogHelper $catalogHelper)
{
$this->imagesHelper = $imagesHelper;
$this->catalogHelper = $catalogHelper;
}

/**
* Create a content (just a link or an html block) for inserting image to the content
*
* @param string $encodedFilename
* @param bool $forceStaticPath
* @param bool $renderAsTag
* @param int|null $storeId
* @return string
*/
public function execute(
string $encodedFilename,
bool $forceStaticPath,
bool $renderAsTag,
?int $storeId = null
): string {
$filename = $this->imagesHelper->idDecode($encodedFilename);

$this->catalogHelper->setStoreId($storeId);
$this->imagesHelper->setStoreId($storeId);

if ($forceStaticPath) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
return parse_url($this->imagesHelper->getCurrentUrl() . $filename, PHP_URL_PATH);
}

return $this->imagesHelper->getImageHtmlDeclaration($filename, $renderAsTag);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Cms\Model\Wysiwyg\Images;

use Magento\Backend\Model\UrlInterface;
use Magento\Cms\Helper\Wysiwyg\Images as ImagesHelper;
use Magento\Framework\Url\EncoderInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class GetInsertImageContentTest extends TestCase
{
/**
* @var GetInsertImageContent
*/
private $getInsertImageContent;

/**
* @var ImagesHelper
*/
private $imagesHelper;

/**
* @var EncoderInterface
*/
private $urlEncoder;

/**
* @var UrlInterface
*/
protected $url;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->getInsertImageContent = Bootstrap::getObjectManager()->get(GetInsertImageContent::class);
$this->imagesHelper = Bootstrap::getObjectManager()->get(ImagesHelper::class);
$this->urlEncoder = Bootstrap::getObjectManager()->get(EncoderInterface::class);
$this->url = Bootstrap::getObjectManager()->get(UrlInterface::class);
}

/**
* Test for GetInsertImageContent::execute
*
* @dataProvider imageDataProvider
* @param string $filename
* @param bool $forceStaticPath
* @param bool $renderAsTag
* @param int|null $storeId
* @param string $expectedResult
*/
public function testExecute(
string $filename,
bool $forceStaticPath,
bool $renderAsTag,
?int $storeId,
string $expectedResult
): void {
if (!$forceStaticPath && !$renderAsTag && !$this->imagesHelper->isUsingStaticUrlsAllowed()) {
$expectedResult = $this->url->getUrl(
'cms/wysiwyg/directive',
[
'___directive' => $this->urlEncoder->encode($expectedResult),
'_escape_params' => false
]
);
}

$this->assertEquals(
$expectedResult,
$this->getInsertImageContent->execute(
$this->imagesHelper->idEncode($filename),
$forceStaticPath,
$renderAsTag,
$storeId
)
);
}

/**
* Data provider for testExecute
*
* @return array[]
*/
public function imageDataProvider(): array
{
return [
[
'test-image.jpg',
false,
true,
1,
'<img src="{{media url=&quot;test-image.jpg&quot;}}" alt="" />'
],
[
'catalog/category/test-image.jpg',
true,
false,
1,
'/pub/media/catalog/category/test-image.jpg'
],
[
'test-image.jpg',
false,
false,
1,
'{{media url="test-image.jpg"}}'
],
[
'/test-image.jpg',
false,
true,
2,
'<img src="{{media url=&quot;/test-image.jpg&quot;}}" alt="" />'
],
[
'test-image.jpg',
false,
true,
null,
'<img src="{{media url=&quot;test-image.jpg&quot;}}" alt="" />'
],
];
}
}