tmgmt.base.test 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /*
  3. * @file
  4. * Contains tests for Translation management
  5. */
  6. /**
  7. * Base class for tests.
  8. */
  9. class TMGMTBaseTestCase extends DrupalWebTestCase {
  10. protected $profile = 'testing';
  11. /**
  12. * A default translator using the test translator.
  13. *
  14. * @var TMGMTTranslator
  15. */
  16. protected $default_translator;
  17. /**
  18. * List of permissions used by loginAsAdmin().
  19. *
  20. * @var array
  21. */
  22. protected $admin_permissions = array();
  23. /**
  24. * Drupal user object created by loginAsAdmin().
  25. *
  26. * @var object
  27. */
  28. protected $admin_user = NULL;
  29. /**
  30. * List of permissions used by loginAsTranslator().
  31. *
  32. * @var array
  33. */
  34. protected $translator_permissions = array();
  35. /**
  36. * Drupal user object created by loginAsTranslator().
  37. *
  38. * @var object
  39. */
  40. protected $translator_user = NULL;
  41. /**
  42. * Overrides DrupalWebTestCase::setUp()
  43. */
  44. function setUp() {
  45. $modules = func_get_args();
  46. if (isset($modules[0]) && is_array($modules[0])) {
  47. $modules = $modules[0];
  48. }
  49. $modules = array_merge(array('entity', 'tmgmt', 'tmgmt_test'), $modules);
  50. parent::setUp($modules);
  51. $this->default_translator = tmgmt_translator_load('test_translator');
  52. // Load default admin permissions.
  53. $this->admin_permissions = array(
  54. 'administer languages',
  55. 'access administration pages',
  56. 'administer content types',
  57. 'administer tmgmt',
  58. );
  59. // Load default translator user permissions.
  60. $this->translator_permissions = array(
  61. 'create translation jobs',
  62. 'submit translation jobs',
  63. 'accept translation jobs',
  64. );
  65. }
  66. /**
  67. * Will create a user with admin permissions and log it in.
  68. *
  69. * @param array $additional_permissions
  70. * Additional permissions that will be granted to admin user.
  71. * @param boolean $reset_permissions
  72. * Flag to determine if default admin permissions will be replaced by
  73. * $additional_permissions.
  74. *
  75. * @return object
  76. * Newly created and logged in user object.
  77. */
  78. function loginAsAdmin($additional_permissions = array(), $reset_permissions = FALSE) {
  79. $permissions = $this->admin_permissions;
  80. if ($reset_permissions) {
  81. $permissions = $additional_permissions;
  82. }
  83. elseif (!empty($additional_permissions)) {
  84. $permissions = array_merge($permissions, $additional_permissions);
  85. }
  86. $this->admin_user = $this->drupalCreateUser($permissions);
  87. $this->drupalLogin($this->admin_user);
  88. return $this->admin_user;
  89. }
  90. /**
  91. * Will create a user with translator permissions and log it in.
  92. *
  93. * @param array $additional_permissions
  94. * Additional permissions that will be granted to admin user.
  95. * @param boolean $reset_permissions
  96. * Flag to determine if default admin permissions will be replaced by
  97. * $additional_permissions.
  98. *
  99. * @return object
  100. * Newly created and logged in user object.
  101. */
  102. function loginAsTranslator($additional_permissions = array(), $reset_permissions = FALSE) {
  103. $permissions = $this->translator_permissions;
  104. if ($reset_permissions) {
  105. $permissions = $additional_permissions;
  106. }
  107. elseif (!empty($additional_permissions)) {
  108. $permissions = array_merge($permissions, $additional_permissions);
  109. }
  110. $this->translator_user = $this->drupalCreateUser($permissions);
  111. $this->drupalLogin($this->translator_user);
  112. return $this->translator_user;
  113. }
  114. /**
  115. * Creates, saves and returns a translator.
  116. *
  117. * @return TMGMTTranslator
  118. */
  119. function createTranslator() {
  120. $translator = new TMGMTTranslator();
  121. $translator->name = strtolower($this->randomName());
  122. $translator->label = $this->randomName();
  123. $translator->plugin = 'test_translator';
  124. $translator->settings = array(
  125. 'key' => $this->randomName(),
  126. 'another_key' => $this->randomName(),
  127. );
  128. $this->assertEqual(SAVED_NEW, $translator->save());
  129. // Assert that the translator was assigned a tid.
  130. $this->assertTrue($translator->tid > 0);
  131. return $translator;
  132. }
  133. /**
  134. * Creates, saves and returns a translation job.
  135. *
  136. * @return TMGMTJob
  137. */
  138. function createJob($source = 'en', $target = 'de', $uid = 1) {
  139. $job = tmgmt_job_create($source, $target, $uid);
  140. $this->assertEqual(SAVED_NEW, $job->save());
  141. // Assert that the translator was assigned a tid.
  142. $this->assertTrue($job->tjid > 0);
  143. return $job;
  144. }
  145. /**
  146. * Sets the proper environment.
  147. *
  148. * Currently just adds a new language.
  149. *
  150. * @param string $langcode
  151. * The language code.
  152. */
  153. function setEnvironment($langcode) {
  154. // Add the language.
  155. locale_add_language($langcode);
  156. }
  157. /**
  158. * Asserts job item language codes.
  159. *
  160. * @param TMGMTJobItem $job_item
  161. * Job item to check.
  162. * @param string $expected_source_lang
  163. * Expected source language.
  164. * @param array $actual_lang_codes
  165. * Expected existing language codes (translations).
  166. */
  167. function assertJobItemLangCodes(TMGMTJobItem $job_item, $expected_source_lang, array $actual_lang_codes) {
  168. $this->assertEqual($job_item->getSourceLangCode(), $expected_source_lang);
  169. $existing = $job_item->getExistingLangCodes();
  170. sort($existing);
  171. sort($actual_lang_codes);
  172. $this->assertEqual($existing, $actual_lang_codes);
  173. }
  174. /**
  175. * Adds languages as admin user and switches to a translator user.
  176. */
  177. protected function createLanguagesLoginTranslator($permissions = NULL) {
  178. // Login as admin to be able to set environment variables.
  179. $this->loginAsAdmin();
  180. $this->setEnvironment('de');
  181. $this->setEnvironment('es');
  182. $this->setEnvironment('el');
  183. $base_permissions = array(
  184. 'access administration pages',
  185. 'create translation jobs',
  186. 'submit translation jobs',
  187. );
  188. $permissions = $permissions ? array_merge($permissions, $base_permissions) : $base_permissions;
  189. // Login as translator only with limited permissions to run these tests.
  190. $this->loginAsTranslator($permissions, TRUE);
  191. }
  192. }