From 4316296bd58be2c61c504fbdca4810b85b5cb6b5 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 24 Jul 2023 17:51:48 +1200 Subject: [PATCH] Minor improvement in test set up function --- .../Contribute/Form/Contribution/MainTest.php | 93 ++++++++++--------- tests/phpunit/CiviTest/CiviUnitTestCase.php | 10 +- 2 files changed, 52 insertions(+), 51 deletions(-) diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php index 671a4b31a7..e450e5ce40 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php @@ -18,12 +18,6 @@ */ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase { - /** - * The id of the contribution page. - * @var int - */ - private $contributionPageId; - /** * The id of the contribution page's payment processor. * @var int @@ -54,9 +48,9 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase { /** * Establish a standard list of submit params to more accurately test the submission. */ - private function getSubmitParams() { + private function getSubmitParams(): array { return [ - 'id' => $this->contributionPageId, + 'id' => $this->ids['ContributionPage']['test'], 'amount' => 80, 'first_name' => 'Billy', 'last_name' => 'Gruff', @@ -74,7 +68,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase { /** * Test that the membership is set to recurring if the membership type is always autorenew. */ - public function testSetRecurFunction() { + public function testSetRecurFunction(): void { $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 2, 'minimum_fee' => 80]); $form = $this->getContributionForm(); $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID); @@ -87,7 +81,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase { /** * Test that the membership is set to recurring if the membership type is optionally autorenew and is_recur is true. */ - public function testSetRecurFunctionOptionalYes() { + public function testSetRecurFunctionOptionalYes(): void { $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]); $form = $this->getContributionForm(); $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID); @@ -100,7 +94,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase { /** * Test that the membership is not set to recurring if the membership type is optionally autorenew and is_recur is false. */ - public function testSetRecurFunctionOptionalNo() { + public function testSetRecurFunctionOptionalNo(): void { $membershipTypeID = $this->membershipTypeCreate(['auto_renew' => 1, 'minimum_fee' => 80]); $form = $this->getContributionForm(); $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID); @@ -127,42 +121,49 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase { /** * Get a contribution form object for testing. * + * @params array $submittedValues + * @params array $params + * * @return \CRM_Contribute_Form_Contribution_Main */ - protected function getContributionForm($params = []) { - $this->priceSetId = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [ - 'name' => 'default_membership_type_amount', - 'return' => 'id', - ]); - - $paymentProcessor = $this->paymentProcessorCreate([ - 'payment_processor_type_id' => 'Dummy', - 'is_test' => 0, - ]); - - $contributionPageParams = (array_merge($params, [ - 'currency' => 'NZD', - 'goal_amount' => 6000, - 'is_pay_later' => 0, - 'is_monetary' => 1, - 'pay_later_text' => 'Front up', - 'pay_later_receipt' => 'Ta', - 'is_email_receipt' => 1, - 'payment_processor' => $paymentProcessor, - 'amount_block_is_active' => 1, - ])); - - /** @var \CRM_Contribute_Form_Contribution_Main $form */ - $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main'); - $contributionPage = reset($this->contributionPageCreate($contributionPageParams)['values']); - $form->set('id', $contributionPage['id']); - CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $this->priceSetId); - $form->preProcess(); - $form->buildQuickForm(); - // Need these values to create more realistic submit params (in getSubmitParams). - $this->paymentProcessorId = $paymentProcessor; - $this->contributionPageId = (int) $contributionPage['id']; - return $form; + protected function getContributionForm(array $submittedValues = [], $params = []): CRM_Contribute_Form_Contribution_Main { + try { + $this->priceSetId = $params['priceSetID'] ?? $this->callAPISuccessGetValue('PriceSet', [ + 'name' => 'default_membership_type_amount', + 'return' => 'id', + ]); + + $paymentProcessor = $submittedValues['payment_processor_id'] = $this->paymentProcessorCreate([ + 'payment_processor_type_id' => 'Dummy', + 'is_test' => 0, + ]); + + $contributionPageParams = (array_merge($params, [ + 'currency' => 'NZD', + 'goal_amount' => 6000, + 'is_pay_later' => 0, + 'is_monetary' => 1, + 'pay_later_text' => 'Front up', + 'pay_later_receipt' => 'Ta', + 'is_email_receipt' => 1, + 'payment_processor' => [$paymentProcessor], + 'amount_block_is_active' => 1, + ])); + $contributionPage = $this->contributionPageCreate($contributionPageParams); + $submittedValues['id'] = $_REQUEST['id'] = (int) $contributionPage['id']; + /** @var \CRM_Contribute_Form_Contribution_Main $form */ + $form = $this->getFormObject('CRM_Contribute_Form_Contribution_Main', $submittedValues); + $form->set('id', $contributionPage['id']); + CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPage['id'], $this->priceSetId); + $form->preProcess(); + $form->buildQuickForm(); + // Need these values to create more realistic submit params (in getSubmitParams). + $this->paymentProcessorId = $paymentProcessor; + return $form; + } + catch (CRM_Core_Exception $e) { + $this->fail('Failed to prepare form' . $e->getMessage()); + } } /** @@ -219,7 +220,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase { $this->callAPISuccess('PriceFieldValue', 'create', $priceFieldValueParams); } - $form = $this->getContributionForm(['priceSetID' => $priceSet['id']]); + $form = $this->getContributionForm([], ['priceSetID' => $priceSet['id']]); foreach ($form->_priceSet['fields'] as $pField) { foreach ($pField['options'] as $opId => $opValues) { $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 66197d36aa..abc2e372c1 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -854,22 +854,22 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { * Create contribution page. * * @param array $params + * @param string $identifier * * @return array * Array of contribution page */ - public function contributionPageCreate($params = []): array { - $pageParams = array_merge([ + public function contributionPageCreate(array $params = [], string $identifier = 'test'): array { + return $this->createTestEntity('ContributionPage', array_merge([ 'title' => 'Test Contribution Page', - 'financial_type_id' => 1, + 'financial_type_id:name' => 'Donation', 'currency' => 'USD', 'financial_account_id' => 1, 'is_active' => 1, 'is_allow_other_amount' => 1, 'min_amount' => 10, 'max_amount' => 1000, - ], $params); - return $this->callAPISuccess('ContributionPage', 'create', $pageParams); + ], $params), $identifier); } /** -- 2.25.1