Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
7d61e75f | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
7d61e75f TO |
7 | | This work is published under the GNU AGPLv3 license with some | |
8 | | permitted exceptions and without any warranty. For full license | | |
9 | | and copyright information, see https://civicrm.org/licensing | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
19 | ||
19b53e5b C |
20 | namespace api\v4\Action; |
21 | ||
22 | use api\v4\UnitTestCase; | |
91edcf66 | 23 | use Civi\Api4\Address; |
19b53e5b C |
24 | use Civi\Api4\Contact; |
25 | ||
26 | /** | |
27 | * @group headless | |
28 | */ | |
29 | class GetExtraFieldsTest extends UnitTestCase { | |
30 | ||
f5c0f096 | 31 | public function testGetFieldsByContactType() { |
fe806431 | 32 | $getFields = Contact::getFields(FALSE)->addSelect('name')->setIncludeCustom(FALSE); |
19b53e5b | 33 | |
f5c0f096 CW |
34 | $baseFields = array_column(\CRM_Contact_BAO_Contact::fields(), 'name'); |
35 | $returnedFields = $getFields->execute()->column('name'); | |
36 | $notReturned = array_diff($baseFields, $returnedFields); | |
19b53e5b | 37 | |
f5c0f096 | 38 | // With no contact_type specified, all fields should be returned |
19b53e5b | 39 | $this->assertEmpty($notReturned); |
f5c0f096 CW |
40 | |
41 | $individualFields = $getFields->setValues(['contact_type' => 'Individual'])->execute()->column('name'); | |
42 | $this->assertNotContains('sic_code', $individualFields); | |
43 | $this->assertNotContains('contact_type', $individualFields); | |
44 | $this->assertContains('first_name', $individualFields); | |
45 | ||
46 | $organizationFields = $getFields->setValues(['contact_type' => 'Organization'])->execute()->column('name'); | |
47 | $this->assertContains('sic_code', $organizationFields); | |
48 | $this->assertNotContains('contact_type', $organizationFields); | |
49 | $this->assertNotContains('first_name', $organizationFields); | |
50 | $this->assertNotContains('household_name', $organizationFields); | |
19b53e5b C |
51 | } |
52 | ||
91edcf66 | 53 | public function testGetOptionsAddress() { |
fe806431 | 54 | $getFields = Address::getFields(FALSE)->addWhere('name', '=', 'state_province_id')->setLoadOptions(TRUE); |
91edcf66 CW |
55 | |
56 | $usOptions = $getFields->setValues(['country_id' => 1228])->execute()->first(); | |
57 | ||
58 | $this->assertContains('Alabama', $usOptions['options']); | |
59 | $this->assertNotContains('Alberta', $usOptions['options']); | |
60 | ||
61 | $caOptions = $getFields->setValues(['country_id' => 1039])->execute()->first(); | |
62 | ||
63 | $this->assertNotContains('Alabama', $caOptions['options']); | |
64 | $this->assertContains('Alberta', $caOptions['options']); | |
65 | } | |
66 | ||
37d2f985 CW |
67 | public function testGetFkFields() { |
68 | $fields = \Civi\Api4\Participant::getFields() | |
69 | ->setLoadOptions(TRUE) | |
70 | ->addWhere('name', 'IN', ['event_id', 'event_id.created_id', 'contact_id.gender_id', 'event_id.created_id.sort_name']) | |
71 | ->execute() | |
72 | ->indexBy('name'); | |
73 | ||
74 | $this->assertCount(4, $fields); | |
75 | $this->assertEquals('Participant', $fields['event_id']['entity']); | |
76 | $this->assertEquals('Event', $fields['event_id.created_id']['entity']); | |
77 | $this->assertEquals('Contact', $fields['event_id.created_id.sort_name']['entity']); | |
78 | $this->assertGreaterThan(1, count($fields['contact_id.gender_id']['options'])); | |
79 | } | |
80 | ||
19b53e5b | 81 | } |