View source
<?php
namespace Drupal\Tests\language_cookie\Functional;
class LanguageCookieNegotiationConfigTest extends LanguageCookieTestBase {
public function testLanguageCookieDefaultConfig() {
$config = $this
->config('language_cookie.negotiation');
$this
->assertEquals('language', $config
->get('param'));
$this
->assertEquals(31536000, $config
->get('time'));
$this
->assertEquals('/', $config
->get('path'));
$this
->assertEquals('', $config
->get('domain'));
$this
->assertFalse($config
->get('secure'));
$this
->assertTrue($config
->get('http_only'));
$this
->assertEquals('language_interface', $config
->get('language_type'));
$this
->assertFalse($config
->get('set_on_every_pageload'));
$this
->assertEquals([], $config
->get('blacklisted_paths'));
}
public function testLanguageCookieNegotiationConfigFormAccess() {
$this
->drupalGet('admin/config/regional/language/detection/language_cookie');
$this
->assertSession()
->statusCodeEquals(200);
$user = $this
->drupalCreateUser([
'administer languages',
]);
$this
->drupalLogin($user);
$this
->drupalGet('admin/config/regional/language/detection/language_cookie');
$this
->assertSession()
->statusCodeEquals(200);
$user = $this
->drupalCreateUser();
$this
->drupalLogin($user);
$this
->drupalGet('admin/config/regional/language/detection/language_cookie');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogout();
$this
->drupalGet('admin/config/regional/language/detection/language_cookie');
$this
->assertSession()
->statusCodeEquals(403);
}
public function testLanguageCookieNegotiationConfigForm() {
$this
->drupalGet('admin/config/regional/language/detection/language_cookie');
$this
->submitForm([
'param' => 'language_cookie',
'time' => 100000,
'path' => '/drupal',
'domain' => 'example.com',
'secure' => TRUE,
'http_only' => TRUE,
'set_on_every_pageload' => TRUE,
'blacklisted_paths' => '/admin/*' . PHP_EOL . '/user/*',
], 'Save configuration');
$config = $this
->config('language_cookie.negotiation');
$this
->assertEquals('language_cookie', $config
->get('param'));
$this
->assertEquals(100000, $config
->get('time'));
$this
->assertEquals('/drupal', $config
->get('path'));
$this
->assertEquals('example.com', $config
->get('domain'));
$this
->assertTrue($config
->get('secure'));
$this
->assertTrue($config
->get('http_only'));
$this
->assertTrue($config
->get('set_on_every_pageload'));
$this
->assertEquals([
'/admin/*',
'/user/*',
], $config
->get('blacklisted_paths'));
$this
->drupalGet('admin/config/regional/language/detection/language_cookie');
$this
->assertSession()
->fieldValueEquals('param', 'language_cookie');
$this
->assertSession()
->fieldValueEquals('time', 100000);
$this
->assertSession()
->fieldValueEquals('path', '/drupal');
$this
->assertSession()
->fieldValueEquals('domain', 'example.com');
$this
->assertSession()
->checkboxChecked('secure');
$this
->assertSession()
->checkboxChecked('http_only');
$this
->assertSession()
->checkboxChecked('set_on_every_pageload');
$this
->assertSession()
->fieldValueEquals('blacklisted_paths', '/admin/*' . PHP_EOL . '/user/*');
}
public function testLanguageCookieParamOption() {
$this
->getSession()
->setCookie('language', NULL);
$config = $this
->config('language_cookie.negotiation');
$config
->set('param', 'language_cookie');
$config
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertEmpty($this
->getSession()
->getCookie('language'));
$this
->assertSession()
->cookieEquals('language_cookie', 'fr');
}
public function testLanguageCookieTimeOption() {
$config = $this
->config('language_cookie.negotiation');
$config
->set('time', 60 * 60 * 24);
$config
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertRegExp('/max-age=86/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie contains expires option.');
$config
->set('time', 60 * 60 * 24 * 2);
$config
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('en/node/' . $node
->id());
$this
->assertRegExp('/max-age=172/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie contains expires option.');
}
public function testLanguageCookiePathOption() {
$config = $this
->config('language_cookie.negotiation');
$config
->set('path', '/drupal');
$config
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertRegExp('/path=\\/drupal;/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie contains path option.');
}
public function testLanguageCookieDomainOption() {
$config = $this
->config('language_cookie.negotiation');
$config
->set('domain', 'example.org');
$config
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertRegExp('/domain=example\\.org;/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie contains domain option.');
}
public function testLanguageCookieSecureOption() {
$config = $this
->config('language_cookie.negotiation');
$config
->set('secure', TRUE);
$config
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertRegExp('/secure/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie contains secure option.');
}
public function testLanguageCookieHttpOnlyOption() {
$config = $this
->config('language_cookie.negotiation');
$config
->set('http_only', TRUE);
$config
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertRegExp('/httponly/i', $this
->getSession()
->getResponseHeader('Set-Cookie'), 'Cookie contains httpOnly option.');
$config
->set('http_only', FALSE);
$config
->save();
$this
->drupalGet('en/node/' . $node
->id());
$this
->assertNotRegExp('/httponly/i', $this
->getSession()
->getResponseHeader('Set-Cookie'), 'Cookie contains httpOnly option.');
}
public function testLanguageCookieSetOnEveryPageLoadOption() {
$node = $this
->drupalCreateNode();
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertRegExp('/language=fr/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie is set.');
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertEmpty($this
->drupalGetHeader('Set-Cookie'));
$this
->drupalGet('en/node/' . $node
->id());
$this
->assertRegExp('/language=en/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie is set.');
$config = $this
->config('language_cookie.negotiation');
$config
->set('set_on_every_pageload', TRUE);
$config
->save();
$node = $this
->drupalCreateNode();
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertRegExp('/language=fr/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie is set.');
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertRegExp('/language=fr/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie is set.');
$this
->drupalGet('en/node/' . $node
->id());
$this
->assertRegExp('/language=en/i', $this
->drupalGetHeader('Set-Cookie'), 'Cookie is set.');
}
}