CRM-15988 - Update generated examples
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueTest.php
CommitLineData
6a488035 1<?php
fc928539 2/**
92915c55
TO
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 * +--------------------------------------------------------------------+
6c6e6187 26 */
6a488035
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
e9479dcf
EM
29
30/**
31 * Class api_v3_CustomValueTest
32 */
6a488035 33class api_v3_CustomValueTest extends CiviUnitTestCase {
6c6e6187 34 protected $_apiversion = 3;
6a488035
TO
35 protected $individual;
36 protected $params;
37 protected $ids;
b7c9bc4c 38
6a488035
TO
39 public $DBResetRequired = FALSE;
40
00be9182 41 public function setUp() {
6a488035 42 parent::setUp();
92915c55
TO
43 $this->individual = $this->individualCreate();
44 $this->params = array(
6a488035 45 'entity_id' => $this->individual,
6a488035
TO
46 );
47 $this->ids['single'] = $this->entityCustomGroupWithSingleFieldCreate('mySingleField', 'Contacts');
92915c55 48 $this->ids['multi'] = $this->CustomGroupMultipleCreateWithFields();
6a488035
TO
49 $this->ids['multi2'] = $this->CustomGroupMultipleCreateWithFields(array('title' => 'group2'));
50 }
51
00be9182 52 public function tearDown() {
6a488035
TO
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() {
6a488035 65 $params = array(
bed98343 66 'custom_' . $this->ids['single']['custom_field_id'] => 'customString',
67 ) + $this->params;
6a488035 68
fb32de45 69 $result = $this->callAPIAndDocument('custom_value', 'create', $params, __FUNCTION__, __FILE__);
6a488035 70 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
fc928539 71 $result = $this->callAPISuccess('custom_value', 'get', $params);
6a488035
TO
72 }
73
74 public function testGetMultipleCustomValues() {
75
5c49fee0 76 $description = "This demonstrates the use of CustomValue get to fetch single and multi-valued custom data.";
6a488035
TO
77
78 $params = array(
79 'first_name' => 'abc3',
80 'last_name' => 'xyz3',
81 'contact_type' => 'Individual',
82 'email' => 'man3@yahoo.com',
6a488035
TO
83 'custom_' . $this->ids['single']['custom_field_id'] => "value 1",
84 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 2",
85 'custom_' . $this->ids['multi']['custom_field_id'][1] => "warm beer",
86 'custom_' . $this->ids['multi']['custom_field_id'][2] => "fl* w*",
87 'custom_' . $this->ids['multi2']['custom_field_id'][2] => "vegemite",
88 );
89
fc928539 90 $result = $this->callAPISuccess('Contact', 'create', $params);
6a488035 91 $contact_id = $result['id'];
b422b715 92 $firstCustomField = $this->ids['multi']['custom_field_id'][0];
93 $secondCustomField = $this->ids['multi2']['custom_field_id'][0];
94 $thirdCustomField = $this->ids['multi2']['custom_field_id'][1];
95 $createParams = array(
96 'contact_type' => 'Individual',
97 'id' => $contact_id,
92915c55
TO
98 'custom_' . $firstCustomField => "value 3",
99 'custom_' . $secondCustomField => "coffee",
100 'custom_' . $thirdCustomField => "value 4",
6a488035 101 );
b422b715 102 $result = $this->callAPISuccess('Contact', 'create', $createParams);
6a488035
TO
103
104 $params = array(
fc928539 105 'id' => $result['id'],
6a488035
TO
106 'entity_id' => $result['id'],
107 );
108
fc928539 109 $result = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, $description);
6a488035 110 $params['format.field_names'] = 1;
a828d7b8 111 $resultformatted = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, "utilises field names", 'FormatFieldName');
6a488035 112 // delete the contact
fc928539 113 $this->callAPISuccess('contact', 'delete', array('id' => $contact_id));
b422b715 114 $this->assertEquals('coffee', $result['values'][$secondCustomField]['2']);
115 $this->assertEquals('coffee', $result['values'][$secondCustomField]['latest']);
116 $this->assertEquals($secondCustomField, $result['values'][$secondCustomField]['id']);
117 $this->assertEquals('defaultValue', $result['values'][$secondCustomField]['1']);
118 $this->assertEquals($contact_id, $result['values'][$secondCustomField]['entity_id']);
119 $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0']);
120 $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest']);
121 $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest']);
122 $this->assertEquals('', $result['values'][$thirdCustomField]['1']);
123 $this->assertEquals('value 4', $result['values'][$thirdCustomField]['2']);
6a488035 124 }
6c6e6187 125
842ee194
PN
126 public function testMultipleCustomValues() {
127 $params = array(
128 'first_name' => 'abc3',
129 'last_name' => 'xyz3',
130 'contact_type' => 'Individual',
131 'email' => 'man3@yahoo.com',
132 'custom_' . $this->ids['single']['custom_field_id'] => "value 1",
133 'custom_' . $this->ids['multi']['custom_field_id'][0] . '-1' => "multi value 1",
134 'custom_' . $this->ids['multi']['custom_field_id'][0] . '-2' => "multi value 2",
135 'custom_' . $this->ids['multi']['custom_field_id'][1] => "second multi value 1",
136 );
137
138 $result = $this->callAPISuccess('Contact', 'create', $params);
139 $contact_id = $result['id'];
140 $firstCustomField = $this->ids['multi']['custom_field_id'][1];
141 $secondCustomField = $this->ids['single']['custom_field_id'];
142 $thirdCustomField = $this->ids['multi']['custom_field_id'][0];
6c6e6187 143
842ee194
PN
144 $createParams = array(
145 'contact_type' => 'Individual',
146 'id' => $contact_id,
92915c55
TO
147 'custom_' . $firstCustomField . '-1' => "second multi value 2",
148 'custom_' . $firstCustomField . '-2' => "second multi value 3",
842ee194
PN
149 );
150 $result = $this->callAPISuccess('Contact', 'create', $createParams);
151
152 $params = array(
153 'id' => $result['id'],
154 'entity_id' => $result['id'],
155 );
6a488035 156
842ee194
PN
157 $result = $this->callAPISuccess('CustomValue', 'Get', $params);
158 // delete the contact
159 $this->callAPISuccess('contact', 'delete', array('id' => $contact_id));
6c6e6187 160
842ee194
PN
161 $this->assertEquals($contact_id, $result['values'][$secondCustomField]['entity_id']);
162 $this->assertEquals('value 1', $result['values'][$secondCustomField]['latest']);
163 $this->assertEquals('value 1', $result['values'][$secondCustomField][0]);
164
165 $this->assertEquals($contact_id, $result['values'][$thirdCustomField]['entity_id']);
166 $this->assertEquals('multi value 1', $result['values'][$thirdCustomField][1]);
167 $this->assertEquals('multi value 2', $result['values'][$thirdCustomField][2]);
6c6e6187 168
842ee194
PN
169 $this->assertEquals($contact_id, $result['values'][$firstCustomField]['entity_id']);
170 $this->assertEquals('second multi value 1', $result['values'][$firstCustomField][1]);
171 $this->assertEquals('', $result['values'][$firstCustomField][2]);
172 $this->assertEquals('second multi value 2', $result['values'][$firstCustomField][3]);
173 $this->assertEquals('second multi value 3', $result['values'][$firstCustomField][4]);
174 $this->assertEquals('second multi value 3', $result['values'][$firstCustomField]['latest']);
175 }
96025800 176
ef10e0b5 177}