ContentTranslationUITest.php 11.3 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\entity\Tests\ContentTranslationUITest.
6 7
 */

8
namespace Drupal\content_translation\Tests;
9 10 11

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityNG;
12
use Drupal\Core\Language\Language;
13 14

/**
15
 * Tests the Content Translation UI.
16
 */
17
abstract class ContentTranslationUITest extends ContentTranslationTestBase {
18

19 20 21 22 23 24 25
  /**
   * The id of the entity being translated.
   *
   * @var mixed
   */
  protected $entityId;

26 27 28 29 30 31 32 33 34 35 36
  /**
   * Whether the behavior of the language selector should be tested.
   *
   * @var boolean
   */
  protected $testLanguageSelector = TRUE;

  /**
   * Tests the basic translation UI.
   */
  function testTranslationUI() {
37 38 39 40 41 42 43 44 45 46 47
    $this->assertBasicTranslation();
    $this->assertOutdatedStatus();
    $this->assertPublishedStatus();
    $this->assertAuthoringInfo();
    $this->assertTranslationDeletion();
  }

  /**
   * Tests the basic translation workflow.
   */
  protected function assertBasicTranslation() {
48 49 50
    // Create a new test entity with original values in the default language.
    $default_langcode = $this->langcodes[0];
    $values[$default_langcode] = $this->getNewEntityValues($default_langcode);
51 52
    $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode);
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
53
    $this->assertTrue($entity, 'Entity found in the database.');
54 55 56 57 58 59

    $translation = $this->getTranslation($entity, $default_langcode);
    foreach ($values[$default_langcode] as $property => $value) {
      $stored_value = $this->getValue($translation, $property, $default_langcode);
      $value = is_array($value) ? $value[0]['value'] : $value;
      $message = format_string('@property correctly stored in the default language.', array('@property' => $property));
60
      $this->assertEqual($stored_value, $value, $message);
61 62
    }

63
    // Add a content translation.
64 65 66
    $langcode = 'it';
    $values[$langcode] = $this->getNewEntityValues($langcode);

67
    $base_path = $this->controller->getBasePath($entity);
68
    $path = $langcode . '/' . $base_path . '/translations/add/' . $default_langcode . '/' . $langcode;
69
    $this->drupalPostForm($path, $this->getEditValues($values, $langcode), $this->getFormSubmitAction($entity));
70 71 72
    if ($this->testLanguageSelector) {
      $this->assertNoFieldByXPath('//select[@id="edit-langcode"]', NULL, 'Language selector correclty disabled on translations.');
    }
73
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
74 75 76 77 78 79

    // Switch the source language.
    $langcode = 'fr';
    $source_langcode = 'it';
    $edit = array('source_langcode[source]' => $source_langcode);
    $path = $langcode . '/' . $base_path . '/translations/add/' . $default_langcode . '/' . $langcode;
80
    $this->drupalPostForm($path, $edit, t('Change'));
81
    $this->assertFieldByXPath("//input[@name=\"{$this->fieldName}[0][value]\"]", $values[$source_langcode][$this->fieldName][0]['value'], 'Source language correctly switched.');
82 83 84

    // Add another translation and mark the other ones as outdated.
    $values[$langcode] = $this->getNewEntityValues($langcode);
85
    $edit = $this->getEditValues($values, $langcode) + array('content_translation[retranslate]' => TRUE);
86
    $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity));
87
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
88 89 90 91 92 93 94 95 96 97 98

    // Check that the entered values have been correctly stored.
    foreach ($values as $langcode => $property_values) {
      $translation = $this->getTranslation($entity, $langcode);
      foreach ($property_values as $property => $value) {
        $stored_value = $this->getValue($translation, $property, $langcode);
        $value = is_array($value) ? $value[0]['value'] : $value;
        $message = format_string('%property correctly stored with language %language.', array('%property' => $property, '%language' => $langcode));
        $this->assertEqual($stored_value, $value, $message);
      }
    }
