Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #365 from xwp/feature/add-qunit
Browse files Browse the repository at this point in the history
Add QUnit Tests
  • Loading branch information
westonruter committed Feb 23, 2018
2 parents 2f9a4b0 + 4436273 commit bfc6b4c
Show file tree
Hide file tree
Showing 9 changed files with 1,115 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Gruntfile.js
Expand Up @@ -113,6 +113,11 @@ module.exports = function( grunt ) {
assets_dir: 'wp-assets'
}
}
},

// QUnit tests.
qunit: {
all: [ 'tests/qunit/index.html']
}

} );
Expand All @@ -123,6 +128,7 @@ module.exports = function( grunt ) {
grunt.loadNpmTasks( 'grunt-contrib-cssmin' );
grunt.loadNpmTasks( 'grunt-contrib-jshint' );
grunt.loadNpmTasks( 'grunt-contrib-uglify' );
grunt.loadNpmTasks( 'grunt-contrib-qunit' );
grunt.loadNpmTasks( 'grunt-shell' );
grunt.loadNpmTasks( 'grunt-wp-deploy' );

Expand Down
5 changes: 1 addition & 4 deletions composer.json
Expand Up @@ -24,8 +24,5 @@
"name": "Weston Ruter",
"homepage": "https://weston.ruter.net/"
}
],
"require-dev": {
"satooshi/php-coveralls": "dev-master"
}
]
}
7 changes: 7 additions & 0 deletions customize-posts.php
Expand Up @@ -29,6 +29,13 @@
* @subpackage Customize
*/

// Register WP-CLI command for generating QUnit test suite.
if ( defined( 'WP_CLI' ) ) {
define( 'CUSTOMIZE_POSTS_DIR_URL', plugin_dir_url( __FILE__ ) );
require_once dirname( __FILE__ ) . '/php/class-customize-posts-wp-cli-command.php';
WP_CLI::add_command( 'customize-posts', new Customize_Posts_WP_CLI_Command() );
}

// @codeCoverageIgnoreStart
require_once dirname( __FILE__ ) . '/php/class-customize-posts-plugin.php';
$GLOBALS['customize_posts_plugin'] = new Customize_Posts_Plugin(); // @codeCoverageIgnoreEnd
2 changes: 1 addition & 1 deletion dev-lib
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -16,9 +16,12 @@
"grunt-contrib-copy": "~1.0.0",
"grunt-contrib-cssmin": "~2.2.1",
"grunt-contrib-jshint": "~1.1.0",
"grunt-contrib-qunit": "^2.0.0",
"grunt-contrib-uglify": "~3.0.1",
"grunt-shell": "~2.1.0",
"grunt-wp-deploy": "~1.2.1"
"grunt-wp-deploy": "~1.2.1",
"qunitjs": "^2.4.0",
"sinon": "^3.2.1"
},
"author": "XWP",
"scripts": {
Expand Down
136 changes: 136 additions & 0 deletions php/class-customize-posts-wp-cli-command.php
@@ -0,0 +1,136 @@
<?php
/**
* Customize Posts WP CLI Command class.
*
* @package WordPress
* @subpackage Customize
*/

/**
* Class Customize_Posts_WP_CLI_Command.
*/
class Customize_Posts_WP_CLI_Command extends WP_CLI_Command {

const CORE_BASE_HREF = 'https://develop.svn.wordpress.org/trunk/src/';

const PLUGIN_BASE_HREF = '../../';

/**
* Plugin script handles that will need to be enqueued.
*
* @var array
*/
static $plugin_script_handles = array(
'customize-posts-panel',
'customize-post-date-control',
'customize-post-editor-control',
'customize-post-status-control',
'customize-post-section',
'customize-dynamic-control',
'customize-posts',
'customize-nav-menus-posts-extensions',
'customize-preview-setting-validities',
'customize-deferred-partial',
'customize-post-field-partial',
'customize-preview-posts',
'edit-post-preview-admin',
'edit-post-preview-customize',
'customize-page-template',
'edit-post-preview-admin-page-template',
'customize-featured-image',
'edit-post-preview-admin-featured-image',
'customize-preview-featured-image',
'select2',
);

/**
* Replace the base URL for script sources.
*
* @param string $script_tag Script tag.
* @param string $handle Script handle.
* @return string|boolean Rewritten script src.
*/
static function filter_script_loader_tag( $script_tag, $handle ) {
if ( in_array( $handle, self::$plugin_script_handles, true ) ) {
$script_tag = preg_replace( '#https?://[^"]+?/wp-content/plugins/[^/]+/#', self::PLUGIN_BASE_HREF, $script_tag );
} elseif ( 0 === strpos( $handle, 'select2-locale-' ) ) {
$script_tag = '';
} elseif ( self::is_core_script( $script_tag ) ) {
$script_tag = preg_replace( '#https?://[^"]+?/(?=(wp-includes|wp-admin)/)#', self::CORE_BASE_HREF, $script_tag );
} else {
$script_tag = false;
}

return $script_tag;
}

/**
* Checks to see if its core script.
*
* @param string $script_tag Script handle.
* @return bool
*/
public static function is_core_script( $script_tag ) {
return false !== strpos( $script_tag, 'wp-includes/' ) || false !== strpos( $script_tag, 'wp-admin/' );
}

/**
* Print dependencies for the supplied script handles.
*
* @subcommand generate-qunit-test-suite
*/
public function generate_qunit_test_suite() {
global $wp_customize;

$test_suite_file = dirname( __FILE__ ) . '/../tests/qunit/index.html';
$test_suite_template = dirname( __FILE__ ) . '/../tests/qunit/test-suite-template.html';

if ( empty( $wp_customize ) || ! ( $wp_customize instanceof \WP_Customize_Manager ) ) {
require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
$wp_customize = new \WP_Customize_Manager(); // WPCS: override ok.
}

foreach ( self::$plugin_script_handles as $script_handle ) {
if ( ! wp_scripts()->query( $script_handle, 'registered' ) ) {
WP_CLI::error( "Script handle not registered: $script_handle" );
}
}

add_filter( 'script_loader_tag', array( __CLASS__, 'filter_script_loader_tag' ), 10, 2 );

ob_start();
echo "<div hidden>\n";

do_action( 'customize_controls_enqueue_scripts' );
$wp_scripts = wp_scripts();
$customize_posts_scripts = array();

/**
* Get only customize-posts plugin handles.
*/
if ( ! empty( $wp_scripts->queue ) ) {
foreach ( $wp_scripts->queue as $script_handle ) {
if ( 0 === strpos( $wp_scripts->registered[ $script_handle ]->src, CUSTOMIZE_POSTS_DIR_URL ) ) {
array_push( $customize_posts_scripts, $script_handle );
}
}
}

wp_print_scripts( $customize_posts_scripts );

echo "</div>\n";
$output = ob_get_clean();

$output = preg_replace( '/<link.+?>/', '', $output );
$output = preg_replace( '#<style[^>]*>.+?</style>#s', '', $output );

$test_suite = file_get_contents( $test_suite_template );
$test_suite = preg_replace( '/(?=<html)/', "<!-- WARNING! Do not edit this file. It is generated via: wp customize-posts generate-qunit-test-suite -->\n", $test_suite );
$test_suite = str_replace( '{{dependencies}}', $output, $test_suite );
file_put_contents( $test_suite_file, $test_suite );

remove_filter( 'script_loader_tag', array( __CLASS__, 'filter_script_loader_tag' ), 10 );

WP_CLI::success( sprintf( 'Wrote test suite to %s', realpath( $test_suite_file ) ) );
}
}

0 comments on commit bfc6b4c

Please sign in to comment.