From 28b1cb04b619707f61e21cb2e6ab21d4bff0ae5f Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Mon, 11 May 2015 08:37:30 -0400
Subject: [PATCH] Add more utility functions to prepare the IMAP account used for testing

---
 tests/Selenium/bootstrap.php         |   73 ++++++++++++++++++++++++++++--------
 tests/Selenium/data/mail/list_00.eml |    0 
 tests/Selenium/Mail/Getunread.php    |   26 ++++++++++++
 tests/Selenium/Mail/List.php         |   13 ++++++
 4 files changed, 95 insertions(+), 17 deletions(-)

diff --git a/tests/Selenium/Mail/Getunread.php b/tests/Selenium/Mail/Getunread.php
index d6362f2..16b6bd1 100644
--- a/tests/Selenium/Mail/Getunread.php
+++ b/tests/Selenium/Mail/Getunread.php
@@ -2,12 +2,36 @@
 
 class Selenium_Mail_Getunread extends Selenium_Test
 {
+    protected $msgcount = 0;
+
+    protected function setUp()
+    {
+        parent::setUp();
+
+        bootstrap::init_imap();
+        bootstrap::purge_mailbox('INBOX');
+
+        // import email messages
+        foreach (glob(TESTS_DIR . 'Selenium/data/mail/list_*.eml') as $f) {
+            bootstrap::import_message($f, 'INBOX');
+            $this->msgcount++;
+        }
+    }
+
     public function testGetunread()
     {
         $this->go('mail');
 
         $res = $this->ajaxResponse('getunread', "rcmail.http_request('getunread')");
-
         $this->assertEquals('getunread', $res['action']);
+
+        $env = $this->get_env();
+        $this->assertEquals($env['unread_counts']['INBOX'], $this->msgcount);
+
+        $li = $this->byCssSelector('.folderlist li.inbox');
+        $this->assertHasClass('unread', $li);
+
+        $badge = $this->byCssSelector('.folderlist li.inbox span.unreadcount');
+        $this->assertEquals(strval($this->msgcount), $badge->text());
     }
 }
diff --git a/tests/Selenium/Mail/List.php b/tests/Selenium/Mail/List.php
index 9c1d4a8..88fdb25 100644
--- a/tests/Selenium/Mail/List.php
+++ b/tests/Selenium/Mail/List.php
@@ -2,6 +2,19 @@
 
 class Selenium_Mail_List extends Selenium_Test
 {
+    protected function setUp()
+    {
+        parent::setUp();
+
+        bootstrap::init_imap();
+        bootstrap::purge_mailbox('INBOX');
+
+        // import email messages
+        foreach (glob(TESTS_DIR . 'Selenium/data/mail/list_00.eml') as $f) {
+            bootstrap::import_message($f, 'INBOX');
+        }
+    }
+
     public function testList()
     {
         $this->go('mail');
diff --git a/tests/Selenium/bootstrap.php b/tests/Selenium/bootstrap.php
index fe2d111..dfc9c67 100644
--- a/tests/Selenium/bootstrap.php
+++ b/tests/Selenium/bootstrap.php
@@ -54,6 +54,8 @@
  */
 class bootstrap
 {
+    static $imap_ready = null;
+
     /**
      * Wipe and re-initialize (mysql) database
      */
@@ -91,8 +93,12 @@
      */
     public static function init_imap()
     {
-        if (!TESTS_USER)
+        if (!TESTS_USER) {
             return false;
+        }
+        else if (self::$imap_ready !== null) {
+            return self::$imap_ready;
+        }
 
         $rcmail = rcmail::get_instance();
         $imap = $rcmail->get_storage();
@@ -110,27 +116,62 @@
         }
 
         if (!$imap->connect($imap_host, TESTS_USER, TESTS_PASS, $imap_port, $imap_ssl)) {
+            self::$imap_ready = false;
             die("IMAP error: unable to authenticate with user " . TESTS_USER);
         }
 
-        // create Archive mailbox
+        self::$imap_ready = true;
+
+        self::purge_mailbox('INBOX');
+        self::ensure_mailbox('Archive', true);
+
+        return self::$imap_ready;
+    }
+
+    /**
+     * 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('Archive', $folders)) {
-            $imap->create_folder('Archive', true);
+        if (!in_array($mailbox, $folders)) {
+            $imap->create_folder($mailbox, true);
         }
-        else {
-            $imap->delete_message('*', 'Archive');
+        else if ($empty) {
+            $imap->delete_message('*', $mailbox);
         }
-
-        // 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();
     }
 }
 
diff --git a/tests/Selenium/data/mail/lines.eml b/tests/Selenium/data/mail/list_00.eml
similarity index 100%
rename from tests/Selenium/data/mail/lines.eml
rename to tests/Selenium/data/mail/list_00.eml

--
Gitblit v1.9.1