<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[MammothWP]]></title><description><![CDATA[WordPress plugins and developer tools.]]></description><link>https://mammothwp.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 29 Nov 2026 20:52:36 GMT</lastBuildDate><atom:link href="https://mammothwp.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Run your plugin's checks on a real WordPress site with wp-self-test]]></title><description><![CDATA[This is a cross-post of our guide on mammothwp.com. The code and behaviour below are taken from that page. Unit tests tell you that a function returns the…]]></description><link>https://mammothwp.hashnode.dev/run-your-plugin-s-checks-on-a-real-wordpress-site-with-wp-self-test</link><guid isPermaLink="true">https://mammothwp.hashnode.dev/run-your-plugin-s-checks-on-a-real-wordpress-site-with-wp-self-test</guid><category><![CDATA[wp-cli]]></category><category><![CDATA[Testing]]></category><category><![CDATA[WordPress]]></category><category><![CDATA[PHP]]></category><dc:creator><![CDATA[Mammoth WP]]></dc:creator><pubDate>Thu, 12 Nov 2026 13:03:37 GMT</pubDate><content:encoded><![CDATA[<p>This is a cross-post of our guide on <a href="https://mammothwp.com/guides/wordpress-plugin-self-test-wp-self-test/">mammothwp.com</a>. The code and behaviour below are taken from that page.</p>
<p>Unit tests tell you that a function returns the right value. They don't tell you whether your REST route is registered on this particular site, whether the WP-CLI command still parses its flags after a deploy, or whether your Ability still answers.</p>
<p><a href="https://packagist.org/packages/mammothwp/wp-self-test">mammothwp/wp-self-test</a> runs checks like these inside a real WordPress install. A plugin ships a <code>self-test/manifest.json</code> and a few case classes. The same cases then run from wp-admin, WP-CLI, REST, the Abilities API or a git pre-push hook.</p>
<h2>Install</h2>
<pre><code class="language-bash">composer require mammothwp/wp-self-test
</code></pre>
<p>It is a runtime dependency, because the dashboard, the command and the abilities run inside WordPress. It requires PHP 7.4 or newer.</p>
<p>When the environment is <code>production</code>, the package boots nothing: no commands, abilities, dashboard or manifests, because a run creates data on the site. The environment comes from <code>WP_ENV</code> if it is defined, otherwise from <code>wp_get_environment_type()</code>. The <code>mmwp_self_test_enabled</code> filter overrides this per request. Every surface requires <code>manage_options</code>, which you can change with the <code>mmwp_self_test_capability</code> filter.</p>
<h2>Register a manifest</h2>
<p>In your plugin's main file:</p>
<pre><code class="language-php">add_action('mmwp_self_test_register', function (\MammothWP\SelfTest\Registry $registry) {
    require __DIR__ . '/self-test/Things_Read_Case.php';

    $registry-&gt;register('my-plugin', __DIR__ . '/self-test/manifest.json', [
        'label' =&gt; 'My Plugin',
    ]);
});
</code></pre>
<p>The hook runs at <code>init</code>, after the package's classes are loaded, so require the case files inside it. A <code>require</code> at the top of the main file would run too early.</p>
<p><code>self-test/manifest.json</code>:</p>
<pre><code class="language-json">{
    "version": 1,
    "cases": [
        {
            "id": "my-plugin-things-read",
            "label": "Things: list over REST and the Ability",
            "category": "content",
            "layers": ["rest", "ability"],
            "class": "Things_Read_Case",
            "requires": [],
            "args": {},
            "covers": {
                "rest": ["my-plugin/v1/things"],
                "ability": ["my-plugin-things/list-things"]
            }
        }
    ]
}
</code></pre>
<p>Case ids must be unique across every registered plugin. <code>covers</code> is documentation the package carries along. Your own tests can use it to check that every route, command and ability you register is claimed by a case.</p>
<h2>Write a case</h2>
<p>Every case has four phases, and each can be called on its own:</p>
<ul>
<li><code>preview()</code> describes what the case will do, with no side effects.</li>
<li><code>run()</code> performs the calls and returns a JSON-serialisable context. It may create data.</li>
<li><code>validate($context)</code> turns what was observed into <code>check()</code> or <code>skip()</code> rows.</li>
<li><code>cleanup($context)</code> removes what <code>run()</code> created. The runner stores the context, so cleanup can happen in a later request.</li>
</ul>
<p>This case only reads, so it keeps the default <code>cleanup()</code>, which removes nothing:</p>
<pre><code class="language-php">use MammothWP\SelfTest\Case_Base;

