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)

LanguageNegotiationBrowser has no unit tests at the moment.

Proposed resolution

Add unit tests for the following methods - if possible - of LanguageNegotiationBrowser:

  • ::getLangcode()

Remaining tasks

Define the test scenarios for each method listed above.
Implement the tests.

Issue fork drupal-3130104

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

Here is a patch with 5 unit test cases; at the moment it is however blocked by #3130438: Inject page_cache_kill_switch service to LanguageNegotiationBrowser

jungle’s picture

Status: Active » Needs work

Thank you @stefanos.petrakis!

  1. Coding standards checking passes
  2. +++ b/core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationBrowserTest.php
    @@ -0,0 +1,127 @@
    +  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

  3. +++ b/core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationBrowserTest.php
    @@ -0,0 +1,127 @@
    +    $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/LanguageNegotiationBrowserTest.php
    @@ -0,0 +1,127 @@
    +    // Create a language manager stub.
    ...
    +    // Create a user stub.
    

    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.01 KB
new1.31 KB

Thanks for the review, here is the updated patch.

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

Status: Needs work » Needs review
StatusFileSize
new7.31 KB

Right, working on different issues got me a tiny bit confused.

The latest patch (#5) will fail till #3130438: Inject page_cache_kill_switch service to LanguageNegotiationBrowser lands.

In the meantime, here is a combined patch of the constuctor-DI mods to the LanguageNegotiationBrowser together with the Unit tests, for proof of the tests working nicely together when constructor-DI is implemented.

andypost’s picture

Status: Needs review » Postponed

Yep, better to fix injection instead of mocking Drupal::service()

andypost’s picture

Status: Postponed » Needs work

Blocker is in

andypost’s picture

Looks it needs to re-upload patch 5, requeued

andypost’s picture

btw Looking on test coverage it looks better to create base test-class or trait with a method to create plugins via factory

andypost’s picture

+++ b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationBrowser.php
@@ -25,6 +28,30 @@ class LanguageNegotiationBrowser extends LanguageNegotiationMethodBase {
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static($container->get('page_cache_kill_switch'));

The plugin must get configuration into constructor, so test coverage should catch it

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.

stefanos.petrakis’s picture

Picking this up again, following comments from @andypost and using a PR for the work from this point on.
Waiting for the bot to approve so this can move into NR.

stefanos.petrakis’s picture

Status: Needs work » Needs review
smustgrave’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Scratched out the remaining tasks as they appear to be completed. The MR contains the test for getLangCode().

Coverage looks good

quietone’s picture

Status: Reviewed & tested by the community » Needs work
stefanos.petrakis’s picture

Status: Needs work » Needs review

Thanks for the review @quietone; I believe I responded to all points adequately

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

stefanos.petrakis’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Few small things but overall looks close.

stefanos.petrakis’s picture

Status: Needs work » Needs review

Refactored into a yet simpler form, I think this may be it.

smustgrave’s picture

Status: Needs review » Needs work

Appears @jungle has one open thread if that can be answered.

Other then that looks good!

stefanos.petrakis’s picture

Status: Needs work » Needs review

Replied to the thread from @jungle (thanks, good shout). Setting this back to NR

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

stefanos.petrakis’s picture

Status: Needs work » Needs review

Disagreeing with the "Needs Review Queue Bot"

smustgrave’s picture

Seeing some weirdness myself.

Opening MR 2900 I see there are 30 files changed.
Clicking file tab I see 2
If I go to https://git.drupalcode.org/project/drupal/-/merge_requests/2900.diff I see TONS of changes.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

stefanos.petrakis’s picture

Status: Needs work » Needs review

Again, (in a new MR due to some issues with Gitlab's UI and the git history in the old branch), this is open for review.
The remaining open point from @jungle has been addressed.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems to be good now but think this maybe should be postponed for #3130107: Extend unit test coverage for LanguageNegotiationContentEntity since the fixes intersect. This will have to be reviewed again when #3130107: Extend unit test coverage for LanguageNegotiationContentEntity lands

quietone’s picture

Status: Reviewed & tested by the community » Needs work

I was looking at this again and this appears to be a duplicate of #3130751: Fix LanguageNegotiationContentEntity::getLangcode() errors thrown, which was committed last month. Looking more closely at the test cases I think this is adding some, such as

    // Case 3: LanguageManager available, a request is given, but
    // HTTP_ACCEPT_LANGUAGE is not provided.

I think what needs to happen is to improve the testing in the existing \Drupal\Tests\language\Unit\Plugin\LanguageNegotiation\LanguageNegotiationContentEntityTest with any tests cases that are new here.

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

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.