From 70dae5674ea2e1667eab450476b61e7b221ee09c Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Wed, 22 Jan 2020 15:03:29 +0530 Subject: [PATCH 01/35] Setup Theme PHPUnit tests --- .phpcs.xml.dist | 49 +++++++++++++ .travis.yml | 65 +++++++++++++++++ README.md | 9 +++ bin/install-wp-tests.sh | 155 ++++++++++++++++++++++++++++++++++++++++ phpcs.xml | 11 +++ phpunit.xml.dist | 16 +++++ tests/bootstrap.php | 48 +++++++++++++ tests/test-sample.php | 20 ++++++ 8 files changed, 373 insertions(+) create mode 100644 .phpcs.xml.dist create mode 100644 .travis.yml create mode 100755 bin/install-wp-tests.sh create mode 100644 phpunit.xml.dist create mode 100644 tests/bootstrap.php create mode 100644 tests/test-sample.php diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist new file mode 100644 index 0000000..067d574 --- /dev/null +++ b/.phpcs.xml.dist @@ -0,0 +1,49 @@ + + + Generally-applicable sniffs for WordPress plugins. + + + . + /vendor/ + /node_modules/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ec9429e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,65 @@ +sudo: false +dist: trusty + +language: php + +notifications: + email: + on_success: never + on_failure: change + +branches: + only: + - master + +cache: + directories: + - $HOME/.composer/cache + +matrix: + include: + - php: 7.2 + env: WP_VERSION=latest + - 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 + dist: precise + +before_script: + - export PATH="$HOME/.composer/vendor/bin:$PATH" + - | + if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then + phpenv config-rm xdebug.ini + else + echo "xdebug.ini does not exist" + fi + - | + if [[ ! -z "$WP_VERSION" ]] ; then + bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION + composer global require "phpunit/phpunit=4.8.*|5.7.*" + 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: + - | + if [[ ! -z "$WP_VERSION" ]] ; then + phpunit + WP_MULTISITE=1 phpunit + fi + - | + if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then + phpcs + fi diff --git a/README.md b/README.md index 6e4150e..e4bc561 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,15 @@ npm run precommit - Sync complete `build` directory on server. - Before code push to repository, make sure you lint your code using `npm run precommit` command. +### Unit testing + +- Setup local unit test environment by running script from terminal + +```./bin/install-wp-tests.sh [db-host] [wp-version] [skip-database-creation]``` +- Execute `phpunit` in terminal from repository to run all test cases. +- Execute `phpunit ./tests/inc/test-class.php` in terminal with file path to run specific tests. + + Good luck! Does this interest you? diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh new file mode 100755 index 0000000..5ceac4b --- /dev/null +++ b/bin/install-wp-tests.sh @@ -0,0 +1,155 @@ +#!/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} + +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]+\-(beta|RC)[0-9]+$ ]]; then + WP_BRANCH=${WP_VERSION%\-*} + WP_TESTS_TAG="branches/$WP_BRANCH" + +elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then + WP_TESTS_TAG="branches/$WP_VERSION" +elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + WP_TESTS_TAG="tags/${WP_VERSION%??}" + else + WP_TESTS_TAG="tags/$WP_VERSION" + fi +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 $TMPDIR/wordpress-nightly + download https://wordpress.org/nightly-builds/wordpress-latest.zip $TMPDIR/wordpress-nightly/wordpress-nightly.zip + unzip -q $TMPDIR/wordpress-nightly/wordpress-nightly.zip -d $TMPDIR/wordpress-nightly/ + mv $TMPDIR/wordpress-nightly/wordpress/* $WP_CORE_DIR + else + if [ $WP_VERSION == 'latest' ]; then + local ARCHIVE_NAME='latest' + elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then + # https serves multiple offers, whereas http serves single. + download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json + if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then + # version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x + LATEST_VERSION=${WP_VERSION%??} + else + # otherwise, scan the releases and get the most up to date minor version of the major release + local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'` + LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1) + fi + if [[ -z "$LATEST_VERSION" ]]; then + local ARCHIVE_NAME="wordpress-$WP_VERSION" + else + local ARCHIVE_NAME="wordpress-$LATEST_VERSION" + fi + else + local ARCHIVE_NAME="wordpress-$WP_VERSION" + fi + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz + tar --strip-components=1 -zxmf $TMPDIR/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/phpcs.xml b/phpcs.xml index 5c42785..372ce1c 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -45,6 +45,7 @@ + @@ -53,6 +54,10 @@ + + + + + tests/* diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..16a3902 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,16 @@ + + + + + ./tests/ + ./tests/test-sample.php + + + diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..39b2b7d --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,48 @@ +assertTrue( true ); + } +} From 9a3d459698f3b4f7c47cef1d5bacc224807cf62d Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Wed, 22 Jan 2020 21:31:47 +0530 Subject: [PATCH 02/35] Add PHPUnit tests for classes --- tests/test-base-blank-theme.php | 45 +++++++++++++++++ tests/test-class-assets.php | 83 ++++++++++++++++++++++++++++++++ tests/test-class-blank-theme.php | 75 +++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 tests/test-base-blank-theme.php create mode 100644 tests/test-class-assets.php create mode 100644 tests/test-class-blank-theme.php diff --git a/tests/test-base-blank-theme.php b/tests/test-base-blank-theme.php new file mode 100644 index 0000000..45c6c16 --- /dev/null +++ b/tests/test-base-blank-theme.php @@ -0,0 +1,45 @@ +assertFalse( wp_script_is( 'jquery' ) ); + + do_action( 'wp_enqueue_scripts' ); + $this->assertTrue( wp_script_is( 'jquery' ) ); + + } // end testjQueryIsLoaded + + public function test_active_theme() { + + + $this->assertTrue( wp_get_theme() == 'blank-theme' ); + } // end testThemeInitialization + + public function test_inactive_theme() { + + $this->assertFalse( wp_get_theme() == 'Twenty Eleven' ); + + } // end testInactiveTheme +} diff --git a/tests/test-class-assets.php b/tests/test-class-assets.php new file mode 100644 index 0000000..c417221 --- /dev/null +++ b/tests/test-class-assets.php @@ -0,0 +1,83 @@ + + * + * @package Blank_Theme + */ + +namespace BLANK_THEME\Tests; + +use Exception; +use BLANK_THEME\Inc\Assets; + +/** + * Class Test_Assets + * + * @coversDefaultClass \BLANK_THEME\Inc\Assets + */ +class Test_Assets extends \WP_UnitTestCase { + /** + * This Assets data member will contain Assets class object. + * + * @var BLANK_THEME\Inc\Assets + */ + protected $instance = false; + + /** + * This function activate the theme. + * + * @return void + */ + public function setUp() : void { + + parent::setUp(); + $this->instance = Assets::get_instance(); + switch_theme( 'blank-theme' ); + update_option( 'thread_comments', 1 ); + add_filter( 'comments_open', array( $this, 'open_comments' ) ); + } + + /** + * Filter to open comments for the post. + * + * @param bool $open Whether comments for the post are open. + * + * @return bool comments for the post are open or not. + */ + public function open_comments( $open ) { + return true; + } + + /** + * Function to test hooks setup. + */ + public function test_setup_hooks() { + $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $this->instance, 'register_scripts' ) ) ); + $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $this->instance, 'register_styles' ) ) ); + } + + /** + * Function to test scripts registration. + */ + public function test_register_scripts() { + + $this->assertFalse( wp_script_is( 'blank-theme-main' ) ); + $this->assertFalse( wp_style_is( 'blank-theme-main' ) ); + + $this->assertFalse( wp_script_is( 'blank-theme-home' ) ); + $this->assertFalse( wp_style_is( 'blank-theme-home' ) ); + + $this->assertFalse( wp_script_is( 'blank-theme-single' ) ); + $this->assertFalse( wp_style_is( 'blank-theme-single' ) ); + + $this->assertFalse( wp_script_is( 'comment-reply' ) ); + $this->assertFalse( wp_style_is( 'comment-reply' ) ); + + do_action( 'wp_enqueue_scripts' ); + + $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); + $this->assertTrue( wp_style_is( 'blank-theme-main' ) ); + } +} diff --git a/tests/test-class-blank-theme.php b/tests/test-class-blank-theme.php new file mode 100644 index 0000000..3b0fa16 --- /dev/null +++ b/tests/test-class-blank-theme.php @@ -0,0 +1,75 @@ + + * + * @package Blank_Theme + */ + +namespace BLANK_THEME\Tests; + +use Exception; +use BLANK_THEME\Inc\BLANK_THEME; + +/** + * Class Test_Blank_Theme + * + * @coversDefaultClass \BLANK_THEME\Inc\BLANK_THEME + */ +class Test_Blank_Theme extends \WP_UnitTestCase { + /** + * This Assets data member will contain Assets class object. + * + * @var BLANK_THEME\Inc\BLANK_THEME + */ + protected $instance = false; + + /** + * This function activate the theme. + * + * @return void + */ + public function setUp() : void { + + parent::setUp(); + $this->instance = BLANK_THEME::get_instance(); + switch_theme( 'blank-theme' ); + } + + /** + * Test constructor function. + */ + public function test_construct() { + $this->assertInstanceOf( 'BLANK_THEME\Inc\BLANK_THEME', $this->instance ); + } + + /** + * Function to test hooks setup. + */ + public function test_setup_hooks() { + $this->assertEquals( 10, has_filter( 'excerpt_more', array( $this->instance, 'add_read_more_link' ) ) ); + $this->assertEquals( 10, has_filter( 'body_class', array( $this->instance, 'filter_body_classes' ) ) ); + $this->assertEquals( 10, has_action( 'wp_head', array( $this->instance, 'add_pingback_link' ) ) ); + } + + /** + * Test function setup theme + */ + public function test_setup_theme() { + + $this->assertTrue( get_theme_support( 'automatic-feed-links' ) ); + $this->assertTrue( get_theme_support( 'title-tag' ) ); + $this->assertTrue( get_theme_support( 'post-thumbnails' ) ); + $this->assertTrue( get_theme_support( 'customize-selective-refresh-widgets' ) ); + $this->assertTrue( get_theme_support( 'jetpack-responsive-videos' ) ); + $this->assertTrue( get_theme_support( 'wp-block-styles' ) ); + $this->assertTrue( get_theme_support( 'align-wide' ) ); + + // @TODO: check why tests are failing for html5. + //$this->assertIsArray( get_theme_support( 'html5' ) ); + $this->assertIsArray( get_theme_support( 'post-formats' ) ); + $this->assertIsArray( get_theme_support( 'custom-background' ) ); + $this->assertIsArray( get_theme_support( 'custom-logo' ) ); + } +} From 3c938c3ca3568ce160976439c4199b40ec820d93 Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Thu, 23 Jan 2020 19:33:35 +0530 Subject: [PATCH 03/35] Write test cases for Classes and add code coverage annotations --- phpunit.xml.dist | 10 +++- tests/test-class-assets.php | 17 ++---- tests/test-class-blank-theme.php | 58 ++++++++++++++++++- tests/test-class-customizer.php | 95 ++++++++++++++++++++++++++++++++ tests/test-class-widgets.php | 57 +++++++++++++++++++ 5 files changed, 221 insertions(+), 16 deletions(-) create mode 100644 tests/test-class-customizer.php create mode 100644 tests/test-class-widgets.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 16a3902..d53e9d2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,9 +8,17 @@ convertWarningsToExceptions="true" > - + ./tests/ ./tests/test-sample.php + + + ./inc/classes/ + + + + + diff --git a/tests/test-class-assets.php b/tests/test-class-assets.php index c417221..3de3744 100644 --- a/tests/test-class-assets.php +++ b/tests/test-class-assets.php @@ -52,6 +52,8 @@ public function open_comments( $open ) { /** * Function to test hooks setup. + * + * @covers ::_setup_hooks */ public function test_setup_hooks() { $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $this->instance, 'register_scripts' ) ) ); @@ -60,21 +62,12 @@ public function test_setup_hooks() { /** * Function to test scripts registration. + * + * @covers ::register_scripts + * @covers ::register_styles */ public function test_register_scripts() { - $this->assertFalse( wp_script_is( 'blank-theme-main' ) ); - $this->assertFalse( wp_style_is( 'blank-theme-main' ) ); - - $this->assertFalse( wp_script_is( 'blank-theme-home' ) ); - $this->assertFalse( wp_style_is( 'blank-theme-home' ) ); - - $this->assertFalse( wp_script_is( 'blank-theme-single' ) ); - $this->assertFalse( wp_style_is( 'blank-theme-single' ) ); - - $this->assertFalse( wp_script_is( 'comment-reply' ) ); - $this->assertFalse( wp_style_is( 'comment-reply' ) ); - do_action( 'wp_enqueue_scripts' ); $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); diff --git a/tests/test-class-blank-theme.php b/tests/test-class-blank-theme.php index 3b0fa16..96c0f36 100644 --- a/tests/test-class-blank-theme.php +++ b/tests/test-class-blank-theme.php @@ -21,7 +21,7 @@ class Test_Blank_Theme extends \WP_UnitTestCase { /** * This Assets data member will contain Assets class object. * - * @var BLANK_THEME\Inc\BLANK_THEME + * @var \BLANK_THEME\Inc\BLANK_THEME */ protected $instance = false; @@ -33,12 +33,14 @@ class Test_Blank_Theme extends \WP_UnitTestCase { public function setUp() : void { parent::setUp(); - $this->instance = BLANK_THEME::get_instance(); switch_theme( 'blank-theme' ); + $this->instance = BLANK_THEME::get_instance(); } /** * Test constructor function. + * + * @covers ::__construct */ public function test_construct() { $this->assertInstanceOf( 'BLANK_THEME\Inc\BLANK_THEME', $this->instance ); @@ -46,6 +48,8 @@ public function test_construct() { /** * Function to test hooks setup. + * + * @covers ::_setup_hooks */ public function test_setup_hooks() { $this->assertEquals( 10, has_filter( 'excerpt_more', array( $this->instance, 'add_read_more_link' ) ) ); @@ -55,9 +59,13 @@ public function test_setup_hooks() { /** * Test function setup theme + * + * @covers ::_setup_theme */ public function test_setup_theme() { + do_action( 'after_setup_theme' ); + $this->assertTrue( get_theme_support( 'automatic-feed-links' ) ); $this->assertTrue( get_theme_support( 'title-tag' ) ); $this->assertTrue( get_theme_support( 'post-thumbnails' ) ); @@ -67,9 +75,53 @@ public function test_setup_theme() { $this->assertTrue( get_theme_support( 'align-wide' ) ); // @TODO: check why tests are failing for html5. - //$this->assertIsArray( get_theme_support( 'html5' ) ); + //$this->assertIsArray( get_theme_support( 'html5' ) ); $this->assertIsArray( get_theme_support( 'post-formats' ) ); $this->assertIsArray( get_theme_support( 'custom-background' ) ); $this->assertIsArray( get_theme_support( 'custom-logo' ) ); + + $this->assertArrayHasKey( 'primary', get_registered_nav_menus(), 'Primary menu registered' ); + + } + + /** + * Test add read more link. + * + * @covers ::add_read_more_link + */ + public function test_add_read_more_link() { + global $post; + + $post_id = $this->factory()->post->create( array( 'post_title' => 'Test Post' ) ); + $post = get_post( $post_id ); + + $read_more_link = sprintf( '%s', get_permalink( $post->ID ), esc_html__( 'Read More', 'blank-theme' ) ); + + $this->assertEquals( $read_more_link, $this->instance->add_read_more_link() ); + } + + /** + * Test function to add custom body classes. + * + * @covers ::filter_body_classes + */ + public function test_filter_body_classes() { + $this->assertContains( 'test-class', $this->instance->filter_body_classes( array( 'test-class' ) ) ); + } + + /** + * Test function to add pingback link. + * + * @covers ::add_pingback_link + */ + public function test_add_pingback_link() { + $expected = ''; + + if ( is_singular() && pings_open() ) { + $expected = ''; + } + + $this->expectOutputString( $expected ); + $this->instance->add_pingback_link(); } } diff --git a/tests/test-class-customizer.php b/tests/test-class-customizer.php new file mode 100644 index 0000000..600e8ea --- /dev/null +++ b/tests/test-class-customizer.php @@ -0,0 +1,95 @@ + + * + * @package Blank_Theme + */ + +namespace BLANK_THEME\Tests; + +use Exception; +use BLANK_THEME\Inc\Customizer; + +/** + * Class Test_Customizer + * + * @coversDefaultClass \Blank_Theme\Inc\Customizer + */ +class Test_Customizer extends \WP_UnitTestCase { + /** + * This Assets data member will contain Assets class object. + * + * @var BLANK_THEME\Inc\Customizer + */ + protected $instance = false; + + /** + * This function activate the theme. + * + * @return void + */ + public function setUp(): void { + + parent::setUp(); + switch_theme( 'blank-theme' ); + $this->instance = Customizer::get_instance(); + } + + /** + * Test constructor function. + * + * @covers ::__construct + */ + public function test_construct() { + $this->assertInstanceOf( 'BLANK_THEME\Inc\Customizer', $this->instance ); + } + + /** + * Function to test hooks setup. + * + * @covers ::_setup_hooks + */ + public function test_setup_hooks() { + $this->assertEquals( 10, has_action( 'customize_register', array( $this->instance, 'customize_register' ) ) ); + $this->assertEquals( 10, has_action( 'customize_preview_init', array( $this->instance, 'customize_preview_init' ) ) ); + } + + /** + * Test customize partial blog name. + * + * @covers ::customize_partial_blog_name + */ + public function test_customize_partial_blog_name() { + $bloginfo = get_bloginfo( 'name' ); + + $this->expectOutputString( $bloginfo ); + $this->instance->customize_partial_blog_name(); + } + + /** + * Test customize partial blog decription. + * + * @covers ::customize_partial_blog_description + */ + public function test_partial_blog_description() { + $blogdescription = get_bloginfo( 'description' ); + + $this->expectOutputString( $blogdescription ); + $this->instance->customize_partial_blog_description(); + } + + /** + * Test customizer scripts. + * + * @covers ::enqueue_customizer_scripts + */ + public function test_enqueue_customizer_scripts() { + $this->assertFalse( wp_script_is( 'blank-theme-customizer' ) ); + + do_action( 'wp_enqueue_scripts' ); + + $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); + } +} diff --git a/tests/test-class-widgets.php b/tests/test-class-widgets.php new file mode 100644 index 0000000..c1ec0ad --- /dev/null +++ b/tests/test-class-widgets.php @@ -0,0 +1,57 @@ + + * + * @package Blank_Theme + */ + +namespace BLANK_THEME\Tests; + +use Exception; +use BLANK_THEME\Inc\Widgets; + +/** + * Class Test_Customizer + * + * @coversDefaultClass \BLANK_THEME\Inc\Widgets + */ +class Test_Widgets extends \WP_UnitTestCase { + /** + * This Assets data member will contain Assets class object. + * + * @var BLANK_THEME\Inc\Widgets + */ + protected $instance = false; + + /** + * This function activate the theme. + * + * @return void + */ + public function setUp(): void { + + parent::setUp(); + switch_theme( 'blank-theme' ); + $this->instance = Widgets::get_instance(); + } + + /** + * Test constructor function. + * + * @covers ::__construct + */ + public function test_construct() { + $this->assertInstanceOf( 'BLANK_THEME\Inc\Widgets', $this->instance ); + } + + /** + * Function to test hooks setup. + * + * @covers ::_setup_hooks + */ + public function test_setup_hooks() { + $this->assertEquals( 10, has_action( 'widgets_init', array( $this->instance, 'register_widgets' ) ) ); + } +} From ff3f0a9b4511f98e7bb72b9fb1bcd4150747840b Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Fri, 24 Jan 2020 13:17:24 +0530 Subject: [PATCH 04/35] Fix issues with theme setup --- inc/classes/class-blank-theme.php | 9 +++------ inc/classes/class-infinite-scroll.php | 6 ++++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/inc/classes/class-blank-theme.php b/inc/classes/class-blank-theme.php index 090deac..f41e7c7 100644 --- a/inc/classes/class-blank-theme.php +++ b/inc/classes/class-blank-theme.php @@ -27,7 +27,6 @@ protected function __construct() { Widgets::get_instance(); $this->_setup_hooks(); - $this->_setup_theme(); } @@ -48,6 +47,7 @@ protected function _setup_hooks() { * Actions */ add_action( 'wp_head', [ $this, 'add_pingback_link' ] ); + add_action( 'after_setup_theme', [ $this, 'setup_theme' ] ); } @@ -56,7 +56,7 @@ protected function _setup_hooks() { * * @return void */ - protected function _setup_theme() { + public function setup_theme() { load_theme_textdomain( 'blank-theme', BLANK_THEME_TEMP_DIR . '/languages' ); @@ -122,9 +122,6 @@ protected function _setup_theme() { ] ); - if ( ! isset( $content_width ) ) { - $content_width = 900; - } } /** @@ -172,7 +169,7 @@ public function filter_body_classes( $classes ) { */ public function add_pingback_link() { if ( is_singular() && pings_open() ) { - echo ''; + echo ''; } } diff --git a/inc/classes/class-infinite-scroll.php b/inc/classes/class-infinite-scroll.php index 8e08a2b..e6e3da1 100644 --- a/inc/classes/class-infinite-scroll.php +++ b/inc/classes/class-infinite-scroll.php @@ -5,12 +5,14 @@ * @package Blank-Theme */ -namespace Blank_Theme; +namespace BLANK_THEME\Inc; + +use Blank_Theme\Inc\Traits\Singleton; /** * Class Infinite_Scroll */ -class Infinite_Scroll extends Base { +class Infinite_Scroll { /** * Setup Jetpack for infinite theme support. From 1fd953a52d9ce2e49af07c7e326dcb75fc36bfde Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Fri, 24 Jan 2020 18:46:25 +0530 Subject: [PATCH 05/35] Update tests --- tests/test-base-blank-theme.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/test-base-blank-theme.php b/tests/test-base-blank-theme.php index 45c6c16..8c38a58 100644 --- a/tests/test-base-blank-theme.php +++ b/tests/test-base-blank-theme.php @@ -13,15 +13,17 @@ */ class Test_Base_Blank_Theme extends WP_UnitTestCase { - public function setUp() { + /** + * Setup theme for test. + */ + public function setUp() : void { parent::setUp(); switch_theme( 'blank-theme' ); } - public function tearDown() { - parent::tearDown(); - } - + /** + * Test if jQuery is loaded. + */ public function test_jquery_is_loaded() { $this->assertFalse( wp_script_is( 'jquery' ) ); @@ -31,15 +33,17 @@ public function test_jquery_is_loaded() { } // end testjQueryIsLoaded + /** + * Test if theme is active. + */ public function test_active_theme() { - - - $this->assertTrue( wp_get_theme() == 'blank-theme' ); + $this->assertTrue( wp_get_theme()->get( 'Name' ) === 'blank-theme' ); } // end testThemeInitialization + /** + * Test random bundled theme is inactive. + */ public function test_inactive_theme() { - - $this->assertFalse( wp_get_theme() == 'Twenty Eleven' ); - + $this->assertFalse( wp_get_theme()->get( 'Name' ) === 'twentytwenty' ); } // end testInactiveTheme } From 90748ea974f918391d34f3b0bd1d81cf52e55219 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Fri, 24 Jan 2020 18:49:48 +0530 Subject: [PATCH 06/35] Add phpunit GH action --- .github/workflows/phpunit_on_pull_request.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .github/workflows/phpunit_on_pull_request.yml diff --git a/.github/workflows/phpunit_on_pull_request.yml b/.github/workflows/phpunit_on_pull_request.yml new file mode 100644 index 0000000..473d8f9 --- /dev/null +++ b/.github/workflows/phpunit_on_pull_request.yml @@ -0,0 +1,12 @@ +on: pull_request +name: PHPUnit +jobs: + runPHPCSInspection: + name: Run PHPUnit test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.sha }} + - name: Run PHPUnit test + uses: docker://rtcamp/action-run-phpunit:v1.0.0 From 62b07594a234214708a9e155f16dcd02572d8435 Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Fri, 24 Jan 2020 21:07:52 +0530 Subject: [PATCH 07/35] Move title-tag support to init action --- inc/classes/class-blank-theme.php | 11 ++++++++++- tests/test-class-blank-theme.php | 5 ++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/inc/classes/class-blank-theme.php b/inc/classes/class-blank-theme.php index f41e7c7..18701a8 100644 --- a/inc/classes/class-blank-theme.php +++ b/inc/classes/class-blank-theme.php @@ -48,6 +48,7 @@ protected function _setup_hooks() { */ add_action( 'wp_head', [ $this, 'add_pingback_link' ] ); add_action( 'after_setup_theme', [ $this, 'setup_theme' ] ); + add_action( 'init', [ $this, 'add_title_tag_support' ] ); } @@ -61,7 +62,6 @@ public function setup_theme() { load_theme_textdomain( 'blank-theme', BLANK_THEME_TEMP_DIR . '/languages' ); add_theme_support( 'automatic-feed-links' ); - add_theme_support( 'title-tag' ); add_theme_support( 'post-thumbnails' ); add_theme_support( 'customize-selective-refresh-widgets' ); add_theme_support( 'jetpack-responsive-videos' ); @@ -124,6 +124,15 @@ public function setup_theme() { } + /** + * Function to add Title theme support. + * + * @action init. + */ + public function add_title_tag_support() { + add_theme_support( 'title-tag' ); + } + /** * Add read more link * diff --git a/tests/test-class-blank-theme.php b/tests/test-class-blank-theme.php index 96c0f36..950a5c4 100644 --- a/tests/test-class-blank-theme.php +++ b/tests/test-class-blank-theme.php @@ -60,7 +60,7 @@ public function test_setup_hooks() { /** * Test function setup theme * - * @covers ::_setup_theme + * @covers ::setup_theme */ public function test_setup_theme() { @@ -74,8 +74,7 @@ public function test_setup_theme() { $this->assertTrue( get_theme_support( 'wp-block-styles' ) ); $this->assertTrue( get_theme_support( 'align-wide' ) ); - // @TODO: check why tests are failing for html5. - //$this->assertIsArray( get_theme_support( 'html5' ) ); + $this->assertIsArray( get_theme_support( 'html5' ) ); $this->assertIsArray( get_theme_support( 'post-formats' ) ); $this->assertIsArray( get_theme_support( 'custom-background' ) ); $this->assertIsArray( get_theme_support( 'custom-logo' ) ); From 2f78b2f595f48d5ac765c30a6961ebfb564a4523 Mon Sep 17 00:00:00 2001 From: Kiran Potphode Date: Thu, 30 Jan 2020 12:00:24 +0530 Subject: [PATCH 08/35] Delete unwanted files --- .phpcs.xml.dist | 49 ------------------------------------- .travis.yml | 65 ------------------------------------------------- 2 files changed, 114 deletions(-) delete mode 100644 .phpcs.xml.dist delete mode 100644 .travis.yml diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist deleted file mode 100644 index 067d574..0000000 --- a/.phpcs.xml.dist +++ /dev/null @@ -1,49 +0,0 @@ - - - Generally-applicable sniffs for WordPress plugins. - - - . - /vendor/ - /node_modules/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ec9429e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,65 +0,0 @@ -sudo: false -dist: trusty - -language: php - -notifications: - email: - on_success: never - on_failure: change - -branches: - only: - - master - -cache: - directories: - - $HOME/.composer/cache - -matrix: - include: - - php: 7.2 - env: WP_VERSION=latest - - 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 - dist: precise - -before_script: - - export PATH="$HOME/.composer/vendor/bin:$PATH" - - | - if [ -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini ]; then - phpenv config-rm xdebug.ini - else - echo "xdebug.ini does not exist" - fi - - | - if [[ ! -z "$WP_VERSION" ]] ; then - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - composer global require "phpunit/phpunit=4.8.*|5.7.*" - 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: - - | - if [[ ! -z "$WP_VERSION" ]] ; then - phpunit - WP_MULTISITE=1 phpunit - fi - - | - if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then - phpcs - fi From 8a5e332f8fa84d0a57360f525520a4c06690f424 Mon Sep 17 00:00:00 2001 From: Vaishali Agola Date: Fri, 21 Feb 2020 13:40:14 +0530 Subject: [PATCH 09/35] Add tests folder to skip_folders list --- .github/workflows/phpcs_on_pull_request.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/phpcs_on_pull_request.yml b/.github/workflows/phpcs_on_pull_request.yml index 1bb9451..335cf1a 100644 --- a/.github/workflows/phpcs_on_pull_request.yml +++ b/.github/workflows/phpcs_on_pull_request.yml @@ -11,6 +11,7 @@ jobs: - name: Run PHPCS inspection uses: docker://rtcamp/action-phpcs-code-review:v2.0.0 env: + SKIP_FOLDERS: "tests,.github" VAULT_ADDR: ${{ secrets.VAULT_ADDR }} VAULT_TOKEN: ${{ secrets.VAULT_TOKEN }} with: From bbf8de828630fa755f0c0d6061492c695c8a3e72 Mon Sep 17 00:00:00 2001 From: Vaishali Agola Date: Fri, 21 Feb 2020 13:44:45 +0530 Subject: [PATCH 10/35] Update readme with Contributing guideline --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index e4bc561..97bc575 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,34 @@ npm run precommit - Sync complete `build` directory on server. - Before code push to repository, make sure you lint your code using `npm run precommit` command. +## Contributing + +### Report a Bug + +Before you create a new issue, please search [existing issues](https://github.com/rtCamp/blank-theme/issues) to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version. + +Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/rtCamp/blank-theme/issues/new). Include as much detail as you can, and clear steps to reproduce if possible. + +### Create a pull request + +Want to contribute a new feature? Please first open a new issue to discuss whether the feature is a good fit for the project. + +Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request. + +1. Search existing issues. If you can’t find anything related to what you want to work on, open a new issue. + +1. Fork the repository. + +1. Create a branch from `develop` for each issue you’d like to address. Commit your changes. + +1. Push the code changes from your local clone to your fork. + +1. Open a pull request. + +1. Respond to code review feedback in a timely manner, recognizing development is a collaborative process. + +1. You need at least one approval and Once your pull request has passed code review and tests, it will be merged into `develop` and be in the pipeline for the next release. + ### Unit testing - Setup local unit test environment by running script from terminal From e51e85895f36afcd82880608018539f0fdeff11b Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Fri, 28 Feb 2020 18:55:50 +0530 Subject: [PATCH 11/35] Restructure test files and remove unwanted files. --- tests/{ => inc/classes}/test-class-assets.php | 0 .../classes}/test-class-blank-theme.php | 8 ++++---- .../classes}/test-class-customizer.php | 0 .../{ => inc/classes}/test-class-widgets.php | 1 - tests/test-sample.php | 20 ------------------- 5 files changed, 4 insertions(+), 25 deletions(-) rename tests/{ => inc/classes}/test-class-assets.php (100%) rename tests/{ => inc/classes}/test-class-blank-theme.php (91%) rename tests/{ => inc/classes}/test-class-customizer.php (100%) rename tests/{ => inc/classes}/test-class-widgets.php (98%) delete mode 100644 tests/test-sample.php diff --git a/tests/test-class-assets.php b/tests/inc/classes/test-class-assets.php similarity index 100% rename from tests/test-class-assets.php rename to tests/inc/classes/test-class-assets.php diff --git a/tests/test-class-blank-theme.php b/tests/inc/classes/test-class-blank-theme.php similarity index 91% rename from tests/test-class-blank-theme.php rename to tests/inc/classes/test-class-blank-theme.php index 950a5c4..315df01 100644 --- a/tests/test-class-blank-theme.php +++ b/tests/inc/classes/test-class-blank-theme.php @@ -74,10 +74,10 @@ public function test_setup_theme() { $this->assertTrue( get_theme_support( 'wp-block-styles' ) ); $this->assertTrue( get_theme_support( 'align-wide' ) ); - $this->assertIsArray( get_theme_support( 'html5' ) ); - $this->assertIsArray( get_theme_support( 'post-formats' ) ); - $this->assertIsArray( get_theme_support( 'custom-background' ) ); - $this->assertIsArray( get_theme_support( 'custom-logo' ) ); + $this->assertTrue( is_array( get_theme_support( 'html5' ) ) ); + $this->assertTrue( is_array( get_theme_support( 'post-formats' ) ) ); + $this->assertTrue( is_array( get_theme_support( 'custom-background' ) ) ); + $this->assertTrue( is_array( get_theme_support( 'custom-logo' ) ) ); $this->assertArrayHasKey( 'primary', get_registered_nav_menus(), 'Primary menu registered' ); diff --git a/tests/test-class-customizer.php b/tests/inc/classes/test-class-customizer.php similarity index 100% rename from tests/test-class-customizer.php rename to tests/inc/classes/test-class-customizer.php diff --git a/tests/test-class-widgets.php b/tests/inc/classes/test-class-widgets.php similarity index 98% rename from tests/test-class-widgets.php rename to tests/inc/classes/test-class-widgets.php index c1ec0ad..a762f8c 100644 --- a/tests/test-class-widgets.php +++ b/tests/inc/classes/test-class-widgets.php @@ -9,7 +9,6 @@ namespace BLANK_THEME\Tests; -use Exception; use BLANK_THEME\Inc\Widgets; /** diff --git a/tests/test-sample.php b/tests/test-sample.php deleted file mode 100644 index a62ac22..0000000 --- a/tests/test-sample.php +++ /dev/null @@ -1,20 +0,0 @@ -assertTrue( true ); - } -} From 73a269d8a4ffa8f0d8606c5097e77b0291311f4b Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Fri, 28 Feb 2020 18:56:26 +0530 Subject: [PATCH 12/35] Ignore traits directory from test coverage --- phpunit.xml.dist | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d53e9d2..574f81a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,12 +10,14 @@ ./tests/ - ./tests/test-sample.php - ./inc/classes/ + ./inc/ + + ./inc/traits + From 6a26437d98261498f18b3bce080062bbf27b048a Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Fri, 28 Feb 2020 19:05:40 +0530 Subject: [PATCH 13/35] Add Utility class and load in test environment --- tests/bootstrap.php | 2 + tests/helpers/class-utility.php | 124 ++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 tests/helpers/class-utility.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 39b2b7d..b3c69e9 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -40,6 +40,8 @@ function _register_theme() { add_filter( 'pre_option_stylesheet', function() use ( $current_theme ) { return $current_theme; }); + + require_once dirname( __FILE__ ) . '/helpers/class-utility.php'; } tests_add_filter( 'muplugins_loaded', '_register_theme' ); diff --git a/tests/helpers/class-utility.php b/tests/helpers/class-utility.php new file mode 100644 index 0000000..162f47b --- /dev/null +++ b/tests/helpers/class-utility.php @@ -0,0 +1,124 @@ +getMethod( $method_name ); + $method->setAccessible( true ); + + return $method->invokeArgs( $object, $parameters ); + + } + + /** + * Utility method to get private/protected property of a class/object. + * This is a generic wrapper function to align with relfection class and not to be use directly. + * + * @param mixed $object_or_class_name The object/class whose property is to be accessed. + * @param string $property_name The name of the property to access. + * + * @return mixed Value of the hidden property being accessed. + */ + public static function get_property( $object_or_class_name, $property_name ) { + + $object = null; + + if ( is_object( $object_or_class_name ) ) { + $object = $object_or_class_name; + $class_name = get_class( $object ); + } else { + $class_name = $object_or_class_name; + } + + $o_reflection = new \ReflectionClass( $class_name ); + $property = $o_reflection->getProperty( $property_name ); + $property->setAccessible( true ); + + return $property->getValue( $object ); + + } + + /** + * Utility method to set private/protected property of an object/class. + * This is a generic wrapper function to align with relfection class and not to be use directly. + * + * @param mixed $object_or_class_name The object/class whose property is to be accessed. + * @param string $property_name The name of the property to access. + * @param mixed $property_value The value to be set for the hidden property. + * + * @return mixed Value of the hidden property being accessed. + */ + public static function set_and_get_property( $object_or_class_name, string $property_name, $property_value ) { + + $object = null; + + if ( is_object( $object_or_class_name ) ) { + $object = $object_or_class_name; + $class_name = get_class( $object ); + } else { + $class_name = $object_or_class_name; + } + + $o_reflection = new \ReflectionClass( $class_name ); + $property = $o_reflection->getProperty( $property_name ); + $property->setAccessible( true ); + $property->setValue( $object, $property_value ); + + return $property->getValue( $object ); + + } + + /** + * Utility method to capture output from a function. + * + * @param Callable $callback The callback from which output is to be captured + * @param array $parameters Parameters to be passed to the $callback. + * @return mixed Output from callback + * + * @throws \ErrorException Error exception. + */ + public static function buffer_and_return( $callback, array $parameters = array() ) { + + if ( ! is_callable( $callback ) ) { + throw new \ErrorException( sprintf( '%s::%s() expects first parameter to be a valid callback', __CLASS__, __FUNCTION__ ) ); + } + + ob_start(); + + call_user_func_array( $callback, $parameters ); + + return trim( ob_get_clean() ); + + } + +} From 348580d2faeb28bc3a7b26cc0da4af044c684c49 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Fri, 28 Feb 2020 20:01:41 +0530 Subject: [PATCH 14/35] Fix code coverage for widgets class --- tests/inc/classes/test-class-widgets.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/inc/classes/test-class-widgets.php b/tests/inc/classes/test-class-widgets.php index a762f8c..b3fcfd9 100644 --- a/tests/inc/classes/test-class-widgets.php +++ b/tests/inc/classes/test-class-widgets.php @@ -40,17 +40,27 @@ public function setUp(): void { * Test constructor function. * * @covers ::__construct + * @covers ::_setup_hooks */ public function test_construct() { + + Utility::invoke_method( $this->instance, '__construct' ); + $this->assertInstanceOf( 'BLANK_THEME\Inc\Widgets', $this->instance ); - } - /** - * Function to test hooks setup. - * - * @covers ::_setup_hooks - */ - public function test_setup_hooks() { $this->assertEquals( 10, has_action( 'widgets_init', array( $this->instance, 'register_widgets' ) ) ); + + } + + public function test_register_widgets() { + + $this->instance->register_widgets(); + + $sidebars = wp_get_sidebars_widgets(); + + $this->assertArrayHasKey( 'sidebar-1', $sidebars ); + $this->assertArrayHasKey( 'sidebar-2', $sidebars ); + } + } From 55559dea04c6ee2bddb09840ccd1cdcea3ad65f8 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Fri, 28 Feb 2020 20:31:09 +0530 Subject: [PATCH 15/35] Add test cases for missing methods --- tests/helpers/class-utility.php | 21 ++++++ tests/inc/classes/test-class-assets.php | 95 +++++++++++++++++++++---- 2 files changed, 104 insertions(+), 12 deletions(-) diff --git a/tests/helpers/class-utility.php b/tests/helpers/class-utility.php index 162f47b..2b248cd 100644 --- a/tests/helpers/class-utility.php +++ b/tests/helpers/class-utility.php @@ -121,4 +121,25 @@ public static function buffer_and_return( $callback, array $parameters = array() } + /** + * Utility method to mock global wp query. + * + * @param array $args WP query arguments. + * @param array $conditions wp query conditions. + */ + public static function mock_wp_query( $args, $conditions ) { + + $wp_query = new \WP_Query( $args ); + + foreach ( $conditions as $key => $value ) { + $wp_query->{$key} = $value; + } + + // phpcs:disable + $GLOBALS['wp_query'] = $wp_query; + $GLOBALS['wp_the_query'] = $GLOBALS['wp_query']; + do_action_ref_array( 'pre_get_posts', [ &$GLOBALS['wp_query'] ] ); + // phpcs:enable + } + } diff --git a/tests/inc/classes/test-class-assets.php b/tests/inc/classes/test-class-assets.php index 3de3744..7c81ae7 100644 --- a/tests/inc/classes/test-class-assets.php +++ b/tests/inc/classes/test-class-assets.php @@ -36,28 +36,22 @@ public function setUp() : void { $this->instance = Assets::get_instance(); switch_theme( 'blank-theme' ); update_option( 'thread_comments', 1 ); - add_filter( 'comments_open', array( $this, 'open_comments' ) ); - } - - /** - * Filter to open comments for the post. - * - * @param bool $open Whether comments for the post are open. - * - * @return bool comments for the post are open or not. - */ - public function open_comments( $open ) { - return true; + add_filter( 'comments_open', '__return_true' ); } /** * Function to test hooks setup. * * @covers ::_setup_hooks + * @covers ::__construct */ public function test_setup_hooks() { + + Utility::invoke_method( $this->instance, '__construct' ); + $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $this->instance, 'register_scripts' ) ) ); $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $this->instance, 'register_styles' ) ) ); + } /** @@ -70,7 +64,84 @@ public function test_register_scripts() { do_action( 'wp_enqueue_scripts' ); + Utility::mock_wp_query( + [], + [ + 'is_singular' => true, + ] + ); + $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); $this->assertTrue( wp_style_is( 'blank-theme-main' ) ); + $this->assertTrue( wp_script_is( 'comment-reply' ) ); + } + + /** + * Function to test scripts registration. + * + * @covers ::register_scripts + * @covers ::register_styles + */ + public function test_register_scripts_is_home() { + + Utility::mock_wp_query( + [], + [ + 'is_home' => true, + ] + ); + do_action( 'wp_enqueue_scripts' ); + + $this->assertTrue( wp_script_is( 'blank-theme-home' ) ); + $this->assertTrue( wp_style_is( 'blank-theme-home' ) ); + } + + /** + * Function to test scripts registration. + * + * @covers ::register_scripts + * @covers ::register_styles + */ + public function test_register_scripts_is_single() { + + Utility::mock_wp_query( + [], + [ + 'is_single' => true, + ] + ); + do_action( 'wp_enqueue_scripts' ); + + $this->assertTrue( wp_script_is( 'blank-theme-single' ) ); + $this->assertTrue( wp_style_is( 'blank-theme-single' ) ); + } + + /** + * @covers ::get_asset_file_path + */ + public function test_get_asset_file_path() { + $asset_path = [ + 'test' => 'test1', + ]; + + Utility::set_and_get_property( $this->instance, 'asset_paths', $asset_path ); + + $expected_path = untrailingslashit( get_template_directory() ) . '/assets/build/test1'; + $this->assertEquals( $expected_path, $this->instance->get_asset_file_path( 'test' ) ); + } + + /** + * @covers ::get_asset_file_uri + */ + public function test_get_asset_file_uri() { + + $asset_path = [ + 'test' => 'test1', + ]; + + Utility::set_and_get_property( $this->instance, 'asset_paths', $asset_path ); + + $expected_path = untrailingslashit( get_template_directory_uri() ) . '/assets/build/test1'; + $this->assertEquals( $expected_path, $this->instance->get_asset_file_uri( 'test' ) ); } } From 00aa1552b96efb7497b52a02b38cd6f7f7e4c0b4 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Mon, 2 Mar 2020 20:49:47 +0530 Subject: [PATCH 16/35] Fix class-assets code coverage --- inc/classes/class-assets.php | 2 ++ tests/inc/classes/test-class-assets.php | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/inc/classes/class-assets.php b/inc/classes/class-assets.php index 56cd84c..5cf221c 100644 --- a/inc/classes/class-assets.php +++ b/inc/classes/class-assets.php @@ -104,6 +104,8 @@ public function register_styles() { * Reads 'manifest.json' file generated by the build process. * * @return bool|void + * + * @codeCoverageIgnore Ignoring because not able to mock response of wp_remote_get. */ public function get_asset_paths() { diff --git a/tests/inc/classes/test-class-assets.php b/tests/inc/classes/test-class-assets.php index 7c81ae7..a136458 100644 --- a/tests/inc/classes/test-class-assets.php +++ b/tests/inc/classes/test-class-assets.php @@ -37,6 +37,12 @@ public function setUp() : void { switch_theme( 'blank-theme' ); update_option( 'thread_comments', 1 ); add_filter( 'comments_open', '__return_true' ); + $this->mock_post = $this->factory()->post->create_and_get(); + $GLOBALS['post'] = $this->mock_post; + } + + public function tearDown() { + unset( $GLOBALS['post'] ); } /** @@ -63,17 +69,8 @@ public function test_setup_hooks() { public function test_register_scripts() { do_action( 'wp_enqueue_scripts' ); - - Utility::mock_wp_query( - [], - [ - 'is_singular' => true, - ] - ); - $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); $this->assertTrue( wp_style_is( 'blank-theme-main' ) ); - $this->assertTrue( wp_script_is( 'comment-reply' ) ); } /** @@ -83,7 +80,7 @@ public function test_register_scripts() { * @covers ::register_styles */ public function test_register_scripts_is_home() { - + $old_wp_query = $GLOBALS['wp_query']; Utility::mock_wp_query( [], [ @@ -94,6 +91,7 @@ public function test_register_scripts_is_home() { $this->assertTrue( wp_script_is( 'blank-theme-home' ) ); $this->assertTrue( wp_style_is( 'blank-theme-home' ) ); + $GLOBALS['wp_query'] = $old_wp_query; } /** @@ -103,7 +101,7 @@ public function test_register_scripts_is_home() { * @covers ::register_styles */ public function test_register_scripts_is_single() { - + $old_wp_query = $GLOBALS['wp_query']; Utility::mock_wp_query( [], [ @@ -114,6 +112,7 @@ public function test_register_scripts_is_single() { $this->assertTrue( wp_script_is( 'blank-theme-single' ) ); $this->assertTrue( wp_style_is( 'blank-theme-single' ) ); + $GLOBALS['wp_query'] = $old_wp_query; } /** From 631ffb580b928c642aa34f142307aed0d2db461b Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Mon, 2 Mar 2020 20:50:37 +0530 Subject: [PATCH 17/35] Fix class-blank-theme.php unit test code coverage --- inc/classes/class-blank-theme.php | 3 + tests/inc/classes/test-class-blank-theme.php | 104 ++++++++++++++++--- 2 files changed, 92 insertions(+), 15 deletions(-) diff --git a/inc/classes/class-blank-theme.php b/inc/classes/class-blank-theme.php index 18701a8..00d517e 100644 --- a/inc/classes/class-blank-theme.php +++ b/inc/classes/class-blank-theme.php @@ -128,6 +128,9 @@ public function setup_theme() { * Function to add Title theme support. * * @action init. + * + * @codeCoverageIgnore Not able to test this as it throws unexpected incorrect usgae error + * when called in unit test case. */ public function add_title_tag_support() { add_theme_support( 'title-tag' ); diff --git a/tests/inc/classes/test-class-blank-theme.php b/tests/inc/classes/test-class-blank-theme.php index 315df01..03a7ce8 100644 --- a/tests/inc/classes/test-class-blank-theme.php +++ b/tests/inc/classes/test-class-blank-theme.php @@ -35,23 +35,74 @@ public function setUp() : void { parent::setUp(); switch_theme( 'blank-theme' ); $this->instance = BLANK_THEME::get_instance(); + $this->mock_post = $this->factory()->post->create_and_get(); + $GLOBALS['post'] = $this->mock_post; + add_filter( 'is_active_sidebar', [ $this, 'deactivate_sidebar' ], 10, 2 ); + } + + public function tearDown() { + unset( $GLOBALS['post'] ); } /** * Test constructor function. * * @covers ::__construct + * @covers ::_setup_hooks */ public function test_construct() { - $this->assertInstanceOf( 'BLANK_THEME\Inc\BLANK_THEME', $this->instance ); - } - /** - * Function to test hooks setup. - * - * @covers ::_setup_hooks - */ - public function test_setup_hooks() { + Utility::invoke_method( $this->instance, '__construct' ); + $this->assertInstanceOf( 'BLANK_THEME\Inc\BLANK_THEME', $this->instance ); + $hooks = [ + [ + 'type' => 'filter', + 'name' => 'excerpt_more', + 'priority' => 10, + 'function' => 'add_read_more_link', + ], + [ + 'type' => 'filter', + 'name' => 'body_class', + 'priority' => 10, + 'function' => 'filter_body_classes', + ], + [ + 'type' => 'action', + 'name' => 'wp_head', + 'priority' => 10, + 'function' => 'add_pingback_link', + ], + [ + 'type' => 'action', + 'name' => 'after_setup_theme', + 'priority' => 10, + 'function' => 'setup_theme', + ], + [ + 'type' => 'action', + 'name' => 'init', + 'priority' => 10, + 'function' => 'add_title_tag_support', + ], + ]; + + // Check if hooks loaded. + foreach ( $hooks as $hook ) { + + $this->assertEquals( + $hook['priority'], + call_user_func( + sprintf( 'has_%s', $hook['type'] ), + $hook['name'], + [ + $this->instance, + $hook['function'], + ] + ), + sprintf( 'BLANK_THEME::__construct() failed to register %1$s "%2$s" to %3$s()', $hook['type'], $hook['name'], $hook['function'] ) + ); + } $this->assertEquals( 10, has_filter( 'excerpt_more', array( $this->instance, 'add_read_more_link' ) ) ); $this->assertEquals( 10, has_filter( 'body_class', array( $this->instance, 'filter_body_classes' ) ) ); $this->assertEquals( 10, has_action( 'wp_head', array( $this->instance, 'add_pingback_link' ) ) ); @@ -64,7 +115,7 @@ public function test_setup_hooks() { */ public function test_setup_theme() { - do_action( 'after_setup_theme' ); + $this->instance->setup_theme(); $this->assertTrue( get_theme_support( 'automatic-feed-links' ) ); $this->assertTrue( get_theme_support( 'title-tag' ) ); @@ -105,7 +156,18 @@ public function test_add_read_more_link() { * @covers ::filter_body_classes */ public function test_filter_body_classes() { + //error_log( var_export( is_active_sidebar( 'sidebar-1' ), true ) ); $this->assertContains( 'test-class', $this->instance->filter_body_classes( array( 'test-class' ) ) ); + $this->assertContains( 'hfeed', $this->instance->filter_body_classes( array( 'test-class' ) ) ); + $this->assertContains( 'no-sidebar', $this->instance->filter_body_classes( array( 'test-class' ) ) ); + Utility::mock_wp_query( + [], + [ + 'is_singular' => true, + ] + ); + + $this->assertNotContains( 'hfeed', $this->instance->filter_body_classes( array( 'test-class' ) ) ); } /** @@ -114,13 +176,25 @@ public function test_filter_body_classes() { * @covers ::add_pingback_link */ public function test_add_pingback_link() { - $expected = ''; - if ( is_singular() && pings_open() ) { - $expected = ''; - } + add_filter( 'pings_open', '__return_true' ); + $expected = ''; + + Utility::mock_wp_query( + [], + [ + 'is_singular' => true, + ] + ); + $actual = Utility::buffer_and_return( [ $this->instance, 'add_pingback_link' ] ); + $this->assertEquals( $expected, $actual ); + } + + public function deactivate_sidebar( $is_active, $index ) { - $this->expectOutputString( $expected ); - $this->instance->add_pingback_link(); + if ( 'sidebar-1' === $index ) { + return false; + } + return $is_active; } } From b03926263e632306f834355fce90c9ea2a51abfe Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Mon, 2 Mar 2020 20:51:36 +0530 Subject: [PATCH 18/35] Add missing test cases for test-class-customizer.php --- tests/bootstrap.php | 1 + tests/inc/classes/test-class-customizer.php | 73 ++++++++++++++++----- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b3c69e9..0e6819d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -42,6 +42,7 @@ function _register_theme() { }); require_once dirname( __FILE__ ) . '/helpers/class-utility.php'; + require_once ABSPATH . WPINC . '/class-wp-customize-manager.php'; } tests_add_filter( 'muplugins_loaded', '_register_theme' ); diff --git a/tests/inc/classes/test-class-customizer.php b/tests/inc/classes/test-class-customizer.php index 600e8ea..63aae5f 100644 --- a/tests/inc/classes/test-class-customizer.php +++ b/tests/inc/classes/test-class-customizer.php @@ -9,7 +9,6 @@ namespace BLANK_THEME\Tests; -use Exception; use BLANK_THEME\Inc\Customizer; /** @@ -41,19 +40,45 @@ public function setUp(): void { * Test constructor function. * * @covers ::__construct + * @covers ::_setup_hooks */ public function test_construct() { + Utility::invoke_method( $this->instance, '__construct' ); + $this->assertInstanceOf( 'BLANK_THEME\Inc\Customizer', $this->instance ); - } - /** - * Function to test hooks setup. - * - * @covers ::_setup_hooks - */ - public function test_setup_hooks() { - $this->assertEquals( 10, has_action( 'customize_register', array( $this->instance, 'customize_register' ) ) ); - $this->assertEquals( 10, has_action( 'customize_preview_init', array( $this->instance, 'customize_preview_init' ) ) ); + + $hooks = [ + [ + 'type' => 'action', + 'name' => 'customize_register', + 'priority' => 10, + 'function' => 'customize_register', + ], + [ + 'type' => 'action', + 'name' => 'customize_preview_init', + 'priority' => 10, + 'function' => 'customize_preview_init', + ], + ]; + + // Check if hooks loaded. + foreach ( $hooks as $hook ) { + + $this->assertEquals( + $hook['priority'], + call_user_func( + sprintf( 'has_%s', $hook['type'] ), + $hook['name'], + [ + $this->instance, + $hook['function'], + ] + ), + sprintf( 'Customizer::__construct() failed to register %1$s "%2$s" to %3$s()', $hook['type'], $hook['name'], $hook['function'] ) + ); + } } /** @@ -62,10 +87,11 @@ public function test_setup_hooks() { * @covers ::customize_partial_blog_name */ public function test_customize_partial_blog_name() { - $bloginfo = get_bloginfo( 'name' ); + $expected = get_bloginfo( 'name' ); + + $actual = Utility::buffer_and_return( [ $this->instance, 'customize_partial_blog_name' ] ); + $this->assertEquals( $expected, $actual ); - $this->expectOutputString( $bloginfo ); - $this->instance->customize_partial_blog_name(); } /** @@ -74,10 +100,10 @@ public function test_customize_partial_blog_name() { * @covers ::customize_partial_blog_description */ public function test_partial_blog_description() { - $blogdescription = get_bloginfo( 'description' ); + $expected = get_bloginfo( 'description' ); - $this->expectOutputString( $blogdescription ); - $this->instance->customize_partial_blog_description(); + $actual = Utility::buffer_and_return( [ $this->instance, 'customize_partial_blog_description' ] ); + $this->assertEquals( $expected, $actual ); } /** @@ -88,8 +114,21 @@ public function test_partial_blog_description() { public function test_enqueue_customizer_scripts() { $this->assertFalse( wp_script_is( 'blank-theme-customizer' ) ); - do_action( 'wp_enqueue_scripts' ); + $this->instance->enqueue_customizer_scripts(); $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); } + + public function test_customize_register() { + + wp_set_current_user( + $this->factory->user->create( [ 'role' => 'administrator' ] ) + ); + + $wp_customize = new \WP_Customize_Manager(); + + do_action( 'customize_register', $wp_customize ); + + $this->assertEquals( $wp_customize->get_setting( 'blogname' )->transport, 'postMessage' ); + } } From 4e2dfe2246743d15906cf0c238be312916316f33 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Tue, 3 Mar 2020 20:37:00 +0530 Subject: [PATCH 19/35] Fix test cases for infinite scroll class --- .../classes/test-class-infinite-scroll.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/inc/classes/test-class-infinite-scroll.php diff --git a/tests/inc/classes/test-class-infinite-scroll.php b/tests/inc/classes/test-class-infinite-scroll.php new file mode 100644 index 0000000..b15b61a --- /dev/null +++ b/tests/inc/classes/test-class-infinite-scroll.php @@ -0,0 +1,90 @@ +instance = new Infinite_Scroll(); + } + + /** + * @covers ::setup_jetpack + */ + public function test_setup_jetpack() { + $this->instance->setup_jetpack(); + + $expected_data = get_theme_support( 'infinite-scroll' )[0]; + + $this->assertArrayHasKey( 'container', $expected_data ); + $this->assertEquals( 'main', $expected_data['container'] ); + + $this->assertArrayHasKey( 'render', $expected_data ); + + $this->assertArrayHasKey( 'footer', $expected_data ); + $this->assertEquals( 'page', $expected_data['footer'] ); + } + + /** + * @covers ::render_callback + */ + public function test_render_callback() { + + $post_ids = $this->factory->post->create_many( + 10, + [ + 'post_type' => 'post', + ] + ); + + Utility::mock_wp_query( + [ 'post_type' => 'post' ], + [] + ); + $output = Utility::buffer_and_return( [ $this->instance, 'render_callback' ] ); + + $this->assertNotEmpty( $output ); + $this->assertContains( ' 'post', + ], + [ + 'is_search' => true, + ] + ); + + $output = Utility::buffer_and_return( [ $this->instance, 'render_callback' ] ); + + $this->assertNotEmpty( $output ); + $this->assertContains( ' Date: Tue, 3 Mar 2020 20:37:30 +0530 Subject: [PATCH 20/35] Fix assets class test case coverage --- inc/classes/class-assets.php | 4 +- tests/inc/classes/test-class-assets.php | 62 ++++++++++++++++++++----- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/inc/classes/class-assets.php b/inc/classes/class-assets.php index 5cf221c..05f88ac 100644 --- a/inc/classes/class-assets.php +++ b/inc/classes/class-assets.php @@ -71,7 +71,7 @@ public function register_scripts() { } if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { - wp_enqueue_script( 'comment-reply' ); + wp_enqueue_script( 'comment-reply' ); // @codeCoverageIgnore Ignoring becuase trying to mock comments_open() throws error. } } @@ -104,8 +104,6 @@ public function register_styles() { * Reads 'manifest.json' file generated by the build process. * * @return bool|void - * - * @codeCoverageIgnore Ignoring because not able to mock response of wp_remote_get. */ public function get_asset_paths() { diff --git a/tests/inc/classes/test-class-assets.php b/tests/inc/classes/test-class-assets.php index a136458..d678831 100644 --- a/tests/inc/classes/test-class-assets.php +++ b/tests/inc/classes/test-class-assets.php @@ -9,8 +9,8 @@ namespace BLANK_THEME\Tests; -use Exception; use BLANK_THEME\Inc\Assets; +use WP_Error; /** * Class Test_Assets @@ -82,10 +82,10 @@ public function test_register_scripts() { public function test_register_scripts_is_home() { $old_wp_query = $GLOBALS['wp_query']; Utility::mock_wp_query( - [], - [ + array(), + array( 'is_home' => true, - ] + ) ); do_action( 'wp_enqueue_scripts' ); @@ -103,10 +103,10 @@ public function test_register_scripts_is_home() { public function test_register_scripts_is_single() { $old_wp_query = $GLOBALS['wp_query']; Utility::mock_wp_query( - [], - [ + array(), + array( 'is_single' => true, - ] + ) ); do_action( 'wp_enqueue_scripts' ); @@ -119,9 +119,9 @@ public function test_register_scripts_is_single() { * @covers ::get_asset_file_path */ public function test_get_asset_file_path() { - $asset_path = [ + $asset_path = array( 'test' => 'test1', - ]; + ); Utility::set_and_get_property( $this->instance, 'asset_paths', $asset_path ); @@ -134,13 +134,53 @@ public function test_get_asset_file_path() { */ public function test_get_asset_file_uri() { - $asset_path = [ + $asset_path = array( 'test' => 'test1', - ]; + ); Utility::set_and_get_property( $this->instance, 'asset_paths', $asset_path ); $expected_path = untrailingslashit( get_template_directory_uri() ) . '/assets/build/test1'; $this->assertEquals( $expected_path, $this->instance->get_asset_file_uri( 'test' ) ); } + + /** + * @covers ::get_asset_paths + */ + public function test_get_asset_paths() { + $http_response = [ + 'body' => wp_json_encode( + [ + 'test1' => 'Test 1', + 'test2' => 'Test 2', + ] + ), + ]; + + $this->mock_http_response( $http_response ); + + $expected_data = [ + 'test1' => 'Test 1', + 'test2' => 'Test 2', + ]; + + $this->assertEquals( $expected_data, $this->instance->get_asset_paths() ); + + $http_response = new \WP_Error(); + + $this->mock_http_response( $http_response ); + + $this->assertFalse( $this->instance->get_asset_paths() ); + } + + public function mock_http_response( $mocked_response ) { + add_filter( + 'pre_http_request', + function( $response, $args, $url ) use ( $mocked_response ) { + return $mocked_response; + }, + 10, + 3 + ); + } } From d8b21a027e31729cf40675fb82803d65f2c46788 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Tue, 3 Mar 2020 20:37:58 +0530 Subject: [PATCH 21/35] Fix error with customize_register method --- inc/classes/class-customizer.php | 14 +++++++++++--- tests/inc/classes/test-class-customizer.php | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/inc/classes/class-customizer.php b/inc/classes/class-customizer.php index 7fbe9cf..e094791 100644 --- a/inc/classes/class-customizer.php +++ b/inc/classes/class-customizer.php @@ -47,9 +47,17 @@ protected function _setup_hooks() { */ public function customize_register( \WP_Customize_Manager $wp_customize ) { - $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; - $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage'; - $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage'; + if ( ! empty( $wp_customize->get_setting( 'blogname' ) ) ) { + $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; + } + + if ( ! empty( $wp_customize->get_setting( 'blogdescription' ) ) ) { + $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage'; + } + + if ( ! empty( $wp_customize->get_setting( 'header_textcolor' ) ) ) { + $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage'; + } if ( isset( $wp_customize->selective_refresh ) ) { diff --git a/tests/inc/classes/test-class-customizer.php b/tests/inc/classes/test-class-customizer.php index 63aae5f..cb74dc2 100644 --- a/tests/inc/classes/test-class-customizer.php +++ b/tests/inc/classes/test-class-customizer.php @@ -127,6 +127,12 @@ public function test_customize_register() { $wp_customize = new \WP_Customize_Manager(); + $wp_customize->add_setting( 'blogname' , array( + 'default' => 'Test', + 'transport' => 'refresh', + ) ); + + do_action( 'customize_register', $wp_customize ); $this->assertEquals( $wp_customize->get_setting( 'blogname' )->transport, 'postMessage' ); From 95c654e752a79c5e56d7d8e299a45a1e0b55f51d Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Wed, 4 Mar 2020 18:49:06 +0530 Subject: [PATCH 22/35] Fix customizer test cases and run phpcbf on test cases --- inc/classes/class-assets.php | 3 +- tests/inc/classes/test-class-assets.php | 12 ++-- tests/inc/classes/test-class-blank-theme.php | 48 ++++++++-------- tests/inc/classes/test-class-customizer.php | 56 +++++++++++++------ .../classes/test-class-infinite-scroll.php | 20 +++---- tests/test-base-blank-theme.php | 49 ---------------- 6 files changed, 81 insertions(+), 107 deletions(-) delete mode 100644 tests/test-base-blank-theme.php diff --git a/inc/classes/class-assets.php b/inc/classes/class-assets.php index 05f88ac..f67ebd0 100644 --- a/inc/classes/class-assets.php +++ b/inc/classes/class-assets.php @@ -70,8 +70,9 @@ public function register_scripts() { wp_enqueue_script( 'blank-theme-single' ); } + // Ignoring this block becuase trying to mock comments_open() throws error. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { - wp_enqueue_script( 'comment-reply' ); // @codeCoverageIgnore Ignoring becuase trying to mock comments_open() throws error. + wp_enqueue_script( 'comment-reply' ); // @codeCoverageIgnore } } diff --git a/tests/inc/classes/test-class-assets.php b/tests/inc/classes/test-class-assets.php index d678831..05d7928 100644 --- a/tests/inc/classes/test-class-assets.php +++ b/tests/inc/classes/test-class-assets.php @@ -148,21 +148,21 @@ public function test_get_asset_file_uri() { * @covers ::get_asset_paths */ public function test_get_asset_paths() { - $http_response = [ + $http_response = array( 'body' => wp_json_encode( - [ + array( 'test1' => 'Test 1', 'test2' => 'Test 2', - ] + ) ), - ]; + ); $this->mock_http_response( $http_response ); - $expected_data = [ + $expected_data = array( 'test1' => 'Test 1', 'test2' => 'Test 2', - ]; + ); $this->assertEquals( $expected_data, $this->instance->get_asset_paths() ); diff --git a/tests/inc/classes/test-class-blank-theme.php b/tests/inc/classes/test-class-blank-theme.php index 03a7ce8..0a4b519 100644 --- a/tests/inc/classes/test-class-blank-theme.php +++ b/tests/inc/classes/test-class-blank-theme.php @@ -34,10 +34,10 @@ public function setUp() : void { parent::setUp(); switch_theme( 'blank-theme' ); - $this->instance = BLANK_THEME::get_instance(); + $this->instance = BLANK_THEME::get_instance(); $this->mock_post = $this->factory()->post->create_and_get(); $GLOBALS['post'] = $this->mock_post; - add_filter( 'is_active_sidebar', [ $this, 'deactivate_sidebar' ], 10, 2 ); + add_filter( 'is_active_sidebar', array( $this, 'deactivate_sidebar' ), 10, 2 ); } public function tearDown() { @@ -54,38 +54,38 @@ public function test_construct() { Utility::invoke_method( $this->instance, '__construct' ); $this->assertInstanceOf( 'BLANK_THEME\Inc\BLANK_THEME', $this->instance ); - $hooks = [ - [ + $hooks = array( + array( 'type' => 'filter', 'name' => 'excerpt_more', 'priority' => 10, 'function' => 'add_read_more_link', - ], - [ + ), + array( 'type' => 'filter', 'name' => 'body_class', 'priority' => 10, 'function' => 'filter_body_classes', - ], - [ + ), + array( 'type' => 'action', 'name' => 'wp_head', 'priority' => 10, 'function' => 'add_pingback_link', - ], - [ + ), + array( 'type' => 'action', 'name' => 'after_setup_theme', 'priority' => 10, 'function' => 'setup_theme', - ], - [ + ), + array( 'type' => 'action', 'name' => 'init', 'priority' => 10, 'function' => 'add_title_tag_support', - ], - ]; + ), + ); // Check if hooks loaded. foreach ( $hooks as $hook ) { @@ -95,10 +95,10 @@ public function test_construct() { call_user_func( sprintf( 'has_%s', $hook['type'] ), $hook['name'], - [ + array( $this->instance, $hook['function'], - ] + ) ), sprintf( 'BLANK_THEME::__construct() failed to register %1$s "%2$s" to %3$s()', $hook['type'], $hook['name'], $hook['function'] ) ); @@ -156,15 +156,15 @@ public function test_add_read_more_link() { * @covers ::filter_body_classes */ public function test_filter_body_classes() { - //error_log( var_export( is_active_sidebar( 'sidebar-1' ), true ) ); + // error_log( var_export( is_active_sidebar( 'sidebar-1' ), true ) ); $this->assertContains( 'test-class', $this->instance->filter_body_classes( array( 'test-class' ) ) ); $this->assertContains( 'hfeed', $this->instance->filter_body_classes( array( 'test-class' ) ) ); $this->assertContains( 'no-sidebar', $this->instance->filter_body_classes( array( 'test-class' ) ) ); Utility::mock_wp_query( - [], - [ + array(), + array( 'is_singular' => true, - ] + ) ); $this->assertNotContains( 'hfeed', $this->instance->filter_body_classes( array( 'test-class' ) ) ); @@ -181,12 +181,12 @@ public function test_add_pingback_link() { $expected = ''; Utility::mock_wp_query( - [], - [ + array(), + array( 'is_singular' => true, - ] + ) ); - $actual = Utility::buffer_and_return( [ $this->instance, 'add_pingback_link' ] ); + $actual = Utility::buffer_and_return( array( $this->instance, 'add_pingback_link' ) ); $this->assertEquals( $expected, $actual ); } diff --git a/tests/inc/classes/test-class-customizer.php b/tests/inc/classes/test-class-customizer.php index cb74dc2..3b230ea 100644 --- a/tests/inc/classes/test-class-customizer.php +++ b/tests/inc/classes/test-class-customizer.php @@ -47,21 +47,20 @@ public function test_construct() { $this->assertInstanceOf( 'BLANK_THEME\Inc\Customizer', $this->instance ); - - $hooks = [ - [ + $hooks = array( + array( 'type' => 'action', 'name' => 'customize_register', 'priority' => 10, 'function' => 'customize_register', - ], - [ + ), + array( 'type' => 'action', 'name' => 'customize_preview_init', 'priority' => 10, 'function' => 'customize_preview_init', - ], - ]; + ), + ); // Check if hooks loaded. foreach ( $hooks as $hook ) { @@ -71,10 +70,10 @@ public function test_construct() { call_user_func( sprintf( 'has_%s', $hook['type'] ), $hook['name'], - [ + array( $this->instance, $hook['function'], - ] + ) ), sprintf( 'Customizer::__construct() failed to register %1$s "%2$s" to %3$s()', $hook['type'], $hook['name'], $hook['function'] ) ); @@ -89,7 +88,7 @@ public function test_construct() { public function test_customize_partial_blog_name() { $expected = get_bloginfo( 'name' ); - $actual = Utility::buffer_and_return( [ $this->instance, 'customize_partial_blog_name' ] ); + $actual = Utility::buffer_and_return( array( $this->instance, 'customize_partial_blog_name' ) ); $this->assertEquals( $expected, $actual ); } @@ -102,7 +101,7 @@ public function test_customize_partial_blog_name() { public function test_partial_blog_description() { $expected = get_bloginfo( 'description' ); - $actual = Utility::buffer_and_return( [ $this->instance, 'customize_partial_blog_description' ] ); + $actual = Utility::buffer_and_return( array( $this->instance, 'customize_partial_blog_description' ) ); $this->assertEquals( $expected, $actual ); } @@ -122,19 +121,42 @@ public function test_enqueue_customizer_scripts() { public function test_customize_register() { wp_set_current_user( - $this->factory->user->create( [ 'role' => 'administrator' ] ) + $this->factory->user->create( array( 'role' => 'administrator' ) ) ); $wp_customize = new \WP_Customize_Manager(); - $wp_customize->add_setting( 'blogname' , array( - 'default' => 'Test', - 'transport' => 'refresh', - ) ); + $wp_customize->add_setting( + 'blogname', + array( + 'default' => 'Test', + 'transport' => 'postName', + ) + ); + $wp_customize->add_setting( + 'blogdescription', + array( + 'default' => 'Test', + 'transport' => 'postName', + ) + ); - do_action( 'customize_register', $wp_customize ); + $wp_customize->add_setting( + 'header_textcolor', + array( + 'default' => 'Test', + 'transport' => 'postName', + ) + ); + + $this->instance->customize_register( $wp_customize ); $this->assertEquals( $wp_customize->get_setting( 'blogname' )->transport, 'postMessage' ); + + $this->assertEquals( $wp_customize->get_setting( 'blogdescription' )->transport, 'postMessage' ); + + $this->assertEquals( $wp_customize->get_setting( 'header_textcolor' )->transport, 'postMessage' ); + } } diff --git a/tests/inc/classes/test-class-infinite-scroll.php b/tests/inc/classes/test-class-infinite-scroll.php index b15b61a..a6df3dc 100644 --- a/tests/inc/classes/test-class-infinite-scroll.php +++ b/tests/inc/classes/test-class-infinite-scroll.php @@ -58,30 +58,30 @@ public function test_render_callback() { $post_ids = $this->factory->post->create_many( 10, - [ + array( 'post_type' => 'post', - ] + ) ); Utility::mock_wp_query( - [ 'post_type' => 'post' ], - [] + array( 'post_type' => 'post' ), + array() ); - $output = Utility::buffer_and_return( [ $this->instance, 'render_callback' ] ); + $output = Utility::buffer_and_return( array( $this->instance, 'render_callback' ) ); $this->assertNotEmpty( $output ); $this->assertContains( ' 'post', - ], - [ + ), + array( 'is_search' => true, - ] + ) ); - $output = Utility::buffer_and_return( [ $this->instance, 'render_callback' ] ); + $output = Utility::buffer_and_return( array( $this->instance, 'render_callback' ) ); $this->assertNotEmpty( $output ); $this->assertContains( 'assertFalse( wp_script_is( 'jquery' ) ); - - do_action( 'wp_enqueue_scripts' ); - $this->assertTrue( wp_script_is( 'jquery' ) ); - - } // end testjQueryIsLoaded - - /** - * Test if theme is active. - */ - public function test_active_theme() { - $this->assertTrue( wp_get_theme()->get( 'Name' ) === 'blank-theme' ); - } // end testThemeInitialization - - /** - * Test random bundled theme is inactive. - */ - public function test_inactive_theme() { - $this->assertFalse( wp_get_theme()->get( 'Name' ) === 'twentytwenty' ); - } // end testInactiveTheme -} From 821afca63fce490808381a99d966f1434a3a33e6 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Wed, 4 Mar 2020 20:26:32 +0530 Subject: [PATCH 23/35] Add test cases for custom functions --- tests/inc/helpers/test-custom-functions.php | 94 +++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tests/inc/helpers/test-custom-functions.php diff --git a/tests/inc/helpers/test-custom-functions.php b/tests/inc/helpers/test-custom-functions.php new file mode 100644 index 0000000..a72b9f2 --- /dev/null +++ b/tests/inc/helpers/test-custom-functions.php @@ -0,0 +1,94 @@ +mock_post = $this->factory()->post->create_and_get(); + $GLOBALS['post'] = $this->mock_post; + } + + public function tearDown() { + unset( $GLOBALS['post'] ); + } + + /** + * @covers ::blank_theme_primary_classes() + */ + public function test_blank_theme_primary_classes() { + + set_theme_mod( 'blank_theme_sidebar_position', 'left' ); + $expected = 'blank-theme-primary large-8 medium-8 small-12 cell column large-order-2 medium-order-2 small-order-1 test clearfix'; + $output = Utility::buffer_and_return( 'blank_theme_primary_classes', array( 'test' ) ); + $this->assertEquals( $expected, $output ); + + set_theme_mod( 'blank_theme_sidebar_position', 'right' ); + $expected = 'blank-theme-primary large-8 medium-8 small-12 cell column test clearfix'; + $output = Utility::buffer_and_return( 'blank_theme_primary_classes', array( 'test' ) ); + $this->assertEquals( $expected, $output ); + + set_theme_mod( 'blank_theme_sidebar_position', 'no_sidebar' ); + $expected = 'blank-theme-primary large-12 medium-12 small-12 cell column test clearfix'; + $output = Utility::buffer_and_return( 'blank_theme_primary_classes', array( 'test' ) ); + $this->assertEquals( $expected, $output ); + + set_theme_mod( 'blank_theme_sidebar_position', false ); + } + + public function test_blank_theme_secondary_classes() { + + $expected = 'blank-theme-secondary widget-area large-4 medium-4 small-12 cell column test clearfix'; + $output = Utility::buffer_and_return( 'blank_theme_secondary_classes', array( 'test' ) ); + $this->assertEquals( $expected, $output ); + + set_theme_mod( 'blank_theme_sidebar_position', 'left' ); + $expected = 'blank-theme-secondary widget-area large-4 medium-4 small-12 cell column large-order-1 medium-order-1 small-order-2 test clearfix'; + $output = Utility::buffer_and_return( 'blank_theme_secondary_classes', array( 'test' ) ); + $this->assertEquals( $expected, $output ); + } + + public function test_blank_theme_get_font_url() { + $expected = esc_url( get_template_directory_uri() . '/assets/fonts/test/path' ); + $output = Utility::buffer_and_return( 'blank_theme_get_font_url', array( '/test/path' ) ); + $this->assertEquals( $expected, $output ); + } + + public function test_blank_theme_pagination() { + + $post_ids = $this->factory->post->create_many( + 10, + array( + 'post_type' => 'post', + ) + ); + + Utility::mock_wp_query( + array( 'post_type' => 'post' ), + array() + ); + + $output = Utility::buffer_and_return( 'blank_theme_pagination' ); + $this->assertContains( 'nav', $output ); + $this->assertContains( 'page-numbers', $output ); + } + +} From 627ca06f90088ca3a9e86df192ded5d6d2b40941 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Wed, 4 Mar 2020 20:26:50 +0530 Subject: [PATCH 24/35] Remove unnecessary setup code --- tests/inc/classes/test-class-assets.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/inc/classes/test-class-assets.php b/tests/inc/classes/test-class-assets.php index 05d7928..3d5b828 100644 --- a/tests/inc/classes/test-class-assets.php +++ b/tests/inc/classes/test-class-assets.php @@ -35,8 +35,7 @@ public function setUp() : void { parent::setUp(); $this->instance = Assets::get_instance(); switch_theme( 'blank-theme' ); - update_option( 'thread_comments', 1 ); - add_filter( 'comments_open', '__return_true' ); + $this->mock_post = $this->factory()->post->create_and_get(); $GLOBALS['post'] = $this->mock_post; } From 12b3959c3d013a17a7d635e786d4ea9cd79c868c Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Thu, 5 Mar 2020 18:57:41 +0530 Subject: [PATCH 25/35] Add code coverage ignore to register asset methods --- inc/classes/class-assets.php | 6 +++ inc/classes/class-customizer.php | 3 ++ tests/inc/classes/test-class-assets.php | 55 --------------------- tests/inc/classes/test-class-customizer.php | 13 ----- 4 files changed, 9 insertions(+), 68 deletions(-) diff --git a/inc/classes/class-assets.php b/inc/classes/class-assets.php index f67ebd0..3b61897 100644 --- a/inc/classes/class-assets.php +++ b/inc/classes/class-assets.php @@ -53,6 +53,9 @@ protected function _setup_hooks() { * Register scripts. * * @action wp_enqueue_scripts + * + * Ignoring this because the asset file names are generated dynamically and fetched from manifest so getting issues with mocking. + * @codeCoverageIgnore */ public function register_scripts() { @@ -81,6 +84,9 @@ public function register_scripts() { * Register styles. * * @action wp_enqueue_scripts + * + * Ignoring this because the asset file names are generated dynamically and fetched from manifest so getting issues with mocking. + * @codeCoverageIgnore */ public function register_styles() { diff --git a/inc/classes/class-customizer.php b/inc/classes/class-customizer.php index e094791..3f713ae 100644 --- a/inc/classes/class-customizer.php +++ b/inc/classes/class-customizer.php @@ -102,6 +102,9 @@ public function customize_partial_blog_description() { * Enqueue customizer scripts. * * @action customize_preview_init + * + * Ignoring this because the asset file names are generated dynamically and fetched from manifest so getting issues with mocking. + * @codeCoverageIgnore */ public function enqueue_customizer_scripts() { wp_enqueue_script( diff --git a/tests/inc/classes/test-class-assets.php b/tests/inc/classes/test-class-assets.php index 3d5b828..b5f08c3 100644 --- a/tests/inc/classes/test-class-assets.php +++ b/tests/inc/classes/test-class-assets.php @@ -59,61 +59,6 @@ public function test_setup_hooks() { } - /** - * Function to test scripts registration. - * - * @covers ::register_scripts - * @covers ::register_styles - */ - public function test_register_scripts() { - - do_action( 'wp_enqueue_scripts' ); - $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); - $this->assertTrue( wp_style_is( 'blank-theme-main' ) ); - } - - /** - * Function to test scripts registration. - * - * @covers ::register_scripts - * @covers ::register_styles - */ - public function test_register_scripts_is_home() { - $old_wp_query = $GLOBALS['wp_query']; - Utility::mock_wp_query( - array(), - array( - 'is_home' => true, - ) - ); - do_action( 'wp_enqueue_scripts' ); - - $this->assertTrue( wp_script_is( 'blank-theme-home' ) ); - $this->assertTrue( wp_style_is( 'blank-theme-home' ) ); - $GLOBALS['wp_query'] = $old_wp_query; - } - - /** - * Function to test scripts registration. - * - * @covers ::register_scripts - * @covers ::register_styles - */ - public function test_register_scripts_is_single() { - $old_wp_query = $GLOBALS['wp_query']; - Utility::mock_wp_query( - array(), - array( - 'is_single' => true, - ) - ); - do_action( 'wp_enqueue_scripts' ); - - $this->assertTrue( wp_script_is( 'blank-theme-single' ) ); - $this->assertTrue( wp_style_is( 'blank-theme-single' ) ); - $GLOBALS['wp_query'] = $old_wp_query; - } - /** * @covers ::get_asset_file_path */ diff --git a/tests/inc/classes/test-class-customizer.php b/tests/inc/classes/test-class-customizer.php index 3b230ea..0192495 100644 --- a/tests/inc/classes/test-class-customizer.php +++ b/tests/inc/classes/test-class-customizer.php @@ -105,19 +105,6 @@ public function test_partial_blog_description() { $this->assertEquals( $expected, $actual ); } - /** - * Test customizer scripts. - * - * @covers ::enqueue_customizer_scripts - */ - public function test_enqueue_customizer_scripts() { - $this->assertFalse( wp_script_is( 'blank-theme-customizer' ) ); - - $this->instance->enqueue_customizer_scripts(); - - $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); - } - public function test_customize_register() { wp_set_current_user( From 98c55555eeab1f37af8356986c999ca741d2b909 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Tue, 24 Mar 2020 18:21:56 +0530 Subject: [PATCH 26/35] Fix phpcs --- inc/classes/class-customizer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/classes/class-customizer.php b/inc/classes/class-customizer.php index 3f713ae..2b1c733 100644 --- a/inc/classes/class-customizer.php +++ b/inc/classes/class-customizer.php @@ -48,11 +48,11 @@ protected function _setup_hooks() { public function customize_register( \WP_Customize_Manager $wp_customize ) { if ( ! empty( $wp_customize->get_setting( 'blogname' ) ) ) { - $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; + $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; } if ( ! empty( $wp_customize->get_setting( 'blogdescription' ) ) ) { - $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage'; + $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage'; } if ( ! empty( $wp_customize->get_setting( 'header_textcolor' ) ) ) { @@ -111,7 +111,7 @@ public function enqueue_customizer_scripts() { 'blank-theme-customizer', get_template_directory_uri() . '/assets/build/js/admin/customizer.js', [ 'customize-preview' ], - false, + false, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion true ); } From d0fc4d7e3466d36df8dd91ed43326ef4e1ba3613 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Tue, 24 Mar 2020 18:38:22 +0530 Subject: [PATCH 27/35] Fix phpcs --- inc/classes/class-customizer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/classes/class-customizer.php b/inc/classes/class-customizer.php index 2b1c733..c3fde4d 100644 --- a/inc/classes/class-customizer.php +++ b/inc/classes/class-customizer.php @@ -107,11 +107,11 @@ public function customize_partial_blog_description() { * @codeCoverageIgnore */ public function enqueue_customizer_scripts() { - wp_enqueue_script( + wp_enqueue_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion 'blank-theme-customizer', get_template_directory_uri() . '/assets/build/js/admin/customizer.js', [ 'customize-preview' ], - false, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion + false, true ); } From 0cbd0e4aedda3d65dc288a1d0b32db5bd9c0c7d4 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Wed, 25 Mar 2020 18:34:44 +0530 Subject: [PATCH 28/35] Exclude template-tags and autoloader from code coverage --- phpunit.xml.dist | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 574f81a..8406a73 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,6 +16,8 @@ ./inc/ + ./inc/helpers/template-tags.php + ./inc/helpers/autoloader.php ./inc/traits From 099a82340b1301cbdcd4faaed7af9f33ddf0a310 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Wed, 25 Mar 2020 19:00:13 +0530 Subject: [PATCH 29/35] Update contributors workflow --- README.md | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 97bc575..f0801cf 100644 --- a/README.md +++ b/README.md @@ -74,31 +74,29 @@ npm run precommit ## Contributing -### Report a Bug +### Reporting a bug 🐞 -Before you create a new issue, please search [existing issues](https://github.com/rtCamp/blank-theme/issues) to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version. +Before creating a new issue, do browse through the [existing issues](https://github.com/rtCamp/blank-theme/issues) for resolution or upcoming fixes. -Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/rtCamp/blank-theme/issues/new). Include as much detail as you can, and clear steps to reproduce if possible. +If you still need to [log an issue](https://github.com/rtCamp/blank-theme/issues/new), making sure to include as much detail as you can, including clear steps to reproduce your issue if possible. ### Create a pull request -Want to contribute a new feature? Please first open a new issue to discuss whether the feature is a good fit for the project. +Want to contribute a new feature? Start a conversation by logging an [issue](https://github.com/rtCamp/blank-theme/issues). -Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request. +Once you're ready to send a pull request, please run through the following checklist: -1. Search existing issues. If you can’t find anything related to what you want to work on, open a new issue. +1. Browse through the existing issues for anything related to what you want to work on. If you don't find any related issues, open a new one. 1. Fork the repository. -1. Create a branch from `develop` for each issue you’d like to address. Commit your changes. +1. Create a branch from `develop` for each issue you'd like to address and commit your changes. 1. Push the code changes from your local clone to your fork. -1. Open a pull request. +1. Open a pull request and that's it! We'll with feedback as soon as possible (Isn't collaboration a great thing? 😌) -1. Respond to code review feedback in a timely manner, recognizing development is a collaborative process. - -1. You need at least one approval and Once your pull request has passed code review and tests, it will be merged into `develop` and be in the pipeline for the next release. +1. Once your pull request has passed final code review and tests, it will be merged into `develop` and be in the pipeline for the next release. Props to you! 🎉 ### Unit testing From 5596f142b8ad8519ef6073c50492b4ad6b510f42 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Wed, 25 Mar 2020 19:00:49 +0530 Subject: [PATCH 30/35] Remove duplicate test assertions --- tests/inc/classes/test-class-blank-theme.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/inc/classes/test-class-blank-theme.php b/tests/inc/classes/test-class-blank-theme.php index 0a4b519..de5660b 100644 --- a/tests/inc/classes/test-class-blank-theme.php +++ b/tests/inc/classes/test-class-blank-theme.php @@ -103,9 +103,6 @@ public function test_construct() { sprintf( 'BLANK_THEME::__construct() failed to register %1$s "%2$s" to %3$s()', $hook['type'], $hook['name'], $hook['function'] ) ); } - $this->assertEquals( 10, has_filter( 'excerpt_more', array( $this->instance, 'add_read_more_link' ) ) ); - $this->assertEquals( 10, has_filter( 'body_class', array( $this->instance, 'filter_body_classes' ) ) ); - $this->assertEquals( 10, has_action( 'wp_head', array( $this->instance, 'add_pingback_link' ) ) ); } /** From 3b1fbf277e8a4b7bc9110df692e9c516e7fc74d4 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Wed, 25 Mar 2020 21:05:50 +0530 Subject: [PATCH 31/35] Add ignore annotation for a custom function --- inc/helpers/custom-functions.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inc/helpers/custom-functions.php b/inc/helpers/custom-functions.php index 57aec75..2c5138e 100644 --- a/inc/helpers/custom-functions.php +++ b/inc/helpers/custom-functions.php @@ -81,6 +81,8 @@ function blank_theme_pagination() { * @param string $slug file slug like you use in get_template_part without php extension. * @param array $variables pass an array of variables you want to use in array keys. * + * @codeCoverageIgnore Ignoring becuase not able to mock output for locate_template + * * @return void */ function blank_theme_get_template_part( $slug, $variables = [] ) { From 92c99b06747306948365a2725c5fd4d2e9da26b5 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Thu, 26 Mar 2020 17:39:46 +0530 Subject: [PATCH 32/35] Add doc description for test functions --- tests/inc/classes/test-class-assets.php | 21 ++++++++++++ tests/inc/classes/test-class-blank-theme.php | 23 +++++++++++++ tests/inc/classes/test-class-customizer.php | 19 +++++++++-- .../classes/test-class-infinite-scroll.php | 8 +++++ tests/inc/classes/test-class-widgets.php | 11 ++++++- tests/inc/helpers/test-custom-functions.php | 32 ++++++++++++++++++- 6 files changed, 109 insertions(+), 5 deletions(-) diff --git a/tests/inc/classes/test-class-assets.php b/tests/inc/classes/test-class-assets.php index b5f08c3..8dc5a72 100644 --- a/tests/inc/classes/test-class-assets.php +++ b/tests/inc/classes/test-class-assets.php @@ -49,6 +49,8 @@ public function tearDown() { * * @covers ::_setup_hooks * @covers ::__construct + * + * @return void */ public function test_setup_hooks() { @@ -60,7 +62,11 @@ public function test_setup_hooks() { } /** + * Tests `get_asset_file_path` function. + * * @covers ::get_asset_file_path + * + * @return void */ public function test_get_asset_file_path() { $asset_path = array( @@ -74,7 +80,11 @@ public function test_get_asset_file_path() { } /** + * Tests `get_asset_file_uri` function. + * * @covers ::get_asset_file_uri + * + * @return void */ public function test_get_asset_file_uri() { @@ -89,7 +99,11 @@ public function test_get_asset_file_uri() { } /** + * Tests `get_asset_paths` function. + * * @covers ::get_asset_paths + * + * @return void */ public function test_get_asset_paths() { $http_response = array( @@ -117,6 +131,13 @@ public function test_get_asset_paths() { $this->assertFalse( $this->instance->get_asset_paths() ); } + /** + * Helper function to mock http response. + * + * @param array $mocked_response An array of mocked response data. + * + * @return void + */ public function mock_http_response( $mocked_response ) { add_filter( 'pre_http_request', diff --git a/tests/inc/classes/test-class-blank-theme.php b/tests/inc/classes/test-class-blank-theme.php index de5660b..a9c7856 100644 --- a/tests/inc/classes/test-class-blank-theme.php +++ b/tests/inc/classes/test-class-blank-theme.php @@ -40,6 +40,11 @@ public function setUp() : void { add_filter( 'is_active_sidebar', array( $this, 'deactivate_sidebar' ), 10, 2 ); } + /** + * Reset global post after every test case. + * + * @return void + */ public function tearDown() { unset( $GLOBALS['post'] ); } @@ -49,6 +54,8 @@ public function tearDown() { * * @covers ::__construct * @covers ::_setup_hooks + * + * @return void */ public function test_construct() { @@ -109,6 +116,8 @@ public function test_construct() { * Test function setup theme * * @covers ::setup_theme + * + * @return void */ public function test_setup_theme() { @@ -135,6 +144,8 @@ public function test_setup_theme() { * Test add read more link. * * @covers ::add_read_more_link + * + * @return void */ public function test_add_read_more_link() { global $post; @@ -151,6 +162,8 @@ public function test_add_read_more_link() { * Test function to add custom body classes. * * @covers ::filter_body_classes + * + * @return void */ public function test_filter_body_classes() { // error_log( var_export( is_active_sidebar( 'sidebar-1' ), true ) ); @@ -171,6 +184,8 @@ public function test_filter_body_classes() { * Test function to add pingback link. * * @covers ::add_pingback_link + * + * @return void */ public function test_add_pingback_link() { @@ -187,6 +202,14 @@ public function test_add_pingback_link() { $this->assertEquals( $expected, $actual ); } + /** + * Helper function to disabled sidebar. + * + * @param boolean $is_active Whether the sidebar is active. + * @param int|string $index Index, name, or ID of the dynamic sidebar. + * + * @return boolean + */ public function deactivate_sidebar( $is_active, $index ) { if ( 'sidebar-1' === $index ) { diff --git a/tests/inc/classes/test-class-customizer.php b/tests/inc/classes/test-class-customizer.php index 0192495..588276e 100644 --- a/tests/inc/classes/test-class-customizer.php +++ b/tests/inc/classes/test-class-customizer.php @@ -37,10 +37,12 @@ public function setUp(): void { } /** - * Test constructor function. + * Tests class construct. * * @covers ::__construct * @covers ::_setup_hooks + * + * @return void */ public function test_construct() { Utility::invoke_method( $this->instance, '__construct' ); @@ -81,9 +83,11 @@ public function test_construct() { } /** - * Test customize partial blog name. + * Tests customize partial blog name. * * @covers ::customize_partial_blog_name + * + * @return void */ public function test_customize_partial_blog_name() { $expected = get_bloginfo( 'name' ); @@ -94,9 +98,11 @@ public function test_customize_partial_blog_name() { } /** - * Test customize partial blog decription. + * Tests customize partial blog description. * * @covers ::customize_partial_blog_description + * + * @return void */ public function test_partial_blog_description() { $expected = get_bloginfo( 'description' ); @@ -105,6 +111,13 @@ public function test_partial_blog_description() { $this->assertEquals( $expected, $actual ); } + /** + * Tests `customize_register` function. + * + * @covers ::customize_register + * + * @return void + */ public function test_customize_register() { wp_set_current_user( diff --git a/tests/inc/classes/test-class-infinite-scroll.php b/tests/inc/classes/test-class-infinite-scroll.php index a6df3dc..679105d 100644 --- a/tests/inc/classes/test-class-infinite-scroll.php +++ b/tests/inc/classes/test-class-infinite-scroll.php @@ -35,7 +35,11 @@ public function setUp(): void { } /** + * Tests `setup_jetpack` function. + * * @covers ::setup_jetpack + * + * @return void */ public function test_setup_jetpack() { $this->instance->setup_jetpack(); @@ -52,7 +56,11 @@ public function test_setup_jetpack() { } /** + * Tests `render_callback` function. + * * @covers ::render_callback + * + * @return void */ public function test_render_callback() { diff --git a/tests/inc/classes/test-class-widgets.php b/tests/inc/classes/test-class-widgets.php index b3fcfd9..cd968fb 100644 --- a/tests/inc/classes/test-class-widgets.php +++ b/tests/inc/classes/test-class-widgets.php @@ -37,10 +37,12 @@ public function setUp(): void { } /** - * Test constructor function. + * Tests class construct. * * @covers ::__construct * @covers ::_setup_hooks + * + * @return void */ public function test_construct() { @@ -52,6 +54,13 @@ public function test_construct() { } + /** + * Tests `register_widgets` function. + * + * @covers ::register_widgets + * + * @return void + */ public function test_register_widgets() { $this->instance->register_widgets(); diff --git a/tests/inc/helpers/test-custom-functions.php b/tests/inc/helpers/test-custom-functions.php index a72b9f2..68f9c33 100644 --- a/tests/inc/helpers/test-custom-functions.php +++ b/tests/inc/helpers/test-custom-functions.php @@ -27,12 +27,21 @@ public function setUp() : void { $GLOBALS['post'] = $this->mock_post; } + /** + * Reset global post after every test case. + * + * @return void + */ public function tearDown() { unset( $GLOBALS['post'] ); } /** - * @covers ::blank_theme_primary_classes() + * Tests `blank_theme_primary_classes` function. + * + * @covers blank_theme_primary_classes() + * + * @return void */ public function test_blank_theme_primary_classes() { @@ -54,6 +63,13 @@ public function test_blank_theme_primary_classes() { set_theme_mod( 'blank_theme_sidebar_position', false ); } + /** + * Tests `blank_theme_secondary_classes` function. + * + * @covers blank_theme_secondary_classes() + * + * @return void + */ public function test_blank_theme_secondary_classes() { $expected = 'blank-theme-secondary widget-area large-4 medium-4 small-12 cell column test clearfix'; @@ -66,12 +82,26 @@ public function test_blank_theme_secondary_classes() { $this->assertEquals( $expected, $output ); } + /** + * Tests `blank_theme_get_font_url` function. + * + * @covers blank_theme_get_font_url() + * + * @return void + */ public function test_blank_theme_get_font_url() { $expected = esc_url( get_template_directory_uri() . '/assets/fonts/test/path' ); $output = Utility::buffer_and_return( 'blank_theme_get_font_url', array( '/test/path' ) ); $this->assertEquals( $expected, $output ); } + /** + * Tests `blank_theme_pagination()` function. + * + * @covers blank_theme_pagination() + * + * @return void + */ public function test_blank_theme_pagination() { $post_ids = $this->factory->post->create_many( From f7711437b1de2a58375293ab99cd3c1b14f1fbf7 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Thu, 26 Mar 2020 18:31:23 +0530 Subject: [PATCH 33/35] Fix covers annotation --- tests/inc/helpers/test-custom-functions.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/inc/helpers/test-custom-functions.php b/tests/inc/helpers/test-custom-functions.php index 68f9c33..96ed57b 100644 --- a/tests/inc/helpers/test-custom-functions.php +++ b/tests/inc/helpers/test-custom-functions.php @@ -39,7 +39,7 @@ public function tearDown() { /** * Tests `blank_theme_primary_classes` function. * - * @covers blank_theme_primary_classes() + * @covers ::blank_theme_primary_classes * * @return void */ @@ -66,7 +66,7 @@ public function test_blank_theme_primary_classes() { /** * Tests `blank_theme_secondary_classes` function. * - * @covers blank_theme_secondary_classes() + * @covers ::blank_theme_secondary_classes * * @return void */ @@ -85,7 +85,7 @@ public function test_blank_theme_secondary_classes() { /** * Tests `blank_theme_get_font_url` function. * - * @covers blank_theme_get_font_url() + * @covers ::blank_theme_get_font_url * * @return void */ @@ -98,7 +98,7 @@ public function test_blank_theme_get_font_url() { /** * Tests `blank_theme_pagination()` function. * - * @covers blank_theme_pagination() + * @covers ::blank_theme_pagination * * @return void */ From 679412eafd9e2929d38c7cdb0602cd01bc1511e9 Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Fri, 10 Jul 2020 18:26:48 +0530 Subject: [PATCH 34/35] Fix undefined function warning --- inc/classes/class-customizer.php | 2 +- tests/inc/classes/test-class-customizer.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/classes/class-customizer.php b/inc/classes/class-customizer.php index c3fde4d..fee2083 100644 --- a/inc/classes/class-customizer.php +++ b/inc/classes/class-customizer.php @@ -34,7 +34,7 @@ protected function _setup_hooks() { * Actions */ add_action( 'customize_register', [ $this, 'customize_register' ] ); - add_action( 'customize_preview_init', [ $this, 'customize_preview_init' ] ); + add_action( 'customize_preview_init', [ $this, 'enqueue_customizer_scripts' ] ); } diff --git a/tests/inc/classes/test-class-customizer.php b/tests/inc/classes/test-class-customizer.php index 588276e..69b75c8 100644 --- a/tests/inc/classes/test-class-customizer.php +++ b/tests/inc/classes/test-class-customizer.php @@ -60,7 +60,7 @@ public function test_construct() { 'type' => 'action', 'name' => 'customize_preview_init', 'priority' => 10, - 'function' => 'customize_preview_init', + 'function' => 'enqueue_customizer_scripts', ), ); From 448abbdcbd801adefe3b3ba30bc70dd1a720552b Mon Sep 17 00:00:00 2001 From: Deepak Lalwani Date: Fri, 10 Jul 2020 21:56:51 +0530 Subject: [PATCH 35/35] Fix unit test cases after syncing branch with master --- inc/classes/class-assets.php | 6 - tests/inc/classes/test-class-assets.php | 128 +++++++++--------- tests/inc/classes/test-class-blank-theme.php | 2 +- tests/inc/classes/test-class-customizer.php | 2 +- .../classes/test-class-infinite-scroll.php | 98 -------------- tests/inc/classes/test-class-widgets.php | 2 +- tests/inc/helpers/test-custom-functions.php | 124 ----------------- 7 files changed, 64 insertions(+), 298 deletions(-) delete mode 100644 tests/inc/classes/test-class-infinite-scroll.php delete mode 100644 tests/inc/helpers/test-custom-functions.php diff --git a/inc/classes/class-assets.php b/inc/classes/class-assets.php index d892fe8..546dd52 100644 --- a/inc/classes/class-assets.php +++ b/inc/classes/class-assets.php @@ -43,9 +43,6 @@ protected function setup_hooks() { * Register scripts. * * @action wp_enqueue_scripts - * - * Ignoring this because the asset file names are generated dynamically and fetched from manifest so getting issues with mocking. - * @codeCoverageIgnore */ public function register_scripts() { @@ -73,9 +70,6 @@ public function register_scripts() { * Register styles. * * @action wp_enqueue_scripts - * - * Ignoring this because the asset file names are generated dynamically and fetched from manifest so getting issues with mocking. - * @codeCoverageIgnore */ public function register_styles() { diff --git a/tests/inc/classes/test-class-assets.php b/tests/inc/classes/test-class-assets.php index 8dc5a72..e5ab26d 100644 --- a/tests/inc/classes/test-class-assets.php +++ b/tests/inc/classes/test-class-assets.php @@ -35,19 +35,12 @@ public function setUp() : void { parent::setUp(); $this->instance = Assets::get_instance(); switch_theme( 'blank-theme' ); - - $this->mock_post = $this->factory()->post->create_and_get(); - $GLOBALS['post'] = $this->mock_post; - } - - public function tearDown() { - unset( $GLOBALS['post'] ); } /** * Function to test hooks setup. * - * @covers ::_setup_hooks + * @covers ::setup_hooks * @covers ::__construct * * @return void @@ -62,90 +55,91 @@ public function test_setup_hooks() { } /** - * Tests `get_asset_file_path` function. - * - * @covers ::get_asset_file_path + * Tests script registration. * - * @return void + * @covers ::register_scripts + * @covers ::register_script */ - public function test_get_asset_file_path() { - $asset_path = array( - 'test' => 'test1', - ); + public function test_register_scripts() { - Utility::set_and_get_property( $this->instance, 'asset_paths', $asset_path ); + $this->instance->register_scripts(); - $expected_path = untrailingslashit( get_template_directory() ) . '/assets/build/test1'; - $this->assertEquals( $expected_path, $this->instance->get_asset_file_path( 'test' ) ); - } + $this->assertTrue( wp_script_is( 'blank-theme-main' ) ); - /** - * Tests `get_asset_file_uri` function. - * - * @covers ::get_asset_file_uri - * - * @return void - */ - public function test_get_asset_file_uri() { + Utility::mock_wp_query( + array(), + array( + 'is_front_page' => true, + 'is_home' => true, + ) + ); + + $this->instance->register_scripts(); - $asset_path = array( - 'test' => 'test1', + $this->assertTrue( wp_script_is( 'blank-theme-home' ) ); + + Utility::mock_wp_query( + array(), + array( + 'is_single' => true, + ) ); - Utility::set_and_get_property( $this->instance, 'asset_paths', $asset_path ); + $this->instance->register_scripts(); - $expected_path = untrailingslashit( get_template_directory_uri() ) . '/assets/build/test1'; - $this->assertEquals( $expected_path, $this->instance->get_asset_file_uri( 'test' ) ); + $this->assertTrue( wp_script_is( 'blank-theme-single' ) ); } /** - * Tests `get_asset_paths` function. - * - * @covers ::get_asset_paths + * Tests styles registration. * - * @return void + * @covers ::register_styles + * @covers ::register_style */ - public function test_get_asset_paths() { - $http_response = array( - 'body' => wp_json_encode( - array( - 'test1' => 'Test 1', - 'test2' => 'Test 2', - ) - ), - ); + public function test_register_styles() { - $this->mock_http_response( $http_response ); + $this->instance->register_styles(); - $expected_data = array( - 'test1' => 'Test 1', - 'test2' => 'Test 2', + $this->assertTrue( wp_style_is( 'blank-theme-main' ) ); + + Utility::mock_wp_query( + array(), + array( + 'is_front_page' => true, + 'is_home' => true, + ) ); - $this->assertEquals( $expected_data, $this->instance->get_asset_paths() ); + $this->instance->register_styles(); + + $this->assertTrue( wp_style_is( 'blank-theme-home' ) ); - $http_response = new \WP_Error(); + Utility::mock_wp_query( + array(), + array( + 'is_single' => true, + ) + ); - $this->mock_http_response( $http_response ); + $this->instance->register_styles(); - $this->assertFalse( $this->instance->get_asset_paths() ); + $this->assertTrue( wp_style_is( 'blank-theme-single' ) ); } /** - * Helper function to mock http response. + * Tests get_file_version function. * - * @param array $mocked_response An array of mocked response data. - * - * @return void + * @covers ::get_file_version */ - public function mock_http_response( $mocked_response ) { - add_filter( - 'pre_http_request', - function( $response, $args, $url ) use ( $mocked_response ) { - return $mocked_response; - }, - 10, - 3 - ); + public function test_get_file_version() { + + $this->assertEquals( '1.0' , $this->instance->get_file_version( 'non-existent.css', '1.0' ) ); + + $this->assertFalse( $this->instance->get_file_version( 'non-existent.css', false ) ); + + $file_path = sprintf( '%s/%s', BLANK_THEME_BUILD_DIR, 'css/main.css' ); + $expected = filemtime( $file_path ); + $this->assertEquals( $expected, $this->instance->get_file_version( 'css/main.css', false ) ); } + } diff --git a/tests/inc/classes/test-class-blank-theme.php b/tests/inc/classes/test-class-blank-theme.php index a9c7856..048ef8d 100644 --- a/tests/inc/classes/test-class-blank-theme.php +++ b/tests/inc/classes/test-class-blank-theme.php @@ -53,7 +53,7 @@ public function tearDown() { * Test constructor function. * * @covers ::__construct - * @covers ::_setup_hooks + * @covers ::setup_hooks * * @return void */ diff --git a/tests/inc/classes/test-class-customizer.php b/tests/inc/classes/test-class-customizer.php index 69b75c8..ea454fd 100644 --- a/tests/inc/classes/test-class-customizer.php +++ b/tests/inc/classes/test-class-customizer.php @@ -40,7 +40,7 @@ public function setUp(): void { * Tests class construct. * * @covers ::__construct - * @covers ::_setup_hooks + * @covers ::setup_hooks * * @return void */ diff --git a/tests/inc/classes/test-class-infinite-scroll.php b/tests/inc/classes/test-class-infinite-scroll.php deleted file mode 100644 index 679105d..0000000 --- a/tests/inc/classes/test-class-infinite-scroll.php +++ /dev/null @@ -1,98 +0,0 @@ -instance = new Infinite_Scroll(); - } - - /** - * Tests `setup_jetpack` function. - * - * @covers ::setup_jetpack - * - * @return void - */ - public function test_setup_jetpack() { - $this->instance->setup_jetpack(); - - $expected_data = get_theme_support( 'infinite-scroll' )[0]; - - $this->assertArrayHasKey( 'container', $expected_data ); - $this->assertEquals( 'main', $expected_data['container'] ); - - $this->assertArrayHasKey( 'render', $expected_data ); - - $this->assertArrayHasKey( 'footer', $expected_data ); - $this->assertEquals( 'page', $expected_data['footer'] ); - } - - /** - * Tests `render_callback` function. - * - * @covers ::render_callback - * - * @return void - */ - public function test_render_callback() { - - $post_ids = $this->factory->post->create_many( - 10, - array( - 'post_type' => 'post', - ) - ); - - Utility::mock_wp_query( - array( 'post_type' => 'post' ), - array() - ); - $output = Utility::buffer_and_return( array( $this->instance, 'render_callback' ) ); - - $this->assertNotEmpty( $output ); - $this->assertContains( ' 'post', - ), - array( - 'is_search' => true, - ) - ); - - $output = Utility::buffer_and_return( array( $this->instance, 'render_callback' ) ); - - $this->assertNotEmpty( $output ); - $this->assertContains( 'mock_post = $this->factory()->post->create_and_get(); - $GLOBALS['post'] = $this->mock_post; - } - - /** - * Reset global post after every test case. - * - * @return void - */ - public function tearDown() { - unset( $GLOBALS['post'] ); - } - - /** - * Tests `blank_theme_primary_classes` function. - * - * @covers ::blank_theme_primary_classes - * - * @return void - */ - public function test_blank_theme_primary_classes() { - - set_theme_mod( 'blank_theme_sidebar_position', 'left' ); - $expected = 'blank-theme-primary large-8 medium-8 small-12 cell column large-order-2 medium-order-2 small-order-1 test clearfix'; - $output = Utility::buffer_and_return( 'blank_theme_primary_classes', array( 'test' ) ); - $this->assertEquals( $expected, $output ); - - set_theme_mod( 'blank_theme_sidebar_position', 'right' ); - $expected = 'blank-theme-primary large-8 medium-8 small-12 cell column test clearfix'; - $output = Utility::buffer_and_return( 'blank_theme_primary_classes', array( 'test' ) ); - $this->assertEquals( $expected, $output ); - - set_theme_mod( 'blank_theme_sidebar_position', 'no_sidebar' ); - $expected = 'blank-theme-primary large-12 medium-12 small-12 cell column test clearfix'; - $output = Utility::buffer_and_return( 'blank_theme_primary_classes', array( 'test' ) ); - $this->assertEquals( $expected, $output ); - - set_theme_mod( 'blank_theme_sidebar_position', false ); - } - - /** - * Tests `blank_theme_secondary_classes` function. - * - * @covers ::blank_theme_secondary_classes - * - * @return void - */ - public function test_blank_theme_secondary_classes() { - - $expected = 'blank-theme-secondary widget-area large-4 medium-4 small-12 cell column test clearfix'; - $output = Utility::buffer_and_return( 'blank_theme_secondary_classes', array( 'test' ) ); - $this->assertEquals( $expected, $output ); - - set_theme_mod( 'blank_theme_sidebar_position', 'left' ); - $expected = 'blank-theme-secondary widget-area large-4 medium-4 small-12 cell column large-order-1 medium-order-1 small-order-2 test clearfix'; - $output = Utility::buffer_and_return( 'blank_theme_secondary_classes', array( 'test' ) ); - $this->assertEquals( $expected, $output ); - } - - /** - * Tests `blank_theme_get_font_url` function. - * - * @covers ::blank_theme_get_font_url - * - * @return void - */ - public function test_blank_theme_get_font_url() { - $expected = esc_url( get_template_directory_uri() . '/assets/fonts/test/path' ); - $output = Utility::buffer_and_return( 'blank_theme_get_font_url', array( '/test/path' ) ); - $this->assertEquals( $expected, $output ); - } - - /** - * Tests `blank_theme_pagination()` function. - * - * @covers ::blank_theme_pagination - * - * @return void - */ - public function test_blank_theme_pagination() { - - $post_ids = $this->factory->post->create_many( - 10, - array( - 'post_type' => 'post', - ) - ); - - Utility::mock_wp_query( - array( 'post_type' => 'post' ), - array() - ); - - $output = Utility::buffer_and_return( 'blank_theme_pagination' ); - $this->assertContains( 'nav', $output ); - $this->assertContains( 'page-numbers', $output ); - } - -}