You are here

public function RedirectAPITest::testMultilanguageCases in Redirect 8

Test multilingual redirects.

File

tests/src/Kernel/RedirectAPITest.php, line 288

Class

RedirectAPITest
Redirect entity and redirect API test coverage.

Namespace

Drupal\Tests\redirect\Kernel

Code

public function testMultilanguageCases() {

  // Add a redirect for english.

  /** @var \Drupal\redirect\Entity\Redirect $en_redirect */
  $en_redirect = $this->storage
    ->create();
  $en_redirect
    ->setSource('langpath');
  $en_redirect
    ->setRedirect('/about');
  $en_redirect
    ->setLanguage('en');
  $en_redirect
    ->save();

  // Add a redirect for germany.

  /** @var \Drupal\redirect\Entity\Redirect $en_redirect */
  $en_redirect = $this->storage
    ->create();
  $en_redirect
    ->setSource('langpath');
  $en_redirect
    ->setRedirect('node');
  $en_redirect
    ->setLanguage('de');
  $en_redirect
    ->save();

  // Check redirect for english.

  /** @var \Drupal\redirect\RedirectRepository $repository */
  $repository = \Drupal::service('redirect.repository');
  $found = $repository
    ->findBySourcePath('langpath');
  if (!empty($found)) {
    $this
      ->assertEquals($found[1]
      ->getRedirectUrl()
      ->toString(), '/about', 'Multilingual redirect resolved properly.');
    $this
      ->assertEquals($found[1]
      ->get('language')[0]->value, 'en', 'Multilingual redirect resolved properly.');
  }
  else {
    $this
      ->fail('Failed to resolve the multilingual redirect.');
  }

  // Check redirect for germany.
  \Drupal::configFactory()
    ->getEditable('system.site')
    ->set('default_langcode', 'de')
    ->save();

  /** @var \Drupal\redirect\RedirectRepository $repository */
  $repository = \Drupal::service('redirect.repository');
  $found = $repository
    ->findBySourcePath('langpath');
  if (!empty($found)) {
    $this
      ->assertEquals($found[2]
      ->getRedirectUrl()
      ->toString(), '/node', 'Multilingual redirect resolved properly.');
    $this
      ->assertEquals($found[2]
      ->get('language')[0]->value, 'de', 'Multilingual redirect resolved properly.');
  }
  else {
    $this
      ->fail('Failed to resolve the multilingual redirect.');
  }
}