| 1 | <?php |
| 2 | require_once 'CiviTest/CiviUnitTestCase.php'; |
| 3 | require_once 'CiviTest/Contact.php'; |
| 4 | |
| 5 | /** |
| 6 | * Class CRM_Contact_BAO_ContactType_ContactSearchTest |
| 7 | */ |
| 8 | class CRM_Contact_BAO_ContactType_ContactSearchTest extends CiviUnitTestCase { |
| 9 | |
| 10 | public function setUp() { |
| 11 | parent::setUp(); |
| 12 | $students = 'indivi_student' . substr(sha1(rand()), 0, 7); |
| 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 | |
| 23 | $parents = 'indivi_parent' . substr(sha1(rand()), 0, 7); |
| 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 | |
| 34 | $orgs = 'org_sponsor' . substr(sha1(rand()), 0, 7); |
| 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 | |
| 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 | |
| 88 | /** |
| 89 | * Search with only type. |
| 90 | * |
| 91 | * Success expected. |
| 92 | */ |
| 93 | public function testSearchWithType() { |
| 94 | |
| 95 | // for type:Individual |
| 96 | $params = array('contact_type' => 'Individual', 'version' => 3); |
| 97 | $result = civicrm_api('contact', 'get', $params); |
| 98 | |
| 99 | $individual = $result['values'][$this->individual]; |
| 100 | $indiviStudent = $result['values'][$this->indiviStudent]; |
| 101 | $indiviParent = $result['values'][$this->indiviParent]; |
| 102 | |
| 103 | //asserts for type:Individual |
| 104 | $this->assertEquals($individual['contact_id'], $this->individual); |
| 105 | $this->assertEquals($individual['first_name'], $this->indiviParams['first_name']); |
| 106 | $this->assertEquals($individual['contact_type'], $this->indiviParams['contact_type']); |
| 107 | $this->assertNotContains('contact_sub_type', $individual); |
| 108 | |
| 109 | //asserts for type:Individual subtype:Student |
| 110 | $this->assertEquals($indiviStudent['contact_id'], $this->indiviStudent); |
| 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']); |
| 114 | |
| 115 | //asserts for type:Individual subtype:Parent |
| 116 | $this->assertEquals($indiviParent['contact_id'], $this->indiviParent); |
| 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']); |
| 120 | |
| 121 | // for type:Organization |
| 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 |
| 129 | $this->assertEquals($organization['contact_id'], $this->organization); |
| 130 | $this->assertEquals($organization['organization_name'], $this->organizationParams['organization_name']); |
| 131 | $this->assertEquals($organization['contact_type'], $this->organizationParams['contact_type']); |
| 132 | $this->assertNotContains('contact_sub_type', $organization); |
| 133 | |
| 134 | //asserts for type:Organization subtype:Sponsor |
| 135 | $this->assertEquals($orgSponsor['contact_id'], $this->orgSponsor); |
| 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']); |
| 139 | |
| 140 | // for type:Household |
| 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 |
| 147 | $this->assertEquals($household['contact_id'], $this->household); |
| 148 | $this->assertEquals($household['household_name'], $this->householdParams['household_name']); |
| 149 | $this->assertEquals($household['contact_type'], $this->householdParams['contact_type']); |
| 150 | $this->assertNotContains('contact_sub_type', $household); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Search with only subtype. |
| 155 | * |
| 156 | * Success expected. |
| 157 | */ |
| 158 | public function testSearchWithSubype() { |
| 159 | |
| 160 | // for subtype:Student |
| 161 | $params = array('contact_sub_type' => $this->student, 'version' => 3); |
| 162 | $result = civicrm_api('contact', 'get', $params); |
| 163 | |
| 164 | $indiviStudent = $result['values'][$this->indiviStudent]; |
| 165 | |
| 166 | //asserts for type:Individual subtype:Student |
| 167 | $this->assertEquals($indiviStudent['contact_id'], $this->indiviStudent); |
| 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']); |
| 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 | |
| 180 | // for subtype:Sponsor |
| 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 |
| 187 | $this->assertEquals($orgSponsor['contact_id'], $this->orgSponsor); |
| 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']); |
| 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 | |
| 201 | /** |
| 202 | * Search with type as well as subtype. |
| 203 | * |
| 204 | * Success expected. |
| 205 | */ |
| 206 | public function testSearchWithTypeSubype() { |
| 207 | |
| 208 | // for type:individual subtype:Student |
| 209 | $params = array('contact_sub_type' => $this->student, 'version' => 3); |
| 210 | $result = civicrm_api('contact', 'get', $params); |
| 211 | |
| 212 | $indiviStudent = $result['values'][$this->indiviStudent]; |
| 213 | |
| 214 | //asserts for type:Individual subtype:Student |
| 215 | $this->assertEquals($indiviStudent['contact_id'], $this->indiviStudent); |
| 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']); |
| 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 | |
| 228 | // for type:Organization subtype:Sponsor |
| 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 |
| 235 | $this->assertEquals($orgSponsor['contact_id'], $this->orgSponsor); |
| 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']); |
| 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 | |
| 249 | /** |
| 250 | * Search with invalid type or subtype. |
| 251 | */ |
| 252 | public function testSearchWithInvalidData() { |
| 253 | // for invalid type |
| 254 | $params = array('contact_type' => 'Invalid' . CRM_Core_DAO::VALUE_SEPARATOR . 'Invalid', 'version' => 3); |
| 255 | $result = civicrm_api('contact', 'get', $params); |
| 256 | $this->assertEquals(empty($result['values']), TRUE); |
| 257 | |
| 258 | // for invalid subtype |
| 259 | $params = array('contact_sub_type' => 'Invalid', 'version' => 3); |
| 260 | $result = civicrm_api('contact', 'get', $params); |
| 261 | $this->assertEquals(empty($result['values']), TRUE); |
| 262 | |
| 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); |
| 266 | $this->assertEquals(empty($result['values']), TRUE); |
| 267 | |
| 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); |
| 271 | $this->assertEquals(empty($result['values']), TRUE); |
| 272 | |
| 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); |
| 276 | $this->assertEquals(empty($result['values']), TRUE); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Search with wrong type or subtype. |
| 281 | */ |
| 282 | public function testSearchWithWrongdData() { |
| 283 | |
| 284 | // for type:Individual subtype:Sponsor |
| 285 | $defaults = array(); |
| 286 | $params = array('contact_type' => 'Individual' . CRM_Core_DAO::VALUE_SEPARATOR . $this->sponsor, 'version' => 3); |
| 287 | $result = civicrm_api('contact', 'get', $params); |
| 288 | $this->assertEquals(empty($result['values']), TRUE); |
| 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); |
| 293 | $this->assertEquals(empty($result['values']), TRUE); |
| 294 | |
| 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); |
| 298 | $this->assertEquals(empty($result['values']), TRUE); |
| 299 | |
| 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); |
| 303 | $this->assertEquals(empty($result['values']), TRUE); |
| 304 | } |
| 305 | |
| 306 | } |