Aleksander Machniak
2016-02-05 bd0551b22076b82a6d49e9f7a2b2e0c90a1b2326
tests/Selenium/bootstrap.php
@@ -38,7 +38,7 @@
    die("Fatal error: ini_set/set_include_path does not work.");
}
$rcmail = rcmail::get_instance('test');
$rcmail = rcmail::get_instance(0, 'test');
define('TESTS_URL',     $rcmail->config->get('tests_url'));
define('TESTS_BROWSER', $rcmail->config->get('tests_browser', 'firefox'));
@@ -62,29 +62,31 @@
    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'),
                escapeshellarg($dsn['host']),
                escapeshellarg($dsn['user']),
                escapeshellarg($dsn['pass']),
                escapeshellarg($db_name)
                escapeshellarg($dsn['hostspec']),
                escapeshellarg($dsn['username']),
                escapeshellarg($dsn['password']),
                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'])));
        }
    }
@@ -100,14 +102,31 @@
            return self::$imap_ready;
        }
        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();
        $imap_host = $rcmail->config->get('default_host');
        $a_host = parse_url($args['host']);
        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_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 {
@@ -115,17 +134,11 @@
            $imap_ssl = false;
        }
        if (!$imap->connect($imap_host, TESTS_USER, TESTS_PASS, $imap_port, $imap_ssl)) {
            self::$imap_ready = 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;
        self::purge_mailbox('INBOX');
        self::ensure_mailbox('Archive', true);
        return self::$imap_ready;
    }
    /**
@@ -183,9 +196,12 @@
 */
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
@@ -193,8 +209,12 @@
        $this->setBrowserUrl($baseurl . '/tests/Selenium');
    }
    protected function login()
    protected function login($username = null, $password = null)
    {
        if (!empty($username)) {
            $this->login_data = array($username, $password);
        }
        $this->go('mail', null, true);
    }
@@ -204,8 +224,8 @@
        $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();
@@ -317,6 +337,11 @@
        return $response;
    }
    protected function getText($element)
    {
        return $element->text() ?: $element->attribute('textContent');
    }
    protected function assertHasClass($classname, $element)
    {
        $this->assertContains($classname, $element->attribute('class'));