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.

Issue fork drupal-3130107

Command icon 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

stefanos.petrakis created an issue. See original summary.

stefanos.petrakis’s picture

Status: Needs review » Needs work

The last submitted patch, 2: drupal-language_negotiation_content_entity_unit_tests-3130107-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

stefanos.petrakis’s picture

Status: Needs work » Needs review
StatusFileSize
new5.57 KB
new1.25 KB

Making this patch more like what the bot likes.

jungle’s picture

Status: Needs review » Needs work

Thanks @stefanos.petrakis for working on this!

  1. 3 coding standards messages

    core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationContentEntityTest.php
    line 10 Unused use statement
    line 14 Unused use statement
    line 17 Unused use statement

  2. +++ b/core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationContentEntityTest.php
    @@ -0,0 +1,138 @@
    +  protected function setUp() {
    

    See #3107732: Add return typehints to setUp/tearDown methods in concrete test classes, return type hinting is expected in 9.x now,

    -  protected function setUp() {
    + protected function setUp(): void {
    
  3. +++ b/core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationContentEntityTest.php
    @@ -0,0 +1,138 @@
    +    $language_de = $this->createMock(LanguageInterface::class);
    +    $language_de->expects($this->any())
    

    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.

  4. +++ b/core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationContentEntityTest.php
    @@ -0,0 +1,138 @@
    +    // Create a language manager stub.
    +    $language_manager = $this->getMockBuilder(ConfigurableLanguageManagerInterface::class)
    ...
    +    // Create a user stub.
    +    $this->user = $this->getMockBuilder(AccountInterface::class)
    

    Code here is self-explained. To me, comments are unnecessary, but it's ok to keep it if you like.

jungle’s picture

Version: 8.9.x-dev » 9.1.x-dev

Switching to 9.1.x, could be backported to 8.x

stefanos.petrakis’s picture

Status: Needs work » Needs review
StatusFileSize
new5.34 KB
new1.95 KB

Thanks for the review @jungle, here is the updated patch.

Status: Needs review » Needs work
stefanos.petrakis’s picture

Status: Needs work » Needs review
StatusFileSize
new5.36 KB
new915 bytes

Again, 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.

stefanos.petrakis’s picture

jungle’s picture

+++ b/core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationContentEntityTest.php
@@ -0,0 +1,133 @@
+    try {
+      $languageNegotiationContentEntity->getLangcode();
+    }
+    catch (Throwable $t) {
+      $this->assertEquals("Trying to get property of non-object", str_replace("'query' ", '', $t->getMessage()));
+    }
...
+    try {
+      $languageNegotiationContentEntity->getLangcode($request);
+    }
+    catch (Throwable $t) {
+      $this->assertEquals("Call to a member function getLanguages() on null", $t->getMessage());
+    }

To have try-catch in tests, not sure if it's against the best practice.

expectException() and expectExceptionMessage() 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.

stefanos.petrakis’s picture

I 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.

expectException and expectExceptionMessage will not catch errors using the standard PHP configuration.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

This 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.

stefanos.petrakis’s picture

Status: Needs review » Postponed

That 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".

stefanos.petrakis’s picture

Assigned: Unassigned » stefanos.petrakis
Status: Postponed » Needs work

Picking this up again; since #3130751: Fix LanguageNegotiationContentEntity::getLangcode() errors thrown has been fixed, the PR will need a few changes.

stefanos.petrakis’s picture

Title: Add unit test coverage for LanguageNegotiationContentEntity » Extend unit test coverage for LanguageNegotiationContentEntity
Related issues: +#3130104: Add unit test coverage for LanguageNegotiationBrowser

Tests 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.

stefanos.petrakis’s picture

Status: Needs work » Needs review

Ready for review

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs issue summary update

Changes look good but the issue summary mentions tests for

  • ::processOutbound()
  • ::getLanguageSwitchLinks()

Which I don't see.

stefanos.petrakis’s picture

Status: Needs work » Needs review

Added 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

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update

See 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.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Needs a rebase.

rpayanm made their first commit to this issue’s fork.

rpayanm’s picture

Status: Needs work » Needs review

Please review.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Going to mark it but going to ask @rpayanm why didn't you just update the existing MR? It was pointing to the correct branch?

rpayanm’s picture

Going to mark it but going to ask @rpayanm why didn't you just update the existing MR? It was pointing to the correct branch?

I 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.

  • catch committed d2eb0235 on 11.x
    Issue #3130107 by stefanos.petrakis, rpayanm, smustgrave, jungle: Extend...

  • catch committed afa61a59 on 10.1.x
    Issue #3130107 by stefanos.petrakis, rpayanm, smustgrave, jungle: Extend...
catch’s picture

Status: Reviewed & tested by the community » Fixed

This 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!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.