From 416ccf88175ae732683d8e86b89c7e843a742e18 Mon Sep 17 00:00:00 2001 From: Shreya Agarwal Date: Thu, 20 Jun 2024 13:59:46 +0530 Subject: [PATCH 1/4] fix: fix deletion of constants deletes more lines than expected --- src/WPConfigTransformer.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/WPConfigTransformer.php b/src/WPConfigTransformer.php index 7957375..d97f1e3 100644 --- a/src/WPConfigTransformer.php +++ b/src/WPConfigTransformer.php @@ -226,8 +226,21 @@ public function remove( $type, $name ) { return false; } - $pattern = sprintf( '/(?<=^|;|<\?php\s|<\?\s)%s\s*(\S|$)/m', preg_quote( $this->wp_configs[ $type ][ $name ]['src'], '/' ) ); - $contents = preg_replace( $pattern, '$1', $this->wp_config_src ); + if ( 'constant' === $type ) { + $pattern = sprintf( + "/\bdefine\s*\(\s*['\"]%s['\"]\s*,\s*(('[^']*'|\"[^\"]*\")|\s*(?:[\s\S]*?))\s*\)\s*;\s*/mi", + preg_quote( $name, '/' ) + ); + } elseif ( 'variable' === $type ) { + $pattern = sprintf( + '/^\s*\$%s\s*=\s*[\s\S]*?;\s*$/mi', + preg_quote( $name, '/' ) + ); + } else { + return false; + } + + $contents = preg_replace( $pattern, '', $this->wp_config_src ); return $this->save( $contents ); } From 309155308a09281784274cd20bcfb8e791cf37fc Mon Sep 17 00:00:00 2001 From: Shreya Agarwal Date: Thu, 20 Jun 2024 15:38:38 +0530 Subject: [PATCH 2/4] refactor: remove redundant checks --- src/WPConfigTransformer.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/WPConfigTransformer.php b/src/WPConfigTransformer.php index d97f1e3..d6ba7b0 100644 --- a/src/WPConfigTransformer.php +++ b/src/WPConfigTransformer.php @@ -231,13 +231,11 @@ public function remove( $type, $name ) { "/\bdefine\s*\(\s*['\"]%s['\"]\s*,\s*(('[^']*'|\"[^\"]*\")|\s*(?:[\s\S]*?))\s*\)\s*;\s*/mi", preg_quote( $name, '/' ) ); - } elseif ( 'variable' === $type ) { + } else { $pattern = sprintf( '/^\s*\$%s\s*=\s*[\s\S]*?;\s*$/mi', preg_quote( $name, '/' ) ); - } else { - return false; } $contents = preg_replace( $pattern, '', $this->wp_config_src ); From 848addaca36fa04cfe46c4d75ce81ce29e585569 Mon Sep 17 00:00:00 2001 From: Shreya Agarwal Date: Thu, 20 Jun 2024 16:44:28 +0530 Subject: [PATCH 3/4] Add test for removal of constants with concatenated expressions --- tests/AddRemoveTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/AddRemoveTest.php b/tests/AddRemoveTest.php index db1fcd8..e5e1de8 100644 --- a/tests/AddRemoveTest.php +++ b/tests/AddRemoveTest.php @@ -142,6 +142,30 @@ public function testRemoveStringVariables() { } } + 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 and unchanged + $this->assertTrue( self::$config_transformer->exists( 'constant', $second_name ), "Check {$second_name} still exists" ); + + // Load the wp-config to check if only the intended constant was removed + require self::$test_config_path; + $this->assertFalse( defined( $name ), "{$name} should not be defined" ); + $this->assertTrue( defined( $second_name ), "{$second_name} should still be defined" ); + } + public function testAddConstantNoPlacementAnchor() { $this->expectException( Exception::class ); $this->expectExceptionMessage( 'Unable to locate placement anchor.' ); From 1df20c2199fea9e06306ea3f7fffd9f9938f0b7a Mon Sep 17 00:00:00 2001 From: Shreya Agarwal Date: Thu, 20 Jun 2024 17:11:55 +0530 Subject: [PATCH 4/4] improved test logic to ensure proper constant handling --- tests/AddRemoveTest.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/AddRemoveTest.php b/tests/AddRemoveTest.php index e5e1de8..3b3e6f1 100644 --- a/tests/AddRemoveTest.php +++ b/tests/AddRemoveTest.php @@ -157,13 +157,8 @@ public function testRemoveConstantWithConcatenation() { $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 and unchanged + // Ensure the second constant is still present after the removal $this->assertTrue( self::$config_transformer->exists( 'constant', $second_name ), "Check {$second_name} still exists" ); - - // Load the wp-config to check if only the intended constant was removed - require self::$test_config_path; - $this->assertFalse( defined( $name ), "{$name} should not be defined" ); - $this->assertTrue( defined( $second_name ), "{$second_name} should still be defined" ); } public function testAddConstantNoPlacementAnchor() {