Add ContributionRecur crud api
authoreileen <emcnaughton@wikimedia.org>
Tue, 12 May 2020 01:58:53 +0000 (13:58 +1200)
committereileen <emcnaughton@wikimedia.org>
Tue, 12 May 2020 02:12:47 +0000 (14:12 +1200)
Civi/Api4/Contribution.php
Civi/Api4/ContributionPage.php
Civi/Api4/ContributionRecur.php [new file with mode: 0644]
Civi/Api4/Service/Spec/Provider/ContributionCreationSpecProvider.php
Civi/Api4/Service/Spec/Provider/ContributionRecurCreationSpecProvider.php [new file with mode: 0644]
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ContributionPageTest.php
tests/phpunit/api/v3/ContributionRecurTest.php

index fe0fe24647078fd8ca07120f0e1c8fbe4db722b6..99743ba0154a58075d803451998e676e0a5b67a6 100644 (file)
  +--------------------------------------------------------------------+
  */
 
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- */
-
-
 namespace Civi\Api4;
 
 /**
index 17f1c6a0ef62672209981163f1c7889cdbd0ec72..cfafb5b018d33ff731b97847f52afd8f5876ac13 100644 (file)
  +--------------------------------------------------------------------+
  */
 
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- */
-
-
 namespace Civi\Api4;
 
 /**
diff --git a/Civi/Api4/ContributionRecur.php b/Civi/Api4/ContributionRecur.php
new file mode 100644 (file)
index 0000000..4d3884d
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Api4;
+
+/**
+ * ContributionRecur entity.
+ *
+ * @package Civi\Api4
+ */
+class ContributionRecur extends Generic\DAOEntity {
+
+}
index 1a370b34492479c5b9386a18db6a825b7427b51a..a7095a0ba30d6e405ab32d4e503ece8a9c352ca3 100644 (file)
  +--------------------------------------------------------------------+
  */
 
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC https://civicrm.org/licensing
- * $Id$
- *
- */
-
-
 namespace Civi\Api4\Service\Spec\Provider;
 
 use Civi\Api4\Service\Spec\RequestSpec;
 
+/**
+ * Class ContributionCreationSpecProvider
+ *
+ * @package Civi\Api4\Service\Spec\Provider
+ */
 class ContributionCreationSpecProvider implements Generic\SpecProviderInterface {
 
   /**
diff --git a/Civi/Api4/Service/Spec/Provider/ContributionRecurCreationSpecProvider.php b/Civi/Api4/Service/Spec/Provider/ContributionRecurCreationSpecProvider.php
new file mode 100644 (file)
index 0000000..6d38d6a
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+
+namespace Civi\Api4\Service\Spec\Provider;
+
+use Civi\Api4\Service\Spec\RequestSpec;
+
+/**
+ * Class ContributionRecurCreationSpecProvider
+ *
+ * @package Civi\Api4\Service\Spec\Provider
+ */
+class ContributionRecurCreationSpecProvider implements Generic\SpecProviderInterface {
+
+  /**
+   * @inheritDoc
+   */
+  public function modifySpec(RequestSpec $spec) {
+    $spec->getFieldByName('create_date')->setDefaultValue('now');
+  }
+
+  /**
+   * @inheritDoc
+   */
+  public function applies($entity, $action) {
+    return $entity === 'ContributionRecur' && $action === 'create';
+  }
+
+}
index b003861d858d3ba7ff30e16d476c1a3959a9a55e..af0f28dd4af099da91d801975ec85013c664b76e 100644 (file)
@@ -3562,4 +3562,35 @@ VALUES
     }
   }
 
+  /**
+   * Generic create test.
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function basicCreateTest(int $version) {
+    $this->_apiversion = $version;
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
+    $this->assertEquals(1, $result['count']);
+    $this->assertNotNull($result['values'][$result['id']]['id']);
+    $this->getAndCheck($this->params, $result['id'], $this->_entity);
+  }
+
+  /**
+   * Generic delete test.
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
+   */
+  protected function basicDeleteTest($version) {
+    $this->_apiversion = $version;
+    $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
+    $deleteParams = ['id' => $result['id']];
+    $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
+    $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
+    $this->assertEquals(0, $checkDeleted['count']);
+  }
+
 }
index 5d4b4a8a10730d8978cd3dcbe033ed567c6a11c0..dcf684ea7d343e9e2dc0559344847506f3b02c5e 100644 (file)
@@ -21,7 +21,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
   protected $params;
   protected $id = 0;
   protected $contactIds = [];
