3 namespace Drupal\content_translation\Tests;
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;
12 * Tests that contextual links are available for content translation.
14 * @group content_translation
16 class ContentTranslationContextualLinksTest extends WebTestBase {
19 * The bundle being tested.
26 * The content type being tested.
28 * @var \Drupal\node\Entity\NodeType
30 protected $contentType;
33 * The 'translator' user to use during testing.
35 * @var \Drupal\user\UserInterface
37 protected $translator;
40 * The enabled languages.
51 public static $modules = ['content_translation', 'contextual', 'node'];
54 * The profile to install as a basis for testing.
58 protected $profile = 'testing';
60 protected function setUp() {
62 // Set up an additional language.
63 $this->langcodes = [\Drupal::languageManager()->getDefaultLanguage()->getId(), 'es'];
64 ConfigurableLanguage::createFromLangcode('es')->save();
66 // Create a content type.
67 $this->bundle = $this->randomMachineName();
68 $this->contentType = $this->drupalCreateContentType(['type' => $this->bundle]);
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',
78 'entity_type' => 'node',
79 'field_name' => 'field_test_text',
80 'bundle' => $this->bundle,
81 'label' => 'Test text-field',
83 entity_get_form_display('node', $this->bundle, 'default')
84 ->setComponent('field_test_text', [
85 'type' => 'text_textfield',
90 // Create a translator user.
92 'access contextual links',
94 "edit any $this->bundle content",
95 'translate any entity',
97 $this->translator = $this->drupalCreateUser($permissions);
101 * Tests that a contextual link is available for translating a node.
103 public function testContentTranslationContextualLinks() {
105 $title = $this->randomString();
106 $this->drupalCreateNode(['type' => $this->bundle, 'title' => $title, 'langcode' => 'en']);
107 $node = $this->drupalGetNodeByTitle($title);
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);
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,
118 $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
119 $this->drupalLogout();
121 // Check that the translate link appears on the node page.
122 $this->drupalLogin($this->translator);
123 $translate_link = 'node/' . $node->id() . '/translations';
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.');
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.');
137 * Get server-rendered contextual links for the given contextual link ids.
139 * Copied from \Drupal\contextual\Tests\ContextualDynamicContextTest::renderContextualLinks().
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.
149 protected function renderContextualLinks($ids, $current_path) {
150 // Build POST values.
152 for ($i = 0; $i < count($ids); $i++) {
153 $post['ids[' . $i . ']'] = $ids[$i];
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);
163 $post = implode('&', $post);
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',