Add unit test to cover pcp submission
authoreileen <emcnaughton@wikimedia.org>
Fri, 2 Oct 2020 03:22:42 +0000 (16:22 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 2 Oct 2020 22:16:39 +0000 (11:16 +1300)
tests/phpunit/CRM/Contribute/Form/ContributionTest.php
tests/phpunit/CRM/PCP/BAO/PCPTest.php
tests/phpunit/CRMTraits/PCP/PCPTestTrait.php

index 12647109169d263aa3767d5f76c410197c228f2b..3ab9be1c36013b005542c678be3f31819c56b5ca 100644 (file)
@@ -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.
    */
index adfe01f2bae43904014480a5cabe65d74c73b9b5..267b33d5db133676eebae0fa3bd779968d27d795 100644 (file)
@@ -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() {
index f6d4113e9924a13a3fd2109f29ed895de72bdfbb..04db4f2a3b5c4675677403b3db1e7eb9d41c5163 100644 (file)
@@ -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;
+  }
+
 }