From 6a50ebde8bea81ba0525699a16addd5688d0451b Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Tue, 27 Nov 2018 12:12:48 -0500 Subject: [PATCH] Adds continuous integration testing with travis (https://github.com/woocommerce/woocommerce-blocks/pull/156) * Add travis to run JS & CSS linting, JS tests, and PHPUnit tests * Move API initiation out of the gutenberg/files check So that php tests run without having to build the JS files * Add very basic product endpoint tests * Fix stylelint issues in CSS * Remove e2e setup, replace with dependency setup * Add missing install step * Remove cache for now * Add specific `har-validator` dependency because v 5.1.2 has disappeared * Only run npm install for the JS test job * Check for installed plugins * Update install location * Fix typo in plugin name * Check the BRANCH variable, rather than TRAVIS_PULL_REQUEST_BRANCH, which is only set on PR runs * Remove debug command * A change to trigger CI * Set the expected node & npm versions * Remove copy-pasted command, does not apply --- plugins/woocommerce-blocks/.nvmrc | 1 + plugins/woocommerce-blocks/.travis.yml | 34 ++++ .../assets/css/abstracts/_breakpoints.scss | 27 +-- .../assets/css/abstracts/_mixins.scss | 2 +- .../assets/css/product-category-block.scss | 2 +- plugins/woocommerce-blocks/package-lock.json | 63 +++---- plugins/woocommerce-blocks/package.json | 5 +- plugins/woocommerce-blocks/phpunit.xml.dist | 17 ++ .../tests/api/products-extra.php | 116 ++++++++++++ .../tests/bin/install-wp-tests.sh | 171 ++++++++++++++++++ .../woocommerce-blocks/tests/bin/phpunit.sh | 8 + .../woocommerce-blocks/tests/bootstrap.php | 104 +++++++++++ .../woocommerce-gutenberg-products-block.php | 2 +- 13 files changed, 494 insertions(+), 58 deletions(-) create mode 100644 plugins/woocommerce-blocks/.nvmrc create mode 100644 plugins/woocommerce-blocks/.travis.yml create mode 100644 plugins/woocommerce-blocks/phpunit.xml.dist create mode 100644 plugins/woocommerce-blocks/tests/api/products-extra.php create mode 100755 plugins/woocommerce-blocks/tests/bin/install-wp-tests.sh create mode 100755 plugins/woocommerce-blocks/tests/bin/phpunit.sh create mode 100644 plugins/woocommerce-blocks/tests/bootstrap.php diff --git a/plugins/woocommerce-blocks/.nvmrc b/plugins/woocommerce-blocks/.nvmrc new file mode 100644 index 00000000000..b009dfb9d9f --- /dev/null +++ b/plugins/woocommerce-blocks/.nvmrc @@ -0,0 +1 @@ +lts/* diff --git a/plugins/woocommerce-blocks/.travis.yml b/plugins/woocommerce-blocks/.travis.yml new file mode 100644 index 00000000000..8ddcb192b9a --- /dev/null +++ b/plugins/woocommerce-blocks/.travis.yml @@ -0,0 +1,34 @@ +sudo: required + +dist: trusty + +language: php + +before_install: + - nvm install && nvm use + - npm install npm -g + +before_script: + - phpenv config-rm xdebug.ini + - bash tests/bin/install-wp-tests.sh wgpb_admin root '' localhost $WP_VERSION + - node --version + - npm --version + - timedatectl + +jobs: + include: + - stage: test + env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress + php: 7.1 + script: + - npm install + - npm run lint + - npm run build + - npm test + - bash tests/bin/phpunit.sh + + - stage: test + env: WP_VERSION=latest WP_MULTISITE=0 WP_CORE_DIR=/tmp/wordpress + php: 5.6 + script: + - bash tests/bin/phpunit.sh diff --git a/plugins/woocommerce-blocks/assets/css/abstracts/_breakpoints.scss b/plugins/woocommerce-blocks/assets/css/abstracts/_breakpoints.scss index 050fc819b96..4039e83cf39 100644 --- a/plugins/woocommerce-blocks/assets/css/abstracts/_breakpoints.scss +++ b/plugins/woocommerce-blocks/assets/css/abstracts/_breakpoints.scss @@ -12,23 +12,25 @@ $breakpoints: 320px, 400px, 600px, 782px, 960px, 1280px, 1440px; @if type-of( $size ) == string { $approved-value: 0; @each $breakpoint in $breakpoints { - $and-larger: '>' + $breakpoint; - $and-smaller: '<' + $breakpoint; + $and-larger: ">" + $breakpoint; + $and-smaller: "<" + $breakpoint; @if $size == $and-smaller { $approved-value: 1; @media (max-width: $breakpoint) { @content; } - } @else { + } + @else { @if $size == $and-larger { $approved-value: 2; @media (min-width: $breakpoint + 1) { @content; } - } @else { + } + @else { @each $breakpoint-end in $breakpoints { - $range: $breakpoint + '-' + $breakpoint-end; + $range: $breakpoint + "-" + $breakpoint-end; @if $size == $range { $approved-value: 3; @media (min-width: $breakpoint + 1) and (max-width: $breakpoint-end) { @@ -40,18 +42,19 @@ $breakpoints: 320px, 400px, 600px, 782px, 960px, 1280px, 1440px; } } @if $approved-value == 0 { - $sizes: ''; + $sizes: ""; @each $breakpoint in $breakpoints { - $sizes: $sizes + ' ' + $breakpoint; + $sizes: $sizes + " " + $breakpoint; } - @warn 'ERROR in breakpoint( #{ $size } ) : You can only use these sizes[ #{$sizes} ] using the following syntax [ <#{ nth( $breakpoints, 1 ) } >#{ nth( $breakpoints, 1 ) } #{ nth( $breakpoints, 1 ) }-#{ nth( $breakpoints, 2 ) } ]'; + @warn "ERROR in breakpoint( #{ $size } ) : You can only use these sizes[ #{$sizes} ] using the following syntax [ <#{ nth( $breakpoints, 1 ) } >#{ nth( $breakpoints, 1 ) } #{ nth( $breakpoints, 1 ) }-#{ nth( $breakpoints, 2 ) } ]"; } - } @else { - $sizes: ''; + } + @else { + $sizes: ""; @each $breakpoint in $breakpoints { - $sizes: $sizes + ' ' + $breakpoint; + $sizes: $sizes + " " + $breakpoint; } - @error 'ERROR in breakpoint( #{ $size } ) : Please wrap the breakpoint $size in parenthesis. You can use these sizes[ #{$sizes} ] using the following syntax [ <#{ nth( $breakpoints, 1 ) } >#{ nth( $breakpoints, 1 ) } #{ nth( $breakpoints, 1 ) }-#{ nth( $breakpoints, 2 ) } ]'; + @error "ERROR in breakpoint( #{ $size } ) : Please wrap the breakpoint $size in parenthesis. You can use these sizes[ #{$sizes} ] using the following syntax [ <#{ nth( $breakpoints, 1 ) } >#{ nth( $breakpoints, 1 ) } #{ nth( $breakpoints, 1 ) }-#{ nth( $breakpoints, 2 ) } ]"; } } } diff --git a/plugins/woocommerce-blocks/assets/css/abstracts/_mixins.scss b/plugins/woocommerce-blocks/assets/css/abstracts/_mixins.scss index 1d9aa0fe8ea..1443afa2662 100644 --- a/plugins/woocommerce-blocks/assets/css/abstracts/_mixins.scss +++ b/plugins/woocommerce-blocks/assets/css/abstracts/_mixins.scss @@ -22,7 +22,7 @@ color: transparent; &::after { - content: '\00a0'; + content: "\00a0"; } } diff --git a/plugins/woocommerce-blocks/assets/css/product-category-block.scss b/plugins/woocommerce-blocks/assets/css/product-category-block.scss index 2e3384efb35..ccffe0b8794 100644 --- a/plugins/woocommerce-blocks/assets/css/product-category-block.scss +++ b/plugins/woocommerce-blocks/assets/css/product-category-block.scss @@ -1,6 +1,6 @@ // Import the woocommerce components stylesheet // @todo Move this to a separate file so we can build a cacheable single stylesheet for all blocks. -@import '../../node_modules/@woocommerce/components/build-style/style.css'; +@import "../../node_modules/@woocommerce/components/build-style/style.css"; .wc-block-products-category { overflow: hidden; diff --git a/plugins/woocommerce-blocks/package-lock.json b/plugins/woocommerce-blocks/package-lock.json index 88560a5f9fa..5ac5582bc4a 100644 --- a/plugins/woocommerce-blocks/package-lock.json +++ b/plugins/woocommerce-blocks/package-lock.json @@ -6113,8 +6113,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -6135,14 +6134,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -6157,20 +6154,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -6287,8 +6281,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -6300,7 +6293,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -6315,7 +6307,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -6323,14 +6314,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -6349,7 +6338,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -6430,8 +6418,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -6443,7 +6430,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -6529,8 +6515,7 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -6566,7 +6551,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -6586,7 +6570,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -6630,14 +6613,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -6978,9 +6959,9 @@ "dev": true }, "har-validator": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.1.2.tgz", - "integrity": "sha512-OFxb5MZXCUMx43X7O8LK4FKggEQx6yC5QPmOcBnYbJ9UjxEcMcrMbaR0af5HZpqeFopw2GwQRQi34ZXI7YLM5w==", + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/har-validator/-/har-validator-5.1.3.tgz", + "integrity": "sha512-sNvOCzEQNr/qrvJgc3UG/kD4QtlHycrzwS+6mfTrrSq97BvaYcPZZI1ZSqGSPR73Cxn4LKTD4PttRwfU7jWq5g==", "dev": true, "requires": { "ajv": "^6.5.5", @@ -8126,7 +8107,7 @@ }, "callsites": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", "integrity": "sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=", "dev": true }, @@ -8516,7 +8497,7 @@ }, "callsites": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", "integrity": "sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=", "dev": true }, @@ -8749,7 +8730,7 @@ }, "callsites": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", "integrity": "sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=", "dev": true }, @@ -9037,7 +9018,7 @@ }, "callsites": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", "integrity": "sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=", "dev": true }, @@ -9359,7 +9340,7 @@ }, "callsites": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", "integrity": "sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=", "dev": true }, @@ -9569,7 +9550,7 @@ }, "callsites": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", "integrity": "sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=", "dev": true }, @@ -9976,7 +9957,7 @@ "dependencies": { "callsites": { "version": "2.0.0", - "resolved": "http://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", + "resolved": "https://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", "integrity": "sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=", "dev": true }, @@ -13921,7 +13902,7 @@ }, "stream-browserify": { "version": "2.0.1", - "resolved": "http://registry.npmjs.org/stream-browserify/-/stream-browserify-2.0.1.tgz", + "resolved": "https://registry.npmjs.org/stream-browserify/-/stream-browserify-2.0.1.tgz", "integrity": "sha1-ZiZu5fm9uZQKTkUUyvtDu3Hlyds=", "dev": true, "requires": { diff --git a/plugins/woocommerce-blocks/package.json b/plugins/woocommerce-blocks/package.json index b58078d6c42..f949ec24b14 100644 --- a/plugins/woocommerce-blocks/package.json +++ b/plugins/woocommerce-blocks/package.json @@ -47,6 +47,7 @@ "eslint-plugin-jsx-a11y": "6.1.2", "eslint-plugin-react": "7.11.1", "eslint-plugin-wordpress": "git://github.com/WordPress-Coding-Standards/eslint-plugin-wordpress.git#1774343f6226052a46b081e01db3fca8793cc9f1", + "har-validator": "^5.1.3", "interpolate-components": "^1.1.1", "mini-css-extract-plugin": "0.4.4", "node-sass": "4.10.0", @@ -60,8 +61,8 @@ "webpack-cli": "3.1.2" }, "engines": { - "node": ">=8.9.3", - "npm": ">=5.5.1" + "node": "10.13.0", + "npm": "6.4.1" }, "dependencies": { "@woocommerce/components": "1.0.1" diff --git a/plugins/woocommerce-blocks/phpunit.xml.dist b/plugins/woocommerce-blocks/phpunit.xml.dist new file mode 100644 index 00000000000..83ecbf61fcb --- /dev/null +++ b/plugins/woocommerce-blocks/phpunit.xml.dist @@ -0,0 +1,17 @@ + + + + + ./tests/ + + + diff --git a/plugins/woocommerce-blocks/tests/api/products-extra.php b/plugins/woocommerce-blocks/tests/api/products-extra.php new file mode 100644 index 00000000000..c2386c74dc3 --- /dev/null +++ b/plugins/woocommerce-blocks/tests/api/products-extra.php @@ -0,0 +1,116 @@ +user = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + $this->author = $this->factory->user->create( + array( + 'role' => 'author', + ) + ); + } + + /** + * Test route registration. + * + * @since 1.2.0 + */ + public function test_register_routes() { + $routes = $this->server->get_routes(); + + $this->assertArrayHasKey( '/wgbp/v3/products', $routes ); + $this->assertArrayHasKey( '/wgbp/v3/products/(?P[\d]+)', $routes ); + $this->assertArrayHasKey( '/wgbp/v3/products/batch', $routes ); + } + + /** + * Test getting products. + * + * @since 1.2.0 + */ + public function test_get_products() { + wp_set_current_user( $this->user ); + WC_Helper_Product::create_external_product(); + sleep( 1 ); // So both products have different timestamps. + $product = WC_Helper_Product::create_simple_product(); + $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wgbp/v3/products' ) ); + $products = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + + $this->assertEquals( 2, count( $products ) ); + $this->assertEquals( 'Dummy Product', $products[0]['name'] ); + $this->assertEquals( 'DUMMY SKU', $products[0]['sku'] ); + $this->assertEquals( 'Dummy External Product', $products[1]['name'] ); + $this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] ); + } + + /** + * Test getting products as an author. + * + * @since 1.2.0 + */ + public function test_get_products_as_author() { + wp_set_current_user( $this->author ); + WC_Helper_Product::create_simple_product(); + $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wgbp/v3/products' ) ); + $this->assertEquals( 403, $response->get_status() ); + } + + /** + * Test getting products with custom ordering. + * + * @since 1.2.0 + */ + public function test_get_products_order_by_price() { + wp_set_current_user( $this->user ); + WC_Helper_Product::create_external_product(); + sleep( 1 ); // So both products have different timestamps. + $product = WC_Helper_Product::create_simple_product( false ); // Prevent saving, since we save here. + // Customize the price, otherwise both are 10. + $product->set_props( array( + 'regular_price' => 15, + 'price' => 15, + ) ); + $product->save(); + + $request = new WP_REST_Request( 'GET', '/wgbp/v3/products' ); + $request->set_param( 'orderby', 'price' ); + $request->set_param( 'order', 'asc' ); + $response = $this->server->dispatch( $request ); + $products = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertEquals( 2, count( $products ) ); + + // The external product should be first, then the simple product + $this->assertEquals( 'Dummy External Product', $products[0]['name'] ); + $this->assertEquals( 'Dummy Product', $products[1]['name'] ); + $this->assertEquals( '10', $products[0]['price'] ); + $this->assertEquals( '15', $products[1]['price'] ); + } +} diff --git a/plugins/woocommerce-blocks/tests/bin/install-wp-tests.sh b/plugins/woocommerce-blocks/tests/bin/install-wp-tests.sh new file mode 100755 index 00000000000..49c764d54f1 --- /dev/null +++ b/plugins/woocommerce-blocks/tests/bin/install-wp-tests.sh @@ -0,0 +1,171 @@ +#!/usr/bin/env bash +# See https://raw.githubusercontent.com/wp-cli/scaffold-command/master/templates/install-wp-tests.sh + +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} + +TMPDIR=${TMPDIR-/tmp} +TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") +WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} +WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/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_deps() { + + # Script Variables + WP_SITE_URL="http://local.wordpress.test" + BRANCH=$TRAVIS_BRANCH + REPO=$TRAVIS_REPO_SLUG + WORKING_DIR="$PWD" + + if [ "$TRAVIS_PULL_REQUEST_BRANCH" != "" ]; then + BRANCH=$TRAVIS_PULL_REQUEST_BRANCH + REPO=$TRAVIS_PULL_REQUEST_SLUG + fi + + # Set up WordPress using wp-cli + mkdir -p "$WP_CORE_DIR" + cd "$WP_CORE_DIR" + + curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar + php wp-cli.phar core config --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PASS --dbhost=$DB_HOST --dbprefix=wptests_ + php wp-cli.phar core install --url="$WP_SITE_URL" --title="Example" --admin_user=admin --admin_password=password --admin_email=info@example.com --path=$WP_CORE_DIR --skip-email + + # Install Gutenberg + php wp-cli.phar plugin install gutenberg --activate + + # Install WooCommerce + cd "wp-content/plugins/" + # As zip file does not include tests, we have to get it from git repo. + git clone --depth 1 https://github.com/woocommerce/woocommerce.git + cd "$WP_CORE_DIR" + php wp-cli.phar plugin activate woocommerce + + if [ "$BRANCH" != "" ]; then + # Install the correct branch of the plugin, if running from Travis CI. + php wp-cli.phar plugin install https://github.com/$REPO/archive/$BRANCH.zip --activate + fi + + # Back to original dir + cd "$WORKING_DIR" +} + +install_wp +install_test_suite +install_db +install_deps diff --git a/plugins/woocommerce-blocks/tests/bin/phpunit.sh b/plugins/woocommerce-blocks/tests/bin/phpunit.sh new file mode 100755 index 00000000000..bf080e618ff --- /dev/null +++ b/plugins/woocommerce-blocks/tests/bin/phpunit.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +WORKING_DIR="$PWD" +cd "$WP_CORE_DIR/wp-content/plugins/woocommerce-gutenberg-products-block/" +phpunit --version +phpunit -c phpunit.xml.dist +TEST_RESULT=$? +cd "$WORKING_DIR" +exit $TEST_RESULT diff --git a/plugins/woocommerce-blocks/tests/bootstrap.php b/plugins/woocommerce-blocks/tests/bootstrap.php new file mode 100644 index 00000000000..524cfa923c4 --- /dev/null +++ b/plugins/woocommerce-blocks/tests/bootstrap.php @@ -0,0 +1,104 @@ +reinit(); + } else { + $GLOBALS['wp_roles'] = null; // WPCS: override ok. + wp_roles(); + } + + echo esc_html( 'Loaded WooCommerce Gutenberg Products Block plugin' . PHP_EOL ); +} + +/** + * Adds WooCommerce testing framework classes. + */ +function wgpb_test_includes() { + $wc_tests_framework_base_dir = wc_dir() . '/tests/'; + + // WooCommerce test classes. + // Framework. + require_once $wc_tests_framework_base_dir . '/framework/class-wc-unit-test-factory.php'; + require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-session-handler.php'; + require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-wc-data.php'; + require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-wc-object-query.php'; + require_once $wc_tests_framework_base_dir . '/framework/class-wc-mock-payment-gateway.php'; + require_once $wc_tests_framework_base_dir . '/framework/class-wc-payment-token-stub.php'; + require_once $wc_tests_framework_base_dir . '/framework/vendor/class-wp-test-spy-rest-server.php'; + + // Test cases. + require_once $wc_tests_framework_base_dir . '/includes/wp-http-testcase.php'; + require_once $wc_tests_framework_base_dir . '/framework/class-wc-unit-test-case.php'; + require_once $wc_tests_framework_base_dir . '/framework/class-wc-api-unit-test-case.php'; + require_once $wc_tests_framework_base_dir . '/framework/class-wc-rest-unit-test-case.php'; + + // Helpers. + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-product.php'; + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-coupon.php'; + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-fee.php'; + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-shipping.php'; + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-customer.php'; + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-order.php'; + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-shipping-zones.php'; + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-payment-token.php'; + require_once $wc_tests_framework_base_dir . '/framework/helpers/class-wc-helper-settings.php'; +} + +/** + * Manually load the plugin being tested. + */ +function _manually_load_plugin() { + echo esc_html( 'Loading Gutenberg' . PHP_EOL ); + require dirname( dirname( dirname( __FILE__ ) ) ) . '/gutenberg/gutenberg.php'; + + echo esc_html( 'Loading WooCommerce' . PHP_EOL ); + define( 'WC_TAX_ROUNDING_MODE', 'auto' ); + define( 'WC_USE_TRANSACTIONS', false ); + require_once wc_dir() . '/woocommerce.php'; + + echo esc_html( 'Installing WooCommerce Gutenberg Products Block plugin' . PHP_EOL ); + wgpb_admin_install(); +} +tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); + +// Start up the WP testing environment. +require $_tests_dir . '/includes/bootstrap.php'; + +wgpb_test_includes(); diff --git a/plugins/woocommerce-blocks/woocommerce-gutenberg-products-block.php b/plugins/woocommerce-blocks/woocommerce-gutenberg-products-block.php index 1c50bb36e95..37f7471ca84 100644 --- a/plugins/woocommerce-blocks/woocommerce-gutenberg-products-block.php +++ b/plugins/woocommerce-blocks/woocommerce-gutenberg-products-block.php @@ -26,7 +26,6 @@ function wgpb_initialize() { if ( $files_exist && function_exists( 'register_block_type' ) ) { add_action( 'init', 'wgpb_register_products_block' ); - add_action( 'rest_api_init', 'wgpb_register_api_routes' ); add_action( 'enqueue_block_editor_assets', 'wgpb_extra_gutenberg_scripts' ); } @@ -34,6 +33,7 @@ function wgpb_initialize() { add_action( 'admin_notices', 'wgpb_plugins_notice' ); } + add_action( 'rest_api_init', 'wgpb_register_api_routes' ); } add_action( 'woocommerce_loaded', 'wgpb_initialize' );