Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
fc928539 | 2 | /** |
b6708aeb | 3 | +--------------------------------------------------------------------+ |
06a1bc01 | 4 | | CiviCRM version 4.5 | |
b6708aeb | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
b6708aeb | 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 | */ | |
6a488035 TO |
27 | |
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | class api_v3_CustomValueTest extends CiviUnitTestCase { | |
fc928539 | 30 | protected $_apiversion =3; |
6a488035 TO |
31 | protected $individual; |
32 | protected $params; | |
33 | protected $ids; | |
b7c9bc4c | 34 | |
6a488035 TO |
35 | public $DBResetRequired = FALSE; |
36 | ||
37 | function setUp() { | |
38 | parent::setUp(); | |
6a488035 TO |
39 | $this->individual = $this->individualCreate(); |
40 | $this->params = array( | |
6a488035 | 41 | 'entity_id' => $this->individual, |
6a488035 TO |
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() { | |
6a488035 TO |
61 | $params = array( |
62 | 'custom_' . $this->ids['single']['custom_field_id'] => 'customString') + $this->params; | |
63 | ||
fb32de45 | 64 | $result = $this->callAPIAndDocument('custom_value', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 | 65 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
fc928539 | 66 | $result = $this->callAPISuccess('custom_value', 'get', $params); |
6a488035 TO |
67 | } |
68 | ||
69 | public function testGetMultipleCustomValues() { | |
70 | ||
71 | $description = "/*this demonstrates the use of CustomValue get"; | |
72 | ||
73 | $params = array( | |
74 | 'first_name' => 'abc3', | |
75 | 'last_name' => 'xyz3', | |
76 | 'contact_type' => 'Individual', | |
77 | 'email' => 'man3@yahoo.com', | |
6a488035 TO |
78 | 'custom_' . $this->ids['single']['custom_field_id'] => "value 1", |
79 | 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 2", | |
80 | 'custom_' . $this->ids['multi']['custom_field_id'][1] => "warm beer", | |
81 | 'custom_' . $this->ids['multi']['custom_field_id'][2] => "fl* w*", | |
82 | 'custom_' . $this->ids['multi2']['custom_field_id'][2] => "vegemite", | |
83 | ); | |
84 | ||
85 | ||
fc928539 | 86 | $result = $this->callAPISuccess('Contact', 'create', $params); |
6a488035 | 87 | $contact_id = $result['id']; |
b422b715 | 88 | $firstCustomField = $this->ids['multi']['custom_field_id'][0]; |
89 | $secondCustomField = $this->ids['multi2']['custom_field_id'][0]; | |
90 | $thirdCustomField = $this->ids['multi2']['custom_field_id'][1]; | |
91 | $createParams = array( | |
92 | 'contact_type' => 'Individual', | |
93 | 'id' => $contact_id, | |
94 | 'custom_' . $firstCustomField => "value 3", | |
95 | 'custom_' . $secondCustomField => "coffee", | |
96 | 'custom_' . $thirdCustomField => "value 4", | |
6a488035 | 97 | ); |
b422b715 | 98 | $result = $this->callAPISuccess('Contact', 'create', $createParams); |
6a488035 TO |
99 | |
100 | $params = array( | |
fc928539 | 101 | 'id' => $result['id'], |
6a488035 TO |
102 | 'entity_id' => $result['id'], |
103 | ); | |
104 | ||
fc928539 | 105 | $result = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, $description); |
6a488035 | 106 | $params['format.field_names'] = 1; |
fc928539 | 107 | $resultformatted = $this->callAPIAndDocument('CustomValue', 'Get', $params, __FUNCTION__, __FILE__, "utilises field names", 'formatFieldName'); |
6a488035 | 108 | // delete the contact |
fc928539 | 109 | $this->callAPISuccess('contact', 'delete', array('id' => $contact_id)); |
b422b715 | 110 | $this->assertEquals('coffee', $result['values'][$secondCustomField]['2']); |
111 | $this->assertEquals('coffee', $result['values'][$secondCustomField]['latest']); | |
112 | $this->assertEquals($secondCustomField, $result['values'][$secondCustomField]['id']); | |
113 | $this->assertEquals('defaultValue', $result['values'][$secondCustomField]['1']); | |
114 | $this->assertEquals($contact_id, $result['values'][$secondCustomField]['entity_id']); | |
115 | $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0']); | |
116 | $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest']); | |
117 | $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest']); | |
118 | $this->assertEquals('', $result['values'][$thirdCustomField]['1']); | |
119 | $this->assertEquals('value 4', $result['values'][$thirdCustomField]['2']); | |
6a488035 | 120 | } |
6a488035 TO |
121 | } |
122 |