diff --git a/CRM/GoCardless/Upgrader.php b/CRM/GoCardless/Upgrader.php index fc632d4..90b9475 100644 --- a/CRM/GoCardless/Upgrader.php +++ b/CRM/GoCardless/Upgrader.php @@ -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) { diff --git a/tests/phpunit/GoCardlessTest.php b/tests/phpunit/GoCardlessTest.php index 78ce272..f7420b0 100644 --- a/tests/phpunit/GoCardlessTest.php +++ b/tests/phpunit/GoCardlessTest.php @@ -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. @@ -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. *