From 6a1ffeb25acfc8015c976477e9e403f574e39a66 Mon Sep 17 00:00:00 2001 From: thingsym Date: Thu, 8 Dec 2022 16:27:30 +0900 Subject: [PATCH 01/12] ci: update github actions --- .github/workflows/wp-plugin-deploy.yml | 2 +- .github/workflows/wp-plugin-trunk.yml | 2 +- .github/workflows/wp-plugin-unit-test.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/wp-plugin-deploy.yml b/.github/workflows/wp-plugin-deploy.yml index 9dd7280..959ef2e 100644 --- a/.github/workflows/wp-plugin-deploy.yml +++ b/.github/workflows/wp-plugin-deploy.yml @@ -12,7 +12,7 @@ jobs: timeout-minutes: 30 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: WordPress Plugin Deploy uses: 10up/action-wordpress-plugin-deploy@stable diff --git a/.github/workflows/wp-plugin-trunk.yml b/.github/workflows/wp-plugin-trunk.yml index 8d39e50..43dab9a 100644 --- a/.github/workflows/wp-plugin-trunk.yml +++ b/.github/workflows/wp-plugin-trunk.yml @@ -12,7 +12,7 @@ jobs: timeout-minutes: 30 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: WordPress.org plugin asset/readme update uses: 10up/action-wordpress-plugin-asset-update@stable diff --git a/.github/workflows/wp-plugin-unit-test.yml b/.github/workflows/wp-plugin-unit-test.yml index f875bac..2317e9a 100644 --- a/.github/workflows/wp-plugin-unit-test.yml +++ b/.github/workflows/wp-plugin-unit-test.yml @@ -41,7 +41,7 @@ jobs: MYSQL_ROOT_PASSWORD: root steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup PHP v${{ matrix.php }} uses: shivammathur/setup-php@v2 From e02f91b271dac73cef2958d451ed7618bdcf10c8 Mon Sep 17 00:00:00 2001 From: thingsym Date: Thu, 8 Dec 2022 16:53:47 +0900 Subject: [PATCH 02/12] fix: fix composer scripts --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 3233c63..b402380 100644 --- a/composer.json +++ b/composer.json @@ -37,8 +37,8 @@ "post-install-cmd": "@phpcs:config-set", "post-update-cmd" : "@phpcs:config-set", "phpcs:config-set": "phpcs --config-set installed_paths vendor/wp-coding-standards/wpcs,vendor/phpcompatibility/php-compatibility,vendor/phpcompatibility/phpcompatibility-paragonie,vendor/phpcompatibility/phpcompatibility-wp", - "phpcs": "phpcs -p -s -v -n ./ --standard=./phpcs.ruleset.xml --extensions=php", - "phpcs:warning": "phpcs -p -s -v ./ --standard=./phpcs.ruleset.xml --extensions=php", + "phpcs": "phpcs -p -s -v ./ --standard=./phpcs.ruleset.xml --extensions=php", + "phpcs:error": "phpcs -p -s -v -n ./ --standard=./phpcs.ruleset.xml --extensions=php", "format:phpcbf": "phpcbf -p -s -v ./ --standard=./phpcs.ruleset.xml --extensions=php", "phpmd": "phpmd ./ text ./phpmd.ruleset.xml --suffixes php --exclude /languages,/tests,/vendor,/node_modules", "phpstan": "phpstan analyse", From 31a735b7f9144cfc1a8fc813064ae12cc7e2fb33 Mon Sep 17 00:00:00 2001 From: thingsym Date: Mon, 12 Dec 2022 19:04:30 +0900 Subject: [PATCH 03/12] refactor: imporve code with phpcs --- multi-device-switcher.php | 21 +++++++++++++-------- wp-cli.php | 38 +++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/multi-device-switcher.php b/multi-device-switcher.php index 48403a5..8d9fbfa 100644 --- a/multi-device-switcher.php +++ b/multi-device-switcher.php @@ -470,21 +470,25 @@ public function set_cookie_normal_theme() { * @since 1.0.0 */ public function session() { + // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['pc-switcher'] ) ) { - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized - setcookie( $this->cookie_name_pc_switcher, $_GET['pc-switcher'] ? '1' : '', 0, '/', '', is_ssl(), false ); + // phpcs:ignore WordPress.Security.NonceVerification.Recommended + setcookie( $this->cookie_name_pc_switcher, sanitize_text_field( wp_unslash( $_GET['pc-switcher'] ) ) ? '1' : '', 0, '/', '', is_ssl(), false ); if ( isset( $_SERVER['REQUEST_URI'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $uri = preg_replace( '/^(.+?)(\?.*)$/', '$1', $_SERVER['REQUEST_URI'] ); } + // phpcs:ignore WordPress.Security.NonceVerification.Recommended unset( $_GET['pc-switcher'] ); + // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! empty( $_GET ) ) { + // phpcs:ignore WordPress.Security.NonceVerification.Recommended $uri = $uri . '?' . http_build_query( $_GET ); } - wp_redirect( esc_url( $uri ) ); + wp_safe_redirect( esc_url( $uri ) ); exit; } } @@ -519,7 +523,7 @@ public function add_pc_switcher( $pc_switcher = 0 ) { ); } - $uri = is_ssl() ? 'https://' : 'http://'; + $uri = is_ssl() ? 'https://' : 'http://'; if ( isset( $_SERVER['HTTP_HOST'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized @@ -681,7 +685,8 @@ public function admin_enqueue_scripts( $hook_suffix = '' ) { 'multi-device-switcher-options', plugins_url() . '/multi-device-switcher/multi-device-switcher.js', array( 'jquery', 'jquery-ui-tabs' ), - $this->plugin_data['Version'] + $this->plugin_data['Version'], + false ); } @@ -821,7 +826,7 @@ public function plugin_action_links( $links = array() ) { * @since 1.8.1 */ public function plugin_metadata_links( $links, $file ) { - if ( $file == plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ) ) { + if ( $file === plugin_basename( __MULTI_DEVICE_SWITCHER_FILE__ ) ) { $links[] = '' . __( 'Become a sponsor', 'multi-device-switcher' ) . ''; } @@ -910,7 +915,7 @@ public function load_textdomain() { */ public function load_plugin_data() { if ( ! function_exists( 'get_plugin_data' ) ) { - require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); + require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $this->plugin_data = get_plugin_data( __MULTI_DEVICE_SWITCHER_FILE__ ); @@ -1321,7 +1326,7 @@ public function validate_options( $input ) { } if ( isset( $input['add_custom_switcher'] ) && ! empty( $input['custom_switcher'] ) && ! isset( $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] ) ) { - if ( ! in_array( $input['custom_switcher'], array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) ) + if ( ! in_array( $input['custom_switcher'], array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) && preg_match( '/^[A-Za-z0-9]{1,20}$/', $input['custom_switcher'] ) ) { $output[ 'custom_switcher_theme_' . $input['custom_switcher'] ] = 'None'; $output[ 'custom_switcher_userAgent_' . $input['custom_switcher'] ] = ''; diff --git a/wp-cli.php b/wp-cli.php index 1f80ec1..8f48fbc 100644 --- a/wp-cli.php +++ b/wp-cli.php @@ -132,7 +132,7 @@ public function theme( $args, $assoc_args ) { $options = get_option( $this->option_name ); if ( isset( $slug ) ) { - if ( '' == $slug || 'None' == $slug ) { + if ( '' === $slug || 'None' === $slug ) { $theme = 'None'; } } @@ -141,7 +141,7 @@ public function theme( $args, $assoc_args ) { $themes = wp_get_themes(); foreach ( $themes as $theme_slug => $header ) { $slug_table[ $header->get( 'Name' ) ] = $theme_slug; - if ( $slug == $theme_slug ) { + if ( $slug === $theme_slug ) { $theme = $header->get( 'Name' ); } } @@ -152,7 +152,7 @@ public function theme( $args, $assoc_args ) { if ( isset( $theme ) ) { $default_theme = wp_get_theme()->get( 'Name' ); - if ( $default_theme == $theme ) { + if ( $default_theme === $theme ) { WP_CLI::error( $theme . ' theme is in active' ); } @@ -160,8 +160,8 @@ public function theme( $args, $assoc_args ) { WP_CLI::error( $theme . ' theme is not installed' ); } - if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) ) ) { - if ( 'smart' == $name ) { + if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) { + if ( 'smart' === $name ) { $name = 'smartphone'; } $options[ 'theme_' . $name ] = $theme; @@ -180,8 +180,8 @@ public function theme( $args, $assoc_args ) { } } else { - if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) ) ) { - if ( 'smart' == $name ) { + if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) { + if ( 'smart' === $name ) { $name = 'smartphone'; } WP_CLI::success( $options[ 'theme_' . $name ] . ' | ' . $slug_table[ $options[ 'theme_' . $name ] ] ); @@ -224,8 +224,8 @@ public function useragent( $args, $assoc_args ) { $options = get_option( $this->option_name ); if ( isset( $useragent ) ) { - if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) ) ) { - if ( 'smartphone' == $name ) { + if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) { + if ( 'smartphone' === $name ) { $name = 'smart'; } $options[ 'userAgent_' . $name ] = $useragent; @@ -244,8 +244,8 @@ public function useragent( $args, $assoc_args ) { } } else { - if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) ) ) { - if ( 'smartphone' == $name ) { + if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) { + if ( 'smartphone' === $name ) { $name = 'smart'; } WP_CLI::success( $options[ 'userAgent_' . $name ] ); @@ -327,13 +327,13 @@ public function add( $args, $assoc_args ) { $themes = wp_get_themes(); foreach ( $themes as $theme_slug => $header ) { $slug_table[ $header->get( 'Name' ) ] = $theme_slug; - if ( ! isset( $assoc_args['theme'] ) && $slug == $theme_slug ) { + if ( ! isset( $assoc_args['theme'] ) && $slug === $theme_slug ) { $theme = $header->get( 'Name' ); } } $options = get_option( $this->option_name ); - if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) ) ) { + if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) { WP_CLI::error( 'Default Switcher can\'t add' ); } elseif ( isset( $options[ 'custom_switcher_theme_' . $name ] ) ) { @@ -341,7 +341,7 @@ public function add( $args, $assoc_args ) { } else { $default_theme = wp_get_theme()->get( 'Name' ); - if ( $default_theme == $theme ) { + if ( $default_theme === $theme ) { WP_CLI::error( $theme . ' theme is in active' ); } @@ -384,7 +384,7 @@ public function delete( $args, $assoc_args ) { $options = get_option( $this->option_name ); - if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ) ) ) { + if ( in_array( $name, array( 'smartphone', 'smart', 'tablet', 'mobile', 'game' ), true ) ) { WP_CLI::error( 'Default Switcher can\'t delete' ); } elseif ( isset( $options[ 'custom_switcher_theme_' . $name ] ) ) { @@ -424,12 +424,12 @@ public function pc_switcher( $args, $assoc_args ) { $options = get_option( $this->option_name ); - if ( 'on' == $flag ) { + if ( 'on' === $flag ) { $options['pc_switcher'] = 1; update_option( $this->option_name, $options ); WP_CLI::success( 'turn on PC Switcher' ); } - elseif ( 'off' == $flag ) { + elseif ( 'off' === $flag ) { $options['pc_switcher'] = 0; update_option( $this->option_name, $options ); WP_CLI::success( 'turn off PC Switcher' ); @@ -463,12 +463,12 @@ public function css( $args, $assoc_args ) { $options = get_option( $this->option_name ); - if ( 'on' == $flag ) { + if ( 'on' === $flag ) { $options['default_css'] = 1; update_option( $this->option_name, $options ); WP_CLI::success( 'turn on default CSS' ); } - elseif ( 'off' == $flag ) { + elseif ( 'off' === $flag ) { $options['default_css'] = 0; update_option( $this->option_name, $options ); WP_CLI::success( 'turn off default CSS' ); From f14e6a8cc8e3a52632d451cdbf735ddaaac2f86b Mon Sep 17 00:00:00 2001 From: thingsym Date: Mon, 12 Dec 2022 19:06:33 +0900 Subject: [PATCH 04/12] perf: add enqueue_styles method --- multi-device-switcher.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/multi-device-switcher.php b/multi-device-switcher.php index 8d9fbfa..91a17aa 100644 --- a/multi-device-switcher.php +++ b/multi-device-switcher.php @@ -493,6 +493,25 @@ public function session() { } } + /** + * Enqueue styles for default_css + * + * @access public + * + * @return void + * + * @since 1.8.5 + */ + public function enqueue_styles() { + wp_enqueue_style( + 'pc-switcher-options', + plugins_url() . '/multi-device-switcher/pc-switcher.css', + array(), + $this->plugin_data['Version'], + 'all' + ); + } + /** * Add pc switcher button. * @@ -514,13 +533,7 @@ public function add_pc_switcher( $pc_switcher = 0 ) { if ( $pc_switcher && $name && 'None' !== $name ) { if ( $options['default_css'] ) { - wp_enqueue_style( - 'pc-switcher-options', - plugins_url() . '/multi-device-switcher/pc-switcher.css', - array(), - $this->plugin_data['Version'], - 'all' - ); + $this->enqueue_styles(); } $uri = is_ssl() ? 'https://' : 'http://'; From 844786da3a0132c79e02ca0dbaa1ac8a4a173334 Mon Sep 17 00:00:00 2001 From: thingsym Date: Mon, 12 Dec 2022 19:07:31 +0900 Subject: [PATCH 05/12] chore: fix phpcs.ruleset.xml --- phpcs.ruleset.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/phpcs.ruleset.xml b/phpcs.ruleset.xml index 27423fc..68dde20 100755 --- a/phpcs.ruleset.xml +++ b/phpcs.ruleset.xml @@ -68,6 +68,7 @@ + From 7644c8d237b8535a5f2296c6ca5f19d5b65da3d2 Mon Sep 17 00:00:00 2001 From: thingsym Date: Mon, 12 Dec 2022 19:11:36 +0900 Subject: [PATCH 06/12] test: add test case --- tests/test-cdn.php | 2 +- tests/test-customizer.php | 32 +- tests/test-detect-useragent.php | 2 +- tests/test-functions-detect_device.php | 6 +- tests/test-functions-switch-theme.php | 2 +- tests/test-functions.php | 32 +- tests/test-pc-switcher-widget.php | 116 +++++- tests/test-pc-switcher.php | 507 +++++++++++++++++++++++++ tests/test-shortcode.php | 30 +- 9 files changed, 680 insertions(+), 49 deletions(-) create mode 100644 tests/test-pc-switcher.php diff --git a/tests/test-cdn.php b/tests/test-cdn.php index 03cc377..90530fb 100644 --- a/tests/test-cdn.php +++ b/tests/test-cdn.php @@ -79,7 +79,7 @@ function detect_device_action_hook_cloudfront() { $this->assertSame( 'tablet', $this->multi_device_switcher->device ); - unset($GLOBALS['_SERVER']['HTTP_USER_AGENT']); + unset( $GLOBALS['_SERVER']['HTTP_USER_AGENT'] ); } function _set_device_cloudfront() { diff --git a/tests/test-customizer.php b/tests/test-customizer.php index 94fe347..7157f06 100644 --- a/tests/test-customizer.php +++ b/tests/test-customizer.php @@ -102,7 +102,37 @@ function customizer() { * @group customizer */ function save_case() { - $this->markTestIncomplete( 'This test has not been implemented yet.' ); + $this->wp_customize->set_post_value( 'multi_device_switcher_options[theme_smartphone]', 'none' ); + $setting = $this->wp_customize->get_setting( 'multi_device_switcher_options[theme_smartphone]' ); + $setting->save(); + $this->assertSame( 'none', $setting->value() ); + + $option = $this->multi_device_switcher->get_options( 'theme_smartphone' ); + $this->assertSame( 'none', $option ); + + $this->wp_customize->set_post_value( 'multi_device_switcher_options[theme_tablet]', 'none' ); + $setting = $this->wp_customize->get_setting( 'multi_device_switcher_options[theme_tablet]' ); + $setting->save(); + $this->assertSame( 'none', $setting->value() ); + + $option = $this->multi_device_switcher->get_options( 'theme_tablet' ); + $this->assertSame( 'none', $option ); + + $this->wp_customize->set_post_value( 'multi_device_switcher_options[theme_mobile]', 'none' ); + $setting = $this->wp_customize->get_setting( 'multi_device_switcher_options[theme_mobile]' ); + $setting->save(); + $this->assertSame( 'none', $setting->value() ); + + $option = $this->multi_device_switcher->get_options( 'theme_mobile' ); + $this->assertSame( 'none', $option ); + + $this->wp_customize->set_post_value( 'multi_device_switcher_options[theme_game]', 'none' ); + $setting = $this->wp_customize->get_setting( 'multi_device_switcher_options[theme_game]' ); + $setting->save(); + $this->assertSame( 'none', $setting->value() ); + + $option = $this->multi_device_switcher->get_options( 'theme_game' ); + $this->assertSame( 'none', $option ); } } diff --git a/tests/test-detect-useragent.php b/tests/test-detect-useragent.php index 30eecfb..c9fa760 100644 --- a/tests/test-detect-useragent.php +++ b/tests/test-detect-useragent.php @@ -240,7 +240,7 @@ function detect_useragent() { $this->assertSame( $case[ 'device' ], $this->multi_device_switcher->device, $case[ 'massage' ] ); $this->multi_device_switcher->device = ''; - unset($GLOBALS['_SERVER']['HTTP_USER_AGENT']); + unset( $GLOBALS['_SERVER']['HTTP_USER_AGENT'] ); } } diff --git a/tests/test-functions-detect_device.php b/tests/test-functions-detect_device.php index 6c45d4a..5327d26 100644 --- a/tests/test-functions-detect_device.php +++ b/tests/test-functions-detect_device.php @@ -65,7 +65,7 @@ function detect_device_match_device() { $this->assertFalse( has_filter( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_rest_disable_switcher' ) ) ); $this->assertFalse( has_filter( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_enable_disable_switcher' ) ) ); - unset($GLOBALS['_SERVER']['HTTP_USER_AGENT']); + unset( $GLOBALS['_SERVER']['HTTP_USER_AGENT'] ); } /** @@ -75,12 +75,12 @@ function detect_device_match_device() { function detect_device_enable_disable_switcher() { $GLOBALS['_COOKIE']['disable-switcher'] = 1; $this->multi_device_switcher->detect_device(); + unset( $GLOBALS['_COOKIE']['disable-switcher'] ); $this->assertSame( 10, has_action( 'init', array( $this->multi_device_switcher, 'session' ) ) ); $this->assertSame( 10, has_filter( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_rest_disable_switcher' ) ) ); $this->assertFalse( has_filter( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_enable_disable_switcher' ) ) ); - unset($GLOBALS['_COOKIE']['disable-switcher']); } /** @@ -114,7 +114,7 @@ function detect_device_match_disable_switcher() { $this->assertFalse( has_filter( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_rest_disable_switcher' ) ) ); $this->assertSame( 10, has_filter( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_enable_disable_switcher' ) ) ); - unset($GLOBALS['_SERVER']['REQUEST_URI']); + unset($GLOBALS['_SERVER']['REQUEST_URI'] ); } /** diff --git a/tests/test-functions-switch-theme.php b/tests/test-functions-switch-theme.php index e6c3bcd..35bd95f 100644 --- a/tests/test-functions-switch-theme.php +++ b/tests/test-functions-switch-theme.php @@ -78,6 +78,7 @@ function switch_theme_enable_pc_switcher() { $this->multi_device_switcher->device = 'smart'; $GLOBALS['_COOKIE']['pc-switcher'] = 1; $this->multi_device_switcher->switch_theme(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); $this->assertFalse( has_filter( 'stylesheet', array( $this->multi_device_switcher, 'get_stylesheet' ) ) ); $this->assertFalse( has_filter( 'template', array( $this->multi_device_switcher, 'get_template' ) ) ); @@ -85,7 +86,6 @@ function switch_theme_enable_pc_switcher() { $this->assertSame( 10, has_action( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_switch_theme' ) ) ); $this->assertfalse( has_action( 'wp_headers', array( $this->multi_device_switcher, 'set_cookie_normal_theme' ) ) ); - unset($GLOBALS['_COOKIE']['pc-switcher']); } } diff --git a/tests/test-functions.php b/tests/test-functions.php index 68b3ab9..78d472d 100644 --- a/tests/test-functions.php +++ b/tests/test-functions.php @@ -27,7 +27,6 @@ public function setUp() { update_option( 'multi_device_switcher_options', $options ); } - function tearDown() { parent::tearDown(); } @@ -103,18 +102,6 @@ function session() { // $this->assertSame( '', $this->multi_device_switcher->session() ); } - /** - * @test - * @group functions - */ - function add_pc_switcher() { - $this->markTestIncomplete( 'This test has not been implemented yet.' ); - - // $this->multi_device_switcher->device = 'smart'; - // $this->assertSame( '', $this->multi_device_switcher->add_pc_switcher() ); - // $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); - } - /** * @test * @group functions @@ -139,7 +126,7 @@ function is_pc_switcher() { $GLOBALS['_COOKIE']['pc-switcher'] = 1; $this->assertTrue( $this->multi_device_switcher->is_pc_switcher() ); - unset($GLOBALS['_COOKIE']['pc-switcher']); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); } /** @@ -199,7 +186,7 @@ function is_disable_switcher() { $GLOBALS['_SERVER']['REQUEST_URI'] = '/abc'; $this->assertTrue( $this->multi_device_switcher->is_disable_switcher() ); - unset($GLOBALS['_SERVER']['REQUEST_URI']); + unset( $GLOBALS['_SERVER']['REQUEST_URI'] ); } /** @@ -233,7 +220,7 @@ function shortcode_display_switcher() { ); $content = "test"; $this->assertSame( '', $this->multi_device_switcher->shortcode_display_switcher( $atts, $content ) ); - unset($GLOBALS['_COOKIE']['pc-switcher']); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); } /** @@ -277,8 +264,17 @@ function admin_enqueue() { * @test * @group functions */ - function admin_init() { - $this->markTestIncomplete( 'This test has not been implemented yet.' ); + function register_settings() { + $this->multi_device_switcher->register_settings(); + + global $wp_registered_settings; + global $wp_settings_sections; + global $wp_settings_fields; + + $this->assertTrue( isset( $wp_registered_settings['multi_device_switcher_options'] ) ); + $this->assertSame( 'multi_device_switcher', $wp_registered_settings['multi_device_switcher_options']['group'] ); + $this->assertTrue( in_array( $this->multi_device_switcher, $wp_registered_settings['multi_device_switcher_options']['sanitize_callback'] ) ); + $this->assertTrue( in_array( 'validate_options', $wp_registered_settings['multi_device_switcher_options']['sanitize_callback'] ) ); } /** diff --git a/tests/test-pc-switcher-widget.php b/tests/test-pc-switcher-widget.php index 2463aab..576614b 100644 --- a/tests/test-pc-switcher-widget.php +++ b/tests/test-pc-switcher-widget.php @@ -4,9 +4,44 @@ class Test_pc_switcher_Pc_Switcher_Widget extends WP_UnitTestCase { public function setUp() { parent::setUp(); + + $this->multi_device_switcher = new Multi_Device_Switcher(); + + $options = array( + 'pc_switcher' => 1, + 'default_css' => 1, + 'theme_smartphone' => 'Twenty Sixteen', + 'theme_tablet' => 'Twenty Sixteen', + 'theme_mobile' => 'None', + 'theme_game' => 'None', + 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko', + 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko', + 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia', + 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox', + 'disable_path' => '', + 'enable_regex' => 0, + 'custom_switcher_theme_test' => 'Twenty Sixteen', + 'custom_switcher_userAgent_test' => 'test1,test2', + ); + + update_option( 'multi_device_switcher_options', $options ); + + $this->multi_device_switcher->load_plugin_data(); + $GLOBALS['_SERVER']['REQUEST_URI'] = ''; + $this->pc_switcher = new PC_Switcher(); } + function tearDown() { + parent::tearDown(); + + $GLOBALS['_SERVER']['REQUEST_URI'] = ''; + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + global $multi_device_switcher; + $multi_device_switcher->device = ''; + } + /** * @test * @group widget @@ -50,24 +85,95 @@ function widget() { ); $instance = array(); + ob_start(); + $this->pc_switcher->widget( $args, $instance ); + $widget = ob_get_clean(); + + $this->assertSame( '', $widget ); + + global $multi_device_switcher; + $multi_device_switcher->device = 'smart'; + + ob_start(); + $this->pc_switcher->widget( $args, $instance ); + $widget = ob_get_clean(); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=1' ) . '/', $widget ); + $this->assertRegExp( '/' . preg_quote( 'class="active">Mobile' ) . '/', $widget ); + $GLOBALS['_COOKIE']['pc-switcher'] = 1; - // setcookie( 'pc-switcher', 1 ); ob_start(); $this->pc_switcher->widget( $args, $instance ); $widget = ob_get_clean(); - // var_dump($widget); - // $this->assertMatchesRegularExpression( '#

aaaaa

#', $widget ); + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=0' ) . '/', $widget ); + $this->assertRegExp( '/' . preg_quote( 'class="active">PC' ) . '/', $widget ); + } - $this->markTestIncomplete( 'This test has not been implemented yet.' ); + /** + * @test + * @group widget + */ + function widget_switcher_off() { + $options = array( + 'pc_switcher' => 0, + 'default_css' => 1, + 'theme_smartphone' => 'Twenty Sixteen', + 'theme_tablet' => 'Twenty Sixteen', + 'theme_mobile' => 'None', + 'theme_game' => 'None', + 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko', + 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko', + 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia', + 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox', + 'disable_path' => '', + 'enable_regex' => 0, + 'custom_switcher_theme_test' => 'Twenty Sixteen', + 'custom_switcher_userAgent_test' => 'test1,test2', + ); + + update_option( 'multi_device_switcher_options', $options ); + + $args = array( + 'before_widget' => '', + 'before_title' => '

', + 'after_title' => '

', + ); + $instance = array(); + + ob_start(); + $this->pc_switcher->widget( $args, $instance ); + $widget = ob_get_clean(); + + $this->assertSame( '', $widget ); + + global $multi_device_switcher; + $multi_device_switcher->device = 'smart'; + + ob_start(); + $this->pc_switcher->widget( $args, $instance ); + $widget = ob_get_clean(); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=1' ) . '/', $widget ); + $this->assertRegExp( '/' . preg_quote( 'class="active">Mobile' ) . '/', $widget ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + + ob_start(); + $this->pc_switcher->widget( $args, $instance ); + $widget = ob_get_clean(); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=0' ) . '/', $widget ); + $this->assertRegExp( '/' . preg_quote( 'class="active">PC' ) . '/', $widget ); } /** * @test * @group widget */ - function update_case() { + function update() { $new_instance = array(); $expected = array(); diff --git a/tests/test-pc-switcher.php b/tests/test-pc-switcher.php new file mode 100644 index 0000000..d394010 --- /dev/null +++ b/tests/test-pc-switcher.php @@ -0,0 +1,507 @@ +multi_device_switcher = new Multi_Device_Switcher(); + + $options = array( + 'pc_switcher' => 1, + 'default_css' => 1, + 'theme_smartphone' => 'Twenty Sixteen', + 'theme_tablet' => 'Twenty Sixteen', + 'theme_mobile' => 'None', + 'theme_game' => 'None', + 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko', + 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko', + 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia', + 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox', + 'disable_path' => '', + 'enable_regex' => 0, + 'custom_switcher_theme_test' => 'Twenty Sixteen', + 'custom_switcher_userAgent_test' => 'test1,test2', + ); + + update_option( 'multi_device_switcher_options', $options ); + + $this->multi_device_switcher->load_plugin_data(); + $GLOBALS['_SERVER']['REQUEST_URI'] = ''; + } + + function tearDown() { + parent::tearDown(); + + $GLOBALS['_SERVER']['REQUEST_URI'] = ''; + wp_dequeue_style( 'pc-switcher-options' ); + $this->multi_device_switcher->device = ''; + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + } + + /** + * @test + * @group pc_switcher + */ + function enqueue_styles() { + $this->multi_device_switcher->enqueue_styles(); + $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); + } + + /** + * @test + * @group pc_switcher + */ + function add_pc_switcher_case_no_switching() { + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + } + + /** + * @test + * @group pc_switcher + */ + function add_pc_switcher_case_switching() { + $this->multi_device_switcher->device = 'smart'; + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=1' ) . '/', $result ); + $this->assertRegExp( '/' . preg_quote( 'class="active">Mobile' ) . '/', $result ); + $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=1' ) . '/', $result ); + $this->assertRegExp( '/' . preg_quote( 'class="active">Mobile' ) . '/', $result ); + $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=1' ) . '/', $result ); + $this->assertRegExp( '/' . preg_quote( 'class="active">Mobile' ) . '/', $result ); + $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=0' ) . '/', $result ); + $this->assertRegExp( '/' . preg_quote( 'class="active">PC' ) . '/', $result ); + $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=1' ) . '/', $result ); + $this->assertRegExp( '/' . preg_quote( 'class="active">Mobile' ) . '/', $result ); + $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=1' ) . '/', $result ); + $this->assertRegExp( '/' . preg_quote( 'class="active">Mobile' ) . '/', $result ); + $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); + } + + /** + * @test + * @group pc_switcher + */ + function add_pc_switcher_case_no_switching_with_switcher_off() { + $options = array( + 'pc_switcher' => 0, + 'default_css' => 1, + 'theme_smartphone' => 'Twenty Sixteen', + 'theme_tablet' => 'Twenty Sixteen', + 'theme_mobile' => 'None', + 'theme_game' => 'None', + 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko', + 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko', + 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia', + 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox', + 'disable_path' => '', + 'enable_regex' => 0, + 'custom_switcher_theme_test' => 'Twenty Sixteen', + 'custom_switcher_userAgent_test' => 'test1,test2', + ); + + update_option( 'multi_device_switcher_options', $options ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + } + + /** + * @test + * @group pc_switcher + */ + function add_pc_switcher_case_switching_with_switcher_off() { + $this->multi_device_switcher->device = 'smart'; + + $options = array( + 'pc_switcher' => 0, + 'default_css' => 1, + 'theme_smartphone' => 'Twenty Sixteen', + 'theme_tablet' => 'Twenty Sixteen', + 'theme_mobile' => 'None', + 'theme_game' => 'None', + 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko', + 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko', + 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia', + 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox', + 'disable_path' => '', + 'enable_regex' => 0, + 'custom_switcher_theme_test' => 'Twenty Sixteen', + 'custom_switcher_userAgent_test' => 'test1,test2', + ); + + update_option( 'multi_device_switcher_options', $options ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + + $this->assertSame( '', $result ); + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + wp_dequeue_style( 'pc-switcher-options' ); + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + + $this->assertRegExp( '/' . preg_quote( 'pc-switcher=1' ) . '/', $result ); + $this->assertRegExp( '/' . preg_quote( 'class="active">Mobile' ) . '/', $result ); + $this->assertTrue( wp_style_is( 'pc-switcher-options' ) ); + } + + /** + * @test + * @group pc_switcher + */ + function add_pc_switcher_case_with_default_css_off() { + $options = array( + 'pc_switcher' => 1, + 'default_css' => 0, + 'theme_smartphone' => 'Twenty Sixteen', + 'theme_tablet' => 'Twenty Sixteen', + 'theme_mobile' => 'None', + 'theme_game' => 'None', + 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko', + 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko', + 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia', + 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox', + 'disable_path' => '', + 'enable_regex' => 0, + 'custom_switcher_theme_test' => 'Twenty Sixteen', + 'custom_switcher_userAgent_test' => 'test1,test2', + ); + + update_option( 'multi_device_switcher_options', $options ); + + wp_dequeue_style( 'pc-switcher-options' ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + + $this->multi_device_switcher->device = 'smart'; + + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + } + + /** + * @test + * @group pc_switcher + */ + function add_pc_switcher_case_with_switcher_off_and_default_css_off() { + $options = array( + 'pc_switcher' => 0, + 'default_css' => 0, + 'theme_smartphone' => 'Twenty Sixteen', + 'theme_tablet' => 'Twenty Sixteen', + 'theme_mobile' => 'None', + 'theme_game' => 'None', + 'userAgent_smart' => 'iPhone, iPod, Android.*Mobile, dream, CUPCAKE, Windows Phone, IEMobile.*Touch, webOS, BB10.*Mobile, BlackBerry.*Mobile, Mobile.*Gecko', + 'userAgent_tablet' => 'iPad, Kindle, Silk, Android(?!.*Mobile), Windows.*Touch, PlayBook, Tablet.*Gecko', + 'userAgent_mobile' => 'DoCoMo, SoftBank, J-PHONE, Vodafone, KDDI, UP.Browser, WILLCOM, emobile, DDIPOCKET, Windows CE, BlackBerry, Symbian, PalmOS, Huawei, IAC, Nokia', + 'userAgent_game' => 'PSP, PS2, PLAYSTATION 3, PlayStation (Portable|Vita|4|5), Nitro, Nintendo (3DS|Wii|WiiU|Switch), Xbox', + 'disable_path' => '', + 'enable_regex' => 0, + 'custom_switcher_theme_test' => 'Twenty Sixteen', + 'custom_switcher_userAgent_test' => 'test1,test2', + ); + + update_option( 'multi_device_switcher_options', $options ); + + wp_dequeue_style( 'pc-switcher-options' ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + + $this->multi_device_switcher->device = 'smart'; + + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher(); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 0 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + + $GLOBALS['_COOKIE']['pc-switcher'] = 1; + ob_start(); + $this->multi_device_switcher->add_pc_switcher( 1 ); + $result = ob_get_clean(); + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + + $this->assertFalse( wp_style_is( 'pc-switcher-options' ) ); + } + +} diff --git a/tests/test-shortcode.php b/tests/test-shortcode.php index 954f52f..01113f4 100644 --- a/tests/test-shortcode.php +++ b/tests/test-shortcode.php @@ -29,6 +29,10 @@ public function setUp() { function tearDown() { parent::tearDown(); + + $GLOBALS['_SERVER']['REQUEST_URI'] = ''; + unset( $GLOBALS['_COOKIE']['pc-switcher'] ); + } /** @@ -46,34 +50,22 @@ function is_shortcode() { function shortcode_output_multi() { $output = do_shortcode('[multi]pc[/multi]'); $content = 'pc'; - $this->assertSame($content, $output); + $this->assertSame( $content, $output ); - // $GLOBALS['_COOKIE']['pc-switcher'] = 1; // $this->multi_device_switcher->device = 'smart'; + // $GLOBALS['_COOKIE']['pc-switcher'] = 1; + + // global $multi_device_switcher; + // $multi_device_switcher->device = 'smart'; // var_dump($this->multi_device_switcher->device); - // var_dump($this->multi_device_switcher->is_multi_device( "smart" )); + // var_dump($this->multi_device_switcher->is_multi_device( 'smart' )); // var_dump($this->multi_device_switcher->is_pc_switcher()); // $output = do_shortcode('[multi device="smart"]smart[/multi]'); // $content = 'smart'; - // $this->assertSame($content, $output); - - - // $this->multi_device_switcher->device = 'custom_switcher_test'; - // var_dump($this->multi_device_switcher->device); - // // var_dump($this->multi_device_switcher->is_multi_device( "test" )); - // // var_dump($this->multi_device_switcher->is_pc_switcher()); - // $output = do_shortcode('[multi device="test"]test[/multi]'); - // $expected = 'test'; - // $this->assertSame($expected, $output); - - $GLOBALS['_COOKIE']['pc-switcher'] = 1; - unset($GLOBALS['_COOKIE']['pc-switcher']); + // $this->assertSame( $content, $output ); } } - -# customized CSS -# Display Switcher From 4496ca9b85b96e836a68c729bf817c9250ad7dcc Mon Sep 17 00:00:00 2001 From: thingsym Date: Mon, 12 Dec 2022 19:12:00 +0900 Subject: [PATCH 07/12] chore: fix .editorconfig --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index d03caaf..5f85f99 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,7 +11,7 @@ trim_trailing_whitespace = true indent_style = tab indent_size = 2 -[*.{json,yml,xml,neon}] +[*.{json,yml,xml,neon,bats}] indent_style = space [*.md] From f7531450b2ac0a830e20129078198791e9010f12 Mon Sep 17 00:00:00 2001 From: thingsym Date: Mon, 12 Dec 2022 19:13:07 +0900 Subject: [PATCH 08/12] test: fix bats test case --- tests/cli/error.bats | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/cli/error.bats b/tests/cli/error.bats index 8b56c88..4338c55 100755 --- a/tests/cli/error.bats +++ b/tests/cli/error.bats @@ -13,6 +13,8 @@ setup() { } @test "switch theme - fail" { + wp theme activate twentynineteen + run wp multi-device theme smartphone twentynineteen assert_failure @@ -102,6 +104,8 @@ setup() { assert_status 1 assert_equal "Error: Custom Switcher already exists" + wp theme activate twentynineteen + run wp multi-device delete testtest run wp multi-device add testtest twentynineteen "iPad, Kindle, Sony Tablet, Nexus 7" From 37eb9d637a2cc3db6c6cd232e4b6d4984507638d Mon Sep 17 00:00:00 2001 From: thingsym Date: Tue, 13 Dec 2022 17:30:46 +0900 Subject: [PATCH 09/12] ci: add bats test --- .github/workflows/bats-test.yml | 89 +++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 .github/workflows/bats-test.yml diff --git a/.github/workflows/bats-test.yml b/.github/workflows/bats-test.yml new file mode 100644 index 0000000..b249c10 --- /dev/null +++ b/.github/workflows/bats-test.yml @@ -0,0 +1,89 @@ +name: Bats test + +on: + push: + branches: + - '**' + - '!svn-trunk' + - '!test-matrix' + tags-ignore: + - '**' + pull_request: + branches: + - '**' + +jobs: + bats: + name: WordPress ${{ matrix.wordpress }} * PHP v${{ matrix.php }} * ${{ matrix.os }} * Multisite ${{ matrix.multisite }} + runs-on: ${{ matrix.os }} + timeout-minutes: 30 + + strategy: + fail-fast: false + + matrix: + os: [ ubuntu-20.04 ] + php: [ '7.4' ] + wordpress: [ latest ] + multisite: [ 0 ] + + env: + PHP_VERSION: ${{ matrix.php }} + WP_VERSION: ${{ matrix.wordpress }} + WP_MULTISITE: ${{ matrix.multisite }} + + services: + database: + image: mysql:5.7 + ports: + - 3306:3306 + env: + MYSQL_ROOT_PASSWORD: root + options: --health-cmd "mysqladmin ping -h 0.0.0.0" --health-interval 20s --health-timeout 10s --health-retries 10 + + steps: + - name: Setup PHP v${{ matrix.php }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: mbstring, intl, php-mysql + + - name: Install WP-CLI + run: | + curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar + chmod +x wp-cli.phar + mv wp-cli.phar /usr/local/bin/wp + + - name: chown to runner:docker + run: sudo chown -R runner:docker /var/www/html + + - name: Install WordPress + run: | + wp core download --version=${{ matrix.wordpress }} + wp config create --dbhost=0.0.0.0:3306 --dbname=wordpress_test --dbuser=root --dbpass=root --force + wp db create --allow-root + wp core install --url=example.com --title=Example --admin_user=admin --admin_password=admin --admin_email=admin@example.com + working-directory: /var/www/html + + - name: Install default WordPress theme + run: | + wp theme install twentysixteen + wp theme install twentyfifteen + wp theme install twentynineteen + working-directory: /var/www/html + + - uses: actions/checkout@v3 + + - name: Move plugin + run: cp -r /home/runner/work/multi-device-switcher/multi-device-switcher /var/www/html/wp-content/plugins/multi-device-switcher + + - name: Active Plugin + run: wp plugin activate multi-device-switcher + working-directory: /var/www/html + + - name: Setup Bats + uses: mig4/setup-bats@v1 + + - name: Run Bats test + run: bats tests/cli + working-directory: /var/www/html/wp-content/plugins/multi-device-switcher From 4b4c146182617a9c8086e08a95e50c96f810e639 Mon Sep 17 00:00:00 2001 From: thingsym Date: Tue, 13 Dec 2022 17:35:59 +0900 Subject: [PATCH 10/12] ci: add healthcheck to database container --- .github/workflows/wp-plugin-unit-test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/wp-plugin-unit-test.yml b/.github/workflows/wp-plugin-unit-test.yml index 2317e9a..db6dc67 100644 --- a/.github/workflows/wp-plugin-unit-test.yml +++ b/.github/workflows/wp-plugin-unit-test.yml @@ -39,6 +39,7 @@ jobs: - 3306:3306 env: MYSQL_ROOT_PASSWORD: root + options: --health-cmd "mysqladmin ping -h 0.0.0.0" --health-interval 20s --health-timeout 10s --health-retries 10 steps: - uses: actions/checkout@v3 From f3b7a6472b78398bdf1b39eb4c300b9fe603fe7f Mon Sep 17 00:00:00 2001 From: thingsym Date: Tue, 13 Dec 2022 19:26:56 +0900 Subject: [PATCH 11/12] ci: add phpcs job --- .github/workflows/wp-plugin-unit-test.yml | 42 +++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/.github/workflows/wp-plugin-unit-test.yml b/.github/workflows/wp-plugin-unit-test.yml index db6dc67..8dcf5e0 100644 --- a/.github/workflows/wp-plugin-unit-test.yml +++ b/.github/workflows/wp-plugin-unit-test.yml @@ -88,3 +88,45 @@ jobs: - name: Run PHPUnit - WordPress ${{ matrix.wordpress }} * Multisite ${{ matrix.multisite }} run: phpunit + + phpcs: + name: PHP CodeSniffer + runs-on: ${{ matrix.os }} + timeout-minutes: 30 + + strategy: + fail-fast: false + + matrix: + os: [ ubuntu-20.04 ] + php: [ '7.4' ] + wordpress: [ latest ] + multisite: [ 0 ] + + env: + PHP_VERSION: ${{ matrix.php }} + WP_VERSION: ${{ matrix.wordpress }} + WP_MULTISITE: ${{ matrix.multisite }} + + steps: + - uses: actions/checkout@v3 + + - name: Setup PHP v${{ matrix.php }} + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: mbstring, intl, php-mysql + + - name: Install php_codesniffer library and PHP_CodeSniffer rules + run: | + composer global require "squizlabs/php_codesniffer=3.*" + composer global require "wp-coding-standards/wpcs=2.*" + composer global require "phpcompatibility/phpcompatibility-wp=*" + + - name: phpcs --config-set + run: | + phpcs --config-set installed_paths /home/runner/.composer/vendor/wp-coding-standards/wpcs,/home/runner/.composer/vendor/phpcompatibility/php-compatibility,/home/runner/.composer/vendor/phpcompatibility/phpcompatibility-paragonie,/home/runner/.composer/vendor/phpcompatibility/phpcompatibility-wp + phpcs -i + + - name: Run phpcs + run: phpcs -p -s -v ./ --standard=./phpcs.ruleset.xml --extensions=php From 3ae23bd2ac2be1c15ad14590da290cdd3ef0f587 Mon Sep 17 00:00:00 2001 From: thingsym Date: Tue, 13 Dec 2022 19:45:52 +0900 Subject: [PATCH 12/12] Version 1.8.5 --- README.md | 11 +++++++++++ composer.json | 2 +- languages/multi-device-switcher-ja.po | 2 +- languages/multi-device-switcher.pot | 2 +- multi-device-switcher.php | 2 +- pc-switcher-widget.php | 2 +- readme.txt | 14 +++++++++++++- 7 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bb4376d..8eb01b6 100644 --- a/README.md +++ b/README.md @@ -437,6 +437,17 @@ For operation compatibility between PHP version and WordPress version, see below ## Changelog +* Version 1.8.5 + * add phpcs job to ci + * add healthcheck to database container + * add bats test to ci + * fix bats test case + * add test case + * fix phpcs.ruleset.xml + * add enqueue_styles method + * imporve code with phpcs + * fix composer scripts + * update github actions * Version 1.8.4 * tested up to 6.1.0 * update japanese translation diff --git a/composer.json b/composer.json index b402380..6f3c44c 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "thingsym/multi-device-switcher", - "version": "1.8.4", + "version": "1.8.5", "description": "This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game and custom).", "author": [ { diff --git a/languages/multi-device-switcher-ja.po b/languages/multi-device-switcher-ja.po index 4c8582d..402937a 100644 --- a/languages/multi-device-switcher-ja.po +++ b/languages/multi-device-switcher-ja.po @@ -2,7 +2,7 @@ # This file is distributed under the GPLv2 or later. msgid "" msgstr "" -"Project-Id-Version: Multi Device Switcher 1.8.4\n" +"Project-Id-Version: Multi Device Switcher 1.8.5\n" "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/multi-device-" "switcher\n" "POT-Creation-Date: 2022-11-25T10:39:41+00:00\n" diff --git a/languages/multi-device-switcher.pot b/languages/multi-device-switcher.pot index ffc4a6b..3f9e0fb 100644 --- a/languages/multi-device-switcher.pot +++ b/languages/multi-device-switcher.pot @@ -2,7 +2,7 @@ # This file is distributed under the GPLv2 or later. msgid "" msgstr "" -"Project-Id-Version: Multi Device Switcher 1.8.4\n" +"Project-Id-Version: Multi Device Switcher 1.8.5\n" "Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/multi-device-switcher\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/multi-device-switcher.php b/multi-device-switcher.php index 91a17aa..53146b2 100644 --- a/multi-device-switcher.php +++ b/multi-device-switcher.php @@ -3,7 +3,7 @@ * Plugin Name: Multi Device Switcher * Plugin URI: https://github.com/thingsym/multi-device-switcher * Description: This WordPress plugin allows you to set a separate theme for device (Smart Phone, Tablet PC, Mobile Phone, Game and custom). - * Version: 1.8.4 + * Version: 1.8.5 * Author: thingsym * Author URI: https://www.thingslabo.com/ * License: GPLv2 or later diff --git a/pc-switcher-widget.php b/pc-switcher-widget.php index 1e154bf..e48c943 100644 --- a/pc-switcher-widget.php +++ b/pc-switcher-widget.php @@ -3,7 +3,7 @@ * Widget Name: PC Switcher Widget * Plugin URI: https://github.com/thingsym/multi-device-switcher * Description: PC Switcher Widget add-on for the Multi Device Switcher. Use this widget to add the PC Switcher to a widget. - * Version: 1.8.4 + * Version: 1.8.5 * Author: thingsym * Author URI: https://www.thingslabo.com/ * License: GPLv2 or later diff --git a/readme.txt b/readme.txt index 6b8e9a6..946d8c0 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Contributors: thingsym Link: https://github.com/thingsym/multi-device-switcher Donate link: https://github.com/sponsors/thingsym Tags: switcher, theme, ipad, iphone, android, tablet, mobile, game -Stable tag: 1.8.4 +Stable tag: 1.8.5 Tested up to: 6.1.0 Requires at least: 4.9 Requires PHP: 5.6 @@ -395,6 +395,18 @@ For more information about the Multi Device Switcher Command, see `wp help multi == Changelog == += 1.8.5 = +* add phpcs job to ci +* add healthcheck to database container +* add bats test to ci +* fix bats test case +* add test case +* fix phpcs.ruleset.xml +* add enqueue_styles method +* imporve code with phpcs +* fix composer scripts +* update github actions + = 1.8.4 = * tested up to 6.1.0 * update japanese translation