function LanguageNegotiationUrlTest::testNeutralLanguages

Same name in other branches
  1. 11.x 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 \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl @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.