|
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
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @coversDefaultClass \SilverLeague\LogViewer\Task\RemoveOldLogEntriesTask |
|
13
|
|
|
* @package silverstripe-logviewer |
|
14
|
|
|
* @author Robbie Averill <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class RemoveOldLogEntriesTaskTest extends SapphireTest |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritDoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $usesDatabase = true; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritDoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $fixture_file = 'RemoveOldLogEntriesTaskTest.yml'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Test that the configuration properties are set correctly |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testClassProperties() |
|
32
|
|
|
{ |
|
33
|
|
|
$task = new RemoveOldLogEntriesTask; |
|
34
|
|
|
$this->assertContains('Remove LogEntry', $task->getTitle()); |
|
35
|
|
|
$this->assertContains('that are older than', $task->getDescription()); |
|
36
|
|
|
$this->assertSame('RemoveOldLogEntriesTask', Config::inst()->get(RemoveOldLogEntriesTask::class, 'segment')); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Test that the max log age, cron schedule and cron enabled can be set via YAML configuration |
|
41
|
|
|
* |
|
42
|
|
|
* @dataProvider configurableSettingsProvider |
|
43
|
|
|
*/ |
|
44
|
|
|
public function testSettingsAreConfigurable($getter, $setting, $value) |
|
45
|
|
|
{ |
|
46
|
|
|
Config::modify()->set(LogEntry::class, $setting, $value); |
|
47
|
|
|
$this->assertSame($value, (new RemoveOldLogEntriesTask)->{$getter}()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array[] |
|
52
|
|
|
*/ |
|
53
|
|
|
public function configurableSettingsProvider() |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
|
|
['getMaxAge', 'max_log_age', 9], |
|
57
|
|
|
['getCronEnabled', 'cron_enabled', false], |
|
58
|
|
|
['getSchedule', 'cron_schedule', '1 2 3 4 5'] |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Test that when in the cron context and the cron task is disabled that nothing happens |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testNothingHappensInCronContextIfCronIsDisabled() |
|
66
|
|
|
{ |
|
67
|
|
|
$mock = $this |
|
68
|
|
|
->getMockBuilder(RemoveOldLogEntriesTask::class) |
|
69
|
|
|
->setMethods(['removeOldLogs']) |
|
70
|
|
|
->getMock(); |
|
71
|
|
|
|
|
72
|
|
|
$mock |
|
73
|
|
|
->expects($this->never()) |
|
74
|
|
|
->method('removeOldLogs'); |
|
75
|
|
|
|
|
76
|
|
|
Config::modify()->set(LogEntry::class, 'cron_enabled', false); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertFalse($mock->process()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Test that old log entries are removed from the database according to the max age setting. The actual date |
|
83
|
|
|
* used for checking is gathered from the SQL server inside the query, so we can't really mock it - using 1 day |
|
84
|
|
|
* and a new record created now instead. |
|
85
|
|
|
* |
|
86
|
|
|
* @covers ::removeOldLogs |
|
87
|
|
|
* @covers ::run |
|
88
|
|
|
* @covers ::process |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testRemoveOldLogEntries() |
|
91
|
|
|
{ |
|
92
|
|
|
Config::modify()->set(LogEntry::class, 'max_log_age', 1); |
|
93
|
|
|
|
|
94
|
|
|
LogEntry::create(['Entry' => 'Will not be deleted', 'Level' => 'ERROR']); |
|
95
|
|
|
|
|
96
|
|
|
ob_start(); |
|
97
|
|
|
$result = (new RemoveOldLogEntriesTask)->process(); |
|
98
|
|
|
$second = (new RemoveOldLogEntriesTask)->run(new HTTPRequest('GET', '/')); |
|
99
|
|
|
$buffer = ob_get_clean(); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertTrue($result); |
|
102
|
|
|
$this->assertContains('Removed 6 log(s)', $buffer); |
|
103
|
|
|
// Nothing to do the second time |
|
104
|
|
|
$this->assertFalse($second); |
|
105
|
|
|
$this->assertContains('Removed 0 log(s)', $buffer); |
|
106
|
|
|
$this->assertContains('older than 1 day(s)', $buffer); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|