Skip to content

Commit 87b2718

Browse files
authored
Merge pull request #75 from WP-API/add-github-actions-ci
Add GitHub Actions CI, bump minimum PHP to 7.4 and WP to 6.5
2 parents 133d905 + ed12a9e commit 87b2718

16 files changed

Lines changed: 1214 additions & 78 deletions

.github/workflows/tests.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
phpunit:
10+
name: PHP ${{ matrix.php }} / WP ${{ matrix.wp }}
11+
runs-on: ubuntu-latest
12+
13+
services:
14+
mysql:
15+
image: mysql:8.0
16+
env:
17+
MYSQL_ROOT_PASSWORD: root
18+
MYSQL_DATABASE: wordpress_test
19+
ports:
20+
- 3306:3306
21+
options: >-
22+
--health-cmd="mysqladmin ping"
23+
--health-interval=10s
24+
--health-timeout=5s
25+
--health-retries=3
26+
27+
strategy:
28+
fail-fast: false
29+
matrix:
30+
php: ['7.4', '8.0', '8.1', '8.2', '8.3']
31+
wp: ['6.5', '6.6', '6.7', '6.8', '6.9']
32+
33+
steps:
34+
- uses: actions/checkout@v4
35+
36+
- name: Install SVN
37+
run: sudo apt-get update -q && sudo apt-get install -y subversion
38+
39+
- name: Set up PHP ${{ matrix.php }}
40+
uses: shivammathur/setup-php@v2
41+
with:
42+
php-version: ${{ matrix.php }}
43+
tools: composer
44+
coverage: none
45+
46+
- name: Cache Composer packages
47+
uses: actions/cache@v4
48+
with:
49+
path: vendor
50+
key: ${{ runner.os }}-php-${{ matrix.php }}-${{ hashFiles('composer.json') }}
51+
restore-keys: ${{ runner.os }}-php-${{ matrix.php }}-
52+
53+
- name: Install Composer dependencies
54+
run: composer install --prefer-dist --no-progress --no-interaction
55+
56+
- name: Install WordPress test suite
57+
run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 ${{ matrix.wp }} true
58+
59+
- name: Run PHPUnit
60+
run: vendor/bin/phpunit
61+
62+
- name: Run PHPUnit (multisite)
63+
run: WP_MULTISITE=1 vendor/bin/phpunit
64+
65+
phpcs:
66+
name: PHPCS
67+
runs-on: ubuntu-latest
68+
69+
steps:
70+
- uses: actions/checkout@v4
71+
72+
- name: Set up PHP
73+
uses: shivammathur/setup-php@v2
74+
with:
75+
php-version: '8.3'
76+
tools: composer
77+
coverage: none
78+
79+
- name: Install Composer dependencies
80+
run: composer install --prefer-dist --no-progress --no-interaction
81+
82+
- name: Run PHPCS
83+
run: vendor/bin/phpcs

.phpcs.xml.dist

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<file>.</file>
77
<exclude-pattern>/vendor/</exclude-pattern>
88
<exclude-pattern>/node_modules/</exclude-pattern>
9+
<exclude-pattern>/tests/</exclude-pattern>
910

1011
<!-- How to scan -->
1112
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
@@ -19,12 +20,13 @@
1920
<!-- Rules: WordPress Coding Standards -->
2021
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
2122
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
22-
<config name="minimum_supported_wp_version" value="4.8"/>
23+
<config name="minimum_supported_wp_version" value="6.5"/>
2324
<rule ref="WordPress">
2425
<exclude name="Generic.Commenting" />
2526
<exclude name="Generic.Arrays.DisallowShortArraySyntax" />
2627
<exclude name="Squiz.Commenting" />
2728
<exclude name="WordPress.DateTime.RestrictedFunctions.date_date" />
2829
<exclude name="WordPress.NamingConventions.ValidHookName.UseUnderscores" />
30+
<exclude name="WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode" />
2931
</rule>
3032
</ruleset>

.travis.yml

Lines changed: 0 additions & 58 deletions
This file was deleted.

