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

Conversation

jmonteros422
Copy link
Contributor

@jmonteros422 jmonteros422 commented Aug 19, 2020

Description (*)

This PR Extracts logic from Magento\Cms\Controller\Adminhtml\Wysiwyg\Images\OnInsert::execute() controller to Magento\Cms\Model\Wysiwyg\Images\PrepareImage::execute() model

Dependent ASI Pull Request

PR: magento/adobe-stock-integration#1756

Fixed Issues

Fixes magento/adobe-stock-integration#1504: [2.1-develop] Insert rendition images to the content from media gallery instead of original images

…ages to the content from media gallery instead of original images - Extract logic from controller to a model to enable before plugin interception of parameters
@m2-assistant
Copy link

m2-assistant bot commented Aug 19, 2020

Hi @jmonteros422. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

For more details, please, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@jmonteros422 jmonteros422 changed the title magento/adobe-stock-integration#1504:[Related M2 PR] Insert rendition images to the content from media gallery instead of original images #1504:[Related M2 PR] Insert rendition images to the content from media gallery instead of original images Aug 19, 2020
@jmonteros422
Copy link
Contributor Author

@magento run all tests

Copy link
Member

@sivaschenko sivaschenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request @jmonteros422 ! Please see my comments.

I have removed the information about relation from this PR as it can be processed independently, ASI PR should still keep the relation information (until this PR is merged)

Also please cover the extracted model with an integration test

*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\Cms\Model\Wysiwyg\Images\PrepareImage $prepareImage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the constructor dependency in a backwards-compatible way.

See "Adding a constructor parameter" section of https://devdocs.magento.com/contributor-guide/backward-compatible-development/

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

class PrepareImage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the class name more meaningful

Suggested change
class PrepareImage
class GetInsertImageContent

/**
* @var \Magento\Cms\Model\Wysiwyg\Images\PrepareImage
*/
protected $prepareImage;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected $prepareImage;
protected $getInsterImageContent;

$image = $imagesHelper->getImageHtmlDeclaration($filename, $asIs);
}
/** @var \Magento\Cms\Model\Wysiwyg\Images\PrepareImage $image */
$image = $this->prepareImage->execute($request->getParams());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid one-use variables like $image

} else {
$image = $imagesHelper->getImageHtmlDeclaration($filename, $asIs);
}
/** @var \Magento\Cms\Model\Wysiwyg\Images\PrepareImage $image */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect and not necessary annotation. $image is a string and that is already defined as a return type of called method

public function execute(array $data): string
{
$filename = $this->imagesHelper->idDecode($data['filename']);
$storeId = (int)$data['store_id'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Store id value was previously retrieved from store field

* @param array $data
* @return string
*/
public function execute(array $data): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to have the meaningful parameters:

Suggested change
public function execute(array $data): string
public function execute(string $endcodedFilename, int $storeId, bool $forceStaticPath, bool $renderAsTag): string

$filename = $this->imagesHelper->idDecode($data['filename']);
$storeId = (int)$data['store_id'];

$this->catalogHelper->setStoreId($storeId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please check why this call to catalog helper is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sivaschenko , did a check on different store view with and without that call and seems to not change anything on the rendered text in the content. I just included it on the model to not miss anything out from the original class. Let me know your thoughts.

Comment on lines 51 to 58
if ($data['force_static_path']) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$image = parse_url($this->imagesHelper->getCurrentUrl() . $filename, PHP_URL_PATH);
} else {
$image = $this->imagesHelper->getImageHtmlDeclaration($filename, $data['as_is']);
}

return $image;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to remove unnecessary variable and simplify the code

Suggested change
if ($data['force_static_path']) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$image = parse_url($this->imagesHelper->getCurrentUrl() . $filename, PHP_URL_PATH);
} else {
$image = $this->imagesHelper->getImageHtmlDeclaration($filename, $data['as_is']);
}
return $image;
if ($data['force_static_path']) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
return parse_url($this->imagesHelper->getCurrentUrl() . $filename, PHP_URL_PATH);
}
return $this->imagesHelper->getImageHtmlDeclaration($filename, $data['as_is']);

@ghost ghost assigned sivaschenko Aug 19, 2020
@sivaschenko sivaschenko changed the title #1504:[Related M2 PR] Insert rendition images to the content from media gallery instead of original images Extracted logic from wysiwyg OnInsert controller to a model Aug 19, 2020
@sivaschenko sivaschenko added Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. labels Aug 19, 2020
…content from media gallery instead of original images - Extracted logic from wysiwyg OnInsert controller to a model
@jmonteros422
Copy link
Contributor Author

@magento run all tests

@jmonteros422 jmonteros422 changed the title Extracted logic from wysiwyg OnInsert controller to a model [WIP] Extracted logic from wysiwyg OnInsert controller to a model Aug 21, 2020
…to-the-content-from-media-gallery-instead-of-original-images
… a model to enable before plugin interception of parameters - Test failure fixes. Made store_id parameter optional.
@jmonteros422
Copy link
Contributor Author

@magento run all tests

… a model to enable before plugin interception of parameters - Fix static test
@jmonteros422
Copy link
Contributor Author

@magento run all tests

…Insert controller to a model - Integration test
@sivaschenko
Copy link
Member

@magento run Functional Tests B2B

@sivaschenko
Copy link
Member

Previous tests failed as they have been triggered from dependent ASI PR

@sivaschenko
Copy link
Member

@magento run all tests

* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
* @var GetInsertImageContent
*/
protected $getInsertImageContent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be private.

@ghost ghost assigned sidolov Aug 27, 2020
@ghost ghost dismissed sivaschenko’s stale review August 27, 2020 15:07

Pull Request state was updated. Re-review required.

@sivaschenko
Copy link
Member

@magento run all tests

@sivaschenko
Copy link
Member

@magento run all tests

@magento-engcom-team magento-engcom-team merged commit e7ca769 into magento:2.4-develop Sep 2, 2020
@m2-assistant
Copy link

m2-assistant bot commented Sep 2, 2020

Hi @jmonteros422, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Component: Cms Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: accept Project: ASI QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope Release Line: 2.4 Severity: S2 Major restrictions or short-term circumventions are required until a fix is available.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Insert rendition images to the content from media gallery instead of original images
6 participants