Skip to content

Commit

Permalink
Adding more inline comments and add a check for an empty pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Zodiac1978 committed Jun 13, 2024
1 parent 0ade800 commit 380e541
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,17 @@ public static function regexpOperators() {
}

public function testRegexpReplace() {
/* Testing if an actual replacment works correctly */
$this->assertQuery(
"INSERT INTO _options (option_name, option_value) VALUES ('test-ignore', '1');"
);
$this->assertQuery(
"INSERT INTO _options (option_name, option_value) VALUES ('test-remove', '2');"
);

$this->assertQuery( "SELECT * FROM _options WHERE REGEXP_REPLACE(option_name, '(-ignore|-remove)', '') = 'test'" );
$this->assertCount( 2, $this->engine->get_query_results() );

/* If one of the required parameters is null, the return value is null, copying the MYSQL/MariaDB behavior */
$this->assertQuery( "SELECT REGEXP_REPLACE( null, 'a', 'x') as result" );
$results = $this->engine->get_query_results();
$this->assertEquals( null, $results[0]->result );
Expand All @@ -140,6 +141,11 @@ public function testRegexpReplace() {
$this->assertQuery( "SELECT REGEXP_REPLACE( 'abc', 'a', null) as result" );
$results = $this->engine->get_query_results();
$this->assertEquals( null, $results[0]->result );

/* Providing an empty pattern should produce an error - but we changed that to null to avoid breaking things */
$this->assertQuery( "SELECT REGEXP_REPLACE( 'abc', '', 'x') as result" );
$results = $this->engine->get_query_results();
$this->assertEquals( null, $results[0]->result );
}

public function testInsertDateNow() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@ public function regexp_replace( $field, $pattern, $replacement ) {
return null;
}

/* Return null if the pattern is empty - this changes MySQL/MariaDB behavior! */
if ( empty( $pattern ) ) {
return null;
}

if ( "\x00" === $pattern[0] ) {
$pattern = substr( $pattern, 1 );
$flags = '';
Expand Down

0 comments on commit 380e541

Please sign in to comment.