Skip to content

Commit

Permalink
Merge pull request #49 from xknown/fix/wpconfig-mixed-lines
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera authored Nov 10, 2023
2 parents 5a617cf + dd6f0f9 commit 202aa80
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/WPConfigTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function exists( $type, $name ) {
throw new Exception( 'Config file is empty.' );
}
// Normalize the newline to prevent an issue coming from OSX.
$this->wp_config_src = str_replace( array( "\n\r", "\r" ), "\n", $wp_config_src );
$this->wp_config_src = str_replace( array( "\r\n", "\n\r", "\r" ), "\n", $wp_config_src );
$this->wp_configs = $this->parse_wp_config( $this->wp_config_src );

if ( ! isset( $this->wp_configs[ $type ] ) ) {
Expand Down
43 changes: 43 additions & 0 deletions tests/5-UpdateMixedLineEndingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use WP_CLI\Tests\TestCase;

class UpdateMixedLineEndingsTest extends TestCase {
private static $test_config_path;
private static $test_config_lines;
private static $config_transformer;
public static function set_up_before_class() {
self::$test_config_lines = array(
"<?php\n",
"// this is a demo\r\n",
"\r\n",
"\r\n",
"define( 'DB_NAME', '' );\n",
"define( 'DB_HOST', '' );\r\n",
"define( 'DB_USER', '' );\n\r",
"\r\n",
"\n\r",
"\r",
"\r",
"\r\n",
"define( 'DB_COLLATE', '');\n",
"\n\r",
"\n\r",
"\r",
"\r",
);
self::$test_config_path = tempnam( sys_get_temp_dir(), 'wp-config' );
file_put_contents( self::$test_config_path, implode( '', self::$test_config_lines ) );
self::$config_transformer = new WPConfigTransformer( self::$test_config_path );
}
public static function tear_down_after_class() {
unlink( self::$test_config_path );
}
public function testMixedLineEndingsAreNormalized() {
$this->assertTrue( self::$config_transformer->update( 'constant', 'DB_HOST', 'demo' ) );
$this->assertTrue( self::$config_transformer->update( 'constant', 'DB_HOST', '' ) );

$modified_config_lines = file( self::$test_config_path );
$this->assertSame( array_map( 'trim', self::$test_config_lines ), array_map( 'trim', $modified_config_lines ) );
}
}

0 comments on commit 202aa80

Please sign in to comment.