4 * Class CRM_Core_BAO_UFFieldTest
7 class CRM_Core_BAO_UFFieldTest
extends CiviUnitTestCase
{
9 public function setUp() {
12 $this->quickCleanup(array('civicrm_uf_group', 'civicrm_uf_field'));
16 * When passing in a GID, fields should be omitted if they already appear in the group.
18 public function testGetAvailable_byGid() {
19 $ufGroupId = $this->createUFGroup(array(
21 'field_name' => 'do_not_sms',
22 'field_type' => 'Contact',
25 'field_name' => 'first_name',
26 'field_type' => 'Individual',
29 'field_name' => 'amount_level',
30 'field_type' => 'Contribution',
33 'field_name' => 'participant_note',
34 'field_type' => 'Participant',
37 'field_name' => 'join_date',
38 'field_type' => 'Membership',
41 'field_name' => 'activity_date_time',
42 'field_type' => 'Activity',
45 $fields = CRM_Core_BAO_UFField
::getAvailableFields($ufGroupId);
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']);
51 $this->assertFalse(isset($fields['Individual']['first_name'])); // already used
52 $this->assertEquals('birth_date', $fields['Individual']['birth_date']['name']);
54 $this->assertEquals('organization_name', $fields['Organization']['organization_name']['name']);
55 $this->assertEquals('legal_name', $fields['Organization']['legal_name']['name']);
57 $this->assertFalse(isset($fields['Contribution']['amount_level'])); // already used
58 $this->assertEquals('cancel_reason', $fields['Contribution']['cancel_reason']['name']);
60 $this->assertFalse(isset($fields['Participant']['participant_note'])); // already used
61 $this->assertEquals('participant_role', $fields['Participant']['participant_role']['name']);
63 $this->assertFalse(isset($fields['Membership']['join_date'])); // already used
64 $this->assertEquals('end_date', $fields['Membership']['membership_end_date']['name']);
66 $this->assertFalse(isset($fields['Activity']['activity_date_time'])); // already used
67 $this->assertEquals('subject', $fields['Activity']['activity_subject']['name']);
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']));
74 // This behavior is not necessarily desirable, but it's the status quo
75 $this->assertEquals('first_name', $fields['Staff']['first_name']['name']);
79 * When passing in $defaults, the currently selected field should still be included -- even if
80 * it's already part of the profile.
82 public function testGetAvailable_byGidDefaults() {
83 $ufGroupId = $this->createUFGroup(array(
85 'field_name' => 'do_not_sms',
86 'field_type' => 'Contact',
89 'field_name' => 'first_name',
90 'field_type' => 'Individual',
93 $defaults = array('field_name' => array('Individual', 'first_name'));
94 $fields = CRM_Core_BAO_UFField
::getAvailableFields($ufGroupId, $defaults);
96 $this->assertFalse(isset($fields['Contact']['do_not_sms'])); // already used
97 $this->assertEquals('city', $fields['Contact']['city']['name']);
99 $this->assertEquals('first_name', $fields['Individual']['first_name']['name']); // used by me
100 $this->assertEquals('birth_date', $fields['Individual']['birth_date']['name']);
104 * When omitting a GID, return a list of all fields.
106 public function testGetAvailable_full() {
107 $fields = CRM_Core_BAO_UFField
::getAvailableFields();
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']);
113 $this->assertEquals('first_name', $fields['Individual']['first_name']['name']);
114 $this->assertEquals('birth_date', $fields['Individual']['birth_date']['name']);
116 $this->assertEquals('organization_name', $fields['Organization']['organization_name']['name']);
117 $this->assertEquals('legal_name', $fields['Organization']['legal_name']['name']);
119 $this->assertEquals('amount_level', $fields['Contribution']['amount_level']['name']);
120 $this->assertEquals('cancel_reason', $fields['Contribution']['cancel_reason']['name']);
122 $this->assertEquals('participant_note', $fields['Participant']['participant_note']['name']);
123 $this->assertEquals('participant_role', $fields['Participant']['participant_role']['name']);
125 $this->assertEquals('join_date', $fields['Membership']['join_date']['name']);
126 $this->assertEquals('end_date', $fields['Membership']['membership_end_date']['name']);
128 $this->assertEquals('activity_date_time', $fields['Activity']['activity_date_time']['name']);
129 $this->assertEquals('subject', $fields['Activity']['activity_subject']['name']);
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']));
136 // This behavior is not necessarily desirable, but it's the status quo
137 $this->assertEquals('first_name', $fields['Staff']['first_name']['name']);
141 * When omitting a GID, return a list of all fields.
143 public function testGetAvailableFlat() {
144 $fields = CRM_Core_BAO_UFField
::getAvailableFieldsFlat();
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']);
150 $this->assertEquals('Individual', $fields['first_name']['field_type']);
151 $this->assertEquals('Individual', $fields['birth_date']['field_type']);
153 $this->assertEquals('Organization', $fields['organization_name']['field_type']);
154 $this->assertEquals('Organization', $fields['legal_name']['field_type']);
156 $this->assertEquals('Contribution', $fields['amount_level']['field_type']);
157 $this->assertEquals('Contribution', $fields['cancel_reason']['field_type']);
159 $this->assertEquals('Participant', $fields['participant_note']['field_type']);
160 $this->assertEquals('Participant', $fields['participant_role']['field_type']);
162 $this->assertEquals('Membership', $fields['join_date']['field_type']);
163 $this->assertEquals('Membership', $fields['membership_end_date']['field_type']);
165 $this->assertEquals('Activity', $fields['activity_date_time']['field_type']);
166 $this->assertEquals('Activity', $fields['activity_subject']['field_type']);
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']));
175 * Make sure that the existence of a profile doesn't break listing all fields
177 * public function testGetAvailable_mixed() {
179 * $this->testGetAvailable_full();
180 * // $this->testGetAvailable_byGid();
181 * $this->testGetAvailable_full();
182 * // $this->testGetAvailable_byGid();
186 * @param array $fields
187 * List of fields to include in the profile.
191 protected function createUFGroup($fields) {
192 $ufGroup = CRM_Core_DAO
::createTestObject('CRM_Core_DAO_UFGroup');
193 $this->assertTrue(is_numeric($ufGroup->id
));
195 foreach ($fields as $field) {
198 'uf_group_id' => $ufGroup->id
,
199 'visibility' => 'Public Pages and Listings',
201 'label' => 'Label for ' . $field['field_name'],
202 'is_searchable' => 1,
204 'location_type_id' => NULL,
206 $params = array_merge($field, $defaults);
207 $ufField = civicrm_api('UFField', 'create', $params);
208 $this->assertAPISuccess($ufField);