Merge pull request #4863 from totten/master-phpcbf4
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueTest.php
1 <?php
2 /**
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Class api_v3_CustomValueTest
32 */
33 class api_v3_CustomValueTest extends CiviUnitTestCase {
34 protected $_apiversion = 3;
35 protected $individual;
36 protected $params;
37 protected $ids;
38
39 public $DBResetRequired = FALSE;
40
41 public function setUp() {
42 parent::setUp();
43 $this->individual = $this->individualCreate();
44 $this->params = array(
45 'entity_id' => $this->individual,
46 );
47 $this->ids['single'] = $this->entityCustomGroupWithSingleFieldCreate('mySingleField', 'Contacts');
48 $this->ids['multi'] = $this->CustomGroupMultipleCreateWithFields();
49 $this->ids['multi2'] = $this->CustomGroupMultipleCreateWithFields(array('title' => 'group2'));
50 }
51
52 public function tearDown() {
53 $tablesToTruncate = array(
54 'civicrm_email',
55 'civicrm_custom_field',
56 'civicrm_custom_group',
57 'civicrm_contact',
58 );
59
60 // true tells quickCleanup to drop any tables that might have been created in the test
61 $this->quickCleanup($tablesToTruncate, TRUE);
62 }
63
64 public function testCreateCustomValue() {
65 $params = array(
66 'custom_' . $this->ids['single']['custom_field_id'] => 'customString') + $this->params;
67
68 $result = $this->callAPIAndDocument('custom_value', 'create', $params, __FUNCTION__, __FILE__);
69 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
70 $result = $this->callAPISuccess('custom_value', 'get', $params);
71 }
72
73 public function testGetMultipleCustomValues() {
74
75 $description = "/*this demonstrates the use of CustomValue get";
76
77 $params = array(
78 'first_name' => 'abc3',
79 'last_name' => 'xyz3',
80 'contact_type' => 'Individual',
81 'email' => 'man3@yahoo.com',
82 'custom_' . $this->ids['single']['custom_field_id'] => "value 1",
83 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 2",
84 'custom_' . $this->ids['multi']['custom_field_id'][1] => "warm beer",
85 'custom_' . $this->ids['multi']['custom_field_id'][2] => "fl* w*",
86 'custom_' . $this->ids['multi2']['custom_field_id'][2] => "vegemite",
87 );
88
89 $result = $this->callAPISuccess('Contact', 'create', $params);
90 $contact_id = $result['id'];
91 $firstCustomField = $this->ids['multi']['custom_field_id'][0];
92 $secondCustomField = $this->ids['multi2']['custom_field_id'][0];
93 $thirdCustomField = $this->ids['multi2']['custom_field_id'][1];
94 $createParams = array(
95 'contact_type' => 'Individual',
96 'id' => $contact_id,
97 'custom_' . $firstCustomField => "value 3",
98 'custom_' . $secondCustomField => "coffee",
99 'custom_' . $thirdCustomField => "value 4",
100 );
101 $result = $this->callAPISuccess('Contact', 'create', $createParams);
102
103 $params = array(
104 'id' => $result['id'],
105 'entity_id' => $result['id'],
106 );
107
108 $result = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, $description);
109 $params['format.field_names'] = 1;
110 $resultformatted = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, "utilises field names", 'formatFieldName');
111 // delete the contact
112 $this->callAPISuccess('contact', 'delete', array('id' => $contact_id));
113 $this->assertEquals('coffee', $result['values'][$secondCustomField]['2']);
114 $this->assertEquals('coffee', $result['values'][$secondCustomField]['latest']);
115 $this->assertEquals($secondCustomField, $result['values'][$secondCustomField]['id']);
116 $this->assertEquals('defaultValue', $result['values'][$secondCustomField]['1']);
117 $this->assertEquals($contact_id, $result['values'][$secondCustomField]['entity_id']);
118 $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0']);
119 $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest']);
120 $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest']);
121 $this->assertEquals('', $result['values'][$thirdCustomField]['1']);
122 $this->assertEquals('value 4', $result['values'][$thirdCustomField]['2']);
123 }
124
125 public function testMultipleCustomValues() {
126 $params = array(
127 'first_name' => 'abc3',
128 'last_name' => 'xyz3',
129 'contact_type' => 'Individual',
130 'email' => 'man3@yahoo.com',
131 'custom_' . $this->ids['single']['custom_field_id'] => "value 1",
132 'custom_' . $this->ids['multi']['custom_field_id'][0] . '-1' => "multi value 1",
133 'custom_' . $this->ids['multi']['custom_field_id'][0] . '-2' => "multi value 2",
134 'custom_' . $this->ids['multi']['custom_field_id'][1] => "second multi value 1",
135 );
136
137 $result = $this->callAPISuccess('Contact', 'create', $params);
138 $contact_id = $result['id'];
139 $firstCustomField = $this->ids['multi']['custom_field_id'][1];
140 $secondCustomField = $this->ids['single']['custom_field_id'];
141 $thirdCustomField = $this->ids['multi']['custom_field_id'][0];
142
143 $createParams = array(
144 'contact_type' => 'Individual',
145 'id' => $contact_id,
146 'custom_' . $firstCustomField . '-1' => "second multi value 2",
147 'custom_' . $firstCustomField . '-2' => "second multi value 3",
148 );
149 $result = $this->callAPISuccess('Contact', 'create', $createParams);
150
151 $params = array(
152 'id' => $result['id'],
153 'entity_id' => $result['id'],
154 );
155
156 $result = $this->callAPISuccess('CustomValue', 'Get', $params);
157 // delete the contact
158 $this->callAPISuccess('contact', 'delete', array('id' => $contact_id));
159
160 $this->assertEquals($contact_id, $result['values'][$secondCustomField]['entity_id']);
161 $this->assertEquals('value 1', $result['values'][$secondCustomField]['latest']);
162 $this->assertEquals('value 1', $result['values'][$secondCustomField][0]);
163
164 $this->assertEquals($contact_id, $result['values'][$thirdCustomField]['entity_id']);
165 $this->assertEquals('multi value 1', $result['values'][$thirdCustomField][1]);
166 $this->assertEquals('multi value 2', $result['values'][$thirdCustomField][2]);
167
168 $this->assertEquals($contact_id, $result['values'][$firstCustomField]['entity_id']);
169 $this->assertEquals('second multi value 1', $result['values'][$firstCustomField][1]);
170 $this->assertEquals('', $result['values'][$firstCustomField][2]);
171 $this->assertEquals('second multi value 2', $result['values'][$firstCustomField][3]);
172 $this->assertEquals('second multi value 3', $result['values'][$firstCustomField][4]);
173 $this->assertEquals('second multi value 3', $result['values'][$firstCustomField]['latest']);
174 }
175 }