-
Notifications
You must be signed in to change notification settings - Fork 84
Add strict slug validation for plugin-tests and theme-tests commands #358
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
This comment was marked as resolved.
This comment was marked as resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds strict slug validation to the scaffold plugin-tests and scaffold theme-tests commands to prevent generating corrupted bootstrap.php files when users provide invalid characters like trailing slashes in plugin/theme names.
Key Changes:
- Added regex validation
/^[a-zA-Z0-9-]+$/to reject slugs with special characters - Updated error messages from directory-based errors to character validation errors
- Added comprehensive test scenarios for slash, backslash, and underscore rejection
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/Scaffold_Command.php | Added regex validation to reject slugs containing characters other than alphanumerics and dashes |
| features/scaffold-plugin-tests.feature | Added test cases for rejecting slashes, backslashes, and underscores; updated error message expectations |
| features/scaffold-theme-tests.feature | Added test cases for rejecting slashes, backslashes, and underscores; updated error message expectations |
| features/scaffold-block.feature | Changed test command from I run to I try for invalid plugin slug scenario |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| When I try `wp scaffold theme-tests t12_child` | ||
| Then STDERR should be: | ||
| """ | ||
| Error: Invalid theme slug specified. The slug can only contain alphanumeric characters and dashes. | ||
| """ |
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case validates that t12_child (with underscore) is rejected, but underscores are commonly used in WordPress theme directory names. This test enforces overly restrictive validation that would break compatibility with existing themes that use underscores in their slugs. The test should be removed or the validation logic should be updated to allow underscores.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit e97b194. Removed this test case and updated validation to allow underscores.
src/Scaffold_Command.php
Outdated
| // Validate slug contains only alphanumeric characters and dashes. | ||
| if ( ! preg_match( '/^[a-zA-Z0-9-]+$/', $slug ) ) { | ||
| WP_CLI::error( "Invalid {$type} slug specified. The slug can only contain alphanumeric characters and dashes." ); |
Copilot
AI
Jan 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistency with other scaffold commands: The scaffold _s (underscores) command at line 369 uses the regex /^[a-z_]\w+$/i which explicitly allows underscores in theme slugs. The new validation for plugin-tests and theme-tests rejects underscores, creating an inconsistency where a user could scaffold a theme with underscores but then be unable to scaffold tests for it. The validation should be aligned with other scaffold commands to allow underscores.
| // Validate slug contains only alphanumeric characters and dashes. | |
| if ( ! preg_match( '/^[a-zA-Z0-9-]+$/', $slug ) ) { | |
| WP_CLI::error( "Invalid {$type} slug specified. The slug can only contain alphanumeric characters and dashes." ); | |
| // Validate slug contains only alphanumeric characters, dashes, and underscores. | |
| if ( ! preg_match( '/^[a-zA-Z0-9_-]+$/', $slug ) ) { | |
| WP_CLI::error( "Invalid {$type} slug specified. The slug can only contain alphanumeric characters, dashes, and underscores." ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit e97b194. Updated the regex to /^[a-zA-Z0-9_-]+$/ to allow underscores, making it consistent with the scaffold _s command validation. The error message now mentions "alphanumeric characters, underscores, and dashes".
|
@copilot address feedback |
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
When users pass plugin/theme names with invalid characters to
scaffold plugin-testsorscaffold theme-tests, the generatedbootstrap.phpcontains corrupted file paths like'/my-plugin/.php', causing PHP fatal errors on test execution.Changes
scaffold_plugin_theme_tests()to only allow alphanumeric characters (a-z, A-Z, 0-9), underscores (_), and dashes (-)scaffold _s) to allow underscoresExample
The validation rejects:
my-plugin/ormy-plugin\my@plugin,my.pluginmy plugin../pluginThe validation allows:
MyPlugin,plugin123my-pluginmy_pluginThe fix follows the existing pattern for invalid slug validation (
.and..) and implements the hard-error approach rather than silently transforming input.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.