i18n_taxonomy.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @file
  4. * Test case for multilingual taxonomy
  5. */
  6. class i18nTaxonomyTestCase extends Drupali18nTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'Taxonomy translation',
  10. 'group' => 'Internationalization',
  11. 'description' => 'Taxonomy translation functions'
  12. );
  13. }
  14. function setUp() {
  15. parent::setUp(array('i18n_taxonomy', 'field_test'));
  16. parent::setUpLanguages();
  17. // Create users.
  18. $filtered_html_format = filter_format_load('filtered_html');
  19. $full_html_format = filter_format_load('full_html');
  20. $this->admin_user = $this->drupalCreateUser(array(
  21. 'access field_test content',
  22. 'administer field_test content',
  23. 'administer taxonomy',
  24. 'administer languages',
  25. 'administer site configuration',
  26. filter_permission_name($filtered_html_format),
  27. filter_permission_name($full_html_format),
  28. ));
  29. $this->translator = $this->drupalCreateUser(array('translate interface', 'translate user-defined strings'));
  30. }
  31. function testTaxonomyTermLocalize() {
  32. $this->drupalLogin($this->admin_user);
  33. // Make Input Format "Filter Text" translatable
  34. $edit = array(
  35. 'i18n_string_allowed_formats[filtered_html]' => 'filtered_html',
  36. 'i18n_string_allowed_formats[plain_text]' => 'plain_text',
  37. );
  38. $this->drupalPost('admin/config/regional/i18n/strings', $edit, t('Save configuration'));
  39. // Create a localizable vocabulary.
  40. $vocab = $this->createVocabulary(array('i18n_mode' => I18N_MODE_LOCALIZE));
  41. $this->assertEqual(i18n_taxonomy_vocabulary_mode($vocab->vid), I18N_MODE_LOCALIZE, 'A vocabulary has been created and it is localizable.');
  42. $this->field_name = $this->createTermField($vocab->machine_name);
  43. // Create a term to be localized. We use a common prefix to facilitate the testing of autocomplete suggestions.
  44. $prefix = $this->randomName() . '_';
  45. $term = $this->createTerm(array('vid' => $vocab->vid, 'name' => $prefix . $this->randomName()));
  46. $this->drupalLogin($this->translator);
  47. // Create and Save Spanish translation, again using the same prefix.
  48. $term_translation = array(
  49. 'name' => $this->createStringTranslation('taxonomy', $term->name, array($this->secondary_language => $prefix . $this->randomName())),
  50. 'description' => $this->createStringTranslation('taxonomy', $term->description, array($this->secondary_language => $prefix . $this->randomName())),
  51. );
  52. $this->drupalLogin($this->admin_user);
  53. $langcode = LANGUAGE_NONE;
  54. $edit = array(
  55. "{$this->field_name}[$langcode]" => array($term->tid),
  56. );
  57. // Test the widgets in the original language.
  58. $this->drupalGet('test-entity/add/test-bundle');
  59. $this->assertText($term->name, 'Widget values are displayed correctly in default language.');
  60. $this->drupalPost(NULL, $edit, t('Save'));
  61. $this->assertText($term->name, 'Field values are displayed correctly in default language.');
  62. // Terms should be localized in the field widget.
  63. $this->drupalGet($this->secondary_language . '/test-entity/add/test-bundle');
  64. $this->assertText($term_translation['name'][$this->secondary_language], 'Widget values are displayed correctly in non-default languages.');
  65. $this->drupalPost(NULL, $edit, t('Save'));
  66. $this->assertText($term_translation['name'][$this->secondary_language], 'Field values are displayed correctly in non-default languages.');
  67. // Term name and term description should be localized
  68. $this->drupalGet('taxonomy/term/' . $term->tid, array('language' => i18n_language_object($this->default_language)));
  69. $this->assertText($term->name, 'Term title is displayed correctly in default language.');
  70. $this->assertText($term->description, 'Term description is displayed correctly in default language.');
  71. // Term name and term description should be localized
  72. $this->drupalGet('taxonomy/term/' . $term->tid, array('language' => i18n_language_object($this->secondary_language)));
  73. $this->assertText($term_translation['name'][$this->secondary_language], 'Term title is displayed correctly in non-default language.');
  74. $this->assertText($term_translation['description'][$this->secondary_language], 'Term description is displayed correctly in non-default language.');
  75. // Autocomplete should respect localization.
  76. $autocomplete_path = 'taxonomy/autocomplete/' . $this->field_name . '/' . $prefix;
  77. $autocomplete_values = $this->drupalGetAJAX($autocomplete_path);
  78. $this->assertTrue(isset($autocomplete_values[$term->name]), 'Correct autocomplete suggestions in default language.');
  79. $this->assertFalse(isset($autocomplete_values[$term_translation['name'][$this->secondary_language]]), 'No incorrect autocomplete suggestions in non-default languages');
  80. // Autocomplete should respect localization, but doesn't.
  81. // $autocomplete_path = $this->secondary_language . '/taxonomy/autocomplete/' . $this->field_name . '/' . $prefix;
  82. // $autocomplete_values = $this->drupalGetAJAX($autocomplete_path);
  83. // $this->assertFalse(isset($autocomplete_values[$term->name]), 'Correct autocomplete suggestions in non-default languages.');
  84. // $this->assertTrue(isset($autocomplete_values[$term_translation[$this->secondary_language]]), 'No incorrect autocomplete suggestions in non-default languages.');
  85. }
  86. function testTaxonomyTermTranslate() {
  87. // Create a translateable vocabulary.
  88. $vocab = $this->createVocabulary(array('i18n_mode' => I18N_MODE_TRANSLATE));
  89. $this->assertEqual(i18n_taxonomy_vocabulary_mode($vocab->vid), I18N_MODE_TRANSLATE, 'A vocabulary has been created and it is translateable.');
  90. $this->field_select = $this->createTermField($vocab->machine_name);
  91. $this->field_autocomplete = $this->createTermField($vocab->machine_name, 'taxonomy_autocomplete');
  92. // Create a term to be translated.
  93. $en_term = $this->createTerm(array('vid' => $vocab->vid, 'language' => $this->default_language));
  94. $es_term = $this->createTerm(array('vid' => $vocab->vid, 'language' => $this->secondary_language));
  95. $this->drupalLogin($this->admin_user);
  96. // Set terms as translations of each other.
  97. $edit = array(
  98. 'translations[' . $this->default_language . ']' => $en_term->name,
  99. 'translations[' . $this->secondary_language . ']' => $es_term->name,
  100. );
  101. $this->drupalPost('admin/structure/taxonomy/' . $vocab->machine_name . '/list/sets/add', $edit, t('Save'));
  102. $this->drupalGet('admin/structure/taxonomy/' . $vocab->machine_name . '/list/sets');
  103. // Freetagging creates terms with the correct language.
  104. $new_term_name = $this->randomName();
  105. $langcode = LANGUAGE_NONE;
  106. $edit = array(
  107. "{$this->field_autocomplete}[$langcode]" => $new_term_name,
  108. );
  109. $this->drupalPost($this->secondary_language . '/test-entity/add/test-bundle', $edit, t('Save'));
  110. $new_term = current(taxonomy_get_term_by_name($new_term_name));
  111. $this->assertEqual($new_term->language, $this->secondary_language, 'Freetagging creates terms with the correct language.');
  112. // Term translations are used for language switching.
  113. $language_switcher = language_negotiation_get_switch_links(LANGUAGE_TYPE_INTERFACE, 'taxonomy/term/' . $en_term->tid);
  114. $this->assertEqual($language_switcher->links[$this->secondary_language]['href'], 'taxonomy/term/' . $es_term->tid, 'Term translations are used for language switching.');
  115. }
  116. /**
  117. * Tests the implementation of 'options_list_callback' for term reference fields.
  118. * Enable and disable the callback properly. Avoid WSOD!
  119. */
  120. function testTaxonomyFieldCallback() {
  121. $field_name = 'taxonomy_term_test_field';
  122. $field = field_create_field(array(
  123. 'field_name' => $field_name,
  124. 'type' => 'taxonomy_term_reference',
  125. ));
  126. $field = field_info_field($field_name);
  127. $callback = 'i18n_taxonomy_allowed_values';
  128. $this->assertTrue(function_exists($callback), "Function $callback exists.");
  129. $this->assertEqual($field['settings']['options_list_callback'], $callback, "$callback ist option list callback.");
  130. module_disable(array('i18n_taxonomy'));
  131. $field = field_info_field($field_name);
  132. $this->assertNotEqual($field['settings']['options_list_callback'], $callback, "$callback ist option list callback.");
  133. }
  134. // Create vocabulary with given fields
  135. function drupalCreateVocabulary($vocab = array()) {
  136. $vocab += array('name' => $this->randomName(10), 'description' => $this->randomName(20));
  137. taxonomy_vocabulary_save($vocab);
  138. return (object)$vocab;
  139. }
  140. // Create term with given fields
  141. function drupalCreateTerms($number = 1, $data = array()) {
  142. $list = array();
  143. for ($i = 1; $i <= $number ; $i++ ) {
  144. $term = $this->createTerm($data);
  145. $list[$term->tid] = $term;
  146. }
  147. return $list;
  148. }
  149. /**
  150. * Returns a new vocabulary with random properties.
  151. */
  152. function createVocabulary($data = array()) {
  153. // Create a vocabulary.
  154. $data += array(
  155. 'i18n_mode' => I18N_MODE_LOCALIZE,
  156. 'name' => $this->randomName(),
  157. 'description' => $this->randomName(),
  158. 'machine_name' => drupal_strtolower($this->randomName()),
  159. 'help' => '',
  160. 'nodes' => array('article' => 'article'),
  161. 'weight' => mt_rand(0, 10),
  162. );
  163. $vocabulary = (object)$data;
  164. taxonomy_vocabulary_save($vocabulary);
  165. return $vocabulary;
  166. }
  167. /**
  168. * Returns a new term with random properties in vocabulary $vid.
  169. */
  170. function createTerm($data = array()) {
  171. $data += array(
  172. 'name' => $this->randomName(),
  173. 'description' => $this->randomName(),
  174. // Use the first available text format and vocabulary.
  175. 'format' => filter_default_format(),
  176. 'vid' => 1,
  177. );
  178. $term = (object)$data;
  179. taxonomy_term_save($term);
  180. return $term;
  181. }
  182. /**
  183. * Setup a field and instance.
  184. */
  185. function createTermField($machine_name, $widget = 'options_select') {
  186. $field_name = drupal_strtolower($this->randomName());
  187. $this->field = array(
  188. 'field_name' => $field_name,
  189. 'type' => 'taxonomy_term_reference',
  190. 'settings' => array(
  191. 'allowed_values' => array(
  192. array(
  193. 'vocabulary' => $machine_name,
  194. 'parent' => '0',
  195. ),
  196. ),
  197. )
  198. );
  199. field_create_field($this->field);
  200. $this->instance = array(
  201. 'field_name' => $field_name,
  202. 'entity_type' => 'test_entity',
  203. 'bundle' => 'test_bundle',
  204. 'widget' => array(
  205. 'type' => $widget,
  206. ),
  207. 'display' => array(
  208. 'full' => array(
  209. 'type' => 'taxonomy_term_reference_link',
  210. ),
  211. ),
  212. );
  213. field_create_instance($this->instance);
  214. return $field_name;
  215. }
  216. }