composer.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@
1111
],
1212
"require": {
1313
"composer/installers": "~1.0",
14-
"php": "^5.6.0||^7.0||^8.0"
14+
"php": ">=7.4"
1515
},
1616
"require-dev": {
1717
"squizlabs/php_codesniffer": "^3.3.1",
1818
"wp-coding-standards/wpcs": "^2.1.1",
19-
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
19+
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
2020
"phpcompatibility/phpcompatibility-wp": "^2.0",
21-
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0"
21+
"phpunit/phpunit": "^9.6",
22+
"yoast/phpunit-polyfills": "^1.1 || ^2.0"
23+
},
24+
"config": {
25+
"allow-plugins": {
26+
"composer/installers": true,
27+
"dealerdirect/phpcodesniffer-composer-installer": true
28+
}
2229
},
2330
"scripts": {
2431
"post-install-cmd": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/wp-coding-standards/wpcs",

inc/endpoints/class-token.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ public function register_routes() {
2020
'oauth2',
2121
'/access_token',
2222
[
23-
'methods' => 'POST',
24-
'callback' => [ $this, 'exchange_token' ],
25-
'args' => [
26-
'grant_type' => [
23+
'methods' => 'POST',
24+
'callback' => [ $this, 'exchange_token' ],
25+
'permission_callback' => '__return_true',
26+
'args' => [
27+
'grant_type' => [
2728
'required' => true,
2829
'type' => 'string',
2930
'validate_callback' => [ $this, 'validate_grant_type' ],
3031
],
31-
'client_id' => [
32+
'client_id' => [
3233
'required' => false,
3334
'type' => 'string',
3435
'validate_callback' => 'rest_validate_request_arg',
3536
],
36-
'code' => [
37+
'code' => [
3738
'required' => false,
3839
'type' => 'string',
3940
'validate_callback' => 'rest_validate_request_arg',
@@ -67,7 +68,7 @@ public function validate_grant_type( $type ) {
6768
* @return array|WP_Error Token data on success, or error on failure.
6869
*/
6970
public function exchange_token( WP_REST_Request $request ) {
70-
if ( $request['grant_type'] === 'client_credentials' ) {
71+
if ( 'client_credentials' === $request['grant_type'] ) {
7172
return $this->handle_client_credentials( $request );
7273
}
7374

@@ -206,7 +207,7 @@ private function extract_client_credentials( WP_REST_Request $request ) {
206207
$encoded = substr( $auth_header, 6 );
207208
$decoded = base64_decode( $encoded, true );
208209

209-
if ( $decoded === false ) {
210+
if ( false === $decoded ) {
210211
return new WP_Error(
211212
'oauth2.endpoints.token.invalid_request',
212213
__( 'Invalid Authorization header.', 'oauth2' ),

inc/endpoints/namespace.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ function register() {
2323
'oauth2',
2424
'/authorize',
2525
[
26-
'methods' => 'GET',
27-
'callback' => __NAMESPACE__ . '\\redirect_to_authorize',
26+
'methods' => 'GET',
27+
'callback' => __NAMESPACE__ . '\\redirect_to_authorize',
28+
'permission_callback' => '__return_true',
2829
]
2930
);
3031
}

inc/tokens/class-access-token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public static function create_for_client( ClientInterface $client, $meta = [] )
303303
* @return bool True if this is a client token, false otherwise.
304304
*/
305305
public function is_client_token() {
306-
return $this->user === null;
306+
return null === $this->user;
307307
}
308308

309309
/**

phpunit.xml.dist

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<?xml version="1.0"?>
22
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
35
bootstrap="tests/bootstrap.php"
46
backupGlobals="false"
57
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
>
8+
>
109
<testsuites>
11-
<testsuite>
10+
<testsuite name="OAuth2">
1211
<directory prefix="test-" suffix=".php">./tests/</directory>
1312
<exclude>./tests/test-sample.php</exclude>
1413
</testsuite>

tests/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
exit( 1 );
1717
}
1818

19+
// PHPUnit Polyfills required by the WP test bootstrap since WP 5.9.
20+
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', dirname( __DIR__ ) . '/vendor/yoast/phpunit-polyfills' );
21+
1922
// Give access to tests_add_filter() function.
2023
require_once $_tests_dir . '/includes/functions.php';
2124

tests/class-test-case.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Shared base test case.
4+
*
5+
* @package WP\OAuth2\Tests
6+
*/
7+
8+
namespace WP\OAuth2\Tests;
9+
10+
use WP\OAuth2\Client;
11+
use WP_UnitTestCase;
12+
13+
/**
14+
* Base test case with helpers shared across all OAuth2 test classes.
15+
*/
16+
abstract class Test_Case extends WP_UnitTestCase {
17+
18+
/**
19+
* Create and approve a test client.
20+
*
21+
* @param array $meta_overrides Optional overrides for the client meta.
22+
* @param string $name Client name.
23+
*
24+
* @return Client
25+
*/
26+
protected function create_client( array $meta_overrides = [], $name = 'Test Client' ) {
27+
$data = [
28+
'name' => $name,
29+
'description' => 'Test client description.',
30+
'meta' => array_merge(
31+
[
32+
'callback' => 'https://example.com/callback',
33+
'type' => 'web',
34+
'client_credentials_enabled' => false,
35+
],
36+
$meta_overrides
37+
),
38+
];
39+
40+
$client = Client::create( $data );
41+
$this->assertInstanceOf( Client::class, $client, 'create_client helper: Client::create() must return a Client instance.' );
42+
$client->approve();
43+
44+
return $client;
45+
}
46+
}

0 commit comments

Comments
 (0)