Skip to content

Commit

Permalink
Merge pull request #399 from omise/release-v5.3.1
Browse files Browse the repository at this point in the history
Bump v5.3.1
  • Loading branch information
ajzkk authored Sep 5, 2023
2 parents ad6bce7 + 6e8b742 commit 95d5fcf
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

### [v5.3.1 _(Sep 05, 2023)_](https://github.com/omise/omise-woocommerce/releases/tag/v5.3.1)
- Fixed capabilities api calling on every pages. (PR [#398](https://github.com/omise/omise-woocommerce/pull/398))

### [v5.3.0 _(Aug 23, 2023)_](https://github.com/omise/omise-woocommerce/releases/tag/v5.3.0)
- Add Alipay+ on Thailand psp. (PR [#394](https://github.com/omise/omise-woocommerce/pull/394))
- Adding admin_notices action once all dependencies are loaded. (PR [#395](https://github.com/omise/omise-woocommerce/pull/395))
Expand Down
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
4 changes: 2 additions & 2 deletions omise-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Opn Payments
* Plugin URI: https://www.omise.co/woocommerce
* Description: Opn Payments is a WordPress plugin designed specifically for WooCommerce. The plugin adds support for Opn Payments Payment Gateway's payment methods to WooCommerce.
* Version: 5.3.0
* Version: 5.3.1
* Author: Opn Payments and contributors
* Author URI: https://github.com/omise/omise-woocommerce/graphs/contributors
* Text Domain: omise
Expand All @@ -22,7 +22,7 @@ class Omise
*
* @var string
*/
public $version = '5.3.0';
public $version = '5.3.1';

/**
* The Omise Instance.
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: Opn Payments
Tags: opn payments, payment, payment gateway, woocommerce plugin, omise, opn, installment, internet banking, alipay, paynow, truemoney wallet, woocommerce payment
Requires at least: 4.3.1
Tested up to: 6.0.2
Stable tag: 5.3.0
Stable tag: 5.3.1
License: MIT
License URI: https://opensource.org/licenses/MIT

Expand Down Expand Up @@ -34,6 +34,10 @@ From there:

== Changelog ==

= 5.3.1 =

- Fixed capabilities api calling on every pages. (PR [#398](https://github.com/omise/omise-woocommerce/pull/398))

= 5.3.0 =

- Add Alipay+ on Thailand psp. (PR [#394](https://github.com/omise/omise-woocommerce/pull/394))
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
{
/**
* @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

0 comments on commit 95d5fcf

Please sign in to comment.