Last active
August 29, 2015 14:23
-
-
Save victor-shelepen/1841f5bbd59768962772 to your computer and use it in GitHub Desktop.
This is a test for Drupal 8. It tests page caching with language negotiation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Contains \Drupal\language\Tests\LanguagePageCacheTest. | |
*/ | |
// d8runtest --class "Drupal\language\Tests\LanguagePageCacheTest" | |
namespace Drupal\language\Tests; | |
use Drupal\Core\Url; | |
use Drupal\language\Entity\ConfigurableLanguage; | |
use Drupal\Core\Language\Language; | |
use Drupal\Core\Language\LanguageInterface; | |
use Drupal\simpletest\WebTestBase; | |
/** | |
* Tests browser language detection and page caching. | |
* | |
* @group language | |
*/ | |
class LanguagePageCacheTest extends WebTestBase { | |
/** | |
* The added languages. | |
* | |
* @var array | |
*/ | |
protected $langcodes; | |
/** | |
* Modules to enable. | |
* | |
* @var array | |
*/ | |
//public static $modules = array('user', 'language', 'content_translation', 'entity_test'); | |
public static $modules = array('locale', 'language', 'block', 'language_test'); | |
/** | |
* Adds additional languages. | |
*/ | |
protected function setupLanguages() { | |
$this->langcodes = array('fr'); | |
foreach ($this->langcodes as $langcode) { | |
ConfigurableLanguage::createFromLangcode($langcode)->save(); | |
} | |
array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId()); | |
} | |
protected function setUp() { | |
parent::setUp(); | |
$this->setupLanguages(); | |
// Create and login user. | |
$admin_user = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'access administration pages')); | |
$this->drupalLogin($admin_user); | |
} | |
/** | |
* ... | |
*/ | |
function testPageCache() { | |
$this->drupalGet('admin/config/regional/language/detection'); | |
$this->setLanguageNegotiationOrder(); | |
$this->drupalGet('admin/config/regional/language/detection'); | |
// Enable the language switching block. | |
$block = $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, array( | |
'id' => 'test_language_block', | |
// Ensure a 2-byte UTF-8 sequence is in the tested output. | |
'label' => $this->randomMachineName(8) . '×', | |
)); | |
$this->drupalLogout(); | |
$this->setCache(); | |
$path = 'language_test/type-link-active-class'; | |
$this->drupalGet('fr/language_test/type-link-active-class'); | |
$links = $this->getLinks($path); | |
$this->assert(TRUE, print_r($links, 1)); | |
$this->assert($links['fr']['class'] == 'is-active', 'The "en" is defied'); | |
$this->getPage($path, 'en'); | |
$links = $this->getLinks($path); | |
$this->assert(TRUE, print_r($links, 1)); | |
$this->assert($links['en']['class'] == 'is-active', 'The "en" is defied'); | |
$this->getPage($path, 'fr'); | |
$links = $this->getLinks($path); | |
$this->assert(TRUE, print_r($links, 1)); | |
$this->assert($links['fr']['class'] == 'is-active', 'The "fr" is defied'); | |
} | |
public function setCache($period=300) { | |
$config = $this->config('system.performance'); | |
$config->set('cache.page.max_age', $period); | |
$config->save(); | |
} | |
public function setLanguageNegotiationOrder() { | |
//$this->config('language.negotiation')->set('url.prefixes.en', 'en')->save(); | |
$method_weights = [ | |
'language-url' => '-20', | |
'language-browser' => '-19', | |
'language-selected' => '-15' | |
]; | |
$language_negotiator = \Drupal::service('language_negotiator'); | |
$language_negotiator->saveConfiguration(LanguageInterface::TYPE_INTERFACE, $method_weights); | |
$this->config('language.types')->set('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.method_weights', $method_weights)->save(); | |
$this->rebuildContainer(); | |
} | |
public function getPage($path, $langcode_browser_fallback='en') { | |
$http_header = array('Accept-Language: ' . $langcode_browser_fallback . ';q=1'); | |
$language = new Language(array('id' => '')); | |
$this->drupalGet($path, array('language' => $language), $http_header); | |
} | |
public function getLinks($path) { | |
return [ | |
'no_lang' => $this->xpath('//a[@id = :id and @data-drupal-link-system-path = :path]', array(':id' => 'no_lang_link', ':path' => $path))[0], | |
'en' => $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', array(':id' => 'en_link', ':lang' => 'en', ':path' => $path))[0], | |
'fr' => $this->xpath('//a[@id = :id and @hreflang = :lang and @data-drupal-link-system-path = :path]', array(':id' => 'fr_link', ':lang' => 'fr', ':path' => $path))[0] | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment