ContentTranslationContextualLinksTest.php 5.34 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\content_translation\Tests\ContentTranslationContextualLinksTest.
 */

namespace Drupal\content_translation\Tests;

10
use Drupal\Component\Serialization\Json;
11
use Drupal\language\Entity\ConfigurableLanguage;
12 13 14 15 16
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\WebTestBase;

/**
 * Tests that contextual links are available for content translation.
17 18
 *
 * @group content_translation
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 */
class ContentTranslationContextualLinksTest extends WebTestBase {

  /**
   * The bundle being tested.
   *
   * @var string
   */
  protected $bundle;

  /**
   * The content type being tested.
   *
   * @var NodeType
   */
  protected $contentType;

  /**
   * The 'translator' user to use during testing.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $translator;

43 44 45 46 47 48 49
  /**
   * The enabled languages.
   *
   * @var array
   */
  protected $langcodes;

50 51 52 53 54 55 56 57 58 59 60 61 62 63
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('content_translation', 'contextual', 'node');

  /**
   * The profile to install as a basis for testing.
   *
   * @var string
   */
  protected $profile = 'testing';

64
  protected function setUp() {
65
    parent::setUp();
66 67
    // Set up an additional language.
    $this->langcodes = array(language_default()->id, 'es');
68
    ConfigurableLanguage::createFromLangcode('es')->save();
69 70

    // Create a content type.
71
    $this->bundle = $this->randomMachineName();
72 73
    $this->contentType = $this->drupalCreateContentType(array('type' => $this->bundle));

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    // Enable translation for the current entity type and ensure the change is
    // picked up.
    content_translation_set_config('node', $this->bundle, 'enabled', TRUE);
    drupal_static_reset();
    \Drupal::entityManager()->clearCachedBundles();
    \Drupal::service('router.builder')->rebuild();

    // Add a translatable field to the content type.
    entity_create('field_storage_config', array(
      'name' => 'field_test_text',
      'entity_type' => 'node',
      'type' => 'text',
      'cardinality' => 1,
      'translatable' => TRUE,
    ))->save();
    entity_create('field_instance_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();

    // Enable content translation.
    $configuration = array(
      'langcode' => language_default()->id,
      'language_show' => TRUE,
    );
    language_save_default_configuration('node', $this->bundle, $configuration);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    // Create a translator user.
    $permissions = array(
      'access contextual links',
      'administer nodes',
      "edit any $this->bundle content",
      'translate any entity',
    );
    $this->translator = $this->drupalCreateUser($permissions);
  }

  /**
   * Tests that a contextual link is available for translating a node.
   */
  public function testContentTranslationContextualLinks() {
    // Create a node.
    $title = $this->randomString();
124
    $this->drupalCreateNode(array('type' => $this->bundle, 'title' => $title, 'langcode' => 'en'));
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    $node = $this->drupalGetNodeByTitle($title);

    // Check that the translate link appears on the node page.
    $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->drupalSetContent($json['node:node=1:']);
    $this->assertLinkByHref($translate_link, 0, 'The contextual link to translate the node is shown.');

    // Check that the link leads to the translate page.
    $this->drupalGet($translate_link);
    $this->assertRaw(t('Translations of %label', array('%label' => $node->label())), 'The contextual link leads to the translate page.');
  }

  /**
   * Get server-rendered contextual links for the given contextual link ids.
   *
   * Copied from \Drupal\contextual\Tests\ContextualDynamicContextTest::renderContextualLinks().
   *
   * @param array $ids
   *   An array of contextual link ids.
   * @param string $current_path
   *   The Drupal path for the page for which the contextual links are rendered.
   *
   * @return string
   *   The response body.
   */
  protected function renderContextualLinks($ids, $current_path) {
    // Build POST values.
    $post = array();
    for ($i = 0; $i < count($ids); $i++) {
      $post['ids[' . $i . ']'] = $ids[$i];
    }

    // Serialize POST values.
    foreach ($post as $key => $value) {
      // Encode according to application/x-www-form-urlencoded
      // Both names and values needs to be urlencoded, according to
      // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
      $post[$key] = urlencode($key) . '=' . urlencode($value);
    }
    $post = implode('&', $post);

    // Perform HTTP request.
    return $this->curlExec(array(
      CURLOPT_URL => url('contextual/render', 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',
      ),
    ));
  }

}