Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
3 | require_once 'CiviTest/Contact.php'; | |
4 | class CRM_Core_BAO_UFFieldTest extends CiviUnitTestCase { | |
5 | ||
6 | function setUp() { | |
7 | parent::setUp(); | |
8 | ||
9 | $this->quickCleanup(array('civicrm_uf_group', 'civicrm_uf_field')); | |
10 | } | |
11 | ||
12 | /** | |
13 | * When passing in a GID, fields should be omitted if they already appear in the group. | |
14 | */ | |
15 | public function testGetAvailable_byGid() { | |
16 | $ufGroupId = $this->createUFGroup(array( | |
17 | array( | |
18 | 'field_name' => 'do_not_sms', | |
19 | 'field_type' => 'Contact', | |
20 | ), | |
21 | array( | |
22 | 'field_name' => 'first_name', | |
23 | 'field_type' => 'Individual', | |
24 | ), | |
25 | array( | |
26 | 'field_name' => 'amount_level', | |
27 | 'field_type' => 'Contribution', | |
28 | ), | |
29 | array( | |
30 | 'field_name' => 'participant_note', | |
31 | 'field_type' => 'Participant', | |
32 | ), | |
33 | array( | |
34 | 'field_name' => 'join_date', | |
35 | 'field_type' => 'Membership', | |
36 | ), | |
37 | array( | |
38 | 'field_name' => 'activity_date_time', | |
39 | 'field_type' => 'Activity', | |
40 | ) | |
41 | )); | |
42 | $fields = CRM_Core_BAO_UFField::getAvailableFields($ufGroupId); | |
43 | ||
44 | // Make sure that each entity has 1+ present field and 1+ missing (already-used) field | |
45 | $this->assertFalse(isset($fields['Contact']['do_not_sms'])); // already used | |
46 | $this->assertEquals('city', $fields['Contact']['city']['name']); | |
47 | ||
48 | $this->assertFalse(isset($fields['Individual']['first_name'])); // already used | |
49 | $this->assertEquals('birth_date', $fields['Individual']['birth_date']['name']); | |
50 | ||
51 | $this->assertEquals('organization_name', $fields['Organization']['organization_name']['name']); | |
52 | $this->assertEquals('legal_name', $fields['Organization']['legal_name']['name']); | |
53 | ||
54 | $this->assertFalse(isset($fields['Contribution']['amount_level'])); // already used | |
55 | $this->assertEquals('cancel_reason', $fields['Contribution']['cancel_reason']['name']); | |
56 | ||
57 | $this->assertFalse(isset($fields['Participant']['participant_note'])); // already used | |
58 | $this->assertEquals('participant_role', $fields['Participant']['participant_role']['name']); | |
59 | ||
60 | $this->assertFalse(isset($fields['Membership']['join_date'])); // already used | |
61 | $this->assertEquals('end_date', $fields['Membership']['membership_end_date']['name']); | |
62 | ||
63 | $this->assertFalse(isset($fields['Activity']['activity_date_time'])); // already used | |
64 | $this->assertEquals('subject', $fields['Activity']['activity_subject']['name']); | |
65 | ||
66 | // Make sure that some of the blacklisted fields don't appear | |
67 | $this->assertFalse(isset($fields['Contribution']['is_pay_later'])); | |
68 | $this->assertFalse(isset($fields['Participant']['participant_role_id'])); | |
69 | $this->assertFalse(isset($fields['Membership']['membership_type_id'])); | |
70 | ||
71 | // This behavior is not necessarily desirable, but it's the status quo | |
72 | $this->assertEquals('first_name', $fields['Staff']['first_name']['name']); | |
73 | } | |
74 | ||
75 | /** | |
76 | * When passing in $defaults, the currently selected field should still be included -- even if | |
77 | * it's already part of the profile. | |
78 | */ | |
79 | public function testGetAvailable_byGidDefaults() { | |
80 | $ufGroupId = $this->createUFGroup(array( | |
81 | array( | |
82 | 'field_name' => 'do_not_sms', | |
83 | 'field_type' => 'Contact', | |
84 | ), | |
85 | array( | |
86 | 'field_name' => 'first_name', | |
87 | 'field_type' => 'Individual', | |
88 | ), | |
89 | )); | |
90 | $defaults = array('field_name' => array('Individual', 'first_name')); | |
91 | $fields = CRM_Core_BAO_UFField::getAvailableFields($ufGroupId, $defaults); | |
92 | ||
93 | $this->assertFalse(isset($fields['Contact']['do_not_sms'])); // already used | |
94 | $this->assertEquals('city', $fields['Contact']['city']['name']); | |
95 | ||
96 | $this->assertEquals('first_name', $fields['Individual']['first_name']['name']); // used by me | |
97 | $this->assertEquals('birth_date', $fields['Individual']['birth_date']['name']); | |
98 | } | |
99 | ||
100 | /** | |
101 | * When omitting a GID, return a list of all fields. | |
102 | */ | |
103 | public function testGetAvailable_full() { | |
104 | $fields = CRM_Core_BAO_UFField::getAvailableFields(); | |
105 | ||
106 | // Make sure that each entity appears with at least one field | |
107 | $this->assertEquals('do_not_sms', $fields['Contact']['do_not_sms']['name']); | |
108 | $this->assertEquals('city', $fields['Contact']['city']['name']); | |
109 | ||
110 | $this->assertEquals('first_name', $fields['Individual']['first_name']['name']); | |
111 | $this->assertEquals('birth_date', $fields['Individual']['birth_date']['name']); | |
112 | ||
113 | $this->assertEquals('organization_name', $fields['Organization']['organization_name']['name']); | |
114 | $this->assertEquals('legal_name', $fields['Organization']['legal_name']['name']); | |
115 | ||
116 | $this->assertEquals('amount_level', $fields['Contribution']['amount_level']['name']); | |
117 | $this->assertEquals('cancel_reason', $fields['Contribution']['cancel_reason']['name']); | |
118 | ||
119 | $this->assertEquals('participant_note', $fields['Participant']['participant_note']['name']); | |
120 | $this->assertEquals('participant_role', $fields['Participant']['participant_role']['name']); | |
121 | ||
122 | $this->assertEquals('join_date', $fields['Membership']['join_date']['name']); | |
123 | $this->assertEquals('end_date', $fields['Membership']['membership_end_date']['name']); | |
124 | ||
125 | $this->assertEquals('activity_date_time', $fields['Activity']['activity_date_time']['name']); | |
126 | $this->assertEquals('subject', $fields['Activity']['activity_subject']['name']); | |
127 | ||
128 | // Make sure that some of the blacklisted fields don't appear | |
129 | $this->assertFalse(isset($fields['Contribution']['is_pay_later'])); | |
130 | $this->assertFalse(isset($fields['Participant']['participant_role_id'])); | |
131 | $this->assertFalse(isset($fields['Membership']['membership_type_id'])); | |
132 | ||
133 | // This behavior is not necessarily desirable, but it's the status quo | |
134 | $this->assertEquals('first_name', $fields['Staff']['first_name']['name']); | |
135 | } | |
136 | ||
137 | /** | |
138 | * When omitting a GID, return a list of all fields. | |
139 | */ | |
140 | public function testGetAvailableFlat() { | |
141 | $fields = CRM_Core_BAO_UFField::getAvailableFieldsFlat(); | |
142 | ||
143 | // Make sure that each entity appears with at least one field | |
144 | $this->assertEquals('Contact', $fields['do_not_sms']['field_type']); | |
145 | $this->assertEquals('Contact', $fields['city']['field_type']); | |
146 | ||
147 | $this->assertEquals('Individual', $fields['first_name']['field_type']); | |
148 | $this->assertEquals('Individual', $fields['birth_date']['field_type']); | |
149 | ||
150 | $this->assertEquals('Organization', $fields['organization_name']['field_type']); | |
151 | $this->assertEquals('Organization', $fields['legal_name']['field_type']); | |
152 | ||
153 | $this->assertEquals('Contribution', $fields['amount_level']['field_type']); | |
154 | $this->assertEquals('Contribution', $fields['cancel_reason']['field_type']); | |
155 | ||
156 | $this->assertEquals('Participant', $fields['participant_note']['field_type']); | |
157 | $this->assertEquals('Participant', $fields['participant_role']['field_type']); | |
158 | ||
159 | $this->assertEquals('Membership', $fields['join_date']['field_type']); | |
160 | $this->assertEquals('Membership', $fields['membership_end_date']['field_type']); | |
161 | ||
162 | $this->assertEquals('Activity', $fields['activity_date_time']['field_type']); | |
163 | $this->assertEquals('Activity', $fields['activity_subject']['field_type']); | |
164 | ||
165 | // Make sure that some of the blacklisted fields don't appear | |
166 | $this->assertFalse(isset($fields['is_pay_later'])); | |
167 | $this->assertFalse(isset($fields['participant_role_id'])); | |
168 | $this->assertFalse(isset($fields['membership_type_id'])); | |
169 | } | |
170 | ||
171 | /** | |
172 | * Make sure that the existence of a profile doesn't break listing all fields | |
173 | * | |
174 | public function testGetAvailable_mixed() { | |
175 | // FIXME | |
176 | $this->testGetAvailable_full(); | |
177 | // $this->testGetAvailable_byGid(); | |
178 | $this->testGetAvailable_full(); | |
179 | // $this->testGetAvailable_byGid(); | |
180 | } // */ | |
181 | ||
182 | /** | |
183 | * @param array $fields list of fields to include in the profile | |
184 | * @return int field id | |
185 | */ | |
186 | protected function createUFGroup($fields) { | |
187 | $ufGroup = CRM_Core_DAO::createTestObject('CRM_Core_DAO_UFGroup'); | |
188 | $this->assertTrue(is_numeric($ufGroup->id)); | |
189 | ||
190 | foreach ($fields as $field) { | |
191 | $defaults = array( | |
192 | 'version' => 3, | |
193 | 'uf_group_id' => $ufGroup->id, | |
194 | 'visibility' => 'Public Pages and Listings', | |
195 | 'weight' => 1, | |
196 | 'label' => 'Label for ' . $field['field_name'], | |
197 | 'is_searchable' => 1, | |
198 | 'is_active' => 1, | |
199 | 'location_type_id' => NULL, | |
200 | ); | |
201 | $params = array_merge($field, $defaults); | |
202 | $ufField = civicrm_api('UFField', 'create', $params); | |
203 | $this->assertAPISuccess($ufField); | |
204 | } | |
205 | ||
206 | return $ufGroup->id; | |
207 | } | |
208 | } |