Merge pull request #1277 from totten/master-reporttestcase
[civicrm-core.git] / tests / phpunit / CiviTest / Custom.php
1 <?php
2 class Custom extends CiviUnitTestCase {
3 /**
4 * Helper function to create Custom Group
5 *
6 * @return object of created group
7 */
8 static function createGroup($group, $extends = NULL, $isMultiple = FALSE) {
9 if (empty($group)) {
10 if (isset($extends) &&
11 !is_array($extends)
12 ) {
13 $extends = array($extends);
14 }
15 $group = array(
16 'title' => 'Test_Group',
17 'name' => 'test_group',
18 'extends' => $extends,
19 'style' => 'Inline',
20 'is_multiple' => $isMultiple,
21 'is_active' => 1,
22 'version' => 3,
23 );
24 }
25 else {
26 // this is done for backward compatibility
27 // with tests older than 3.2.3
28 if (isset($group['extends']) &&
29 !is_array($group['extends'])
30 ) {
31 $group['extends'] = array($group['extends']);
32 }
33 }
34
35 $result = civicrm_api('custom_group', 'create', $group);
36
37 if ($result['is_error']) {
38 return NULL;
39 }
40
41 // this is done for backward compatibility
42 // with tests older than 3.2.3
43 require_once 'CRM/Core/BAO/CustomGroup.php';
44 $group = new CRM_Core_BAO_CustomGroup();
45 $group->id = $result['id'];
46 $group->find(TRUE);
47
48 return $group;
49 }
50
51 /**
52 * Helper function to create Custom Field
53 *
54 * @return object of created field
55 */
56 static function createField($params, $fields = NULL) {
57 if (empty($params)) {
58 $params = array(
59 'custom_group_id' => $fields['groupId'],
60 'label' => empty($fields['label']) ? 'test_' . $fields['dataType'] : $fields['label'],
61 'html_type' => $fields['htmlType'],
62 'data_type' => $fields['dataType'],
63 'weight' => 4,
64 'is_required' => 1,
65 'is_searchable' => 0,
66 'is_active' => 1,
67 'version' => 3,
68 );
69 }
70
71 $result = civicrm_api('custom_field', 'create', $params);
72
73 if ($result['is_error']) {
74 print_r($result);
75 return NULL;
76 }
77
78 // this is done for backward compatibility
79 // with tests older than 3.2.3
80 $customField = new CRM_Core_DAO_CustomField();
81 $customField->id = $result['id'];
82 $customField->find(TRUE);
83
84 return $customField;
85 }
86
87 /**
88 * Helper function to delete custom field
89 *
90 * @param object of Custom Field to delete
91 */
92 static function deleteField($params) {
93 require_once 'CRM/Core/BAO/CustomField.php';
94 CRM_Core_BAO_CustomField::deleteField($params);
95 }
96
97 /**
98 * Helper function to delete custom group
99 *
100 * @param object Custom Group to delete
101 * @return boolean true if Group deleted, false otherwise
102 */
103 static function deleteGroup($params) {
104 require_once 'CRM/Core/BAO/CustomGroup.php';
105 $deleteCustomGroup = CRM_Core_BAO_CustomGroup::deleteGroup($params, TRUE);
106 return $deleteCustomGroup;
107 }
108 }
109
110