├── .gitignore ├── .mailmap ├── .travis.yml ├── Gruntfile.js ├── README.md ├── composer.json ├── package.json ├── phpunit.xml ├── tests └── bootstrap.php └── wp-cli-theme-test-command.php /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | composer.lock 3 | vendor 4 | -------------------------------------------------------------------------------- /.mailmap: -------------------------------------------------------------------------------- 1 | Paolo Tresso Paolo T. 2 | Paolo Tresso Paolo T. 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.5 5 | - 5.4 6 | - 5.3 7 | 8 | env: 9 | - WP_VERSION=3.7.1 WP_MULTISITE=0 10 | 11 | before_install: 12 | - set -ex 13 | # - composer install 14 | # set up wp-cli 15 | - export VERSION="dev-master" 16 | - export WP_CORE_DIR="/tmp/wordpress" 17 | - mkdir $WP_CORE_DIR 18 | - mysql -e 'CREATE DATABASE wordpress_test;' -uroot 19 | # - php composer.phar config repositories.theme_test vcs $TRAVIS_BUILD_DIR 20 | # - php composer.phar update --dev --no-interaction --prefer-source 21 | # - php composer.phar require pixline/wp-cli-theme-test-command=dev-$TRAVIS_BRANCH 22 | 23 | before_script: 24 | - cd $TRAVIS_BUILD_DIR 25 | - composer install --dev --no-interaction --prefer-source 26 | - export PATH="$TRAVIS_BUILD_DIR/vendor/bin/:$PATH" 27 | - cd $WP_CORE_DIR 28 | - wp --info 29 | - wp core download --version=$WP_VERSION 30 | - wp core config --dbname=wordpress_test --dbuser=root 31 | - wp core install --url=http://localhost --title="WP" --admin_user=admin --admin_email=test@example.org --admin_password=test 32 | - wp theme-test install --data=wptest --plugin=debug --menus 33 | 34 | script: exit 0 35 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function (grunt) { 2 | 3 | "use strict"; 4 | 5 | grunt.initConfig({ 6 | 7 | pkg: grunt.file.readJSON('package.json'), 8 | 9 | phpcs: { 10 | options: { 11 | bin: 'phpcs -p -s -v', 12 | standard: 'WordPress' 13 | }, 14 | main: { 15 | dir: './*.php' 16 | }, 17 | }, 18 | 19 | phplint: { 20 | main: [ 'wp-cli-test-command.php' ], 21 | }, 22 | 23 | shell: { 24 | 'phpmd': { 25 | command: '/usr/bin/phpmd wp-cli-test-command.php text codesize,design,naming,unusedcode', 26 | options: { 27 | stdout: true, 28 | failOnError: false 29 | } 30 | } 31 | } 32 | 33 | }); 34 | 35 | 36 | grunt.loadNpmTasks('grunt-phpcs'); 37 | grunt.loadNpmTasks('grunt-phplint'); 38 | grunt.loadNpmTasks('grunt-shell'); 39 | 40 | grunt.registerTask( 'lint' , [ 'phpcs', 'phplint', 'shell:phpmd' ] ); 41 | grunt.registerTask( 'default' , [ 'lint' ] ); 42 | 43 | }; 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wp-cli theme-test command 2 | 3 | [![Build Status - master](https://travis-ci.org/pixline/wp-cli-theme-test-command.png?branch=master)](https://travis-ci.org/pixline/wp-cli-theme-test-command) [![Latest Stable Version](https://poser.pugx.org/pixline/wp-cli-theme-test-command/v/stable.png)](https://packagist.org/packages/pixline/wp-cli-theme-test-command) 4 | 5 | Install and configure Theme "unit tests" (sample data, required plugins and options) with wp-cli 6 | 7 | http://codex.wordpress.org/Theme_Unit_Test 8 | 9 | 10 | ## Installation 11 | 12 | ### Default install: wp-cli + composer 13 | 14 | Composer installation as referenced in [wp-cli Community Packages setup](https://github.com/wp-cli/wp-cli/wiki/Community-Packages). 15 | 16 | 0) Make sure to have [wp-cli](http://wp-cli.org#install) already installed. 17 | 18 | 1) Go to the directory containing the root `composer.json` file: 19 | 20 | ```bash 21 | cd ~/.wp-cli 22 | ``` 23 | 24 | 2) Add the WP-CLI Package Index: 25 | 26 | ```bash 27 | composer config repositories.wp-cli composer http://wp-cli.org/package-index/ 28 | ``` 29 | 30 | 3) Install the package: 31 | 32 | ```bash 33 | composer require pixline/wp-cli-theme-test-command=dev-master 34 | ``` 35 | 36 | 37 | ## Usage 38 | 39 | NOTE: This command can't deal (yet?) with global flags like ```--path```, please run it in the WordPress root folder. 40 | 41 | ``` 42 | wp theme-test install [options] 43 | 44 | Angle brackets groups possible values, default is marked with * 45 | 46 | [--data=< unit-test* | wpcom-theme | wpcom-demo | wptest | skip >] 47 | 48 | URL/path to custom WXR data file, or built-in: 49 | 50 | 'unit-test' = Default Theme Unit Test datafile 51 | 'wpcom-theme' = Alternative wpcom datafile (1) 52 | 'wpcom-demo' = Alternative wpcom datafile (2) 53 | 'wptest' = manovotny/wptest datafile 54 | 'skip' = Do not install new data 55 | 56 | [--plugin=< theme* | vip | devel | all | skip >] 57 | 58 | Plugin bundle to install. 59 | Bundles are as suggested in the 'developer' plugin, 60 | 'debug' bundle is an exclusive feature :-) 61 | 62 | 'theme' = Default plugin setup 63 | 'vip' = Default + wpcom VIP plugin setup 64 | 'devel' = Default + plugin developer setup 65 | 'debug' = Default + debugger setup 66 | 'all' = Default + VIP + developer + debugger! 67 | 'skip' = Do not install/activate plugin bundles 68 | 69 | [--option=< default* | skip >] 70 | 71 | 72 | Updates blog options to the test default values. 73 | 74 | 'skip' = Do not update options 75 | 'default' = Updates options to their default test value: 76 | 77 | - blogname WordPress Theme Unit Test Site 78 | - posts_per_page 5 79 | - thread_comments 1 80 | - thread_comments_depth 3 81 | - page_comments 1 82 | - comments_per_page 5 83 | - medium_max_w null 84 | - medium_max_h null 85 | - large_max_w null 86 | - large_max_h null 87 | - permalink_structure /%year%/%monthnum%/%day%/%postname%/ 88 | 89 | [--menus] Add custom nav menus 90 | 91 | ``` 92 | 93 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pixline/wp-cli-theme-test-command", 3 | "type": "command", 4 | "description": "Install and configure WordPress theme unit tests", 5 | "keywords": ["wp-cli","theme-test"], 6 | "homepage": "https://github.com/pixline/wp-cli-theme-test-command", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Paolo Tresso", 11 | "email": "plugins@swergroup.com", 12 | "homepage": "http://swergroup.com", 13 | "role": "Developer" 14 | } 15 | ], 16 | "require": { 17 | "php": ">=5.3.0" 18 | }, 19 | "support": { 20 | "issues": "https://github.com/pixline/wp-cli-theme-test-command/issues", 21 | "source": "https://github.com/pixline/wp-cli-theme-test-command" 22 | }, 23 | "autoload": { 24 | "files": [ "wp-cli-theme-test-command.php" ] 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wp-cli_theme_test_command", 3 | "version": "0.3.2", 4 | "description": "Install and run WordPress unit tests from wp-cli", 5 | "main": "Gruntfile.js", 6 | "directories": { 7 | "test": "tests" 8 | }, 9 | "dependencies": { 10 | "grunt": "~0.4.1", 11 | "grunt-cli": "~0.1.9", 12 | "grunt-phpcs": "~0.1.1", 13 | "grunt-shell": "~0.2.2", 14 | "grunt-phplint": "0.0.5" 15 | }, 16 | "devDependencies": {}, 17 | "scripts": { 18 | "test": "echo \"Error: no test specified\" && exit 1" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "ssh://git@github.com:pixline/wp-cli-theme-test-command.git" 23 | }, 24 | "keywords": [ 25 | "wp", 26 | "plugin", 27 | "" 28 | ], 29 | "author": "pixline", 30 | "license": "GPL2", 31 | "gitHead": "82c1b9d68ba013b6c8c69277f1de559ebf93f21c", 32 | "readmeFilename": "README.md" 33 | } 34 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | tests/ 8 | 9 | 10 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 | 6 | * @version 0.5.0 7 | * @when after_wp_load 8 | * @synopsis 9 | */ 10 | 11 | class Theme_Test_Cmd extends WP_CLI_Command{ 12 | 13 | /** 14 | * create optional test nav menu 15 | * 16 | * At least two custom menus should be created in order to test a theme 17 | * The standard Theme data file now ships with optimal menus built-in 18 | * This method actually makes sense with custom WXR files only 19 | * 20 | * @since 0.2 21 | */ 22 | private function create_test_menus(){ 23 | $pages = get_all_page_ids(); 24 | $items = array(); 25 | foreach ( $pages as $page_ID ): 26 | $info = get_page( $page_ID ); 27 | $items[ $info->post_title ] = get_permalink( $page_ID ); 28 | endforeach; 29 | 30 | # pick three random entries 31 | $random = array_rand( $items, 3 ); 32 | 33 | # build menus 34 | $menus = array( 35 | 'Full Menu' => array( 36 | 'slug' => 'full-menu', 37 | 'menu_items' => $items, 38 | ), 39 | 'Short Menu' => array( 40 | 'slug' => 'short-menu', 41 | 'menu_items' => array( 42 | $items[ $random[0] ], 43 | $items[ $random[1] ], 44 | $items[ $random[2] ], 45 | ) 46 | ), 47 | ); 48 | 49 | # register menus 50 | foreach ( $menus as $title => $data ): 51 | register_nav_menu( $data['slug'], $title ); 52 | if ( false == is_nav_menu( $title ) ): 53 | $menu_ID = wp_create_nav_menu( $title ); 54 | foreach ( $data['menu_items'] as $name => $url ): 55 | $add_item = array( 56 | 'menu-item-type' => 'custom', 57 | 'menu-item-url' => $url, 58 | 'menu-item-title' => $name, 59 | ); 60 | wp_update_nav_menu_item( $menu_ID, 0, $add_item ); 61 | endforeach; 62 | WP_CLI::success( 'Created menu '.$title ); 63 | endif; 64 | endforeach; 65 | } 66 | 67 | /** 68 | * Update blog options with optimal testing standards 69 | * 70 | * @since 0.2 71 | */ 72 | private function update_test_options( $option = NULL ){ 73 | if ( 'skip' === $option ) 74 | return; 75 | 76 | WP_CLI::launch( 'wp option update blogname "WordPress Theme Unit Test Site"' ); 77 | WP_CLI::launch( 'wp option update posts_per_page 5' ); 78 | WP_CLI::launch( 'wp option update thread_comments 1' ); 79 | WP_CLI::launch( 'wp option update thread_comments_depth 3' ); 80 | WP_CLI::launch( 'wp option update page_comments 1' ); 81 | WP_CLI::launch( 'wp option update comments_per_page 5' ); 82 | WP_CLI::launch( 'wp option update medium_max_w ""' ); 83 | WP_CLI::launch( 'wp option update medium_max_h ""' ); 84 | WP_CLI::launch( 'wp option update large_max_w ""' ); 85 | WP_CLI::launch( 'wp option update large_max_h ""' ); 86 | WP_CLI::launch( 'wp option update permalink_structure "/%year%/%monthnum%/%day%/%postname%/"' ); 87 | } 88 | 89 | /** 90 | * Check plugin status, install and activate as needed 91 | * 92 | * @param string $option --plugin value 93 | * @since 0.2 94 | */ 95 | private function manage_plugins( $option = NULL ){ 96 | if ( 'skip' === $option ) 97 | return; 98 | 99 | # default plugin set 100 | $std_plugin = array( 101 | 'debug-bar', 102 | 'debug-bar-console', 103 | 'debug-bar-cron', 104 | 'debug-bar-extender', 105 | 'developer', 106 | 'log-viewer', 107 | 'monster-widget', 108 | 'piglatin', 109 | 'regenerate-thumbnails', 110 | 'rewrite-rules-inspector', 111 | 'rtl-tester', 112 | 'simply-show-ids', 113 | 'theme-check', 114 | 'theme-test-drive', 115 | 'user-switching', 116 | 'wordpress-importer', 117 | 'wordpress-beta-tester', 118 | ); 119 | 120 | # wpcom VIP plugin set 121 | $vip_plugin = array( 122 | 'grunion-contact-form', 123 | 'jetpack', 124 | 'mp6', 125 | 'polldaddy', 126 | 'vip-scanner', 127 | ); 128 | 129 | # plugin developers bundle 130 | $dev_plugin = array( 131 | 'log-deprecated-notices', 132 | ); 133 | 134 | # debug plugin bundle (author's choice) 135 | # please file a pull request to include/exclude plugins 136 | $debug_plugin = array( 137 | 'debug-bar-actions-and-filters-addon', 138 | 'debug-bar-constants', 139 | 'debug-my-plugin', 140 | 'debug-objects', 141 | 'uploadplus', 142 | ); 143 | 144 | switch ( $option ): 145 | case 'vip': 146 | $plugin_list = array_merge( $std_plugin, $vip_plugin ); 147 | break; 148 | 149 | case 'devel': 150 | $plugin_list = array_merge( $std_plugin, $dev_plugin ); 151 | break; 152 | 153 | case 'debug': 154 | $plugin_list = array_merge( $std_plugin, $debug_plugin ); 155 | break; 156 | 157 | case 'all': 158 | $plugin_list = array_merge( $std_plugin, $vip_plugin, $dev_plugin, $debug_plugin ); 159 | 160 | case 'theme': 161 | default: 162 | $plugin_list = $std_plugin; 163 | break; 164 | endswitch; 165 | 166 | $skip_activation = array( 'piglatin', 'wordpress-beta-tester' ); 167 | # do install 168 | foreach ( $plugin_list as $plugin ) : 169 | $res = WP_CLI::launch( 'wp plugin status '.$plugin, false ); 170 | 171 | if ( isset( $res ) && $res === 1 ) { 172 | # install plugin (maybe skip piglatin) 173 | $cmdflag = ( in_array( $plugin, $skip_activation ) ) ? '' : ' --activate'; 174 | WP_CLI::launch( 'wp plugin install ' . $plugin . $cmdflag ); 175 | } else { 176 | # activate plugin (maybe skip piglatin) 177 | if ( false === in_array( $plugin, $skip_activation ) ) 178 | WP_CLI::launch( 'wp plugin activate '.$plugin ); 179 | } 180 | endforeach; 181 | } 182 | 183 | /** 184 | * Download and install theme unit test datafile 185 | * 186 | * @param string $option --data value 187 | * @since 0.2 188 | */ 189 | private function import_test_data( $option = NULL ){ 190 | if ( 'skip' === $option ) 191 | return; 192 | 193 | $option = ( NULL === $option ) ? 'unit-test' : $option; 194 | 195 | $datafiles = array( 196 | 'unit-test' => 'https://wpcom-themes.svn.automattic.com/demo/theme-unit-test-data.xml', 197 | 'wpcom-theme' => 'https://wpcom-themes.svn.automattic.com/demo/wordpress-com-theme-test.xml', 198 | 'wpcom-demo' => 'https://wpcom-themes.svn.automattic.com/demo/demo-data.xml', 199 | 'wptest' => 'https://raw.github.com/manovotny/wptest/master/wptest.xml', 200 | ); 201 | $keys = array_values( array_keys( $datafiles ) ); 202 | 203 | if ( in_array( $option, $keys ) ): 204 | $download_url = $datafiles[$option]; 205 | elseif ( false != $option ): 206 | $download_url = $option; 207 | else : 208 | WP_CLI::error( 'Missing WXR path/URL.' ); 209 | endif; 210 | 211 | WP_CLI::line( 'WXR data URL: ' . $download_url ); 212 | $silent = WP_CLI::get_config( 'quiet' ) ? '--silent ' : ''; 213 | $cmdline = "curl -f $silent $download_url -o /tmp/wp-cli-test-data.xml"; 214 | 215 | WP_CLI::launch( $cmdline ); 216 | WP_CLI::launch( 'wp import /tmp/wp-cli-test-data.xml --authors=skip' ); 217 | } 218 | 219 | 220 | /** 221 | * Install and setup theme unit test options, data and plugins 222 | * 223 | * Usage: wp theme-test setup [options] 224 | * 225 | * ## OPTIONS 226 | * 227 | * --data= 228 | * : URL/path to WXR data file [unit-test*|wpcom-theme|wpcom-demo|wptest|skip] 229 | * 230 | * --plugin= 231 | * : Install and activate suggested plugin bundle [theme*|vip|devel|debug|all|skip] 232 | * 233 | * --option=