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