3 namespace Drupal\Tests\content_translation\Functional;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\Tests\node\Functional\NodeTestBase;
7 use Drupal\user\Entity\Role;
10 * Tests the content translation operations available in the content listing.
12 * @group content_translation
14 class ContentTranslationOperationsTest extends NodeTestBase {
19 * @var \Drupal\user\Entity\User|false
26 * @var \Drupal\user\Entity\User|false
35 public static $modules = ['language', 'content_translation', 'node', 'views', 'block'];
40 protected function setUp() {
43 // Enable additional languages.
44 $langcodes = ['es', 'ast'];
45 foreach ($langcodes as $langcode) {
46 ConfigurableLanguage::createFromLangcode($langcode)->save();
49 // Enable translation for the current entity type and ensure the change is
51 \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE);
52 drupal_static_reset();
53 \Drupal::entityManager()->clearCachedDefinitions();
54 \Drupal::service('router.builder')->rebuild();
55 \Drupal::service('entity.definition_update_manager')->applyUpdates();
57 $this->baseUser1 = $this->drupalCreateUser(['access content overview']);
58 $this->baseUser2 = $this->drupalCreateUser(['access content overview', 'create content translations', 'update content translations', 'delete content translations']);
62 * Test that the operation "Translate" is displayed in the content listing.
64 public function testOperationTranslateLink() {
65 $node = $this->drupalCreateNode(['type' => 'article', 'langcode' => 'es']);
66 // Verify no translation operation links are displayed for users without
68 $this->drupalLogin($this->baseUser1);
69 $this->drupalGet('admin/content');
70 $this->assertNoLinkByHref('node/' . $node->id() . '/translations');
71 $this->drupalLogout();
72 // Verify there's a translation operation link for users with enough
74 $this->drupalLogin($this->baseUser2);
75 $this->drupalGet('admin/content');
76 $this->assertLinkByHref('node/' . $node->id() . '/translations');
78 // Ensure that an unintended misconfiguration of permissions does not open
79 // access to the translation form, see https://www.drupal.org/node/2558905.
80 $this->drupalLogout();
81 user_role_change_permissions(
82 Role::AUTHENTICATED_ID,
84 'create content translations' => TRUE,
85 'access content' => FALSE,
88 $this->drupalLogin($this->baseUser1);
89 $this->drupalGet($node->urlInfo('drupal:content-translation-overview'));
90 $this->assertResponse(403);
92 // Ensure that the translation overview is also not accessible when the user
93 // has 'access content', but the node is not published.
94 user_role_change_permissions(
95 Role::AUTHENTICATED_ID,
97 'create content translations' => TRUE,
98 'access content' => TRUE,
101 $node->setUnpublished()->save();
102 $this->drupalGet($node->urlInfo('drupal:content-translation-overview'));
103 $this->assertResponse(403);
104 $this->drupalLogout();
106 // Ensure the 'Translate' local task does not show up anymore when disabling
107 // translations for a content type.
108 $node->setPublished()->save();
109 user_role_change_permissions(
110 Role::AUTHENTICATED_ID,
112 'administer content translation' => TRUE,
113 'administer languages' => TRUE,
116 $this->drupalPlaceBlock('local_tasks_block');
117 $this->drupalLogin($this->baseUser2);
118 $this->drupalGet('node/' . $node->id());
119 $this->assertLinkByHref('node/' . $node->id() . '/translations');
120 $this->drupalPostForm('admin/config/regional/content-language', ['settings[node][article][translatable]' => FALSE], t('Save configuration'));
121 $this->drupalGet('node/' . $node->id());
122 $this->assertNoLinkByHref('node/' . $node->id() . '/translations');
126 * Tests the access to the overview page for translations.
128 * @see content_translation_translate_access()
130 public function testContentTranslationOverviewAccess() {
131 $access_control_handler = \Drupal::entityManager()->getAccessControlHandler('node');
132 $user = $this->createUser(['create content translations', 'access content']);
133 $this->drupalLogin($user);
135 $node = $this->drupalCreateNode(['status' => FALSE, 'type' => 'article']);
136 $this->assertFalse(content_translation_translate_access($node)->isAllowed());
137 $access_control_handler->resetCache();
139 $node->setPublished();
141 $this->assertTrue(content_translation_translate_access($node)->isAllowed());
142 $access_control_handler->resetCache();
144 user_role_change_permissions(
145 Role::AUTHENTICATED_ID,
147 'access content' => FALSE,
151 $user = $this->createUser(['create content translations']);
152 $this->drupalLogin($user);
153 $this->assertFalse(content_translation_translate_access($node)->isAllowed());
154 $access_control_handler->resetCache();