Skip to content

Commit 82a263c

Browse files
committed
Clean up tools & add tests
1 parent f9f1cef commit 82a263c

File tree

6 files changed

+186
-60
lines changed

6 files changed

+186
-60
lines changed

bin/install-tests.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

3-
WP_VERSION=$1
4-
WP_TESTS_DIR=$2
3+
WP_VERSION=`grep "wp_version =" ../../../wp-includes/version.php | awk '{print $3}' | sed "s/'//g" | sed "s/;//g"`
4+
WP_TESTS_DIR=tests/wp-tests
55

66
function download {
77
if command -v curl >/dev/null 2>&1; then
@@ -33,4 +33,4 @@ fi
3333
echo "Installing WordPress PHPUnit Test Suite into '${WP_TESTS_DIR}' ..."
3434

3535
rm -rf ${WP_TESTS_DIR}
36-
svn co -q https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/ ${WP_TESTS_DIR}
36+
svn co -q https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/ ${WP_TESTS_DIR}

bin/phpunit.sh

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
#!/bin/bash
2-
# Usage: ./tests/bin.sh xdebug_on /srv/www/wordpress-default/public_html/wp-content/plugins/jwt-auth
2+
# Usage: ./tests/bin.sh xdebug_on
33
# Runs the PHPUnit tests with html coverage output in the VVV wordpress-default site.
4+
# Setting xdebug_off or leaving it unset will run PHPUnit without creating a coverage report.
45

56
set -e
67

8+
path=`pwd | sed 's/.*\(\/www\)/\1/g'`
9+
710
xdebug=$1
811
if [ -z "$xdebug" ]; then
912
xdebug="xdebug_off"
1013
fi
1114

12-
path=$2
13-
if [ -z "$path" ]; then
14-
path="/srv/www/wordpress-default/public_html/wp-content/plugins/jwt-auth"
15-
fi
16-
1715
if [ $xdebug = "xdebug_on" ]; then
18-
COVERAGE="--coverage-html $path/coverage";
16+
COVERAGE="--coverage-html /srv/$path/coverage";
1917
fi
2018

21-
vagrant ssh -c "$xdebug && cd $path && WP_TESTS_DIR=tests/wp-tests/phpunit phpunit $COVERAGE"
19+
vagrant ssh -c "$xdebug && cd "/srv/$path" && phpunit $COVERAGE"

composer.json

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,57 @@
11
{
22
"name": "jwt-auth",
33
"type": "wordpress-plugin",
4-
"description": "@todo",
4+
"description": "Experimental JWT Authentication plugin.",
55
"homepage": "https://github.com/WP-API/jwt-auth",
66
"license": "GPLv2",
77
"prefer-stable" : true,
88
"require": {
99
"php": ">=5.3"
1010
},
1111
"require-dev": {
12-
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.4",
12+
"brainmaestro/composer-git-hooks": "^2.6.0",
13+
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
1314
"firebase/php-jwt": "^5.0",
1415
"phpcompatibility/phpcompatibility-wp": "*",
1516
"php-coveralls/php-coveralls": "^2.1",
1617
"slowprog/composer-copy-file": "0.2.1",
1718
"wp-coding-standards/wpcs": "*",
18-
"xwp/wp-dev-lib": "dev-feature/composer-first"
19+
"xwp/wp-dev-lib": "^1.0.0"
1920
},
2021
"scripts": {
2122
"phpcs": [
22-
"./vendor/bin/phpcs . --standard=.phpcs.ruleset.xml"
23+
"./vendor/bin/phpcs ${1:.} --standard=.phpcs.ruleset.xml"
2324
],
25+
"phpcbf": [
26+
"./vendor/bin/phpcbf ${1:.} --standard=.phpcs.ruleset.xml"
27+
],
2428
"phpunit": [
25-
"./bin/phpunit.sh xdebug_off $1"
29+
"./bin/phpunit.sh xdebug_off"
2630
],
2731
"phpunit-coverage": [
28-
"./bin/phpunit.sh xdebug_on $1"
32+
"./bin/phpunit.sh xdebug_on"
2933
],
30-
"post-autoload-dump": [
31-
"[ -f .git/hooks/pre-commit ] || ./vendor/xwp/wp-dev-lib/scripts/install-pre-commit-hook.sh"
32-
],
33-
"post-install-cmd": [
34-
"@install-tests",
35-
"SlowProg\\CopyFile\\ScriptHandler::copy"
36-
],
37-
"post-update-cmd": [
38-
"@install-tests",
39-
"SlowProg\\CopyFile\\ScriptHandler::copy"
40-
],
41-
"readme": [
42-
"./vendor/xwp/wp-dev-lib/scripts/generate-markdown-readme"
43-
],
44-
"install-tests": [
45-
"./bin/install-tests.sh 4.9.8 ./tests/wp-tests"
46-
]
34+
"post-install-cmd": [
35+
"./bin/install-tests.sh",
36+
"./vendor/bin/cghooks add --no-lock",
37+
"SlowProg\\CopyFile\\ScriptHandler::copy"
38+
],
39+
"post-update-cmd": [
40+
"./bin/install-tests.sh",
41+
"./vendor/bin/cghooks update",
42+
"SlowProg\\CopyFile\\ScriptHandler::copy"
43+
],
44+
"readme": [
45+
"./vendor/xwp/wp-dev-lib/scripts/generate-markdown-readme"
46+
]
4747
},
4848
"extra": {
4949
"copy-file": {
5050
"tests/autoload.php": "tests/wp-tests/phpunit/wp-tests-config.php",
5151
"vendor/firebase/php-jwt/src/": "wp-includes/rest-api/auth/jwt/"
52-
}
52+
},
53+
"hooks": {
54+
"pre-commit": "./vendor/xwp/wp-dev-lib/scripts/pre-commit"
55+
}
5356
}
5457
}

tests/bootstrap.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,17 @@
6868
unset( $_plugin_dir, $_plugin_file_candidate, $_plugin_file_src );
6969

7070
/**
71-
* Force plugins defined in a constant (supplied by phpunit.xml) to be active at runtime.
71+
* Force plugins defined in a constant (supplied by phpunit.xml.dist) to be active at runtime.
7272
*
7373
* @filter site_option_active_sitewide_plugins
7474
* @filter option_active_plugins
7575
*
7676
* @param array $active_plugins All active plugins.
7777
* @return array
7878
*/
79-
function xwp_filter_active_plugins_for_phpunit( $active_plugins ) {
79+
function _phpunit_filter_active_plugins( $active_plugins ) {
8080
$forced_active_plugins = array();
81-
if ( file_exists( WP_CONTENT_DIR . '/themes/vip/plugins/vip-init.php' ) && defined( 'WP_TEST_VIP_QUICKSTART_ACTIVATED_PLUGINS' ) ) {
82-
$forced_active_plugins = preg_split( '/\s*,\s*/', WP_TEST_VIP_QUICKSTART_ACTIVATED_PLUGINS );
83-
} elseif ( defined( 'WP_TEST_ACTIVATED_PLUGINS' ) ) {
81+
if ( defined( 'WP_TEST_ACTIVATED_PLUGINS' ) ) {
8482
$forced_active_plugins = preg_split( '/\s*,\s*/', WP_TEST_ACTIVATED_PLUGINS );
8583
}
8684
if ( ! empty( $forced_active_plugins ) ) {
@@ -90,24 +88,19 @@ function xwp_filter_active_plugins_for_phpunit( $active_plugins ) {
9088
}
9189
return $active_plugins;
9290
}
93-
tests_add_filter( 'site_option_active_sitewide_plugins', 'xwp_filter_active_plugins_for_phpunit' );
94-
tests_add_filter( 'option_active_plugins', 'xwp_filter_active_plugins_for_phpunit' );
91+
tests_add_filter( 'site_option_active_sitewide_plugins', '_phpunit_filter_active_plugins' );
92+
tests_add_filter( 'option_active_plugins', '_phpunit_filter_active_plugins' );
9593

9694
/**
97-
* Load the plugins.
95+
* Load the plugin.
9896
*/
99-
function xwp_unit_test_load_plugin_file() {
97+
function _phpunit_load_plugin_file() {
10098
global $_plugin_file;
10199

102-
// Force vip-init.php to be loaded on VIP quickstart.
103-
if ( file_exists( WP_CONTENT_DIR . '/themes/vip/plugins/vip-init.php' ) ) {
104-
require_once WP_CONTENT_DIR . '/themes/vip/plugins/vip-init.php';
105-
}
106-
107100
// Load this plugin.
108101
require_once $_plugin_file;
109102
unset( $_plugin_file );
110103
}
111-
tests_add_filter( 'muplugins_loaded', 'xwp_unit_test_load_plugin_file' );
104+
tests_add_filter( 'muplugins_loaded', '_phpunit_load_plugin_file' );
112105

113106
require $_tests_dir . '/includes/bootstrap.php';

tests/wp-admin/includes/class-test-wp-key-pair-list-table.php

Lines changed: 138 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
*/
1616
class Test_WP_Key_Pair_List_Table extends WP_UnitTestCase {
1717

18+
/**
19+
* @var WP_Key_Pair_List_Table
20+
*/
21+
protected $table;
22+
23+
function setUp() {
24+
parent::setUp();
25+
$this->table = new WP_Key_Pair_List_Table( array( 'screen' => 'profile' ) );
26+
}
27+
1828
/**
1929
* Test get_columns().
2030
*
@@ -30,8 +40,7 @@ public function test_get_columns() {
3040
'token',
3141
'revoke',
3242
);
33-
$table = new WP_Key_Pair_List_Table( array( 'screen' => 'profile' ) );
34-
$columns = $table->get_columns();
43+
$columns = $this->table->get_columns();
3544
foreach ( $expected as $column ) {
3645
$this->assertArrayHasKey( $column, $columns );
3746
}
@@ -44,7 +53,28 @@ public function test_get_columns() {
4453
* @since 0.1
4554
*/
4655
public function test_prepare_items() {
47-
$this->markTestIncomplete();
56+
$this->table->items = array(
57+
array(
58+
'name' => 'First',
59+
'api_key' => '12345',
60+
),
61+
array(
62+
'name' => 'Second',
63+
'api_key' => '54321',
64+
),
65+
);
66+
$this->table->prepare_items();
67+
68+
ob_start();
69+
$this->table->display();
70+
$output = ob_get_clean();
71+
72+
preg_match_all( '/<tr data-api_key="(\d+)" data-name="(\S+)"[^>]*>/', $output, $matches, PREG_SET_ORDER, 0 );
73+
74+
foreach ( $this->table->items as $key => $item ) {
75+
$this->assertEquals( $item['api_key'], $matches[ $key ][1] );
76+
$this->assertEquals( $item['name'], $matches[ $key ][2] );
77+
}
4878
}
4979

5080
/**
@@ -54,7 +84,93 @@ public function test_prepare_items() {
5484
* @since 0.1
5585
*/
5686
public function test_column_default() {
57-
$this->markTestIncomplete();
87+
$tests = array(
88+
array(
89+
'item' => array(
90+
'name' => 'First',
91+
),
92+
'with' => 'name',
93+
'want' => 'First',
94+
),
95+
array(
96+
'item' => array(
97+
'api_key' => '12345',
98+
),
99+
'with' => 'api_key',
100+
'want' => '12345',
101+
),
102+
array(
103+
'item' => array(
104+
'created' => '',
105+
),
106+
'with' => 'created',
107+
'want' => '&mdash;',
108+
),
109+
array(
110+
'item' => array(
111+
'created' => mktime( 0, 0, 0, 2, 1, 2019 ),
112+
),
113+
'with' => 'created',
114+
'want' => 'February 1, 2019 12:00 am',
115+
),
116+
array(
117+
'item' => array(
118+
'last_used' => '',
119+
),
120+
'with' => 'last_used',
121+
'want' => '&mdash;',
122+
),
123+
array(
124+
'item' => array(
125+
'last_used' => mktime( 0, 0, 0, 2, 1, 2019 ),
126+
),
127+
'with' => 'last_used',
128+
'want' => 'February 1, 2019 12:00 am',
129+
),
130+
array(
131+
'item' => array(
132+
'last_ip' => '',
133+
),
134+
'with' => 'last_ip',
135+
'want' => '&mdash;',
136+
),
137+
array(
138+
'item' => array(
139+
'last_ip' => '127.0.0.1',
140+
),
141+
'with' => 'last_ip',
142+
'want' => '127.0.0.1',
143+
),
144+
array(
145+
'item' => array(
146+
'api_key' => '12345',
147+
),
148+
'with' => 'token',
149+
'want' => get_submit_button( 'New Token', 'secondary', 'token-key-pair-12345', false ),
150+
),
151+
array(
152+
'item' => array(
153+
'api_key' => '12345',
154+
),
155+
'with' => 'revoke',
156+
'want' => get_submit_button( 'Revoke', 'delete', 'revoke-key-pair-12345', false ),
157+
),
158+
array(
159+
'item' => array(
160+
'api_key' => '12345',
161+
),
162+
'with' => 'not_real',
163+
'want' => '',
164+
),
165+
);
166+
167+
$reflection = new ReflectionClass( get_class( $this->table ) );
168+
$method = $reflection->getMethod( 'column_default' );
169+
$method->setAccessible( true );
170+
171+
foreach ( $tests as $test ) {
172+
$this->assertEquals( $test['want'], $method->invokeArgs( $this->table, array( $test['item'], $test['with'] ) ) );
173+
}
58174
}
59175

60176
/**
@@ -64,7 +180,11 @@ public function test_column_default() {
64180
* @since 0.1
65181
*/
66182
public function test_display_tablenav() {
67-
$this->markTestIncomplete();
183+
ob_start();
184+
$this->table->display_tablenav( 'bottom' );
185+
$output = ob_get_clean();
186+
187+
$this->assertContains( 'revoke-all-key-pairs', $output );
68188
}
69189

70190
/**
@@ -74,6 +194,18 @@ public function test_display_tablenav() {
74194
* @since 0.1
75195
*/
76196
public function test_single_row() {
77-
$this->markTestIncomplete();
197+
$item = array(
198+
'name' => 'First',
199+
'api_key' => '12345',
200+
);
201+
202+
ob_start();
203+
$this->table->single_row( $item );
204+
$output = ob_get_clean();
205+
206+
preg_match_all( '/<tr data-api_key="(\d+)" data-name="(\S+)"[^>]*>/', $output, $matches, PREG_SET_ORDER, 0 );
207+
208+
$this->assertEquals( $item['api_key'], $matches[0][1] );
209+
$this->assertEquals( $item['name'], $matches[0][2] );
78210
}
79211
}

wp-admin/includes/class-wp-key-pair-list-table.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ protected function column_default( $item, $column_name ) {
8585
}
8686
return $item['last_ip'];
8787
case 'token':
88-
return get_submit_button( __( 'New Token', 'jwt-auth' ), 'secondary', 'token-key-pair-' . $item['api_key'], false );
88+
return get_submit_button( esc_html__( 'New Token', 'jwt-auth' ), 'secondary', 'token-key-pair-' . $item['api_key'], false );
8989
case 'revoke':
90-
return get_submit_button( __( 'Revoke', 'jwt-auth' ), 'delete', 'revoke-key-pair-' . $item['api_key'], false );
90+
return get_submit_button( esc_html__( 'Revoke', 'jwt-auth' ), 'delete', 'revoke-key-pair-' . $item['api_key'], false );
9191
default:
9292
return '';
9393
}
9494
}
9595

9696
/**
97-
* Generates custom table navigation to prevent conflicting nonces.
97+
* Replace table navigation with a revoke all key-pairs button.
9898
*
9999
* @since 0.1
100100
* @access protected
@@ -107,7 +107,7 @@ protected function display_tablenav( $which ) {
107107

108108
<?php if ( 'bottom' === $which ) : ?>
109109
<div class="alignright">
110-
<?php submit_button( __( 'Revoke All', 'jwt-auth' ), 'delete', 'revoke-all-key-pairs', false ); ?>
110+
<?php submit_button( esc_html__( 'Revoke All', 'jwt-auth' ), 'delete', 'revoke-all-key-pairs', false ); ?>
111111
</div>
112112
<?php endif; ?>
113113

0 commit comments

Comments
 (0)