View source
<?php
namespace Drupal\content_language_access\Tests;
use Drupal;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
abstract class ContentLanguageAccessTestBase extends BrowserTestBase {
protected $profile = 'standard';
public static $modules = [
'content_language_access',
];
protected $adminUser;
protected $visitor;
protected $contentType;
protected $nodes;
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer languages',
'administer site configuration',
'access administration pages',
'administer content types',
'administer nodes',
'administer users',
]);
$this->visitor = $this
->drupalCreateUser([
'access content',
]);
$this->languages = Drupal::languageManager()
->getLanguages();
$this
->configureLanguages();
$this
->createContentType();
$this
->createContents();
}
protected function configureLanguages() {
$this
->drupalLogin($this->adminUser);
$this
->addLanguage('aaa');
$this
->addLanguage('bbb');
Drupal::languageManager()
->reset();
}
protected function createContentType(array $values = []) {
$this->contentType = $this
->drupalCreateContentType();
$this
->drupalGet("admin/structure/types/manage/{$this->contentType->id()}");
$this
->assertText(t('Language settings'), 'Multilingual support widget present on content type configuration form.');
$edit = [
'language_configuration[language_alterable]' => TRUE,
];
$this
->drupalPostForm("admin/structure/types/manage/{$this->contentType->id()}", $edit, t('Save content type'));
$this
->assertRaw(t('The content type %type has been updated.', [
'%type' => $this->contentType
->label(),
]));
}
protected function createContents() {
$this
->drupalLogin($this->adminUser);
$languages = $this
->getLanguageList();
foreach ($languages as $language_key => $language) {
$settings = [
'title' => 'Test ' . $language
->getName(),
'langcode' => $language_key,
'type' => $this->contentType
->id(),
];
$this->nodes[$language_key] = $this
->drupalCreateNode($settings);
}
}
protected function getLanguageList($with_neutral_language = TRUE) {
$languages = Drupal::languageManager()
->getLanguages();
if ($with_neutral_language) {
$languages[Language::LANGCODE_NOT_SPECIFIED] = new Language([
'id' => Language::LANGCODE_NOT_SPECIFIED,
'name' => 'Language Neutral',
]);
}
return $languages;
}
protected function addLanguage($language_code) {
$this
->drupalGet('admin/config/regional/language');
if (strpos($this
->getTextContent(), 'edit-languages-' . $language_code) === FALSE) {
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $language_code,
'label' => $language_code,
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
}
}
protected function baseTestContentLanguageAccess() {
$this
->drupalLogin($this->visitor);
$languages = $this
->getLanguageList(FALSE);
foreach ($this->nodes as $node) {
foreach ($languages as $language) {
if ($language
->getId() != Drupal::languageManager()
->getDefaultLanguage()
->getId()) {
$prefix = $language
->getId() . '/';
}
else {
$prefix = '';
}
$this
->drupalGet($prefix . 'node/' . $node
->id());
$node_language = $node
->language()
->getId();
if ($node_language == Language::LANGCODE_NOT_SPECIFIED || $node_language == Language::LANGCODE_NOT_APPLICABLE || $node_language == $language
->getId()) {
$this
->assertResponse(200);
}
else {
$this
->assertResponse(403);
}
}
}
}
}