[REF] Extend testing on membership batch entry & minor cleanup
[civicrm-core.git] / tests / phpunit / CRM / Batch / Form / EntryTest.php
index d080eee3ee9721b9d3e5c3b38d51394c43f12919..e6c74018b10453c955aa5c93cecca34813c3d3af 100644 (file)
@@ -53,7 +53,8 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
    *
    * @var string
    */
-  protected $_contactID = NULL;
+  protected $_contactID;
+
   /**
    * Contact id used in test function.
    *
@@ -75,6 +76,12 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
    */
   protected $_contactID4 = NULL;
 
+  /**
+   * Validate all financial entities before tear down.
+   * @var bool
+   */
+  protected $isValidateFinancialsOnPostAssert = TRUE;
+
   /**
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
@@ -171,9 +178,13 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
    *
    * @param string $thousandSeparator
    *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   *
    * @dataProvider getThousandSeparators
    */
-  public function testProcessMembership($thousandSeparator) {
+  public function testProcessMembership(string $thousandSeparator): void {
     $this->setCurrencySeparators($thousandSeparator);
 
     $form = new CRM_Batch_Form_Entry();
@@ -182,25 +193,28 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
 
     $params = $this->getMembershipData();
     $this->assertEquals(4500.0, $form->testProcessMembership($params));
-    $result = $this->callAPISuccess('membership', 'get');
-    $this->assertEquals(3, $result['count']);
+    $memberships = $this->callAPISuccess('Membership', 'get')['values'];
+    $this->assertCount(3, $memberships);
+
+    $this->assertNotEmpty($memberships[1]['campaign_id']);
+
     //check start dates #1 should default to 1 Jan this year, #2 should be as entered
-    $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $result['values'][1]['start_date']);
-    $this->assertEquals('2013-02-03', $result['values'][2]['start_date']);
+    $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $memberships[1]['start_date']);
+    $this->assertEquals('2013-02-03', $memberships[2]['start_date']);
 
     //check start dates #1 should default to 1 Jan this year, #2 should be as entered
-    $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][1]['end_date']);
-    $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][2]['end_date']);
-    $this->assertEquals('2013-12-01', $result['values'][3]['end_date']);
+    $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $memberships[1]['end_date']);
+    $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $memberships[2]['end_date']);
+    $this->assertEquals('2013-12-01', $memberships[3]['end_date']);
 
     //check start dates #1 should default to 1 Jan this year, #2 should be as entered
-    $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $result['values'][1]['join_date']);
-    $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $result['values'][2]['join_date']);
-    $this->assertEquals(date('Y-m-d'), $result['values'][3]['join_date']);
-    $result = $this->callAPISuccess('contribution', 'get', ['return' => ['total_amount', 'trxn_id']]);
-    $this->assertEquals(3, $result['count']);
-    foreach ($result['values'] as $key => $contribution) {
-      $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', [
+    $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $memberships[1]['join_date']);
+    $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $memberships[2]['join_date']);
+    $this->assertEquals(date('Y-m-d'), $memberships[3]['join_date']);
+    $memberships = $this->callAPISuccess('Contribution', 'get', ['return' => ['total_amount', 'trxn_id']]);
+    $this->assertEquals(3, $memberships['count']);
+    foreach ($memberships['values'] as $key => $contribution) {
+      $this->assertEquals($this->callAPISuccess('LineItem', 'getvalue', [
         'contribution_id' => $contribution['id'],
         'return' => 'line_total',
 
@@ -216,8 +230,9 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
    * @param $thousandSeparator
    *
    * @dataProvider getThousandSeparators
+   * @throws \CRM_Core_Exception
    */
-  public function testProcessContribution($thousandSeparator) {
+  public function testProcessContribution($thousandSeparator): void {
     $this->setCurrencySeparators($thousandSeparator);
     $this->offsetDefaultPriceSet();
     $form = new CRM_Batch_Form_Entry();
@@ -241,17 +256,20 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
 
   /**
    * CRM-18000 - Test start_date, end_date after renewal
+   *
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
    */
-  public function testMembershipRenewalDates() {
+  public function testMembershipRenewalDates(): void {
     $form = new CRM_Batch_Form_Entry();
-    $campaignID = Campaign::create()->setValues(['name' => 'blah', 'title' => 'blah'])->execute()->first()['id'];
     foreach ([$this->_contactID, $this->_contactID2] as $contactID) {
       $membershipParams = [
         'membership_type_id' => $this->_membershipTypeID2,
         'contact_id' => $contactID,
-        'start_date' => "01/01/2015",
-        'join_date' => "01/01/2010",
-        'end_date' => "12/31/2015",
+        'start_date' => '01/01/2015',
+        'join_date' => '01/01/2010',
+        'end_date' => '12/31/2015',
       ];
       $this->contactMembershipCreate($membershipParams);
     }
@@ -264,7 +282,6 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
     ];
     $params['field'][1]['membership_type'] = [0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2];
     $params['field'][1]['receive_date'] = date('Y-m-d');
-    $params['field'][1]['member_campaign_id'] = $campaignID;
 
     // explicitly specify start and end dates
     $params['field'][2]['membership_type'] = [0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2];
@@ -273,13 +290,13 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
     $params['field'][2]['receive_date'] = "2016-04-01";
 
     $this->assertEquals(3.0, $form->testProcessMembership($params));
-    $result = $this->callAPISuccess('membership', 'get')['values'];
+    $result = $this->callAPISuccess('Membership', 'get')['values'];
 
     // renewal dates should be from current if start_date and end_date is passed as NULL
     $this->assertEquals(date('Y-m-d'), $result[1]['start_date']);
-    $endDate = date("Y-m-d", strtotime(date("Y-m-d") . " +1 year -1 day"));
+    $endDate = date('Y-m-d', strtotime(date("Y-m-d") . " +1 year -1 day"));
     $this->assertEquals($endDate, $result[1]['end_date']);
-    $this->assertEquals(1, $result[1]['campaign_id']);
+    $this->assertEquals($params['field'][1]['member_campaign_id'], $result[1]['campaign_id']);
 
     // verify if the modified dates asserts with the dates passed above
     $this->assertEquals('2016-04-01', $result[2]['start_date']);
@@ -289,10 +306,12 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
 
   /**
    * Data provider for test process membership.
+   *
    * @return array
+   * @throws \API_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
    */
-  public function getMembershipData() {
-
+  public function getMembershipData(): array {
     return [
       'batch_id' => 4,
       'primary_profiles' => [1 => NULL, 2 => NULL, 3 => NULL],
@@ -316,6 +335,7 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
           'trxn_id' => 'TX101',
           'check_number' => NULL,
           'contribution_status_id' => 1,
+          'member_campaign_id' => $this->createCampaign(),
         ],
         2 => [
           'membership_type' => [0 => $this->_orgContactID, 1 => $this->_membershipTypeID],
@@ -403,4 +423,18 @@ class CRM_Batch_Form_EntryTest extends CiviUnitTestCase {
     ];
   }
 
+  /**
+   * Create a campaign.
+   *
+   * @return mixed
+   * @throws \API_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   */
+  private function createCampaign(): int {
+    return (int) Campaign::create()->setValues([
+      'name' => 'blah',
+      'title' => 'blah',
+    ])->execute()->first()['id'];
+  }
+
 }