84fe22dab0ea07a484a55fc8f928c1b98cdc9eee
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 class api_v3_CustomValueTest extends CiviUnitTestCase {
31 protected $_apiversion;
32 protected $individual;
33 protected $params;
34 protected $ids;
35 public $_eNoticeCompliant = TRUE;
36 public $DBResetRequired = FALSE;
37
38 function setUp() {
39 parent::setUp();
40 $this->_apiversion = 3;
41 $this->individual = $this->individualCreate();
42 $this->params = array(
43 'version' => $this->_apiversion,
44 'entity_id' => $this->individual,
45 );
46 $this->ids['single'] = $this->entityCustomGroupWithSingleFieldCreate('mySingleField', 'Contacts');
47 $this->ids['multi'] = $this->CustomGroupMultipleCreateWithFields();
48 $this->ids['multi2'] = $this->CustomGroupMultipleCreateWithFields(array('title' => 'group2'));
49 }
50
51 function tearDown() {
52 $tablesToTruncate = array(
53 'civicrm_email',
54 'civicrm_custom_field',
55 'civicrm_custom_group',
56 'civicrm_contact',
57 );
58
59 // true tells quickCleanup to drop any tables that might have been created in the test
60 $this->quickCleanup($tablesToTruncate, TRUE);
61 }
62
63 public function testCreateCustomValue() {
64
65 $params = array(
66 'custom_' . $this->ids['single']['custom_field_id'] => 'customString') + $this->params;
67
68 $result = civicrm_api('custom_value', 'create', $params);
69 $this->assertAPISuccess($result, 'In line ' . __LINE__);
70 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
71 $result = civicrm_api('custom_value', 'get', $params);
72 }
73
74 public function testGetMultipleCustomValues() {
75
76 $description = "/*this demonstrates the use of CustomValue get";
77
78 $params = array(
79 'first_name' => 'abc3',
80 'last_name' => 'xyz3',
81 'contact_type' => 'Individual',
82 'email' => 'man3@yahoo.com',
83 'version' => $this->_apiversion,
84 'custom_' . $this->ids['single']['custom_field_id'] => "value 1",
85 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 2",
86 'custom_' . $this->ids['multi']['custom_field_id'][1] => "warm beer",
87 'custom_' . $this->ids['multi']['custom_field_id'][2] => "fl* w*",
88 'custom_' . $this->ids['multi2']['custom_field_id'][2] => "vegemite",
89 );
90
91
92 $result = civicrm_api('Contact', 'create', $params);
93 $this->assertAPISuccess($result, __LINE__);
94 $contact_id = $result['id'];
95 $result = civicrm_api('Contact', 'create',
96 array(
97 'contact_type' => 'Individual',
98 'id' => $contact_id,
99 'version' => 3,
100 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 3",
101 'custom_' . $this->ids['multi2']['custom_field_id'][0] => "coffee",
102 'custom_' . $this->ids['multi2']['custom_field_id'][1] => "value 4",
103 )
104 );
105
106 $params = array(
107 'id' => $result['id'], 'version' => 3,
108 'entity_id' => $result['id'],
109 );
110
111 $result = civicrm_api('CustomValue', 'Get', $params);
112
113 $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description);
114 $params['format.field_names'] = 1;
115 $resultformatted = civicrm_api('CustomValue', 'Get', $params);
116 $this->documentMe($params, $resultformatted, __FUNCTION__, __FILE__, "utilises field names", 'formatFieldName');
117 // delete the contact
118 civicrm_api('contact', 'delete', array('version' => 1, 'id' => $contact_id));
119
120 $this->assertAPISuccess( $result);
121 $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['2'], "In line " . __LINE__);
122 $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['latest'], "In line " . __LINE__);
123 $this->assertEquals($this->ids['multi2']['custom_field_id'][0], $result['values'][$this->ids['multi2']['custom_field_id'][0]]['id'], "In line " . __LINE__);
124 $this->assertEquals('', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['1'], "In line " . __LINE__);
125 $this->assertEquals($contact_id, $result['values'][$this->ids['multi2']['custom_field_id'][0]]['entity_id'], "In line " . __LINE__);
126 $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0'], "In line " . __LINE__);
127 $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest'], "In line " . __LINE__);
128 $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest'], "In line " . __LINE__);
129 }
130 /*
131 public function testDeleteCustomValue () {
132 $entity = civicrm_api('custom_value','get',$this->params);
133 $result = civicrm_api('custom_value','delete',array('version' =>3,'id' => $entity['id']));
134 $this->documentMe($this->params,$result,__FUNCTION__,__FILE__);
135 $this->assertAPISuccess($result, 'In line ' . __LINE__ );
136 $checkDeleted = civicrm_api('survey','get',array('version' =>3,));
137 $this->assertEquals( 0, $checkDeleted['count'], 'In line ' . __LINE__ );
138
139 }
140
141 public function testGetCustomValueChainDelete () {
142 $description = "demonstrates get + delete in the same call";
143 $subfile = 'ChainedGetDelete';
144 $params = array(
145 'version' =>3,
146 'title' => "survey title",
147 'api.survey.delete' => 1);
148 $result = civicrm_api('survey','create',$this->params);
149 $result = civicrm_api('survey','get',$params );
150 $this->documentMe($params,$result,__FUNCTION__,__FILE__,$description,$subfile);
151 $this->assertAPISuccess($result, 'In line ' . __LINE__ );
152 $this->assertEquals( 0,civicrm_api('survey','getcount',array('version' => 3)), 'In line ' . __LINE__ );
153
154 }
155 */
156 }
157