TitleTranslationTestCase.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * Tests for legacy field replacement.
  4. */
  5. class TitleTranslationTestCase extends DrupalWebTestCase {
  6. /**
  7. *
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Replaced fields translation',
  12. 'description' => 'Test replaced field translation.',
  13. 'group' => 'Title',
  14. 'dependencies' => array('entity_translation'),
  15. );
  16. }
  17. /**
  18. * Use the barebones "testing" installation profile.
  19. */
  20. protected $profile = 'testing';
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function setUp(array $modules = array()) {
  25. // Core.
  26. $modules[] = 'field_test';
  27. $modules[] = 'locale';
  28. $modules[] = 'taxonomy';
  29. // Other dependencies.
  30. $modules[] = 'entity_translation';
  31. // This module.
  32. $modules[] = 'title';
  33. $modules[] = 'title_test';
  34. parent::setUp($modules);
  35. // Create a power user.
  36. $permissions = array(
  37. 'administer modules',
  38. 'view the administration theme',
  39. 'administer languages',
  40. 'administer taxonomy',
  41. 'administer entity translation',
  42. 'translate any entity',
  43. );
  44. $admin_user = $this->drupalCreateUser($permissions);
  45. $this->drupalLogin($admin_user);
  46. // Enable a translation language.
  47. $edit = array('langcode' => 'it');
  48. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  49. $this->assertTrue(drupal_multilingual(), t('Italian language installed.'));
  50. // Enable URL language negotiation.
  51. $name = 'language_content[enabled][locale-url]';
  52. $edit = array($name => 1);
  53. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  54. $this->assertFieldByName($name, 1, t('URL language negotiation enabled.'));
  55. // Enable taxonomy translation.
  56. $name = 'entity_translation_entity_types[taxonomy_term]';
  57. $edit = array($name => 1);
  58. $this->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
  59. $this->assertFieldByName($name, 'taxonomy_term', t('Taxonomy translation enabled.'));
  60. // Create a new vocabulary.
  61. $name = drupal_strtolower($this->randomName());
  62. $edit = array(
  63. 'name' => $this->randomString(),
  64. 'machine_name' => $name,
  65. 'entity_translation_taxonomy' => 1,
  66. );
  67. $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
  68. $this->vocabulary = taxonomy_vocabulary_machine_name_load($name);
  69. $this->assertTrue($this->vocabulary, t('Vocabulary created.'));
  70. // Replace both taxonomy term legacy fields.
  71. $entity_type = 'taxonomy_term';
  72. foreach (title_field_replacement_info($entity_type) as $legacy_field => $info) {
  73. title_field_replacement_toggle($entity_type, $name, $legacy_field);
  74. $t_args = array('%legacy_field' => $legacy_field);
  75. $this->assertTrue(field_info_instance($entity_type, $info['field']['field_name'], $name), t('The %legacy_field field has been correctly replaced.', $t_args));
  76. }
  77. // Ensure static caches do not interfere with API calls.
  78. drupal_static_reset();
  79. }
  80. /**
  81. * Tests taxonomy programmatic translation workflow.
  82. */
  83. public function testProgrammaticTranslationWorkflow() {
  84. // Create a taxonomy term and assign it an original language different from
  85. // the default language.
  86. $langcode = 'it';
  87. $original_values = array(
  88. 'name' => $langcode . '_' . $this->randomName(),
  89. 'description' => $langcode . '_' . $this->randomName(),
  90. );
  91. $term = (object) ($original_values + array(
  92. 'format' => 'filtered_html',
  93. 'vocabulary_machine_name' => $this->vocabulary->machine_name,
  94. 'vid' => $this->vocabulary->vid,
  95. ));
  96. entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage($langcode);
  97. taxonomy_term_save($term);
  98. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  99. $term = $this->termLoad($term->tid);
  100. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), 'Replacing field values correctly created from the legacy field values.');
  101. // Pollute synchronization cache to ensure the expected values are stored
  102. // anyway.
  103. title_entity_sync('taxonomy_term', $term, $langcode);
  104. // Create a translation using the default language.
  105. $translation_langcode = language_default()->language;
  106. $translated_values = array(
  107. 'name' => $translation_langcode . '_' . $this->randomName(),
  108. 'description' => $translation_langcode . '_' . $this->randomName(),
  109. );
  110. foreach ($translated_values as $name => $value) {
  111. $term->{$name} = $value;
  112. }
  113. $translation = array(
  114. 'language' => $translation_langcode,
  115. 'source' => $langcode,
  116. 'uid' => $this->loggedInUser->uid,
  117. 'status' => 1,
  118. 'translate' => 0,
  119. 'created' => REQUEST_TIME,
  120. 'changed' => REQUEST_TIME,
  121. );
  122. entity_translation_get_handler('taxonomy_term', $term)->setTranslation($translation);
  123. taxonomy_term_save($term);
  124. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  125. $term = $this->termLoad($term->tid, $translation_langcode);
  126. $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode), 'Replacing field translations correctly created.');
  127. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode, FALSE), 'Replacing field original values correctly preserved.');
  128. // Delete the translation.
  129. entity_translation_get_handler('taxonomy_term', $term)->removeTranslation($translation_langcode);
  130. taxonomy_term_save($term);
  131. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  132. $term = $this->termLoad($term->tid, $langcode);
  133. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), 'Replacing field translations correctly deleted.');
  134. // Make the term language neutral.
  135. entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage(LANGUAGE_NONE);
  136. foreach ($original_values as $name => $value) {
  137. $field_name = $name . '_field';
  138. $term->{$field_name}[LANGUAGE_NONE] = $term->{$field_name}[$langcode];
  139. $term->{$field_name}[$langcode] = array();
  140. }
  141. taxonomy_term_save($term);
  142. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  143. $term = $this->termLoad($term->tid);
  144. $this->assertTrue($this->checkFieldValues($term, $original_values, LANGUAGE_NONE), 'Term original language correctly changed to the former translation language.');
  145. // Change the term language to the former translation language.
  146. entity_translation_get_handler('taxonomy_term', $term)->setOriginalLanguage($translation_langcode);
  147. foreach ($original_values as $name => $value) {
  148. $field_name = $name . '_field';
  149. $term->{$field_name}[$translation_langcode] = $term->{$field_name}[LANGUAGE_NONE];
  150. $term->{$field_name}[LANGUAGE_NONE] = array();
  151. }
  152. taxonomy_term_save($term);
  153. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  154. $term = $this->termLoad($term->tid, $translation_langcode);
  155. $this->assertTrue($this->checkFieldValues($term, $original_values, $translation_langcode), 'Term original language correctly changed to language neutral.');
  156. // Make a replacing field untranslatable and change its value.
  157. $field_name = 'name_field';
  158. $field = field_info_field($field_name);
  159. $field['translatable'] = FALSE;
  160. field_update_field($field);
  161. $original_values['name'] = LANGUAGE_NONE . '_' . $this->randomName();
  162. $term->name = $original_values['name'];
  163. taxonomy_term_save($term);
  164. $this->assertTrue($this->checkLegacyValues($term, $original_values), 'Legacy field values correctly stored.');
  165. $term = $this->termLoad($term->tid);
  166. $this->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['value'], $original_values['name'], 'Untranslatable replacing field on translatable entity correctly handled.');
  167. }
  168. /**
  169. * Tests taxonomy form translation workflow.
  170. */
  171. public function testFormTranslationWorkflow() {
  172. // Create a taxonomy term and check that legacy fields are properly
  173. // populated.
  174. $original_values = array(
  175. 'name' => $this->randomName(),
  176. 'description' => $this->randomName(),
  177. );
  178. $langcode = 'en';
  179. $edit = $this->editValues($original_values, $langcode);
  180. $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save'));
  181. $term = current(entity_load('taxonomy_term', FALSE, array('name' => $original_values['name']), TRUE));
  182. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term created.'));
  183. // Translate the taxonomy term and check that both the original values and
  184. // the translations were correctly stored.
  185. $translated_values = array(
  186. 'name' => $this->randomName(),
  187. 'description' => $this->randomName(),
  188. );
  189. $translation_langcode = 'it';
  190. $edit = $this->editValues($translated_values, 'it');
  191. $this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/add/' . $langcode . '/' . $translation_langcode, $edit, t('Save'));
  192. $term = $this->termLoad($term->tid);
  193. $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation created.'));
  194. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.'));
  195. // Check that legacy fields have the correct values.
  196. $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
  197. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));
  198. // Updated the taxonomy term translation and check that both the original
  199. // values and the translations were correctly stored.
  200. $translated_values = array(
  201. 'name' => $this->randomName(),
  202. 'description' => $this->randomName(),
  203. );
  204. $edit = $this->editValues($translated_values, $translation_langcode);
  205. $this->drupalPost($translation_langcode . '/taxonomy/term/' . $term->tid . '/edit/' . $translation_langcode, $edit, t('Save'));
  206. $term = $this->termLoad($term->tid);
  207. $this->assertTrue($this->checkFieldValues($term, $translated_values, $translation_langcode, FALSE), t('Taxonomy term translation updated.'));
  208. $this->assertTrue($this->checkFieldValues($term, $original_values, $langcode), t('Taxonomy term original values preserved.'));
  209. // Check that legacy fields have the correct values.
  210. $this->assertEqual($term->name, $original_values['name'], t('Taxonomy term name correctly stored.'));
  211. $this->assertEqual($term->description, $original_values['description'], t('Taxonomy term description correctly stored.'));
  212. }
  213. /**
  214. * Loads a term using the given language as active language.
  215. *
  216. * @param int $tid
  217. * The term identifier.
  218. * @param string|null $langcode
  219. * (optional) The active language to be set. Defaults to none.
  220. *
  221. * @return object|bool
  222. * A term object.
  223. */
  224. protected function termLoad($tid, $langcode = NULL) {
  225. drupal_static_reset();
  226. title_active_language($langcode);
  227. return current(entity_load('taxonomy_term', array($tid), array(), TRUE));
  228. }
  229. /**
  230. * Returns the drupalPost() $edit array corresponding to the given values.
  231. */
  232. protected function editValues($values, $langcode) {
  233. $edit = array();
  234. foreach ($values as $name => $value) {
  235. $edit["{$name}_field[{$langcode}][0][value]"] = $value;
  236. }
  237. return $edit;
  238. }
  239. /**
  240. * Checks the field values.
  241. *
  242. * Checks that the field values and optionally the legacy ones match the given
  243. * values.
  244. */
  245. protected function checkFieldValues($term, $values, $langcode, $legacy_match = TRUE) {
  246. foreach ($values as $name => $value) {
  247. $field_value = $term->{$name . '_field'}[$langcode][0]['value'];
  248. if ($field_value != $value || ($legacy_match !== ($field_value == $term->{$name}))) {
  249. return FALSE;
  250. }
  251. }
  252. return TRUE;
  253. }
  254. /**
  255. * Checks that the legacy field values in the database match the given values.
  256. */
  257. protected function checkLegacyValues($term, $values) {
  258. $record = db_query('SELECT * FROM {taxonomy_term_data} t WHERE t.tid = :tid', array(':tid' => $term->tid))->fetchAssoc();
  259. foreach ($values as $name => $value) {
  260. if ($record[$name] != $value) {
  261. return FALSE;
  262. }
  263. }
  264. return TRUE;
  265. }
  266. }