function LanguageNegotiationContentEntityTest::testProcessOutbound

Same name in other branches
  1. 11.x core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationContentEntityTest.php \Drupal\Tests\language\Unit\Plugin\LanguageNegotiation\LanguageNegotiationContentEntityTest::testProcessOutbound()

@covers ::processOutbound

File

core/modules/language/tests/src/Unit/Plugin/LanguageNegotiation/LanguageNegotiationContentEntityTest.php, line 134

Class

LanguageNegotiationContentEntityTest
Tests the LanguageNegotiationContentEntity plugin class.

Namespace

Drupal\Tests\language\Unit\Plugin\LanguageNegotiation

Code

public function testProcessOutbound() : void {
    // Case 1: Not all processing conditions are met.
    $languageNegotiationContentEntityMock = $this->createPartialMock($this->getPluginClass(), [
        'hasLowerLanguageNegotiationWeight',
        'meetsContentEntityRoutesCondition',
    ]);
    $languageNegotiationContentEntityMock->expects($this->exactly(2))
        ->method('hasLowerLanguageNegotiationWeight')
        ->willReturnOnConsecutiveCalls(FALSE, TRUE);
    $languageNegotiationContentEntityMock->expects($this->once())
        ->method('meetsContentEntityRoutesCondition')
        ->willReturnOnConsecutiveCalls(FALSE);
    $options = [];
    $path = $this->randomMachineName();
    // Case 1a: Empty request.
    $this->assertEquals($path, $languageNegotiationContentEntityMock->processOutbound($path));
    $request = Request::create('/foo', 'GET');
    $request->server = new ServerBag();
    // Case 1b: Missing the route key in $options.
    $this->assertEquals($path, $languageNegotiationContentEntityMock->processOutbound($path, $options, $request));
    $options = [
        'route' => $this->createMock(Route::class),
    ];
    // Case 1c: hasLowerLanguageNegotiationWeight() returns FALSE.
    $this->assertEquals($path, $languageNegotiationContentEntityMock->processOutbound($path, $options, $request));
    // Case 1d: meetsContentEntityRoutesCondition() returns FALSE.
    $this->assertEquals($path, $languageNegotiationContentEntityMock->processOutbound($path, $options, $request));
    // Case 2: Cannot figure out the langcode.
    $languageNegotiationContentEntityMock = $this->createPartialMock($this->getPluginClass(), [
        'hasLowerLanguageNegotiationWeight',
        'meetsContentEntityRoutesCondition',
        'getLangcode',
    ]);
    $languageNegotiationContentEntityMock->expects($this->any())
        ->method('hasLowerLanguageNegotiationWeight')
        ->willReturn(TRUE);
    $languageNegotiationContentEntityMock->expects($this->any())
        ->method('meetsContentEntityRoutesCondition')
        ->willReturn(TRUE);
    $languageNegotiationContentEntityMock->expects($this->exactly(2))
        ->method('getLangcode')
        ->willReturnOnConsecutiveCalls(NULL, 'de');
    $this->assertEquals($path, $languageNegotiationContentEntityMock->processOutbound($path, $options, $request));
    // Case 3: Can figure out the langcode.
    // Case 3a: via $options['language'].
    $options['language'] = $this->languages['en'];
    $options['query'] = NULL;
    $bubbleableMetadataMock = $this->createMock(BubbleableMetadata::class);
    $bubbleableMetadataMock->expects($this->exactly(3))
        ->method('addCacheContexts')
        ->with([
        'url.query_args:' . LanguageNegotiationContentEntity::QUERY_PARAMETER,
    ]);
    $this->assertEquals($path, $languageNegotiationContentEntityMock->processOutbound($path, $options, $request, $bubbleableMetadataMock));
    $this->assertFalse(isset($options['language']));
    $this->assertTrue(isset($options['query'][LanguageNegotiationContentEntity::QUERY_PARAMETER]));
    $this->assertEquals('en', $options['query'][LanguageNegotiationContentEntity::QUERY_PARAMETER]);
    // Case 3a1: via $options['language'] with an additional $options['query'][static::QUERY_PARAMETER].
    $options['language'] = $this->languages['en'];
    $options['query'][LanguageNegotiationContentEntity::QUERY_PARAMETER] = 'xx';
    $this->assertEquals($path, $languageNegotiationContentEntityMock->processOutbound($path, $options, $request, $bubbleableMetadataMock));
    $this->assertFalse(isset($options['language']));
    $this->assertEquals('xx', $options['query'][LanguageNegotiationContentEntity::QUERY_PARAMETER]);
    // Case 3b: via getLangcode().
    unset($options['query'][LanguageNegotiationContentEntity::QUERY_PARAMETER]);
    $this->assertEquals($path, $languageNegotiationContentEntityMock->processOutbound($path, $options, $request, $bubbleableMetadataMock));
    $this->assertEquals('de', $options['query'][LanguageNegotiationContentEntity::QUERY_PARAMETER]);
}

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