i18n_access.test 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @file
  4. * Test suite for i18n_access.module
  5. */
  6. class i18nAccessTestCase extends DrupalWebTestCase {
  7. /**
  8. * Implementation of getInfo().
  9. */
  10. function getInfo() {
  11. return array(
  12. 'name' => t('Translation Access'),
  13. 'description' => t('Test suite for the i18n_access module.'),
  14. 'group' => t('i18n'),
  15. );
  16. }
  17. /**
  18. * Implementation of setUp().
  19. */
  20. function setUp() {
  21. parent::setUp('locale', 'translation', 'i18n_access');
  22. $this->admin_user = $this->drupalCreateUser(array('administer languages', 'administer site configuration', 'access administration pages', 'administer content types', 'administer nodes', 'administer users'));
  23. $this->translator = $this->drupalCreateUser(array('create story content', 'edit own story content', 'translate content'));
  24. $this->visitor = $this->drupalCreateUser(array('access content'));
  25. $this->drupalLogin($this->admin_user);
  26. $this->addLanguage('fr');
  27. $this->addLanguage('de');
  28. $this->setLanguagePermissions($this->translator, array('en', 'fr'));
  29. // Set Story content type to use multilingual support with translation.
  30. $edit = array();
  31. $edit['language_content_type'] = 2;
  32. $this->drupalPost('admin/content/node-type/story', $edit, t('Save content type'));
  33. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Story')), t('Story content type has been updated.'));
  34. }
  35. /**
  36. * Enable the specified language if it has not been already.
  37. *
  38. * @param string $language_code
  39. * The language code to enable.
  40. */
  41. function addLanguage($language_code) {
  42. // Check to make sure that language has not already been installed.
  43. $this->drupalGet('admin/settings/language');
  44. if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
  45. // Doesn't have language installed so add it.
  46. $edit = array();
  47. $edit['langcode'] = $language_code;
  48. $this->drupalPost('admin/settings/language/add', $edit, t('Add language'));
  49. $languages = language_list('language', TRUE); // Make sure not using cached version.
  50. $this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
  51. if (array_key_exists($language_code, $languages)) {
  52. $this->assertRaw(t('The language %language has been created and can now be used.', array('%language' => $languages[$language_code]->name)), t('Language has been created.'));
  53. }
  54. }
  55. else {
  56. // Ensure that it is enabled.
  57. $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
  58. $this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
  59. }
  60. }
  61. /**
  62. * Sets the language permission for the specified user. Must be logged in as
  63. * an 'administer users' privileged user before calling this.
  64. *
  65. * @param $account
  66. * The user account to modify
  67. * @param $languages
  68. * An array of language codes to give permission for
  69. */
  70. function setLanguagePermissions($account, $languages = array()) {
  71. $this->assertTrue(user_access('administer users'), t('User has permission to administer users'));
  72. foreach ($languages as $langcode) {
  73. $key = 'i18n_access[' . $langcode . ']';
  74. $edit[$key] = $langcode;
  75. $expected[$langcode] = $langcode;
  76. }
  77. $this->drupalPost('user/' . $account->uid . '/edit', $edit, t('Save'));
  78. $actual = i18n_access_load_permissions($account->uid);
  79. $this->assertEqual($expected, $actual, t('Language permissions set correctly.'), 'i18n_access');
  80. }
  81. /**
  82. * Assert that a language option exists in the language select field on the
  83. * current page.
  84. * @param $langcode
  85. * Value of the language option to assert.
  86. * @param $message
  87. * Message to display.
  88. * @param $group
  89. * The group this message belongs to.
  90. * @return
  91. * TRUE on pass, FALSE on fail.
  92. */
  93. function assertLanguageOption($langcode, $message, $group = 'Other') {
  94. $xpath = '//select[@name="language"]/option';
  95. $fields = $this->xpath($xpath);
  96. // If value specified then check array for match.
  97. $found = TRUE;
  98. if (isset($langcode)) {
  99. $found = FALSE;
  100. if ($fields) {
  101. foreach ($fields as $field) {
  102. if ($field['value'] == $langcode) {
  103. $found = TRUE;
  104. }
  105. }
  106. }
  107. }
  108. return $this->assertTrue($fields && $found, $message, $group);
  109. }
  110. /**
  111. * Assert that a language option does not exist in the language select field
  112. * on the current page.
  113. * @param $langcode
  114. * Value of the language option to assert.
  115. * @param $message
  116. * Message to display.
  117. * @param $group
  118. * The group this message belongs to.
  119. * @return
  120. * TRUE on pass, FALSE on fail.
  121. */
  122. function assertNoLanguageOption($langcode, $message, $group = 'Other') {
  123. $xpath = '//select[@name="language"]/option';
  124. $fields = $this->xpath($xpath);
  125. // If value specified then check array for match.
  126. $found = TRUE;
  127. if (isset($langcode)) {
  128. $found = FALSE;
  129. if ($fields) {
  130. foreach ($fields as $field) {
  131. if ($field['value'] == $langcode) {
  132. $found = TRUE;
  133. }
  134. }
  135. }
  136. }
  137. return $this->assertFalse($fields && $found, $message, $group);
  138. }
  139. function dsm($object) {
  140. $this->error('<pre>' . check_plain(print_r($object, 1)) . '</pre>');
  141. }
  142. /**
  143. * Test translator user. User with 'create story content' and 'edit own story
  144. * content' permissions should be able to create and edit story nodes only in
  145. * the languages that they have permissions for.
  146. */
  147. function testTranslatorUser() {
  148. $this->drupalLogin($this->translator);
  149. $this->drupalGet('node/add/story');
  150. $this->assertField('language', t('Found language selector.'));
  151. $perms = i18n_access_load_permissions($this->translator->uid);
  152. $languages = language_list();
  153. $languages[I18N_ACCESS_LANGUAGE_NEUTRAL] = (object)array('language' => '', 'name' => 'Language Neutral');
  154. foreach ($languages as $key => $language) {
  155. // TODO: Add in check for language neutral
  156. if (isset($perms[$key]) && $perms[$key]) {
  157. $this->assertLanguageOption($language->language, t('Option found for %language in language selector.', array('%language' => $language->name)));
  158. }
  159. else {
  160. $this->assertNoLanguageOption($language->language, t('Option not found for %language in language selector.', array('%language' => $language->name)));
  161. }
  162. }
  163. }
  164. /**
  165. * Test admin user. User with 'administer nodes' permission should be able to
  166. * create and edit nodes regardless of the language
  167. */
  168. function testAdminUser() {
  169. $this->drupalLogin($this->admin_user);
  170. $this->drupalGet('node/add/story');
  171. $this->assertField('language', t('Found language selector.'));
  172. $perms = i18n_access_load_permissions($this->admin_user->uid);
  173. $languages = language_list();
  174. $languages[I18N_ACCESS_LANGUAGE_NEUTRAL] = (object)array('language' => '', 'name' => 'Language Neutral');
  175. foreach ($languages as $language) {
  176. // TODO: Add in check for language neutral
  177. $this->assertLanguageOption($language->language, t('Option found for %language, regardless of permission, for administrator.', array('%language' => $language->name)));
  178. }
  179. }
  180. }