-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate PRB settings data to ECE (#3688)
* create class for migrating prb to ece * include and initiate class * use express checkout option name * add changelog
- Loading branch information
Showing
6 changed files
with
139 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
includes/migrations/class-migrate-payment-request-data-to-express-checkout-data.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data | ||
*/ | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data | ||
* | ||
* Migrates Payment Request settings data to Express Checkout settings data. | ||
* | ||
* @since 9.1.0 | ||
*/ | ||
class Migrate_Payment_Request_Data_To_Express_Checkout_Data { | ||
/** | ||
* Migrate_Payment_Request_Data_To_Express_Checkout_Data constructor. | ||
*/ | ||
public function __construct() { | ||
add_action( 'woocommerce_stripe_updated', [ $this, 'maybe_migrate' ] ); | ||
} | ||
|
||
/** | ||
* Only execute the migration if not applied yet. | ||
*/ | ||
public function maybe_migrate() { | ||
$stripe_gateway = $this->get_gateway(); | ||
|
||
$express_checkout_enabled = $stripe_gateway->get_option( 'express_checkout' ); | ||
|
||
if ( empty( $express_checkout_enabled ) ) { | ||
$this->migrate(); | ||
} | ||
} | ||
|
||
/** | ||
* Copies over Payment Request settings data to Express Checkout settings data. | ||
*/ | ||
private function migrate() { | ||
$stripe_gateway = $this->get_gateway(); | ||
|
||
$payment_request_enabled = $stripe_gateway->get_option( 'payment_request', 'no' ); | ||
$payment_request_button_type = $stripe_gateway->get_option( 'payment_request_button_type', 'default' ); | ||
$payment_request_button_theme = $stripe_gateway->get_option( 'payment_request_button_theme', 'dark' ); | ||
$payment_request_button_size = $stripe_gateway->get_option( 'payment_request_button_size', 'default' ); | ||
$payment_request_button_locations = $stripe_gateway->get_option( 'payment_request_button_locations', [ 'checkout' ] ); | ||
|
||
$stripe_gateway->update_option( 'express_checkout', $payment_request_enabled ); | ||
$stripe_gateway->update_option( 'express_checkout_button_type', $payment_request_button_type ); | ||
$stripe_gateway->update_option( 'express_checkout_button_theme', $payment_request_button_theme ); | ||
$stripe_gateway->update_option( 'express_checkout_button_size', $payment_request_button_size ); | ||
$stripe_gateway->update_option( 'express_checkout_button_locations', $payment_request_button_locations ); | ||
} | ||
|
||
/** | ||
* Returns the main Stripe payment gateways. | ||
* | ||
* @return WC_Stripe_Payment_Gateway | ||
*/ | ||
public function get_gateway() { | ||
return woocommerce_gateway_stripe()->get_main_stripe_gateway(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...nit/admin/migrations/test-class-migrate-payment-request-data-to-express-checkout-data.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data | ||
*/ | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
|
||
/** | ||
* Allowed_Payment_Request_Button_Types_Update unit tests. | ||
*/ | ||
class Migrate_Payment_Request_Data_To_Express_Checkout_Data_Test extends WP_UnitTestCase { | ||
|
||
/** | ||
* Stripe gateway mock. | ||
* | ||
* @var MockObject|WC_Gateway_Stripe | ||
*/ | ||
private $gateway_mock; | ||
|
||
/** | ||
* @var Migrate_Payment_Request_Data_To_Express_Checkout_Data | ||
*/ | ||
private $migration; | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
|
||
$this->gateway_mock = $this->getMockBuilder( WC_Gateway_Stripe::class ) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->migration = $this->getMockBuilder( Migrate_Payment_Request_Data_To_Express_Checkout_Data::class ) | ||
->disableOriginalConstructor() | ||
->setMethods( [ 'get_gateway' ] ) | ||
->getMock(); | ||
} | ||
|
||
public function test_migration_not_executed_when_data_already_migrated() { | ||
$this->setup_environment( [ 'express_checkout' => 'yes' ] ); | ||
|
||
$this->gateway_mock->expects( $this->never() ) | ||
->method( 'update_option' ); | ||
|
||
$this->migration->maybe_migrate(); | ||
} | ||
|
||
public function test_prb_settings_data_migration_to_ece_settings_data() { | ||
$this->setup_environment( [] ); | ||
|
||
$this->gateway_mock->expects( $this->exactly( 5 ) ) | ||
->method( 'update_option' ); | ||
|
||
$this->migration->maybe_migrate(); | ||
} | ||
|
||
private function setup_environment( $settings ) { | ||
$this->gateway_mock->method( 'get_option' ) | ||
->willReturnCallback( | ||
function ( $key ) use ( $settings ) { | ||
return isset( $settings[ $key ] ) ? $settings[ $key ] : ''; | ||
} | ||
); | ||
$this->migration->method( 'get_gateway' )->willReturn( $this->gateway_mock ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters