4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 namespace api\v
4\Action
;
23 use Civi\Api4\Address
;
24 use Civi\Api4\Contact
;
25 use Civi\Api4\Activity
;
26 use Civi\Api4\CustomField
;
27 use Civi\Api4\CustomGroup
;
29 use Civi\Api4\OptionValue
;
34 class PseudoconstantTest
extends BaseCustomValueTest
{
36 public function testOptionValue() {
37 $cid = Contact
::create()->setCheckPermissions(FALSE)->addValue('first_name', 'bill')->execute()->first()['id'];
38 $subject = uniqid('subject');
40 ->addValue('option_group_id:name', 'activity_type')
41 ->addValue('label', 'Fake')
45 ->addValue('activity_type_id:name', 'Fake')
46 ->addValue('source_contact_id', $cid)
47 ->addValue('subject', $subject)
50 $act = Activity
::get()
51 ->addWhere('activity_type_id:name', '=', 'Fake')
52 ->addWhere('subject', '=', $subject)
53 ->addSelect('activity_type_id:label')
54 ->addSelect('activity_type_id')
57 $this->assertCount(1, $act);
58 $this->assertEquals('Fake', $act[0]['activity_type_id:label']);
59 $this->assertTrue(is_numeric($act[0]['activity_type_id']));
62 public function testAddressOptions() {
63 $cid = Contact
::create()->setCheckPermissions(FALSE)->addValue('first_name', 'addr')->execute()->first()['id'];
67 'state_province_id:abbr' => 'CA',
68 'country_id:label' => 'United States',
69 'street_address' => '1',
73 'state_province_id:abbr' => 'CA',
74 'country_id:label' => 'Uruguay',
75 'street_address' => '2',
79 'state_province_id:abbr' => 'CA',
80 'country_id:abbr' => 'ES',
81 'street_address' => '3',
85 $addr = Address
::get()
86 ->addWhere('contact_id', '=', $cid)
87 ->addSelect('state_province_id:abbr', 'state_province_id:name', 'country_id:label', 'country_id:name')
88 ->addOrderBy('street_address')
91 $this->assertCount(3, $addr);
94 $this->assertEquals('CA', $addr[0]['state_province_id:abbr']);
95 $this->assertEquals('California', $addr[0]['state_province_id:name']);
96 $this->assertEquals('US', $addr[0]['country_id:name']);
97 $this->assertEquals('United States', $addr[0]['country_id:label']);
98 // Uruguay - Canelones
99 $this->assertEquals('CA', $addr[1]['state_province_id:abbr']);
100 $this->assertEquals('Canelones', $addr[1]['state_province_id:name']);
101 $this->assertEquals('UY', $addr[1]['country_id:name']);
102 $this->assertEquals('Uruguay', $addr[1]['country_id:label']);
104 $this->assertEquals('CA', $addr[2]['state_province_id:abbr']);
105 $this->assertEquals('Cádiz', $addr[2]['state_province_id:name']);
106 $this->assertEquals('ES', $addr[2]['country_id:name']);
107 $this->assertEquals('Spain', $addr[2]['country_id:label']);
110 public function testCustomOptions() {
111 CustomGroup
::create()
112 ->setCheckPermissions(FALSE)
113 ->addValue('name', 'myPseudoconstantTest')
114 ->addValue('extends', 'Individual')
115 ->addChain('field', CustomField
::create()
116 ->addValue('custom_group_id', '$id')
117 ->addValue('option_values', ['r' => 'red', 'g' => 'green', 'b' => 'blue'])
118 ->addValue('label', 'Color')
119 ->addValue('html_type', 'Select')
122 $cid = Contact
::create()
123 ->setCheckPermissions(FALSE)
124 ->addValue('first_name', 'col')
125 ->addValue('myPseudoconstantTest.Color:label', 'blue')
126 ->execute()->first()['id'];
128 $result = Contact
::get()
129 ->setCheckPermissions(FALSE)
130 ->addWhere('id', '=', $cid)
131 ->addSelect('myPseudoconstantTest.Color:label', 'myPseudoconstantTest.Color')
132 ->execute()->first();
134 $this->assertEquals('blue', $result['myPseudoconstantTest.Color:label']);
135 $this->assertEquals('b', $result['myPseudoconstantTest.Color']);
138 public function testJoinOptions() {
139 $cid1 = Contact
::create()->setCheckPermissions(FALSE)
140 ->addValue('first_name', 'Tom')
141 ->addValue('gender_id:label', 'Male')
142 ->addChain('email', Email
::create()->setValues(['contact_id' => '$id', 'email' => 'tom@example.com', 'location_type_id:name' => 'Work']))
143 ->execute()->first()['id'];
144 $cid2 = Contact
::create()->setCheckPermissions(FALSE)
145 ->addValue('first_name', 'Sue')
146 ->addValue('gender_id:name', 'Female')
147 ->addChain('email', Email
::create()->setValues(['contact_id' => '$id', 'email' => 'sue@example.com', 'location_type_id:name' => 'Home']))
148 ->execute()->first()['id'];
149 $cid3 = Contact
::create()->setCheckPermissions(FALSE)
150 ->addValue('first_name', 'Pat')
151 ->addChain('email', Email
::create()->setValues(['contact_id' => '$id', 'email' => 'pat@example.com', 'location_type_id:name' => 'Home']))
152 ->execute()->first()['id'];
154 $emails = Email
::get()
155 ->addSelect('location_type_id:name', 'contact.gender_id:label', 'email', 'contact_id')
156 ->addWhere('contact_id', 'IN', [$cid1, $cid2, $cid3])
157 ->addWhere('contact.gender_id:label', 'IN', ['Male', 'Female'])
158 ->execute()->indexBy('contact_id');
159 $this->assertCount(2, $emails);
160 $this->assertEquals('Work', $emails[$cid1]['location_type_id:name']);
161 $this->assertEquals('Home', $emails[$cid2]['location_type_id:name']);
162 $this->assertEquals('Male', $emails[$cid1]['contact.gender_id:label']);
163 $this->assertEquals('Female', $emails[$cid2]['contact.gender_id:label']);
165 $emails = Email
::get()
166 ->addSelect('location_type_id:name', 'contact.gender_id:label', 'email', 'contact_id')
167 ->addWhere('contact_id', 'IN', [$cid1, $cid2, $cid3])
168 ->addWhere('location_type_id:name', 'IN', ['Home'])
169 ->execute()->indexBy('contact_id');
170 $this->assertCount(2, $emails);
171 $this->assertEquals('Home', $emails[$cid2]['location_type_id:name']);
172 $this->assertEquals('Home', $emails[$cid3]['location_type_id:name']);
173 $this->assertEquals('Female', $emails[$cid2]['contact.gender_id:label']);
174 $this->assertNull($emails[$cid3]['contact.gender_id:label']);