i18n_field.test 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * Test case for multilingual fields.
  5. */
  6. class i18nFieldTestCase extends Drupali18nTestCase {
  7. public static function getInfo() {
  8. return array(
  9. 'name' => 'Field translation',
  10. 'group' => 'Internationalization',
  11. 'description' => 'Field translation functions'
  12. );
  13. }
  14. function setUp() {
  15. parent::setUp('i18n_field', 'field_test');
  16. parent::setUpLanguages(array('access field_test content', 'administer field_test content'));
  17. $this->translator = $this->drupalCreateUser(array('translate interface', 'translate user-defined strings'));
  18. }
  19. /**
  20. * Test the translation of list fields, including allowed values.
  21. */
  22. function testListFieldTranslation() {
  23. $field_name = drupal_strtolower($this->randomName());
  24. $label = $this->randomName();
  25. $description = $this->randomName();
  26. $value = $this->randomName();
  27. $field = array(
  28. 'field_name' => $field_name,
  29. 'type' => 'list_integer',
  30. 'cardinality' => 1,
  31. 'settings' => array(
  32. 'allowed_values' => array(1 => $value),
  33. ),
  34. );
  35. $field = field_create_field($field);
  36. $instance = array(
  37. 'field_name' => $field_name,
  38. 'entity_type' => 'test_entity',
  39. 'bundle' => 'test_bundle',
  40. 'label' => $label,
  41. 'description' => $description,
  42. 'widget' => array(
  43. 'type' => 'options_buttons',
  44. ),
  45. );
  46. $instance = field_create_instance($instance);
  47. // Refresh i18n_strings.
  48. $edit = array('groups[field]' => TRUE);
  49. $this->drupalPost('admin/config/regional/translate/i18n_string', $edit, t('Refresh strings'));
  50. // Save translations for each attribute.
  51. $label_translation = $this->createStringTranslation('field', $label);
  52. $description_translation = $this->createStringTranslation('field', $description);
  53. $value_translation = $this->createStringTranslation('field', $value);
  54. $this->drupalLogin($this->admin_user);
  55. // Test untranslated values in default language.
  56. $this->drupalGet('test-entity/add/test-bundle');
  57. $this->assertText($label, 'Field label is not translated');
  58. $this->assertText($description, 'Field description is not translated');
  59. $this->assertText($value, 'Field allowed values are not translated');
  60. // Test translated values in secondary language.
  61. $this->drupalGet($this->secondary_language . '/test-entity/add/test-bundle');
  62. $this->assertText($label_translation[$this->secondary_language], 'Field label is translated');
  63. $this->assertText($description_translation[$this->secondary_language], 'Field description is translated');
  64. $this->assertText($value_translation[$this->secondary_language], 'Field allowed values are translated');
  65. }
  66. /**
  67. * Test the translation of text fields, including default values.
  68. */
  69. function testTextFieldTranslation() {
  70. $field_name = drupal_strtolower($this->randomName());
  71. $label = $this->randomName();
  72. $description = $this->randomName();
  73. $default_value = $this->randomName();
  74. $field = array(
  75. 'field_name' => $field_name,
  76. 'type' => 'text',
  77. 'cardinality' => 1,
  78. );
  79. $field = field_create_field($field);
  80. $instance = array(
  81. 'field_name' => $field_name,
  82. 'entity_type' => 'test_entity',
  83. 'bundle' => 'test_bundle',
  84. 'label' => $label,
  85. 'description' => $description,
  86. 'default_value' => array(0 => array('value' => $default_value)),
  87. 'widget' => array(
  88. 'type' => 'text_textfield',
  89. ),
  90. );
  91. $instance = field_create_instance($instance);
  92. // Refresh i18n_strings.
  93. $edit = array('groups[field]' => TRUE);
  94. $this->drupalPost('admin/config/regional/translate/i18n_string', $edit, t('Refresh strings'));
  95. // Save translations for each attribute.
  96. $label_translation = $this->createStringTranslation('field', $label);
  97. $description_translation = $this->createStringTranslation('field', $description);
  98. $default_value_translation = $this->createStringTranslation('field', $default_value);
  99. $this->drupalLogin($this->admin_user);
  100. // Test untranslated values in default language.
  101. $this->drupalGet('test-entity/add/test-bundle');
  102. $this->assertText($label, 'Field label is not translated');
  103. $this->assertText($description, 'Field description is not translated');
  104. $this->assertRaw($default_value, 'Default value is not translated');
  105. // Test translated values in secondary language.
  106. $this->drupalGet($this->secondary_language . '/test-entity/add/test-bundle');
  107. $this->assertText($label_translation[$this->secondary_language], 'Field label is translated');
  108. $this->assertText($description_translation[$this->secondary_language], 'Field description is translated');
  109. $this->assertRaw($default_value_translation[$this->secondary_language], 'Default value translated');
  110. }
  111. }