Skip to content

Commit 202aa80

Browse files
authored
Merge pull request #49 from xknown/fix/wpconfig-mixed-lines
2 parents 5a617cf + dd6f0f9 commit 202aa80

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/WPConfigTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function exists( $type, $name ) {
7070
throw new Exception( 'Config file is empty.' );
7171
}
7272
// Normalize the newline to prevent an issue coming from OSX.
73-
$this->wp_config_src = str_replace( array( "\n\r", "\r" ), "\n", $wp_config_src );
73+
$this->wp_config_src = str_replace( array( "\r\n", "\n\r", "\r" ), "\n", $wp_config_src );
7474
$this->wp_configs = $this->parse_wp_config( $this->wp_config_src );
7575

7676
if ( ! isset( $this->wp_configs[ $type ] ) ) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use WP_CLI\Tests\TestCase;
4+
5+
class UpdateMixedLineEndingsTest extends TestCase {
6+
private static $test_config_path;
7+
private static $test_config_lines;
8+
private static $config_transformer;
9+
public static function set_up_before_class() {
10+
self::$test_config_lines = array(
11+
"<?php\n",
12+
"// this is a demo\r\n",
13+
"\r\n",
14+
"\r\n",
15+
"define( 'DB_NAME', '' );\n",
16+
"define( 'DB_HOST', '' );\r\n",
17+
"define( 'DB_USER', '' );\n\r",
18+
"\r\n",
19+
"\n\r",
20+
"\r",
21+
"\r",
22+
"\r\n",
23+
"define( 'DB_COLLATE', '');\n",
24+
"\n\r",
25+
"\n\r",
26+
"\r",
27+
"\r",
28+
);
29+
self::$test_config_path = tempnam( sys_get_temp_dir(), 'wp-config' );
30+
file_put_contents( self::$test_config_path, implode( '', self::$test_config_lines ) );
31+
self::$config_transformer = new WPConfigTransformer( self::$test_config_path );
32+
}
33+
public static function tear_down_after_class() {
34+
unlink( self::$test_config_path );
35+
}
36+
public function testMixedLineEndingsAreNormalized() {
37+
$this->assertTrue( self::$config_transformer->update( 'constant', 'DB_HOST', 'demo' ) );
38+
$this->assertTrue( self::$config_transformer->update( 'constant', 'DB_HOST', '' ) );
39+
40+
$modified_config_lines = file( self::$test_config_path );
41+
$this->assertSame( array_map( 'trim', self::$test_config_lines ), array_map( 'trim', $modified_config_lines ) );
42+
}
43+
}

0 commit comments

Comments
 (0)