From: Tim Otten Date: Fri, 23 May 2014 04:17:44 +0000 (-0700) Subject: CRM-14478 - CRM_Case_XMLRepository - Add getActivityReferenceCount(), getRelationship... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=e19323c9f78ea5937cc35e6b382204ae0097faa4;p=civicrm-core.git CRM-14478 - CRM_Case_XMLRepository - Add getActivityReferenceCount(), getRelationshipReferenceCount() --- diff --git a/CRM/Case/XMLProcessor/Process.php b/CRM/Case/XMLProcessor/Process.php index fa05dac55f..bacd687b78 100644 --- a/CRM/Case/XMLProcessor/Process.php +++ b/CRM/Case/XMLProcessor/Process.php @@ -253,6 +253,52 @@ class CRM_Case_XMLProcessor_Process extends CRM_Case_XMLProcessor { return $result; } + /** + * @param SimpleXMLElement $caseTypeXML + * @return array symbolic activity-type names + */ + function getDeclaredActivityTypes($caseTypeXML) { + $result = array(); + + if ($caseTypeXML->ActivityTypes && $caseTypeXML->ActivityTypes->ActivityType) { + foreach ($caseTypeXML->ActivityTypes->ActivityType as $activityTypeXML) { + $result[] = (string) $activityTypeXML->name; + } + } + + if ($caseTypeXML->ActivitySets && $caseTypeXML->ActivitySets->ActivitySet) { + foreach ($caseTypeXML->ActivitySets->ActivitySet as $activitySetXML) { + if ($activitySetXML->ActivityTypes && $activitySetXML->ActivityTypes->ActivityType) { + foreach ($activitySetXML->ActivityTypes->ActivityType as $activityTypeXML) { + $result[] = (string) $activityTypeXML->name; + } + } + } + } + + $result = array_unique($result); + sort($result); + return $result; + } + + /** + * @param SimpleXMLElement $caseTypeXML + * @return array symbolic relationship-type names + */ + function getDeclaredRelationshipTypes($caseTypeXML) { + $result = array(); + + if ($caseTypeXML->CaseRoles && $caseTypeXML->CaseRoles->RelationshipType) { + foreach ($caseTypeXML->CaseRoles->RelationshipType as $relTypeXML) { + $result[] = (string) $relTypeXML->name; + } + } + + $result = array_unique($result); + sort($result); + return $result; + } + function deleteEmptyActivity(&$params) { $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); diff --git a/CRM/Case/XMLRepository.php b/CRM/Case/XMLRepository.php index fec0ff2f32..0214254bef 100644 --- a/CRM/Case/XMLRepository.php +++ b/CRM/Case/XMLRepository.php @@ -53,7 +53,7 @@ class CRM_Case_XMLRepository { /** * @param bool $fresh - * @return CRM_Case_XMLProcessor + * @return CRM_Case_XMLRepository */ public static function singleton($fresh = FALSE) { if (!self::$singleton || $fresh) { @@ -65,7 +65,8 @@ class CRM_Case_XMLRepository { /** * @param array $xml */ - public function __construct($xml = array()) { + public function __construct($allCaseTypes = NULL, $xml = array()) { + $this->allCaseTypes = $allCaseTypes; $this->xml = $xml; } @@ -153,4 +154,53 @@ class CRM_Case_XMLRepository { } return $this->hookCache; } + + /** + * @return array symbolic names of case-types + */ + public function getAllCaseTypes() { + if ($this->allCaseTypes === NULL) { + $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name"); + } + return $this->allCaseTypes; + } + + /** + * Determine the number of times a particular activity-type is + * referenced in CiviCase XML. + * + * @param string $activityType symbolic-name of an activity type + * @return int + */ + function getActivityReferenceCount($activityType) { + $p = new CRM_Case_XMLProcessor_Process(); + $count = 0; + foreach ($this->getAllCaseTypes() as $caseTypeName) { + $caseTypeXML = $this->retrieve($caseTypeName); + if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) { + $count++; + } + } + return $count; + } + + /** + * Determine the number of times a particular activity-type is + * referenced in CiviCase XML. + * + * @param string $relationshipTypeName symbolic-name of a relationship-type + * @return int + */ + function getRelationshipReferenceCount($relationshipTypeName) { + $p = new CRM_Case_XMLProcessor_Process(); + $count = 0; + foreach ($this->getAllCaseTypes() as $caseTypeName) { + $caseTypeXML = $this->retrieve($caseTypeName); + if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) { + $count++; + } + } + return $count; + } + } diff --git a/tests/phpunit/CRM/Case/XMLRepositoryTest.php b/tests/phpunit/CRM/Case/XMLRepositoryTest.php new file mode 100644 index 0000000000..46cd28db01 --- /dev/null +++ b/tests/phpunit/CRM/Case/XMLRepositoryTest.php @@ -0,0 +1,162 @@ +fixtures['CaseTypeWithSingleActivityType'] = ' + + CaseTypeWithSingleActivityType + + + Single Activity Type + 1 + + + + '; + $this->fixtures['CaseTypeWithTwoActivityTypes'] = ' + + CaseTypeWithTwoActivityTypes + + + First Activity Type + 1 + + + Second Activity Type + + + + '; + $this->fixtures['CaseTypeWithThreeActivityTypes'] = ' + + CaseTypeWithThreeActivityTypes + + + First Activity Type + 1 + + + Second Activity Type + + + Third Activity Type + + + + '; + $this->fixtures['CaseTypeWithSingleRole'] = ' + + CaseTypeWithSingleRole + + + Single Role + 1 + + + + '; + $this->fixtures['CaseTypeWithTwoRoles'] = ' + + CaseTypeWithTwoRoles + + + First Role + 1 + + + Second Role + + + + '; + $this->fixtures['CaseTypeWithThreeRoles'] = ' + + CaseTypeWithThreeRoles + + + First Role + 1 + + + Second Role + + + Third Role + + + + '; + + } + + function testGetActivityReferenceCount_1() { + $repo = new CRM_Case_XMLRepository( + array('CaseTypeWithSingleActivityType'), + array( + 'CaseTypeWithSingleActivityType' => new SimpleXMLElement($this->fixtures['CaseTypeWithSingleActivityType']), + /* noise: */ + 'CaseTypeWithSingleRole' => new SimpleXMLElement($this->fixtures['CaseTypeWithSingleRole']), + ) + ); + + $this->assertEquals(1, $repo->getActivityReferenceCount('Single Activity Type')); + $this->assertEquals(0, $repo->getActivityReferenceCount('First Activity Type')); + $this->assertEquals(0, $repo->getActivityReferenceCount('Second Activity Type')); + $this->assertEquals(0, $repo->getActivityReferenceCount('Third Activity Type')); + } + + function testGetActivityReferenceCount_23() { + $repo = new CRM_Case_XMLRepository( + array('CaseTypeWithTwoActivityTypes', 'CaseTypeWithThreeActivityTypes'), + array( + 'CaseTypeWithTwoActivityTypes' => new SimpleXMLElement($this->fixtures['CaseTypeWithTwoActivityTypes']), + 'CaseTypeWithThreeActivityTypes' => new SimpleXMLElement($this->fixtures['CaseTypeWithThreeActivityTypes']), + /* noise: */ + 'CaseTypeWithSingleRole' => new SimpleXMLElement($this->fixtures['CaseTypeWithSingleRole']), + ) + ); + + $this->assertEquals(0, $repo->getActivityReferenceCount('Single Activity Type')); + $this->assertEquals(2, $repo->getActivityReferenceCount('First Activity Type')); + $this->assertEquals(2, $repo->getActivityReferenceCount('Second Activity Type')); + $this->assertEquals(1, $repo->getActivityReferenceCount('Third Activity Type')); + } + + function testGetRoleReferenceCount_1() { + $repo = new CRM_Case_XMLRepository( + array('CaseTypeWithSingleRole', 'CaseTypeWithSingleActivityType'), + array( + 'CaseTypeWithSingleRole' => new SimpleXMLElement($this->fixtures['CaseTypeWithSingleRole']), + /* noise: */ + 'CaseTypeWithSingleActivityType' => new SimpleXMLElement($this->fixtures['CaseTypeWithSingleActivityType']), + ) + ); + + $this->assertEquals(1, $repo->getRelationshipReferenceCount('Single Role')); + $this->assertEquals(0, $repo->getRelationshipReferenceCount('First Role')); + $this->assertEquals(0, $repo->getRelationshipReferenceCount('Second Role')); + $this->assertEquals(0, $repo->getRelationshipReferenceCount('Third Role')); + } + + function testGetRoleReferenceCount_23() { + $repo = new CRM_Case_XMLRepository( + array('CaseTypeWithTwoRoles', 'CaseTypeWithThreeRoles', 'CaseTypeWithSingleActivityType'), + array( + 'CaseTypeWithTwoRoles' => new SimpleXMLElement($this->fixtures['CaseTypeWithTwoRoles']), + 'CaseTypeWithThreeRoles' => new SimpleXMLElement($this->fixtures['CaseTypeWithThreeRoles']), + /* noise: */ + 'CaseTypeWithSingleActivityType' => new SimpleXMLElement($this->fixtures['CaseTypeWithSingleActivityType']), + ) + ); + + $this->assertEquals(0, $repo->getRelationshipReferenceCount('Single Role')); + $this->assertEquals(2, $repo->getRelationshipReferenceCount('First Role')); + $this->assertEquals(2, $repo->getRelationshipReferenceCount('Second Role')); + $this->assertEquals(1, $repo->getRelationshipReferenceCount('Third Role')); + } +} \ No newline at end of file