Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0eea664b | 2 | |
aba1cd8b EM |
3 | /** |
4 | * Class CRM_Contact_BAO_ContactType_ContactSearchTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
6a488035 | 7 | class CRM_Contact_BAO_ContactType_ContactSearchTest extends CiviUnitTestCase { |
6a488035 | 8 | |
00be9182 | 9 | public function setUp() { |
6a488035 | 10 | parent::setUp(); |
92fcb95f | 11 | $students = 'indivi_student' . substr(sha1(rand()), 0, 7); |
41e1fa31 | 12 | $params = [ |
6a488035 TO |
13 | 'label' => $students, |
14 | 'name' => $students, | |
15 | // Individual | |
16 | 'parent_id' => 1, | |
17 | 'is_active' => 1, | |
41e1fa31 | 18 | ]; |
f2040bc6 | 19 | CRM_Contact_BAO_ContactType::add($params); |
6a488035 TO |
20 | $this->student = $params['name']; |
21 | ||
92fcb95f | 22 | $parents = 'indivi_parent' . substr(sha1(rand()), 0, 7); |
41e1fa31 | 23 | $params = [ |
6a488035 TO |
24 | 'label' => $parents, |
25 | 'name' => $parents, | |
26 | // Individual | |
27 | 'parent_id' => 1, | |
28 | 'is_active' => 1, | |
41e1fa31 | 29 | ]; |
f2040bc6 | 30 | CRM_Contact_BAO_ContactType::add($params); |
6a488035 TO |
31 | $this->parent = $params['name']; |
32 | ||
92fcb95f | 33 | $orgs = 'org_sponsor' . substr(sha1(rand()), 0, 7); |
41e1fa31 | 34 | $params = [ |
6a488035 TO |
35 | 'label' => $orgs, |
36 | 'name' => $orgs, | |
37 | // Organization | |
38 | 'parent_id' => 3, | |
39 | 'is_active' => 1, | |
41e1fa31 | 40 | ]; |
f2040bc6 | 41 | CRM_Contact_BAO_ContactType::add($params); |
6a488035 TO |
42 | $this->sponsor = $params['name']; |
43 | ||
41e1fa31 | 44 | $this->indiviParams = [ |
6a488035 TO |
45 | 'first_name' => 'Anne', |
46 | 'last_name' => 'Grant', | |
47 | 'contact_type' => 'Individual', | |
41e1fa31 | 48 | ]; |
f2040bc6 | 49 | $this->individual = $this->individualCreate($this->indiviParams); |
6a488035 | 50 | |
41e1fa31 | 51 | $this->individualStudentParams = [ |
6a488035 TO |
52 | 'first_name' => 'Bill', |
53 | 'last_name' => 'Adams', | |
54 | 'contact_type' => 'Individual', | |
55 | 'contact_sub_type' => $this->student, | |
41e1fa31 | 56 | ]; |
f2040bc6 | 57 | $this->individualStudent = $this->individualCreate($this->individualStudentParams); |
6a488035 | 58 | |
41e1fa31 | 59 | $this->indiviParentParams = [ |
6a488035 TO |
60 | 'first_name' => 'Alen', |
61 | 'last_name' => 'Adams', | |
62 | 'contact_type' => 'Individual', | |
63 | 'contact_sub_type' => $this->parent, | |
41e1fa31 | 64 | ]; |
f2040bc6 | 65 | $this->indiviParent = $this->individualCreate($this->indiviParentParams); |
6a488035 | 66 | |
41e1fa31 | 67 | $this->organizationParams = [ |
6a488035 TO |
68 | 'organization_name' => 'Compumentor', |
69 | 'contact_type' => 'Organization', | |
41e1fa31 | 70 | ]; |
f2040bc6 | 71 | $this->organization = $this->organizationCreate($this->organizationParams); |
6a488035 | 72 | |
41e1fa31 | 73 | $this->orgSponsorParams = [ |
6a488035 TO |
74 | 'organization_name' => 'Conservation Corp', |
75 | 'contact_type' => 'Organization', | |
76 | 'contact_sub_type' => $this->sponsor, | |
41e1fa31 | 77 | ]; |
f2040bc6 | 78 | $this->orgSponsor = $this->organizationCreate($this->orgSponsorParams); |
6a488035 | 79 | |
41e1fa31 | 80 | $this->householdParams = [ |
6a488035 TO |
81 | 'household_name' => "John Doe's home", |
82 | 'contact_type' => 'Household', | |
41e1fa31 | 83 | ]; |
f2040bc6 | 84 | $this->household = $this->householdCreate($this->householdParams); |
6a488035 TO |
85 | } |
86 | ||
c490a46a | 87 | /** |
eceb18cc | 88 | * Search with only type. |
fe482240 EM |
89 | * |
90 | * Success expected. | |
c490a46a | 91 | */ |
00be9182 | 92 | public function testSearchWithType() { |
6a488035 | 93 | |
c490a46a | 94 | // for type:Individual |
41e1fa31 | 95 | $params = ['contact_type' => 'Individual', 'version' => 3]; |
92915c55 | 96 | $result = civicrm_api('contact', 'get', $params); |
6a488035 | 97 | |
92915c55 | 98 | $individual = $result['values'][$this->individual]; |
f2040bc6 | 99 | $individualStudent = $result['values'][$this->individualStudent]; |
92915c55 | 100 | $indiviParent = $result['values'][$this->indiviParent]; |
6a488035 TO |
101 | |
102 | //asserts for type:Individual | |
a15773db | 103 | $this->assertEquals($individual['contact_id'], $this->individual); |
ba4a1892 TM |
104 | $this->assertEquals($individual['first_name'], $this->indiviParams['first_name']); |
105 | $this->assertEquals($individual['contact_type'], $this->indiviParams['contact_type']); | |
6a488035 TO |
106 | $this->assertNotContains('contact_sub_type', $individual); |
107 | ||
108 | //asserts for type:Individual subtype:Student | |
f2040bc6 | 109 | $this->assertEquals($individualStudent['contact_id'], $this->individualStudent); |
110 | $this->assertEquals($individualStudent['first_name'], $this->individualStudentParams['first_name']); | |
111 | $this->assertEquals($individualStudent['contact_type'], $this->individualStudentParams['contact_type']); | |
112 | $this->assertEquals(end($individualStudent['contact_sub_type']), $this->individualStudentParams['contact_sub_type']); | |
6a488035 TO |
113 | |
114 | //asserts for type:Individual subtype:Parent | |
a15773db | 115 | $this->assertEquals($indiviParent['contact_id'], $this->indiviParent); |
ba4a1892 TM |
116 | $this->assertEquals($indiviParent['first_name'], $this->indiviParentParams['first_name']); |
117 | $this->assertEquals($indiviParent['contact_type'], $this->indiviParentParams['contact_type']); | |
118 | $this->assertEquals(end($indiviParent['contact_sub_type']), $this->indiviParentParams['contact_sub_type']); | |
6a488035 | 119 | |
c490a46a | 120 | // for type:Organization |
41e1fa31 | 121 | $params = ['contact_type' => 'Organization', 'version' => 3]; |
6a488035 TO |
122 | $result = civicrm_api('contact', 'get', $params); |
123 | ||
124 | $organization = $result['values'][$this->organization]; | |
125 | $orgSponsor = $result['values'][$this->orgSponsor]; | |
126 | ||
127 | //asserts for type:Organization | |
a15773db | 128 | $this->assertEquals($organization['contact_id'], $this->organization); |
ba4a1892 TM |
129 | $this->assertEquals($organization['organization_name'], $this->organizationParams['organization_name']); |
130 | $this->assertEquals($organization['contact_type'], $this->organizationParams['contact_type']); | |
6a488035 TO |
131 | $this->assertNotContains('contact_sub_type', $organization); |
132 | ||
133 | //asserts for type:Organization subtype:Sponsor | |
a15773db | 134 | $this->assertEquals($orgSponsor['contact_id'], $this->orgSponsor); |
ba4a1892 TM |
135 | $this->assertEquals($orgSponsor['organization_name'], $this->orgSponsorParams['organization_name']); |
136 | $this->assertEquals($orgSponsor['contact_type'], $this->orgSponsorParams['contact_type']); | |
137 | $this->assertEquals(end($orgSponsor['contact_sub_type']), $this->orgSponsorParams['contact_sub_type']); | |
6a488035 | 138 | |
c490a46a | 139 | // for type:Household |
41e1fa31 | 140 | $params = ['contact_type' => 'Household', 'version' => 3]; |
6a488035 TO |
141 | $result = civicrm_api('contact', 'get', $params); |
142 | ||
143 | $household = $result['values'][$this->household]; | |
144 | ||
145 | //asserts for type:Household | |
a15773db | 146 | $this->assertEquals($household['contact_id'], $this->household); |
ba4a1892 TM |
147 | $this->assertEquals($household['household_name'], $this->householdParams['household_name']); |
148 | $this->assertEquals($household['contact_type'], $this->householdParams['contact_type']); | |
6a488035 TO |
149 | $this->assertNotContains('contact_sub_type', $household); |
150 | } | |
151 | ||
c490a46a | 152 | /** |
eceb18cc | 153 | * Search with only subtype. |
fe482240 EM |
154 | * |
155 | * Success expected. | |
c490a46a | 156 | */ |
00be9182 | 157 | public function testSearchWithSubype() { |
6a488035 | 158 | |
c490a46a | 159 | // for subtype:Student |
41e1fa31 | 160 | $params = ['contact_sub_type' => $this->student, 'version' => 3]; |
92915c55 | 161 | $result = civicrm_api('contact', 'get', $params); |
6a488035 | 162 | |
f2040bc6 | 163 | $individualStudent = $result['values'][$this->individualStudent]; |
6a488035 TO |
164 | |
165 | //asserts for type:Individual subtype:Student | |
f2040bc6 | 166 | $this->assertEquals($individualStudent['contact_id'], $this->individualStudent); |
167 | $this->assertEquals($individualStudent['first_name'], $this->individualStudentParams['first_name']); | |
168 | $this->assertEquals($individualStudent['contact_type'], $this->individualStudentParams['contact_type']); | |
169 | $this->assertEquals(end($individualStudent['contact_sub_type']), $this->individualStudentParams['contact_sub_type']); | |
6a488035 TO |
170 | |
171 | //all other contact(rather than subtype:student) should not | |
172 | //exists | |
173 | $this->assertNotContains($this->individual, $result['values']); | |
174 | $this->assertNotContains($this->indiviParent, $result['values']); | |
175 | $this->assertNotContains($this->organization, $result['values']); | |
176 | $this->assertNotContains($this->orgSponsor, $result['values']); | |
177 | $this->assertNotContains($this->household, $result['values']); | |
178 | ||
c490a46a | 179 | // for subtype:Sponsor |
41e1fa31 | 180 | $params = ['contact_sub_type' => $this->sponsor, 'version' => 3]; |
6a488035 TO |
181 | $result = civicrm_api('contact', 'get', $params); |
182 | ||
183 | $orgSponsor = $result['values'][$this->orgSponsor]; | |
184 | ||
185 | //asserts for type:Organization subtype:Sponsor | |
a15773db | 186 | $this->assertEquals($orgSponsor['contact_id'], $this->orgSponsor); |
ba4a1892 TM |
187 | $this->assertEquals($orgSponsor['organization_name'], $this->orgSponsorParams['organization_name']); |
188 | $this->assertEquals($orgSponsor['contact_type'], $this->orgSponsorParams['contact_type']); | |
189 | $this->assertEquals(end($orgSponsor['contact_sub_type']), $this->orgSponsorParams['contact_sub_type']); | |
6a488035 TO |
190 | |
191 | //all other contact(rather than subtype:Sponsor) should not | |
192 | //exists | |
193 | $this->assertNotContains($this->individual, $result['values']); | |
f2040bc6 | 194 | $this->assertNotContains($this->individualStudent, $result['values']); |
6a488035 TO |
195 | $this->assertNotContains($this->indiviParent, $result['values']); |
196 | $this->assertNotContains($this->organization, $result['values']); | |
197 | $this->assertNotContains($this->household, $result['values']); | |
198 | } | |
199 | ||
c490a46a | 200 | /** |
eceb18cc | 201 | * Search with type as well as subtype. |
fe482240 EM |
202 | * |
203 | * Success expected. | |
c490a46a | 204 | */ |
00be9182 | 205 | public function testSearchWithTypeSubype() { |
6a488035 | 206 | |
c490a46a | 207 | // for type:individual subtype:Student |
41e1fa31 | 208 | $params = ['contact_sub_type' => $this->student, 'version' => 3]; |
92915c55 | 209 | $result = civicrm_api('contact', 'get', $params); |
6a488035 | 210 | |
f2040bc6 | 211 | $individualStudent = $result['values'][$this->individualStudent]; |
6a488035 TO |
212 | |
213 | //asserts for type:Individual subtype:Student | |
f2040bc6 | 214 | $this->assertEquals($individualStudent['contact_id'], $this->individualStudent); |
215 | $this->assertEquals($individualStudent['first_name'], $this->individualStudentParams['first_name']); | |
216 | $this->assertEquals($individualStudent['contact_type'], $this->individualStudentParams['contact_type']); | |
217 | $this->assertEquals(end($individualStudent['contact_sub_type']), $this->individualStudentParams['contact_sub_type']); | |
6a488035 TO |
218 | |
219 | //all other contact(rather than subtype:student) should not | |
220 | //exists | |
221 | $this->assertNotContains($this->individual, $result['values']); | |
222 | $this->assertNotContains($this->indiviParent, $result['values']); | |
223 | $this->assertNotContains($this->organization, $result['values']); | |
224 | $this->assertNotContains($this->orgSponsor, $result['values']); | |
225 | $this->assertNotContains($this->household, $result['values']); | |
226 | ||
c490a46a | 227 | // for type:Organization subtype:Sponsor |
41e1fa31 | 228 | $params = ['contact_sub_type' => $this->sponsor, 'version' => 3]; |
6a488035 TO |
229 | $result = civicrm_api('contact', 'get', $params); |
230 | ||
231 | $orgSponsor = $result['values'][$this->orgSponsor]; | |
232 | ||
233 | //asserts for type:Organization subtype:Sponsor | |
a15773db | 234 | $this->assertEquals($orgSponsor['contact_id'], $this->orgSponsor); |
ba4a1892 TM |
235 | $this->assertEquals($orgSponsor['organization_name'], $this->orgSponsorParams['organization_name']); |
236 | $this->assertEquals($orgSponsor['contact_type'], $this->orgSponsorParams['contact_type']); | |
237 | $this->assertEquals(end($orgSponsor['contact_sub_type']), $this->orgSponsorParams['contact_sub_type']); | |
6a488035 TO |
238 | |
239 | //all other contact(rather than subtype:Sponsor) should not | |
240 | //exists | |
241 | $this->assertNotContains($this->individual, $result['values']); | |
f2040bc6 | 242 | $this->assertNotContains($this->individualStudent, $result['values']); |
6a488035 TO |
243 | $this->assertNotContains($this->indiviParent, $result['values']); |
244 | $this->assertNotContains($this->organization, $result['values']); | |
245 | $this->assertNotContains($this->household, $result['values']); | |
246 | } | |
247 | ||
c490a46a | 248 | /** |
eceb18cc | 249 | * Search with invalid type or subtype. |
c490a46a | 250 | */ |
00be9182 | 251 | public function testSearchWithInvalidData() { |
6a488035 | 252 | // for invalid type |
41e1fa31 | 253 | $params = [ |
254 | 'contact_type' => 'Invalid' . CRM_Core_DAO::VALUE_SEPARATOR . 'Invalid', | |
255 | 'version' => 3, | |
256 | ]; | |
92915c55 | 257 | $result = civicrm_api('contact', 'get', $params); |
a15773db | 258 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 | 259 | |
6a488035 | 260 | // for invalid subtype |
41e1fa31 | 261 | $params = ['contact_sub_type' => 'Invalid', 'version' => 3]; |
6a488035 | 262 | $result = civicrm_api('contact', 'get', $params); |
a15773db | 263 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 | 264 | |
6a488035 | 265 | // for invalid contact type as well as subtype |
41e1fa31 | 266 | $params = [ |
267 | 'contact_type' => 'Invalid' . CRM_Core_DAO::VALUE_SEPARATOR . 'Invalid', | |
268 | 'version' => 3, | |
269 | ]; | |
6a488035 | 270 | $result = civicrm_api('contact', 'get', $params); |
a15773db | 271 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 | 272 | |
6a488035 | 273 | // for valid type and invalid subtype |
41e1fa31 | 274 | $params = [ |
275 | 'contact_type' => 'Individual' . CRM_Core_DAO::VALUE_SEPARATOR . 'Invalid', | |
276 | 'version' => 3, | |
277 | ]; | |
6a488035 | 278 | $result = civicrm_api('contact', 'get', $params); |
a15773db | 279 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 | 280 | |
6a488035 | 281 | // for invalid type and valid subtype |
41e1fa31 | 282 | $params = [ |
283 | 'contact_type' => 'Invalid' . CRM_Core_DAO::VALUE_SEPARATOR . 'indivi_student', | |
284 | 'version' => 3, | |
285 | ]; | |
6a488035 | 286 | $result = civicrm_api('contact', 'get', $params); |
a15773db | 287 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 TO |
288 | } |
289 | ||
c490a46a | 290 | /** |
eceb18cc | 291 | * Search with wrong type or subtype. |
c490a46a | 292 | */ |
00be9182 | 293 | public function testSearchWithWrongdData() { |
6a488035 TO |
294 | |
295 | // for type:Individual subtype:Sponsor | |
41e1fa31 | 296 | $defaults = []; |
297 | $params = [ | |
298 | 'contact_type' => 'Individual' . CRM_Core_DAO::VALUE_SEPARATOR . $this->sponsor, | |
299 | 'version' => 3, | |
300 | ]; | |
92915c55 | 301 | $result = civicrm_api('contact', 'get', $params); |
a15773db | 302 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 TO |
303 | |
304 | // for type:Orgaization subtype:Parent | |
41e1fa31 | 305 | $params = [ |
306 | 'contact_type' => 'Orgaization' . CRM_Core_DAO::VALUE_SEPARATOR . $this->parent, | |
307 | 'version' => 3, | |
308 | ]; | |
6a488035 | 309 | $result = civicrm_api('contact', 'get', $params, $defaults); |
a15773db | 310 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 | 311 | |
6a488035 | 312 | // for type:Household subtype:Sponsor |
41e1fa31 | 313 | $params = [ |
314 | 'contact_type' => 'Household' . CRM_Core_DAO::VALUE_SEPARATOR . $this->sponsor, | |
315 | 'version' => 3, | |
316 | ]; | |
6a488035 | 317 | $result = civicrm_api('contact', 'get', $params, $defaults); |
a15773db | 318 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 | 319 | |
6a488035 | 320 | // for type:Household subtype:Student |
41e1fa31 | 321 | $params = [ |
322 | 'contact_type' => 'Household' . CRM_Core_DAO::VALUE_SEPARATOR . $this->student, | |
323 | 'version' => 3, | |
324 | ]; | |
6a488035 | 325 | $result = civicrm_api('contact', 'get', $params, $defaults); |
a15773db | 326 | $this->assertEquals(empty($result['values']), TRUE); |
6a488035 | 327 | } |
96025800 | 328 | |
6a488035 | 329 | } |