Skip to content

Commit 7e2c071

Browse files
committed
test(mailbox): refact Mailbox test
1 parent d4516b0 commit 7e2c071

File tree

10 files changed

+1646
-1535
lines changed

10 files changed

+1646
-1535
lines changed

modules/core/hm-mailbox.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public function __construct($server_id, $user_config, $session, $config) {
4646
}
4747
}
4848

49+
/**
50+
* Set connection
51+
* @param object $connection The connection object to inject
52+
*/
53+
public function set_connection($connection) {
54+
$this->connection = $connection;
55+
}
56+
4957
public function connect() {
5058
if (! $this->connection) {
5159
return false;
@@ -563,6 +571,14 @@ public function search($folder, $target='ALL', $terms=array(), $sort=null, $reve
563571
if (! $this->select_folder($folder)) {
564572
return [];
565573
}
574+
575+
// Handle JMAP specifically since it's "IMAP-like" but has different method signatures
576+
if ($this->type === self::TYPE_JMAP) {
577+
// JMAP search uses IMAP-like parameters but handles sorting internally
578+
$uids = $this->connection->search($target, false, $terms, [], $exclude_deleted, $exclude_auto_bcc, $only_auto_bcc);
579+
return $uids;
580+
}
581+
566582
if ($this->is_imap()) {
567583
if ($sort) {
568584
if ($this->connection->is_supported('SORT')) {

modules/imap/hm-ews.php

Lines changed: 955 additions & 953 deletions
Large diffs are not rendered by default.

modules/imap/hm-imap.php

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,63 +19,65 @@
1919
* IMAP connection manager
2020
* @subpackage imap/lib
2121
*/
22-
class Hm_IMAP_List {
22+
if (!class_exists('Hm_IMAP_List')) {
23+
class Hm_IMAP_List {
2324

24-
use Hm_Server_List;
25+
use Hm_Server_List;
2526

26-
public static $use_cache = false;
27-
protected static $user_config;
28-
protected static $session;
27+
public static $use_cache = false;
28+
protected static $user_config;
29+
protected static $session;
2930

30-
public static function init($user_config, $session) {
31-
self::initRepo('imap_servers', $user_config, $session, self::$server_list);
32-
self::$user_config = $user_config;
33-
self::$session = $session;
34-
}
31+
public static function init($user_config, $session) {
32+
self::initRepo('imap_servers', $user_config, $session, self::$server_list);
33+
self::$user_config = $user_config;
34+
self::$session = $session;
35+
}
3536

36-
public static function service_connect($id, $server, $user, $pass, $cache=false) {
37-
$config = array(
38-
'server' => $server['server'],
39-
'port' => $server['port'],
40-
'tls' => $server['tls'],
41-
'type' => array_key_exists('type', $server) ? $server['type'] : 'imap',
42-
'username' => $user,
43-
'password' => $pass,
44-
'use_cache' => self::$use_cache
45-
);
37+
public static function service_connect($id, $server, $user, $pass, $cache=false) {
38+
$config = array(
39+
'server' => $server['server'],
40+
'port' => $server['port'],
41+
'tls' => $server['tls'],
42+
'type' => array_key_exists('type', $server) ? $server['type'] : 'imap',
43+
'username' => $user,
44+
'password' => $pass,
45+
'use_cache' => self::$use_cache
46+
);
4647

47-
if (array_key_exists('auth', $server)) {
48-
$config['auth'] = $server['auth'];
49-
}
48+
if (array_key_exists('auth', $server)) {
49+
$config['auth'] = $server['auth'];
50+
}
5051

51-
self::$server_list[$id]['object'] = new Hm_Mailbox($id, self::$user_config, self::$session, $config);
52-
if (self::$use_cache && $cache && is_array($cache)) {
53-
self::$server_list[$id]['object']->get_connection()->load_cache($cache, 'array');
52+
self::$server_list[$id]['object'] = new Hm_Mailbox($id, self::$user_config, self::$session, $config);
53+
if (self::$use_cache && $cache && is_array($cache)) {
54+
self::$server_list[$id]['object']->get_connection()->load_cache($cache, 'array');
55+
}
56+
57+
return self::$server_list[$id]['object']->connect();
5458
}
55-
56-
return self::$server_list[$id]['object']->connect();
57-
}
5859

59-
public static function get_cache($hm_cache, $id) {
60-
if (!self::$use_cache) {
61-
return false;
60+
public static function get_cache($hm_cache, $id) {
61+
if (!self::$use_cache) {
62+
return false;
63+
}
64+
$res = $hm_cache->get('imap'.$id);
65+
return $res;
6266
}
63-
$res = $hm_cache->get('imap'.$id);
64-
return $res;
65-
}
6667

67-
public static function get_connected_mailbox($id, $hm_cache = null) {
68-
if ($hm_cache) {
69-
$cache = self::get_cache($hm_cache, $id);
70-
} else {
71-
$cache = false;
68+
public static function get_connected_mailbox($id, $hm_cache = null) {
69+
if ($hm_cache) {
70+
$cache = self::get_cache($hm_cache, $id);
71+
} else {
72+
$cache = false;
73+
}
74+
return self::connect($id, $cache);
7275
}
73-
return self::connect($id, $cache);
74-
}
7576

76-
public static function get_mailbox_without_connection($config) {
77-
$config['type'] = array_key_exists('type', $config) ? $config['type'] : 'imap';
78-
return new Hm_Mailbox($config['id'], self::$user_config, self::$session, $config);
77+
public static function get_mailbox_without_connection($config) {
78+
$config['type'] = array_key_exists('type', $config) ? $config['type'] : 'imap';
79+
return new Hm_Mailbox($config['id'], self::$user_config, self::$session, $config);
80+
}
7981
}
8082
}
8183

@@ -178,7 +180,7 @@ class Hm_IMAP extends Hm_IMAP_Cache {
178180
);
179181

180182
/* holds the current IMAP connection state */
181-
private $state = 'disconnected';
183+
public $state = 'disconnected';
182184

183185
/* used for message part content streaming */
184186
private $stream_size = 0;

modules/imap/hm-jmap.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,4 +1359,35 @@ private function get_raw_message_content($blob_id, $name) {
13591359
$this->api->format = 'json';
13601360
return $res;
13611361
}
1362+
1363+
/**
1364+
* Check if a feature is supported (JMAP compatibility method)
1365+
* JMAP doesn't use IMAP extensions, so most features are handled differently
1366+
*/
1367+
public function is_supported($feature) {
1368+
// TODO: Implement more features as needed, but most IMAP features don't have direct JMAP equivalents
1369+
return false;
1370+
}
1371+
1372+
/**
1373+
* Get message sort order (JMAP compatibility method)
1374+
* This provides IMAP-like interface for JMAP sorting
1375+
* JMAP doesn't have direct equivalent to IMAP SORT
1376+
* Fall back to search and let JMAP handle sorting internally
1377+
*/
1378+
public function get_message_sort_order($sort, $reverse=false, $target='ALL', $terms=array(), $exclude_deleted=true, $exclude_auto_bcc=true, $only_auto_bcc=false) {
1379+
return $this->search($target, false, $terms, [], $exclude_deleted, $exclude_auto_bcc, $only_auto_bcc);
1380+
}
1381+
1382+
/**
1383+
* Sort by fetch (JMAP compatibility method)
1384+
* JMAP doesn't need this since it handles sorting differently
1385+
*/
1386+
public function sort_by_fetch($sort, $reverse=false, $target='ALL', $uids='') {
1387+
// TODO: implement according to JMAP spec if needed
1388+
if (empty($uids)) {
1389+
return [];
1390+
}
1391+
return explode(',', $uids);
1392+
}
13621393
}

modules/smtp/hm-smtp.php

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,54 @@
1010
* SMTP connection manager
1111
* @subpackage smtp/lib
1212
*/
13-
class Hm_SMTP_List {
13+
if (!class_exists('Hm_SMTP_List')) {
14+
class Hm_SMTP_List {
1415

15-
use Hm_Server_List;
16+
use Hm_Server_List;
1617

17-
protected static $user_config;
18-
protected static $session;
18+
protected static $user_config;
19+
protected static $session;
1920

20-
public static function init($user_config, $session) {
21-
self::initRepo('smtp_servers', $user_config, $session, self::$server_list);
22-
self::$user_config = $user_config;
23-
self::$session = $session;
24-
}
21+
public static function init($user_config, $session) {
22+
self::initRepo('smtp_servers', $user_config, $session, self::$server_list);
23+
self::$user_config = $user_config;
24+
self::$session = $session;
25+
}
2526

26-
public static function service_connect($id, $server, $user, $pass, $cache=false) {
27-
$config = array(
28-
'id' => $id,
29-
'server' => $server['server'],
30-
'port' => $server['port'],
31-
'tls' => $server['tls'],
32-
'username' => $user,
33-
'password' => $pass,
34-
'type' => array_key_exists('type', $server) && !empty($server['type']) ? $server['type'] : 'smtp',
35-
);
36-
if (array_key_exists('auth', $server)) {
37-
$config['auth'] = $server['auth'];
38-
}
39-
if (array_key_exists('no_auth', $server)) {
40-
$config['no_auth'] = true;
41-
}
42-
self::$server_list[$id]['object'] = new Hm_Mailbox($id, self::$user_config, self::$session, $config);
43-
if (! self::$server_list[$id]['object']->connect()) {
44-
return self::$server_list[$id]['object'];
45-
}
46-
return false;
47-
}
27+
public static function service_connect($id, $server, $user, $pass, $cache=false) {
28+
$config = array(
29+
'id' => $id,
30+
'server' => $server['server'],
31+
'port' => $server['port'],
32+
'tls' => $server['tls'],
33+
'username' => $user,
34+
'password' => $pass,
35+
'type' => array_key_exists('type', $server) && !empty($server['type']) ? $server['type'] : 'smtp',
36+
);
37+
if (array_key_exists('auth', $server)) {
38+
$config['auth'] = $server['auth'];
39+
}
40+
if (array_key_exists('no_auth', $server)) {
41+
$config['no_auth'] = true;
42+
}
43+
self::$server_list[$id]['object'] = new Hm_Mailbox($id, self::$user_config, self::$session, $config);
44+
if (! self::$server_list[$id]['object']->connect()) {
45+
return self::$server_list[$id]['object'];
46+
}
47+
return false;
48+
}
4849

49-
public static function get_cache($session, $id) {
50-
return false;
51-
}
50+
public static function get_cache($session, $id) {
51+
return false;
52+
}
5253

53-
public static function address_list() {
54-
$addrs = array();
55-
foreach (self::$server_list as $server) {
56-
$addrs[] = $server['user'];
54+
public static function address_list() {
55+
$addrs = array();
56+
foreach (self::$server_list as $server) {
57+
$addrs[] = $server['user'];
58+
}
59+
return $addrs;
5760
}
58-
return $addrs;
5961
}
6062
}
6163

tests/phpunit/bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
/* get mock objects */
2828
require APP_PATH.'tests/phpunit/mocks.php';
2929

30+
31+
/* get the imap|jmap|ews|smtp classes */
32+
require_once APP_PATH.'modules/imap/hm-imap.php';
33+
require_once APP_PATH.'modules/imap/hm-jmap.php';
34+
require_once APP_PATH.'modules/imap/hm-ews.php';
35+
require_once APP_PATH.'modules/smtp/hm-smtp.php';
36+
require_once APP_PATH.'modules/core/hm-mailbox.php';
37+
3038
/* get the stubs */
3139
require APP_PATH.'tests/phpunit/stubs.php';
3240

0 commit comments

Comments
 (0)