Skip to content
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

Magento_Backend store switcher: Use nowdoc instead of heredoc #32249

Conversation

fnogatz
Copy link
Member

@fnogatz fnogatz commented Feb 23, 2021

Description (*)

The heredoc in Magento/Backend/view/adminhtml/templates/store/switcher.phtml#L203-L216 is not valid, because the embedded JavaScript contains $this, which happens to be a valid variable name in PHP, too. Consequently, PHP tries to replace its occurrences by the current class object which results in an error. This can be avoided by using nowdoc instead of heredoc, which is the aim of this PR.

In Magento core, the is_using_iframe flag is never set. As a consequence, the else-branch in Magento/Backend/view/adminhtml/templates/store/switcher.phtml#L203-L216 is never called, so the error does not occur in a standard environment. However, we use Amasty's Improved Layered Navigation, which sets this flag to true for one of their backend configurations. The resulting error (in developer mode) looks as follows:

Screenshot from 2021-02-23 08-32-05

Related Pull Requests

Two years ago, there was #20656 to replace all heredoc by nowdoc, but it was closed.

Fixed Issues (if relevant)

No separate issue created.

Manual testing scenarios (*)

For reproducing this error, it is not necessary to install Magento or the referenced Amasty extension at all. Simply paste the snippet into a fresh PHP file test.php:

<?php
$test = <<<script
            jQuery('#preview_selected_store').val(scopeId);
            jQuery('#preview_form').submit();

            jQuery('.store-switcher .dropdown-menu li a').each(function() {
                var $this = jQuery(this);

                if ($this.data('role') === 'store-view-id' && $this.data('value') == scopeId) {
                    jQuery('#store-change-button').html($this.text());
                }
            });

            jQuery('#store-change-button').click();
script;
echo $test;

Calling the script via php -f test.php yields a similar error. Changing the first line to <<<'script' resolves this issue.

Questions or comments

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

Resolved issues:

  1. resolves [Issue] Magento_Backend store switcher: Use nowdoc instead of heredoc #32262: Magento_Backend store switcher: Use nowdoc instead of heredoc

@m2-assistant
Copy link

m2-assistant bot commented Feb 23, 2021

Hi @fnogatz. Thank you for your contribution
Here are 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
  13. Semantic Version Checker

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

@mrtuvn
Copy link
Contributor

mrtuvn commented Feb 23, 2021

never thought code styles like that can exists in the core. Mix between js-php with hard to track imho

@gabrieldagama gabrieldagama added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Feb 23, 2021
@gabrieldagama
Copy link
Contributor

@magento create issue

@bgorski
Copy link
Contributor

bgorski commented Feb 27, 2021

@magento run all tests

@m2-community-project m2-community-project bot moved this from Pending Review to Review in Progress in High Priority Pull Requests Dashboard Feb 27, 2021
@m2-community-project m2-community-project bot moved this from Pending Review to Review in Progress in High Priority Pull Requests Dashboard Feb 27, 2021
@magento-engcom-team
Copy link
Contributor

Hi @bgorski, thank you for the review.
ENGCOM-8831 has been created to process this Pull Request
✳️ @bgorski, could you please add one of the following labels to the Pull Request?

Label Description
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests
Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests
Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests

@bgorski bgorski added Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests Award: bug fix labels Feb 27, 2021
@bgorski
Copy link
Contributor

bgorski commented Feb 27, 2021

@fnogatz thank you for your contribution!
Looks like we have some issues with tests now. Although they're not failing because of this PR, most likely we'll have to wait for all the tests to end with a success before merging this.

@bgorski
Copy link
Contributor

bgorski commented Mar 1, 2021

@magento run Static Tests, Integration Tests, Functional Tests EE, Functional Tests CE, Functional Tests B2B

@bgorski
Copy link
Contributor

bgorski commented Mar 1, 2021

@magento run Static Tests, Functional Tests EE

@bgorski
Copy link
Contributor

bgorski commented Mar 1, 2021

@magento run Functional Tests B2B

@bgorski
Copy link
Contributor

bgorski commented Mar 1, 2021

@magento run Static Tests, Functional Tests EE, Functional Tests B2B

@bgorski
Copy link
Contributor

bgorski commented Mar 2, 2021

