Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ on:
jobs:
test:
uses: wp-cli/.github/.github/workflows/reusable-testing.yml@main
with:
minimum-wp: '4.9'
minimum-php: '7.2'
with-coverage: false
28 changes: 3 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tool to reset WordPress database. This reset WP database but retains given admin



Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contributing)
Quick links: [Using](#using) | [Installing](#installing)

## Using

Expand All @@ -25,7 +25,7 @@ wp database reset --author=<username>

## Installing

Installing this package requires WP-CLI v2.11 or greater. Update to the latest stable release with `wp cli update`.
Installing this package requires WP-CLI v3.0 or greater. Update to the latest stable release with `wp cli update`.

Once you've done so, you can install the latest stable version of this package with:

Expand All @@ -36,30 +36,8 @@ wp package install ernilambar/database-command:@stable
To install the latest development version of this package, use the following command instead:

```bash
wp package install ernilambar/database-command:dev-master
wp package install ernilambar/database-command:dev-main
```

## Contributing

We appreciate you taking the initiative to contribute to this project.

Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

For a more thorough introduction, [check out WP-CLI's guide to contributing](https://make.wordpress.org/cli/handbook/contributing/). This package follows those policy and guidelines.

### Reporting a bug

Think you’ve found a bug? We’d love for you to help us get it fixed.

Before you create a new issue, you should [search existing issues](https://github.com/ernilambar/database-command/issues?q=label%3Abug%20) to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version.

Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/ernilambar/database-command/issues/new). Include as much detail as you can, and clear steps to reproduce if possible. For more guidance, [review our bug report documentation](https://make.wordpress.org/cli/handbook/bug-reports/).

### Creating a pull request

Want to contribute a new feature? Please first [open a new issue](https://github.com/ernilambar/database-command/issues/new) to discuss whether the feature is a good fit for the project.

Once you've decided to commit the time to seeing your pull request through, [please follow our guidelines for creating a pull request](https://make.wordpress.org/cli/handbook/pull-requests/) to make sure it's a pleasant experience. See "[Setting up](https://make.wordpress.org/cli/handbook/pull-requests/#setting-up)" for details specific to working on this package locally.


*This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.*
22 changes: 14 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
],
"homepage": "https://github.com/ernilambar/database-command",
"require": {
"wp-cli/wp-cli": "^2.11"
"php": ">=7.2.24 || ^8.0",
"wp-cli/wp-cli": "^3.0"
},
"require-dev": {
"wp-cli/entity-command": "^2",
"wp-cli/scaffold-package-command": "^2",
"wp-cli/wp-cli-tests": "^4"
"wp-cli/db-command": "^3.0",
"wp-cli/entity-command": "^3.0",
"wp-cli/role-command": "^3.0",
"wp-cli/scaffold-package-command": "^3.0",
"wp-cli/wp-cli-tests": "^5.2"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -37,8 +40,11 @@
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"ergebnis/composer-normalize": true
"ergebnis/composer-normalize": true,
"johnpbloch/wordpress-core-installer": true,
"phpstan/extension-installer": true
},
"optimize-autoloader": true,
"process-timeout": 7200,
"sort-packages": true
},
Expand All @@ -49,8 +55,7 @@
"readme": {
"sections": [
"Using",
"Installing",
"Contributing"
"Installing"
]
}
},
Expand All @@ -60,9 +65,10 @@
"lint": "run-linter-tests",
"phpcbf": "run-phpcbf-cleanup",
"phpcs": "run-phpcs-tests",
"phpstan": "run-phpstan-tests",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"readme": "./vendor/bin/wp scaffold package-readme . --force",
"readme": "./vendor/bin/wp scaffold package-readme . --force --branch=main",
"test": [
"@lint",
"@phpcs",
Expand Down
163 changes: 163 additions & 0 deletions features/database-command-basic.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,87 @@ Feature: Basic tests
missing --author parameter (Administrator user you want to keep after reset.)
"""

Scenario: Test empty author parameter value
Given a WP install

When I try `wp database reset --author=""`
Then STDERR should be:
"""
Error: User does not exist.
"""

Scenario: Test whitespace-only author parameter value
Given a WP install

When I try `wp database reset --author=" "`
Then STDERR should be:
"""
Error: User does not exist.
"""

Scenario: Test reset when multiple administrators exist
Given a WP install

When I run `wp user create firstadmin firstadmin@gmail.com --role=administrator`
And I run `wp user create secondadmin secondadmin@gmail.com --role=administrator`
And I run `wp database reset --author=secondadmin`
And I run `wp user list --role=administrator --format=count`
Then STDOUT should be:
"""
1
"""

Scenario: Test reset accepts administrator usernames with punctuation
Given a WP install

When I run `wp user create admin.name admin.name@gmail.com --role=administrator`
And I run `wp database reset --author=admin.name`
Then STDOUT should contain:
"""
Success: Database is reset successfully.
"""

@skip-sqlite
Scenario: Test reset fails when wp_install returns an error
Given a WP install
And a wp-content/mu-plugins/simulate-install-failure.php file:
"""
<?php
function wp_install( $blog_title, $user_name, $user_email, $is_public, $deprecated = '', $user_password = '', $language = '' ) {
return new WP_Error( 'simulated_install_error', 'Simulated wp_install failure.' );
}
"""

When I run `wp user create installfailadmin installfailadmin@gmail.com --role=administrator`
And I try `wp database reset --author=installfailadmin`
Then STDERR should contain:
"""
Error: Reset failed
"""

Scenario: Test reset accepts a custom role with manage_options capability
Given a WP install

When I run `wp role create customadmin "Custom Admin"`
And I run `wp user create customadminuser customadminuser@gmail.com --role=customadmin`
And I run `wp eval 'get_role( "customadmin" )->add_cap( "manage_options" );'`
And I run `wp database reset --author=customadminuser`
Then STDOUT should contain:
"""
Success: Database is reset successfully.
"""

Scenario: Test repeated reset with the same administrator
Given a WP install

When I run `wp user create repeatadmin repeatadmin@gmail.com --role=administrator`
And I run `wp database reset --author=repeatadmin`
And I run `wp database reset --author=repeatadmin`
Then STDOUT should contain:
"""
Success: Database is reset successfully.
"""

Scenario: Test author parameter is passed but non-existent user
Given a WP install

Expand Down Expand Up @@ -56,3 +137,85 @@ Feature: Basic tests
"""
Success: Database is reset successfully.
"""

Scenario: Test missing author parameter is caught before multisite check runs
Given a WP multisite subdirectory install

When I try `wp database reset`
Then STDERR should contain:
"""
missing --author parameter
"""

Scenario: Test author with leading/trailing whitespace is trimmed
Given a WP install

When I run `wp user create spacedadmin spacedadmin@gmail.com --role=administrator`
And I run `wp database reset --author=" spacedadmin "`
Then STDOUT should contain:
"""
Success: Database is reset successfully.
"""

@skip-sqlite
Scenario: Test author lookup is case-insensitive on collations that support it
Given a WP install

When I run `wp user create caseadmin caseadmin@gmail.com --role=administrator`
And I run `wp database reset --author=CASEADMIN`
Then STDOUT should contain:
"""
Success: Database is reset successfully.
"""

Scenario: Test author as user ID does not match a username
Given a WP install

When I run `wp user create idadmin idadmin@gmail.com --role=administrator`
And I run `wp user get idadmin --field=ID`
And save STDOUT as {USER_ID}
And I try `wp database reset --author={USER_ID}`
Then STDERR should be:
"""
Error: User does not exist.
"""

Scenario: Test author as email does not match a username
Given a WP install

When I run `wp user create emailadmin emailadmin@gmail.com --role=administrator`
And I try `wp database reset --author=emailadmin@gmail.com`
Then STDERR should be:
"""
Error: User does not exist.
"""

Scenario: Test custom role without manage_options capability fails
Given a WP install

When I run `wp role create customnorole "Custom NoRole"`
And I run `wp user create noroleuser noroleuser@gmail.com --role=customnorole`
And I try `wp database reset --author=noroleuser`
Then STDERR should be:
"""
Error: User is not administrator.
"""

Scenario: Test editor role cannot be used as author
Given a WP install

When I run `wp user create testeditor testeditor@gmail.com --role=editor`
And I try `wp database reset --author=testeditor`
Then STDERR should be:
"""
Error: User is not administrator.
"""

Scenario: Test help shows author parameter
Given an empty directory

When I try `PAGER= wp help database reset`
Then STDOUT should contain:
"""
--author
"""
81 changes: 81 additions & 0 deletions features/database-command-reset.feature
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,84 @@ Feature: Test reset behaviour
"""
{SITE_URL}
"""

Scenario: Test reset preserves blog_public option
Given a WP install

When I run `wp user create testadmin_bp testadmin_bp@gmail.com --role=administrator`
And I run `wp option set blog_public 0`
And I run `wp database reset --author=testadmin_bp`
And I run `wp option get blog_public`
Then STDOUT should be:
"""
0
"""

Scenario: Test reset preserves home and siteurl when customized
Given a WP install

When I run `wp user create testadmin_home testadmin_home@gmail.com --role=administrator`
And I run `wp option set home https://example-home.test`
And I run `wp option set siteurl https://example-site.test`
And I run `wp database reset --author=testadmin_home`
And I run `wp option get home`
Then STDOUT should be:
"""
https://example-home.test
"""
When I run `wp option get siteurl`
Then STDOUT should be:
"""
https://example-site.test
"""

@skip-sqlite
Scenario: Test reset removes prefixed custom tables but keeps non-prefixed
Given a WP install

When I run `wp db query "CREATE TABLE wp_custom_test_reset (id INT PRIMARY KEY)"`
And I run `wp db query "CREATE TABLE custom_noprefix_reset (id INT PRIMARY KEY)"`
And I run `wp user create testadmin_ct testadmin_ct@gmail.com --role=administrator`
And I run `wp database reset --author=testadmin_ct`
And I run `wp db query "SHOW TABLES LIKE 'wp_custom_test_reset'" --skip-column-names`
Then STDOUT should not contain:
"""
wp_custom_test_reset
"""
When I run `wp db query "SHOW TABLES LIKE 'custom_noprefix_reset'" --skip-column-names`
Then STDOUT should contain:
"""
custom_noprefix_reset
"""

Scenario: Test previous admin is removed after sequential resets
Given a WP install

When I run `wp user create adminA adminA@gmail.com --role=administrator`
And I run `wp database reset --author=adminA`
And I run `wp user create adminB adminB@gmail.com --role=administrator`
And I run `wp database reset --author=adminB`
And I try `wp user get adminA --field=login`
Then STDERR should contain:
"""
Invalid user
"""

@skip-sqlite
Scenario: Test reset clears activation key and password nag
Given a WP install

When I run `wp user create testadmin_meta testadmin_meta@gmail.com --role=administrator`
And I run `wp eval 'update_user_meta( get_user_by( "login", "testadmin_meta" )->ID, "default_password_nag", true );'`
And I run `wp db query "UPDATE wp_users SET user_activation_key='testkey123' WHERE user_login='testadmin_meta'"`
And I run `wp database reset --author=testadmin_meta`
And I run `wp db query "SELECT user_activation_key FROM wp_users WHERE user_login='testadmin_meta'" --skip-column-names`
Then STDOUT should not contain:
"""
testkey123
"""
When I run `wp eval 'echo get_user_meta( get_user_by( "login", "testadmin_meta" )->ID, "default_password_nag", true ) ? "1" : "0";'`
Then STDOUT should be:
"""
0
"""
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: 8
paths:
- src
- command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
Loading
Loading