Skip to content

Tests: Add unit test coverage for the plugin activation, deactivation and uninstall hook functions - #14523

Open
huzaifaalmesbah wants to merge 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:tests/register-uninstall-hook
Open

huzaifaalmesbah wants to merge 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:tests/register-uninstall-hook

Conversation

@huzaifaalmesbah

Copy link
Copy Markdown
Member

Description

Adds tests for register_activation_hook(), register_deactivation_hook() and register_uninstall_hook(), which plugins use to run code when they are activated, deactivated or deleted. None of the three had tests.

For the activation and deactivation hooks, the tests cover:

  • the callback being hooked to activate_{$plugin} / deactivate_{$plugin} at the default priority, with the plugin file reduced to its basename for plugins in a directory, single-file plugins, relative paths, must-use plugins and backslash separators;
  • the callback running with the network-wide flag when the action fires.

For the uninstall hook, they cover:

  • function names and static methods (as a string or an array) being stored in the uninstall_plugins option under the plugin basename;
  • an object method being rejected with _doing_it_wrong() and nothing being stored;
  • callbacks of other plugins being kept;
  • a changed callback, including one of another type, replacing the stored one;
  • the option not being saved again when the callback is unchanged.

Removing or changing each branch of the three functions makes at least one test fail.

The uninstall tests start from an empty uninstall_plugins option. When the option does not exist yet, (array) get_option( 'uninstall_plugins' ) turns false into array( false ), so the first plugin to register an uninstall hook also saves a stray 0 => false entry. That behavior is left out of these tests and can be handled separately.

This is a test-only change; no production code is modified.

Testing Instructions

  1. Run the tests on single site:
    npm run test:php -- --filter 'Tests_Functions_Register(Uninstall|Activation|Deactivation)Hook'
  2. Run them on multisite:
    npm run test:php -- --filter 'Tests_Functions_Register(Uninstall|Activation|Deactivation)Hook' -c tests/phpunit/multisite.xml
  3. Run them in random order:
    npm run test:php -- --filter 'Tests_Functions_Register(Uninstall|Activation|Deactivation)Hook' --order-by=random
  4. Run the plugins group:
    npm run test:php -- --group plugins
  5. Run PHPCS on the test files:
    node ./tools/local-env/scripts/docker.js run --rm php ./vendor/bin/phpcs tests/phpunit/tests/functions/registerActivationHook.php tests/phpunit/tests/functions/registerDeactivationHook.php tests/phpunit/tests/functions/registerUninstallHook.php

Trac ticket: https://core.trac.wordpress.org/ticket/65819

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Brainstorming edge-case scenarios and drafting test boilerplate; test architecture, assertions, and local PHPUnit verification were written and validated by me.

@huzaifaalmesbah
huzaifaalmesbah marked this pull request as ready for review October 20, 2026 05:35
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props huzaifaalmesbah.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant