diff --git a/custom_from.php b/custom_from.php index 28c6cb5..a2596c7 100644 --- a/custom_from.php +++ b/custom_from.php @@ -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); diff --git a/tests/CustomFromTest.php b/tests/CustomFromTest.php index 2571a2d..3c77852 100644 --- a/tests/CustomFromTest.php +++ b/tests/CustomFromTest.php @@ -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' ) ); @@ -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' => 'primary@domain.ext'), + array('to' => 'alice@primary.ext'), + array(self::RULES => 'to=e'), + array(), + null, // TODO: assert alice@primary.ext was matched + ), + // Subject rule "exact" shouldn't match suffix + array( + array('to' => 'alice+suffix@primary.ext'), + array(self::RULES => 'to=e'), + array(), null, ), + // Subject rule "prefix" should match address exactly array( - array('to' => 'primary+suffix@domain.ext'), - 'primary+suffix@domain.ext', - ) + array('to' => 'alice@primary.ext'), + array(), + array(), + null, // TODO: assert alice@primary.ext was matched + ), + // Subject rule "prefix" should match address by prefix + array( + array('to' => 'alice+suffix@primary.ext'), + array(), + array(), + 'alice+suffix@primary.ext', + ), + // Subject rule "prefix" should not match different user + array( + array('to' => 'carl+suffix@primary.ext'), + array(), + array(), + null, + ), + // Subject rule "domain" on custom header should match address by domain + array( + array('to' => 'unknown@primary.ext', 'x-custom' => 'unknown@secondary.ext'), + array(self::RULES => 'x-custom=d'), + array(), + 'unknown@secondary.ext' // TODO: assert bob@primary.ext was matched + ), + // Subject rule "domain" should not match different domain + array( + array('to' => 'unknown@unknown.ext'), + array(self::RULES => 'to=d'), + array(), + null, + ), + // Subject rule "other" should match anything + array( + array('to' => 'unknown@unknown.ext'), + array(self::RULES => 'to=o'), + array(), + 'unknown@unknown.ext' // TODO: assert bob@primary.ext was matched + ), + // Subject rule is overridden by user prefrences + array( + array('to' => 'unknown@secondary.ext'), + array(self::RULES => 'to=e'), + array(self::SUBJECT => 'domain'), + 'unknown@secondary.ext' + ), + // Contains constraint in configuration options matches address + array( + array('to' => 'alice+match@secondary.ext'), + array(self::CONTAINS => 'match'), + array(self::SUBJECT => 'domain'), + 'alice+match@secondary.ext' + ), + // Contains constraint in configuration options rejects no match + array( + array('to' => 'alice+other@secondary.ext'), + array(self::CONTAINS => 'match'), + array(self::SUBJECT => 'domain'), + null + ), + // Contains constraint in user preferences rejects no match + array( + array('to' => 'alice+other@secondary.ext'), + 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' => 'primary@domain.ext', 'name' => 'Primary', 'standard' => '1'); - $identity2 = array('email' => 'secondary@domain.ext', 'name' => 'Secondary', 'standard' => '0'); + $identity1 = array('email' => 'alice@primary.ext', 'name' => 'Alice', 'standard' => '0'); + $identity2 = array('email' => 'bob@primary.ext', 'name' => 'Bob', 'standard' => '1'); + $identity3 = array('email' => 'carl@secondary.ext', '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)));