|
| 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\filesystem\exception\filesystem_exception; |
| 16 | +use phpbb\messenger\queue; |
| 17 | + |
| 18 | +class phpbb_messenger_queue_test extends phpbb_test_case |
| 19 | +{ |
| 20 | + protected $config; |
| 21 | + protected $dispatcher; |
| 22 | + protected $service_collection; |
| 23 | + |
| 24 | + /** @var queue */ |
| 25 | + protected $messenger_queue; |
| 26 | + |
| 27 | + /** |
| 28 | + * Set up the test case |
| 29 | + */ |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + $this->config = new config([ |
| 33 | + 'last_queue_run' => time() - 30, |
| 34 | + 'queue_interval' => 600, |
| 35 | + ]); |
| 36 | + $this->dispatcher = $this->getMockBuilder('phpbb\event\dispatcher') |
| 37 | + ->disableOriginalConstructor() |
| 38 | + ->getMock(); |
| 39 | + $this->service_collection = $this->getMockBuilder('phpbb\di\service_collection') |
| 40 | + ->disableOriginalConstructor() |
| 41 | + ->onlyMethods(['getIterator', 'add_service_class']) |
| 42 | + ->getMock(); |
| 43 | + |
| 44 | + $this->cache_file = __DIR__ . '/../tmp/queue_test'; |
| 45 | + |
| 46 | + if (file_exists($this->cache_file)) |
| 47 | + { |
| 48 | + @unlink($this->cache_file); |
| 49 | + } |
| 50 | + |
| 51 | + $this->messenger_queue = $this->getMockBuilder('phpbb\messenger\queue') |
| 52 | + ->setConstructorArgs([ |
| 53 | + $this->config, |
| 54 | + $this->dispatcher, |
| 55 | + $this->service_collection, |
| 56 | + $this->cache_file |
| 57 | + ]) |
| 58 | + ->addMethods(['get_data']) |
| 59 | + ->getMock(); |
| 60 | + |
| 61 | + $this->messenger_queue->method('get_data') |
| 62 | + ->willReturnCallback(function(){ |
| 63 | + $data_reflection = new \ReflectionProperty(queue::class, 'data'); |
| 64 | + return $data_reflection->getValue($this->messenger_queue); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_init() |
| 69 | + { |
| 70 | + $this->messenger_queue->init('email', 5); |
| 71 | + $this->assertEquals([ |
| 72 | + 'email' => [ |
| 73 | + 'package_size' => 5, |
| 74 | + 'data' => [], |
| 75 | + ] |
| 76 | + ], $this->messenger_queue->get_data()); |
| 77 | + |
| 78 | + $this->messenger_queue->init('jabber', 9); |
| 79 | + |
| 80 | + $this->assertEquals([ |
| 81 | + 'email' => [ |
| 82 | + 'package_size' => 5, |
| 83 | + 'data' => [], |
| 84 | + ], |
| 85 | + 'jabber' => [ |
| 86 | + 'package_size' => 9, |
| 87 | + 'data' => [], |
| 88 | + ] |
| 89 | + ], $this->messenger_queue->get_data()); |
| 90 | + } |
| 91 | + |
| 92 | + public function test_put() |
| 93 | + { |
| 94 | + $this->messenger_queue->init('email', 5); |
| 95 | + $this->assertEquals([ |
| 96 | + 'email' => [ |
| 97 | + 'package_size' => 5, |
| 98 | + 'data' => [], |
| 99 | + ] |
| 100 | + ], $this->messenger_queue->get_data()); |
| 101 | + |
| 102 | + $this->messenger_queue->put('email', ['data1']); |
| 103 | + |
| 104 | + $this->assertEquals([ |
| 105 | + 'email' => [ |
| 106 | + 'package_size' => 5, |
| 107 | + 'data' => [ |
| 108 | + ['data1'], |
| 109 | + ], |
| 110 | + ], |
| 111 | + ], $this->messenger_queue->get_data()); |
| 112 | + } |
| 113 | + |
| 114 | + public function test_process_no_cache_file() |
| 115 | + { |
| 116 | + $this->assertFileDoesNotExist($this->cache_file); |
| 117 | + $this->assertGreaterThan(5, time() - $this->config['last_queue_run']); |
| 118 | + $this->messenger_queue->init('email', 5); |
| 119 | + $this->assertEquals([ |
| 120 | + 'email' => [ |
| 121 | + 'package_size' => 5, |
| 122 | + 'data' => [], |
| 123 | + ] |
| 124 | + ], $this->messenger_queue->get_data()); |
| 125 | + |
| 126 | + $this->messenger_queue->process(); |
| 127 | + $this->assertFileDoesNotExist($this->cache_file); |
| 128 | + $this->assertLessThan(5, time() - $this->config['last_queue_run']); |
| 129 | + } |
| 130 | + |
| 131 | + public function test_process_no_queue_handling() |
| 132 | + { |
| 133 | + // First save queue data |
| 134 | + $this->assertFileDoesNotExist($this->cache_file); |
| 135 | + $this->messenger_queue->init('email', 5); |
| 136 | + $this->messenger_queue->init('jabber', 10); |
| 137 | + $this->assertEquals([ |
| 138 | + 'email' => [ |
| 139 | + 'package_size' => 5, |
| 140 | + 'data' => [], |
| 141 | + ], |
| 142 | + 'jabber' => [ |
| 143 | + 'package_size' => 10, |
| 144 | + 'data' => [], |
| 145 | + ] |
| 146 | + ], $this->messenger_queue->get_data()); |
| 147 | + |
| 148 | + $this->messenger_queue->put('email', ['data1']); |
| 149 | + $this->messenger_queue->put('jabber', ['data2']); |
| 150 | + $this->messenger_queue->save(); |
| 151 | + $this->assertFileExists($this->cache_file); |
| 152 | + $this->assertEquals([], $this->messenger_queue->get_data()); |
| 153 | + |
| 154 | + $this->config['last_queue_run'] = time() - 1000; |
| 155 | + |
| 156 | + // Process the queue |
| 157 | + $this->messenger_queue->process(); |
| 158 | + } |
| 159 | + |
| 160 | + public function test_process_no_queue_handling_chmod_exception() |
| 161 | + { |
| 162 | + // First save queue data |
| 163 | + $this->assertFileDoesNotExist($this->cache_file); |
| 164 | + $this->messenger_queue->init('email', 5); |
| 165 | + $this->messenger_queue->init('jabber', 10); |
| 166 | + $this->assertEquals([ |
| 167 | + 'email' => [ |
| 168 | + 'package_size' => 5, |
| 169 | + 'data' => [], |
| 170 | + ], |
| 171 | + 'jabber' => [ |
| 172 | + 'package_size' => 10, |
| 173 | + 'data' => [], |
| 174 | + ] |
| 175 | + ], $this->messenger_queue->get_data()); |
| 176 | + |
| 177 | + $this->messenger_queue->put('email', ['data1']); |
| 178 | + $this->messenger_queue->put('jabber', ['data2']); |
| 179 | + $this->messenger_queue->save(); |
| 180 | + $this->assertFileExists($this->cache_file); |
| 181 | + $this->assertEquals([], $this->messenger_queue->get_data()); |
| 182 | + |
| 183 | + $this->config['last_queue_run'] = time() - 1000; |
| 184 | + |
| 185 | + // Override the filesystem to simulate a chmod failure |
| 186 | + $filesystem = $this->getMockBuilder('phpbb\filesystem\filesystem') |
| 187 | + ->disableOriginalConstructor() |
| 188 | + ->onlyMethods(['phpbb_chmod']) |
| 189 | + ->getMock(); |
| 190 | + $filesystem->method('phpbb_chmod') |
| 191 | + ->will($this->throwException(new filesystem_exception('Chmod failed'))); |
| 192 | + $filesystem_reflection = new \ReflectionProperty(queue::class, 'filesystem'); |
| 193 | + $filesystem_reflection->setAccessible(true); |
| 194 | + $filesystem_reflection->setValue($this->messenger_queue, $filesystem); |
| 195 | + |
| 196 | + // Process the queue |
| 197 | + $this->messenger_queue->process(); |
| 198 | + } |
| 199 | + |
| 200 | + public function test_process_complete() |
| 201 | + { |
| 202 | + // First save queue data |
| 203 | + $this->assertFileDoesNotExist($this->cache_file); |
| 204 | + $this->messenger_queue->init('email', 5); |
| 205 | + $this->messenger_queue->init('jabber', 10); |
| 206 | + $this->assertEquals([ |
| 207 | + 'email' => [ |
| 208 | + 'package_size' => 5, |
| 209 | + 'data' => [], |
| 210 | + ], |
| 211 | + 'jabber' => [ |
| 212 | + 'package_size' => 10, |
| 213 | + 'data' => [], |
| 214 | + ] |
| 215 | + ], $this->messenger_queue->get_data()); |
| 216 | + |
| 217 | + $this->messenger_queue->put('email', ['data1']); |
| 218 | + $this->messenger_queue->put('jabber', ['data2']); |
| 219 | + $this->messenger_queue->save(); |
| 220 | + $this->assertFileExists($this->cache_file); |
| 221 | + $this->assertEquals([], $this->messenger_queue->get_data()); |
| 222 | + |
| 223 | + $this->config['last_queue_run'] = time() - 1000; |
| 224 | + |
| 225 | + // Prepare service iterator and messenger methods |
| 226 | + $email_method = $this->getMockBuilder('phpbb\messenger\method\email') |
| 227 | + ->disableOriginalConstructor() |
| 228 | + ->onlyMethods(['get_queue_object_name', 'process_queue']) |
| 229 | + ->getMock(); |
| 230 | + $email_method->method('get_queue_object_name') |
| 231 | + ->willReturn('email'); |
| 232 | + $email_method->method('process_queue') |
| 233 | + ->willReturnCallback(function(array &$queue_data) { |
| 234 | + $this->assertEquals([ |
| 235 | + 'package_size' => 5, |
| 236 | + 'data' => [ |
| 237 | + ['data1'], |
| 238 | + ], |
| 239 | + ], $queue_data['email']); |
| 240 | + unset($queue_data['email']); |
| 241 | + }); |
| 242 | + $jabber_method = $this->getMockBuilder('phpbb\messenger\method\jabber') |
| 243 | + ->disableOriginalConstructor() |
| 244 | + ->onlyMethods(['get_queue_object_name', 'process_queue']) |
| 245 | + ->getMock(); |
| 246 | + $jabber_method->method('get_queue_object_name') |
| 247 | + ->willReturn('jabber'); |
| 248 | + $jabber_method->method('process_queue') |
| 249 | + ->willReturnCallback(function(array &$queue_data) { |
| 250 | + $this->assertEquals([ |
| 251 | + 'package_size' => 10, |
| 252 | + 'data' => [ |
| 253 | + ['data2'], |
| 254 | + ], |
| 255 | + ], $queue_data['jabber']); |
| 256 | + unset($queue_data['jabber']); |
| 257 | + }); |
| 258 | + |
| 259 | + $this->service_collection->method('getIterator') |
| 260 | + ->willReturn(new \ArrayIterator([ |
| 261 | + 'email' => $email_method, |
| 262 | + 'jabber' => $jabber_method, |
| 263 | + ])); |
| 264 | + |
| 265 | + // Process the queue |
| 266 | + $this->messenger_queue->process(); |
| 267 | + $this->assertFileDoesNotExist($this->cache_file); |
| 268 | + } |
| 269 | + |
| 270 | + public function test_save_no_data() |
| 271 | + { |
| 272 | + $this->assertFileDoesNotExist($this->cache_file); |
| 273 | + $this->messenger_queue->save(); |
| 274 | + $this->assertFileDoesNotExist($this->cache_file); |
| 275 | + } |
| 276 | + |
| 277 | + public function test_save() |
| 278 | + { |
| 279 | + $this->assertFileDoesNotExist($this->cache_file); |
| 280 | + $this->messenger_queue->init('email', 5); |
| 281 | + $this->assertEquals([ |
| 282 | + 'email' => [ |
| 283 | + 'package_size' => 5, |
| 284 | + 'data' => [], |
| 285 | + ] |
| 286 | + ], $this->messenger_queue->get_data()); |
| 287 | + |
| 288 | + $this->messenger_queue->put('email', ['data1']); |
| 289 | + $this->messenger_queue->save(); |
| 290 | + $this->assertFileExists($this->cache_file); |
| 291 | + $this->assertEquals([], $this->messenger_queue->get_data()); |
| 292 | + } |
| 293 | + |
| 294 | + public function test_save_twice() |
| 295 | + { |
| 296 | + $this->assertFileDoesNotExist($this->cache_file); |
| 297 | + $this->messenger_queue->init('email', 5); |
| 298 | + $this->assertEquals([ |
| 299 | + 'email' => [ |
| 300 | + 'package_size' => 5, |
| 301 | + 'data' => [], |
| 302 | + ] |
| 303 | + ], $this->messenger_queue->get_data()); |
| 304 | + |
| 305 | + $this->messenger_queue->put('email', ['data1']); |
| 306 | + $this->messenger_queue->put('jabber', ['data2']); |
| 307 | + $this->messenger_queue->save(); |
| 308 | + $this->assertFileExists($this->cache_file); |
| 309 | + $this->assertEquals([], $this->messenger_queue->get_data()); |
| 310 | + |
| 311 | + $this->messenger_queue->put('email', ['data3']); |
| 312 | + $this->messenger_queue->save(); |
| 313 | + $this->assertEquals([], $this->messenger_queue->get_data()); |
| 314 | + } |
| 315 | + |
| 316 | + public function test_save_chmod_fail() |
| 317 | + { |
| 318 | + $this->assertFileDoesNotExist($this->cache_file); |
| 319 | + $this->messenger_queue->init('email', 5); |
| 320 | + $this->assertEquals([ |
| 321 | + 'email' => [ |
| 322 | + 'package_size' => 5, |
| 323 | + 'data' => [], |
| 324 | + ] |
| 325 | + ], $this->messenger_queue->get_data()); |
| 326 | + |
| 327 | + $this->messenger_queue->put('email', ['data1']); |
| 328 | + |
| 329 | + // Override the filesystem to simulate a chmod failure |
| 330 | + $filesystem = $this->getMockBuilder('phpbb\filesystem\filesystem') |
| 331 | + ->disableOriginalConstructor() |
| 332 | + ->onlyMethods(['phpbb_chmod']) |
| 333 | + ->getMock(); |
| 334 | + $filesystem->method('phpbb_chmod') |
| 335 | + ->will($this->throwException(new filesystem_exception('Chmod failed'))); |
| 336 | + $filesystem_reflection = new \ReflectionProperty(queue::class, 'filesystem'); |
| 337 | + $filesystem_reflection->setAccessible(true); |
| 338 | + $filesystem_reflection->setValue($this->messenger_queue, $filesystem); |
| 339 | + |
| 340 | + $this->messenger_queue->save(); |
| 341 | + $this->assertFileExists($this->cache_file); |
| 342 | + $this->assertEquals([], $this->messenger_queue->get_data()); |
| 343 | + } |
| 344 | +} |
0 commit comments