From 927d64a7598deb0e4a204194fd0f29b2015b7ee1 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 2 Oct 2020 16:22:42 +1300 Subject: [PATCH] Add unit test to cover pcp submission --- .../CRM/Contribute/Form/ContributionTest.php | 29 ++++++++++++++++++- tests/phpunit/CRM/PCP/BAO/PCPTest.php | 27 +++++------------ tests/phpunit/CRMTraits/PCP/PCPTestTrait.php | 18 ++++++++++++ 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php index 1264710916..3ab9be1c36 100644 --- a/tests/phpunit/CRM/Contribute/Form/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Form/ContributionTest.php @@ -17,6 +17,7 @@ * @group headless */ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { + use CRMTraits_PCP_PCPTestTrait; protected $_individualId; protected $_contribution; @@ -80,7 +81,7 @@ class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase { parent::setUp(); $this->_userId = $this->createLoggedInUser(); - $this->_individualId = $this->individualCreate(); + $this->_individualId = $this->ids['contact'][0] = $this->individualCreate(); $this->_params = [ 'contact_id' => $this->_individualId, 'receive_date' => '20120511', @@ -809,6 +810,32 @@ Price Field - Price Field 1 1 $ 100.00 $ 100.00 $mut->stop(); } + /** + * Test submitting the back office contribution form with pcp data. + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \Civi\Payment\Exception\PaymentProcessorException + */ + public function testSubmitWithPCP() { + $params = $this->pcpParams(); + $pcpID = $this->createPCPBlock($params); + $form = new CRM_Contribute_Form_Contribution(); + $form->testSubmit([ + 'financial_type_id' => 3, + 'contact_id' => $this->_individualId, + 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'), + 'contribution_status_id' => 1, + 'total_amount' => 5, + 'pcp_made_through_id' => $pcpID, + 'pcp_display_in_roll' => '1', + 'pcp_roll_nickname' => 'Dobby', + 'pcp_personal_note' => 'I wuz here', + ], CRM_Core_Action::ADD); + $softCredit = $this->callAPISuccessGetSingle('ContributionSoft', []); + $this->assertEquals('Dobby', $softCredit['pcp_roll_nickname']); + } + /** * Test the submit function on the contribution page. */ diff --git a/tests/phpunit/CRM/PCP/BAO/PCPTest.php b/tests/phpunit/CRM/PCP/BAO/PCPTest.php index adfe01f2ba..267b33d5db 100644 --- a/tests/phpunit/CRM/PCP/BAO/PCPTest.php +++ b/tests/phpunit/CRM/PCP/BAO/PCPTest.php @@ -45,26 +45,15 @@ class CRM_PCP_BAO_PCPTest extends CiviUnitTestCase { $this->assertEquals($params['is_active'], $pcpBlock->is_active, 'Check for is_active.'); } - public function testAddPCP() { - $blockParams = $this->pcpBlockParams(); - $pcpBlock = CRM_PCP_BAO_PCPBlock::create($blockParams); - + /** + * Basic create test. + * + * @throws \CRM_Core_Exception + */ + public function testCreatePCP() { $params = $this->pcpParams(); - $params['pcp_block_id'] = $pcpBlock->id; - - $pcp = CRM_PCP_BAO_PCP::create($params); - - $this->assertInstanceOf('CRM_PCP_DAO_PCP', $pcp, 'Check for created object'); - $this->assertEquals($params['contact_id'], $pcp->contact_id, 'Check for entity table.'); - $this->assertEquals($params['status_id'], $pcp->status_id, 'Check for status.'); - $this->assertEquals($params['title'], $pcp->title, 'Check for title.'); - $this->assertEquals($params['intro_text'], $pcp->intro_text, 'Check for intro_text.'); - $this->assertEquals($params['page_text'], $pcp->page_text, 'Check for page_text.'); - $this->assertEquals($params['donate_link_text'], $pcp->donate_link_text, 'Check for donate_link_text.'); - $this->assertEquals($params['is_thermometer'], $pcp->is_thermometer, 'Check for is_thermometer.'); - $this->assertEquals($params['is_honor_roll'], $pcp->is_honor_roll, 'Check for is_honor_roll.'); - $this->assertEquals($params['goal_amount'], $pcp->goal_amount, 'Check for goal_amount.'); - $this->assertEquals($params['is_active'], $pcp->is_active, 'Check for is_active.'); + $pcpID = $this->createPCPBlock($params); + $this->getAndCheck($params, $pcpID, 'Pcp'); } public function testAddPCPNoStatus() { diff --git a/tests/phpunit/CRMTraits/PCP/PCPTestTrait.php b/tests/phpunit/CRMTraits/PCP/PCPTestTrait.php index f6d4113e99..04db4f2a3b 100644 --- a/tests/phpunit/CRMTraits/PCP/PCPTestTrait.php +++ b/tests/phpunit/CRMTraits/PCP/PCPTestTrait.php @@ -73,4 +73,22 @@ trait CRMTraits_PCP_PCPTestTrait { return $params; } + /** + * Create a pcp block for testing. + * + * @param array $params + * + * @return int + */ + protected function createPCPBlock(array $params):int { + $blockParams = $this->pcpBlockParams(); + $pcpBlock = CRM_PCP_BAO_PCPBlock::create($blockParams); + + $params = array_merge($this->pcpParams(), $params); + $params['pcp_block_id'] = $pcpBlock->id; + + $pcp = CRM_PCP_BAO_PCP::create($params); + return (int) $pcp->id; + } + } -- 2.25.1