-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathtest-file-manager.php
146 lines (118 loc) · 3.51 KB
/
test-file-manager.php
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
<?php
/**
* Class LoggerTest
*
* @package WPDiscourse
*/
namespace WPDiscourse\Test;
use WPDiscourse\Logs\FileManager;
use WPDiscourse\Test\UnitTest;
/**
* Logger test case.
*/
class FileManagerTest extends UnitTest {
/**
* Setup test class.
*/
public function setUp(): void {
parent::setUp();
static::reset_permissions();
}
/**
* Teardown test class.
*/
public function tearDown(): void {
parent::tearDown();
static::reset_permissions();
}
/**
* Validation creates discourse uploads folder and .htaccess file if they don't exist.
*/
public function test_validation_uploads_creation() {
$file_manager = new FileManager();
$this->recursive_rmdir( $file_manager->upload_dir );
$this->assertDirectoryDoesNotExist( $file_manager->upload_dir );
$file_manager->validate();
$this->assertDirectoryExists( $file_manager->upload_dir );
$this->assertFileExists( $file_manager->upload_dir . '/.htaccess' );
}
/**
* Validation creates discourse logs folder and .htaccess file if they don't exist.
*/
public function test_validation_logs_creation() {
$file_manager = new FileManager();
$this->recursive_rmdir( $file_manager->logs_dir );
$this->assertDirectoryDoesNotExist( $file_manager->logs_dir );
$file_manager->validate();
$this->assertDirectoryExists( $file_manager->logs_dir );
$this->assertFileExists( $file_manager->logs_dir . '/.htaccess' );
}
/**
* It is ready if validation passes.
*/
public function test_validation_ready() {
$file_manager = new FileManager();
$this->assertTrue( $file_manager->validate() );
$this->assertTrue( $file_manager->ready() );
}
/**
* It is not ready if validation is not run
*/
public function test_validation_not_ready() {
$file_manager = new FileManager();
$this->assertFalse( $file_manager->ready() );
}
/**
* Validation will not pass if wp uploads directory is not writable
*/
public function test_validation_when_wp_uploads_not_writable() {
$file_manager = new FileManager();
chmod( wp_upload_dir()['basedir'], 0444 );
$this->assertFalse( $file_manager->validate() );
$this->assertFalse( $file_manager->ready() );
}
/**
* Validation will not pass if all necessary folders and files are not present and writable.
*/
public function test_validation_when_folders_partially_restricted() {
$file_manager = new FileManager();
$this->assertTrue( $file_manager->validate() );
$this->assertTrue( $file_manager->ready() );
chmod( $file_manager->logs_dir, 0444 );
$this->assertFalse( $file_manager->validate() );
$this->assertFalse( $file_manager->ready() );
}
/**
* Reset directory permissions.
*/
protected function reset_permissions() {
$file_manager = new FileManager();
chmod( wp_upload_dir()['basedir'], 0744 );
if ( is_dir( $file_manager->upload_dir ) ) {
chmod( $file_manager->upload_dir, 0744 );
}
if ( is_dir( $file_manager->logs_dir ) ) {
chmod( $file_manager->logs_dir, 0744 );
}
}
/**
* Recursively remove directory.
*
* @param string $dir Path of directory to remove.
*/
protected function recursive_rmdir( $dir ) {
if ( is_dir( $dir ) ) {
$objects = scandir( $dir );
foreach ( $objects as $object ) {
if ( '.' !== $object && '..' !== $object ) {
if ( is_dir( $dir . DIRECTORY_SEPARATOR . $object ) ) {
$this->recursive_rmdir( $dir . DIRECTORY_SEPARATOR . $object );
} else {
unlink( $dir . DIRECTORY_SEPARATOR . $object );
}
}
}
rmdir( $dir );
}
}
}