From 76654fc769e21d5709f5f5f0ab6c547188cba06c Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Wed, 3 May 2017 12:50:01 +0100 Subject: [PATCH 1/9] Setup server side unit tests --- .travis.yml | 66 +++++++++++++- bin/install-wp-tests.sh | 127 +++++++++++++++++++++++++++ index.php | 21 ++++- phpcs.ruleset.xml | 10 +++ phpunit.xml.dist | 14 +++ tests/bootstrap.php | 25 ++++++ tests/test-dynamic-blocks-render.php | 42 +++++++++ tests/test-registration.php | 27 ++++++ 8 files changed, 325 insertions(+), 7 deletions(-) create mode 100755 bin/install-wp-tests.sh create mode 100644 phpcs.ruleset.xml create mode 100644 phpunit.xml.dist create mode 100644 tests/bootstrap.php create mode 100644 tests/test-dynamic-blocks-render.php create mode 100644 tests/test-registration.php diff --git a/.travis.yml b/.travis.yml index a9652ccc33e4a..1bfd065aa3ed4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,63 @@ -language: node_js -node_js: - - "node" +sudo: false + +language: + - php + - node_js + +notifications: + email: + on_success: never + on_failure: change + +branches: + only: + - master + +cache: + directories: + - vendor + - $HOME/.composer/cache + +matrix: + include: + - php: 7.1 + env: WP_VERSION=latest + - php: 7.0 + env: WP_VERSION=latest + - php: 5.6 + env: WP_VERSION=latest + - php: 5.6 + env: WP_VERSION=trunk + - php: 5.6 + env: WP_TRAVISCI=phpcs + - php: 5.3 + env: WP_VERSION=latest + +before_script: + - export PATH="$HOME/.composer/vendor/bin:$PATH" + - | + if [[ ! -z "$WP_VERSION" ]] ; then + bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION + if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then + composer global require "phpunit/phpunit=4.8.*" + else + composer global require "phpunit/phpunit=5.7.*" + fi + fi + - | + if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then + composer global require wp-coding-standards/wpcs + phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs + fi + script: - - "npm run ci" + - | + if [[ ! -z "$WP_VERSION" ]] ; then + phpunit + WP_MULTISITE=1 phpunit + fi + - | + if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then + phpcs --standard=phpcs.ruleset.xml $(find . -name '*.php') + fi + - npm run ci diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh new file mode 100755 index 0000000000000..73bb4c787eb2c --- /dev/null +++ b/bin/install-wp-tests.sh @@ -0,0 +1,127 @@ +#!/usr/bin/env bash + +if [ $# -lt 3 ]; then + echo "usage: $0 [db-host] [wp-version] [skip-database-creation]" + exit 1 +fi + +DB_NAME=$1 +DB_USER=$2 +DB_PASS=$3 +DB_HOST=${4-localhost} +WP_VERSION=${5-latest} +SKIP_DB_CREATE=${6-false} + +WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} +WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/} + +download() { + if [ `which curl` ]; then + curl -s "$1" > "$2"; + elif [ `which wget` ]; then + wget -nv -O "$2" "$1" + fi +} + +if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then + WP_TESTS_TAG="tags/$WP_VERSION" +elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + WP_TESTS_TAG="trunk" +else + # http serves a single offer, whereas https serves multiple. we only want one + download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json + grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json + LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') + if [[ -z "$LATEST_VERSION" ]]; then + echo "Latest WordPress version could not be found" + exit 1 + fi + WP_TESTS_TAG="tags/$LATEST_VERSION" +fi + +set -ex + +install_wp() { + + if [ -d $WP_CORE_DIR ]; then + return; + fi + + mkdir -p $WP_CORE_DIR + + if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then + mkdir -p /tmp/wordpress-nightly + download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip + unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/ + mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR + else + if [ $WP_VERSION == 'latest' ]; then + local ARCHIVE_NAME='latest' + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz + tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR + fi + + download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php +} + +install_test_suite() { + # portable in-place argument for both GNU sed and Mac OSX sed + if [[ $(uname -s) == 'Darwin' ]]; then + local ioption='-i .bak' + else + local ioption='-i' + fi + + # set up testing suite if it doesn't yet exist + if [ ! -d $WP_TESTS_DIR ]; then + # set up testing suite + mkdir -p $WP_TESTS_DIR + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data + fi + + if [ ! -f wp-tests-config.php ]; then + download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php + # remove all forward slashes in the end + WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php + sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php + fi + +} + +install_db() { + + if [ ${SKIP_DB_CREATE} = "true" ]; then + return 0 + fi + + # parse DB_HOST for port or socket references + local PARTS=(${DB_HOST//\:/ }) + local DB_HOSTNAME=${PARTS[0]}; + local DB_SOCK_OR_PORT=${PARTS[1]}; + local EXTRA="" + + if ! [ -z $DB_HOSTNAME ] ; then + if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" + elif ! [ -z $DB_SOCK_OR_PORT ] ; then + EXTRA=" --socket=$DB_SOCK_OR_PORT" + elif ! [ -z $DB_HOSTNAME ] ; then + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" + fi + fi + + # create database + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA +} + +install_wp +install_test_suite +install_db diff --git a/index.php b/index.php index 89d70866886c8..219482f5ce859 100644 --- a/index.php +++ b/index.php @@ -55,8 +55,6 @@ function register_block( $slug, $settings ) { } if ( isset( $registered_blocks[ $slug ] ) ) { - $message = sprintf( __( 'Block "%s" is already registered.' ), $slug ); - _doing_it_wrong( __FUNCTION__, $message, '0.1.0' ); return; } @@ -66,6 +64,23 @@ function register_block( $slug, $settings ) { return $settings; } +/** + * Unregisters a block. + * + * @param string slug Block slug + * @return array The previous block value, if it has been + * successfully unregistered; otherwise `null`. + */ +function unregister_block( $slug ) { + global $registered_blocks; + if ( ! isset( $registered_blocks[ $slug ] ) ) { + $message = sprintf( __( 'Block "%s" is not registered.' ), $slug ); + _doing_it_wrong( __FUNCTION__, $message, '0.1.0' ); + return; + } + unset( $registered_blocks[ $slug ] ); +} + /** * Extract the block attributes from the block's attributes string * @@ -99,7 +114,7 @@ function do_blocks( $content ) { global $registered_blocks; // Extract the blocks from the post content - $open_matcher = '/).)*)-->.*/'; + $open_matcher = '/).)*)-->/'; preg_match_all( $open_matcher, $content, $matches, PREG_OFFSET_CAPTURE ); $new_content = $content; diff --git a/phpcs.ruleset.xml b/phpcs.ruleset.xml new file mode 100644 index 0000000000000..210c25a14e74a --- /dev/null +++ b/phpcs.ruleset.xml @@ -0,0 +1,10 @@ + + + Generally-applicable sniffs for WordPress plugins + + + + + */node_modules/* + */vendor/* + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000000000..44f0fdb69cf28 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,14 @@ + + + + ./tests/ + + + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000000000..4cc74bcdf89fd --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,25 @@ + [ $this, 'render_dummy_block' ] ); + register_block( 'core/dummy', $settings ); + $post_content = + 'before' . + '' . + 'between' . + '' . + 'after'; + + $updated_post_content = do_blocks( $post_content ); + unregister_block( 'core/dummy' ); + $this->assertEquals( $updated_post_content, + 'before' . + 'b1' . + 'between' . + 'b2' . + 'after' + ); + } +} diff --git a/tests/test-registration.php b/tests/test-registration.php new file mode 100644 index 0000000000000..e18ee72e34817 --- /dev/null +++ b/tests/test-registration.php @@ -0,0 +1,27 @@ + 'text' ); + $updated_settings = register_block( 'core/text', $settings ); + $this->assertEquals( $updated_settings, array( + 'icon' => 'text', + 'slug' => 'core/text' + ) ); + unregister_block( 'core/text' ); + } +} From 1d7b3ef9def1241e72fecab30029a4d7dd78649a Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 4 May 2017 13:10:09 +0100 Subject: [PATCH 2/9] Testing: Fix the travis Matrix --- .travis.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1bfd065aa3ed4..e70d67a05cd05 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,16 +22,13 @@ matrix: include: - php: 7.1 env: WP_VERSION=latest - - php: 7.0 - env: WP_VERSION=latest - php: 5.6 env: WP_VERSION=latest - - php: 5.6 - env: WP_VERSION=trunk - - php: 5.6 - env: WP_TRAVISCI=phpcs - - php: 5.3 + - php: 5.2 env: WP_VERSION=latest + - php: 5.6 + env: TRAVISCI=phpcs + - env: TRAVISCI=js before_script: - export PATH="$HOME/.composer/vendor/bin:$PATH" @@ -45,7 +42,7 @@ before_script: fi fi - | - if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then + if [[ "$TRAVISCI" == "phpcs" ]] ; then composer global require wp-coding-standards/wpcs phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs fi @@ -57,7 +54,10 @@ script: WP_MULTISITE=1 phpunit fi - | - if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then + if [[ "$TRAVISCI" == "phpcs" ]] ; then phpcs --standard=phpcs.ruleset.xml $(find . -name '*.php') fi - - npm run ci + - | + if [[ "$TRAVISCI" == "js" ]] ; then + npm run ci + fi From ee130055b852dd8179a8ac4f36b7b1d566009594 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 4 May 2017 13:21:29 +0100 Subject: [PATCH 3/9] Testing: Move the unit tests to phpunit folder --- phpunit.xml.dist | 4 ++-- {tests => phpunit}/bootstrap.php | 2 +- {tests => phpunit}/test-dynamic-blocks-render.php | 0 {tests => phpunit}/test-registration.php | 0 4 files changed, 3 insertions(+), 3 deletions(-) rename {tests => phpunit}/bootstrap.php (96%) rename {tests => phpunit}/test-dynamic-blocks-render.php (100%) rename {tests => phpunit}/test-registration.php (100%) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 44f0fdb69cf28..ed4079d33ed58 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - ./tests/ + ./phpunit/ diff --git a/tests/bootstrap.php b/phpunit/bootstrap.php similarity index 96% rename from tests/bootstrap.php rename to phpunit/bootstrap.php index 4cc74bcdf89fd..a398feb4fa5fe 100644 --- a/tests/bootstrap.php +++ b/phpunit/bootstrap.php @@ -2,7 +2,7 @@ /** * PHPUnit bootstrap file * - * @package Guttenberg + * @package Gutenberg */ $_tests_dir = getenv( 'WP_TESTS_DIR' ); diff --git a/tests/test-dynamic-blocks-render.php b/phpunit/test-dynamic-blocks-render.php similarity index 100% rename from tests/test-dynamic-blocks-render.php rename to phpunit/test-dynamic-blocks-render.php diff --git a/tests/test-registration.php b/phpunit/test-registration.php similarity index 100% rename from tests/test-registration.php rename to phpunit/test-registration.php From 7aec8baea43e1f8edc8015c6923e675d66b3d7e0 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 4 May 2017 13:24:41 +0100 Subject: [PATCH 4/9] Blocks: Non-greedy match for the server side rendering --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.php b/index.php index 219482f5ce859..03c4de6f16f94 100644 --- a/index.php +++ b/index.php @@ -114,7 +114,7 @@ function do_blocks( $content ) { global $registered_blocks; // Extract the blocks from the post content - $open_matcher = '/).)*)-->/'; + $open_matcher = '/).)*)-->.*?/'; preg_match_all( $open_matcher, $content, $matches, PREG_OFFSET_CAPTURE ); $new_content = $content; From 07c793dd90596e25bd2a38dac9b1103d9834ede8 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 4 May 2017 13:50:42 +0100 Subject: [PATCH 5/9] Try to fix travis config --- .travis.yml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index e70d67a05cd05..2a70b3710abc8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ sudo: false -language: - - php - - node_js +language: php notifications: email: @@ -18,6 +16,12 @@ cache: - vendor - $HOME/.composer/cache +env: + - TRAVIS_NODE_VERSION="7" + +install: + - rm -rf ~/.nvm && git clone https://github.com/creationix/nvm.git ~/.nvm && (cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`) && source ~/.nvm/nvm.sh && nvm install $TRAVIS_NODE_VERSION + matrix: include: - php: 7.1 @@ -28,7 +32,8 @@ matrix: env: WP_VERSION=latest - php: 5.6 env: TRAVISCI=phpcs - - env: TRAVISCI=js + - php: 5.6 + env: TRAVISCI=js before_script: - export PATH="$HOME/.composer/vendor/bin:$PATH" From a60d52eff370212d49fd430a390ca7805999a6bb Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 4 May 2017 14:00:43 +0100 Subject: [PATCH 6/9] Testing: run tests on all branches --- .travis.yml | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2a70b3710abc8..c7a063f116b42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,20 +7,13 @@ notifications: on_success: never on_failure: change -branches: - only: - - master - cache: directories: - vendor - $HOME/.composer/cache -env: - - TRAVIS_NODE_VERSION="7" - -install: - - rm -rf ~/.nvm && git clone https://github.com/creationix/nvm.git ~/.nvm && (cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`) && source ~/.nvm/nvm.sh && nvm install $TRAVIS_NODE_VERSION +before_install: + - nvm install 6 && nvm use 6 matrix: include: @@ -32,7 +25,7 @@ matrix: env: WP_VERSION=latest - php: 5.6 env: TRAVISCI=phpcs - - php: 5.6 + - php: 7.1 env: TRAVISCI=js before_script: From 378ee1b138d9c5823fbe0ff4d375c3dae8fd354d Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 4 May 2017 14:25:20 +0100 Subject: [PATCH 7/9] Fix JS build --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c7a063f116b42..00b89b3287590 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ cache: - $HOME/.composer/cache before_install: - - nvm install 6 && nvm use 6 + - nvm install 7 && nvm use 7 matrix: include: @@ -57,5 +57,6 @@ script: fi - | if [[ "$TRAVISCI" == "js" ]] ; then + npm install npm run ci fi From f0cd8603bec8bce61a3c6736e5eac4cd7156bfe6 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 4 May 2017 15:04:56 +0100 Subject: [PATCH 8/9] Testing Fix phpcs job --- index.php | 93 ++++++++++--------- phpunit.xml.dist | 2 +- ...p => class-dynamic-blocks-render-test.php} | 37 +++++--- ...ration.php => class-registration-test.php} | 23 ++++- 4 files changed, 91 insertions(+), 64 deletions(-) rename phpunit/{test-dynamic-blocks-render.php => class-dynamic-blocks-render-test.php} (54%) rename phpunit/{test-registration.php => class-registration-test.php} (58%) diff --git a/index.php b/index.php index 03c4de6f16f94..6d7adaf579de2 100644 --- a/index.php +++ b/index.php @@ -33,10 +33,10 @@ function gutenberg_menu() { /** * Registers a block. * - * @param string $slug Block slug including namespace. - * @param array $settings Block settings + * @param string $slug Block slug including namespace. + * @param array $settings Block settings. - * @return array The block, if it has been successfully registered. + * @return array The block, if it has been successfully registered. */ function register_block( $slug, $settings ) { global $registered_blocks; @@ -58,7 +58,7 @@ function register_block( $slug, $settings ) { return; } - $settings[ 'slug' ] = $slug; + $settings['slug'] = $slug; $registered_blocks[ $slug ] = $settings; return $settings; @@ -67,13 +67,14 @@ function register_block( $slug, $settings ) { /** * Unregisters a block. * - * @param string slug Block slug - * @return array The previous block value, if it has been - * successfully unregistered; otherwise `null`. + * @param string $slug Block slug. + * @return array The previous block value, if it has been + * successfully unregistered; otherwise `null`. */ function unregister_block( $slug ) { global $registered_blocks; if ( ! isset( $registered_blocks[ $slug ] ) ) { + /* translators: 1: block slug */ $message = sprintf( __( 'Block "%s" is not registered.' ), $slug ); _doing_it_wrong( __FUNCTION__, $message, '0.1.0' ); return; @@ -86,7 +87,7 @@ function unregister_block( $slug ) { * * @since 0.1.0 * - * @param string $attr_string Attributes string + * @param string $attr_string Attributes string. * @return array */ @@ -94,8 +95,8 @@ function parse_block_attributes( $attr_string ) { $attributes_matcher = '/([^\s]+):([^\s]+)\s*/'; preg_match_all( $attributes_matcher, $attr_string, $matches ); $attributes = array(); - foreach ( $matches[ 1 ] as $index => $attribute_match ) { - $attributes[ $attribute_match ] = $matches[ 2 ][ $index ]; + foreach ( $matches[1] as $index => $attribute_match ) { + $attributes[ $attribute_match ] = $matches[2][ $index ]; } return $attributes; @@ -106,40 +107,40 @@ function parse_block_attributes( $attr_string ) { * * @since 0.1.0 * - * @param string $content Post content - - * @return string Updated post content + * @param string $content Post content. + * + * @return string Updated post content. */ function do_blocks( $content ) { global $registered_blocks; - // Extract the blocks from the post content + // Extract the blocks from the post content. $open_matcher = '/).)*)-->.*?/'; preg_match_all( $open_matcher, $content, $matches, PREG_OFFSET_CAPTURE ); $new_content = $content; - foreach ( $matches[ 0 ] as $index => $block_match ) { - $block_name = $matches[ 1 ][ $index ][ 0 ]; - // do nothing if the block is not registered + foreach ( $matches[0] as $index => $block_match ) { + $block_name = $matches[1][ $index ][0]; + // do nothing if the block is not registered. if ( ! isset( $registered_blocks[ $block_name ] ) ) { continue; } - $block_markup = $block_match[ 0 ]; - $block_position = $block_match[ 1 ]; - $block_attributes_string = $matches[ 2 ][ $index ][ 0 ]; + $block_markup = $block_match[0]; + $block_position = $block_match[1]; + $block_attributes_string = $matches[2][ $index ][0]; $block_attributes = parse_block_attributes( $block_attributes_string ); - // Call the block's render function to generate the dynamic output - $output = call_user_func( $registered_blocks[ $block_name ][ 'render' ], $block_attributes ); + // Call the block's render function to generate the dynamic output. + $output = call_user_func( $registered_blocks[ $block_name ]['render'], $block_attributes ); - // Replace the matched block with the dynamic output + // Replace the matched block with the dynamic output. $new_content = str_replace( $block_markup, $output, $new_content ); } return $new_content; } -add_filter( 'the_content', 'do_blocks', 10 ); // BEFORE do_shortcode() +add_filter( 'the_content', 'do_blocks', 10 ); // BEFORE do_shortcode(). /** * Registers common scripts to be used as dependencies of the editor and plugins. @@ -173,9 +174,9 @@ function gutenberg_register_scripts() { * @since 0.1.0 */ function gutenberg_add_edit_links_filters() { - // For hierarchical post types + // For hierarchical post types. add_filter( 'page_row_actions', 'gutenberg_add_edit_links', 10, 2 ); - // For non-hierarchical post types + // For non-hierarchical post types. add_filter( 'post_row_actions', 'gutenberg_add_edit_links', 10, 2 ); } add_action( 'admin_init', 'gutenberg_add_edit_links_filters' ); @@ -185,14 +186,18 @@ function gutenberg_add_edit_links_filters() { * the Gutenberg editor. * * @since 0.1.0 + * + * @param array $actions Post actions. + * @param array $post Edited post. + * + * @return array Updated post actions. */ function gutenberg_add_edit_links( $actions, $post ) { $can_edit_post = current_user_can( 'edit_post', $post->ID ); $title = _draft_or_post_title( $post->ID ); if ( $can_edit_post && 'trash' !== $post->post_status ) { - // Build the Gutenberg edit action. See also: - // WP_Posts_List_Table::handle_row_actions() + // Build the Gutenberg edit action. See also: WP_Posts_List_Table::handle_row_actions(). $gutenberg_url = menu_page_url( 'gutenberg', false ); $gutenberg_action = sprintf( '%s', @@ -208,7 +213,9 @@ function gutenberg_add_edit_links( $actions, $post ) { $edit_offset = array_search( 'edit', array_keys( $actions ), true ); $actions = array_merge( array_slice( $actions, 0, $edit_offset + 1 ), - array( 'gutenberg hide-if-no-js' => $gutenberg_action ), + array( + 'gutenberg hide-if-no-js' => $gutenberg_action, + ), array_slice( $actions, $edit_offset + 1 ) ); } @@ -221,6 +228,8 @@ function gutenberg_add_edit_links( $actions, $post ) { * * @since 0.1.0 * + * @param string $domain Translation domain. + * * @return array */ function gutenberg_get_jed_locale_data( $domain ) { @@ -232,10 +241,10 @@ function gutenberg_get_jed_locale_data( $domain ) { $domain => array( '' => array( 'domain' => $domain, - 'lang' => is_admin() ? get_user_locale() : get_locale() - ) - ) - ) + 'lang' => is_admin() ? get_user_locale() : get_locale(), + ), + ), + ), ); if ( ! empty( $translations->headers['Plural-Forms'] ) ) { @@ -268,16 +277,16 @@ function gutenberg_scripts_and_styles( $hook ) { * Scripts */ - // The editor code itself + // The editor code itself. wp_enqueue_script( 'wp-editor', plugins_url( 'editor/build/index.js', __FILE__ ), array( 'wp-i18n', 'wp-blocks', 'wp-element' ), filemtime( plugin_dir_path( __FILE__ ) . 'editor/build/index.js' ), - true // $in_footer + true // enqueue in the footer. ); - // Load an actual post if an ID is specified + // Load an actual post if an ID is specified. $post_to_edit = null; if ( isset( $_GET['post_id'] ) && (int) $_GET['post_id'] > 0 ) { $request = new WP_REST_Request( @@ -304,7 +313,7 @@ function gutenberg_scripts_and_styles( $hook ) { ); } - // Prepare Jed locale data + // Prepare Jed locale data. $locale_data = gutenberg_get_jed_locale_data( 'gutenberg' ); wp_add_inline_script( 'wp-editor', @@ -312,7 +321,7 @@ function gutenberg_scripts_and_styles( $hook ) { 'before' ); - // Initialize the editor + // Initialize the editor. wp_add_inline_script( 'wp-editor', 'wp.editor.createEditorInstance( \'editor\', _wpGutenbergPost );' ); /** @@ -361,11 +370,3 @@ function the_gutenberg_project() { 'render_html_block' -) ); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ed4079d33ed58..2eb5f4eae3c4b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,7 +8,7 @@ > - ./phpunit/ + ./phpunit/ diff --git a/phpunit/test-dynamic-blocks-render.php b/phpunit/class-dynamic-blocks-render-test.php similarity index 54% rename from phpunit/test-dynamic-blocks-render.php rename to phpunit/class-dynamic-blocks-render-test.php index eadeaf97e7eb7..a7c01439ccf2d 100644 --- a/phpunit/test-dynamic-blocks-render.php +++ b/phpunit/class-dynamic-blocks-render-test.php @@ -1,26 +1,35 @@ [ $this, 'render_dummy_block' ] ); + $settings = array( + 'render' => array( + $this, + 'render_dummy_block', + ), + ); register_block( 'core/dummy', $settings ); $post_content = 'before' . diff --git a/phpunit/test-registration.php b/phpunit/class-registration-test.php similarity index 58% rename from phpunit/test-registration.php rename to phpunit/class-registration-test.php index e18ee72e34817..08b86e58fe394 100644 --- a/phpunit/test-registration.php +++ b/phpunit/class-registration-test.php @@ -1,7 +1,17 @@ 'text' ); + $settings = array( + 'icon' => 'text', + ); $updated_settings = register_block( 'core/text', $settings ); $this->assertEquals( $updated_settings, array( 'icon' => 'text', - 'slug' => 'core/text' + 'slug' => 'core/text', ) ); unregister_block( 'core/text' ); } From d0eb6adf3197cd143e23ff53fd225e7657addff1 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 4 May 2017 15:23:20 +0100 Subject: [PATCH 9/9] Testing: Fix phpunit for old PHP verions --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 00b89b3287590..076ccfd024946 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,7 +34,10 @@ before_script: if [[ ! -z "$WP_VERSION" ]] ; then bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." ]]; then - composer global require "phpunit/phpunit=4.8.*" + mkdir -p $HOME/phpunit-bin + wget -O $HOME/phpunit-bin/phpunit https://phar.phpunit.de/phpunit-4.8.phar + chmod +x $HOME/phpunit-bin/phpunit + export PATH=$PATH:$HOME/phpunit-bin/ else composer global require "phpunit/phpunit=5.7.*" fi