View source
<?php
namespace Drupal\content_translation\Tests;
use Drupal\Component\Serialization\Json;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\WebTestBase;
class ContentTranslationContextualLinksTest extends WebTestBase {
protected $bundle;
protected $contentType;
protected $translator;
protected $langcodes;
public static $modules = array(
'content_translation',
'contextual',
'node',
);
protected $profile = 'testing';
protected function setUp() {
parent::setUp();
$this->langcodes = array(
\Drupal::languageManager()
->getDefaultLanguage()
->getId(),
'es',
);
ConfigurableLanguage::createFromLangcode('es')
->save();
$this->bundle = $this
->randomMachineName();
$this->contentType = $this
->drupalCreateContentType(array(
'type' => $this->bundle,
));
entity_create('field_storage_config', array(
'field_name' => 'field_test_text',
'entity_type' => 'node',
'type' => 'text',
'cardinality' => 1,
))
->save();
entity_create('field_config', array(
'entity_type' => 'node',
'field_name' => 'field_test_text',
'bundle' => $this->bundle,
'label' => 'Test text-field',
))
->save();
entity_get_form_display('node', $this->bundle, 'default')
->setComponent('field_test_text', array(
'type' => 'text_textfield',
'weight' => 0,
))
->save();
$permissions = array(
'access contextual links',
'administer nodes',
"edit any {$this->bundle} content",
'translate any entity',
);
$this->translator = $this
->drupalCreateUser($permissions);
}
public function testContentTranslationContextualLinks() {
$title = $this
->randomString();
$this
->drupalCreateNode(array(
'type' => $this->bundle,
'title' => $title,
'langcode' => 'en',
));
$node = $this
->drupalGetNodeByTitle($title);
$this
->drupalLogin($this->rootUser);
$edit = array(
'entity_types[node]' => TRUE,
'settings[node][' . $this->bundle . '][settings][language][language_alterable]' => TRUE,
'settings[node][' . $this->bundle . '][translatable]' => TRUE,
'settings[node][' . $this->bundle . '][fields][field_test_text]' => TRUE,
);
$this
->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
$this
->drupalLogout();
$this
->drupalLogin($this->translator);
$translate_link = 'node/' . $node
->id() . '/translations';
$response = $this
->renderContextualLinks(array(
'node:node=1:',
), 'node/' . $node
->id());
$this
->assertResponse(200);
$json = Json::decode($response);
$this
->setRawContent($json['node:node=1:']);
$this
->assertLinkByHref($translate_link, 0, 'The contextual link to translate the node is shown.');
$this
->drupalGet($translate_link);
$this
->assertRaw(t('Translations of %label', array(
'%label' => $node
->label(),
)), 'The contextual link leads to the translate page.');
}
protected function renderContextualLinks($ids, $current_path) {
$post = array();
for ($i = 0; $i < count($ids); $i++) {
$post['ids[' . $i . ']'] = $ids[$i];
}
foreach ($post as $key => $value) {
$post[$key] = urlencode($key) . '=' . urlencode($value);
}
$post = implode('&', $post);
return $this
->curlExec(array(
CURLOPT_URL => \Drupal::url('contextual.render', array(), array(
'absolute' => TRUE,
'query' => array(
'destination' => $current_path,
),
)),
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $post,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
),
));
}
}