Skip to content
Open
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,20 @@ npm run test:integration

### [Local development server](https://bero.dev/building-automated-tests-with-wordpress-playground/#cli)

- [ ] Run the local development server using Playground CLI
- [ ] Mount the plugin code in the WordPress site
- [ ] Configure the WordPress site using a Blueprint
- [x] Run the local development server using Playground CLI
- [x] Mount the plugin code in the WordPress site
- [x] Configure the WordPress site using a Blueprint

### [End-to-end tests](https://bero.dev/building-automated-tests-with-wordpress-playground/#e2e)

- [ ] Admin page
- [ ] Check if `/wp-admin/admin.php?page=workshop-tests` page loads
- [x] Admin page
- [x] Check if `/wp-admin/admin.php?page=workshop-tests` page loads
- [ ] Check if the message is displayed after submitting the form
- [ ] Check if the message is persisted after a page reload

### [Integration tests](https://bero.dev/building-automated-tests-with-wordpress-playground/#integration)

- [ ] Check if the plugin is active
- [x] Check if the plugin is active
- [ ] Rest API endpoint `POST` `/wp-json/PTD/v1/message`
- [ ] Confirm the API endpoint fails when not authenticated
- [ ] Confirm the API endpoint returns expected response when authenticated
Expand Down
8 changes: 8 additions & 0 deletions blueprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"steps": [
{
"step": "activatePlugin",
"pluginPath": "/wordpress/wp-content/plugins/playground-testing-demo/playground-testing-demo.php"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions tests/end-to-end/admin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { test, expect } from '@playwright/test';
import { runCLI } from "@wp-playground/cli";
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { login } from '@wp-playground/blueprints';

test('Admin page', async ({ page }) => {
const blueprint = JSON.parse(
readFileSync(
resolve('./blueprint.json'),
'utf8'
)
)
const cli = await runCLI({
"command": "server",
"mount": [
{
"hostPath": ".",
"vfsPath": "/wordpress/wp-content/plugins/playground-testing-demo"
}
],
blueprint
});

const server = cli.server;
const handler = cli.requestHandler;
const php = await handler.getPrimaryPhp();

const url = new URL(
'/wp-admin/admin.php?page=workshop-tests',
handler.absoluteUrl
);

await login(
php,
{
username: 'admin'
}
);

await page.goto(url.toString());

// Expect a title "to contain" a substring.
await expect(page).toHaveTitle(/Workshop Tests/);

await server.close();
});

9 changes: 0 additions & 9 deletions tests/end-to-end/example.spec.ts

This file was deleted.

93 changes: 93 additions & 0 deletions tests/integration/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { runCLI } from "@wp-playground/cli";
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { login } from '@wp-playground/blueprints';
import { PHP, PHPRequestHandler } from '@php-wasm/universal';
import { URL } from 'url';
import makeFetchCookie from "fetch-cookie";

const fetchCookie = makeFetchCookie(fetch);

const getAuthHeaders = async (handler: PHPRequestHandler, php: PHP) => {
await php.writeFile(
"/wordpress/get-nonce.php",
`<?php
require_once '/wordpress/wp-load.php';
echo json_encode(
array(
'X-WP-Nonce' => wp_create_nonce('wp_rest'),
)
);
`
);

await login(php, {
username: "admin",
});
const response = await fetchCookie(handler.absoluteUrl + "/get-nonce.php");
return await response.json();
};

describe("API", () => {
let server;
let handler: PHPRequestHandler;
let php: PHP;
let apiUrl: URL;

beforeAll(async () => {
const blueprint = JSON.parse(
readFileSync(resolve("./blueprint.json"), "utf8")
);
const cli = await runCLI({
command: "server",
mount: [
{
hostPath: ".",
vfsPath: "/wordpress/wp-content/plugins/playground-testing-demo",
},
],
blueprint,
});
server = cli.server;
handler = cli.requestHandler;
php = await handler.getPrimaryPhp();
apiUrl = new URL("/wp-json/PTD/v1/message", handler.absoluteUrl);
});
afterAll(async () => {
await server.close();
});
it("Check if plugin is active", async () => {
const result = await php.run({
code: `<?php
require_once '/wordpress/wp-load.php';
echo json_encode(
is_plugin_active('playground-testing-demo/playground-testing-demo.php')
);
`,
});
expect(result.json).toBe(true);
});
it("API should fail", async () => {
const formData = new FormData();
formData.append("message", "hi");
const response = await fetch(apiUrl.toString(), {
method: "POST",
body: formData,
});
console.log(await response.json());
expect(response.status).toBe(401);
});
it("API should pass", async () => {
const headers = await getAuthHeaders(handler, php);
const formData = new FormData();
formData.append("message", "hi");
const response = await fetchCookie(apiUrl.toString(), {
method: "POST",
body: formData,
headers,
});
console.log(await response.json());
expect(response.status).toBe(200);
});
});
7 changes: 0 additions & 7 deletions tests/integration/example.test.ts

This file was deleted.