+ /**
+ * Test function for getting contact types.
+ *
+ * @throws \API_Exception
+ */
+ public function testContactTypeInfo() {
+ $blahType = ['is_active' => 0, 'name' => 'blah', 'label' => 'blah blah', 'parent_id:name' => 'Individual'];
+ $createdType = ContactType::create()->setValues($blahType)->execute()->first();
+ $activeTypes = CRM_Contact_BAO_ContactType::contactTypeInfo();
+ $expected = $this->getExpectedContactTypes();
+ $this->assertEquals($expected, $activeTypes);
+ $allTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE);
+ $expected['blah'] = [
+ 'is_active' => FALSE,
+ 'name' => 'blah',
+ 'label' => 'blah blah',
+ 'id' => $createdType['id'],
+ 'parent_id' => 1,
+ 'is_reserved' => FALSE,
+ 'parent' => 'Individual',
+ 'parent_label' => 'Individual',
+ 'description' => NULL,
+ 'image_URL' => NULL,
+ ];
+ $this->assertEquals($expected, $allTypes);
+ }
+
+ /**
+ * Get all expected types.
+ *
+ * @return array
+ */
+ public function getExpectedContactTypes() {
+ return [
+ 'Individual' =>
+ [
+ 'id' => '1',
+ 'name' => 'Individual',
+ 'label' => 'Individual',
+ 'is_active' => TRUE,
+ 'is_reserved' => TRUE,
+ 'description' => NULL,
+ 'parent_id' => NULL,
+ 'parent' => NULL,
+ 'parent_label' => NULL,
+ 'image_URL' => NULL,
+ ],
+ 'Household' =>
+ [
+ 'id' => '2',
+ 'name' => 'Household',
+ 'label' => 'Household',
+ 'is_active' => TRUE,
+ 'is_reserved' => TRUE,
+ 'description' => NULL,
+ 'parent_id' => NULL,
+ 'parent' => NULL,
+ 'parent_label' => NULL,
+ 'image_URL' => NULL,
+ ],
+ 'Organization' =>
+ [
+ 'id' => '3',
+ 'name' => 'Organization',
+ 'label' => 'Organization',
+ 'is_active' => TRUE,
+ 'is_reserved' => TRUE,
+ 'description' => NULL,
+ 'parent_id' => NULL,
+ 'parent' => NULL,
+ 'parent_label' => NULL,
+ 'image_URL' => NULL,
+ ],
+ 'Student' =>
+ [
+ 'id' => 4,
+ 'name' => 'Student',
+ 'label' => 'Student',
+ 'parent_id' => 1,
+ 'is_active' => '1',
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Individual',
+ 'parent_label' => 'Individual',
+ 'image_URL' => NULL,
+ ],
+ 'Parent' =>
+ [
+ 'id' => 5,
+ 'name' => 'Parent',
+ 'label' => 'Parent',
+ 'parent_id' => 1,
+ 'is_active' => TRUE,
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Individual',
+ 'parent_label' => 'Individual',
+ 'image_URL' => NULL,
+ ],
+ 'Staff' =>
+ [
+ 'id' => 6,
+ 'name' => 'Staff',
+ 'label' => 'Staff',
+ 'parent_id' => 1,
+ 'is_active' => TRUE,
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Individual',
+ 'parent_label' => 'Individual',
+ 'image_URL' => NULL,
+ ],
+ 'Team' =>
+ [
+ 'id' => 7,
+ 'name' => 'Team',
+ 'label' => 'Team',
+ 'parent_id' => 3,
+ 'is_active' => TRUE,
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Organization',
+ 'parent_label' => 'Organization',
+ 'image_URL' => NULL,
+ ],
+ 'Sponsor' =>
+ [
+ 'id' => 8,
+ 'name' => 'Sponsor',
+ 'label' => 'Sponsor',
+ 'parent_id' => 3,
+ 'is_active' => TRUE,
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Organization',
+ 'parent_label' => 'Organization',
+ 'image_URL' => NULL,
+ ],
+ 'sub1_individual' =>
+ [
+ 'id' => $this->ids['ContactType'][0],
+ 'name' => 'sub1_individual',
+ 'label' => 'sub1_individual',
+ 'parent_id' => 1,
+ 'is_active' => TRUE,
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Individual',
+ 'parent_label' => 'Individual',
+ 'image_URL' => NULL,
+ ],
+ 'sub2_individual' =>
+ [
+ 'id' => $this->ids['ContactType'][1],
+ 'name' => 'sub2_individual',
+ 'label' => 'sub2_individual',
+ 'parent_id' => 1,
+ 'is_active' => TRUE,
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Individual',
+ 'parent_label' => 'Individual',
+ 'image_URL' => NULL,
+ ],
+ 'sub_organization' =>
+ [
+ 'id' => $this->ids['ContactType'][2],
+ 'name' => 'sub_organization',
+ 'label' => 'sub_organization',
+ 'parent_id' => 3,
+ 'is_active' => TRUE,
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Organization',
+ 'parent_label' => 'Organization',
+ 'image_URL' => NULL,
+ ],
+ 'sub_household' =>
+ [
+ 'id' => $this->ids['ContactType'][3],
+ 'name' => 'sub_household',
+ 'label' => 'sub_household',
+ 'parent_id' => 2,
+ 'is_active' => TRUE,
+ 'is_reserved' => FALSE,
+ 'description' => NULL,
+ 'parent' => 'Household',
+ 'parent_label' => 'Household',
+ 'image_URL' => NULL,
+ ],
+ ];
+ }
+
+ /**
+ * Get subtypes for all main types.
+ *
+ * @return array
+ */
+ public function getExpectedAllSubtypes() {
+ return array_merge(
+ $this->getExpectedContactSubTypes('Individual'),
+ $this->getExpectedContactSubTypes('Household'),
+ $this->getExpectedContactSubTypes('Organization')
+ );
+ }
+
+ /**
+ * Get the expected subtypes of the given contact type.
+ *
+ * @param string $parentType
+ *
+ * @return array
+ */
+ public function getExpectedContactSubTypes($parentType) {
+ $expected = $this->getExpectedContactTypes();
+ foreach ($expected as $index => $values) {
+ if (($values['parent_label'] ?? '') !== $parentType) {
+ unset($expected[$index]);
+ }
+ }
+ return $expected;
+ }
+