b85c3aad5d1efbd585bdab2a0d7df430baacf52d
[yaffs-website] / src / Functional / ContentTranslationContextualLinksTest.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Functional;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Tests that contextual links are available for content translation.
12  *
13  * @group content_translation
14  */
15 class ContentTranslationContextualLinksTest extends BrowserTestBase {
16
17   /**
18    * The bundle being tested.
19    *
20    * @var string
21    */
22   protected $bundle;
23
24   /**
25    * The content type being tested.
26    *
27    * @var \Drupal\node\Entity\NodeType
28    */
29   protected $contentType;
30
31   /**
32    * The 'translator' user to use during testing.
33    *
34    * @var \Drupal\user\UserInterface
35    */
36   protected $translator;
37
38   /**
39    * The enabled languages.
40    *
41    * @var array
42    */
43   protected $langcodes;
44
45   /**
46    * Modules to enable.
47    *
48    * @var array
49    */
50   public static $modules = ['content_translation', 'contextual', 'node'];
51
52   /**
53    * The profile to install as a basis for testing.
54    *
55    * @var string
56    */
57   protected $profile = 'testing';
58
59   protected function setUp() {
60     parent::setUp();
61     // Set up an additional language.
62     $this->langcodes = [\Drupal::languageManager()->getDefaultLanguage()->getId(), 'es'];
63     ConfigurableLanguage::createFromLangcode('es')->save();
64
65     // Create a content type.
66     $this->bundle = $this->randomMachineName();
67     $this->contentType = $this->drupalCreateContentType(['type' => $this->bundle]);
68
69     // Add a field to the content type. The field is not yet translatable.
70     FieldStorageConfig::create([
71       'field_name' => 'field_test_text',
72       'entity_type' => 'node',
73       'type' => 'text',
74       'cardinality' => 1,
75     ])->save();
76     FieldConfig::create([
77       'entity_type' => 'node',
78       'field_name' => 'field_test_text',
79       'bundle' => $this->bundle,
80       'label' => 'Test text-field',
81     ])->save();
82     entity_get_form_display('node', $this->bundle, 'default')
83       ->setComponent('field_test_text', [
84         'type' => 'text_textfield',
85         'weight' => 0,
86       ])
87       ->save();
88
89     // Create a translator user.
90     $permissions = [
91       'access contextual links',
92       'administer nodes',
93       "edit any $this->bundle content",
94       'translate any entity',
95     ];
96     $this->translator = $this->drupalCreateUser($permissions);
97   }
98
99   /**
100    * Tests that a contextual link is available for translating a node.
101    */
102   public function testContentTranslationContextualLinks() {
103     // Create a node.
104     $title = $this->randomString();
105     $this->drupalCreateNode(['type' => $this->bundle, 'title' => $title, 'langcode' => 'en']);
106     $node = $this->drupalGetNodeByTitle($title);
107
108     // Use a UI form submission to make the node type and field translatable.
109     // This tests that caches are properly invalidated.
110     $this->drupalLogin($this->rootUser);
111     $edit = [
112       'entity_types[node]' => TRUE,
113       'settings[node][' . $this->bundle . '][settings][language][language_alterable]' => TRUE,
114       'settings[node][' . $this->bundle . '][translatable]' => TRUE,
115       'settings[node][' . $this->bundle . '][fields][field_test_text]' => TRUE,
116     ];
117     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
118     $this->drupalLogout();
119
120     // Check that the link leads to the translate page.
121     $this->drupalLogin($this->translator);
122     $translate_link = 'node/' . $node->id() . '/translations';
123     $this->drupalGet($translate_link);
124     $this->assertRaw(t('Translations of %label', ['%label' => $node->label()]), 'The contextual link leads to the translate page.');
125   }
126
127 }