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