Skip to content

Commit

Permalink
Add basic test coverage for message_compose hook. (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
r3c authored Sep 28, 2024
1 parent 4beefb3 commit 28e2549
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 16 deletions.
2 changes: 1 addition & 1 deletion custom_from.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private static function get_configuration(rcmail $rcmail)
}

// Read "rules" parameter from global configuration & preferences if allowed
$rules_config = $rcmail->config->get('custom_from_header_rules', 'bcc=p;cc=p;from=p;to=p;x-original-to=p');
$rules_config = $rcmail->config->get('custom_from_header_rules', 'bcc=ep;cc=ep;from=ep;to=ep;x-original-to=ep');
$rules = array();

foreach (explode(';', $rules_config) as $pair) {
Expand Down
41 changes: 38 additions & 3 deletions tests/CustomFromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,51 @@ public static function storage_init_should_fetch_headers_provider(): array
#[DataProvider('storage_init_should_fetch_headers_provider')]
public function test_storage_init_should_fetch_headers($config_values, $user_prefs, $expected): void
{
$plugin = self::create_plugin($config_values, $user_prefs);
$rcmail = rcmail::mock();
$rcmail->mock_config($config_values);
$rcmail->mock_user(array(), $user_prefs);

$plugin = self::create_plugin();
$params = $plugin->storage_init(array());

$this->assertSame($params['fetch_headers'], $expected);
}

private static function create_plugin($config_values, $user_prefs)
public static function message_compose_should_set_state_provider(): array
{
rcmail::mock_instance($config_values, $user_prefs);
return array(
array(
array('to' => '[email protected]'),
null,
),
array(
array('to' => '[email protected]'),
'[email protected]',
)
);
}

#[DataProvider('message_compose_should_set_state_provider')]
public function test_message_compose_should_set_state($message, $expected): void
{
$identity1 = array('email' => '[email protected]', 'name' => 'Primary', 'standard' => '1');
$identity2 = array('email' => '[email protected]', 'name' => 'Secondary', 'standard' => '0');

$id = 17;
$uid = '42';
$rcmail = rcmail::mock();
$rcmail->mock_config(array());
$rcmail->mock_message($uid, $message);
$rcmail->mock_user(array($identity1, $identity2), array());

$plugin = self::create_plugin();
$plugin->message_compose(array('id' => $id, 'param' => array('uid' => $uid)));

$this->assertSame($_SESSION["custom_from_$id"], $expected);
}

private static function create_plugin()
{
$plugin = new custom_from(null);
$plugin->init();

Expand Down
79 changes: 67 additions & 12 deletions tests/rcmail_mock.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
<?php

function format_email_recipient($email)
{
return $email;
}

class rcmail
{
private static rcmail | null $instance = null;
private static rcmail $instance;

public static function get_instance()
{
return self::$instance;
}

public static function mock_instance(array $config_values, array $user_prefs)
public static function mock()
{
self::$instance = new self();

return self::$instance;
}

public $config = null;
public $messages = array();
public $user = null;

public function get_message($id)
{
return $this->messages[$id];
}

public function get_storage()
{
return $this;
}

public function mock_config($config_values)
{
$rcmail = new self();
$rcmail->config = new rcube_config($config_values);
$rcmail->user = new rcube_user($user_prefs);
$this->config = new rcube_config($config_values);
}

self::$instance = $rcmail;
public function mock_message($id, $message_fields)
{
$this->messages[$id] = new rcube_message($message_fields);
}

public $config;
public $user;
public function mock_user($identities, $prefs)
{
$this->user = new rcube_user($identities, $prefs);
}
}

class rcube_config
Expand All @@ -37,13 +66,21 @@ public function get($name, $def = null)
}
}

class rcube_user
class rcube_message
{
public array $prefs;
public string $to;

public function __construct($prefs)
public function __construct($fields)
{
$this->prefs = $prefs;
$this->to = $fields['to'];
}
}

class rcube_mime
{
public static function decode_address_list($address)
{
return array(array('mailto' => $address, 'name' => $address));
}
}

Expand All @@ -53,3 +90,21 @@ public function add_texts() {}
public function add_hook() {}
public function load_config() {}
}

class rcube_user
{
public array $prefs;

private array $identities;

public function __construct($identities, $prefs)
{
$this->identities = $identities;
$this->prefs = $prefs;
}

public function list_identities()
{
return $this->identities;
}
}

0 comments on commit 28e2549

Please sign in to comment.