db backup prior to drupal security update
[yaffs-website] / web / core / modules / content_translation / src / Tests / ContentTranslationContextualLinksTest.php
1 <?php
2
3 namespace Drupal\content_translation\Tests;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
10
11 /**
12  * Tests that contextual links are available for content translation.
13  *
14  * @group content_translation
15  */
16 class ContentTranslationContextualLinksTest extends WebTestBase {
17
18   /**
19    * The bundle being tested.
20    *
21    * @var string
22    */
23   protected $bundle;
24
25   /**
26    * The content type being tested.
27    *
28    * @var \Drupal\node\Entity\NodeType
29    */
30   protected $contentType;
31
32   /**
33    * The 'translator' user to use during testing.
34    *
35    * @var \Drupal\user\UserInterface
36    */
37   protected $translator;
38
39   /**
40    * The enabled languages.
41    *
42    * @var array
43    */
44   protected $langcodes;
45
46   /**
47    * Modules to enable.
48    *
49    * @var array
50    */
51   public static $modules = ['content_translation', 'contextual', 'node'];
52
53   /**
54    * The profile to install as a basis for testing.
55    *
56    * @var string
57    */
58   protected $profile = 'testing';
59
60   protected function setUp() {
61     parent::setUp();
62     // Set up an additional language.
63     $this->langcodes = [\Drupal::languageManager()->getDefaultLanguage()->getId(), 'es'];
64     ConfigurableLanguage::createFromLangcode('es')->save();
65
66     // Create a content type.
67     $this->bundle = $this->randomMachineName();
68     $this->contentType = $this->drupalCreateContentType(['type' => $this->bundle]);
69
70     // Add a field to the content type. The field is not yet translatable.
71     FieldStorageConfig::create([
72       'field_name' => 'field_test_text',
73       'entity_type' => 'node',
74       'type' => 'text',
75       'cardinality' => 1,
76     ])->save();
77     FieldConfig::create([
78       'entity_type' => 'node',
79       'field_name' => 'field_test_text',
80       'bundle' => $this->bundle,
81       'label' => 'Test text-field',
82     ])->save();
83     entity_get_form_display('node', $this->bundle, 'default')
84       ->setComponent('field_test_text', [
85         'type' => 'text_textfield',
86         'weight' => 0,
87       ])
88       ->save();
89
90     // Create a translator user.
91     $permissions = [
92       'access contextual links',
93       'administer nodes',
94       "edit any $this->bundle content",
95       'translate any entity',
96     ];
97     $this->translator = $this->drupalCreateUser($permissions);
98   }
99
100   /**
101    * Tests that a contextual link is available for translating a node.
102    */
103   public function testContentTranslationContextualLinks() {
104     // Create a node.
105     $title = $this->randomString();
106     $this->drupalCreateNode(['type' => $this->bundle, 'title' => $title, 'langcode' => 'en']);
107     $node = $this->drupalGetNodeByTitle($title);
108
109     // Use a UI form submission to make the node type and field translatable.
110     // This tests that caches are properly invalidated.
111     $this->drupalLogin($this->rootUser);
112     $edit = [
113       'entity_types[node]' => TRUE,
114       'settings[node][' . $this->bundle . '][settings][language][language_alterable]' => TRUE,
115       'settings[node][' . $this->bundle . '][translatable]' => TRUE,
116       'settings[node][' . $this->bundle . '][fields][field_test_text]' => TRUE,
117     ];
118     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
119     $this->drupalLogout();
120
121     // Check that the translate link appears on the node page.
122     $this->drupalLogin($this->translator);
123     $translate_link = 'node/' . $node->id() . '/translations';
124
125     $response = $this->renderContextualLinks(['node:node=1:'], 'node/' . $node->id());
126     $this->assertResponse(200);
127     $json = Json::decode($response);
128     $this->setRawContent($json['node:node=1:']);
129     $this->assertLinkByHref($translate_link, 0, 'The contextual link to translate the node is shown.');
130
131     // Check that the link leads to the translate page.
132     $this->drupalGet($translate_link);
133     $this->assertRaw(t('Translations of %label', ['%label' => $node->label()]), 'The contextual link leads to the translate page.');
134   }
135
136   /**
137    * Get server-rendered contextual links for the given contextual link ids.
138    *
139    * Copied from \Drupal\contextual\Tests\ContextualDynamicContextTest::renderContextualLinks().
140    *
141    * @param array $ids
142    *   An array of contextual link ids.
143    * @param string $current_path
144    *   The Drupal path for the page for which the contextual links are rendered.
145    *
146    * @return string
147    *   The response body.
148    */
149   protected function renderContextualLinks($ids, $current_path) {
150     // Build POST values.
151     $post = [];
152     for ($i = 0; $i < count($ids); $i++) {
153       $post['ids[' . $i . ']'] = $ids[$i];
154     }
155
156     // Serialize POST values.
157     foreach ($post as $key => $value) {
158       // Encode according to application/x-www-form-urlencoded
159       // Both names and values needs to be urlencoded, according to
160       // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
161       $post[$key] = urlencode($key) . '=' . urlencode($value);
162     }
163     $post = implode('&', $post);
164
165     // Perform HTTP request.
166     return $this->curlExec([
167       CURLOPT_URL => \Drupal::url('contextual.render', [], ['absolute' => TRUE, 'query' => ['destination' => $current_path]]),
168       CURLOPT_POST => TRUE,
169       CURLOPT_POSTFIELDS => $post,
170       CURLOPT_HTTPHEADER => [
171         'Accept: application/json',
172         'Content-Type: application/x-www-form-urlencoded',
173       ],
174     ]);
175   }
176
177 }