From f7c5f44ffbfa793f12a0a873d2e6e172fee7a249 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Thu, 7 May 2015 17:42:18 +0200 Subject: [PATCH] Populate IMAP test account with sample message and test its listing in Selenium tests --- tests/Selenium/Login.php | 1 + tests/Selenium/Mail/List.php | 16 +++++++- tests/Selenium/Settings/About.php | 3 +- tests/Selenium/bootstrap.php | 65 ++++++++++++++++++++++++++---- tests/Selenium/data/mail/lines.eml | 45 +++++++++++++++++++++ tests/Selenium/phpunit.xml | 16 ++++++-- 6 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 tests/Selenium/data/mail/lines.eml diff --git a/tests/Selenium/Login.php b/tests/Selenium/Login.php index 65b082851..6910b43e6 100644 --- a/tests/Selenium/Login.php +++ b/tests/Selenium/Login.php @@ -5,6 +5,7 @@ class Selenium_Login extends Selenium_Test protected function setUp() { bootstrap::init_db(); + bootstrap::init_imap(); parent::setUp(); } diff --git a/tests/Selenium/Mail/List.php b/tests/Selenium/Mail/List.php index 7574c1801..9c1d4a8d8 100644 --- a/tests/Selenium/Mail/List.php +++ b/tests/Selenium/Mail/List.php @@ -2,7 +2,7 @@ class Selenium_Mail_List extends Selenium_Test { - public function testCheckRecent() + public function testList() { $this->go('mail'); @@ -13,7 +13,6 @@ class Selenium_Mail_List extends Selenium_Test $this->assertRegExp('/this\.set_unread_count/', $res['exec']); $this->assertRegExp('/this\.set_rowcount/', $res['exec']); $this->assertRegExp('/this\.set_message_coltypes/', $res['exec']); -// $this->assertRegExp('/this\.add_message_row/', $res['exec']); $this->assertContains('current_page', $res['env']); $this->assertContains('exists', $res['env']); @@ -21,5 +20,18 @@ class Selenium_Mail_List extends Selenium_Test $this->assertContains('pagesize', $res['env']); $this->assertContains('messagecount', $res['env']); $this->assertContains('mailbox', $res['env']); + + $this->assertEquals($res['env']['mailbox'], 'INBOX'); + $this->assertEquals($res['env']['messagecount'], 1); + + // check message list + $row = $this->byCssSelector('.messagelist tbody tr:first-child'); + $this->assertHasClass('unread', $row); + + $subject = $this->byCssSelector('.messagelist tbody tr:first-child td.subject'); + $this->assertEquals('Lines', $subject->text()); + + $icon = $this->byCssSelector('.messagelist tbody tr:first-child td.status span'); + $this->assertHasClass('unread', $icon); } } diff --git a/tests/Selenium/Settings/About.php b/tests/Selenium/Settings/About.php index 9a6c31d4b..4cd49431a 100644 --- a/tests/Selenium/Settings/About.php +++ b/tests/Selenium/Settings/About.php @@ -4,7 +4,8 @@ class Selenium_Settings_About extends Selenium_Test { public function testAbout() { - $this->url(TESTS_URL . '/?_task=settings&_action=about'); + $this->url(TESTS_URL . '?_task=settings&_action=about'); + sleep(TESTS_SLEEP); // check task and action $env = $this->get_env(); diff --git a/tests/Selenium/bootstrap.php b/tests/Selenium/bootstrap.php index 124a81ef2..fe2d11156 100644 --- a/tests/Selenium/bootstrap.php +++ b/tests/Selenium/bootstrap.php @@ -24,7 +24,7 @@ if (php_sapi_name() != 'cli') if (!defined('INSTALL_PATH')) define('INSTALL_PATH', realpath(__DIR__ . '/../../') . '/' ); -define('TESTS_DIR', __DIR__ . '/'); +define('TESTS_DIR', realpath(__DIR__ . '/../') . '/'); if (@is_dir(TESTS_DIR . 'config')) { define('RCUBE_CONFIG_DIR', TESTS_DIR . 'config'); @@ -77,7 +77,7 @@ class bootstrap if ($dsn['scheme'] == 'mysql' || $dsn['scheme'] == 'mysqli') { 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']), @@ -94,7 +94,43 @@ class bootstrap if (!TESTS_USER) return false; - // TBD. + $rcmail = rcmail::get_instance(); + $imap = $rcmail->get_storage(); + + $imap_host = $rcmail->config->get('default_host'); + $a_host = parse_url($args['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, TESTS_USER, TESTS_PASS, $imap_port, $imap_ssl)) { + die("IMAP error: unable to authenticate with user " . TESTS_USER); + } + + // create Archive mailbox + $folders = $imap->list_folders(); + if (!in_array('Archive', $folders)) { + $imap->create_folder('Archive', true); + } + else { + $imap->delete_message('*', 'Archive'); + } + + // empty Inbox + $imap->delete_message('*', 'INBOX'); + + // import email messages + foreach (glob(TESTS_DIR . 'Selenium/data/mail/*.eml') as $f) { + $imap->save_message('INBOX', file_get_contents($f)); + } + + $imap->close(); } } @@ -118,8 +154,11 @@ class Selenium_Test extends PHPUnit_Extensions_Selenium2TestCase protected function login() { - $this->go('mail'); + $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"]'); @@ -134,16 +173,21 @@ class Selenium_Test extends PHPUnit_Extensions_Selenium2TestCase 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); } } @@ -151,7 +195,7 @@ class Selenium_Test extends PHPUnit_Extensions_Selenium2TestCase protected function get_env() { return $this->execute(array( - 'script' => 'return rcmail.env;', + 'script' => 'return window.rcmail ? rcmail.env : {};', 'args' => array(), )); } @@ -225,10 +269,15 @@ class Selenium_Test extends PHPUnit_Extensions_Selenium2TestCase // 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')); + } } diff --git a/tests/Selenium/data/mail/lines.eml b/tests/Selenium/data/mail/lines.eml new file mode 100644 index 000000000..e5977d4fc --- /dev/null +++ b/tests/Selenium/data/mail/lines.eml @@ -0,0 +1,45 @@ +Return-Path: +X-Original-To: tb@tester.local +To: Tom Tester +Subject: Lines +X-PHP-Originating-Script: 501:rcmail.php +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="=_8853bfb47b7da1852ac882e69cc724f3" +Date: Fri, 23 May 2014 19:44:50 +0200 +From: "Thomas B." +Reply-To: hello@roundcube.net +Mail-Reply-To: hello@roundcube.net +Message-ID: <99839b8ec12482419372f1edafa9de75@woodcrest.local> +X-Sender: thomas@roundcube.net +User-Agent: Roundcube Webmail/1.0.1 + +--=_8853bfb47b7da1852ac882e69cc724f3 +Content-Transfer-Encoding: 7bit +Content-Type: text/plain; charset=UTF-8; + format=flowed + +Plain text message body. + +-- +Developer of Free Software +Sent with Roundcube Webmail - roundcube.net +--=_8853bfb47b7da1852ac882e69cc724f3 +Content-Transfer-Encoding: base64 +Content-Type: text/plain; + name=lines.txt +Content-Disposition: attachment; + filename=lines.txt; + size=13 + +Zm9vDQpiYXINCmduYQ== +--=_8853bfb47b7da1852ac882e69cc724f3 +Content-Transfer-Encoding: base64 +Content-Type: text/plain; + name=lines_lf.txt +Content-Disposition: attachment; + filename=lines_lf.txt; + size=11 + +Zm9vCmJhcgpnbmE= +--=_8853bfb47b7da1852ac882e69cc724f3-- diff --git a/tests/Selenium/phpunit.xml b/tests/Selenium/phpunit.xml index b5835cf74..fe0c7016c 100644 --- a/tests/Selenium/phpunit.xml +++ b/tests/Selenium/phpunit.xml @@ -2,20 +2,28 @@ bootstrap="bootstrap.php" colors="true"> - + Login.php - Addressbook/Addressbook.php - Addressbook/Import.php Mail/Mail.php Mail/CheckRecent.php Mail/Compose.php Mail/Getunread.php Mail/List.php + Logout.php + + + Login.php + Addressbook/Addressbook.php + Addressbook/Import.php + Logout.php + + + Login.php Settings/About.php Settings/Folders.php Settings/Identities.php Settings/Settings.php - Logout.php + Logout.php