| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverLeague\LogViewer\Tests\Task; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverLeague\LogViewer\Model\LogEntry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverLeague\LogViewer\Task\RemoveOldLogEntriesTask; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Silverstripe\ORM\FieldType\DBDatetime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @coversDefaultClass \SilverLeague\LogViewer\Task\RemoveOldLogEntriesTask | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package silverstripe-logviewer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author  Robbie Averill <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class RemoveOldLogEntriesTaskTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $usesDatabase = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected static $fixture_file = 'RemoveOldLogEntriesTaskTest.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         Config::inst()->nest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         DBDatetime::set_mock_now('2017-01-08 00:58:00'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Test that the configuration properties are set correctly | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function testClassProperties() | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $task = new RemoveOldLogEntriesTask; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertContains('Remove LogEntry', $task->getTitle()); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertContains('Will run as a cron task', $task->getDescription()); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $this->assertSame('RemoveOldLogEntriesTask', Config::inst()->get(RemoveOldLogEntriesTask::class, 'segment')); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Test that the max log age, cron schedule and cron enabled can be set via YAML configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @dataProvider configurableSettingsProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function testSettingsAreConfigurable($getter, $setting, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         Config::inst()->update('LogViewer', $setting, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertSame($value, (new RemoveOldLogEntriesTask)->{$getter}()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return array[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function configurableSettingsProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ['getMaxAge', 'max_log_age', 9], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ['getCronEnabled', 'cron_enabled', false], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ['getSchedule', 'cron_schedule', '1 2 3 4 5'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Test that when in the cron context and the cron task is disabled that nothing happens | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function testNothingHappensInCronContextIfCronIsDisabled() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $mock = $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ->getMockBuilder(RemoveOldLogEntriesTask::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             ->setMethods(['removeOldLogs']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $mock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ->expects($this->never()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             ->method('removeOldLogs'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         Config::inst()->update('LogViewer', 'cron_enabled', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $this->assertFalse($mock->process()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * Test that old log entries are removed from the database according to the max age setting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @covers ::removeOldLogs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @covers ::run | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @covers ::process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function testRemoveOldLogEntries() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         Config::inst()->update('LogViewer', 'max_log_age', 14); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ob_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $result = (new RemoveOldLogEntriesTask)->process(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $second = (new RemoveOldLogEntriesTask)->run(new HTTPRequest('GET', '/')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $buffer = ob_get_clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->assertTrue($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->assertContains('Removed 3 logs', $buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         // Nothing to do the second time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->assertFalse($second); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->assertContains('Removed 0 logs', $buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->assertContains('older than 14 days', $buffer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         Config::inst()->unnest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |  |