Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
Fix upgrader and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Lott / Artful Robot committed Feb 4, 2020
1 parent 4006882 commit 81eaf83
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CRM/GoCardless/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,22 @@ public function upgrade_0001() {
* @throws Exception
*/
public function upgrade_0002() {
$this->ctx->log->info('Applying update 0002');
if ($this->ctx) {
$this->ctx->log->info('Applying update 0002');
}


// We need the payment_instrument_id for GoCardless
$payment_instrument = civicrm_api3('OptionValue', 'get', [
'option_group_id' => "payment_instrument",
'name' => "direct_debit_gc",
'sequential' => 1,
]);
if (!$payment_instrument['count'] == 1) {
Civi::log()->error("GoCardless upgrade_0002 expected to find a payment instrument with name direct_debit_gc but found none. Cannot perform upgrade step.");
return FALSE;
}
$payment_instrument_id = $payment_instrument['values'][0]['value'];

$processor_types = civicrm_api3('PaymentProcessorType', 'get', [ 'name' => 'GoCardless', 'options' => ['limit' => 0] ]);
if ($processor_types['count'] != 1) {
Expand Down
25 changes: 25 additions & 0 deletions tests/phpunit/GoCardlessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Civi\Test\TransactionalInterface;
use Prophecy\Prophet;
use Prophecy\Argument;
use CRM_GoCardless_ExtensionUtil as E;

/**
* Tests the GoCardless direct debit extension.
Expand Down Expand Up @@ -1672,6 +1673,30 @@ public function testIssue59() {
}
}

public function testUpgrade0002() {
$payment_instrument = civicrm_api3('OptionValue', 'getsingle', [ 'option_group_id' => "payment_instrument", 'name' => "direct_debit_gc" ])['value'];
$processor_type = civicrm_api3('PaymentProcessorType', 'getsingle', [ 'name' => 'GoCardless', 'options' => ['limit' => 0] ])['id'];

// After an install, our processor should be correctly set up.
$proc = civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $this->test_mode_payment_processor['id']]);
$this->assertEquals($payment_instrument, $proc['payment_instrument_id']);
$this->assertEquals(CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT, $proc['payment_type']);

// Now bodge this backwards.
CRM_Core_DAO::executeQuery("UPDATE civicrm_payment_processor SET payment_type=1, payment_instrument_id=1 WHERE id = %1", [
1 => [$this->test_mode_payment_processor['id'], 'Integer']
]);

// Now run the upgrade
$up = new CRM_GoCardless_Upgrader(E::LONG_NAME, E::path());
$up->upgrade_0002();

// And re-test
$proc = civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $this->test_mode_payment_processor['id']]);
$this->assertEquals($payment_instrument, $proc['payment_instrument_id']);
$this->assertEquals(CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT, $proc['payment_type']);

}
/**
* Return a fake GoCardless payment processor.
*
Expand Down

0 comments on commit 81eaf83

Please sign in to comment.