function LanguageNegotiationUrlTest::testNeutralLanguages

Same name and namespace in other branches
  1. 10 core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php \Drupal\Tests\language\Unit\LanguageNegotiationUrlTest::testNeutralLanguages()

Tests outbound path processing for neutral languages.

@dataProvider providerNeutralLanguages

File

core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php, line 164

Class

LanguageNegotiationUrlTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21language%21src%21Plugin%21LanguageNegotiation%21LanguageNegotiationUrl.php/class/LanguageNegotiationUrl/11.x" title="Class for identifying language via URL prefix or domain." class="local">\Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl</a> @group language

Namespace

Drupal\Tests\language\Unit

Code

public function testNeutralLanguages($langcode, $expected_langcode) : void {
    if ($expected_langcode) {
        $this->languageManager
            ->expects($this->once())
            ->method('getCurrentLanguage')
            ->willReturn($this->languages['en']);
    }
    $config = $this->getConfigFactoryStub([
        'language.negotiation' => [
            'url' => [
                'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
                'prefixes' => [
                    'de' => 'de',
                    'en' => 'en',
                ],
            ],
        ],
    ]);
    $request = Request::create('/foo', 'GET');
    $method = new LanguageNegotiationUrl();
    $method->setLanguageManager($this->languageManager);
    $method->setConfig($config);
    $method->setCurrentUser($this->user);
    $this->assertNull($method->getLangcode($request));
    $language = $this->createMock(LanguageInterface::class);
    $language->expects($this->any())
        ->method('getId')
        ->willReturn($langcode);
    $cacheability = new BubbleableMetadata();
    $options = [
        'language' => $language,
    ];
    $method->processOutbound('foo', $options, $request, $cacheability);
    $expected_cacheability = new BubbleableMetadata();
    if ($expected_langcode) {
        $this->assertSame($expected_langcode . '/', $options['prefix']);
        $expected_cacheability->setCacheContexts([
            'languages:' . LanguageInterface::TYPE_URL,
        ]);
    }
    else {
        $this->assertFalse(isset($options['prefix']));
    }
    $this->assertEquals($expected_cacheability, $cacheability);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.