Merge pull request #17125 from eileenmcnaughton/dupe1
[civicrm-core.git] / tests / phpunit / api / v4 / Action / PseudoconstantTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 *
18 */
19
20
21 namespace api\v4\Action;
22
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;
28 use Civi\Api4\Email;
29 use Civi\Api4\OptionValue;
30
31 /**
32 * @group headless
33 */
34 class PseudoconstantTest extends BaseCustomValueTest {
35
36 public function testOptionValue() {
37 $cid = Contact::create()->setCheckPermissions(FALSE)->addValue('first_name', 'bill')->execute()->first()['id'];
38 $subject = uniqid('subject');
39 OptionValue::create()
40 ->addValue('option_group_id:name', 'activity_type')
41 ->addValue('label', 'Fake')
42 ->execute();
43
44 Activity::create()
45 ->addValue('activity_type_id:name', 'Fake')
46 ->addValue('source_contact_id', $cid)
47 ->addValue('subject', $subject)
48 ->execute();
49
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')
55 ->execute();
56
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']));
60 }
61
62 public function testAddressOptions() {
63 $cid = Contact::create()->setCheckPermissions(FALSE)->addValue('first_name', 'addr')->execute()->first()['id'];
64 Address::save()
65 ->addRecord([
66 'contact_id' => $cid,
67 'state_province_id:abbr' => 'CA',
68 'country_id:label' => 'United States',
69 'street_address' => '1',
70 ])
71 ->addRecord([
72 'contact_id' => $cid,
73 'state_province_id:abbr' => 'CA',
74 'country_id:label' => 'Uruguay',
75 'street_address' => '2',
76 ])
77 ->addRecord([
78 'contact_id' => $cid,
79 'state_province_id:abbr' => 'CA',
80 'country_id:abbr' => 'ES',
81 'street_address' => '3',
82 ])
83 ->execute();
84
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')
89 ->execute();
90
91 $this->assertCount(3, $addr);
92
93 // US - California
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']);
103 // Spain - Cádiz
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']);
108 }
109
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')
120 )->execute();
121
122 $cid = Contact::create()
123 ->setCheckPermissions(FALSE)
124 ->addValue('first_name', 'col')
125 ->addValue('myPseudoconstantTest.Color:label', 'blue')
126 ->execute()->first()['id'];
127
128 $result = Contact::get()
129 ->setCheckPermissions(FALSE)
130 ->addWhere('id', '=', $cid)
131 ->addSelect('myPseudoconstantTest.Color:label', 'myPseudoconstantTest.Color')
132 ->execute()->first();
133
134 $this->assertEquals('blue', $result['myPseudoconstantTest.Color:label']);
135 $this->assertEquals('b', $result['myPseudoconstantTest.Color']);
136 }
137
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'];
153
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']);
164
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']);
175 }
176
177 }