View source
<?php
namespace Drupal\Tests\content_translation\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\node\Functional\NodeTestBase;
use Drupal\user\Entity\Role;
class ContentTranslationOperationsTest extends NodeTestBase {
protected $baseUser1;
protected $defaultTheme = 'stark';
protected $baseUser2;
protected static $modules = [
'language',
'content_translation',
'node',
'views',
'block',
];
protected function setUp() : void {
parent::setUp();
$langcodes = [
'es',
'ast',
];
foreach ($langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
$this->baseUser1 = $this
->drupalCreateUser([
'access content overview',
]);
$this->baseUser2 = $this
->drupalCreateUser([
'access content overview',
'create content translations',
'update content translations',
'delete content translations',
]);
}
public function testOperationTranslateLink() {
$node = $this
->drupalCreateNode([
'type' => 'article',
'langcode' => 'es',
]);
$this
->drupalLogin($this->baseUser1);
$this
->drupalGet('admin/content');
$this
->assertSession()
->linkByHrefNotExists('node/' . $node
->id() . '/translations');
$this
->drupalLogout();
$this
->drupalLogin($this->baseUser2);
$this
->drupalGet('admin/content');
$this
->assertSession()
->linkByHrefExists('node/' . $node
->id() . '/translations');
$this
->drupalLogout();
user_role_change_permissions(Role::AUTHENTICATED_ID, [
'create content translations' => TRUE,
'access content' => FALSE,
]);
$this
->drupalLogin($this->baseUser1);
$this
->drupalGet($node
->toUrl('drupal:content-translation-overview'));
$this
->assertSession()
->statusCodeEquals(403);
user_role_change_permissions(Role::AUTHENTICATED_ID, [
'create content translations' => TRUE,
'access content' => TRUE,
]);
$node
->setUnpublished()
->save();
$this
->drupalGet($node
->toUrl('drupal:content-translation-overview'));
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogout();
$node
->setPublished()
->save();
user_role_change_permissions(Role::AUTHENTICATED_ID, [
'administer content translation' => TRUE,
'administer languages' => TRUE,
]);
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalLogin($this->baseUser2);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->linkByHrefExists('node/' . $node
->id() . '/translations');
$this
->drupalGet('admin/config/regional/content-language');
$this
->submitForm([
'settings[node][article][translatable]' => FALSE,
], 'Save configuration');
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->linkByHrefNotExists('node/' . $node
->id() . '/translations');
}
public function testContentTranslationOverviewAccess() {
$access_control_handler = \Drupal::entityTypeManager()
->getAccessControlHandler('node');
$user = $this
->createUser([
'create content translations',
'access content',
]);
$this
->drupalLogin($user);
$node = $this
->drupalCreateNode([
'status' => FALSE,
'type' => 'article',
]);
$this
->assertFalse(content_translation_translate_access($node)
->isAllowed());
$access_control_handler
->resetCache();
$node
->setPublished();
$node
->save();
$this
->assertTrue(content_translation_translate_access($node)
->isAllowed());
$access_control_handler
->resetCache();
user_role_change_permissions(Role::AUTHENTICATED_ID, [
'access content' => FALSE,
]);
$user = $this
->createUser([
'create content translations',
]);
$this
->drupalLogin($user);
$this
->assertFalse(content_translation_translate_access($node)
->isAllowed());
$access_control_handler
->resetCache();
}
}