Skip to content

magento/magento2#39884: Fix type compatibility in Invoice comments me…#40428

Merged
magento-devops-reposync-svc merged 8 commits intomagento:2.4-developfrom
yaroslav-kozar:fix-for-issue-39884
Mar 5, 2026
Merged

magento/magento2#39884: Fix type compatibility in Invoice comments me…#40428
magento-devops-reposync-svc merged 8 commits intomagento:2.4-developfrom
yaroslav-kozar:fix-for-issue-39884

Conversation

@yaroslav-kozar
Copy link
Copy Markdown
Contributor

Description (*)

Fixed a type compatibility issue in Magento\Sales\Model\Order\Invoice. The problem occurred when calling getComments() before addComment(). getComments() stores an array in the object's internal data, while getCommentsCollection() (called by addComment) expected a Collection object, leading to a fatal error.

Key changes:

  • Updated getCommentsCollection() to check for the correct instance and handle existing array data.
  • Refactored getComments() to consistently return an array, even if a Collection is currently stored.
  • Added reload=true in addComment() to ensure data consistency.
  • Added a unit test covering these scenarios to prevent future regressions.

Related Pull Requests

None

Fixed Issues (if relevant)

  1. Fixes Invoice addComment can throw error when Invoice getComments is called first #39884

Manual testing scenarios (*)

  1. Create a script to load an existing invoice and execute the following:
$invoice = $objectManager->get(\Magento\Sales\Api\InvoiceRepositoryInterface::class)->get($id);

// 1. Populates internal data with an array
$invoice->getComments(); 

// 2. Throws Fatal Error before fix; completes successfully after fix
$invoice->addComment('Reproduce Bug'); 

$objectManager->get(\Magento\Sales\Api\InvoiceRepositoryInterface::class)->save($invoice);
  1. Expected Result (Before fix): Error: Call to a member function addItem() on array.
  2. Expected Result (After fix): Comment is added, and the script finishes without errors.
  3. Automated tests: Run vendor/bin/phpunit app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceTest.php.

Questions or comments

The fix ensures that the model correctly manages the transition between the Data Interface (arrays) and the Resource Model (collections).

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)

 - Ensure getComments returns array and getCommentsCollection returns Collection to avoid type conflicts.
@m2-assistant
Copy link
Copy Markdown

m2-assistant Bot commented Jan 9, 2026

Hi @yaroslav-kozar. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ 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
ℹ️ 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.

@yaroslav-kozar
Copy link
Copy Markdown
Contributor Author

@magento run all tests

@yaroslav-kozar
Copy link
Copy Markdown
Contributor Author

@magento run all tests

@engcom-Hotel
Copy link
Copy Markdown
Contributor

@magento run all tests

Copy link
Copy Markdown
Contributor

@engcom-Hotel engcom-Hotel left a comment

Choose a reason for hiding this comment

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

Hello @yaroslav-kozar,

Thank you for your contribution!

The changes seems good to us, but please look into the failed tests they seems to failing due to PR changes.

The failed functional tests are flaky.

Thank you

@engcom-Hotel engcom-Hotel moved this from Pending Review to Changes Requested in Community Dashboard Feb 3, 2026
@engcom-Hotel engcom-Hotel added Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it and removed Progress: pending review labels Feb 3, 2026
@engcom-Dash engcom-Dash self-assigned this Feb 9, 2026
@engcom-Dash
Copy link
Copy Markdown
Contributor

Hello @yaroslav-kozar,

Thank you for your contribution!

The changes seems good to us, but please look into the failed tests they seems to failing due to PR changes.

The failed functional tests are flaky.

Thank you

@engcom-Hotel @yaroslav-kozar I am taking care of the static test failure, as the PR has been pending since last week.

@engcom-Dash
Copy link
Copy Markdown
Contributor

@magento run all tests

1 similar comment
@engcom-Dash
Copy link
Copy Markdown
Contributor

@magento run all tests

@engcom-Dash
Copy link
Copy Markdown
Contributor

@magento run all tests

@engcom-Dash
Copy link
Copy Markdown
Contributor

@engcom-Hotel, I have fixed the Static tests failure. Could you please review the PR so we can proceed further?

@engcom-Dash engcom-Dash moved this from Changes Requested to Pending Review in Community Dashboard Feb 10, 2026
@engcom-Hotel
Copy link
Copy Markdown
Contributor

