Skip to content

Add test for validation for regex flags#41

Merged
gitlost merged 4 commits into
masterfrom
add-validation-for-regex-flags
Aug 29, 2017
Merged

Add test for validation for regex flags#41
gitlost merged 4 commits into
masterfrom
add-validation-for-regex-flags

Conversation

@miya0001

Copy link
Copy Markdown
Member

Comment thread src/DB_Command.php Outdated
if ( ( $regex = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex', false ) ) ) {
$regex_flags = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-flags', false );
// http://php.net/manual/en/reference.pcre.pattern.modifiers.php
if ( $regex_flags && ! preg_match( '/^(?!.*(.).*\1)[imsxeADSUXJu]+$/', $regex_flags ) ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not a fan of this check, either here or in search-replace. The check below if ( false === @preg_match( $search_regex, '' ) ) { at https://github.com/wp-cli/db-command/blob/master/src/DB_Command.php#L845 traps bad modifiers. Also this check doesn't allow for spaces and newlines which are allowed (surprisingly). Also it doesn't allow for PHP version differences. Also PHP doesn't care about duplicates (and spaces and newlines can be duplicated anyway).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gitlost

Oh it looks true ..., but if we pass the incorrect regex-flags, the table will be broken.

$ wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-flags='kppr'
PHP Warning:  preg_replace(): Unknown modifier 'k' in phar:///usr/local/bin/wp/vendor/wp-cli/search-replace-command/src/WP_CLI/SearchReplacer.php on line 86
Warning: preg_replace(): Unknown modifier 'k' in phar:///usr/local/bin/wp/vendor/wp-cli/search-replace-command/src/WP_CLI/SearchReplacer.php on line 86
...
...
$ wp plugin list 
Error: One or more database tables are unavailable. The database may need to be repaired.

I am thinking we need a validation for --regex-flags definitely. Do you have any idea?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using the same check in search-replace as at https://github.com/wp-cli/db-command/blob/master/src/DB_Command.php#L845 should be good enough.

Then STDOUT should be empty

When I try `wp db search 'unfindable' --regex --regex-flags='abcd'`
Then the return code should be 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A And STDERR should be: step would be good.

@gitlost gitlost merged commit bbea3b8 into master Aug 29, 2017
@gitlost gitlost deleted the add-validation-for-regex-flags branch August 29, 2017 15:01
@gitlost gitlost added this to the 1.2.1 milestone Aug 29, 2017
@gitlost gitlost changed the title Add validation for regex flags Add test for validation for regex flags Aug 29, 2017
@miya0001

Copy link
Copy Markdown
Member Author

@gitlost

Ah, I have understood what you are saying. It is working as expected, thanks!
I remove the code, I would like to add tests for the following situation.

  • Pass the incorrect regex flags
  • Pass the incorrect regex delimiter.

And I will update search-replace too. 😄

@gitlost

gitlost commented Aug 29, 2017

Copy link
Copy Markdown
Contributor

Sorry, should have waited to merge. (Was too eager to get my fix for #43 in!)

danielbachhuber pushed a commit that referenced this pull request Nov 18, 2022
Add test for validation for regex flags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants