You are here

public function LanguageContextTest::testNegotiatorInjection in GraphQL 8.3

Test if the language negotiator is injected properly.

File

tests/src/Kernel/Framework/LanguageContextTest.php, line 87

Class

LanguageContextTest
Test contextual language negotiation.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testNegotiatorInjection() {
  $context = $this->container
    ->get('graphql.language_context');
  $negotiator = $this->container
    ->get('language_negotiator');
  $this
    ->assertInstanceOf(FixedLanguageNegotiator::class, $negotiator);

  // Check if the order of negotiators is correct.
  $getEnabledNegotiators = new \ReflectionMethod(FixedLanguageNegotiator::class, 'getEnabledNegotiators');
  $getEnabledNegotiators
    ->setAccessible(TRUE);
  $negotiators = $getEnabledNegotiators
    ->invokeArgs($negotiator, [
    LanguageInterface::TYPE_INTERFACE,
  ]);
  $this
    ->assertEquals([
    'language-graphql' => -999,
    'language-url' => 0,
  ], $negotiators);

  // Check if the GraphQL language negotiation yields the correct result.
  $language = $context
    ->executeInLanguageContext(function () use ($negotiator) {
    $negotiateLanguage = new \ReflectionMethod(FixedLanguageNegotiator::class, 'negotiateLanguage');
    $negotiateLanguage
      ->setAccessible(TRUE);
    return $negotiateLanguage
      ->invokeArgs($negotiator, [
      LanguageInterface::TYPE_INTERFACE,
      'language-graphql',
    ]);
  }, 'fr');
  $this
    ->assertNotNull($language);
  $this
    ->assertEquals('fr', $language
    ->getId());

  // Check if the language type is initialized correctly.
  $result = $context
    ->executeInLanguageContext(function () {
    return \Drupal::service('language_negotiator')
      ->initializeType(LanguageInterface::TYPE_INTERFACE);
  }, 'fr');
  $this
    ->assertEquals('language-graphql', array_keys($result)[0]);
}