Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
5 | class api_v3_CustomValueTest extends CiviUnitTestCase { | |
6 | protected $_apiversion; | |
7 | protected $individual; | |
8 | protected $params; | |
9 | protected $ids; | |
10 | public $_eNoticeCompliant = TRUE; | |
11 | public $DBResetRequired = FALSE; | |
12 | ||
13 | function setUp() { | |
14 | parent::setUp(); | |
15 | $this->_apiversion = 3; | |
16 | $this->individual = $this->individualCreate(); | |
17 | $this->params = array( | |
18 | 'version' => $this->_apiversion, | |
19 | 'entity_id' => $this->individual, | |
20 | 'debug' => 1, | |
21 | ); | |
22 | $this->ids['single'] = $this->entityCustomGroupWithSingleFieldCreate('mySingleField', 'Contacts'); | |
23 | $this->ids['multi'] = $this->CustomGroupMultipleCreateWithFields(); | |
24 | $this->ids['multi2'] = $this->CustomGroupMultipleCreateWithFields(array('title' => 'group2')); | |
25 | } | |
26 | ||
27 | function tearDown() { | |
28 | $tablesToTruncate = array( | |
29 | 'civicrm_email', | |
30 | 'civicrm_custom_field', | |
31 | 'civicrm_custom_group', | |
32 | 'civicrm_contact', | |
33 | ); | |
34 | ||
35 | // true tells quickCleanup to drop any tables that might have been created in the test | |
36 | $this->quickCleanup($tablesToTruncate, TRUE); | |
37 | } | |
38 | ||
39 | public function testCreateCustomValue() { | |
40 | ||
41 | $params = array( | |
42 | 'custom_' . $this->ids['single']['custom_field_id'] => 'customString') + $this->params; | |
43 | ||
44 | $result = civicrm_api('custom_value', 'create', $params); | |
45 | $this->assertAPISuccess($result, 'In line ' . __LINE__); | |
46 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); | |
47 | $result = civicrm_api('custom_value', 'get', $params); | |
48 | } | |
49 | ||
50 | public function testGetMultipleCustomValues() { | |
51 | ||
52 | $description = "/*this demonstrates the use of CustomValue get"; | |
53 | ||
54 | $params = array( | |
55 | 'first_name' => 'abc3', | |
56 | 'last_name' => 'xyz3', | |
57 | 'contact_type' => 'Individual', | |
58 | 'email' => 'man3@yahoo.com', | |
59 | 'version' => $this->_apiversion, | |
60 | 'custom_' . $this->ids['single']['custom_field_id'] => "value 1", | |
61 | 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 2", | |
62 | 'custom_' . $this->ids['multi']['custom_field_id'][1] => "warm beer", | |
63 | 'custom_' . $this->ids['multi']['custom_field_id'][2] => "fl* w*", | |
64 | 'custom_' . $this->ids['multi2']['custom_field_id'][2] => "vegemite", | |
65 | ); | |
66 | ||
67 | ||
68 | $result = civicrm_api('Contact', 'create', $params); | |
69 | $this->assertAPISuccess($result, __LINE__); | |
70 | $contact_id = $result['id']; | |
71 | $result = civicrm_api('Contact', 'create', | |
72 | array( | |
73 | 'contact_type' => 'Individual', | |
74 | 'id' => $contact_id, | |
75 | 'version' => 3, | |
76 | 'custom_' . $this->ids['multi']['custom_field_id'][0] => "value 3", | |
77 | 'custom_' . $this->ids['multi2']['custom_field_id'][0] => "coffee", | |
78 | 'custom_' . $this->ids['multi2']['custom_field_id'][1] => "value 4", | |
79 | ) | |
80 | ); | |
81 | ||
82 | $params = array( | |
83 | 'id' => $result['id'], 'version' => 3, | |
84 | 'entity_id' => $result['id'], | |
85 | ); | |
86 | ||
87 | $result = civicrm_api('CustomValue', 'Get', $params); | |
88 | ||
89 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description); | |
90 | $params['format.field_names'] = 1; | |
91 | $resultformatted = civicrm_api('CustomValue', 'Get', $params); | |
92 | $this->documentMe($params, $resultformatted, __FUNCTION__, __FILE__, "utilises field names", 'formatFieldName'); | |
93 | // delete the contact | |
94 | civicrm_api('contact', 'delete', array('version' => 1, 'id' => $contact_id)); | |
95 | ||
96 | $this->assertAPISuccess( $result); | |
97 | $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['2'], "In line " . __LINE__); | |
98 | $this->assertEquals('coffee', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['latest'], "In line " . __LINE__); | |
99 | $this->assertEquals($this->ids['multi2']['custom_field_id'][0], $result['values'][$this->ids['multi2']['custom_field_id'][0]]['id'], "In line " . __LINE__); | |
100 | $this->assertEquals('', $result['values'][$this->ids['multi2']['custom_field_id'][0]]['1'], "In line " . __LINE__); | |
101 | $this->assertEquals($contact_id, $result['values'][$this->ids['multi2']['custom_field_id'][0]]['entity_id'], "In line " . __LINE__); | |
102 | $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['0'], "In line " . __LINE__); | |
103 | $this->assertEquals('value 1', $result['values'][$this->ids['single']['custom_field_id']]['latest'], "In line " . __LINE__); | |
104 | $this->assertEquals('value 1', $resultformatted['values']['mySingleField']['latest'], "In line " . __LINE__); | |
105 | } | |
106 | /* | |
107 | public function testDeleteCustomValue () { | |
108 | $entity = civicrm_api('custom_value','get',$this->params); | |
109 | $result = civicrm_api('custom_value','delete',array('version' =>3,'id' => $entity['id'])); | |
110 | $this->documentMe($this->params,$result,__FUNCTION__,__FILE__); | |
111 | $this->assertAPISuccess($result, 'In line ' . __LINE__ ); | |
112 | $checkDeleted = civicrm_api('survey','get',array('version' =>3,)); | |
113 | $this->assertEquals( 0, $checkDeleted['count'], 'In line ' . __LINE__ ); | |
114 | ||
115 | } | |
116 | ||
117 | public function testGetCustomValueChainDelete () { | |
118 | $description = "demonstrates get + delete in the same call"; | |
119 | $subfile = 'ChainedGetDelete'; | |
120 | $params = array( | |
121 | 'version' =>3, | |
122 | 'title' => "survey title", | |
123 | 'api.survey.delete' => 1); | |
124 | $result = civicrm_api('survey','create',$this->params); | |
125 | $result = civicrm_api('survey','get',$params ); | |
126 | $this->documentMe($params,$result,__FUNCTION__,__FILE__,$description,$subfile); | |
127 | $this->assertAPISuccess($result, 'In line ' . __LINE__ ); | |
128 | $this->assertEquals( 0,civicrm_api('survey','getcount',array('version' => 3)), 'In line ' . __LINE__ ); | |
129 | ||
130 | } | |
131 | */ | |
132 | } | |
133 |