| | |
| | | if (php_sapi_name() != 'cli') |
| | | die("Not in shell mode (php-cli)"); |
| | | |
| | | if (!defined('INSTALL_PATH')) define('INSTALL_PATH', realpath(dirname(__FILE__) . '/../../') . '/' ); |
| | | if (!defined('INSTALL_PATH')) define('INSTALL_PATH', realpath(__DIR__ . '/../../') . '/' ); |
| | | |
| | | define('TESTS_DIR', dirname(__FILE__) . '/'); |
| | | define('TESTS_DIR', realpath(__DIR__ . '/../') . '/'); |
| | | |
| | | if (@is_dir(TESTS_DIR . 'config')) { |
| | | define('RCUBE_CONFIG_DIR', TESTS_DIR . 'config'); |
| | |
| | | */ |
| | | class bootstrap |
| | | { |
| | | static $imap_ready = null; |
| | | |
| | | /** |
| | | * Wipe and re-initialize (mysql) database |
| | | */ |
| | | public static function init_db() |
| | | { |
| | | $rcmail = rcmail::get_instance(); |
| | | $dsn = rcube_db::parse_dsn($rcmail->config->get('db_dsnw')); |
| | | |
| | | // drop all existing tables first |
| | | $db = $rcmail->get_dbh(); |
| | | $db->query("SET FOREIGN_KEY_CHECKS=0"); |
| | | $sql_res = $db->query("SHOW TABLES"); |
| | | while ($sql_arr = $db->fetch_array($sql_res)) { |
| | | $table = reset($sql_arr); |
| | | $db->query("DROP TABLE $table"); |
| | | } |
| | | if ($dsn['phptype'] == 'mysql' || $dsn['phptype'] == 'mysqli') { |
| | | // drop all existing tables first |
| | | $db = $rcmail->get_dbh(); |
| | | $db->query("SET FOREIGN_KEY_CHECKS=0"); |
| | | $sql_res = $db->query("SHOW TABLES"); |
| | | while ($sql_arr = $db->fetch_array($sql_res)) { |
| | | $table = reset($sql_arr); |
| | | $db->query("DROP TABLE $table"); |
| | | } |
| | | |
| | | // init database with schema |
| | | $dsn = parse_url($rcmail->config->get('db_dsnw')); |
| | | $db_name = trim($dsn['path'], '/'); |
| | | |
| | | if ($dsn['scheme'] == 'mysql' || $dsn['scheme'] == 'mysqli') { |
| | | // init database with schema |
| | | system(sprintf('cat %s %s | mysql -h %s -u %s --password=%s %s', |
| | | realpath(INSTALL_PATH . '/SQL/mysql.initial.sql'), |
| | | realpath(TESTS_DIR . '/Selenium/data/mysql.sql'), |
| | | realpath(TESTS_DIR . 'Selenium/data/mysql.sql'), |
| | | escapeshellarg($dsn['host']), |
| | | escapeshellarg($dsn['user']), |
| | | escapeshellarg($dsn['pass']), |
| | | escapeshellarg($db_name) |
| | | escapeshellarg($dsn['database']) |
| | | )); |
| | | } |
| | | else if ($dsn['phptype'] == 'sqlite') { |
| | | // delete database file -- will be re-initialized on first access |
| | | system(sprintf('rm -f %s', escapeshellarg($dsn['database']))); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | public static function init_imap() |
| | | { |
| | | if (!TESTS_USER) |
| | | if (!TESTS_USER) { |
| | | return false; |
| | | } |
| | | else if (self::$imap_ready !== null) { |
| | | return self::$imap_ready; |
| | | } |
| | | |
| | | // TBD. |
| | | self::connect_imap(TESTS_USER, TESTS_PASS); |
| | | self::purge_mailbox('INBOX'); |
| | | self::ensure_mailbox('Archive', true); |
| | | |
| | | return self::$imap_ready; |
| | | } |
| | | |
| | | /** |
| | | * Authenticate to IMAP with the given credentials |
| | | */ |
| | | public static function connect_imap($username, $password, $host = null) |
| | | { |
| | | $rcmail = rcmail::get_instance(); |
| | | $imap = $rcmail->get_storage(); |
| | | |
| | | if ($imap->is_connected()) { |
| | | $imap->close(); |
| | | self::$imap_ready = false; |
| | | } |
| | | |
| | | $imap_host = $host ?: $rcmail->config->get('default_host'); |
| | | $a_host = parse_url($imap_host); |
| | | if ($a_host['host']) { |
| | | $imap_host = $a_host['host']; |
| | | $imap_ssl = isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls')); |
| | | $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143); |
| | | } |
| | | else { |
| | | $imap_port = 143; |
| | | $imap_ssl = false; |
| | | } |
| | | |
| | | if (!$imap->connect($imap_host, $username, $password, $imap_port, $imap_ssl)) { |
| | | die("IMAP error: unable to authenticate with user " . TESTS_USER); |
| | | } |
| | | |
| | | self::$imap_ready = true; |
| | | } |
| | | |
| | | /** |
| | | * Import the given file into IMAP |
| | | */ |
| | | public static function import_message($filename, $mailbox = 'INBOX') |
| | | { |
| | | if (!self::init_imap()) { |
| | | die(__METHOD__ . ': IMAP connection unavailable'); |
| | | } |
| | | |
| | | $imap = rcmail::get_instance()->get_storage(); |
| | | $imap->save_message($mailbox, file_get_contents($filename)); |
| | | } |
| | | |
| | | /** |
| | | * Delete all messages from the given mailbox |
| | | */ |
| | | public static function purge_mailbox($mailbox) |
| | | { |
| | | if (!self::init_imap()) { |
| | | die(__METHOD__ . ': IMAP connection unavailable'); |
| | | } |
| | | |
| | | $imap = rcmail::get_instance()->get_storage(); |
| | | $imap->delete_message('*', $mailbox); |
| | | } |
| | | |
| | | /** |
| | | * Make sure the given mailbox exists in IMAP |
| | | */ |
| | | public static function ensure_mailbox($mailbox, $empty = false) |
| | | { |
| | | if (!self::init_imap()) { |
| | | die(__METHOD__ . ': IMAP connection unavailable'); |
| | | } |
| | | |
| | | $imap = rcmail::get_instance()->get_storage(); |
| | | |
| | | $folders = $imap->list_folders(); |
| | | if (!in_array($mailbox, $folders)) { |
| | | $imap->create_folder($mailbox, true); |
| | | } |
| | | else if ($empty) { |
| | | $imap->delete_message('*', $mailbox); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | class Selenium_Test extends PHPUnit_Extensions_Selenium2TestCase |
| | | { |
| | | protected $login_data = null; |
| | | |
| | | protected function setUp() |
| | | { |
| | | $this->setBrowser(TESTS_BROWSER); |
| | | $this->login_data = array(TESTS_USER, TESTS_PASS); |
| | | |
| | | // Set root to our index.html, for better performance |
| | | // See https://github.com/sebastianbergmann/phpunit-selenium/issues/217 |
| | |
| | | $this->setBrowserUrl($baseurl . '/tests/Selenium'); |
| | | } |
| | | |
| | | protected function login() |
| | | protected function login($username = null, $password = null) |
| | | { |
| | | $this->go('mail'); |
| | | if (!empty($username)) { |
| | | $this->login_data = array($username, $password); |
| | | } |
| | | |
| | | $this->go('mail', null, true); |
| | | } |
| | | |
| | | protected function do_login() |
| | | { |
| | | $user_input = $this->byCssSelector('form input[name="_user"]'); |
| | | $pass_input = $this->byCssSelector('form input[name="_pass"]'); |
| | | $submit = $this->byCssSelector('form input[type="submit"]'); |
| | | |
| | | $user_input->value(TESTS_USER); |
| | | $pass_input->value(TESTS_PASS); |
| | | $user_input->value($this->login_data[0]); |
| | | $pass_input->value($this->login_data[1]); |
| | | |
| | | // submit login form |
| | | $submit->click(); |
| | |
| | | sleep(TESTS_SLEEP); |
| | | } |
| | | |
| | | protected function go($task = 'mail', $action = null) |
| | | protected function go($task = 'mail', $action = null, $login = true) |
| | | { |
| | | $this->url(TESTS_URL . '?_task=' . $task); |
| | | |
| | | // wait for interface load (initial ajax requests, etc.) |
| | | sleep(TESTS_SLEEP); |
| | | |
| | | // check if we have a valid session |
| | | $env = $this->get_env(); |
| | | if ($login && $env['task'] == 'login') { |
| | | $this->do_login(); |
| | | } |
| | | |
| | | if ($action) { |
| | | $this->click_button($action); |
| | | |
| | | sleep(TESTS_SLEEP); |
| | | } |
| | | } |
| | |
| | | protected function get_env() |
| | | { |
| | | return $this->execute(array( |
| | | 'script' => 'return rcmail.env;', |
| | | 'script' => 'return window.rcmail ? rcmail.env : {};', |
| | | 'args' => array(), |
| | | )); |
| | | } |
| | |
| | | |
| | | // get response |
| | | $response = $this->execute(array( |
| | | 'script' => "return window.test_ajax_response_object['$action'];", |
| | | 'script' => "return window.test_ajax_response_object ? test_ajax_response_object['$action'] : {};", |
| | | 'args' => array(), |
| | | )); |
| | | |
| | | return $response; |
| | | } |
| | | |
| | | protected function assertHasClass($classname, $element) |
| | | { |
| | | $this->assertContains($classname, $element->attribute('class')); |
| | | } |
| | | } |