How to test wordpress admin content in PHPUnit?

How I can test the html-content of my plugin in wp-admin with PHPUnit tests. I want something like this: set_current_screen('admin_page_myplugin'); $html = $this->getOriginalHTML(); $this->assertContains('My plugin unique content', $html); Thanks!
Category: Web

Can't run WP e2e-test-utils because the browser exits on login?

I'm trying to run e2e tests for my Gutenberg plugin. Here's my pertinent code import { createNewPost, enablePageDialogAccept, insertBlock, clickButton, } from "@wordpress/e2e-test-utils"; beforeAll(async () => { await enablePageDialogAccept(); }); describe("content editor basic functions", () => { test("ContentEditor shows up", async () => { await createNewPost(); return true; }); }); I get the following stacktrace Navigation failed because browser has disconnected! at /Users/nav/dev/wordpress-unified/plugins/my-plugin/node_modules/puppeteer/lib/cjs/puppeteer/common/LifecycleWatcher.js:51:147 at /Users/nav/dev/wordpress-unified/plugins/my-plugin/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:51:62 at Array.map (<anonymous>) at Object.emit (/Users/nav/dev/wordpress-unified/plugins/my-plugin/node_modules/puppeteer/lib/cjs/vendor/mitt/src/index.js:51:43) at CDPSession.emit (/Users/nav/dev/wordpress-unified/plugins/my-plugin/node_modules/puppeteer/lib/cjs/puppeteer/common/EventEmitter.js:72:22) at CDPSession._onClosed (/Users/nav/dev/wordpress-unified/plugins/my-plugin/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:247:14) at Connection._onMessage (/Users/nav/dev/wordpress-unified/plugins/my-plugin/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:94:25) …
Category: Web

woocommerce registration form with klaviyo(don't work with current user)

I'm trying to integrate klaviyo with woocom form, this is the code I added below. The issue is when I add any hardcoded email in variable $emailUntrim = "[email protected]" instead of $emailUntrim = $user->user_email; then it works. What I'm trying to achieve is when a new user gets registered in from woocom form they also get added into a list in klaviyo. Any leads would be great regarding the code, it may be not accurate method but I'm open for …
Category: Web

How to change the admin/password when using the E2E Test Utils for Gutenberg

I'm trying to set up a test suite for our Gutenberg blocks plugin using the e2e-test-utils. Our environment is a multi-site WP install and I pass the WP_BASE_URL, WP_USERNAME and WP_PASSWORD as environment variables, so they are not stored anywhere in the repo. I'm trying to make sure that the plugin I am testing on is activated using the asynchronous activatePlugin('name-of--our-plugin') function from the @wordpress/e2e-test-utils package. The problem is that this function assumes there is an existing administrator account with …
Category: Web

How can I enable IDE integration (autocomplete) for WordPress test suite

When testing a theme or plugin, you can use WP-CLI to scaffold the test suite setup, wp scaffold plugin-test plugin. So is there a good way to integrate an editor/IDE (VS Code in my case) so that autocompletion (method signatures, etc) works within the tests? Since the test library is installed in /tmp/ the editor is not indexing those files and thus thinks classes and functions are missing.
Category: Web

Rest API in integration tests - filtering by slug not working?

I have a bunch of custom fields in my REST API response, and I need to refactor the code for it, so I'm creating an integration test for it, just to make sure nothing breaks in the process of refactoring. The testing suite was build using wp scaffold for plugin tests. The test looks like: <?php /** * Class Api_Docs_Page * * @package My_Plugin\Routes\Endpoints */ namespace My_Plugin\Tests\Routes\Endpoints; use WP_REST_Request; use WP_UnitTestCase; /** * Class that tests the /wp-json/wp/v2/pages?slug=api-docs response. */ …
Category: Web

Intermittent database errors when accessing WordPress database

Update: I've narrowed this down to a couple of issues that seem unrelated. The testing suite was disconnecting my database without reconnecting. (resolved) The mysqli_free_result(): Couldn't fetch mysqli_result wp-db.php line 1550 error still pops up for an unknown reason. It seems it sometimes is failing to get a proper result from the query SELECT option_value FROM wp_options WHERE option_name = 'theme_switched' LIMIT 1. Instead it is returning a mysqli_result that while instantiated is internally NULL. When the test succeeds the …
Category: Web

Coverage in integration tests

EDIT: I've created the mwe as a small test plugin: https://github.com/dingo-d/test-plugin Original question: I'm writing tests for my plugin and one thing makes no sense to me and I'm not sure if I'm doing something wrong, or is it just PHPUnit behaving in a certain way. In a class I have a register()method where I add my hooks. When I write a test, I usually instantiate my class (even though I shouldn't because I'm running integration test so when I …
Category: Web

Integration tests don't register menu

I have integration tests set up. The install-wp-tests.sh is taken from the wpcli scaffold plugin tests. I'm on VVV and I have wordpress-develop set up. In my plugin I have phpunit.integration.xml.dist <?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap-integration.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" printerClass="Codedungeon\PHPUnitPrettyResultPrinter\Printer" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" syntaxCheck="false" verbose="true" > <testsuites> <testsuite name="integration"> <directory prefix="test-" suffix=".php">./tests/integration/</directory> </testsuite> </testsuites> <filter> <whitelist> <directory>./</directory> <exclude> <directory>./node_modules</directory> <directory>./vendor</directory> <directory>./tests</directory> </exclude> </whitelist> </filter> <logging> <log type="coverage-html" target="tests/_reports/coverage-integration" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="80" /> <log type="coverage-clover" …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.