Utilized the constants package to fix the failing tests

This commit is contained in:
Christopher Allford 2020-02-04 15:50:23 -08:00
parent 1d15df860e
commit b9ceabaa52
2 changed files with 27 additions and 25 deletions

View File

@ -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.

View File

@ -1,13 +1,25 @@
<?php
/**
* Contains tests for the COD Payment Gateway.
*
* @package WooCommerce/Tests/PaymentGateways
*/
use Automattic\Jetpack\Constants;
/**
* Class WC_Tests_Payment_Gateway_COD
*/
class WC_Tests_Payment_Gateway_COD extends WC_Unit_Test_Case {
/**
* Clean up after each test.
*/
public function tearDown() {
parent::tearDown();
Constants::clear_constants();
}
/**
* Make sure that the options for the "enable_for_methods" setting are not loaded by default.
*/
@ -24,16 +36,15 @@ class WC_Tests_Payment_Gateway_COD extends WC_Unit_Test_Case {
* Make sure that the options for the "enable_for_methods" setting are loaded on the admin page.
*/
public function test_method_options_loaded_for_admin_page() {
// Make sure we are seen as on the correct page for this.
define( 'WP_ADMIN', true );
$_REQUEST['page'] = 'wc-settings';
$_REQUEST['tab'] = 'checkout';
$_REQUEST['section'] = 'cod';
set_current_screen( 'woocommerce_page_wc-settings' );
$gateway = new WC_Gateway_COD();
$form_fields = $gateway->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();