You are here

public function LanguageCookieConditionTest::testBlackListedPaths in Language Cookie 8

Test the "Blacklisted paths" condition.

File

tests/src/Functional/LanguageCookieConditionTest.php, line 18

Class

LanguageCookieConditionTest
Tests that the condition plugins work.

Namespace

Drupal\Tests\language_cookie\Functional

Code

public function testBlackListedPaths() {
  $node = $this
    ->drupalCreateNode();

  // Remove cookie.
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $last = $this->container
    ->get('state')
    ->get('language_cookie_test.language_negotiation_last');
  $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
  $this
    ->assertEquals('en', $last_interface_language);
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->cookieEquals('language', 'fr');

  // Test that the newly set cookie sets the language to french.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $last = $this->container
    ->get('state')
    ->get('language_cookie_test.language_negotiation_last');
  $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
  $this
    ->assertEquals('fr', $last_interface_language);

  // Add node to blacklisted paths.
  $this
    ->drupalGet('en/admin/config/regional/language/detection/language_cookie');
  $this
    ->submitForm([
    'blacklisted_paths' => '/admin/*' . PHP_EOL . '/node/' . $node
      ->id(),
  ], 'Save configuration');
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('en/node/' . $node
    ->id());
  $this
    ->assertEmpty($this
    ->getSession()
    ->getCookie('language'));

  // Add node to blacklisted paths (in the middle).
  $this
    ->drupalGet('en/admin/config/regional/language/detection/language_cookie');
  $this
    ->submitForm([
    'blacklisted_paths' => '/admin/*' . PHP_EOL . '/node/' . $node
      ->id() . PHP_EOL . '/bar',
  ], 'Save configuration');
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('en/node/' . $node
    ->id());
  $this
    ->assertEmpty($this
    ->getSession()
    ->getCookie('language'));

  // Add string that contains node, but not node itself.
  $this
    ->drupalGet('en/admin/config/regional/language/detection/language_cookie');
  $this
    ->submitForm([
    'blacklisted_paths' => '/admin/*' . PHP_EOL . '/node/' . $node
      ->id() . '/foobar' . PHP_EOL . '/bar',
  ], 'Save configuration');
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('en/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->cookieEquals('language', 'en');

  // Add string that starts with node, but not node itself.
  $this
    ->drupalGet('en/admin/config/regional/language/detection/language_cookie');
  $this
    ->submitForm([
    'blacklisted_paths' => '/admin/*' . PHP_EOL . '/node/' . $node
      ->id() . '/foobar',
  ], 'Save configuration');
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('en/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->cookieEquals('language', 'en');

  // Test front page.
  $this
    ->drupalGet('en/admin/config/regional/language/detection/language_cookie');
  $this
    ->submitForm([
    'blacklisted_paths' => '/admin/*',
  ], 'Save configuration');
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('en');
  $this
    ->assertSession()
    ->cookieEquals('language', 'en');
  $this
    ->drupalGet('en/admin/config/regional/language/detection/language_cookie');
  $this
    ->submitForm([
    'blacklisted_paths' => '/admin/*' . PHP_EOL . '<front>',
  ], 'Save configuration');
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('en');
  $this
    ->assertEmpty($this
    ->getSession()
    ->getCookie('language'));

  // Test hardcoded blacklist.
  $this
    ->drupalGet('admin/config/search/path/add');
  $this
    ->submitForm([
    'langcode[0][value]' => 'und',
    'path[0][value]' => '/node/' . $node
      ->id(),
    // We create this alias just to test that the hardcoded paths ported
    // from the Drupal 7 version of this module are also picked up.
    'alias[0][value]' => '/httprl_async_function_callback',
  ], 'Save');
  $this
    ->getSession()
    ->setCookie('language', NULL);
  $this
    ->drupalGet('fr/httprl_async_function_callback');

  // Check that cookie is not set.
  $this
    ->assertEmpty($this
    ->getSession()
    ->getCookie('language'));
}