forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClearLogsTest.php
More file actions
178 lines (137 loc) · 5.64 KB
/
ClearLogsTest.php
File metadata and controls
178 lines (137 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Commands\Housekeeping;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockInputOutput;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
/**
* @internal
*/
#[Group('Others')]
final class ClearLogsTest extends CIUnitTestCase
{
use StreamFilterTrait;
private string $date;
protected function setUp(): void
{
parent::setUp();
// test runs on other tests may log errors since default threshold
// is now 4, so set this to a safe distance
$this->date = date('Y-m-d', strtotime('+1 year'));
command('logs:clear --force');
$this->resetStreamFilterBuffer();
$this->createDummyLogFiles();
}
protected function tearDown(): void
{
command('logs:clear --force');
$this->resetStreamFilterBuffer();
CLI::reset();
parent::tearDown();
}
private function createDummyLogFiles(): void
{
$date = $this->date;
$path = WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$date}.log";
// create 10 dummy log files
for ($i = 0; $i < 10; $i++) {
$newDate = date('Y-m-d', strtotime("+1 year -{$i} day"));
$path = str_replace($date, $newDate, $path);
file_put_contents($path, 'Lorem ipsum');
$date = $newDate;
}
}
public function testClearLogsUsingForce(): void
{
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
command('logs:clear --force');
$this->assertFileDoesNotExist(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . 'index.html');
$this->assertSame("Logs cleared.\n", preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()));
}
public function testClearLogsAbortsClearWithoutForce(): void
{
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$io = new MockInputOutput();
$io->setInputs(['n']);
CLI::setInputOutput($io);
command('logs:clear');
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$this->assertSame(
<<<'EOT'
Deleting logs aborted.
If you want, use the "--force" option to force delete all log files.
EOT,
preg_replace('/\e\[[^m]+m/', '', $io->getOutput(2) . $io->getOutput(3)),
);
}
public function testClearLogsAbortsClearWithoutForceWithDefaultAnswer(): void
{
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$io = new MockInputOutput();
$io->setInputs(['']);
CLI::setInputOutput($io);
command('logs:clear');
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$this->assertSame(
<<<'EOT'
Deleting logs aborted.
If you want, use the "--force" option to force delete all log files.
EOT,
preg_replace('/\e\[[^m]+m/', '', $io->getOutput(2) . $io->getOutput(3)),
);
}
public function testClearLogsWithoutForceButWithConfirmation(): void
{
$this->assertFileExists(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$io = new MockInputOutput();
$io->setInputs(['y']);
CLI::setInputOutput($io);
command('logs:clear');
$this->assertFileDoesNotExist(WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log");
$this->assertSame("Logs cleared.\n", preg_replace('/\e\[[^m]+m/', '', $io->getOutput(2)));
}
#[RequiresOperatingSystem('Darwin|Linux')]
public function testClearLogsFailsOnChmodFailure(): void
{
$path = WRITEPATH . 'logs' . DIRECTORY_SEPARATOR . "log-{$this->date}.log";
file_put_contents($path, 'Lorem ipsum');
// Attempt to make the file itself undeletable by setting the
// immutable/uchg flag on supported platforms.
$immutableSet = false;
if (str_starts_with(PHP_OS, 'Darwin')) {
@exec(sprintf('chflags uchg %s', escapeshellarg($path)), $output, $rc);
$immutableSet = $rc === 0;
} else {
// Try chattr on Linux with sudo (for containerized environments)
@exec('which chattr', $whichOut, $whichRc);
if ($whichRc === 0) {
@exec(sprintf('sudo chattr +i %s', escapeshellarg($path)), $output, $rc);
$immutableSet = $rc === 0;
}
}
if (! $immutableSet) {
$this->markTestSkipped('Cannot set file immutability in this environment');
}
command('logs:clear --force');
// Restore attributes so other tests are not affected.
if (str_starts_with(PHP_OS, 'Darwin')) {
@exec(sprintf('chflags nouchg %s', escapeshellarg($path)));
} else {
@exec(sprintf('sudo chattr -i %s', escapeshellarg($path)));
}
$this->assertFileExists($path);
$this->assertSame("Error in deleting the logs files.\n", preg_replace('/\e\[[^m]+m/', '', $this->getStreamFilterBuffer()));
}
}