-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
magento-engcom-team
merged 12 commits into
magento:2.4-develop
from
jmonteros422:1504-M2-repo-Insert-rendition-images-to-the-content-from-media-gallery-instead-of-original-images
Sep 2, 2020
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff37e3f
magento/adobe-stock-integration#1504:[Related PR] Insert rendition im…
jmonteros422 29d0707
magento/adobe-stock-integration#1504: Insert rendition images to the …
jmonteros422 c06a44c
Merge branch '2.4-develop' into 1504-M2-repo-Insert-rendition-images-…
jmonteros422 e228d76
magento/adobe-stock-integration#1504:Extract logic from controller to…
jmonteros422 408cdb7
magento/adobe-stock-integration#1504:Extract logic from controller to…
jmonteros422 45849cc
magento/adobe-stock-integration#1504: Extracted logic from wysiwyg On…
jmonteros422 10bd41f
magento/adobe-stock-integration#1504: Extracted logic from wysiwyg On…
jmonteros422 d93f3d2
magento/magento2#29677: Fixed static tests
sivaschenko 133e853
Corrected controller interface
sivaschenko 6f1f676
magento/magento2#29677: Corrected property access level
sivaschenko ab2782f
magento/magento2#29677: Returned existing behaviour
sivaschenko 78beb69
Merge branch '2.4-develop' into 1504-M2-repo-Insert-rendition-images-…
sivaschenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 36 additions & 35 deletions
71
app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/OnInsert.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
app/code/Magento/Cms/Model/Wysiwyg/Images/GetInsertImageContent.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
sivaschenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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); | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
...ests/integration/testsuite/Magento/Cms/Model/Wysiwyg/Images/GetInsertImageContentTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="test-image.jpg"}}" 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="/test-image.jpg"}}" alt="" />' | ||
], | ||
[ | ||
'test-image.jpg', | ||
false, | ||
true, | ||
null, | ||
'<img src="{{media url="test-image.jpg"}}" alt="" />' | ||
], | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.