Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / content_translation / tests / src / Functional / ContentTranslationOperationsTest.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Functional;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\Tests\node\Functional\NodeTestBase;
7 use Drupal\user\Entity\Role;
8
9 /**
10  * Tests the content translation operations available in the content listing.
11  *
12  * @group content_translation
13  */
14 class ContentTranslationOperationsTest extends NodeTestBase {
15
16   /**
17    * A base user.
18    *
19    * @var \Drupal\user\Entity\User|false
20    */
21   protected $baseUser1;
22
23   /**
24    * A base user.
25    *
26    * @var \Drupal\user\Entity\User|false
27    */
28   protected $baseUser2;
29
30   /**
31    * Modules to enable.
32    *
33    * @var array
34    */
35   public static $modules = ['language', 'content_translation', 'node', 'views', 'block'];
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     // Enable additional languages.
44     $langcodes = ['es', 'ast'];
45     foreach ($langcodes as $langcode) {
46       ConfigurableLanguage::createFromLangcode($langcode)->save();
47     }
48
49     // Enable translation for the current entity type and ensure the change is
50     // picked up.
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();
56
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']);
59   }
60
61   /**
62    * Test that the operation "Translate" is displayed in the content listing.
63    */
64   public function testOperationTranslateLink() {
65     $node = $this->drupalCreateNode(['type' => 'article', 'langcode' => 'es']);
66     // Verify no translation operation links are displayed for users without
67     // permission.
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
73     // permissions.
74     $this->drupalLogin($this->baseUser2);
75     $this->drupalGet('admin/content');
76     $this->assertLinkByHref('node/' . $node->id() . '/translations');
77
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,
83       [
84         'create content translations' => TRUE,
85         'access content' => FALSE,
86       ]
87     );
88     $this->drupalLogin($this->baseUser1);
89     $this->drupalGet($node->urlInfo('drupal:content-translation-overview'));
90     $this->assertResponse(403);
91
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,
96       [
97         'create content translations' => TRUE,
98         'access content' => TRUE,
99       ]
100     );
101     $node->setUnpublished()->save();
102     $this->drupalGet($node->urlInfo('drupal:content-translation-overview'));
103     $this->assertResponse(403);
104     $this->drupalLogout();
105
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,
111       [
112         'administer content translation' => TRUE,
113         'administer languages' => TRUE,
114       ]
115     );
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');
123   }
124
125   /**
126    * Tests the access to the overview page for translations.
127    *
128    * @see content_translation_translate_access()
129    */
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);
134
135     $node = $this->drupalCreateNode(['status' => FALSE, 'type' => 'article']);
136     $this->assertFalse(content_translation_translate_access($node)->isAllowed());
137     $access_control_handler->resetCache();
138
139     $node->setPublished();
140     $node->save();
141     $this->assertTrue(content_translation_translate_access($node)->isAllowed());
142     $access_control_handler->resetCache();
143
144     user_role_change_permissions(
145       Role::AUTHENTICATED_ID,
146       [
147         'access content' => FALSE,
148       ]
149     );
150
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();
155   }
156
157 }