Skip to content

Commit

Permalink
Improve tests on message_compose hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
r3c committed Sep 29, 2024
1 parent cf122f5 commit dc9da33
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 33 deletions.
2 changes: 1 addition & 1 deletion custom_from.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private static function get_configuration(rcmail $rcmail)
$use_preference = !$rcmail->config->get('custom_from_preference_disable', false);

// Read "contains" parameter from global configuration & preferences if allowed
$contains = $rcmail->config->get('custom_from_compose_contains', '');
$contains = $rcmail->config->get(self::PREFERENCE_COMPOSE_CONTAINS, '');

if ($use_preference) {
$contains = self::get_preference($rcmail, self::PREFERENCE_COMPOSE_CONTAINS, $contains);
Expand Down
130 changes: 98 additions & 32 deletions tests/CustomFromTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,37 @@

final class CustomFromTest extends TestCase
{
const CONTAINS = 'custom_from_compose_contains';
const DISABLE = 'custom_from_preference_disable';
const RULES = 'custom_from_header_rules';
const SUBJECT = 'custom_from_compose_subject';

public static function storage_init_should_fetch_headers_provider(): array
{
return array(
array(
array(
'custom_from_compose_auto' => false
),
array('custom_from_compose_auto' => false),
array(),
''
),
array(
array(
'custom_from_compose_auto' => true
),
array('custom_from_compose_auto' => true),
array(),
'bcc cc from to x-original-to'
),
array(
array(
'custom_from_header_rules' => 'header1=a;header2=b'
),
array(self::RULES => 'header1=a;header2=b'),
array(),
'header1 header2'
),
array(
array(
'custom_from_header_rules' => 'header=a',
'custom_from_preference_disable' => true
),
array(
'custom_from_compose_subject' => 'always'
),
array(self::RULES => 'header=a', self::DISABLE => true),
array(self::SUBJECT => 'always'),
'header'
),
array(
array(
'custom_from_header_rules' => 'header=a',
'custom_from_preference_disable' => false
),
array(
'custom_from_compose_subject' => 'always'
),
array(self::RULES => 'header=a', self::DISABLE => false),
array(self::SUBJECT => 'always'),
'header bcc cc from to x-original-to'
)
);
Expand All @@ -75,29 +64,106 @@ public function test_storage_init_should_fetch_headers($config_values, $user_pre
public static function message_compose_should_set_state_provider(): array
{
return array(
// Subject rule "exact" should match address exactly
array(
array('to' => '[email protected]'),
array('to' => '[email protected]'),
array(self::RULES => 'to=e'),
array(),
null, // TODO: assert [email protected] was matched
),
// Subject rule "exact" shouldn't match suffix
array(
array('to' => '[email protected]'),
array(self::RULES => 'to=e'),
array(),
null,
),
// Subject rule "prefix" should match address exactly
array(
array('to' => '[email protected]'),
'[email protected]',
)
array('to' => '[email protected]'),
array(),
array(),
null, // TODO: assert [email protected] was matched
),
// Subject rule "prefix" should match address by prefix
array(
array('to' => '[email protected]'),
array(),
array(),
'[email protected]',
),
// Subject rule "prefix" should not match different user
array(
array('to' => '[email protected]'),
array(),
array(),
null,
),
// Subject rule "domain" on custom header should match address by domain
array(
array('to' => '[email protected]', 'x-custom' => '[email protected]'),
array(self::RULES => 'x-custom=d'),
array(),
'[email protected]' // TODO: assert [email protected] was matched
),
// Subject rule "domain" should not match different domain
array(
array('to' => '[email protected]'),
array(self::RULES => 'to=d'),
array(),
null,
),
// Subject rule "other" should match anything
array(
array('to' => '[email protected]'),
array(self::RULES => 'to=o'),
array(),
'[email protected]' // TODO: assert [email protected] was matched
),
// Subject rule is overridden by user prefrences
array(
array('to' => '[email protected]'),
array(self::RULES => 'to=e'),
array(self::SUBJECT => 'domain'),
'[email protected]'
),
// Contains constraint in configuration options matches address
array(
array('to' => '[email protected]'),
array(self::CONTAINS => 'match'),
array(self::SUBJECT => 'domain'),
'[email protected]'
),
// Contains constraint in configuration options rejects no match
array(
array('to' => '[email protected]'),
array(self::CONTAINS => 'match'),
array(self::SUBJECT => 'domain'),
null
),
// Contains constraint in user preferences rejects no match
array(
array('to' => '[email protected]'),
array(self::CONTAINS => 'other'),
array(self::CONTAINS => 'match', self::SUBJECT => 'always'),
null
),
);
}

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

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

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

0 comments on commit dc9da33

Please sign in to comment.