99 100 101 102 103 104 105 106 107 108 109
  }

  /**
   * Tests up-to-date status tracking.
   */
  protected function assertOutdatedStatus() {
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
    $langcode = 'fr';
    $default_langcode = $this->langcodes[0];

    // Mark translations as outdated.
110
    $edit = array('content_translation[retranslate]' => TRUE);
111
    $this->drupalPostForm($langcode . '/' . $this->controller->getEditPath($entity), $edit, $this->getFormSubmitAction($entity));
112
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
113 114 115 116

    // Check that every translation has the correct "outdated" status.
    foreach ($this->langcodes as $enabled_langcode) {
      $prefix = $enabled_langcode != $default_langcode ? $enabled_langcode . '/' : '';
117
      $path = $prefix . $this->controller->getEditPath($entity);
118 119
      $this->drupalGet($path);
      if ($enabled_langcode == $langcode) {
120
        $this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is not checked by default.');
121 122
      }
      else {
123 124
        $this->assertFieldByXPath('//input[@name="content_translation[outdated]"]', TRUE, 'The translate flag is checked by default.');
        $edit = array('content_translation[outdated]' => FALSE);
125
        $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity));
126
        $this->drupalGet($path);
127
        $this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is now shown.');
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        $entity = entity_load($this->entityType, $this->entityId, TRUE);
        $this->assertFalse($entity->translation[$enabled_langcode]['outdated'], 'The "outdated" status has been correctly stored.');
      }
    }
  }

  /**
   * Tests the translation publishing status.
   */
  protected function assertPublishedStatus() {
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
    $path = $this->controller->getEditPath($entity);

    // Unpublish translations.
    foreach ($this->langcodes as $index => $langcode) {
      if ($index > 0) {
144
        $edit = array('content_translation[status]' => FALSE);
145
        $this->drupalPostForm($langcode . '/' . $path, $edit, $this->getFormSubmitAction($entity));
146 147
        $entity = entity_load($this->entityType, $this->entityId, TRUE);
        $this->assertFalse($entity->translation[$langcode]['status'], 'The translation has been correctly unpublished.');
148 149 150
      }
    }

151 152
    // Check that the last published translation cannot be unpublished.
    $this->drupalGet($path);
153
    $this->assertFieldByXPath('//input[@name="content_translation[status]" and @disabled="disabled"]', TRUE, 'The last translation is published and cannot be unpublished.');
154 155 156 157 158 159 160 161 162 163 164 165 166 167
  }

  /**
   * Tests the translation authoring information.
   */
  protected function assertAuthoringInfo() {
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
    $path = $this->controller->getEditPath($entity);
    $values = array();

    // Post different authoring information for each translation.
    foreach ($this->langcodes as $index => $langcode) {
      $user = $this->drupalCreateUser();
      $values[$langcode] = array(
168
        'uid' => $user->id(),
169 170 171
        'created' => REQUEST_TIME - mt_rand(0, 1000),
      );
      $edit = array(
172
        'content_translation[name]' => $user->getUsername(),
173
        'content_translation[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
174 175
      );
      $prefix = $index > 0 ? $langcode . '/' : '';
176
      $this->drupalPostForm($prefix . $path, $edit, $this->getFormSubmitAction($entity));
177 178 179 180 181 182 183 184 185 186 187 188
    }

    $entity = entity_load($this->entityType, $this->entityId, TRUE);
    foreach ($this->langcodes as $langcode) {
      $this->assertEqual($entity->translation[$langcode]['uid'] == $values[$langcode]['uid'], 'Translation author correctly stored.');
      $this->assertEqual($entity->translation[$langcode]['created'] == $values[$langcode]['created'], 'Translation date correctly stored.');
    }

    // Try to post non valid values and check that they are rejected.
    $langcode = 'en';
    $edit = array(
      // User names have by default length 8.
189 190
      'content_translation[name]' => $this->randomName(12),
      'content_translation[created]' => '19/11/1978',
191
    );
192
    $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity));
193
    $this->assertTrue($this->xpath('//div[contains(@class, "error")]//ul'), 'Invalid values generate a list of form errors.');
194 195 196 197 198 199 200 201
    $this->assertEqual($entity->translation[$langcode]['uid'] == $values[$langcode]['uid'], 'Translation author correctly kept.');
    $this->assertEqual($entity->translation[$langcode]['created'] == $values[$langcode]['created'], 'Translation date correctly kept.');
  }

  /**
   * Tests translation deletion.
   */
  protected function assertTranslationDeletion() {
202
    // Confirm and delete a translation.
203 204
    $langcode = 'fr';
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
205 206
    $this->drupalPostForm($langcode . '/' . $this->controller->getEditPath($entity), array(), t('Delete translation'));
    $this->drupalPostForm(NULL, array(), t('Delete'));
207
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
208 209
    if ($this->assertTrue(is_object($entity), 'Entity found')) {
      $translations = $entity->getTranslationLanguages();
210
      $this->assertTrue(count($translations) == 2 && empty($translations[$langcode]), 'Translation successfully deleted.');
211
    }
212 213 214 215 216 217 218 219 220 221 222 223 224 225
  }

  /**
   * Returns an array of entity field values to be tested.
   */
  protected function getNewEntityValues($langcode) {
    return array($this->fieldName => array(array('value' => $this->randomName(16))));
  }

  /**
   * Returns an edit array containing the values to be posted.
   */
  protected function getEditValues($values, $langcode, $new = FALSE) {
    $edit = $values[$langcode];
226
    $langcode = $new ? Language::LANGCODE_NOT_SPECIFIED : $langcode;
227 228
    foreach ($values[$langcode] as $property => $value) {
      if (is_array($value)) {
229
        $edit["{$property}[0][value]"] = $value[0]['value'];
230 231 232 233 234 235
        unset($edit[$property]);
      }
    }
    return $edit;
  }

236 237
  /**
   * Returns the form action value to be used to submit the entity form.
238 239 240 241 242 243
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity being tested.
   *
   * @return string
   *   Name of the button to hit.
244
   */
245
  protected function getFormSubmitAction(EntityInterface $entity) {
246 247 248
    return t('Save');
  }

249 250 251
  /**
   * Returns the translation object to use to retrieve the translated values.
   *
252
   * @param \Drupal\Core\Entity\EntityInterface $entity
253 254 255 256 257 258 259 260
   *   The entity being tested.
   * @param string $langcode
   *   The language code identifying the translation to be retrieved.
   *
   * @return \Drupal\Core\TypedData\TranslatableInterface
   *   The translation object to act on.
   */
  protected function getTranslation(EntityInterface $entity, $langcode) {
261
    return $entity->getTranslation($langcode);
262 263 264 265 266
  }

  /**
   * Returns the value for the specified property in the given language.
   *
267
   * @param \Drupal\Core\Entity\EntityInterface $translation
268 269 270 271 272 273 274 275 276
   *   The translation object the property value should be retrieved from.
   * @param string $property
   *   The property name.
   * @param string $langcode
   *   The property value.
   *
   * @return
   *   The property value.
   */
277
  protected function getValue(EntityInterface $translation, $property, $langcode) {
278
    $key = $property == 'user_id' ? 'target_id' : 'value';
279
    return $translation->get($property)->{$key};
280 281 282
  }

}