View source
<?php
namespace Drupal\Tests\translation_views\Functional;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\user\Entity\Role;
use Drupal\views\Tests\ViewTestData;
class TranslationOperationsFieldPermissionsTest extends ViewTestBase {
private static $langcodes = [
'fr',
'de',
'it',
'af',
'sq',
];
public static $modules = [
'content_translation',
'node',
'translation_views',
'translation_views_test_views',
];
protected $profile = 'standard';
public static $testViews = [
'test_operations_links',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->drupalLogin($this->rootUser);
ViewTestData::createTestViews(get_class($this), [
'translation_views_test_views',
]);
try {
$this
->setUpLanguages();
} catch (EntityStorageException $e) {
$this
->verbose($e
->getMessage());
}
$this
->enableTranslation('node', 'article');
$this
->drupalPostForm('node/add/article', [
'title[0][value]' => $this
->randomString(),
], 'Save');
$this
->drupalLogout();
}
private function setUpLanguages() {
foreach (self::$langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
}
private function enableTranslation($category, $subcategory) {
$this
->drupalPostForm('admin/config/regional/content-language', [
"entity_types[{$category}]" => 1,
"settings[{$category}][{$subcategory}][translatable]" => 1,
"settings[{$category}][{$subcategory}][settings][language][language_alterable]" => 1,
], 'Save configuration');
\Drupal::entityTypeManager()
->clearCachedDefinitions();
}
private function translateNode() {
$node = Node::load(1);
foreach (self::$langcodes as $langcode) {
if (!$node
->hasTranslation($langcode)) {
$node
->addTranslation($langcode, [
'title' => $this
->randomMachineName(),
])
->save();
}
}
}
private function addPermissionsForAuthUser(array $permissions = []) {
if (!empty($permissions)) {
$role = Role::load(Role::AUTHENTICATED_ID);
if ($role instanceof Role) {
$this
->grantPermissions($role, $permissions);
}
}
}
public function testTranslationOperationsCreatePermissions() {
$default_language = \Drupal::languageManager()
->getDefaultLanguage();
$target_language = static::$langcodes[mt_rand(0, 4)];
$this
->assertNotNull($target_language);
$this
->assertNotNull($default_language);
$userCreate = $this
->createUser([
'create content translations',
]);
$this
->drupalLogin($userCreate);
$this
->drupalGet('/translate/content', [
'query' => [
'langcode' => $default_language
->getId(),
'translation_target_language' => $target_language,
'translation_outdated' => 'All',
'translation_status' => 'All',
],
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementNotExists('css', 'table > tbody > tr:nth-child(1) .views-field-translation-operations ul li a');
$this
->addPermissionsForAuthUser([
'translate any entity',
]);
$this
->assertTrue($userCreate
->hasPermission('translate any entity'));
$this
->drupalGet('/translate/content', [
'query' => [
'langcode' => $default_language
->getId(),
'translation_target_language' => $target_language,
'translation_outdated' => 'All',
'translation_status' => 'All',
],
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementTextContains('css', "table > tbody > tr:nth-child(1) .views-field-translation-operations ul li a", 'Add');
}
public function testTranslationOperationsUpdatePermissions() {
$this
->translateNode();
$userUpdate = $this
->createUser([
'update content translations',
]);
$this
->drupalLogin($userUpdate);
$this
->drupalGet('/test_operations_links', [
'query' => [
'translation_target_language' => 'fr',
],
]);
$this
->assertSession()
->elementTextNotContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations", 'Edit');
$this
->addPermissionsForAuthUser([
'translate article node',
]);
$this
->assertTrue($userUpdate
->hasPermission('translate article node'));
$this
->drupalGet('/test_operations_links', [
'query' => [
'translation_target_language' => 'fr',
],
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementTextContains('css', '.view-content > div:nth-child(1) .views-field-translation-operations ul .edit a', 'Edit');
$this
->click('.view-content > div:nth-child(1) .views-field-translation-operations ul .edit a');
$this
->assertUrl('/fr/node/1/translations/edit/fr');
$this
->addPermissionsForAuthUser([
'edit any article content',
]);
$this
->assertTrue($userUpdate
->hasPermission('edit any article content'));
$this
->drupalGet('/test_operations_links', [
'query' => [
'translation_target_language' => 'fr',
],
]);
$this
->assertSession()
->elementTextContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations ul .edit a", 'Edit');
$this
->click('.view-content > div:nth-child(1) .views-field-translation-operations ul .edit a');
$this
->assertUrl('/fr/node/1/edit');
}
public function testTranslationOperationsDeletePermissions() {
$this
->translateNode();
$userDelete = $this
->createUser([
'delete content translations',
]);
$this
->drupalLogin($userDelete);
$this
->drupalGet('/test_operations_links', [
'query' => [
'translation_target_language' => 'fr',
],
]);
$this
->assertSession()
->elementTextNotContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations", 'Delete');
$this
->addPermissionsForAuthUser([
'translate article node',
]);
$this
->assertTrue($userDelete
->hasPermission('translate article node'));
$this
->drupalGet('/test_operations_links', [
'query' => [
'translation_target_language' => 'fr',
],
]);
$this
->assertSession()
->elementTextContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations ul .delete a", 'Delete');
$this
->click('.view-content > div:nth-child(1) .views-field-translation-operations ul .delete a');
$this
->assertUrl('/fr/node/1/translations/delete/fr');
$this
->addPermissionsForAuthUser([
'delete any article content',
]);
$this
->assertTrue($userDelete
->hasPermission('delete any article content'));
$this
->drupalGet('/test_operations_links', [
'query' => [
'translation_target_language' => 'fr',
],
]);
$this
->assertSession()
->elementTextContains('css', ".view-content > div:nth-child(1) .views-field-translation-operations ul .delete a", 'Delete');
$this
->click('.view-content > div:nth-child(1) .views-field-translation-operations ul .delete a');
$this
->assertUrl('/fr/node/1/delete');
}
}