Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIT-1699] Fix capabilities api calling on every pages #398

Merged
merged 9 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions includes/class-omise-capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Omise_Capabilities {
*/
public static function retrieve($pKey = null, $sKey = null)
{
if ( !self::shouldCallApi() ) {
return null;
}

$keys = self::getKeys($pKey, $sKey);

// Do not call capabilities API if keys are not present
Expand Down Expand Up @@ -68,6 +72,25 @@ public static function retrieve($pKey = null, $sKey = null)
return self::$instance;
}

/**
* @return boolean
*/
public static function shouldCallApi() {
$omiseSettingPages = [ 'omise' ];
$currentAdminPage = isset( $_GET[ 'page' ] ) ? $_GET[ 'page' ] : '';
// If page is omise setting page from admin panel.
$isOmiseSettingPage = is_admin() && in_array( $currentAdminPage, $omiseSettingPages );

// If page is 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.
// If endpoint url is `order-received`, it mean thank you page.
$isPaymentPage = is_checkout() && !is_wc_endpoint_url( 'order-received' );

return $isPaymentPage || $isOmiseSettingPage;
}


/**
* @param string|null $pKey
* @param string|null $sKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,29 +247,3 @@ public function correctly_calculating_monthly_payment_amount_as_merchant_absorbs
$this->assertEquals(833.33, $result);
}
}

/**
* Mock Omise_Capabilities class.
* NOTE: This might not be an ideal way to mock a class,
* feel free to enhance the test or the core code.
*
* @see includes/class-omise-capabilities
*/
class Omise_Capabilities
ajzkk marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @var self
*/
protected static $the_instance = null;

public static function retrieve()
{
self::$the_instance = self::$the_instance ?: new self();
return self::$the_instance;
}

public function is_zero_interest()
{
return false;
}
}
81 changes: 81 additions & 0 deletions tests/unit/includes/class-omise-capabilities-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

use PHPUnit\Framework\TestCase;

class Omise_Capabilities_Test extends TestCase
{
private $omiseSettingMock;

private $omiseCapabilitiesMock;

/**
* setup add_action and do_action before the test run
*/
public function setUp(): void
{
require_once __DIR__ . '/../../../includes/class-omise-capabilities.php';
$this->omiseSettingMock = Mockery::mock('alias:Omise_Setting');
$this->omiseCapabilitiesMock = Mockery::mock('alias:OmiseCapabilities');
}

/**
* close mockery after test cases are done
*/
public function tearDown(): void
{
Mockery::close();
}

/**
* @dataProvider retrieve_data_provider
* @runInSeparateProcess
* @covers Omise_Capabilities
*/
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;

// mocking page name
$_GET['page'] = $adminPageName;

function is_checkout() { return $GLOBALS['isCheckout']; }
function is_wc_endpoint_url($page) { return $GLOBALS['isThankYouPage']; }
function is_admin() { return $GLOBALS['isAdmin']; }

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 retrieve_data_provider()
{
return [
// checkout page and not thank you page
[true, false, false, '', true],
// // checkout page and also thank you page
[true, true, false, '', false],
// // omise setting page
[true, true, true, 'omise', true],
// // other admin page
[true, true, true, 'other-page', false],
// // non checkout page and also no-admin page
[false, false, false, 'other-page', false],
// // non checkout page, non admin page
[false, false, false, '', false],
];
}
}
1 change: 0 additions & 1 deletion tests/unit/includes/gateway/class-omise-offsite-test.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use PHPunit\Framework\TestCase;
use Mockery;

abstract class Offsite_Test extends TestCase
{
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/omise-woocommerce-test.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Omise;
use Mockery;
use PHPUnit\Framework\TestCase;

class Omise_Test extends TestCase
Expand Down
Loading