| 25 | |
| 26 | function test_extract_from_markers_returns_empty_array_when_file_not_found() { |
| 27 | |
| 28 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 29 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 30 | |
| 31 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 32 | |
| 33 | // Set the mock WP_Filesystem_Direct object to return false when the function 'exists' is called |
| 34 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( false ) ); |
| 35 | |
| 36 | $results = extract_from_markers( 'file', 'marker_1', $mock_wp_filesystem ); |
| 37 | |
| 38 | $this->assertEmpty( $results ); |
| 39 | } |
| 40 | |
| 41 | function test_extract_from_markers_returns_empty_array_for_unfound_marker() { |
| 42 | |
| 43 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 44 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 45 | |
| 46 | $file_contents_without_unfound_marker = array( |
| 47 | "# BEGIN marker_10\n", |
| 48 | "marker_10 line 1\n", |
| 49 | "marker_10 line 2\n", |
| 50 | "marker_10 line 3\n", |
| 51 | "# END marker_10\n", |
| 52 | "\n", |
| 53 | "# BEGIN marker_1\n", |
| 54 | "marker_1 line 1\n", |
| 55 | "marker_1 line 2\n", |
| 56 | "marker_1 line 3\n", |
| 57 | "# END marker_1\n" |
| 58 | ); |
| 59 | |
| 60 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 61 | |
| 62 | // Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_without_unfound_marker as the file contents. |
| 63 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 64 | $mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_without_unfound_marker ) ); |
| 65 | |
| 66 | $results = extract_from_markers( 'file', 'unfound_marker', $mock_wp_filesystem ); |
| 67 | |
| 68 | $this->assertEmpty( $results ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * This test checks that the markers are checked based on the full string. |
| 73 | * |
| 74 | * In the test, we create two marker sections, first 'marker_10', then 'marker_1'. |
| 75 | * If extract_from_markers is working properly, passing in 'marker_1' will return |
| 76 | * the contents between '# BEGIN marker_1' and '# END marker_1'. |
| 77 | */ |
| 78 | function test_extract_from_markers_checks_full_marker_line() { |
| 79 | |
| 80 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 81 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 82 | |
| 83 | $file_contents_with_similar_markers = array( |
| 84 | "# BEGIN marker_10\n", |
| 85 | "marker_10 line 1\n", |
| 86 | "marker_10 line 2\n", |
| 87 | "marker_10 line 3\n", |
| 88 | "# END marker_10\n", |
| 89 | "\n", |
| 90 | "# BEGIN marker_1\n", |
| 91 | "marker_1 line 1\n", |
| 92 | "marker_1 line 2\n", |
| 93 | "marker_1 line 3\n", |
| 94 | "# END marker_1\n" |
| 95 | ); |
| 96 | |
| 97 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 98 | |
| 99 | // Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_with_similar_markers as the file contents. |
| 100 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 101 | $mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_with_similar_markers ) ); |
| 102 | |
| 103 | $results = extract_from_markers( 'file', 'marker_1', $mock_wp_filesystem ); |
| 104 | |
| 105 | $expected = array( |
| 106 | "marker_1 line 1", |
| 107 | "marker_1 line 2", |
| 108 | "marker_1 line 3" |
| 109 | ); |
| 110 | |
| 111 | $this->assertEquals( $expected, $results ); |
| 112 | } |
| 113 | |
| 114 | function test_extract_from_markers_strips_newline_characters() { |
| 115 | |
| 116 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 117 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 118 | |
| 119 | $file_contents_with_newline_char = array( |
| 120 | "# BEGIN marker_1\n", |
| 121 | "marker_1 line 1\n", |
| 122 | "marker_1 line 2\n", |
| 123 | "marker_1 line 3\n", |
| 124 | "# END marker_1\n", |
| 125 | "\n", |
| 126 | "# BEGIN marker_2\n", |
| 127 | "marker_2 line 1\n", |
| 128 | "marker_2 line 2\n", |
| 129 | "marker_2 line 3\n", |
| 130 | "# END marker_2\n" |
| 131 | ); |
| 132 | |
| 133 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 134 | |
| 135 | // Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_with_newline_char as the file contents. |
| 136 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 137 | $mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_with_newline_char ) ); |
| 138 | |
| 139 | $results = extract_from_markers( 'file', 'marker_1', $mock_wp_filesystem ); |
| 140 | |
| 141 | $expected = array( |
| 142 | "marker_1 line 1", |
| 143 | "marker_1 line 2", |
| 144 | "marker_1 line 3" |
| 145 | ); |
| 146 | |
| 147 | $this->assertEquals( $expected, $results ); |
| 148 | } |
| 149 | |
| 150 | function test_extract_from_markers_strips_carriage_returns() { |
| 151 | |
| 152 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 153 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 154 | |
| 155 | $file_contents_with_carriage_return = array( |
| 156 | "# BEGIN marker_1\r", |
| 157 | "marker_1 line 1\r", |
| 158 | "marker_1 line 2\r", |
| 159 | "marker_1 line 3\r", |
| 160 | "# END marker_1\r", |
| 161 | "\r", |
| 162 | "# BEGIN marker_2\r", |
| 163 | "marker_2 line 1\r", |
| 164 | "marker_2 line 2\r", |
| 165 | "marker_2 line 3\r", |
| 166 | "# END marker_2\r" |
| 167 | ); |
| 168 | |
| 169 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 170 | |
| 171 | // Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_with_carriage_return as the file contents. |
| 172 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 173 | $mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_with_carriage_return ) ); |
| 174 | |
| 175 | $results = extract_from_markers( 'file', 'marker_2', $mock_wp_filesystem ); |
| 176 | |
| 177 | $expected = array( |
| 178 | "marker_2 line 1", |
| 179 | "marker_2 line 2", |
| 180 | "marker_2 line 3" |
| 181 | ); |
| 182 | |
| 183 | $this->assertEquals( $expected, $results ); |
| 184 | } |
| 185 | |
| 186 | function test_extract_from_markers_strips_carriage_returns_and_newline_characters() { |
| 187 | |
| 188 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 189 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 190 | |
| 191 | $file_contents_with_carriage_return_and_newline_char = array( |
| 192 | "# BEGIN marker_1\r\n", |
| 193 | "marker_1 line 1\r\n", |
| 194 | "marker_1 line 2\r\n", |
| 195 | "marker_1 line 3\r\n", |
| 196 | "# END marker_1\r\n", |
| 197 | "\r\n", |
| 198 | "# BEGIN marker_2\r\n", |
| 199 | "marker_2 line 1\r\n", |
| 200 | "marker_2 line 2\r\n", |
| 201 | "marker_2 line 3\r\n", |
| 202 | "# END marker_2\r\n" |
| 203 | ); |
| 204 | |
| 205 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 206 | |
| 207 | // Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_with_carriage_return_and_newline_char as the file contents. |
| 208 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 209 | $mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_with_carriage_return_and_newline_char ) ); |
| 210 | |
| 211 | $results = extract_from_markers( 'file', 'marker_1', $mock_wp_filesystem ); |
| 212 | |
| 213 | $expected = array( |
| 214 | "marker_1 line 1", |
| 215 | "marker_1 line 2", |
| 216 | "marker_1 line 3" |
| 217 | ); |
| 218 | |
| 219 | $this->assertEquals( $expected, $results ); |
| 220 | |
| 221 | } |
| 222 | |
| 223 | function test_insert_with_markers_returns_false_when_file_exists_but_is_not_writable() { |
| 224 | |
| 225 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 226 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 227 | |
| 228 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 229 | |
| 230 | // Set the mock WP_Filesystem_Direct object to say the file exists, but is not writable |
| 231 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 232 | $mock_wp_filesystem->expects( $this->once() )->method( 'is_writable' )->will( $this->returnValue( false ) ); |
| 233 | |
| 234 | $insertion = array( '1', '2', '3' ); |
| 235 | |
| 236 | $results = insert_with_markers( 'file', 'marker', $insertion, $mock_wp_filesystem ); |
| 237 | |
| 238 | $this->assertFalse( $results ); |
| 239 | } |
| 240 | |
| 241 | function test_insert_with_markers_formats_insertion_properly() { |
| 242 | |
| 243 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 244 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 245 | |
| 246 | $filename = 'file'; |
| 247 | $marker = 'marker'; |
| 248 | $insertion = array( '1', '2', '3' ); |
| 249 | |
| 250 | $expected_contents = implode( "\n", array( |
| 251 | "", |
| 252 | "# BEGIN {$marker}", |
| 253 | '1', |
| 254 | '2', |
| 255 | '3', |
| 256 | "# END {$marker}" |
| 257 | ) ); |
| 258 | |
| 259 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 260 | |
| 261 | // Set the mock WP_Filesystem_Direct object to say the file doesn't exist |
| 262 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( false ) ); |
| 263 | // Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents. This is an assertion. |
| 264 | $mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( true ) ); |
| 265 | |
| 266 | $results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem ); |
| 267 | |
| 268 | $this->assertTrue( $results ); |
| 269 | } |
| 270 | |
| 271 | function test_insert_with_markers_adds_new_marker_insertion_after_current_file_contents() { |
| 272 | |
| 273 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 274 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 275 | |
| 276 | $file_contents = array( |
| 277 | "# BEGIN marker_1\n", |
| 278 | "marker_1 line 1\n", |
| 279 | "marker_1 line 2\n", |
| 280 | "marker_1 line 3\n", |
| 281 | "# END marker_1" |
| 282 | ); |
| 283 | |
| 284 | $filename = 'file'; |
| 285 | $marker = 'marker'; |
| 286 | $insertion = array( '1', '2', '3' ); |
| 287 | |
| 288 | $expected_contents = implode( "\n", array( |
| 289 | "# BEGIN marker_1", |
| 290 | "marker_1 line 1", |
| 291 | "marker_1 line 2", |
| 292 | "marker_1 line 3", |
| 293 | "# END marker_1", |
| 294 | "", |
| 295 | "# BEGIN {$marker}", |
| 296 | '1', |
| 297 | '2', |
| 298 | '3', |
| 299 | "# END {$marker}" |
| 300 | ) ); |
| 301 | |
| 302 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 303 | |
| 304 | // Set the mock WP_Filesystem_Direct object to say the file exist and is writable. |
| 305 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 306 | $mock_wp_filesystem->expects( $this->once() )->method( 'is_writable' )->will( $this->returnValue( true ) ); |
| 307 | |
| 308 | // Setup the Mock WP_Filesystem_Direct object to return $file_contents when 'get_contents_array' is called |
| 309 | $mock_wp_filesystem->expects( $this->once() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents ) ); |
| 310 | |
| 311 | // Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents. This is an assertion. |
| 312 | $mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( true ) ); |
| 313 | |
| 314 | $results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem ); |
| 315 | |
| 316 | $this->assertTrue( $results ); |
| 317 | } |
| 318 | |
| 319 | function test_insert_with_markers_overwrites_specified_marker_section_with_insertion() { |
| 320 | |
| 321 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 322 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 323 | |
| 324 | $file_contents = array( |
| 325 | "# BEGIN marker_1\n", |
| 326 | "marker_1 line 1\n", |
| 327 | "marker_1 line 2\n", |
| 328 | "marker_1 line 3\n", |
| 329 | "# END marker_1\n", |
| 330 | "\n", |
| 331 | "# BEGIN marker\n", |
| 332 | "marker line 1\n", |
| 333 | "marker line 2\n", |
| 334 | "marker line 3\n", |
| 335 | "# END marker" |
| 336 | ); |
| 337 | |
| 338 | $filename = 'file'; |
| 339 | $marker = 'marker'; |
| 340 | $insertion = array( '1', '2', '3' ); |
| 341 | |
| 342 | $expected_contents = implode( "\n", array( |
| 343 | "# BEGIN marker_1", |
| 344 | "marker_1 line 1", |
| 345 | "marker_1 line 2", |
| 346 | "marker_1 line 3", |
| 347 | "# END marker_1", |
| 348 | "", |
| 349 | "# BEGIN {$marker}", |
| 350 | '1', |
| 351 | '2', |
| 352 | '3', |
| 353 | "# END {$marker}" |
| 354 | ) ); |
| 355 | |
| 356 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 357 | |
| 358 | // Set the mock WP_Filesystem_Direct object to say the file exist and is writable. |
| 359 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 360 | $mock_wp_filesystem->expects( $this->once() )->method( 'is_writable' )->will( $this->returnValue( true ) ); |
| 361 | |
| 362 | // Setup the Mock WP_Filesystem_Direct object to return $file_contents when 'get_contents_array' is called |
| 363 | $mock_wp_filesystem->expects( $this->once() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents ) ); |
| 364 | |
| 365 | // Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents. This is an assertion. |
| 366 | $mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( true ) ); |
| 367 | |
| 368 | $results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem ); |
| 369 | |
| 370 | $this->assertTrue( $results ); |
| 371 | } |
| 372 | |
| 373 | function test_insert_with_markers_strips_newline_characters_and_or_carriage_returns() { |
| 374 | |
| 375 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 376 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 377 | |
| 378 | $file_contents = array( |
| 379 | "# BEGIN marker_1\r\n", |
| 380 | "marker_1 line 1\n", |
| 381 | "marker_1 line 2\n", |
| 382 | "marker_1 line 3\n", |
| 383 | "# END marker_1\r", |
| 384 | "\n", |
| 385 | "# BEGIN marker\r", |
| 386 | "marker line 1\n", |
| 387 | "marker line 2\n", |
| 388 | "marker line 3\n", |
| 389 | "# END marker" |
| 390 | ); |
| 391 | |
| 392 | $filename = 'file'; |
| 393 | $marker = 'marker'; |
| 394 | $insertion = array( '1', '2', '3' ); |
| 395 | |
| 396 | $expected_contents = implode( "\n", array( |
| 397 | "# BEGIN marker_1", |
| 398 | "marker_1 line 1", |
| 399 | "marker_1 line 2", |
| 400 | "marker_1 line 3", |
| 401 | "# END marker_1", |
| 402 | "", |
| 403 | "# BEGIN {$marker}", |
| 404 | '1', |
| 405 | '2', |
| 406 | '3', |
| 407 | "# END {$marker}" |
| 408 | ) ); |
| 409 | |
| 410 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 411 | |
| 412 | // Set the mock WP_Filesystem_Direct object to say the file exist and is writable. |
| 413 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) ); |
| 414 | $mock_wp_filesystem->expects( $this->once() )->method( 'is_writable' )->will( $this->returnValue( true ) ); |
| 415 | |
| 416 | // Setup the Mock WP_Filesystem_Direct object to return $file_contents when 'get_contents_array' is called |
| 417 | $mock_wp_filesystem->expects( $this->once() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents ) ); |
| 418 | |
| 419 | // Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents. This is an assertion. |
| 420 | $mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( true ) ); |
| 421 | |
| 422 | $results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem ); |
| 423 | |
| 424 | $this->assertTrue( $results ); |
| 425 | } |
| 426 | |
| 427 | function test_insert_with_markers_returns_false_when_writing_to_file_fails() { |
| 428 | |
| 429 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' ); |
| 430 | require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' ); |
| 431 | |
| 432 | $filename = 'file'; |
| 433 | $marker = 'marker'; |
| 434 | $insertion = array( '1', '2', '3' ); |
| 435 | |
| 436 | $expected_contents = implode( "\n", array( |
| 437 | "", |
| 438 | "# BEGIN {$marker}", |
| 439 | '1', |
| 440 | '2', |
| 441 | '3', |
| 442 | "# END {$marker}" |
| 443 | ) ); |
| 444 | |
| 445 | $mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock(); |
| 446 | |
| 447 | // Set the mock WP_Filesystem_Direct object to say the file doesn't exist |
| 448 | $mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( false ) ); |
| 449 | // Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents |
| 450 | $mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( false ) ); |
| 451 | |
| 452 | $results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem ); |
| 453 | |
| 454 | $this->assertFalse( $results ); |
| 455 | } |