PageRenderTime 84ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php

https://gitlab.com/reasonat/test8
PHP | 576 lines | 344 code | 64 blank | 168 comment | 23 complexity | 62dd779c162d4fb52bba0a1d5f12e3e5 MD5 | raw file
  1. <?php
  2. namespace Drupal\content_translation\Tests;
  3. use Drupal\Core\Cache\Cache;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Language\Language;
  6. use Drupal\Core\Language\LanguageInterface;
  7. use Drupal\Core\Url;
  8. use Drupal\language\Entity\ConfigurableLanguage;
  9. use Drupal\Component\Utility\SafeMarkup;
  10. use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
  11. /**
  12. * Tests the Content Translation UI.
  13. */
  14. abstract class ContentTranslationUITestBase extends ContentTranslationTestBase {
  15. use AssertPageCacheContextsAndTagsTrait;
  16. /**
  17. * The id of the entity being translated.
  18. *
  19. * @var mixed
  20. */
  21. protected $entityId;
  22. /**
  23. * Whether the behavior of the language selector should be tested.
  24. *
  25. * @var bool
  26. */
  27. protected $testLanguageSelector = TRUE;
  28. /**
  29. * Flag that tells whether the HTML escaping of all languages works or not
  30. * after SafeMarkup change.
  31. *
  32. * @var bool
  33. */
  34. protected $testHTMLEscapeForAllLanguages = FALSE;
  35. /**
  36. * Default cache contexts expected on a non-translated entity.
  37. *
  38. * Cache contexts will not be checked if this list is empty.
  39. *
  40. * @var string[]
  41. */
  42. protected $defaultCacheContexts = ['languages:language_interface', 'theme', 'url.query_args:_wrapper_format', 'user.permissions'];
  43. /**
  44. * Tests the basic translation UI.
  45. */
  46. function testTranslationUI() {
  47. $this->doTestBasicTranslation();
  48. $this->doTestTranslationOverview();
  49. $this->doTestOutdatedStatus();
  50. $this->doTestPublishedStatus();
  51. $this->doTestAuthoringInfo();
  52. $this->doTestTranslationEdit();
  53. $this->doTestTranslationChanged();
  54. $this->doTestChangedTimeAfterSaveWithoutChanges();
  55. $this->doTestTranslationDeletion();
  56. }
  57. /**
  58. * Tests the basic translation workflow.
  59. */
  60. protected function doTestBasicTranslation() {
  61. // Create a new test entity with original values in the default language.
  62. $default_langcode = $this->langcodes[0];
  63. $values[$default_langcode] = $this->getNewEntityValues($default_langcode);
  64. // Create the entity with the editor as owner, so that afterwards a new
  65. // translation is created by the translator and the translation author is
  66. // tested.
  67. $this->drupalLogin($this->editor);
  68. $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode);
  69. $this->drupalLogin($this->translator);
  70. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  71. $this->assertTrue($entity, 'Entity found in the database.');
  72. $this->drupalGet($entity->urlInfo());
  73. $this->assertResponse(200, 'Entity URL is valid.');
  74. // Ensure that the content language cache context is not yet added to the
  75. // page.
  76. $this->assertCacheContexts($this->defaultCacheContexts);
  77. $this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
  78. $this->assertNoText('Source language', 'Source language column correctly hidden.');
  79. $translation = $this->getTranslation($entity, $default_langcode);
  80. foreach ($values[$default_langcode] as $property => $value) {
  81. $stored_value = $this->getValue($translation, $property, $default_langcode);
  82. $value = is_array($value) ? $value[0]['value'] : $value;
  83. $message = format_string('@property correctly stored in the default language.', array('@property' => $property));
  84. $this->assertEqual($stored_value, $value, $message);
  85. }
  86. // Add a content translation.
  87. $langcode = 'it';
  88. $language = ConfigurableLanguage::load($langcode);
  89. $values[$langcode] = $this->getNewEntityValues($langcode);
  90. $entity_type_id = $entity->getEntityTypeId();
  91. $add_url = Url::fromRoute("entity.$entity_type_id.content_translation_add", [
  92. $entity->getEntityTypeId() => $entity->id(),
  93. 'source' => $default_langcode,
  94. 'target' => $langcode
  95. ], array('language' => $language));
  96. $this->drupalPostForm($add_url, $this->getEditValues($values, $langcode), $this->getFormSubmitActionForNewTranslation($entity, $langcode));
  97. // Assert that HTML is escaped in "all languages" in UI after SafeMarkup
  98. // change.
  99. if ($this->testHTMLEscapeForAllLanguages) {
  100. $this->assertNoRaw('&lt;span class=&quot;translation-entity-all-languages&quot;&gt;(all languages)&lt;/span&gt;');
  101. $this->assertRaw('<span class="translation-entity-all-languages">(all languages)</span>');
  102. }
  103. // Ensure that the content language cache context is not yet added to the
  104. // page.
  105. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  106. $this->drupalGet($entity->urlInfo());
  107. $this->assertCacheContexts(Cache::mergeContexts(['languages:language_content'], $this->defaultCacheContexts));
  108. // Reset the cache of the entity, so that the new translation gets the
  109. // updated values.
  110. $metadata_source_translation = $this->manager->getTranslationMetadata($entity->getTranslation($default_langcode));
  111. $metadata_target_translation = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
  112. $author_field_name = $entity->hasField('content_translation_uid') ? 'content_translation_uid' : 'uid';
  113. if ($entity->getFieldDefinition($author_field_name)->isTranslatable()) {
  114. $this->assertEqual($metadata_target_translation->getAuthor()->id(), $this->translator->id(),
  115. SafeMarkup::format('Author of the target translation @langcode correctly stored for translatable owner field.', array('@langcode' => $langcode)));
  116. $this->assertNotEqual($metadata_target_translation->getAuthor()->id(), $metadata_source_translation->getAuthor()->id(),
  117. SafeMarkup::format('Author of the target translation @target different from the author of the source translation @source for translatable owner field.',
  118. array('@target' => $langcode, '@source' => $default_langcode)));
  119. }
  120. else {
  121. $this->assertEqual($metadata_target_translation->getAuthor()->id(), $this->editor->id(), 'Author of the entity remained untouched after translation for non translatable owner field.');
  122. }
  123. $created_field_name = $entity->hasField('content_translation_created') ? 'content_translation_created' : 'created';
  124. if ($entity->getFieldDefinition($created_field_name)->isTranslatable()) {
  125. $this->assertTrue($metadata_target_translation->getCreatedTime() > $metadata_source_translation->getCreatedTime(),
  126. SafeMarkup::format('Translation creation timestamp of the target translation @target is newer than the creation timestamp of the source translation @source for translatable created field.',
  127. array('@target' => $langcode, '@source' => $default_langcode)));
  128. }
  129. else {
  130. $this->assertEqual($metadata_target_translation->getCreatedTime(), $metadata_source_translation->getCreatedTime(), 'Creation timestamp of the entity remained untouched after translation for non translatable created field.');
  131. }
  132. if ($this->testLanguageSelector) {
  133. $this->assertNoFieldByXPath('//select[@id="edit-langcode-0-value"]', NULL, 'Language selector correctly disabled on translations.');
  134. }
  135. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  136. $this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
  137. $this->assertNoText('Source language', 'Source language column correctly hidden.');
  138. // Switch the source language.
  139. $langcode = 'fr';
  140. $language = ConfigurableLanguage::load($langcode);
  141. $source_langcode = 'it';
  142. $edit = array('source_langcode[source]' => $source_langcode);
  143. $entity_type_id = $entity->getEntityTypeId();
  144. $add_url = Url::fromRoute("entity.$entity_type_id.content_translation_add", [
  145. $entity->getEntityTypeId() => $entity->id(),
  146. 'source' => $default_langcode,
  147. 'target' => $langcode
  148. ], array('language' => $language));
  149. // This does not save anything, it merely reloads the form and fills in the
  150. // fields with the values from the different source language.
  151. $this->drupalPostForm($add_url, $edit, t('Change'));
  152. $this->assertFieldByXPath("//input[@name=\"{$this->fieldName}[0][value]\"]", $values[$source_langcode][$this->fieldName][0]['value'], 'Source language correctly switched.');
  153. // Add another translation and mark the other ones as outdated.
  154. $values[$langcode] = $this->getNewEntityValues($langcode);
  155. $edit = $this->getEditValues($values, $langcode) + array('content_translation[retranslate]' => TRUE);
  156. $entity_type_id = $entity->getEntityTypeId();
  157. $add_url = Url::fromRoute("entity.$entity_type_id.content_translation_add", [
  158. $entity->getEntityTypeId() => $entity->id(),
  159. 'source' => $source_langcode,
  160. 'target' => $langcode
  161. ], array('language' => $language));
  162. $this->drupalPostForm($add_url, $edit, $this->getFormSubmitActionForNewTranslation($entity, $langcode));
  163. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  164. $this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
  165. $this->assertText('Source language', 'Source language column correctly shown.');
  166. // Check that the entered values have been correctly stored.
  167. foreach ($values as $langcode => $property_values) {
  168. $translation = $this->getTranslation($entity, $langcode);
  169. foreach ($property_values as $property => $value) {
  170. $stored_value = $this->getValue($translation, $property, $langcode);
  171. $value = is_array($value) ? $value[0]['value'] : $value;
  172. $message = format_string('%property correctly stored with language %language.', array('%property' => $property, '%language' => $langcode));
  173. $this->assertEqual($stored_value, $value, $message);
  174. }
  175. }
  176. }
  177. /**
  178. * Tests that the translation overview shows the correct values.
  179. */
  180. protected function doTestTranslationOverview() {
  181. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  182. $translate_url = $entity->urlInfo('drupal:content-translation-overview');
  183. $this->drupalGet($translate_url);
  184. $translate_url->setAbsolute(FALSE);
  185. foreach ($this->langcodes as $langcode) {
  186. if ($entity->hasTranslation($langcode)) {
  187. $language = new Language(array('id' => $langcode));
  188. $view_url = $entity->url('canonical', ['language' => $language]);
  189. $elements = $this->xpath('//table//a[@href=:href]', [':href' => $view_url]);
  190. $this->assertEqual((string) $elements[0], $entity->getTranslation($langcode)->label(), format_string('Label correctly shown for %language translation.', array('%language' => $langcode)));
  191. $edit_path = $entity->url('edit-form', array('language' => $language));
  192. $elements = $this->xpath('//table//ul[@class="dropbutton"]/li/a[@href=:href]', array(':href' => $edit_path));
  193. $this->assertEqual((string) $elements[0], t('Edit'), format_string('Edit link correct for %language translation.', array('%language' => $langcode)));
  194. }
  195. }
  196. }
  197. /**
  198. * Tests up-to-date status tracking.
  199. */
  200. protected function doTestOutdatedStatus() {
  201. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  202. $langcode = 'fr';
  203. $languages = \Drupal::languageManager()->getLanguages();
  204. // Mark translations as outdated.
  205. $edit = array('content_translation[retranslate]' => TRUE);
  206. $edit_path = $entity->urlInfo('edit-form', array('language' => $languages[$langcode]));
  207. $this->drupalPostForm($edit_path, $edit, $this->getFormSubmitAction($entity, $langcode));
  208. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  209. // Check that every translation has the correct "outdated" status, and that
  210. // the Translation fieldset is open if the translation is "outdated".
  211. foreach ($this->langcodes as $added_langcode) {
  212. $url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($added_langcode)));
  213. $this->drupalGet($url);
  214. if ($added_langcode == $langcode) {
  215. $this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is not checked by default.');
  216. $this->assertFalse($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab should be collapsed by default.');
  217. }
  218. else {
  219. $this->assertFieldByXPath('//input[@name="content_translation[outdated]"]', TRUE, 'The translate flag is checked by default.');
  220. $this->assertTrue($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab is correctly expanded when the translation is outdated.');
  221. $edit = array('content_translation[outdated]' => FALSE);
  222. $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $added_langcode));
  223. $this->drupalGet($url);
  224. $this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is now shown.');
  225. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  226. $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($added_langcode))->isOutdated(), 'The "outdated" status has been correctly stored.');
  227. }
  228. }
  229. }
  230. /**
  231. * Tests the translation publishing status.
  232. */
  233. protected function doTestPublishedStatus() {
  234. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  235. // Unpublish translations.
  236. foreach ($this->langcodes as $index => $langcode) {
  237. if ($index > 0) {
  238. $url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode)));
  239. $edit = array('content_translation[status]' => FALSE);
  240. $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
  241. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  242. $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.');
  243. }
  244. }
  245. // Check that the last published translation cannot be unpublished.
  246. $this->drupalGet($entity->urlInfo('edit-form'));
  247. $this->assertFieldByXPath('//input[@name="content_translation[status]" and @disabled="disabled"]', TRUE, 'The last translation is published and cannot be unpublished.');
  248. }
  249. /**
  250. * Tests the translation authoring information.
  251. */
  252. protected function doTestAuthoringInfo() {
  253. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  254. $values = array();
  255. // Post different authoring information for each translation.
  256. foreach ($this->langcodes as $index => $langcode) {
  257. $user = $this->drupalCreateUser();
  258. $values[$langcode] = array(
  259. 'uid' => $user->id(),
  260. 'created' => REQUEST_TIME - mt_rand(0, 1000),
  261. );
  262. $edit = array(
  263. 'content_translation[uid]' => $user->getUsername(),
  264. 'content_translation[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
  265. );
  266. $url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode)));
  267. $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
  268. }
  269. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  270. foreach ($this->langcodes as $langcode) {
  271. $metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
  272. $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
  273. $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
  274. }
  275. // Try to post non valid values and check that they are rejected.
  276. $langcode = 'en';
  277. $edit = array(
  278. // User names have by default length 8.
  279. 'content_translation[uid]' => $this->randomMachineName(12),
  280. 'content_translation[created]' => '19/11/1978',
  281. );
  282. $this->drupalPostForm($entity->urlInfo('edit-form'), $edit, $this->getFormSubmitAction($entity, $langcode));
  283. $this->assertTrue($this->xpath('//div[contains(@class, "error")]//ul'), 'Invalid values generate a list of form errors.');
  284. $metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
  285. $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly kept.');
  286. $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly kept.');
  287. }
  288. /**
  289. * Tests translation deletion.
  290. */
  291. protected function doTestTranslationDeletion() {
  292. // Confirm and delete a translation.
  293. $this->drupalLogin($this->translator);
  294. $langcode = 'fr';
  295. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  296. $language = ConfigurableLanguage::load($langcode);
  297. $url = $entity->urlInfo('edit-form', array('language' => $language));
  298. $this->drupalPostForm($url, array(), t('Delete translation'));
  299. $this->drupalPostForm(NULL, array(), t('Delete @language translation', array('@language' => $language->getName())));
  300. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  301. if ($this->assertTrue(is_object($entity), 'Entity found')) {
  302. $translations = $entity->getTranslationLanguages();
  303. $this->assertTrue(count($translations) == 2 && empty($translations[$langcode]), 'Translation successfully deleted.');
  304. }
  305. // Check that the translator cannot delete the original translation.
  306. $args = [$this->entityTypeId => $entity->id(), 'language' => 'en'];
  307. $this->drupalGet(Url::fromRoute("entity.$this->entityTypeId.content_translation_delete", $args));
  308. $this->assertResponse(403);
  309. }
  310. /**
  311. * Returns an array of entity field values to be tested.
  312. */
  313. protected function getNewEntityValues($langcode) {
  314. return array($this->fieldName => array(array('value' => $this->randomMachineName(16))));
  315. }
  316. /**
  317. * Returns an edit array containing the values to be posted.
  318. */
  319. protected function getEditValues($values, $langcode, $new = FALSE) {
  320. $edit = $values[$langcode];
  321. $langcode = $new ? LanguageInterface::LANGCODE_NOT_SPECIFIED : $langcode;
  322. foreach ($values[$langcode] as $property => $value) {
  323. if (is_array($value)) {
  324. $edit["{$property}[0][value]"] = $value[0]['value'];
  325. unset($edit[$property]);
  326. }
  327. }
  328. return $edit;
  329. }
  330. /**
  331. * Returns the form action value when submitting a new translation.
  332. *
  333. * @param \Drupal\Core\Entity\EntityInterface $entity
  334. * The entity being tested.
  335. * @param string $langcode
  336. * Language code for the form.
  337. *
  338. * @return string
  339. * Name of the button to hit.
  340. */
  341. protected function getFormSubmitActionForNewTranslation(EntityInterface $entity, $langcode) {
  342. $entity->addTranslation($langcode, $entity->toArray());
  343. return $this->getFormSubmitAction($entity, $langcode);
  344. }
  345. /**
  346. * Returns the form action value to be used to submit the entity form.
  347. *
  348. * @param \Drupal\Core\Entity\EntityInterface $entity
  349. * The entity being tested.
  350. * @param string $langcode
  351. * Language code for the form.
  352. *
  353. * @return string
  354. * Name of the button to hit.
  355. */
  356. protected function getFormSubmitAction(EntityInterface $entity, $langcode) {
  357. return t('Save') . $this->getFormSubmitSuffix($entity, $langcode);
  358. }
  359. /**
  360. * Returns appropriate submit button suffix based on translatability.
  361. *
  362. * @param \Drupal\Core\Entity\EntityInterface $entity
  363. * The entity being tested.
  364. * @param string $langcode
  365. * Language code for the form.
  366. *
  367. * @return string
  368. * Submit button suffix based on translatability.
  369. */
  370. protected function getFormSubmitSuffix(EntityInterface $entity, $langcode) {
  371. return '';
  372. }
  373. /**
  374. * Returns the translation object to use to retrieve the translated values.
  375. *
  376. * @param \Drupal\Core\Entity\EntityInterface $entity
  377. * The entity being tested.
  378. * @param string $langcode
  379. * The language code identifying the translation to be retrieved.
  380. *
  381. * @return \Drupal\Core\TypedData\TranslatableInterface
  382. * The translation object to act on.
  383. */
  384. protected function getTranslation(EntityInterface $entity, $langcode) {
  385. return $entity->getTranslation($langcode);
  386. }
  387. /**
  388. * Returns the value for the specified property in the given language.
  389. *
  390. * @param \Drupal\Core\Entity\EntityInterface $translation
  391. * The translation object the property value should be retrieved from.
  392. * @param string $property
  393. * The property name.
  394. * @param string $langcode
  395. * The property value.
  396. *
  397. * @return
  398. * The property value.
  399. */
  400. protected function getValue(EntityInterface $translation, $property, $langcode) {
  401. $key = $property == 'user_id' ? 'target_id' : 'value';
  402. return $translation->get($property)->{$key};
  403. }
  404. /**
  405. * Returns the name of the field that implements the changed timestamp.
  406. *
  407. * @param \Drupal\Core\Entity\EntityInterface $entity
  408. * The entity being tested.
  409. *
  410. * @return string
  411. * The field name.
  412. */
  413. protected function getChangedFieldName($entity) {
  414. return $entity->hasField('content_translation_changed') ? 'content_translation_changed' : 'changed';
  415. }
  416. /**
  417. * Tests edit content translation.
  418. */
  419. protected function doTestTranslationEdit() {
  420. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  421. $languages = $this->container->get('language_manager')->getLanguages();
  422. foreach ($this->langcodes as $langcode) {
  423. // We only want to test the title for non-english translations.
  424. if ($langcode != 'en') {
  425. $options = array('language' => $languages[$langcode]);
  426. $url = $entity->urlInfo('edit-form', $options);
  427. $this->drupalGet($url);
  428. $this->assertRaw($entity->getTranslation($langcode)->label());
  429. }
  430. }
  431. }
  432. /**
  433. * Tests the basic translation workflow.
  434. */
  435. protected function doTestTranslationChanged() {
  436. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  437. $changed_field_name = $this->getChangedFieldName($entity);
  438. $definition = $entity->getFieldDefinition($changed_field_name);
  439. $config = $definition->getConfig($entity->bundle());
  440. foreach ([FALSE, TRUE] as $translatable_changed_field) {
  441. if ($definition->isTranslatable()) {
  442. // For entities defining a translatable changed field we want to test
  443. // the correct behavior of that field even if the translatability is
  444. // revoked. In that case the changed timestamp should be synchronized
  445. // across all translations.
  446. $config->setTranslatable($translatable_changed_field);
  447. $config->save();
  448. }
  449. elseif ($translatable_changed_field) {
  450. // For entities defining a non-translatable changed field we cannot
  451. // declare the field as translatable on the fly by modifying its config
  452. // because the schema doesn't support this.
  453. break;
  454. }
  455. foreach ($entity->getTranslationLanguages() as $language) {
  456. // Ensure different timestamps.
  457. sleep(1);
  458. $langcode = $language->getId();
  459. $edit = array(
  460. $this->fieldName . '[0][value]' => $this->randomString(),
  461. );
  462. $edit_path = $entity->urlInfo('edit-form', array('language' => $language));
  463. $this->drupalPostForm($edit_path, $edit, $this->getFormSubmitAction($entity, $langcode));
  464. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  465. $this->assertEqual(
  466. $entity->getChangedTimeAcrossTranslations(), $entity->getTranslation($langcode)->getChangedTime(),
  467. format_string('Changed time for language %language is the latest change over all languages.', array('%language' => $language->getName()))
  468. );
  469. }
  470. $timestamps = array();
  471. foreach ($entity->getTranslationLanguages() as $language) {
  472. $next_timestamp = $entity->getTranslation($language->getId())->getChangedTime();
  473. if (!in_array($next_timestamp, $timestamps)) {
  474. $timestamps[] = $next_timestamp;
  475. }
  476. }
  477. if ($translatable_changed_field) {
  478. $this->assertEqual(
  479. count($timestamps), count($entity->getTranslationLanguages()),
  480. 'All timestamps from all languages are different.'
  481. );
  482. }
  483. else {
  484. $this->assertEqual(
  485. count($timestamps), 1,
  486. 'All timestamps from all languages are identical.'
  487. );
  488. }
  489. }
  490. }
  491. /**
  492. * Test the changed time after API and FORM save without changes.
  493. */
  494. public function doTestChangedTimeAfterSaveWithoutChanges() {
  495. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  496. // Test only entities, which implement the EntityChangedInterface.
  497. if ($entity->getEntityType()->isSubclassOf('Drupal\Core\Entity\EntityChangedInterface')) {
  498. $changed_timestamp = $entity->getChangedTime();
  499. $entity->save();
  500. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  501. $this->assertEqual($changed_timestamp, $entity->getChangedTime(), 'The entity\'s changed time wasn\'t updated after API save without changes.');
  502. // Ensure different save timestamps.
  503. sleep(1);
  504. // Save the entity on the regular edit form.
  505. $language = $entity->language();
  506. $edit_path = $entity->urlInfo('edit-form', array('language' => $language));
  507. $this->drupalPostForm($edit_path, [], $this->getFormSubmitAction($entity, $language->getId()));
  508. $entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
  509. $this->assertNotEqual($changed_timestamp, $entity->getChangedTime(), 'The entity\'s changed time was updated after form save without changes.');
  510. }
  511. }
  512. }