setLanguageManager example

->getMock();
    $negotiator->expects($this->any())
      ->method('getNegotiationMethods')
      ->willReturn([
        LanguageNegotiationUrl::METHOD_ID => [
          'class' => 'Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl',
          'weight' => 9,
        ],
      ]);
    $method = new LanguageNegotiationUrl();
    $method->setConfig($config_factory_stub);
    $method->setLanguageManager($this->languageManager);
    $negotiator->expects($this->any())
      ->method('getNegotiationMethodInstance')
      ->willReturn($method);

    // Create a user stub.     $current_user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')
      ->getMock();

    // Create a config event subscriber stub.     $config_subscriber = $this->getMockBuilder('Drupal\language\EventSubscriber\ConfigSubscriber')
      ->disableOriginalConstructor()
      
$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]);
    }
$definitions = array_intersect_key($definitions$enabled_methods);
    }
    return $definitions;
  }

  /** * {@inheritdoc} */
  public function getNegotiationMethodInstance($method_id) {
    if (!isset($this->methods[$method_id])) {
      $instance = $this->negotiatorManager->createInstance($method_id[]);
      $instance->setLanguageManager($this->languageManager);
      $instance->setConfig($this->configFactory);
      $instance->setCurrentUser($this->currentUser);
      $this->methods[$method_id] = $instance;
    }
    return $this->methods[$method_id];
  }

  /** * {@inheritdoc} */
  public function getPrimaryNegotiationMethod($type) {
    
// Case 1: Empty request.     $this->assertEquals(NULL, $languageNegotiationContentEntity->getLangcode());

    // Case 2: A request is available, but the languageManager is not set and     // the static::QUERY_PARAMETER is not provided as a named parameter.     $request = Request::create('/de/foo', 'GET');
    $request->query = new ParameterBag();
    $this->assertEquals(NULL, $languageNegotiationContentEntity->getLangcode($request));

    // Case 3: A request is available, the languageManager is set, but the     // static::QUERY_PARAMETER is not provided as a named parameter.     $languageNegotiationContentEntity->setLanguageManager($this->languageManager);
    $this->assertEquals(NULL, $languageNegotiationContentEntity->getLangcode($request));

    // Case 4: A request is available, the languageManager is set and the     // static::QUERY_PARAMETER is provided as a named parameter.     $expectedLangcode = 'de';
    $request->query->set(LanguageNegotiationContentEntity::QUERY_PARAMETER, $expectedLangcode);
    $this->assertEquals($expectedLangcode$languageNegotiationContentEntity->getLangcode($request));

    // Case 5: A request is available, the languageManager is set and the     // static::QUERY_PARAMETER is provided as a named parameter with a given     // langcode that is not one of the system supported ones.
Home | Imprint | This part of the site doesn't use cookies.