function LanguageNegotiationUrlTest::testPathPrefix

Same name in other branches
  1. 9 core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php \Drupal\Tests\language\Unit\LanguageNegotiationUrlTest::testPathPrefix()
  2. 10 core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php \Drupal\Tests\language\Unit\LanguageNegotiationUrlTest::testPathPrefix()
  3. 11.x core/modules/language/tests/src/Unit/LanguageNegotiationUrlTest.php \Drupal\Tests\language\Unit\LanguageNegotiationUrlTest::testPathPrefix()

Test path prefix language negotiation and outbound path processing.

@dataProvider providerTestPathPrefix

File

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

Class

LanguageNegotiationUrlTest
@coversDefaultClass \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl @group language

Namespace

Drupal\Tests\language\Unit

Code

public function testPathPrefix($prefix, $prefixes, $expected_langcode) {
    $this->languageManager
        ->expects($this->any())
        ->method('getCurrentLanguage')
        ->will($this->returnValue($this->languages[in_array($expected_langcode, [
        'en',
        'de',
    ]) ? $expected_langcode : 'en']));
    $config = $this->getConfigFactoryStub([
        'language.negotiation' => [
            'url' => [
                'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
                'prefixes' => $prefixes,
            ],
        ],
    ]);
    $request = Request::create('/' . $prefix . '/foo', 'GET');
    $method = new LanguageNegotiationUrl();
    $method->setLanguageManager($this->languageManager);
    $method->setConfig($config);
    $method->setCurrentUser($this->user);
    $this->assertEquals($expected_langcode, $method->getLangcode($request));
    $cacheability = new BubbleableMetadata();
    $options = [];
    $method->processOutbound('foo', $options, $request, $cacheability);
    $expected_cacheability = new BubbleableMetadata();
    if ($expected_langcode) {
        $this->assertSame($prefix . '/', $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.