|
| 1 | +<?php |
| 2 | + |
| 3 | +use phpbb\config\config; |
| 4 | +use phpbb\language\language; |
| 5 | +use phpbb\language\language_file_loader; |
| 6 | +use phpbb\messenger\method\jabber; |
| 7 | +use phpbb\messenger\method\messenger_interface; |
| 8 | +use phpbb\messenger\queue; |
| 9 | +use phpbb\path_helper; |
| 10 | +use phpbb\symfony_request; |
| 11 | +use phpbb\template\assets_bag; |
| 12 | + |
| 13 | +/** |
| 14 | + * |
| 15 | + * This file is part of the phpBB Forum Software package. |
| 16 | + * |
| 17 | + * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 18 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 19 | + * |
| 20 | + * For full copyright and license information, please see |
| 21 | + * the docs/CREDITS.txt file. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +class phpbb_messenger_method_jabber_test extends \phpbb_test_case |
| 26 | +{ |
| 27 | + protected $assets_bag; |
| 28 | + protected $cache_path; |
| 29 | + protected config $config; |
| 30 | + protected $dispatcher; |
| 31 | + protected $extension_manager; |
| 32 | + protected jabber $method_jabber; |
| 33 | + protected $method_base; |
| 34 | + protected $language; |
| 35 | + protected $log; |
| 36 | + protected $path_helper; |
| 37 | + protected queue $queue; |
| 38 | + protected $request; |
| 39 | + protected $twig_extensions_collection; |
| 40 | + protected $twig_lexer; |
| 41 | + protected $user; |
| 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 | + 'jab_username' => 'test', |
| 52 | + 'jab_password' => 'password', |
| 53 | + 'jab_use_ssl' => false, |
| 54 | + 'jab_host' => 'localhost', |
| 55 | + 'jab_port' => 5222, |
| 56 | + 'jab_verify_peer' => true, |
| 57 | + 'jab_verify_peer_name' => true, |
| 58 | + 'jab_allow_self_signed' => false, |
| 59 | + ]); |
| 60 | + $config = $this->config; |
| 61 | + $this->dispatcher = $this->getMockBuilder('\phpbb\event\dispatcher') |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + $this->filesystem = new \phpbb\filesystem\filesystem(); |
| 65 | + $this->language = new language(new language_file_loader($phpbb_root_path, $phpEx)); |
| 66 | + $this->queue = $this->createMock(queue::class); |
| 67 | + $this->request = new phpbb_mock_request(); |
| 68 | + $request = $this->request; |
| 69 | + $this->symfony_request = new symfony_request(new phpbb_mock_request()); |
| 70 | + $symfony_request = $this->symfony_request; |
| 71 | + $this->user = $this->getMockBuilder('\phpbb\user') |
| 72 | + ->setConstructorArgs([$this->language, '\phpbb\datetime']) |
| 73 | + ->getMock(); |
| 74 | + $user = $this->user; |
| 75 | + $user->page['root_script_path'] = 'phpbb/'; |
| 76 | + $this->user->host = 'yourdomain.com'; |
| 77 | + $this->path_helper = new path_helper( |
| 78 | + $this->symfony_request, |
| 79 | + $this->request, |
| 80 | + $phpbb_root_path, |
| 81 | + $phpEx |
| 82 | + ); |
| 83 | + $phpbb_container = new phpbb_mock_container_builder; |
| 84 | + $this->twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); |
| 85 | + $twig = new \phpbb\template\twig\environment( |
| 86 | + $this->assets_bag, |
| 87 | + $this->config, |
| 88 | + $this->filesystem, |
| 89 | + $this->path_helper, |
| 90 | + $this->cache_path, |
| 91 | + null, |
| 92 | + new \phpbb\template\twig\loader(''), |
| 93 | + $this->dispatcher, |
| 94 | + array( |
| 95 | + 'cache' => false, |
| 96 | + 'debug' => false, |
| 97 | + 'auto_reload' => true, |
| 98 | + 'autoescape' => false, |
| 99 | + ) |
| 100 | + ); |
| 101 | + $this->twig_lexer = new \phpbb\template\twig\lexer($twig); |
| 102 | + $this->extension_manager = new phpbb_mock_extension_manager( |
| 103 | + __DIR__ . '/', |
| 104 | + array( |
| 105 | + 'vendor2/foo' => array( |
| 106 | + 'ext_name' => 'vendor2/foo', |
| 107 | + 'ext_active' => '1', |
| 108 | + 'ext_path' => 'ext/vendor2/foo/', |
| 109 | + ), |
| 110 | + ) |
| 111 | + ); |
| 112 | + $this->log = $this->createMock(\phpbb\log\log_interface::class); |
| 113 | + |
| 114 | + $this->method_jabber = new jabber( |
| 115 | + $this->assets_bag, |
| 116 | + $this->config, |
| 117 | + $this->dispatcher, |
| 118 | + $this->language, |
| 119 | + $this->queue, |
| 120 | + $this->path_helper, |
| 121 | + $this->request, |
| 122 | + $this->twig_extensions_collection, |
| 123 | + $this->twig_lexer, |
| 124 | + $this->user, |
| 125 | + $phpbb_root_path, |
| 126 | + $this->cache_path, |
| 127 | + $this->extension_manager, |
| 128 | + $this->log |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + public function test_miscellaneous() |
| 133 | + { |
| 134 | + $this->method_jabber->init(); |
| 135 | + $this->assertEquals(messenger_interface::NOTIFY_IM, $this->method_jabber->get_id()); |
| 136 | + $this->assertEquals('jabber', $this->method_jabber->get_queue_object_name()); |
| 137 | + $this->assertFalse($this->method_jabber->is_enabled()); |
| 138 | + $this->config->set('jab_enable', true); |
| 139 | + $this->assertTrue($this->method_jabber->is_enabled()); |
| 140 | + $this->assertEquals(@extension_loaded('openssl'), $this->method_jabber->can_use_ssl()); |
| 141 | + } |
| 142 | + |
| 143 | + public function test_stream_options() |
| 144 | + { |
| 145 | + $this->method_jabber->init(); |
| 146 | + $this->assertEquals($this->method_jabber, $this->method_jabber->stream_options([ |
| 147 | + 'allow_self_signed' => true, |
| 148 | + ])); |
| 149 | + |
| 150 | + $stream_options_reflection = new \ReflectionProperty($this->method_jabber, 'stream_options'); |
| 151 | + $stream_options = $stream_options_reflection->getValue($this->method_jabber); |
| 152 | + $this->assertEquals([ |
| 153 | + 'ssl' => [ |
| 154 | + 'allow_self_signed' => false, |
| 155 | + 'verify_peer' => true, |
| 156 | + 'verify_peer_name' => true, |
| 157 | + ], |
| 158 | + ], $stream_options); |
| 159 | + |
| 160 | + $this->method_jabber->ssl(true); |
| 161 | + |
| 162 | + $this->assertEquals($this->method_jabber, $this->method_jabber->stream_options([ |
| 163 | + 'allow_self_signed' => true, |
| 164 | + ])); |
| 165 | + $stream_options = $stream_options_reflection->getValue($this->method_jabber); |
| 166 | + $this->assertEquals([ |
| 167 | + 'ssl' => [ |
| 168 | + 'allow_self_signed' => true, |
| 169 | + 'verify_peer' => true, |
| 170 | + 'verify_peer_name' => true, |
| 171 | + ], |
| 172 | + ], $stream_options); |
| 173 | + } |
| 174 | + |
| 175 | + public function test_port_ssl_switch() |
| 176 | + { |
| 177 | + $port_reflection = new \ReflectionProperty($this->method_jabber, 'port'); |
| 178 | + |
| 179 | + $this->method_jabber->port(); |
| 180 | + $this->assertEquals(5222, $port_reflection->getValue($this->method_jabber)); |
| 181 | + |
| 182 | + $this->method_jabber->ssl(true) |
| 183 | + ->port(); |
| 184 | + $this->assertEquals(5223, $port_reflection->getValue($this->method_jabber)); |
| 185 | + } |
| 186 | + |
| 187 | + public function test_username() |
| 188 | + { |
| 189 | + $jabber_reflection = new \ReflectionClass($this->method_jabber); |
| 190 | + $username_reflection = $jabber_reflection->getProperty('username'); |
| 191 | + $jid_reflection = $jabber_reflection->getProperty('jid'); |
| 192 | + |
| 193 | + $this->method_jabber->username('foo@bar'); |
| 194 | + $this->assertEquals(['foo', 'bar'], $jid_reflection->getValue($this->method_jabber)); |
| 195 | + $this->assertEquals('foo', $username_reflection->getValue($this->method_jabber)); |
| 196 | + |
| 197 | + $this->method_jabber->username('bar@baz@qux'); |
| 198 | + $this->assertEquals(['bar', 'baz@qux'], $jid_reflection->getValue($this->method_jabber)); |
| 199 | + $this->assertEquals('bar', $username_reflection->getValue($this->method_jabber)); |
| 200 | + } |
| 201 | + |
| 202 | + public function test_server() |
| 203 | + { |
| 204 | + $jabber_reflection = new \ReflectionClass($this->method_jabber); |
| 205 | + $connect_server_reflection = $jabber_reflection->getProperty('connect_server'); |
| 206 | + $server_reflection = $jabber_reflection->getProperty('server'); |
| 207 | + |
| 208 | + $this->method_jabber->server(); |
| 209 | + $this->assertEquals('localhost', $connect_server_reflection->getValue($this->method_jabber)); |
| 210 | + $this->assertEquals('localhost', $server_reflection->getValue($this->method_jabber)); |
| 211 | + |
| 212 | + $this->method_jabber->server('foobar.com'); |
| 213 | + $this->assertEquals('foobar.com', $connect_server_reflection->getValue($this->method_jabber)); |
| 214 | + $this->assertEquals('foobar.com', $server_reflection->getValue($this->method_jabber)); |
| 215 | + |
| 216 | + $this-> method_jabber-> username( '[email protected]'); |
| 217 | + $this->method_jabber->server('foobar.com'); |
| 218 | + $this->assertEquals('foobar.com', $connect_server_reflection->getValue($this->method_jabber)); |
| 219 | + $this->assertEquals('bar.com', $server_reflection->getValue($this->method_jabber)); |
| 220 | + } |
| 221 | + |
| 222 | + public function test_encrypt_password() |
| 223 | + { |
| 224 | + $this->method_jabber->init(); |
| 225 | + $this->method_jabber->password('password'); |
| 226 | + $data = [ |
| 227 | + 'realm' => 'example.com', |
| 228 | + 'nonce' => '12345', |
| 229 | + 'cnonce' => 'abcde', |
| 230 | + 'digest-uri' => 'xmpp/example.com', |
| 231 | + 'nc' => '00000001', |
| 232 | + 'qop' => 'auth', |
| 233 | + ]; |
| 234 | + |
| 235 | + $expected = md5(sprintf( |
| 236 | + '%s:%s:%s:%s:%s:%s', |
| 237 | + md5(pack('H32', md5('test:example.com:password')) . ':12345:abcde'), |
| 238 | + $data['nonce'], |
| 239 | + $data['nc'], |
| 240 | + $data['cnonce'], |
| 241 | + $data['qop'], |
| 242 | + md5('AUTHENTICATE:xmpp/example.com') |
| 243 | + )); |
| 244 | + $this->assertEquals($expected, $this->method_jabber->encrypt_password($data)); |
| 245 | + } |
| 246 | + |
| 247 | + public function test_parse_data() |
| 248 | + { |
| 249 | + $data = 'key1="value1",key2="value2",key3="value3"'; |
| 250 | + $expected = [ |
| 251 | + 'key1' => 'value1', |
| 252 | + 'key2' => 'value2', |
| 253 | + 'key3' => 'value3', |
| 254 | + ]; |
| 255 | + |
| 256 | + $this->assertEquals($expected, $this->method_jabber->parse_data($data)); |
| 257 | + } |
| 258 | + |
| 259 | + public function test_implode_data() |
| 260 | + { |
| 261 | + $data = [ |
| 262 | + 'key1' => 'value1', |
| 263 | + 'key2' => 'value2', |
| 264 | + 'key3' => 'value3', |
| 265 | + ]; |
| 266 | + $expected = 'key1="value1",key2="value2",key3="value3"'; |
| 267 | + |
| 268 | + $this->assertEquals($expected, $this->method_jabber->implode_data($data)); |
| 269 | + } |
| 270 | + |
| 271 | + public function test_xmlize() |
| 272 | + { |
| 273 | + $xml = '<root><child key="value">content</child></root>'; |
| 274 | + $result = $this->method_jabber->xmlize($xml); |
| 275 | + |
| 276 | + $this->assertArrayHasKey('root', $result); |
| 277 | + $this->assertArrayHasKey('child', $result['root'][0]['#']); |
| 278 | + $this->assertEquals('content', $result['root'][0]['#']['child'][0]['#']); |
| 279 | + $this->assertEquals(['key' => 'value'], $result['root'][0]['#']['child'][0]['@']); |
| 280 | + } |
| 281 | + |
| 282 | + public function test_send_xml() |
| 283 | + { |
| 284 | + $jabber_mock = $this->getMockBuilder(jabber::class) |
| 285 | + ->setConstructorArgs([ |
| 286 | + $this->assets_bag, |
| 287 | + $this->config, |
| 288 | + $this->dispatcher, |
| 289 | + $this->language, |
| 290 | + $this->queue, |
| 291 | + $this->path_helper, |
| 292 | + $this->request, |
| 293 | + $this->twig_extensions_collection, |
| 294 | + $this->twig_lexer, |
| 295 | + $this->user, |
| 296 | + '', |
| 297 | + '', |
| 298 | + $this->extension_manager, |
| 299 | + $this->log, |
| 300 | + ]) |
| 301 | + ->onlyMethods(['send_xml']) |
| 302 | + ->getMock(); |
| 303 | + |
| 304 | + $jabber_mock->expects($this->once()) |
| 305 | + ->method('send_xml') |
| 306 | + ->with('<message>Test</message>') |
| 307 | + ->willReturn(true); |
| 308 | + |
| 309 | + $this->assertTrue($jabber_mock->send_xml('<message>Test</message>')); |
| 310 | + } |
| 311 | +} |
0 commit comments