magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Wrong store ID used in Order Address Renderer

Open thomas-kl1 opened this issue 1 year ago • 32 comments

Description (*)

\Magento\Sales\Model\Order\Address\Renderer::format allows to format order address. Internally, the method retrieve the order store ID and update the current store ID. This produces strange behavior if used in a context with a different store as the current store is not set to its previous value at the end of the execution of the method.

The class \Magento\Customer\Model\Address\Config is used within the format method in order to resolve the address format to use accordingly to the order store. This class allows to set the store context thanks to \Magento\Customer\Model\Address\Config::setStore. However this method was not called. Here we call it and reverse the value to its original state at the end of the execution, in order to prevent weird behavior. With this solution, the current store in the manager is not altered and the global context is preserved. Previously, when the method \Magento\Customer\Model\Address\Config::getFormatByCode was called, by the method format, the store was resolved at the first call :

-> \Magento\Customer\Model\Address\Config::getFormatByCode
--> \Magento\Customer\Model\Address\Config::getFormats
---> \Magento\Customer\Model\Address\Config::getStore
public function getStore()
{
    if ($this->_store === null) {
        $this->_store = $this->_storeManager->getStore();
    }
    return $this->_store;
}

I guess that's why there was a $this->storeManager->setCurrentStore($orderStore); in \Magento\Sales\Model\Order\Address\Renderer::format.

This MR aims to remove the usage of $this->storeManager->setStore which may produce unwanted side-effects and use proper method available in the \Magento\Customer\Model\Address\Config API.

Related Pull Requests

Fixed Issues (if relevant)

Cases where multiple order mails (with different stores) have to be sent by the cron job. Order addresses are not formatted accordingly to their respective store ID. Different formats must be set by store ID.

Manual testing scenarios (*)

  1. ...
  2. ...

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)
  • [ ] README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • [ ] All automated tests passed successfully (all builds are green)

Resolved issues:

  1. [x] resolves magento/magento2#38412: Wrong store ID used in Order Address Renderer

thomas-kl1 avatar Aug 29 '23 09:08 thomas-kl1

Hi @thomas-kl1. Thank you for your contribution! Here are some useful tips on 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

:exclamation: 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 :information_source: Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation. Join Magento Community Engineering Slack and ask your questions in #github channel.

m2-assistant[bot] avatar Aug 29 '23 09:08 m2-assistant[bot]

@magento run all tests

thomas-kl1 avatar Aug 29 '23 09:08 thomas-kl1

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@magento run all tests

thomas-kl1 avatar Aug 29 '23 12:08 thomas-kl1

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@magento run all tests

93kamikaze avatar Aug 30 '23 16:08 93kamikaze

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

Unit tests needs to be reworked as they don't test expected results but internal implementation 🤡

thomas-kl1 avatar Sep 01 '23 08:09 thomas-kl1

@magento run all tests

thomas-kl1 avatar Jan 19 '24 09:01 thomas-kl1

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@magento create issue

engcom-Echo avatar Feb 02 '24 05:02 engcom-Echo

@magento run all tests

engcom-Echo avatar Feb 07 '24 05:02 engcom-Echo

@magento run all tests

engcom-Echo avatar Feb 08 '24 07:02 engcom-Echo

@magento run all tests

engcom-Echo avatar Feb 12 '24 05:02 engcom-Echo

@magento run all tests

engcom-Echo avatar Feb 12 '24 07:02 engcom-Echo

@thomas-kl1 Can you please have look on build failure of Integration Tests seems to be caused by PR changes.

engcom-Echo avatar Feb 12 '24 11:02 engcom-Echo

@magento run all tests

thomas-kl1 avatar Feb 12 '24 18:02 thomas-kl1

\Magento\Customer\Model\Metadata\AddressMetadata::getAttributeMetadata $attribute = $this->attributeMetadataDataProvider->getAttribute(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $attributeCode);

\Magento\Customer\Model\AttributeMetadataDataProvider::getAttribute return $this->eavConfig->getAttribute($entityType, $attributeCode);

\Magento\Eav\Model\Config::initSystemAttributes

$attributeCollection = $this->_universalFactory->create($entityType->getEntityAttributeCollection());
$websiteId = $attributeCollection instanceof Collection ? $this->getWebsiteId($attributeCollection) : 0;

\Magento\Eav\Model\ResourceModel\Attribute\Collection::getWebsite

if ($this->_website === null) {
    $this->_website = $this->_storeManager->getStore()->getWebsite();
}
return $this->_website;

thomas-kl1 avatar Feb 12 '24 21:02 thomas-kl1

@magento run all tests

thomas-kl1 avatar Feb 12 '24 22:02 thomas-kl1

@magento run all tests

thomas-kl1 avatar Feb 13 '24 09:02 thomas-kl1

@magento run all tests

thomas-kl1 avatar Feb 13 '24 10:02 thomas-kl1

@magento run all tests

thomas-kl1 avatar Feb 13 '24 11:02 thomas-kl1

@magento run all tests

thomas-kl1 avatar Feb 13 '24 21:02 thomas-kl1

@magento run all tests

thomas-kl1 avatar Feb 13 '24 23:02 thomas-kl1

I'll squash the commits tomorrow, right now I'm on my phone 😅

thomas-kl1 avatar Feb 13 '24 23:02 thomas-kl1

@magento run all tests

thomas-kl1 avatar Feb 14 '24 10:02 thomas-kl1

@magento run all tests

engcom-Echo avatar Feb 26 '24 07:02 engcom-Echo

@magento run all tests

engcom-Echo avatar Feb 26 '24 10:02 engcom-Echo

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

engcom-Hotel avatar Feb 27 '24 12:02 engcom-Hotel

@magento run Functional Tests B2B

andrewbess avatar Feb 27 '24 13:02 andrewbess