EntityTranslationUITest.php 11.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

/**
 * @file
 * Definition of Drupal\entity\Tests\EntityTranslationUITest.
 */

namespace Drupal\translation_entity\Tests;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityNG;
12
use Drupal\Core\Language\Language;
13
14
15
16
17
use Drupal\Core\TypedData\ComplexDataInterface;

/**
 * Tests the Entity Translation UI.
 */
18
abstract class EntityTranslationUITest extends EntityTranslationTestBase {
19

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

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

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

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

    $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));
61
      $this->assertEqual($stored_value, $value, $message);
62
63
64
65
66
67
    }

    // Add an entity translation.
    $langcode = 'it';
    $values[$langcode] = $this->getNewEntityValues($langcode);

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

    // 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;
    $this->drupalPost($path, $edit, t('Change'));
    $this->assertFieldByXPath("//input[@name=\"{$this->fieldName}[fr][0][value]\"]", $values[$source_langcode][$this->fieldName][0]['value'], 'Source language correctly switched.');

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

    // 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);
      }
    }
100
101
102
103
104
105
106
107
108
109
110
111
  }

  /**
   * 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.
    $edit = array('translation_entity[retranslate]' => TRUE);
112
    $this->drupalPost($langcode . '/' . $this->controller->getEditPath($entity), $edit, $this->getFormSubmitAction($entity));
113
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
114
115
116
117

    // Check that every translation has the correct "outdated" status.
    foreach ($this->langcodes as $enabled_langcode) {
      $prefix = $enabled_langcode != $default_langcode ? $enabled_langcode . '/' : '';
118
      $path = $prefix . $this->controller->getEditPath($entity);
119
120
      $this->drupalGet($path);
      if ($enabled_langcode == $langcode) {
121
        $this->assertFieldByXPath('//input[@name="translation_entity[retranslate]"]', FALSE, 'The retranslate flag is not checked by default.');
122
123
      }
      else {
124
125
        $this->assertFieldByXPath('//input[@name="translation_entity[outdated]"]', TRUE, 'The translate flag is checked by default.');
        $edit = array('translation_entity[outdated]' => FALSE);
126
        $this->drupalPost($path, $edit, $this->getFormSubmitAction($entity));
127
        $this->drupalGet($path);
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
        $this->assertFieldByXPath('//input[@name="translation_entity[retranslate]"]', FALSE, 'The retranslate flag is now shown.');
        $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) {
        $edit = array('translation_entity[status]' => FALSE);
146
        $this->drupalPost($langcode . '/' . $path, $edit, $this->getFormSubmitAction($entity));
147
148
        $entity = entity_load($this->entityType, $this->entityId, TRUE);
        $this->assertFalse($entity->translation[$langcode]['status'], 'The translation has been correctly unpublished.');
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
    // Check that the last published translation cannot be unpublished.
    $this->drupalGet($path);
    $this->assertFieldByXPath('//input[@name="translation_entity[status]" and @disabled="disabled"]', TRUE, 'The last translation is published and cannot be unpublished.');
  }

  /**
   * 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(
        'uid' => $user->uid,
        'created' => REQUEST_TIME - mt_rand(0, 1000),
      );
      $edit = array(
        'translation_entity[name]' => $user->name,
        'translation_entity[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
      );
      $prefix = $index > 0 ? $langcode . '/' : '';
177
      $this->drupalPost($prefix . $path, $edit, $this->getFormSubmitAction($entity));
178
179
180
181
182
183
184
185
186
187
188
189
190
    }

    $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.
      'translation_entity[name]' => $this->randomName(12),
191
      'translation_entity[created]' => '19/11/1978',
192
    );
193
    $this->drupalPost($path, $edit, $this->getFormSubmitAction($entity));
194
195
196
197
198
199
200
201
202
    $this->assertTrue($this->xpath('//div[@id="messages"]//div[contains(@class, "error")]//ul'), 'Invalid values generate a list of form errors.');
    $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() {
203
    // Confirm and delete a translation.
204
205
206
    $langcode = 'fr';
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
    $this->drupalPost($langcode . '/' . $this->controller->getEditPath($entity), array(), t('Delete translation'));
207
    $this->drupalPost(NULL, array(), t('Delete'));
208
    $entity = entity_load($this->entityType, $this->entityId, TRUE);
209
210
    if ($this->assertTrue(is_object($entity), 'Entity found')) {
      $translations = $entity->getTranslationLanguages();
211
      $this->assertTrue(count($translations) == 2 && empty($translations[$langcode]), 'Translation successfully deleted.');
212
    }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  }

  /**
   * 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];
227
    $langcode = $new ? Language::LANGCODE_NOT_SPECIFIED : $langcode;
228
229
230
231
232
233
234
235
236
    foreach ($values[$langcode] as $property => $value) {
      if (is_array($value)) {
        $edit["{$property}[$langcode][0][value]"] = $value[0]['value'];
        unset($edit[$property]);
      }
    }
    return $edit;
  }

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

250
251
252
  /**
   * Returns the translation object to use to retrieve the translated values.
   *
253
   * @param \Drupal\Core\Entity\EntityInterface $entity
254
255
256
257
258
259
260
261
   *   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) {
262
    // @todo remove once EntityBCDecorator is gone.
263
    $entity = $entity->getNGEntity();
264
    return $entity instanceof EntityNG ? $entity->getTranslation($langcode, FALSE) : $entity;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  }

  /**
   * Returns the value for the specified property in the given language.
   *
   * @param \Drupal\Core\TypedData\TranslatableInterface $translation
   *   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.
   */
  protected function getValue(ComplexDataInterface $translation, $property, $langcode) {
281
    $key = $property == 'user_id' ? 'target_id' : 'value';
282
283
    // @todo remove EntityBCDecorator condition once EntityBCDecorator is gone.
    if (($translation instanceof EntityInterface) && !($translation instanceof EntityNG) && !($translation instanceof EntityBCDecorator)) {
284
      return is_array($translation->$property) ? $translation->{$property}[$langcode][0][$key] : $translation->$property;
285
286
    }
    else {
287
      return $translation->get($property)->{$key};
288
289
290
291
    }
  }

}