From b9ceabaa5229d144051eba7b26e64671b928b037 Mon Sep 17 00:00:00 2001 From: Christopher Allford Date: Tue, 4 Feb 2020 15:50:23 -0800 Subject: [PATCH] Utilized the constants package to fix the failing tests --- .../gateways/cod/class-wc-gateway-cod.php | 27 +++++++------------ tests/unit-tests/payment-gateways/cod.php | 25 ++++++++++++----- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/includes/gateways/cod/class-wc-gateway-cod.php b/includes/gateways/cod/class-wc-gateway-cod.php index 5b42c0d8f4c..8dbc98a0a1b 100644 --- a/includes/gateways/cod/class-wc-gateway-cod.php +++ b/includes/gateways/cod/class-wc-gateway-cod.php @@ -5,6 +5,8 @@ * @package WooCommerce\Gateways */ +use Automattic\Jetpack\Constants; + if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } @@ -218,29 +220,19 @@ class WC_Gateway_COD extends WC_Payment_Gateway { * @return bool */ private function is_accessing_settings() { - // phpcs:disable WordPress.Security.NonceVerification.Recommended - $is_admin = is_admin(); - if ( $is_admin && ( ! isset( $_REQUEST['page'] ) || 'wc-settings' !== $_REQUEST['page'] ) ) { - $is_admin = false; - } - if ( $is_admin && ( ! isset( $_REQUEST['tab'] ) || 'checkout' !== $_REQUEST['tab'] ) ) { - $is_admin = false; - } - if ( $is_admin && ( ! isset( $_REQUEST['section'] ) || 'cod' !== $_REQUEST['section'] ) ) { - $is_admin = false; + $screen = get_current_screen(); + if ( $screen && $screen->in_admin() && 'woocommerce_page_wc-settings' === $screen->id ) { + return true; } - $is_rest = ( defined( 'REST_REQUEST' ) && true === REST_REQUEST ); - if ( $is_rest ) { + if ( Constants::is_true( 'REST_REQUEST' ) ) { global $wp; - if ( ! isset( $wp->query_vars['rest_route'] ) || false === strpos( $wp->query_vars['rest_route'], '/payment_gateways' ) ) { - $is_rest = false; + if ( isset( $wp->query_vars['rest_route'] ) && false !== strpos( $wp->query_vars['rest_route'], '/payment_gateways' ) ) { + return true; } } - // phpcs:enable - - return $is_admin || $is_rest; + return false; } /** @@ -354,7 +346,6 @@ class WC_Gateway_COD extends WC_Payment_Gateway { /** * Add content to the WC emails. * - * @access public * @param WC_Order $order Order object. * @param bool $sent_to_admin Sent to admin. * @param bool $plain_text Email format: plain text or HTML. diff --git a/tests/unit-tests/payment-gateways/cod.php b/tests/unit-tests/payment-gateways/cod.php index 1c9dacf26fb..a522370e9f9 100644 --- a/tests/unit-tests/payment-gateways/cod.php +++ b/tests/unit-tests/payment-gateways/cod.php @@ -1,13 +1,25 @@ get_form_fields(); + // Clear the screen! + $GLOBALS['current_screen'] = null; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited + $this->assertArrayHasKey( 'enable_for_methods', $form_fields ); $this->assertNotEmpty( $form_fields['enable_for_methods']['options'] ); } @@ -42,7 +53,7 @@ class WC_Tests_Payment_Gateway_COD extends WC_Unit_Test_Case { * Make sure that the options for the "enable_for_methods" setting are not loaded for API requests that don't need it. */ public function test_method_options_not_loaded_for_incorrect_api() { - define( 'REST_REQUEST', true ); + Constants::set_constant( 'REST_REQUEST', true ); $GLOBALS['wp']->query_vars['rest_route'] = '/wc/v2/products'; $gateway = new WC_Gateway_COD(); @@ -57,7 +68,7 @@ class WC_Tests_Payment_Gateway_COD extends WC_Unit_Test_Case { * Make sure that the options for the "enable_for_methods" setting are loaded for API requests that need it. */ public function test_method_options_loaded_for_correct_api() { - define( 'REST_REQUEST', true ); + Constants::set_constant( 'REST_REQUEST', true ); $GLOBALS['wp']->query_vars['rest_route'] = '/wc/v2/payment_gateways'; $gateway = new WC_Gateway_COD();