@magento run Static Tests, Functional Tests EE, Functional Tests B2B

@engcom-Bravo
Copy link
Contributor

✔️ QA Passed

Manual testing scenario

  1. Switch Magento to Developer mode php bin/magento dep:mo:set developer
  2. Create a new Website, Store, Storeview
  3. Add the following code
<?php
$test = <<<script
            jQuery('#preview_selected_store').val(scopeId);
            jQuery('#preview_form').submit();

            jQuery('.store-switcher .dropdown-menu li a').each(function() {
                var $this = jQuery(this);

                if ($this.data('role') === 'store-view-id' && $this.data('value') == scopeId) {
                    jQuery('#store-change-button').html($this.text());
                }
            });

            jQuery('#store-change-button').click();
script;
echo $test;

to the /app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml file like this
code
4. Open Magento Admin panel

BEFORE applying changes provided in the PR, an Exception message appeared

1 exception(s):
Exception #0 (Exception): Notice: Undefined variable: test in .../app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml on line 227

Exception #0 (Exception): Notice: Undefined variable: test in .../app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml on line 227
<pre>#1 include() called at [lib/internal/Magento/Framework/View/TemplateEngine/Php.php:71]
#2 Magento\Framework\View\TemplateEngine\Php->render() called at [lib/internal/Magento/Framework/View/Element/Template.php:273]
#3 Magento\Framework\View\Element\Template->fetchView() called at [generated/code/Magento/Backend/Block/Store/Switcher/Interceptor.php:419]
#4 Magento\Backend\Block\Store\Switcher\Interceptor->fetchView() called at [lib/internal/Magento/Framework/View/Element/Template.php:303]
#5 Magento\Framework\View\Element\Template->_toHtml() called at [app/code/Magento/Backend/Block/Template.php:141]
...
#46 Magento\Backend\Model\View\Result\Page\Interceptor->renderResult() called at [lib/internal/Magento/Framework/App/Http.php:120]
#47 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:23]
#48 Magento\Framework\App\Http\Interceptor->launch() called at [lib/internal/Magento/Framework/App/Bootstrap.php:264]
#49 Magento\Framework\App\Bootstrap->run() called at [pub/index.php:29]
</pre>

exc_before

AFTER switching to the PR, no errors appear in the Admin panel. The Scope can be changed.

@m2-community-project m2-community-project bot added the Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. label Mar 2, 2021
@engcom-Bravo engcom-Bravo moved this from Testing in Progress to Extended Testing (optional) in High Priority Pull Requests Dashboard Mar 2, 2021
@bgorski
Copy link
Contributor

bgorski commented Mar 2, 2021

@magento run Functional Tests EE, Functional Tests B2B

1 similar comment
@bgorski
Copy link
Contributor

bgorski commented Mar 3, 2021

@magento run Functional Tests EE, Functional Tests B2B

@engcom-Foxtrot engcom-Foxtrot moved this from Extended Testing (optional) to Merge in Progress in High Priority Pull Requests Dashboard Mar 4, 2021
@m2-community-project m2-community-project bot moved this from Merge in Progress to Ready for Testing in High Priority Pull Requests Dashboard Mar 6, 2021
@m2-community-project m2-community-project bot moved this from Ready for Testing to Merge in Progress in High Priority Pull Requests Dashboard Mar 6, 2021
@bgorski
Copy link
Contributor

bgorski commented Mar 6, 2021

Not sure why the bot moved that back to 'ready for testing' after I added the Improvement label, but I manually corrected that.

@magento-engcom-team magento-engcom-team merged commit bd0a324 into magento:2.4-develop Mar 7, 2021
@m2-assistant
Copy link

m2-assistant bot commented Mar 7, 2021

Hi @fnogatz, 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.

@gabrieldagama gabrieldagama moved this from Merge in Progress to Recently Merged in High Priority Pull Requests Dashboard Mar 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Frontend Auto-Tests: Not Required Changes in Pull Request does not require coverage by auto-tests Award: bug fix Component: Backend improvement Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: accept Release Line: 2.4 Severity: S2 Major restrictions or short-term circumventions are required until a fix is available. Type: Bug Fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Issue] Magento_Backend store switcher: Use nowdoc instead of heredoc
7 participants