Merge pull request #1260 from aadityawalawalkar/CRM-12994
[civicrm-core.git] / tests / phpunit / api / v3 / CustomValueTest.php
1 <?php
2 /**
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 class api_v3_CustomValueTest extends CiviUnitTestCase {
30 protected $_apiversion =3;
31 protected $individual;
32 protected $params;
33 protected $ids;
34 public $_eNoticeCompliant = TRUE;
35 public $DBResetRequired = FALSE;
36
37 function setUp() {
38 parent::setUp();
39 $this->individual = $this->individualCreate();
40 $this->params = array(
41 'entity_id' => $this->individual,
42 );
43 $this->ids['single'] = $this->entityCustomGroupWithSingleFieldCreate('mySingleField', 'Contacts');
44 $this->ids['multi'] = $this->CustomGroupMultipleCreateWithFields();
45 $this->ids['multi2'] = $this->CustomGroupMultipleCreateWithFields(array('title' => 'group2'));
46 }
47
48 function tearDown() {
49 $tablesToTruncate = array(
50 'civicrm_email',
51 'civicrm_custom_field',
52 'civicrm_custom_group',
53 'civicrm_contact',
54 );
55
56 // true tells quickCleanup to drop any tables that might have been created in the test
57 $this->quickCleanup($tablesToTruncate, TRUE);
58 }
59
60 public function testCreateCustomValue() {
61
62 $params = array(
63 'custom_' . $this->ids['single']['custom_field_id'] => 'customString') + $this->params;
64
65 $result = $this->callAPISuccess('custom_value', 'create', $params);
66 $this->assertAPISuccess($result, 'In line ' . __LINE__);
67 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
68 $result = $this->callAPISuccess('custom_value', 'get', $params);
69 }
70
71 public function testGetMultipleCustomValues() {
72
73 $description = "/*this demonstrates the use of CustomValue get";
74
75 $params = array(
76 'first_name' => 'abc3',
77 'last_name' => 'xyz3',
78 'contact_type' => 'Individual',
79 'email' => 'man3@yahoo.com',
80 'custom_' . $this->ids['single']['custom_field_id'] => "value 1",
81 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 2",
82 'custom_' . $this->ids['multi']['custom_field_id'][1] => "warm beer",
83 'custom_' . $this->ids['multi']['custom_field_id'][2] => "fl* w*",
84 'custom_' . $this->ids['multi2']['custom_field_id'][2] => "vegemite",
85 );
86
87
88 $result = $this->callAPISuccess('Contact', 'create', $params);
89 $this->assertAPISuccess($result, __LINE__);
90 $contact_id = $result['id'];
91 $result = $this->callAPISuccess('Contact', 'create',
92 array(
93 'contact_type' => 'Individual',
94 'id' => $contact_id,
95 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 3",
96 'custom_' . $this->ids['multi2']['custom_field_id'][0] => "coffee",
97 'custom_' . $this->ids['multi2']['custom_field_id'][1] => "value 4",
98 )
99 );
100
101 $params = array(
102 'id' => $result['id'],
103 'entity_id' => $result['id'],
104 );
105
106 $result = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, $description);
107 $params['format.field_names'] = 1;
108 $resultformatted = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, "utilises field names", 'formatFieldName');
109 // delete the contact
110 $this->callAPISuccess('contact', 'delete', array('id' => $contact_id));
111
112 $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['2'], "In line " . __LINE__);
113 $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['latest'], "In line " . __LINE__);
114 $this->assertEquals($this->ids['multi2']['custom_field_id'][0], $result['values'][$this->ids['multi2']['custom_field_id'][0]]['id'], "In line " . __LINE__);
115 $this->assertEquals('', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['1'], "In line " . __LINE__);
116 $this->assertEquals($contact_id, $result['values'][$this->ids['multi2']['custom_field_id'][0]]['entity_id'], "In line " . __LINE__);
117 $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0'], "In line " . __LINE__);
118 $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest'], "In line " . __LINE__);
119 $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest'], "In line " . __LINE__);
120 }
121 }
122