PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Selenium/bootstrap.php

https://bitbucket.org/gencer/roundcubemail
PHP | 234 lines | 146 code | 43 blank | 45 comment | 19 complexity | 765b9a1ff184ec800cb1f5cf8ff1bb00 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. +-----------------------------------------------------------------------+
  4. | tests/Selenium/bootstrap.php |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2009-2014, The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Environment initialization script for unit tests |
  15. +-----------------------------------------------------------------------+
  16. | Author: Thomas Bruederli <roundcube@gmail.com> |
  17. | Author: Aleksander Machniak <alec@alec.pl> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. if (php_sapi_name() != 'cli')
  21. die("Not in shell mode (php-cli)");
  22. if (!defined('INSTALL_PATH')) define('INSTALL_PATH', realpath(dirname(__FILE__) . '/../../') . '/' );
  23. define('TESTS_DIR', dirname(__FILE__) . '/');
  24. if (@is_dir(TESTS_DIR . 'config')) {
  25. define('RCUBE_CONFIG_DIR', TESTS_DIR . 'config');
  26. }
  27. require_once(INSTALL_PATH . 'program/include/iniset.php');
  28. // Extend include path so some plugin test won't fail
  29. $include_path = ini_get('include_path') . PATH_SEPARATOR . TESTS_DIR . '..';
  30. if (set_include_path($include_path) === false) {
  31. die("Fatal error: ini_set/set_include_path does not work.");
  32. }
  33. $rcmail = rcmail::get_instance('test');
  34. define('TESTS_URL', $rcmail->config->get('tests_url'));
  35. define('TESTS_BROWSER', $rcmail->config->get('tests_browser', 'firefox'));
  36. define('TESTS_USER', $rcmail->config->get('tests_username'));
  37. define('TESTS_PASS', $rcmail->config->get('tests_password'));
  38. define('TESTS_SLEEP', $rcmail->config->get('tests_sleep', 5));
  39. PHPUnit_Extensions_Selenium2TestCase::shareSession(true);
  40. /**
  41. * satisfy PHPUnit
  42. */
  43. class bootstrap
  44. {
  45. /**
  46. * Wipe and re-initialize (mysql) database
  47. */
  48. public static function init_db()
  49. {
  50. $rcmail = rcmail::get_instance();
  51. // drop all existing tables first
  52. $db = $rcmail->get_dbh();
  53. $db->query("SET FOREIGN_KEY_CHECKS=0");
  54. $sql_res = $db->query("SHOW TABLES");
  55. while ($sql_arr = $db->fetch_array($sql_res)) {
  56. $table = reset($sql_arr);
  57. $db->query("DROP TABLE $table");
  58. }
  59. // init database with schema
  60. $dsn = parse_url($rcmail->config->get('db_dsnw'));
  61. $db_name = trim($dsn['path'], '/');
  62. if ($dsn['scheme'] == 'mysql' || $dsn['scheme'] == 'mysqli') {
  63. system(sprintf('cat %s %s | mysql -h %s -u %s --password=%s %s',
  64. realpath(INSTALL_PATH . '/SQL/mysql.initial.sql'),
  65. realpath(TESTS_DIR . '/Selenium/data/mysql.sql'),
  66. escapeshellarg($dsn['host']),
  67. escapeshellarg($dsn['user']),
  68. escapeshellarg($dsn['pass']),
  69. escapeshellarg($db_name)
  70. ));
  71. }
  72. }
  73. /**
  74. * Wipe the configured IMAP account and fill with test data
  75. */
  76. public static function init_imap()
  77. {
  78. if (!TESTS_USER)
  79. return false;
  80. // TBD.
  81. }
  82. }
  83. // @TODO: make sure mailbox has some content (always the same) or is empty
  84. // @TODO: plugins: enable all?
  85. /**
  86. * Base class for all tests in this directory
  87. */
  88. class Selenium_Test extends PHPUnit_Extensions_Selenium2TestCase
  89. {
  90. protected function setUp()
  91. {
  92. $this->setBrowser(TESTS_BROWSER);
  93. // Set root to our index.html, for better performance
  94. // See https://github.com/sebastianbergmann/phpunit-selenium/issues/217
  95. $baseurl = preg_replace('!/index(-.+)?\.php^!', '', TESTS_URL);
  96. $this->setBrowserUrl($baseurl . '/tests/Selenium');
  97. }
  98. protected function login()
  99. {
  100. $this->go('mail');
  101. $user_input = $this->byCssSelector('form input[name="_user"]');
  102. $pass_input = $this->byCssSelector('form input[name="_pass"]');
  103. $submit = $this->byCssSelector('form input[type="submit"]');
  104. $user_input->value(TESTS_USER);
  105. $pass_input->value(TESTS_PASS);
  106. // submit login form
  107. $submit->click();
  108. // wait after successful login
  109. sleep(TESTS_SLEEP);
  110. }
  111. protected function go($task = 'mail', $action = null)
  112. {
  113. $this->url(TESTS_URL . '?_task=' . $task);
  114. // wait for interface load (initial ajax requests, etc.)
  115. sleep(TESTS_SLEEP);
  116. if ($action) {
  117. $this->click_button($action);
  118. sleep(TESTS_SLEEP);
  119. }
  120. }
  121. protected function get_env()
  122. {
  123. return $this->execute(array(
  124. 'script' => 'return rcmail.env;',
  125. 'args' => array(),
  126. ));
  127. }
  128. protected function get_buttons($action)
  129. {
  130. $buttons = $this->execute(array(
  131. 'script' => "return rcmail.buttons['$action'];",
  132. 'args' => array(),
  133. ));
  134. if (is_array($buttons)) {
  135. foreach ($buttons as $idx => $button) {
  136. $buttons[$idx] = $button['id'];
  137. }
  138. }
  139. return (array) $buttons;
  140. }
  141. protected function get_objects()
  142. {
  143. return $this->execute(array(
  144. 'script' => "var i,r = []; for (i in rcmail.gui_objects) r.push(i); return r;",
  145. 'args' => array(),
  146. ));
  147. }
  148. protected function click_button($action)
  149. {
  150. $buttons = $this->get_buttons($action);
  151. $id = array_shift($buttons);
  152. // this doesn't work for me
  153. $this->byId($id)->click();
  154. }
  155. protected function ajaxResponse($action, $script = '', $button = false)
  156. {
  157. if (!$script && !$button) {
  158. $script = "rcmail.command('$action')";
  159. }
  160. $script =
  161. "if (!window.test_ajax_response) {
  162. window.test_ajax_response_object = {};
  163. function test_ajax_response(response)
  164. {
  165. if (response.response && response.response.action) {
  166. window.test_ajax_response_object[response.response.action] = response.response;
  167. }
  168. }
  169. rcmail.addEventListener('responsebefore', test_ajax_response);
  170. }
  171. window.test_ajax_response_object['$action'] = null;
  172. $script;
  173. ";
  174. // run request
  175. $this->execute(array(
  176. 'script' => $script,
  177. 'args' => array(),
  178. ));
  179. if ($button) {
  180. $this->click_button($action);
  181. }
  182. // wait
  183. sleep(TESTS_SLEEP);
  184. // get response
  185. $response = $this->execute(array(
  186. 'script' => "return window.test_ajax_response_object['$action'];",
  187. 'args' => array(),
  188. ));
  189. return $response;
  190. }
  191. }