Make quota test work again
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 30 Jun 2010 22:35:55 +0000 (16:35 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 30 Jun 2010 22:35:55 +0000 (16:35 -0600)
Inject all needed dependencies into IMP_Quota drivers to accomplish
this.

imp/lib/Injector/Binder/Quota.php
imp/lib/Quota/Imap.php
imp/lib/Quota/Maildir.php
imp/lib/tests/quota_maildir.phpt

index b2ba81c..5dd3213 100644 (file)
@@ -30,6 +30,19 @@ class IMP_Injector_Binder_Quota implements Horde_Injector_Binder
             $params['password'] = $secret->read($secret->getKey('imp'), $params['password']);
         }
 
+        switch (Horde_String::lower($driver)) {
+        case 'imap':
+            $params['imap_ob'] = $GLOBALS['injector']->getInstance('IMP_Imap')->getOb();
+            $params['mbox'] = $GLOBALS['injector']->getInstance('IMP_Search')->isSearchMbox(IMP::$mailbox)
+                ? 'INBOX'
+                : IMP::$mailbox;
+            break;
+
+        case 'maildir':
+            $params['username'] = $GLOBALS['injector']->getInstance('IMP_Imap')->getOb()->getParam('username');
+            break;
+        }
+
         return IMP_Quota::factory($driver, $params);
     }
 
index 8e0ad2a..de52e90 100644 (file)
 class IMP_Quota_Imap extends IMP_Quota_Driver
 {
     /**
+     * Constructor.
+     *
+     * @param array $params  Parameters:
+     * <pre>
+     * 'imap_ob' - (Horde_Imap_Client_Base) IMAP client object.
+     * 'mbox' - (string) IMAP mailbox to query.
+     * </pre>
+     *
+     * @throws InvalidArgumentException
+     */
+    public function __construct(array $params = array())
+    {
+        foreach (array('imap_ob', 'mbox') as $val) {
+            if (!isset($params[$val])) {
+                throw new InvalidArgumentException('Missing ' . $val . ' parameter');
+            }
+        }
+
+        parent::__construct($params);
+    }
+
+    /**
      * Get quota information (used/allocated), in bytes.
      *
      * @return array  An array with the following keys:
@@ -25,7 +47,7 @@ class IMP_Quota_Imap extends IMP_Quota_Driver
     public function getQuota()
     {
         try {
-            $quota = $GLOBALS['injector']->getInstance('IMP_Imap')->getOb()->getQuotaRoot($GLOBALS['injector']->getInstance('IMP_Search')->isSearchMbox(IMP::$mailbox) ? 'INBOX' : IMP::$mailbox);
+            $quota = $this->_params['imap_ob']->getQuotaRoot($this->_params['mbox']);
         } catch (Horde_Imap_Client_Exception $e) {
             throw new IMP_Exception(_("Unable to retrieve quota"));
         }
index c841607..03aa4ec 100644 (file)
@@ -28,12 +28,16 @@ class IMP_Quota_Maildir extends IMP_Quota_Driver
      *          account name, and the actual username will be substituted in
      *          that location.
      *          E.g., '/home/~U/Maildir/' or '/var/mail/~U/Maildir/'
+     *          DEFAULT: ''
+     * 'username' - (string) Username to substitute into the string.
+     *              DEFAULT: none
      * </pre>
      */
     public function __construct($params = array())
     {
         parent::__construct(array_merge(array(
-            'path' => ''
+            'path' => '',
+            'username' => ''
         ), $params));
     }
 
@@ -53,7 +57,7 @@ class IMP_Quota_Maildir extends IMP_Quota_Driver
         $full = $this->_params['path'] . '/maildirsize';
 
         // Substitute the username in the string if needed.
-        $full = str_replace('~U', $GLOBALS['injector']->getInstance('IMP_Imap')->getOb()->getParam('username'), $full);
+        $full = str_replace('~U', $this->_params['username'], $full);
 
         // Read in the quota file and parse it, if possible.
         if (!is_file($full)) {
index 83c1586..ff0932a 100644 (file)
@@ -3,15 +3,21 @@ IMP_Quota_maildir test.
 --FILE--
 <?php
 
-$_SESSION['imp']['user'] = null;
-require_once dirname(__FILE__) . '/../Quota.php';
-$quota = IMP_Quota::factory('maildir',
-                            array('path' => dirname(__FILE__) . '/fixtures'));
+require_once dirname(__FILE__) . '/../Application.php';
+Horde_Registry::appInit('imp', array(
+    'authentication' => 'none',
+    'cli' => true
+));
+
+$quota = IMP_Quota::factory('Maildir', array(
+    'path' => dirname(__FILE__) . '/fixtures'
+));
+
 var_export($quota->getQuota());
 
 ?>
 --EXPECT--
 array (
-  'usage' => 550839239,
   'limit' => 1000000000,
+  'usage' => 550839239,
 )