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