Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.99% |
129 / 133 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_email_parsing_test | |
96.99% |
129 / 133 |
|
66.67% |
2 / 3 |
3 | |
0.00% |
0 / 1 |
| setUp | |
100.00% |
106 / 106 |
|
100.00% |
1 / 1 |
1 | |||
| email_parsing_data | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| test_email_parsing | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | class phpbb_email_parsing_test extends phpbb_test_case |
| 15 | { |
| 16 | /** @var \messenger */ |
| 17 | protected $messenger; |
| 18 | |
| 19 | /** @var \ReflectionProperty */ |
| 20 | protected $reflection_template_property; |
| 21 | |
| 22 | /** @var \phpbb\messenger\method\email */ |
| 23 | protected $email; |
| 24 | |
| 25 | protected function setUp(): void |
| 26 | { |
| 27 | global $phpbb_container, $config, $phpbb_root_path, $phpEx, $request, $user; |
| 28 | |
| 29 | $phpbb_container = new phpbb_mock_container_builder; |
| 30 | |
| 31 | $config = new \phpbb\config\config(array( |
| 32 | 'board_email_sig' => '-- Thanks, The Management', |
| 33 | 'sitename' => 'yourdomain.com', |
| 34 | 'default_lang' => 'en', |
| 35 | )); |
| 36 | $phpbb_container->set('config', $config); |
| 37 | |
| 38 | $request = new phpbb_mock_request; |
| 39 | $symfony_request = new \phpbb\symfony_request( |
| 40 | $request |
| 41 | ); |
| 42 | $filesystem = new \phpbb\filesystem\filesystem(); |
| 43 | $phpbb_path_helper = new \phpbb\path_helper( |
| 44 | $symfony_request, |
| 45 | $request, |
| 46 | $phpbb_root_path, |
| 47 | $phpEx |
| 48 | ); |
| 49 | $phpbb_container->set('path_helper', $phpbb_path_helper); |
| 50 | $phpbb_container->set('filesystem', $filesystem); |
| 51 | |
| 52 | $cache_path = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/twig'; |
| 53 | $phpbb_container->setParameter('core.template.cache_path', $cache_path); |
| 54 | |
| 55 | $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); |
| 56 | $lang = new \phpbb\language\language($lang_loader); |
| 57 | $user = new \phpbb\user($lang, '\phpbb\datetime'); |
| 58 | $user->data['user_lang'] = 'en'; |
| 59 | $phpbb_container->set('user', $user); |
| 60 | $extension_manager = new phpbb_mock_extension_manager( |
| 61 | __DIR__ . '/', |
| 62 | array( |
| 63 | 'vendor2/foo' => array( |
| 64 | 'ext_name' => 'vendor2/foo', |
| 65 | 'ext_active' => '1', |
| 66 | 'ext_path' => 'ext/vendor2/foo/', |
| 67 | ), |
| 68 | ) |
| 69 | ); |
| 70 | $phpbb_container->set('ext.manager', $extension_manager); |
| 71 | |
| 72 | $assets_bag = new \phpbb\template\assets_bag(); |
| 73 | $phpbb_container->set('assets.bag', $assets_bag); |
| 74 | |
| 75 | $context = new \phpbb\template\context(); |
| 76 | $dispatcher = new \phpbb\event\dispatcher(); |
| 77 | $twig = new \phpbb\template\twig\environment( |
| 78 | $assets_bag, |
| 79 | $config, |
| 80 | $filesystem, |
| 81 | $phpbb_path_helper, |
| 82 | $cache_path, |
| 83 | null, |
| 84 | new \phpbb\template\twig\loader(''), |
| 85 | $dispatcher, |
| 86 | array( |
| 87 | 'cache' => false, |
| 88 | 'debug' => false, |
| 89 | 'auto_reload' => true, |
| 90 | 'autoescape' => false, |
| 91 | ) |
| 92 | ); |
| 93 | $twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang); |
| 94 | $phpbb_container->set('template.twig.extensions.phpbb', $twig_extension); |
| 95 | |
| 96 | $twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); |
| 97 | $twig_extensions_collection->add('template.twig.extensions.phpbb'); |
| 98 | $phpbb_container->set('template.twig.extensions.collection', $twig_extensions_collection); |
| 99 | |
| 100 | $twig->addExtension($twig_extension); |
| 101 | $twig_lexer = new \phpbb\template\twig\lexer($twig); |
| 102 | $phpbb_container->set('template.twig.lexer', $twig_lexer); |
| 103 | $phpbb_container->set('dispatcher', $dispatcher); |
| 104 | $phpbb_container->set('language', $lang); |
| 105 | $phpbb_container->set('request', $request); |
| 106 | |
| 107 | $db = $this->getMockBuilder('\phpbb\db\driver\mysqli') |
| 108 | ->disableOriginalConstructor() |
| 109 | ->getMock(); |
| 110 | $auth = $this->createMock('\phpbb\auth\auth'); |
| 111 | $log = new \phpbb\log\log($db, $user, $auth, $dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE); |
| 112 | $phpbb_container->set('log', $log); |
| 113 | $phpbb_container->setParameter('core.root_path', $phpbb_root_path); |
| 114 | $phpbb_container->setParameter('core.php_ext', $phpEx); |
| 115 | |
| 116 | $core_cache_dir = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/'; |
| 117 | $phpbb_container->setParameter('core.cache_dir', $core_cache_dir); |
| 118 | |
| 119 | $core_messenger_queue_file = $core_cache_dir . 'queue.' . $phpEx; |
| 120 | $phpbb_container->setParameter('core.messenger_queue_file', $core_messenger_queue_file); |
| 121 | |
| 122 | $messenger_method_collection = new \phpbb\di\service_collection($phpbb_container); |
| 123 | $messenger_method_collection->add('messenger.method.email'); |
| 124 | $phpbb_container->set('messenger.method_collection', $messenger_method_collection); |
| 125 | |
| 126 | $messenger_queue = new \phpbb\messenger\queue($config, $dispatcher, $messenger_method_collection, $core_messenger_queue_file); |
| 127 | $phpbb_container->set('messenger.queue', $messenger_queue); |
| 128 | |
| 129 | $this->email = new \phpbb\messenger\method\email( |
| 130 | $assets_bag, |
| 131 | $config, |
| 132 | $dispatcher, |
| 133 | $lang, |
| 134 | $messenger_queue, |
| 135 | $phpbb_path_helper, |
| 136 | $request, |
| 137 | $twig_extensions_collection, |
| 138 | $twig_lexer, |
| 139 | $user, |
| 140 | $phpbb_root_path, |
| 141 | $cache_path, |
| 142 | $extension_manager, |
| 143 | $log |
| 144 | ); |
| 145 | $phpbb_container->set('messenger.method.email', $this->email); |
| 146 | |
| 147 | $reflection = new ReflectionObject($this->email); |
| 148 | $this->reflection_template_property = $reflection->getProperty('template'); |
| 149 | } |
| 150 | |
| 151 | public static function email_parsing_data() |
| 152 | { |
| 153 | return array( |
| 154 | array('Author username', 'Any forum', 'The topic title', 'Dear user'), |
| 155 | array('0', 'Any forum', 'The topic title', 'Dear user'), |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @dataProvider email_parsing_data |
| 161 | */ |
| 162 | public function test_email_parsing($author_name, $forum_name, $topic_title, $username) |
| 163 | { |
| 164 | global $config, $phpEx, $user; |
| 165 | |
| 166 | $this->email->set_addresses($user->data); |
| 167 | |
| 168 | $this->email->assign_vars(array( |
| 169 | 'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . html_entity_decode($config['board_email_sig'], ENT_COMPAT)), |
| 170 | 'SITENAME' => html_entity_decode($config['sitename'], ENT_COMPAT), |
| 171 | |
| 172 | 'AUTHOR_NAME' => $author_name, |
| 173 | 'FORUM_NAME' => $forum_name, |
| 174 | 'TOPIC_TITLE' => $topic_title, |
| 175 | 'USERNAME' => $username, |
| 176 | |
| 177 | 'U_FORUM' => generate_board_url() . "/viewforum.{$phpEx}?f=1", |
| 178 | 'U_STOP_WATCHING_FORUM' => generate_board_url() . "/viewforum.{$phpEx}?uid=2&f=1&unwatch=forum", |
| 179 | )); |
| 180 | $this->email->template('newtopic_notify', $user->data['user_lang'], '', ''); |
| 181 | |
| 182 | $reflection_template = $this->reflection_template_property->getValue($this->email); |
| 183 | $msg = trim($reflection_template->assign_display('body')); |
| 184 | |
| 185 | $this->assertStringContainsString($author_name, $msg); |
| 186 | $this->assertStringContainsString($forum_name, $msg); |
| 187 | $this->assertStringContainsString($topic_title, $msg); |
| 188 | $this->assertStringContainsString($username, $msg); |
| 189 | $this->assertStringContainsString(html_entity_decode($config['sitename'], ENT_COMPAT), $msg); |
| 190 | $this->assertStringContainsString(str_replace('<br />', "\n", "-- \n" . html_entity_decode($config['board_email_sig'], ENT_COMPAT)), $msg); |
| 191 | $this->assertStringNotContainsString('EMAIL_SIG', $msg); |
| 192 | $this->assertStringNotContainsString('U_STOP_WATCHING_FORUM', $msg); |
| 193 | } |
| 194 | } |