Merge pull request #12337 from lcdservices/dev-core-190
[civicrm-core.git] / tests / phpunit / CRM / Price / BAO / PriceSetTest.php
CommitLineData
c039f658 1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
c039f658 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
c039f658 7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
c039f658 28/**
29 * Test class for CRM_Price_BAO_PriceSet.
acb109b7 30 * @group headless
c039f658 31 */
32class CRM_Price_BAO_PriceSetTest extends CiviUnitTestCase {
33
34 /**
35 * Sets up the fixtures.
36 */
37 protected function setUp() {
38 parent::setUp();
39 }
40
41 /**
42 * Tears down the fixture.
43 */
44 protected function tearDown() {
45 }
46
47 /**
48 * Test the correct amount level is returned for an event which is not presented as a price set event.
49 *
50 * (these are denoted as 'quickConfig' in the code - but quickConfig is only supposed to refer to the
51 * configuration interface - there should be no different post process.
52 */
53 public function testGetAmountLevelTextAmount() {
54 $priceSetID = $this->eventPriceSetCreate(9);
55 $priceSet = CRM_Price_BAO_PriceSet::getCachedPriceSetDetail($priceSetID);
56 $field = reset($priceSet['fields']);
57 $params = array('priceSetId' => $priceSetID, 'price_' . $field['id'] => 1);
58 $amountLevel = CRM_Price_BAO_PriceSet::getAmountLevelText($params);
59 $this->assertEquals(CRM_Core_DAO::VALUE_SEPARATOR . 'Price Field - 1' . CRM_Core_DAO::VALUE_SEPARATOR, $amountLevel);
06f258d2
SL
60 $priceFieldValue = $this->callAPISuccess('pricefieldvalue', 'getsingle', array('price_field_id' => $field['id']));
61 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $priceFieldValue['id']));
62 $this->callAPISuccess('PriceField', 'delete', array('id' => $field['id']));
63 $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceSetID));
64 }
65
66 /**
67 * CRM-20237 Test that Copied price set does not generate long name and unneded information
68 */
69 public function testCopyPriceSet() {
70 $priceSetID = $this->eventPriceSetCreate(9);
71 $oldPriceSetInfo = $this->callAPISuccess('PriceSet', 'getsingle', array('id' => $priceSetID));
72 $newPriceSet = CRM_Price_BAO_PriceSet::copy($priceSetID);
46bf1da9 73 $this->assertEquals(substr($oldPriceSetInfo['name'], 0, 20) . 'price_set_' . $newPriceSet->id, $newPriceSet->name);
06f258d2
SL
74 $this->assertEquals($oldPriceSetInfo['title'] . ' [Copy id ' . $newPriceSet->id . ']', $newPriceSet->title);
75 $new2PriceSet = CRM_Price_BAO_PriceSet::copy($newPriceSet->id);
46bf1da9 76 $this->assertEquals(substr($newPriceSet->name, 0, 20) . 'price_set_' . $new2PriceSet->id, $new2PriceSet->name);
06f258d2
SL
77 $this->assertEquals($oldPriceSetInfo['title'] . ' [Copy id ' . $new2PriceSet->id . ']', $new2PriceSet->title);
78 $oldPriceField = $this->callAPISuccess('priceField', 'getsingle', array('price_set_id' => $priceSetID));
79 $oldPriceFieldValue = $this->callAPISuccess('priceFieldValue', 'getsingle', array('price_field_id' => $oldPriceField['id']));
80 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $oldPriceFieldValue['id']));
81 $this->callAPISuccess('PriceField', 'delete', array('id' => $oldPriceField['id']));
82 $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceSetID));
83 $newPriceField = $this->callAPISuccess('PriceField', 'getsingle', array('price_set_id' => $newPriceSet->id));
84 $newPriceFieldValue = $this->callAPISuccess('PriceFieldValue', 'getsingle', array('price_field_id' => $newPriceField['id']));
85 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $newPriceFieldValue['id']));
86 $this->callAPISuccess('PriceField', 'delete', array('id' => $newPriceField['id']));
87 $this->callAPISuccess('PriceSet', 'delete', array('id' => $newPriceSet->id));
88 $new2PriceField = $this->callAPISuccess('PriceField', 'getsingle', array('price_set_id' => $new2PriceSet->id));
89 $new2PriceFieldValue = $this->callAPISuccess('PriceFieldValue', 'getsingle', array('price_field_id' => $new2PriceField['id']));
90 $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $new2PriceFieldValue['id']));
91 $this->callAPISuccess('PriceField', 'delete', array('id' => $new2PriceField['id']));
92 $this->callAPISuccess('PriceSet', 'delete', array('id' => $new2PriceSet->id));
c039f658 93 }
94
8bd87416 95 /**
96 * Test CRM_Price_BAO_PriceSet::getMembershipCount() that return correct number of
97 * membership type occurances against it's corresponding member orgaisation
98 */
99 public function testGetMembershipCount() {
100 // create two organisations
101 $organization1 = $this->organizationCreate();
102 $organization2 = $this->organizationCreate();
103
104 // create three membership type where first two belong to same organisation
105 $membershipType1 = $this->membershipTypeCreate(array(
106 'name' => 'Membership Type 1',
107 'member_of_contact_id' => $organization1,
108 ));
109 $membershipType2 = $this->membershipTypeCreate(array(
110 'name' => 'Membership Type 2',
111 'member_of_contact_id' => $organization1,
112 ));
113 $membershipType3 = $this->membershipTypeCreate(array(
114 'name' => 'Membership Type 3',
115 'member_of_contact_id' => $organization2,
116 ));
117
118 $priceDetails = CRM_Price_BAO_PriceSet::getSetDetail(CRM_Core_DAO::getFieldValue(
119 'CRM_Price_DAO_PriceSet',
120 'default_membership_type_amount',
121 'id', 'name'
122 ));
123 // fetch price field value IDs in array('membership_type_id' => 'price_field_value_id') format
124 $priceFieldValueIDs = array();
125 foreach ($priceDetails as $priceFields) {
126 foreach ($priceFields['fields'] as $priceField) {
127 foreach ($priceField['options'] as $id => $priceFieldValue) {
128 if (in_array($priceFieldValue['membership_type_id'], array($membershipType1, $membershipType2, $membershipType3))) {
129 $priceFieldValueIDs[$priceFieldValue['membership_type_id']] = $id;
130 }
131 }
132 }
133 }
134
135 // CASE 1: when two price field value IDs of membership type that belong to same organization, are chosen
136 $sameOrgPriceFieldIDs = implode(', ', array(
137 $priceFieldValueIDs[$membershipType1],
138 $priceFieldValueIDs[$membershipType2],
139 ));
140 $occurences = CRM_Price_BAO_PriceSet::getMembershipCount($sameOrgPriceFieldIDs);
141 // total number of membership type occurences of same organisation is one
142 $this->assertEquals(1, count($occurences));
143 $this->assertTrue(array_key_exists($organization1, $occurences));
144 // assert that two membership types were chosen from same organisation
145 $this->assertEquals(2, $occurences[$organization1]);
146
147 // CASE 2: when two price field value IDs of membership type that belong to different organizations, are chosen
148 $differentOrgPriceFieldIDs = implode(', ', array(
149 $priceFieldValueIDs[$membershipType1],
150 $priceFieldValueIDs[$membershipType3],
151 ));
152 $occurences = CRM_Price_BAO_PriceSet::getMembershipCount($differentOrgPriceFieldIDs);
153 // total number of membership type occurences of different organisation is two
154 $this->assertEquals(2, count($occurences));
155 $this->assertTrue(array_key_exists($organization1, $occurences));
156 $this->assertTrue(array_key_exists($organization2, $occurences));
157 // assert that two membership types were chosen from different organisation
158 $this->assertEquals(1, $occurences[$organization1]);
159 $this->assertEquals(1, $occurences[$organization2]);
160 }
161
c039f658 162}