Skip to content

Commit

Permalink
Add E2E tests (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks authored Feb 5, 2022
1 parent 0a5f723 commit db7f86f
Show file tree
Hide file tree
Showing 39 changed files with 3,595 additions and 309 deletions.
2 changes: 2 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
/lang/*.pot
/lang/Makefile
/stubs/
/tests/
/docker-compose.yml
/rollup.config.mjs
/package-lock.json
/package.json
/playwright.config.ts
/tsconfig.json
12 changes: 11 additions & 1 deletion .docker/wordpress/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ RUN \
a2ensite default-ssl.conf && \
curl -L https://github.com/FiloSottile/mkcert/releases/download/v1.4.3/mkcert-v1.4.3-linux-amd64 -o /usr/local/bin/mkcert && \
chmod +x /usr/local/bin/mkcert && \
/usr/local/bin/mkcert -cert-file /etc/ssl/certs/ssl-cert-snakeoil.pem -key-file /etc/ssl/private/ssl-cert-snakeoil.key 127.0.0.1 localhost
/usr/local/bin/mkcert -cert-file /etc/ssl/certs/ssl-cert-snakeoil.pem -key-file /etc/ssl/private/ssl-cert-snakeoil.key localhost && \
mkdir -p /var/www/mu-plugins

RUN \
curl -L https://phar.phpunit.de/phpcov.phar -o /usr/local/bin/phpcov && \
chmod +x /usr/local/bin/phpcov && \
pecl install pcov && \
echo 'extension=pcov.so' > /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini && \
echo 'pcov.enabled=1' >> /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini

COPY e2e-helper.php e2e-coverage.php /var/www/mu-plugins/
41 changes: 41 additions & 0 deletions .docker/wordpress/e2e-coverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report\PHP;

// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log

$base_dir = __DIR__ . '/../plugins/two-factor-provider-webauthn';
if ( getenv( 'COLLECT_COVERAGE' ) === '1' && PHP_SAPI !== 'cli' && is_dir( $base_dir ) && is_dir( $base_dir . '/coverage-report' ) ) {
/** @psalm-suppress UnresolvableInclude */
require_once $base_dir . '/vendor/autoload.php';

$filter = new Filter();
$filter->includeDirectory( $base_dir );
$filter->excludeDirectory( $base_dir . '/node_modules' );
$filter->excludeDirectory( $base_dir . '/stubs' );
$filter->excludeDirectory( $base_dir . '/vendor' );

$selector = new Selector();
$dt = new DateTime();
try {
$coverage = new CodeCoverage( $selector->forLineCoverage( $filter ), $filter );
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$coverage->start( sprintf( '%s %s - %s', ( $_SERVER['REQUEST_METHOD'] ?? '-' ), ( $_SERVER['REQUEST_URI'] ?? '-' ), $dt->format( 'YmdHisu' ) ) );

register_shutdown_function( function () use ( $coverage, $base_dir ) {
$coverage->stop();
try {
$php = new PHP();
$dt = new DateTime();
$php->process( $coverage, sprintf( '%s/coverage-report/%s.cov', $base_dir, $dt->format( 'YmdHisu' ) ) );
} catch ( Throwable $e ) {
error_log( (string) $e );
}
} );
} catch ( Throwable $e ) {
error_log( (string) $e );
}
}
26 changes: 26 additions & 0 deletions .docker/wordpress/e2e-helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace WildWolf\WordPress\E2EHelper;

use WP_Error;

/**
* @param mixed $preempt
* @param array $_args
* @param string $url
* @return mixed
*/
function pre_http_request( $preempt, array $_args, string $url ) /* NOSONAR */ {
if ( false !== strpos( $url, '://api.wordpress.org/' ) ) {
$preempt = new WP_Error( 'Forbidden' );
} else {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $url );
}

return $preempt;
}

if ( defined( '\\ABSPATH' ) ) {
add_filter( 'pre_http_request', __NAMESPACE__ . '\\pre_http_request', 1, 3 );
}
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": [ "plugin:@wordpress/eslint-plugin/recommended" ]
"extends": [
"plugin:@wordpress/eslint-plugin/recommended",
"plugin:playwright/playwright-test"
]
}
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
/lang/*.pot export-ignore
/lang/Makefile export-ignore
/stubs/ export-ignore
/tests/ export-ignore
/docker-compose.yml export-ignore
/rollup.config.mjs export-ignore
/package-lock.json export-ignore
/package.json export-ignore
/playwright.config.ts export-ignore
/tsconfig.json export-ignore
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Build and Test

on:
push:
branches:
- "**"
workflow_dispatch:

concurrency:
Expand Down
51 changes: 51 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: E2E Testing

on:
push:
branches:
- "**"
workflow_dispatch:

concurrency:
cancel-in-progress: true
group: ${{ github.workflow }}-${{ github.ref }}

permissions:
contents: read

jobs:
e2e:
name: Run E2E tests
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v2.4.0

- name: Set up Node.js environment
uses: actions/setup-node@v2.5.1
with:
node-version: lts/*
cache: npm

- name: Install dependencies
run: npm ci

- name: Set up PHP
uses: shivammathur/setup-php@2.16.0

- name: Install Composer Dependencies
uses: ramsey/composer-install@2.1.0

- name: Install Playwright
run: npx playwright install chromium

- name: Run PlaywrightE2E tests
run: npm run test:e2e

- name: Upload test results
uses: actions/upload-artifact@v2
if: failure()
with:
name: playwright-test-results
path: test-results/
retention-days: 3
2 changes: 2 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Code Standards Compliance Checks

on:
push:
branches:
- "**"
workflow_dispatch:

concurrency:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/static-code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: Static Code Analysis

on:
push:
branches:
- "**"
workflow_dispatch:

concurrency:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/node_modules
/vendor
/test-results/
/playwright-report/
2 changes: 1 addition & 1 deletion assets/login.min.js

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

2 changes: 1 addition & 1 deletion assets/login.min.js.map

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

6 changes: 5 additions & 1 deletion assets/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ const callback = (): void => {
});

if ('credentials' in navigator) {
startAuthentication();
if (!navigator.webdriver) {
startAuthentication();
} else {
(document.getElementById('webauthn-retry') as HTMLDivElement).removeAttribute('hidden');
}
} else {
showError(L_WEBAUTHN_NOT_SUPPORTED);
}
Expand Down
Loading

0 comments on commit db7f86f

Please sign in to comment.