View source
<?php
namespace Drupal\content_translation\Tests;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Tests\NodeTestBase;
use Drupal\user\Entity\Role;
class ContentTranslationOperationsTest extends NodeTestBase {
protected $baseUser1;
protected $baseUser2;
public static $modules = [
'language',
'content_translation',
'node',
'views',
];
protected function setUp() {
parent::setUp();
$langcodes = [
'es',
'ast',
];
foreach ($langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
drupal_static_reset();
\Drupal::entityManager()
->clearCachedDefinitions();
\Drupal::service('router.builder')
->rebuild();
\Drupal::service('entity.definition_update_manager')
->applyUpdates();
$this->baseUser1 = $this
->drupalCreateUser([
'access content overview',
]);
$this->baseUser2 = $this
->drupalCreateUser([
'access content overview',
'create content translations',
'update content translations',
'delete content translations',
]);
}
function testOperationTranslateLink() {
$node = $this
->drupalCreateNode([
'type' => 'article',
'langcode' => 'es',
]);
$this
->drupalLogin($this->baseUser1);
$this
->drupalGet('admin/content');
$this
->assertNoLinkByHref('node/' . $node
->id() . '/translations');
$this
->drupalLogout();
$this
->drupalLogin($this->baseUser2);
$this
->drupalGet('admin/content');
$this
->assertLinkByHref('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
->urlInfo('drupal:content-translation-overview'));
$this
->assertResponse(403);
user_role_change_permissions(Role::AUTHENTICATED_ID, [
'create content translations' => TRUE,
'access content' => TRUE,
]);
$node
->setPublished(FALSE)
->save();
$this
->drupalGet($node
->urlInfo('drupal:content-translation-overview'));
$this
->assertResponse(403);
}
public function testContentTranslationOverviewAccess() {
$access_control_handler = \Drupal::entityManager()
->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(TRUE);
$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();
}
}