Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for validation for regex flags #41

Merged
merged 4 commits into from
Aug 29, 2017

Conversation

miya0001
Copy link
Member

@@ -820,6 +820,10 @@ public function search( $args, $assoc_args ) {

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
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
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
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.

@@ -701,6 +701,9 @@ Feature: Search through the database
When I run `wp db search 'unfindable' --regex`
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
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
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
Copy link
Contributor

gitlost commented Aug 29, 2017

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