-  protected $_entity = 'contribution_page';
+  protected $_entity = 'ContributionPage';
   protected $contribution_result = NULL;
   protected $_priceSetParams = [];
   protected $_membershipBlockAmount = 2;
@@ -40,8 +40,6 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    */
   protected $_ids = [];
 
-  public $DBResetRequired = TRUE;
-
   /**
    * Setup for test.
    *
@@ -75,7 +73,6 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    * Tear down after test.
    *
    * @throws \CRM_Core_Exception
-   * @throws \CiviCRM_API3_Exception
    */
   public function tearDown() {
     foreach ($this->contactIds as $id) {
@@ -94,11 +91,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    * @throws \CRM_Core_Exception
    */
   public function testCreateContributionPage($version) {
-    $this->_apiversion = $version;
-    $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
-    $this->assertEquals(1, $result['count']);
-    $this->assertNotNull($result['values'][$result['id']]['id']);
-    $this->getAndCheck($this->params, $result['id'], $this->_entity);
+    $this->basicCreateTest($version);
   }
 
   /**
@@ -148,12 +141,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    * @throws \CRM_Core_Exception
    */
   public function testDeleteContributionPage($version) {
-    $this->_apiversion = $version;
-    $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
-    $deleteParams = ['id' => $createResult['id']];
-    $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
-    $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
-    $this->assertEquals(0, $checkDeleted['count']);
+    $this->basicDeleteTest($version);
   }
 
   /**
@@ -183,6 +171,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
 
   /**
    * Test form submission with basic price set.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testSubmitZeroDollar() {
     $this->setUpContributionPage();
@@ -247,6 +237,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
   /**
    * Test form submission with billing first & last name where the contact does
    * otherwise have one and should not be overwritten.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testSubmitNewBillingNameDoNotOverwrite() {
     $this->setUpContributionPage();
@@ -582,10 +574,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', []);
     $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
     $membership = $this->callAPISuccessGetSingle('membership', ['id' => $membershipPayment['membership_id']]);
-    $pendingStatus = $this->callAPISuccessGetSingle('MembershipStatus', [
-      'return' => ["id"],
-      'name' => "Pending",
-    ]);
+    $pendingStatus = $this->callAPISuccessGetSingle('MembershipStatus', ['return' => ['id'], 'name' => 'Pending']);
     $this->assertEquals($membership['status_id'], $pendingStatus['id']);
     $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
   }
@@ -904,6 +893,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    * processor (IATS style - denoted by returning trxn_id)
    * - the first creates a new membership, completed contribution, in progress recurring. Check these
    * - create another - end date should be extended
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentMonth() {
     $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(['duration_unit' => 'month', 'recur_frequency_unit' => 'month']);
@@ -1387,7 +1378,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
     $dummyPP->setDoDirectPaymentResult(['payment_status_id' => 1, 'trxn_id' => 'create_first_success']);
 
-    //Sumbit payment with recur disabled.
+    //Submit payment with recur disabled.
     $submitParams = [
       'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
       'id' => (int) $this->_ids['contribution_page'],
@@ -1851,7 +1842,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
 
     $this->assertEquals(100.00, $contribution['total_amount']);
     $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
-    $this->assertEquals($pledgePayment['values'][2]['status_id'], 1, "This pledge payment should have been completed");
+    $this->assertEquals($pledgePayment['values'][2]['status_id'], 1, 'This pledge payment should have been completed');
     $this->assertEquals($pledgePayment['values'][2]['contribution_id'], $contribution['id']);
   }
 
@@ -1891,7 +1882,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->assertEquals(3, $lineItems['count']);
     $totalLineAmount = 0;
     foreach ($lineItems['values'] as $lineItem) {
-      $totalLineAmount = $totalLineAmount + $lineItem['line_total'];
+      $totalLineAmount += $lineItem['line_total'];
     }
     $this->assertEquals(80, $totalLineAmount);
   }
@@ -1946,7 +1937,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $financialType = $this->createFinancialType();
     $financialTypeId = $financialType['id'];
     // This function sets the Tax Rate at 10% - it currently has no way to pass Tax Rate into it - so let's work with 10%
-    $this->relationForFinancialTypeWithFinancialAccount($financialType['id'], 5);
+    $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
 
     $this->setUpContributionPage();
     $submitParams = [
@@ -1966,7 +1957,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
       'label' => 'Printing Rights',
       'html_type' => 'Text',
     ]);
-    $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
+    $this->callAPISuccess('price_field_value', 'create', [
       'price_set_id' => $priceSetID,
       'price_field_id' => $priceField['id'],
       'label' => 'Printing Rights',
@@ -2019,6 +2010,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    *
    * For example Paypal Checkout will replace the confirm button with it's own but we are able to validate
    * before paypal launches it's modal. In this case the $_REQUEST is post but we need validation to succeed.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testValidatePost() {
     $_SERVER['REQUEST_METHOD'] = 'POST';
@@ -2030,6 +2023,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
 
   /**
    * Test that an error is generated if required fields are not submitted.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testValidateOutputOnMissingRecurFields() {
     $this->params['is_recur_interval'] = 1;
@@ -2075,14 +2070,13 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
   protected function getBasicSubmitParams() {
     $priceFieldID = reset($this->_ids['price_field']);
     $priceFieldValueID = reset($this->_ids['price_field_value']);
-    $submitParams = [
+    return [
       'price_' . $priceFieldID => $priceFieldValueID,
       'id' => (int) $this->_ids['contribution_page'],
       'amount' => 10,
       'priceSetId' => $this->_ids['price_set'][0],
       'payment_processor_id' => 0,
     ];
-    return $submitParams;
   }
 
 }
index ff4919fb3ff02def31049fb1599c36e93c979862..33a4005a71de66266895cc2be106223c84ed0058 100644 (file)
  * @group headless
  */
 class api_v3_ContributionRecurTest extends CiviUnitTestCase {
-  protected $_apiversion = 3;
   protected $params;
-  protected $ids = [];
-  protected $_entity = 'contribution_recur';
+  protected $_entity = 'ContributionRecur';
 
   public $DBResetRequired = FALSE;
 
+  /**
+   * @throws \CRM_Core_Exception
+   */
   public function setUp() {
     parent::setUp();
-    $this->useTransaction(TRUE);
+    $this->useTransaction();
     $this->ids['contact'][0] = $this->individualCreate();
     $this->params = [
       'contact_id' => $this->ids['contact'][0],
@@ -40,22 +41,41 @@ class api_v3_ContributionRecurTest extends CiviUnitTestCase {
     ];
   }
 
-  public function testCreateContributionRecur() {
-    $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
-    $this->assertEquals(1, $result['count']);
-    $this->assertNotNull($result['values'][$result['id']]['id']);
-    $this->getAndCheck($this->params, $result['id'], $this->_entity);
+  /**
+   * Basic create test.
+   *
+   * @dataProvider versionThreeAndFour
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testCreateContributionRecur($version) {
+    $this->basicCreateTest($version);
   }
 
-  public function testGetContributionRecur() {
+  /**
+   * Basic get test.
+   *
+   * @dataProvider versionThreeAndFour
+   *
+   * @param int $version
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testGetContributionRecur($version) {
+    $this->_apiversion = $version;
     $this->callAPISuccess($this->_entity, 'create', $this->params);
-    $getParams = [
-      'amount' => '500',
-    ];
+    $getParams = ['amount' => '500'];
     $result = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
     $this->assertEquals(1, $result['count']);
   }
 
+  /**
+   * @dataProvider versionThreeAndFour
+   *
+   * @throws \CRM_Core_Exception
+   */
   public function testCreateContributionRecurWithToken() {
     // create token
     $this->createLoggedInUser();
@@ -71,14 +91,22 @@ class api_v3_ContributionRecurTest extends CiviUnitTestCase {
     $this->getAndCheck($this->params, $result['id'], $this->_entity);
   }
 
-  public function testDeleteContributionRecur() {
-    $result = $this->callAPISuccess($this->_entity, 'create', $this->params);
-    $deleteParams = ['id' => $result['id']];
-    $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
-    $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
-    $this->assertEquals(0, $checkDeleted['count']);
+  /**
+   * @dataProvider versionThreeAndFour
+   *
+   * @param $version
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testDeleteContributionRecur($version) {
+    $this->basicDeleteTest($version);
   }
 
+  /**
+   * Test expected apiv3 outputs.
+   *
+   * @throws \CRM_Core_Exception
+   */
   public function testGetFieldsContributionRecur() {
     $result = $this->callAPISuccess($this->_entity, 'getfields', ['action' => 'create']);
     $this->assertEquals(12, $result['values']['start_date']['type']);
@@ -86,6 +114,8 @@ class api_v3_ContributionRecurTest extends CiviUnitTestCase {
 
   /**
    * Test that we can cancel a contribution and add a cancel_reason via the api.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testContributionRecurCancel() {
     $result = $this->callAPISuccess($this->_entity, 'create', $this->params);