-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAddRemoveTest.php
More file actions
211 lines (177 loc) · 9.28 KB
/
AddRemoveTest.php
File metadata and controls
211 lines (177 loc) · 9.28 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
use WP_CLI\Tests\TestCase;
class AddRemoveTest extends TestCase {
protected static $test_config_path;
protected static $config_transformer;
protected static $raw_data = array();
protected static $string_data = array();
public static function set_up_before_class() {
self::$raw_data = explode( PHP_EOL, file_get_contents( __DIR__ . '/fixtures/raw-data.txt' ) );
self::$string_data = explode( PHP_EOL, file_get_contents( __DIR__ . '/fixtures/string-data.txt' ) );
if ( version_compare( PHP_VERSION, '7.0', '>=' ) ) {
self::$raw_data = array_merge( self::$raw_data, explode( PHP_EOL, file_get_contents( __DIR__ . '/fixtures/raw-data-extra.txt' ) ) );
}
self::$test_config_path = __DIR__ . '/wp-config-test-add.php';
copy( __DIR__ . '/fixtures/wp-config-example.php', self::$test_config_path );
self::$config_transformer = new WPConfigTransformer( self::$test_config_path );
}
public static function tear_down_after_class() {
unlink( self::$test_config_path );
}
public function testAddRawConstants() {
foreach ( self::$raw_data as $d => $data ) {
$name = "TEST_CONST_ADD_RAW_{$d}";
$this->assertTrue( self::$config_transformer->add( 'constant', $name, $data, array( 'raw' => true ) ), $name );
$this->assertTrue( self::$config_transformer->exists( 'constant', $name ), $name );
}
}
public function testAddStringConstants() {
foreach ( self::$string_data as $d => $data ) {
$name = "TEST_CONST_ADD_STRING_{$d}";
$this->assertTrue( self::$config_transformer->add( 'constant', $name, $data ), $name );
$this->assertTrue( self::$config_transformer->exists( 'constant', $name ), $name );
}
}
public function testAddRawVariables() {
foreach ( self::$raw_data as $d => $data ) {
$name = "test_var_add_raw_{$d}";
$this->assertTrue( self::$config_transformer->add( 'variable', $name, $data, array( 'raw' => true ) ), "\${$name}" );
$this->assertTrue( self::$config_transformer->exists( 'variable', $name ), "\${$name}" );
}
}
public function testAddStringVariables() {
foreach ( self::$string_data as $d => $data ) {
$name = "test_var_add_string_{$d}";
$this->assertTrue( self::$config_transformer->add( 'variable', $name, $data ), "\${$name}" );
$this->assertTrue( self::$config_transformer->exists( 'variable', $name ), "\${$name}" );
}
}
public function testConstantNoAddIfExists() {
$name = 'TEST_CONST_ADD_EXISTS';
$this->assertTrue( self::$config_transformer->add( 'constant', $name, 'foo' ), $name );
$this->assertTrue( self::$config_transformer->exists( 'constant', $name ), $name );
$this->assertFalse( self::$config_transformer->add( 'constant', $name, 'bar' ), $name );
}
public function testVariableNoAddIfExists() {
$name = 'test_var_add_exists';
$this->assertTrue( self::$config_transformer->add( 'variable', $name, 'foo' ), "\${$name}" );
$this->assertTrue( self::$config_transformer->exists( 'variable', $name ), "\${$name}" );
$this->assertFalse( self::$config_transformer->add( 'variable', $name, 'bar' ), "\${$name}" );
}
public function testConfigValues() {
require_once self::$test_config_path;
foreach ( self::$raw_data as $d => $data ) {
// Convert string to a real value.
eval( "\$data = $data;" ); // phpcs:ignore Squiz.PHP.Eval.Discouraged
// Raw Constants
$name = "TEST_CONST_ADD_RAW_{$d}";
$this->assertTrue( defined( $name ), $name );
$this->assertEquals( $data, constant( $name ), $name );
// Raw Variables
$name = "test_var_add_raw_{$d}";
$this->assertTrue( ( isset( ${$name} ) || is_null( ${$name} ) ), "\${$name}" );
$this->assertEquals( $data, ${$name}, "\${$name}" );
}
foreach ( self::$string_data as $d => $data ) {
// String Constants
$name = "TEST_CONST_ADD_STRING_{$d}";
$this->assertTrue( defined( $name ), $name );
$this->assertEquals( $data, constant( $name ), $name );
// String Variables
$name = "test_var_add_string_{$d}";
$this->assertTrue( ( isset( ${$name} ) || is_null( ${$name} ) ), "\${$name}" );
$this->assertEquals( $data, ${$name}, "\${$name}" );
}
$this->assertTrue( defined( 'TEST_CONST_ADD_EXISTS' ), 'TEST_CONST_ADD_EXISTS' );
$this->assertEquals( 'foo', constant( 'TEST_CONST_ADD_EXISTS' ), 'TEST_CONST_ADD_EXISTS' );
$this->assertTrue( ( isset( $test_var_add_exists ) || is_null( $test_var_add_exists ) ), '$test_var_update_add_missing' );
$this->assertEquals( 'foo', $test_var_add_exists, '$test_var_add_exists' );
}
public function testRemoveRawConstants() {
foreach ( self::$raw_data as $d => $data ) {
$name = "TEST_CONST_ADD_RAW_{$d}";
$this->assertTrue( self::$config_transformer->exists( 'constant', $name ), $name );
$this->assertTrue( self::$config_transformer->remove( 'constant', $name ), $name );
$this->assertFalse( self::$config_transformer->exists( 'constant', $name ), $name );
}
}
public function testRemoveStringConstants() {
foreach ( self::$string_data as $d => $data ) {
$name = "TEST_CONST_ADD_STRING_{$d}";
$this->assertTrue( self::$config_transformer->exists( 'constant', $name ), $name );
$this->assertTrue( self::$config_transformer->remove( 'constant', $name ), $name );
$this->assertFalse( self::$config_transformer->exists( 'constant', $name ), $name );
}
}
public function testRemoveRawVariables() {
foreach ( self::$raw_data as $d => $data ) {
$name = "test_var_add_raw_{$d}";
$this->assertTrue( self::$config_transformer->exists( 'variable', $name ), "\${$name}" );
$this->assertTrue( self::$config_transformer->remove( 'variable', $name ), "\${$name}" );
$this->assertFalse( self::$config_transformer->exists( 'variable', $name ), "\${$name}" );
}
}
public function testRemoveStringVariables() {
foreach ( self::$string_data as $d => $data ) {
$name = "test_var_add_string_{$d}";
$this->assertTrue( self::$config_transformer->exists( 'variable', $name ), "\${$name}" );
$this->assertTrue( self::$config_transformer->remove( 'variable', $name ), "\${$name}" );
$this->assertFalse( self::$config_transformer->exists( 'variable', $name ), "\${$name}" );
}
}
public function testRemoveConstantWithConcatenation() {
// Set up the initial state by defining a constant with concatenation
$name = 'TEST_USER_PATH';
$value = "'/var/www/' . get_current_user()";
$this->assertTrue( self::$config_transformer->add( 'constant', $name, $value, array( 'raw' => true ) ), "Adding constant {$name}" );
// Define another constant to check if it remains untouched
$second_name = 'TEST_THIS_AND';
$second_value = "md5('that')";
$this->assertTrue( self::$config_transformer->add( 'constant', $second_name, $second_value, array( 'raw' => true ) ), "Adding constant {$second_name}" );
// Remove the first constant and check the result
$this->assertTrue( self::$config_transformer->remove( 'constant', $name ), "Removing constant {$name}" );
$this->assertFalse( self::$config_transformer->exists( 'constant', $name ), "Check {$name} does not exist" );
// Ensure the second constant is still present after the removal
$this->assertTrue( self::$config_transformer->exists( 'constant', $second_name ), "Check {$second_name} still exists" );
}
public function testAddConstantNoPlacementAnchor() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Unable to locate placement anchor.' );
self::$config_transformer->add( 'constant', 'TEST_CONST_ADD_NO_ANCHOR', 'foo', array( 'anchor' => 'nothingtoseehere' ) );
}
public function testAddVariableNoPlacementAnchor() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Unable to locate placement anchor.' );
self::$config_transformer->add( 'variable', 'test_var_add_no_anchor', 'foo', array( 'anchor' => 'nothingtoseehere' ) );
}
public function testAddConstantNonString() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Config value must be a string.' );
self::$config_transformer->add( 'constant', 'TEST_CONST_ADD_NON_STRING', true );
}
public function testAddVariableNonString() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Config value must be a string.' );
self::$config_transformer->add( 'variable', 'test_var_add_non_string', true );
}
public function testAddConstantEmptyStringRaw() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Raw value for empty string not supported.' );
self::$config_transformer->add( 'constant', 'TEST_CONST_ADD_EMPTY_STRING_RAW', '', array( 'raw' => true ) );
}
public function testAddVariableEmptyStringRaw() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Raw value for empty string not supported.' );
self::$config_transformer->add( 'variable', 'test_var_add_empty_string_raw', '', array( 'raw' => true ) );
}
public function testAddConstantWhitespaceStringRaw() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Raw value for empty string not supported.' );
self::$config_transformer->add( 'constant', 'TEST_CONST_ADD_WHITESPACE_STRING_RAW', ' ', array( 'raw' => true ) );
}
public function testAddVariableWhitespaceStringRaw() {
$this->expectException( Exception::class );
$this->expectExceptionMessage( 'Raw value for empty string not supported.' );
self::$config_transformer->add( 'variable', 'test_var_add_whitespace_string_raw', ' ', array( 'raw' => true ) );
}
}