Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
185 / 185 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| phpbb_messenger_method_base_test | |
100.00% |
185 / 185 |
|
100.00% |
8 / 8 |
8 | |
100.00% |
1 / 1 |
| setUp | |
100.00% |
93 / 93 |
|
100.00% |
1 / 1 |
1 | |||
| test_header | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| test_set_use_queue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| test_error_wout_session | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| test_save_queue | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| test_template_no_lang | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 | |||
| test_template_template_path | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
1 | |||
| test_template_path_fallback | |
100.00% |
27 / 27 |
|
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 | use phpbb\config\config; |
| 15 | use phpbb\language\language; |
| 16 | use phpbb\language\language_file_loader; |
| 17 | use phpbb\messenger\method\email; |
| 18 | use phpbb\messenger\queue; |
| 19 | use phpbb\path_helper; |
| 20 | use phpbb\symfony_request; |
| 21 | use phpbb\template\assets_bag; |
| 22 | |
| 23 | class phpbb_messenger_method_base_test extends \phpbb_test_case |
| 24 | { |
| 25 | protected $assets_bag; |
| 26 | protected $cache_path; |
| 27 | protected config $config; |
| 28 | protected $dispatcher; |
| 29 | protected $extension_manager; |
| 30 | protected email $method_email; |
| 31 | protected $method_base; |
| 32 | protected $language; |
| 33 | protected $log; |
| 34 | protected $path_helper; |
| 35 | protected queue $queue; |
| 36 | protected $request; |
| 37 | protected $twig_extensions_collection; |
| 38 | protected $twig_lexer; |
| 39 | protected $user; |
| 40 | protected $filesystem; |
| 41 | protected $symfony_request; |
| 42 | |
| 43 | public function setUp(): void |
| 44 | { |
| 45 | global $config, $request, $symfony_request, $user, $phpbb_root_path, $phpEx; |
| 46 | |
| 47 | $this->assets_bag = new assets_bag(); |
| 48 | $this->cache_path = $phpbb_root_path . 'cache/' . PHPBB_ENVIRONMENT . '/twig'; |
| 49 | $this->config = new config([ |
| 50 | 'force_server_vars' => false, |
| 51 | ]); |
| 52 | $config = $this->config; |
| 53 | $this->dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher') |
| 54 | ->disableOriginalConstructor() |
| 55 | ->getMock(); |
| 56 | $this->filesystem = new \phpbb\filesystem\filesystem(); |
| 57 | $this->language = new language(new language_file_loader($phpbb_root_path, $phpEx)); |
| 58 | $this->queue = $this->createMock(queue::class); |
| 59 | $this->request = new phpbb_mock_request(); |
| 60 | $request = $this->request; |
| 61 | $this->symfony_request = new symfony_request(new phpbb_mock_request()); |
| 62 | $symfony_request = $this->symfony_request; |
| 63 | $this->user = $this->getMockBuilder('\phpbb\user') |
| 64 | ->setConstructorArgs([$this->language, '\phpbb\datetime']) |
| 65 | ->getMock(); |
| 66 | $user = $this->user; |
| 67 | $user->page['root_script_path'] = 'phpbb/'; |
| 68 | $this->user->host = 'yourdomain.com'; |
| 69 | $this->path_helper = new path_helper( |
| 70 | $this->symfony_request, |
| 71 | $this->request, |
| 72 | $phpbb_root_path, |
| 73 | $phpEx |
| 74 | ); |
| 75 | $phpbb_container = new phpbb_mock_container_builder; |
| 76 | $this->twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); |
| 77 | $twig = new \phpbb\template\twig\environment( |
| 78 | $this->assets_bag, |
| 79 | $this->config, |
| 80 | $this->filesystem, |
| 81 | $this->path_helper, |
| 82 | $this->cache_path, |
| 83 | null, |
| 84 | new \phpbb\template\twig\loader(''), |
| 85 | $this->dispatcher, |
| 86 | array( |
| 87 | 'cache' => false, |
| 88 | 'debug' => false, |
| 89 | 'auto_reload' => true, |
| 90 | 'autoescape' => false, |
| 91 | ) |
| 92 | ); |
| 93 | $this->twig_lexer = new \phpbb\template\twig\lexer($twig); |
| 94 | $this->extension_manager = new phpbb_mock_extension_manager( |
| 95 | __DIR__ . '/', |
| 96 | array( |
| 97 | 'vendor2/foo' => array( |
| 98 | 'ext_name' => 'vendor2/foo', |
| 99 | 'ext_active' => '1', |
| 100 | 'ext_path' => 'ext/vendor2/foo/', |
| 101 | ), |
| 102 | ) |
| 103 | ); |
| 104 | $this->log = $this->createMock(\phpbb\log\log_interface::class); |
| 105 | |
| 106 | $this->method_email = new email( |
| 107 | $this->assets_bag, |
| 108 | $this->config, |
| 109 | $this->dispatcher, |
| 110 | $this->language, |
| 111 | $this->queue, |
| 112 | $this->path_helper, |
| 113 | $this->request, |
| 114 | $this->twig_extensions_collection, |
| 115 | $this->twig_lexer, |
| 116 | $this->user, |
| 117 | $phpbb_root_path, |
| 118 | $this->cache_path, |
| 119 | $this->extension_manager, |
| 120 | $this->log |
| 121 | ); |
| 122 | |
| 123 | $this->method_base = $this->getMockBuilder(\phpbb\messenger\method\base::class) |
| 124 | ->setConstructorArgs([ |
| 125 | $this->assets_bag, |
| 126 | $this->config, |
| 127 | $this->dispatcher, |
| 128 | $this->language, |
| 129 | $this->queue, |
| 130 | $this->path_helper, |
| 131 | $this->request, |
| 132 | $this->twig_extensions_collection, |
| 133 | $this->twig_lexer, |
| 134 | $this->user, |
| 135 | $phpbb_root_path, |
| 136 | $this->cache_path, |
| 137 | $this->extension_manager, |
| 138 | $this->log |
| 139 | ]) |
| 140 | ->getMockForAbstractClass(); |
| 141 | } |
| 142 | |
| 143 | public function test_header() |
| 144 | { |
| 145 | $this->method_base->header('X-AntiAbuse', 'Board servername - ' . $this->user->host); |
| 146 | $this->assertTrue(true); // No exception should be thrown |
| 147 | } |
| 148 | |
| 149 | public function test_set_use_queue() |
| 150 | { |
| 151 | $use_queue_property = new \ReflectionProperty($this->method_base, 'use_queue'); |
| 152 | $this->method_base->set_use_queue(); |
| 153 | $this->assertTrue($use_queue_property->getValue($this->method_base)); |
| 154 | $this->method_base->set_use_queue(false); |
| 155 | $this->assertFalse($use_queue_property->getValue($this->method_base)); |
| 156 | } |
| 157 | |
| 158 | public function test_error_wout_session() |
| 159 | { |
| 160 | $errors = []; |
| 161 | $this->log->method('add') |
| 162 | ->willReturnCallback(function($mode, $user_id, $log_ip, $log_operation, $log_time = false, $additional_data = []) use (&$errors) { |
| 163 | $errors[] = $additional_data[0]; |
| 164 | }); |
| 165 | |
| 166 | $this->user->data['user_id'] = 2; |
| 167 | $this->user->session_id = ''; |
| 168 | $this->user |
| 169 | ->expects($this->once()) |
| 170 | ->method('session_begin') |
| 171 | ->willReturnCallback(function() { |
| 172 | $this->assertTrue(true); |
| 173 | }); |
| 174 | |
| 175 | $this->method_base->error('Test error message'); |
| 176 | |
| 177 | $this->assertCount(1, $errors); |
| 178 | $this->assertEquals('<strong></strong><br><em></em><br><br>Test error message<br>', $errors[0]); |
| 179 | } |
| 180 | |
| 181 | public function test_save_queue() |
| 182 | { |
| 183 | $this->queue->expects($this->once()) |
| 184 | ->method('save'); |
| 185 | $this->method_base->set_use_queue(false); |
| 186 | $this->method_base->save_queue(); |
| 187 | $this->method_base->set_use_queue(true); |
| 188 | $this->method_base->save_queue(); |
| 189 | } |
| 190 | |
| 191 | public function test_template_no_lang() |
| 192 | { |
| 193 | $template_mock = $this->getMockBuilder(\phpbb\template\template::class) |
| 194 | ->disableOriginalConstructor() |
| 195 | ->getMock(); |
| 196 | $filenames = []; |
| 197 | $template_mock->method('set_filenames') |
| 198 | ->willReturnCallback(function($filename_array) use (&$filenames, $template_mock) { |
| 199 | $filenames = array_merge($filenames, $filename_array); |
| 200 | |
| 201 | return $template_mock; |
| 202 | }); |
| 203 | |
| 204 | $base_reflection = new \ReflectionClass($this->method_base); |
| 205 | $template_reflection = $base_reflection->getProperty('template'); |
| 206 | $template_reflection->setValue($this->method_base, $template_mock); |
| 207 | |
| 208 | $this->config->set('default_lang', 'en'); |
| 209 | $this->method_base->template('test'); |
| 210 | $this->assertEquals(['body' => 'test.txt'], $filenames); |
| 211 | } |
| 212 | |
| 213 | public function test_template_template_path() |
| 214 | { |
| 215 | global $phpbb_root_path; |
| 216 | |
| 217 | $template_mock = $this->getMockBuilder(\phpbb\template\template::class) |
| 218 | ->disableOriginalConstructor() |
| 219 | ->getMock(); |
| 220 | $filenames = []; |
| 221 | $template_mock->method('set_filenames') |
| 222 | ->willReturnCallback(function($filename_array) use (&$filenames, $template_mock) { |
| 223 | $filenames = array_merge($filenames, $filename_array); |
| 224 | |
| 225 | return $template_mock; |
| 226 | }); |
| 227 | $template_mock->method('set_custom_style') |
| 228 | ->willReturnCallback(function($path_name, $paths) use($phpbb_root_path) { |
| 229 | $this->assertEquals([['name' => 'en_email', 'ext_path' => 'language/en/email']], $path_name); |
| 230 | $this->assertEquals([$phpbb_root_path . 'language/en/email'], $paths); |
| 231 | }); |
| 232 | |
| 233 | $base_reflection = new \ReflectionClass($this->method_base); |
| 234 | $template_reflection = $base_reflection->getProperty('template'); |
| 235 | $template_reflection->setValue($this->method_base, $template_mock); |
| 236 | |
| 237 | $this->config->set('default_lang', 'en'); |
| 238 | $this->method_base->template('test', '', $phpbb_root_path . 'language/en/email'); |
| 239 | $this->assertEquals(['body' => 'test.txt'], $filenames); |
| 240 | } |
| 241 | |
| 242 | public function test_template_path_fallback() |
| 243 | { |
| 244 | global $phpbb_root_path; |
| 245 | |
| 246 | $template_mock = $this->getMockBuilder(\phpbb\template\template::class) |
| 247 | ->disableOriginalConstructor() |
| 248 | ->getMock(); |
| 249 | $filenames = []; |
| 250 | $template_mock->method('set_filenames') |
| 251 | ->willReturnCallback(function($filename_array) use (&$filenames, $template_mock) { |
| 252 | $filenames = array_merge($filenames, $filename_array); |
| 253 | |
| 254 | return $template_mock; |
| 255 | }); |
| 256 | $template_mock->method('set_custom_style') |
| 257 | ->willReturnCallback(function($path_name, $paths) use($phpbb_root_path) { |
| 258 | $this->assertEquals([ |
| 259 | ['name' => 'de_email', 'ext_path' => 'language/de/email'], |
| 260 | ['name' => 'en_email', 'ext_path' => 'language/en/email'], |
| 261 | ], $path_name); |
| 262 | $this->assertEquals([ |
| 263 | $phpbb_root_path . 'language/de/email', |
| 264 | $phpbb_root_path . 'language/en/email' |
| 265 | ], $paths); |
| 266 | }); |
| 267 | |
| 268 | $base_reflection = new \ReflectionClass($this->method_base); |
| 269 | $template_reflection = $base_reflection->getProperty('template'); |
| 270 | $template_reflection->setValue($this->method_base, $template_mock); |
| 271 | |
| 272 | $this->config->set('default_lang', 'de'); |
| 273 | $this->method_base->template('test', 'de'); |
| 274 | $this->assertEquals(['body' => 'test.txt'], $filenames); |
| 275 | } |
| 276 | } |