Problem/Motivation
While functional tests are great, language negotiation is very complex, and a poorly understood bug introduced in one layer and fixed in another could easily slip by a functional test.
(from parent issue)
LanguageNegotiationContentEntity has no unit tests at the moment.
Proposed resolution
Add unit tests for the following methods - if possible - of LanguageNegotiationContentEntity:
- ::getLangcode()
- ::processOutbound()
- ::getLanguageSwitchLinks()
Remaining tasks
Define the test scenarios for each method listed above.
Implement the tests.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3130107
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
stefanos.petrakisFirst tests for the
::getLangcode()method.Comment #4
stefanos.petrakisMaking this patch more like what the bot likes.
Comment #5
jungleThanks @stefanos.petrakis for working on this!
See #3107732: Add return typehints to setUp/tearDown methods in concrete test classes, return type hinting is expected in 9.x now,
Prefer using Prophecy for mocking, see https://www.drupal.org/docs/8/phpunit/using-prophecy, but it's ok to not use it. Non-mandatory.
Code here is self-explained. To me, comments are unnecessary, but it's ok to keep it if you like.
Comment #6
jungleSwitching to 9.1.x, could be backported to 8.x
Comment #7
stefanos.petrakisThanks for the review @jungle, here is the updated patch.
Comment #9
stefanos.petrakisAgain, making this patch more like what the bot likes.
The reported error is triggered by the difference in error message strings between different PHP versions.
Comment #10
stefanos.petrakisComment #11
jungleTo have try-catch in tests, not sure if it's against the best practice.
expectException()andexpectExceptionMessage()are for that. but if doing so, breaking the one big test into small ones is necessary as in general, one exception is captured in one method. Correct me if I were wrong.Comment #12
stefanos.petrakisI added both of these try/catch at those places in order to get the assertions in place, they are otherwise to be removed for the final version of the patch. Check out the comments before the try/catch blocks.
The final patch in this issue has a dependency on the changes coming from #3130751: Fix LanguageNegotiationContentEntity::getLangcode() errors thrown.
expectExceptionandexpectExceptionMessagewill not catch errors using the standard PHP configuration.Comment #19
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 200
If I'm understanding #9 correct is this issue waiting on #3130751: Fix LanguageNegotiationContentEntity::getLangcode() errors thrown? If so this should be postponed.
Comment #20
stefanos.petrakisThat was my suggestion, yes. Postpone this issue here, till the other one lands and then proceed with this one.
By that time, this issue will probably be better titled "Refactor LanguageNegotiationContentEntityTest to use a plugin factory trait".
Comment #21
stefanos.petrakisPicking this up again; since #3130751: Fix LanguageNegotiationContentEntity::getLangcode() errors thrown has been fixed, the PR will need a few changes.
Comment #22
stefanos.petrakisTests were added in #3130751: Fix LanguageNegotiationContentEntity::getLangcode() errors thrown; the current state of the PR takes those tests and:
(a) Refactors them some using a trait as was suggested in #3130104-11: Add unit test coverage for LanguageNegotiationBrowser. That trait will then be used in that issue as well as any child issues of #3129860: Parts of the language negotiation subsystem are not unit/kernel tested
(b) Adds an extra assertion (relevant code change)
This should be ready for review as soon as the tests turn green.
Comment #23
stefanos.petrakisReady for review
Comment #24
smustgrave commentedChanges look good but the issue summary mentions tests for
Which I don't see.
Comment #25
stefanos.petrakisAdded missing tests (and some updates related to using a shared trait/base class coming from #3130104: Add unit test coverage for LanguageNegotiationBrowser)
Back to review
Comment #26
smustgrave commentedSee tests were added for
::processOutbound()
::getLanguageSwitchLinks()
So removing the IS update tag.
Changes look good but do think this postpones #3130104: Add unit test coverage for LanguageNegotiationBrowser as we are now mixing tickets.
Comment #27
catchNeeds a rebase.
Comment #30
rpayanmPlease review.
Comment #31
smustgrave commentedGoing to mark it but going to ask @rpayanm why didn't you just update the existing MR? It was pointing to the correct branch?
Comment #32
rpayanmI tried to do it, but I found hard conflicts to solve, since the previous patch did not have a lot of changes I created a new branch. Sorry about that.
Comment #35
catchThis looks fine and we can always add/refine to the improved tests later.
Committed/pushed to 11.x and cherry-picked to 10.1.x, thanks!