|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Ideas extension for 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 | + */ |
| 10 | + |
| 11 | +namespace phpbb\ideas\tests\cron; |
| 12 | + |
| 13 | +class cron_test extends \phpbb_test_case |
| 14 | +{ |
| 15 | + /** @var \phpbb\config\config */ |
| 16 | + protected $config; |
| 17 | + |
| 18 | + /** @var \phpbb\ideas\cron\prune_orphaned_ideas */ |
| 19 | + protected $cron_task; |
| 20 | + |
| 21 | + /** @var \phpbb\ideas\factory\ideas|\PHPUnit\Framework\MockObject\MockObject */ |
| 22 | + protected $ideas; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->config = new \phpbb\config\config(['ideas_cron_last_run' => 0]); |
| 29 | + |
| 30 | + $this->ideas = $this->getMockBuilder('\phpbb\ideas\factory\ideas') |
| 31 | + ->disableOriginalConstructor() |
| 32 | + ->getMock(); |
| 33 | + |
| 34 | + $this->cron_task = new \phpbb\ideas\cron\prune_orphaned_ideas($this->config, $this->ideas); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Test the cron task runs correctly |
| 39 | + */ |
| 40 | + public function test_run() |
| 41 | + { |
| 42 | + // Get last run time stored for cron |
| 43 | + $cron_last_run = $this->config['ideas_cron_last_run']; |
| 44 | + |
| 45 | + // Expect ideas method delete_orphans is called once |
| 46 | + $this->ideas->expects(self::once()) |
| 47 | + ->method('delete_orphans'); |
| 48 | + |
| 49 | + // Run the cron task |
| 50 | + $this->cron_task->run(); |
| 51 | + |
| 52 | + // Now the last run time should be greater than before the test |
| 53 | + self::assertGreaterThan($cron_last_run, $this->config['ideas_cron_last_run']); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Data set for test_should_run |
| 58 | + * |
| 59 | + * @return array Array of test data |
| 60 | + */ |
| 61 | + public function should_run_data() |
| 62 | + { |
| 63 | + return array( |
| 64 | + array(time(), false), |
| 65 | + array(strtotime('1 day ago'), false), |
| 66 | + array(strtotime('8 days ago'), true), |
| 67 | + array('', true), |
| 68 | + array(0, true), |
| 69 | + array(null, true), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Test cron task should run after 1 week (7 days) |
| 75 | + * |
| 76 | + * @dataProvider should_run_data |
| 77 | + */ |
| 78 | + public function test_should_run($time, $expected) |
| 79 | + { |
| 80 | + $this->config['ideas_cron_last_run'] = $time; |
| 81 | + |
| 82 | + self::assertSame($expected, $this->cron_task->should_run()); |
| 83 | + } |
| 84 | +} |
0 commit comments