diff --git a/includes/class-omise-capabilities.php b/includes/class-omise-capabilities.php index 9a8caf3d..405f432a 100644 --- a/includes/class-omise-capabilities.php +++ b/includes/class-omise-capabilities.php @@ -35,14 +35,7 @@ class Omise_Capabilities { */ public static function retrieve($pKey = null, $sKey = null) { - $shouldCallApi = self::shouldCallApi( - is_checkout(), - // If endpoint url is `order-received`, it mean thank you page. - is_wc_endpoint_url( 'order-received' ), - is_admin(), - ); - - if ( !$shouldCallApi ) { + if ( !self::shouldCallApi() ) { return null; } @@ -85,18 +78,19 @@ public static function retrieve($pKey = null, $sKey = null) * @param boolean $isThankYouPage * @return boolean */ - public static function shouldCallApi($isCheckout, $isThankYouPage, $isAdmin,) { - $omisePages = [ 'omise' ]; + public static function shouldCallApi() { + $omiseSettingPages = [ 'omise' ]; $currentAdminPage = isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : ''; // checkout page but not thank you page // By default thank you page is also part of checkout pages // and we do not need to call capabilities on thank you page. - $isPaymentPage = $isCheckout && !$isThankYouPage; + // If endpoint url is `order-received`, it mean thank you page. + $isPaymentPage = is_checkout() && !is_wc_endpoint_url( 'order-received' ); // if from admin panel and is omise page. - $isOmiseSettingPage = $isAdmin && in_array( $currentAdminPage, $omisePages ); + $isOmiseSettingPage = is_admin() && in_array( $currentAdminPage, $omiseSettingPages ); return $isPaymentPage || $isOmiseSettingPage; } diff --git a/tests/unit/includes/class-omise-capabilities-test.php b/tests/unit/includes/class-omise-capabilities-test.php index f645bddf..3c66a39a 100644 --- a/tests/unit/includes/class-omise-capabilities-test.php +++ b/tests/unit/includes/class-omise-capabilities-test.php @@ -27,68 +27,54 @@ public function tearDown(): void } /** + * @dataProvider retrieve_data_provider * @runInSeparateProcess * @covers Omise_Capabilities */ - public function test_retrieve_should_return_null_when_it_should_not_call_api() - { - function is_admin() { return false; } - function is_checkout() { return false; } - function is_wc_endpoint_url($page) { return true; } - - $result = Omise_Capabilities::retrieve(); - $this->assertEquals(null, $result); - } - - /** - * @runInSeparateProcess - * @covers Omise_Capabilities - */ - public function test_retrieve_should_return_value_when_it_should_call_api() - { - function is_admin() { return false; } - function is_checkout() { return true; } - function is_wc_endpoint_url($page) { return false; } + public function test_retrieve_should_return_value_when_it_should_call_api($isCheckout, $isThankYouPage, $isAdmin, $adminPageName, $expected) + { + // assigning to global variable, so that we can use in child functions + $GLOBALS['isCheckout'] = $isCheckout; + $GLOBALS['isThankYouPage'] = $isThankYouPage; + $GLOBALS['isAdmin'] = $isAdmin; - $this->omiseSettingMock->shouldReceive('instance')->andReturn($this->omiseSettingMock); - $this->omiseSettingMock->shouldReceive('public_key')->andReturn('pkey_xxx'); - $this->omiseSettingMock->shouldReceive('secret_key')->andReturn('skey_xxx'); - $this->omiseCapabilitiesMock->shouldReceive('retrieve')->once(); - - $result = Omise_Capabilities::retrieve(); + // mocking page name + $_GET['page'] = $adminPageName; - $this->assertEquals('Omise_Capabilities', get_class($result)); - $this->assertTrue(true); - } + function is_checkout() { return $GLOBALS['isCheckout']; } + function is_wc_endpoint_url($page) { return $GLOBALS['isThankYouPage']; } + function is_admin() { return $GLOBALS['isAdmin']; } - /** - * @dataProvider should_call_api_data_provider - * @covers Omise_Capabilities - */ - public function test_should_call_capability_api($isCheckout, $isThankYouPage, $isAdmin, $adminPageName, $expected) - { - $_GET['page'] = $adminPageName; - $result = Omise_Capabilities::shouldCallApi($isCheckout, $isThankYouPage, $isAdmin); - $this->assertEquals($expected, $result); + if ($expected) { + $this->omiseSettingMock->shouldReceive('instance')->andReturn($this->omiseSettingMock); + $this->omiseSettingMock->shouldReceive('public_key')->andReturn('pkey_xxx'); + $this->omiseSettingMock->shouldReceive('secret_key')->andReturn('skey_xxx'); + $this->omiseCapabilitiesMock->shouldReceive('retrieve')->once(); + $result = Omise_Capabilities::retrieve(); + $this->assertEquals('Omise_Capabilities', get_class($result)); + } else { + $result = Omise_Capabilities::retrieve(); + $this->assertEquals(null, $result); + } } /** * Data provider for toSubunitReturnCorrectFormat */ - public function should_call_api_data_provider() + public function retrieve_data_provider() { return [ // checkout page and not thank you page [true, false, false, '', true], - // checkout page and also thank you page + // // checkout page and also thank you page [true, true, false, '', false], - // omise setting page + // // omise setting page [true, true, true, 'omise', true], - // other admin page + // // other admin page [true, true, true, 'other-page', false], - // non checkout page and also no-admin page + // // non checkout page and also no-admin page [false, false, false, 'other-page', false], - // non checkout page, non admin page + // // non checkout page, non admin page [false, false, false, '', false], ]; }