rules_i18n.test 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @file
  4. * Rules i18n tests.
  5. */
  6. /**
  7. * Test the i18n integration.
  8. */
  9. class RulesI18nTestCase extends DrupalWebTestCase {
  10. /**
  11. * Declares test metadata.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Rules I18n',
  16. 'description' => 'Tests translating Rules configs.',
  17. 'group' => 'Rules',
  18. 'dependencies' => array('i18n_string'),
  19. );
  20. }
  21. /**
  22. * Overrides DrupalWebTestCase::setUp().
  23. */
  24. protected function setUp() {
  25. parent::setUp('rules_i18n');
  26. $this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages'));
  27. $this->drupalLogin($this->admin_user);
  28. $this->addLanguage('de');
  29. }
  30. /**
  31. * Copied from i18n module (class Drupali18nTestCase).
  32. *
  33. * We cannot extend from Drupali18nTestCase as else the test-bot would die.
  34. */
  35. public function addLanguage($language_code) {
  36. // Check to make sure that language has not already been installed.
  37. $this->drupalGet('admin/config/regional/language');
  38. if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
  39. // Doesn't have language installed so add it.
  40. $edit = array();
  41. $edit['langcode'] = $language_code;
  42. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  43. // Make sure we are not using a stale list.
  44. drupal_static_reset('language_list');
  45. $languages = language_list('language');
  46. $this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
  47. if (array_key_exists($language_code, $languages)) {
  48. $this->assertRaw(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => $languages[$language_code]->name, '@locale-help' => url('admin/help/locale'))), t('Language has been created.'));
  49. }
  50. }
  51. elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
  52. // It's installed and enabled. No need to do anything.
  53. $this->assertTrue(TRUE, 'Language [' . $language_code . '] already installed and enabled.');
  54. }
  55. else {
  56. // It's installed but not enabled. Enable it.
  57. $this->assertTrue(TRUE, 'Language [' . $language_code . '] already installed.');
  58. $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
  59. $this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
  60. }
  61. }
  62. /**
  63. * Tests translating rules configs.
  64. */
  65. public function testRulesConfigTranslation() {
  66. // Create a rule and translate it.
  67. $rule = rule();
  68. $rule->label = 'label-en';
  69. $rule->action('drupal_message', array('message' => 'English message for [site:current-user].'));
  70. $rule->save();
  71. $actions = $rule->actions();
  72. $id = $actions[0]->elementId();
  73. // Add a translation.
  74. i18n_string_textgroup('rules')->update_translation("rules_config:{$rule->name}:label", 'de', 'label-de');
  75. i18n_string_textgroup('rules')->update_translation("rules_config:{$rule->name}:$id:message", 'de', 'German message für [site:current-user].');
  76. // Execute the Rule and make sure the translated message has been output.
  77. // To do so, set the global language to German.
  78. $languages = language_list();
  79. $GLOBALS['language'] = $languages['de'];
  80. // Clear messages and execute the rule.
  81. i18n_string_textgroup('rules')->cache_reset();
  82. drupal_get_messages();
  83. $rule->execute();
  84. $messages = drupal_get_messages();
  85. $this->assertEqual($messages['status'][0], 'German message für ' . $GLOBALS['user']->name . '.', 'Translated message has been output.');
  86. // Test re-naming the rule.
  87. $rule->name = 'rules_i18n_name_2';
  88. $rule->save();
  89. $translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
  90. $this->assertEqual($translation, 'label-de', 'Translation survives a name change.');
  91. // Test updating and make sure the translation stays.
  92. $rule->label = 'Label new';
  93. $rule->save();
  94. $translation = entity_i18n_string("rules:rules_config:{$rule->name}:label", $rule->label, 'de');
  95. $this->assertEqual($translation, 'label-de', 'Translation survives an update.');
  96. // Test deleting the action and make sure the string is deleted too.
  97. $actions[0]->delete();
  98. $rule->save();
  99. $translation = entity_i18n_string("rules_config:{$rule->name}:$id:message", 'English message for [site:current-user].', 'de');
  100. $this->assertEqual($translation, 'English message for [site:current-user].', 'Translation of deleted action has been deleted.');
  101. // Now delete the whole config and make sure all translations are deleted.
  102. $rule->delete();
  103. $translation = entity_i18n_string("rules_config:{$rule->name}:label", 'label-en', 'de');
  104. $this->assertEqual($translation, 'label-en', 'Translation of deleted config has been deleted.');
  105. }
  106. /**
  107. * Tests the "Translate a text" action.
  108. */
  109. public function testI18nActionT() {
  110. $set = rules_action_set(array());
  111. $set->action('rules_i18n_t', array(
  112. 'text' => 'untranslated',
  113. 'language' => 'de',
  114. ));
  115. $set->action('drupal_message', array('message:select' => 'text'));
  116. $set->save('rules_i18n_test');
  117. // Add a translation.
  118. $actions = $set->getIterator();
  119. $id = $actions[0]->elementId();
  120. i18n_string_textgroup('rules')->update_translation("rules_config:{$set->name}:$id:text", 'de', 'text-de');
  121. // Clear messages and execute it.
  122. drupal_get_messages();
  123. $set->execute();
  124. $messages = drupal_get_messages();
  125. $this->assertEqual($messages['status'][0], 'text-de', 'Text has been successfully translated.');
  126. // Enable the PHP module and make sure PHP in translations is not evaluated.
  127. module_enable(array('php'));
  128. i18n_string_textgroup('rules')->update_translation("rules_config:{$set->name}:$id:text", 'de', 'text <?php echo "eval";?>');
  129. // Clear messages and execute it.
  130. drupal_get_messages();
  131. $set->execute();
  132. $messages = drupal_get_messages();
  133. $this->assertEqual($messages['status'][0], check_plain('text <?php echo "eval";?>'), 'PHP in translated text is not executed.');
  134. }
  135. /**
  136. * Tests the "Select a translated value" action.
  137. */
  138. public function testI18nActionSelect() {
  139. // Make the body field and the node type 'page' translatable.
  140. $field = field_info_field('body');
  141. $field['translatable'] = TRUE;
  142. field_update_field($field);
  143. variable_set('language_content_type_page', 1);
  144. $set = rules_action_set(array('node' => array('type' => 'node')));
  145. $set->action('rules_i18n_select', array(
  146. 'data:select' => 'node:body:value',
  147. 'language' => 'de',
  148. 'data_translated:var' => 'body',
  149. ));
  150. $set->action('drupal_message', array('message:select' => 'body'));
  151. $set->save();
  152. $body['en'][0] = array('value' => 'English body.');
  153. $body['de'][0] = array('value' => 'German body.');
  154. $node = $this->drupalCreateNode(array('language' => 'en', 'body' => $body));
  155. // Clear messages and execute it.
  156. drupal_get_messages();
  157. $set->execute($node);
  158. $messages = drupal_get_messages();
  159. $this->assertEqual($messages['status'][0], "German body.\n", 'Translated text has been selected.');
  160. }
  161. }