Merge branch 'master' into master-civimail-abtest
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantTest.php
index 5a2cf37e46d2a4e986404fbf4b70ebd5e61a568e..8aab82c20efdc1d8a1ac6665b45c767e6347d563 100644 (file)
@@ -34,6 +34,10 @@ require_once 'CiviTest/CiviUnitTestCase.php';
  */
 require_once 'CRM/Utils/DeprecatedUtils.php';
 require_once 'CiviTest/CiviUnitTestCase.php';
+
+/**
+ * Class api_v3_ParticipantTest
+ */
 class api_v3_ParticipantTest extends CiviUnitTestCase {
 
   protected $_apiversion;
@@ -46,6 +50,9 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   protected $_individualId;
   protected $_params;
 
+  /**
+   * @return array
+   */
   function get_info() {
     return array(
       'name' => 'Participant Create',
@@ -91,7 +98,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with complete array + custom field
+   * Check with complete array + custom field
    * Note that the test is written on purpose without any
    * variables specific to participant so it can be replicated into other entities
    * and / or moved to the automated test suite
@@ -117,7 +124,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   ///////////////// civicrm_participant_get methods
 
   /**
-   * check with wrong params type
+   * Check with wrong params type
    */
   function testGetWrongParamsType() {
     $params = 'a string';
@@ -132,7 +139,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with participant_id
+   * Check with participant_id
    */
   function testGetParticipantIdOnly() {
     $params = array(
@@ -161,7 +168,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with params id
+   * Check with params id
    */
   function testGetParamsAsIdOnly() {
     $params = array(
@@ -175,7 +182,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with params id
+   * Check with params id
    */
   function testGetNestedEventGet() {
     //create a second event & add participant to it.
@@ -195,9 +202,10 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
     $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon');
     $this->assertEquals($this->_eventID, $result['values'][$this->_participantID]['api.event.get']['id']);
   }
-  /*
-     * Check Participant Get respects return properties
-     */
+
+  /**
+   * Check Participant Get respects return properties
+   */
   function testGetWithReturnProperties() {
     $params = array(
       'contact_id' => $this->_contactID,      'return.status_id' => 1,
@@ -209,7 +217,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with contact_id
+   * Check with contact_id
    */
   function testGetContactIdOnly() {
     $params = array(
@@ -234,7 +242,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with event_id
+   * Check with event_id
    * fetch first record
    */
   function testGetMultiMatchReturnFirst() {
@@ -247,7 +255,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with event_id
+   * Check with event_id
    * in v3 this should return all participants
    */
   function testGetMultiMatchNoReturnFirst() {
@@ -271,7 +279,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with participant_id
+   * Check with participant_id
    */
   function testSearchParticipantIdOnly() {
     $params = array(
@@ -284,7 +292,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with contact_id
+   * Check with contact_id
    */
   function testSearchContactIdOnly() {
     // Should get 2 participant records for this contact.
@@ -297,7 +305,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with event_id
+   * Check with event_id
    */
   function testSearchByEvent() {
     // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned.
@@ -316,7 +324,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with event_id
+   * Check with event_id
    * fetch with limit
    */
   function testSearchByEventWithLimit() {
@@ -341,7 +349,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with event_id
+   * Check with event_id
    */
   function testCreateMissingContactID() {
     $params = array(
@@ -351,7 +359,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with contact_id
+   * Check with contact_id
    * without event_id
    */
   function testCreateMissingEventID() {
@@ -362,7 +370,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with contact_id & event_id
+   * Check with contact_id & event_id
    */
   function testCreateEventIdOnly() {
     $params = array(
@@ -374,7 +382,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with complete array
+   * Check with complete array
    */
   function testCreateAllParams() {
     $params = $this->_params;
@@ -384,9 +392,10 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
     // assertDBState compares expected values in $match to actual values in the DB
     $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params);
   }
-  /*
-     * Test to check if receive date is being changed per CRM-9763
-     */
+
+  /**
+   * Test to check if receive date is being changed per CRM-9763
+   */
   function testCreateUpdateReceiveDate() {
     $participant = $this->callAPISuccess('participant', 'create', $this->_params);
     $update = array(
@@ -396,9 +405,10 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
     $this->callAPISuccess('participant', 'create', $update);
     $this->getAndCheck(array_merge($this->_params, $update), $participant['id'], 'participant');
   }
-  /*
-     * Test to check if participant fee level is being changed per CRM-9781
-     */
+
+  /**
+   * Test to check if participant fee level is being changed per CRM-9781
+   */
   function testCreateUpdateParticipantFeeLevel() {
     $myParams = $this->_params + array('participant_fee_level' => CRM_Core_DAO::VALUE_SEPARATOR . "fee" . CRM_Core_DAO::VALUE_SEPARATOR);
     $participant = $this->callAPISuccess('participant', 'create', $myParams);
@@ -414,8 +424,102 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
 
     $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
   }
+
+  /**
+   * Test the line items for participant fee with multiple price field values.
+   */
+  function testCreateParticipantLineItems() {
+    // Create a price set for this event.
+
+    $priceset = $this->callAPISuccess('PriceSet', 'create', array(
+      'name' => 'my_price_set',
+      'title' => 'My Price Set',
+      'is_active' => 1,
+      'extends' => 1,
+      'financial_type_id' => 4,
+      // 'entity' => array('civicrm_event' => array($this->_eventID)),
+    ));
+
+    // Add the price set to the event with another API call.
+    // I tried to do this at once, but it did not work.
+
+    $priceset = $this->callAPISuccess('PriceSet', 'create', array(
+      'entity_table' => 'civicrm_event',
+      'entity_id' => $this->_eventID,
+      'id' => $priceset['id'],
+    ));
+
+    $pricefield = $this->callAPISuccess('PriceField', 'create', array(
+      'price_set_id' => $priceset['id'],
+      'name' => 'mypricefield',
+      'label' => 'My Price Field',
+      'html_type' => 'Text',
+      'is_enter_qty' => 1,
+      'is_display_amounts' => 1,
+      'is_active' => 1,
+    ));
+
+    $pfv1 = $this->callAPISuccess('PriceFieldValue', 'create', array(
+      'price_field_id' => $pricefield['id'],
+      'name' => 'pricefieldvalue1',
+      'label' => 'pricefieldvalue1',
+      'amount' => 20,
+      'is_active' => 1,
+      'financial_type_id' => 4,
+    ));
+
+    $pfv2 = $this->callAPISuccess('PriceFieldValue', 'create', array(
+      'price_field_id' => $pricefield['id'],
+      'name' => 'pricefieldvalue2',
+      'label' => 'pricefieldvalue2',
+      'amount' => 5,
+      'is_active' => 1,
+      'financial_type_id' => 4,
+    ));
+
+    // pay 2 times price field value 1, and 2 times price field value 2.
+    $myParams = $this->_params + array('participant_fee_level' => CRM_Core_DAO::VALUE_SEPARATOR . "pricefieldvalue1 - 2" . CRM_Core_DAO::VALUE_SEPARATOR . "pricefieldvalue2 - 2" . CRM_Core_DAO::VALUE_SEPARATOR);
+    $participant = $this->callAPISuccess('participant', 'create', $myParams);
+
+    // expect 2 line items.
+    $lineItems = $this->callAPISuccess('LineItem', 'get', array(
+      'entity_id' => $participant['id'],
+      'entity_table' => 'civicrm_participant',
+    ));
+
+    $this->assertEquals(2, $lineItems['count']);
+
+    // Check quantity, label and unit price of lines.
+    // TODO: These assertions depend on the order of the line items, which is
+    // technically incorrect.
+
+    $lineItem = array_pop($lineItems['values']);
+    $this->assertEquals(2, $lineItem['qty']);
+    $this->assertEquals(5, $lineItem['unit_price']);
+    $this->assertEquals('pricefieldvalue2', $lineItem['label']);
+
+    $lineItem = array_pop($lineItems['values']);
+    $this->assertEquals(2, $lineItem['qty']);
+    $this->assertEquals(20, $lineItem['unit_price']);
+    $this->assertEquals('pricefieldvalue1', $lineItem['label']);
+
+    // Cleanup
+    $this->callAPISuccess('participant', 'delete', array('id' => $participant['id']));
+
+    // TODO: I think the price set should be removed, but I don't know how
+    // to decouple it properly from the event. For the moment, I'll just comment
+    // out the lines below.
+
+    /*
+    $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv1['id']));
+    $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv2['id']));
+    $this->callAPISuccess('PriceField', 'delete', array('id' => $pricefield['id']));
+    $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceset['id']));
+    */
+  }
+
   /**
-   * check with complete array
+   * Check with complete array
    */
   function testUpdate() {
     $participantId = $this->participantCreate(array(
@@ -468,7 +572,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with empty array
+   * Check with empty array
    */
   function testUpdateEmptyParams() {
     $params = array();
@@ -477,7 +581,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check without event_id
+   * Check without event_id
    */
   function testUpdateWithoutEventId() {
     $participantId = $this->participantCreate(array('contactID' => $this->_individualId, 'eventID' => $this->_eventID));
@@ -495,7 +599,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with Invalid participantId
+   * Check with Invalid participantId
    */
   function testUpdateWithWrongParticipantId() {
     $params = array(
@@ -509,7 +613,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with Invalid ContactId
+   * Check with Invalid ContactId
    */
   function testUpdateWithWrongContactId() {
     $participantId = $this->participantCreate(array(
@@ -547,7 +651,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check with participant_id
+   * Check with participant_id
    */
   function testParticipantDelete() {
     $params = array(
@@ -558,7 +662,7 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
   }
 
   /**
-   * check without participant_id
+   * Check without participant_id
    * and with event_id
    * This should return an error because required param is missing..
    */
@@ -568,9 +672,10 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
     $participant = $this->callAPIFailure('participant', 'delete', $params);
     $this->assertNotNull($participant['error_message']);
   }
-  /*
-    * delete with a get - a 'criteria delete'
-    */
+
+  /**
+   * Delete with a get - a 'criteria delete'
+   */
   function testNestedDelete() {
     $description  = "Criteria delete by nesting a GET & a DELETE";
     $subfile      = "NestedDelete";
@@ -581,9 +686,10 @@ class api_v3_ParticipantTest extends CiviUnitTestCase {
     $check = $this->callAPISuccess('participant', 'getcount', array());
     $this->assertEquals(1, $check,"only one participant should be left. line " . __LINE__);
   }
-  /*
-     * Test creation of a participant with an associated contribution
-     */
+
+  /**
+   * Test creation of a participant with an associated contribution
+   */
   function testCreateParticipantWithPayment() {
     $description = "single function to create contact w partipation & contribution. Note that in the
       case of 'contribution' the 'create' is implied (api.contribution.create)";