@magento run all tests

Copy link
Copy Markdown
Contributor

@engcom-Hotel engcom-Hotel left a comment

Choose a reason for hiding this comment

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

Failed tests seems flaky to me hence approving this PR

@ct-prd-projects-boards-automation ct-prd-projects-boards-automation Bot moved this from Review in Progress to Ready for Testing in Community Dashboard Feb 11, 2026
@engcom-Dash engcom-Dash moved this from Ready for Testing to Testing in Progress in Community Dashboard Feb 17, 2026
@engcom-Dash
Copy link
Copy Markdown
Contributor

@magento run all tests

@engcom-Dash
Copy link
Copy Markdown
Contributor

@yaroslav-kozar

Thanks for the collaboration & contribution!

✔️ QA Passed

Preconditions:
Install fresh Magento 2.4-develop

Manual Steps:
Create a script to load an existing invoice and execute the following:

$invoice = $objectManager->get(\Magento\Sales\Api\InvoiceRepositoryInterface::class)->get($id);
// 1. Populates internal data with an array
$invoice->getComments(); 

// 2. Throws Fatal Error before fix; completes successfully after fix
$invoice->addComment('Reproduce Bug'); 

$objectManager->get(\Magento\Sales\Api\InvoiceRepositoryInterface::class)->save($invoice);

Before
FAIL - Call to a member function addItem() on array

After
ALL TESTS PASSED - THE BUG IS FIXED!

Builds are failed. Hence, moving this PR to Extended Testing.

Thanks

@engcom-Dash engcom-Dash moved this from Testing in Progress to Extended testing (optional) in Community Dashboard Feb 18, 2026
@engcom-Dash
Copy link
Copy Markdown
Contributor

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

1 similar comment
@engcom-Dash
Copy link
Copy Markdown
Contributor

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

@engcom-Dash
Copy link
Copy Markdown
Contributor

Few Functional B2B, CE and EE failures are not consistent in recent 2 builds. They are neither part of this PR nor failing because of the PR changes, seems to be flaky. The consistent failures are known issues and have open JIRAs.

CE
Build 1
https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40428/93c7b5bf78693875957ddae4f55013bd/Functional/allure-report-ce/index.html#categories
image
Build 2
https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40428/d12f83bba632437052c2d7f894570de9/Functional/allure-report-ce/index.html#categories/dad3b5a7b0574462665553fcc880a350/9b378cc13485ed71/
image

EE
Build 1
https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40428/ecb9f339e2e1d7768a25d774f207f255/Functional/allure-report-ee/index.html#categories
image
Build 2
https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40428/e97e55a20b1619403eb8eca9d1e0e2bf/Functional/allure-report-ee/index.html#categories
image

B2b
Build 1
https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40428/194502d9c3076369a54a003bf21fc8cb/Functional/allure-report-b2b/index.html
image
Build 2
https://public-results-storage-prod.magento-testing-service.engineering/reports/magento/magento2/pull/40428/8e9c4ad7c790aaba77966f4f8e51a2f2/Functional/allure-report-b2b/index.html
image

Integration test
The failure is occurring across all PRs and is not related to this specific PR.

Known Issue
AssertSuccessMessageAppearsAfterAddingProductToCartThatContainsOutOfStockProductTest ACQE-9359
PurchaseOrderEnableDisableE2ETest ACQE-9362
AdminDisplayingCartAndCatalogPriceRulesGridsTest ACQE-9406

Hence, Moving this PR Merge In Progress.

@engcom-Dash engcom-Dash moved this from Extended testing (optional) to Merge in Progress in Community Dashboard Feb 19, 2026
@magento-devops-reposync-svc magento-devops-reposync-svc merged commit 3e216bf into magento:2.4-develop Mar 5, 2026
8 of 12 checks passed
@ct-prd-projects-boards-automation ct-prd-projects-boards-automation Bot moved this from Merge in Progress to Recently Merged in Community Dashboard Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P3 May be fixed according to the position in the backlog. Progress: deployment queue Project: Community Picked PRs upvoted by the community Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Projects

Status: Recently Merged

Development

Successfully merging this pull request may close these issues.

Invoice addComment can throw error when Invoice getComments is called first

5 participants