View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\language_selection_page\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
final class LanguageSelectionPageConditionTest extends BrowserTestBase {
public const LANGUAGE_SELECTION_PAGE_TEXT = 'This page is the default page of the module Language Selection Page';
public static $modules = [
'language_selection_page',
'language_selection_page_test',
'locale',
'content_translation',
'block',
'node',
'search',
];
protected $configOriginal;
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('search_form_block', [
'label' => 'Search',
]);
$admin = $this
->drupalCreateUser([], NULL, TRUE);
$this
->drupalLogin($admin);
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this
->drupalPostForm('admin/config/regional/language/add', [
'predefined_langcode' => 'fr',
], 'Add language');
$this
->drupalPostForm('admin/config/regional/language/detection/url', [
'prefix[en]' => 'en',
'prefix[fr]' => 'fr',
], 'Save configuration');
$this
->drupalPostForm('admin/config/regional/language/detection', [
'language_interface[enabled][language-selection-page]' => 1,
'language_interface[enabled][language-url]' => 1,
], 'Save settings');
$this
->drupalPostform('admin/structure/types/manage/page', [
'language_configuration[content_translation]' => 1,
], 'Save content type');
$this->configOriginal = $this
->config('language_selection_page.negotiation')
->get();
}
public function testAjax() {
$node = $this
->drupalCreateNode();
$headers = [];
$this
->drupalGet('node/' . $node
->id(), [], $headers);
$this
->assertLanguageSelectionPageLoaded();
$headers['X-Requested-With'] = 'XMLHttpRequest';
$this
->drupalGet('node/' . $node
->id(), [], $headers);
$this
->assertLanguageSelectionPageNotLoaded();
$this
->resetConfiguration();
}
public function testBlackListedPaths() {
$this
->drupalGet('admin/config/regional/language/detection/language_selection_page');
$this
->assertSession()
->responseContains('/node/add/*');
$this
->assertSession()
->responseContains('/node/*/edit');
$node = $this
->drupalCreateNode([
'langcode' => 'fr',
]);
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$blacklisted_paths = '/admin/*' . \PHP_EOL . '/node/' . $node
->id();
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'blacklisted_paths' => $blacklisted_paths,
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageNotLoaded();
$blacklisted_paths = '/admin/*' . \PHP_EOL . '/node/' . $node
->id() . \PHP_EOL . '/bar';
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'blacklisted_paths' => $blacklisted_paths,
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageNotLoaded();
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'blacklisted_paths' => '/admin/*' . \PHP_EOL . '/node/' . $node
->id() . '/foobar' . \PHP_EOL . '/bar',
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'blacklisted_paths' => '/admin/*' . \PHP_EOL . '/node/' . $node
->id() . '/foobar',
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'blacklisted_paths' => '/admin/*',
], 'Save configuration');
$this
->drupalGet('<front>');
$this
->assertLanguageSelectionPageLoaded();
$this
->drupalPostForm('en/admin/config/regional/language/detection/language_selection_page', [
'blacklisted_paths' => '/admin/*' . \PHP_EOL . '<front>',
], 'Save configuration');
$this
->drupalGet('<front>');
$this
->assertLanguageSelectionPageNotLoaded();
$this
->resetConfiguration();
}
public function testEnabledLanguages() {
$node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->drupalPostForm('admin/config/regional/language/detection/url', [
'prefix[en]' => '',
'prefix[fr]' => 'fr',
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageNotLoaded();
$this
->drupalGet('admin/reports/status');
$this
->assertSession()
->pageTextContains('language if you want to have it enabled in the Language Selection Page');
$this
->drupalPostForm('admin/config/regional/language/detection/url', [
'prefix[en]' => 'en',
'prefix[fr]' => 'fr',
], 'Save configuration');
$this
->drupalGet('admin/reports/status');
$this
->assertSession()
->pageTextNotContains('language if you want to have it enabled in the Language Selection Page');
$this
->resetConfiguration();
}
public function testIgnoreLanguageNeutral() {
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'ignore_neutral' => 1,
], 'Save configuration');
$translatable_node1 = $this
->drupalCreateNode([
'langcode' => 'fr',
]);
$this
->drupalGet('node/' . $translatable_node1
->id());
$this
->assertLanguageSelectionPageLoaded();
$untranslatable_node1 = $this
->drupalCreateNode([
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$this
->drupalGet('node/' . $untranslatable_node1
->id());
$this
->assertLanguageSelectionPageNotLoaded();
$untranslatable_node1 = $this
->drupalCreateNode([
'langcode' => LanguageInterface::LANGCODE_NOT_APPLICABLE,
]);
$this
->drupalGet('node/' . $untranslatable_node1
->id());
$this
->assertLanguageSelectionPageNotLoaded();
$this
->drupalPostform('admin/structure/types/manage/page', [
'language_configuration[content_translation]' => 0,
], 'Save content type');
$this
->drupalGet('node/' . $translatable_node1
->id());
$this
->assertLanguageSelectionPageNotLoaded();
$this
->drupalPostform('admin/structure/types/manage/page', [
'language_configuration[content_translation]' => 1,
], 'Save content type');
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'ignore_neutral' => 0,
], 'Save configuration');
$this
->drupalGet('node/' . $untranslatable_node1
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->resetConfiguration();
}
public function testNegotiationMethodOrder() {
$definitions = \Drupal::service('plugin.manager.language_negotiation_method')
->getDefinitions();
self::assertEquals([
'language-interface',
'language-session',
'language-browser',
'language-url',
'language-content-entity',
'language-url-fallback',
'language-selected',
'language-selection-page',
'language-selection-page-test-negotiator',
'language-user',
'language-user-admin',
], array_keys($definitions));
$this
->drupalPostForm('admin/config/regional/language/detection', [
'language_interface[enabled][language-selection-page-test-negotiator]' => 1,
'language_interface[weight][language-selection-page-test-negotiator]' => -10,
'language_interface[enabled][language-selection-page]' => 1,
'language_interface[weight][language-selection-page]' => 0,
'language_interface[enabled][language-url]' => 0,
], 'Save settings');
$node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node
->id() . '-fake');
$this
->assertLanguageSelectionPageNotLoaded();
}
public function testPageTitle() {
$title = 'adJKFD#@H5864193177';
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'title' => $title,
], 'Save configuration');
$node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->assertSession()
->responseContains('<title>' . $title);
$this
->resetConfiguration();
}
public function testPath() {
$node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'path' => '/test',
], 'Save configuration');
$this
->resetConfiguration();
}
public function testType() {
$node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'type' => 'block',
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageNotLoaded();
$this
->assertSession()
->pageTextNotContains('Language Selection Page block');
$this
->drupalPostForm('admin/structure/block/add/language-selection-page/stark', [
'region' => 'content',
], 'Save block');
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->pageTextContains('Language Selection Page block');
$this
->assertLanguageSelectionPageLoaded();
$blacklisted_paths = implode(\PHP_EOL, [
'/admin',
'/admin/*',
'/admin*',
'/node/' . $node
->id(),
]);
$this
->drupalPostForm('admin/config/regional/language/detection/language_selection_page', [
'blacklisted_paths' => $blacklisted_paths,
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->pageTextNotContains('Language Selection Page block');
$this
->assertLanguageSelectionPageNotLoaded();
$this
->resetConfiguration();
$this
->drupalPostForm('en/admin/config/regional/language/detection/language_selection_page', [
'type' => 'standalone',
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->assertSession()
->responseNotContains('<h2>Search</h2>');
$this
->drupalPostForm('en/admin/config/regional/language/detection/language_selection_page', [
'type' => 'embedded',
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertLanguageSelectionPageLoaded();
$this
->assertSession()
->responseContains('<h2>Search</h2>');
$this
->resetConfiguration();
}
protected function assertLanguageSelectionPageLoaded() {
$this
->assertSession()
->pageTextContains(self::LANGUAGE_SELECTION_PAGE_TEXT);
}
protected function assertLanguageSelectionPageNotLoaded() {
$this
->assertSession()
->pageTextNotContains(self::LANGUAGE_SELECTION_PAGE_TEXT);
}
protected function resetConfiguration() {
$this
->config('language_selection_page.negotiation')
->setData($this->configOriginal)
->save();
}
}