From: eileen Date: Tue, 12 May 2020 01:58:53 +0000 (+1200) Subject: Add ContributionRecur crud api X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=d943e78d1d6ed6413c619a1657fbe058742be879;p=civicrm-core.git Add ContributionRecur crud api --- diff --git a/Civi/Api4/Contribution.php b/Civi/Api4/Contribution.php index fe0fe24647..99743ba015 100644 --- a/Civi/Api4/Contribution.php +++ b/Civi/Api4/Contribution.php @@ -10,13 +10,6 @@ +--------------------------------------------------------------------+ */ -/** - * - * @package CRM - * @copyright CiviCRM LLC https://civicrm.org/licensing - */ - - namespace Civi\Api4; /** diff --git a/Civi/Api4/ContributionPage.php b/Civi/Api4/ContributionPage.php index 17f1c6a0ef..cfafb5b018 100644 --- a/Civi/Api4/ContributionPage.php +++ b/Civi/Api4/ContributionPage.php @@ -10,13 +10,6 @@ +--------------------------------------------------------------------+ */ -/** - * - * @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 index 0000000000..4d3884d15f --- /dev/null +++ b/Civi/Api4/ContributionRecur.php @@ -0,0 +1,22 @@ +getFieldByName('create_date')->setDefaultValue('now'); + } + + /** + * @inheritDoc + */ + public function applies($entity, $action) { + return $entity === 'ContributionRecur' && $action === 'create'; + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index b003861d85..af0f28dd4a 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -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']); + } + } diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index 5d4b4a8a10..dcf684ea7d 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -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; } } diff --git a/tests/phpunit/api/v3/ContributionRecurTest.php b/tests/phpunit/api/v3/ContributionRecurTest.php index ff4919fb3f..33a4005a71 100644 --- a/tests/phpunit/api/v3/ContributionRecurTest.php +++ b/tests/phpunit/api/v3/ContributionRecurTest.php @@ -17,16 +17,17 @@ * @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);