class Things_Read_Case extends Case_Base
{
    public function preview()
    {
        return [
            'List things over REST (expects 200)',
            'List things through the list-things ability',
        ];
    }

    public function run()
    {
        $o = [];
        $o['rest'] = $this-&gt;rest('GET', '/my-plugin/v1/things', ['limit' =&gt; 5]);
        $o['list'] = $this-&gt;ability('my-plugin-things/list-things', ['limit' =&gt; 5]);

        return ['observed' =&gt; $o];
    }

    public function validate(array $context)
    {
        $o = $context['observed'];

        return [
            $this-&gt;check('rest', 'GET /things returns 200', $this-&gt;status($o, 'rest') === 200, $this-&gt;detail($o, 'rest')),
            $this-&gt;ability_check($o, 'list', 'list-things returns an items list', function ($data) {
                return isset($data['items']) &amp;&amp; is_array($data['items']);
            }),
        ];
    }
}
</code></pre>
<p><code>rest()</code> dispatches the request in the same process through <code>rest_get_server()</code> and returns the status and data. <code>ability()</code> calls <code>wp_get_ability($name)-&gt;execute($input)</code> and returns <code>found</code>, <code>ok</code>, <code>data</code> and <code>error</code>. When the Abilities API is not available on the site, <code>ability_check()</code> reports a skip instead of a failure.</p>
<p>The route and ability in this example are the ones from the <a href="https://mammothwp.com/guides/declare-once-rest-wp-cli-ability-wp-kit/">wp-kit guide</a>, but the case works the same for any plugin.</p>
<h2>Run it</h2>
<pre><code class="language-bash">wp mmwp self-test my-plugin --format=table
</code></pre>
<p>That runs one plugin's cases. Without a slug, it runs every registered case. The actions are <code>list</code>, <code>preview</code>, <code>run [--cleanup]</code>, <code>cleanup</code> and <code>report</code>. The command exits with code 1 when a case fails, so a script can stop on it.</p>
<p>The same cases are available from:</p>
<ul>
<li><strong>wp-admin:</strong> Tools › Self-test, on non-production sites only.</li>
<li><strong>REST:</strong> <code>mmwp/v1/self-test/cases</code>, <code>preview</code>, <code>run</code>, <code>cleanup</code> and <code>report</code>, using cookie authentication with <code>X-WP-Nonce</code>.</li>
<li><strong>Abilities:</strong> <code>mmwp/self-test-list-cases</code>, <code>-preview</code>, <code>-run</code>, <code>-cleanup</code> and <code>-get-report</code>. Run and cleanup need <code>confirm: true</code>.</li>
</ul>
<h2>Before every push</h2>
<p>Add this to your plugin's <code>composer.json</code>:</p>
<pre><code class="language-json">"scripts": {
    "test": "bash tests/run-tests.sh",
    "post-install-cmd": ["@php vendor/mammothwp/wp-self-test/bin/install-hooks"]
}
</code></pre>
<p><code>install-hooks</code> writes <code>.githooks/pre-push</code> and sets <code>core.hooksPath</code>. On <code>git push</code>, the hook runs <code>php -l</code> on every tracked PHP file and then <code>composer run-script test</code>.</p>
<h2>When to use it</h2>
<ul>
<li>Your plugin registers REST routes, WP-CLI commands or Abilities, and you want to confirm they work on a real install.</li>
<li>You want one set of checks that can run from the admin screen, the command line or a hook.</li>
<li>You need test runs that clean up the data they create.</li>
</ul>
<h2>When not to</h2>
<ul>
<li>You only need unit tests for pure PHP logic.</li>
<li>The site you want to check is production and you are not prepared to create test data there. The package stays off on production unless you turn it on.</li>
</ul>
<h2>Links</h2>
<ul>
<li>Guide: <a href="https://mammothwp.com/guides/wordpress-plugin-self-test-wp-self-test/">https://mammothwp.com/guides/wordpress-plugin-self-test-wp-self-test/</a></li>
<li>Packagist: <a href="https://packagist.org/packages/mammothwp/wp-self-test">https://packagist.org/packages/mammothwp/wp-self-test</a></li>
<li>License: GPLv2 or later</li>
</ul>
<p>Questions or corrections are welcome in the comments. We make wp-self-test at Mammoth WP.</p>
<p><em>Drafted with AI assistance and reviewed by the MammothWP team.</em></p>
]]></content:encoded></item></channel></rss>