return $result;
}
+ /**
+ * @param SimpleXMLElement $caseTypeXML
+ * @return array<string> 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<string> 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);
/**
* @param bool $fresh
- * @return CRM_Case_XMLProcessor
+ * @return CRM_Case_XMLRepository
*/
public static function singleton($fresh = FALSE) {
if (!self::$singleton || $fresh) {
/**
* @param array<String,SimpleXMLElement> $xml
*/
- public function __construct($xml = array()) {
+ public function __construct($allCaseTypes = NULL, $xml = array()) {
+ $this->allCaseTypes = $allCaseTypes;
$this->xml = $xml;
}
}
return $this->hookCache;
}
+
+ /**
+ * @return array<string> 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;
+ }
+
}
--- /dev/null
+<?php
+
+require_once 'CiviTest/CiviUnitTestCase.php';
+
+class CRM_Case_XMLRepositoryTest extends CiviUnitTestCase {
+ protected $fixtures = array();
+
+ protected function setUp() {
+ parent::setUp();
+ $this->fixtures['CaseTypeWithSingleActivityType'] = '
+<CaseType>
+ <name>CaseTypeWithSingleActivityType</name>
+ <ActivityTypes>
+ <ActivityType>
+ <name>Single Activity Type</name>
+ <max_instances>1</max_instances>
+ </ActivityType>
+ </ActivityTypes>
+</CaseType>
+ ';
+ $this->fixtures['CaseTypeWithTwoActivityTypes'] = '
+<CaseType>
+ <name>CaseTypeWithTwoActivityTypes</name>
+ <ActivityTypes>
+ <ActivityType>
+ <name>First Activity Type</name>
+ <max_instances>1</max_instances>
+ </ActivityType>
+ <ActivityType>
+ <name>Second Activity Type</name>
+ </ActivityType>
+ </ActivityTypes>
+</CaseType>
+ ';
+ $this->fixtures['CaseTypeWithThreeActivityTypes'] = '
+<CaseType>
+ <name>CaseTypeWithThreeActivityTypes</name>
+ <ActivityTypes>
+ <ActivityType>
+ <name>First Activity Type</name>
+ <max_instances>1</max_instances>
+ </ActivityType>
+ <ActivityType>
+ <name>Second Activity Type</name>
+ </ActivityType>
+ <ActivityType>
+ <name>Third Activity Type</name>
+ </ActivityType>
+ </ActivityTypes>
+</CaseType>
+ ';
+ $this->fixtures['CaseTypeWithSingleRole'] = '
+<CaseType>
+ <name>CaseTypeWithSingleRole</name>
+ <CaseRoles>
+ <RelationshipType>
+ <name>Single Role</name>
+ <creator>1</creator>
+ </RelationshipType>
+ </CaseRoles>
+</CaseType>
+ ';
+ $this->fixtures['CaseTypeWithTwoRoles'] = '
+<CaseType>
+ <name>CaseTypeWithTwoRoles</name>
+ <CaseRoles>
+ <RelationshipType>
+ <name>First Role</name>
+ <creator>1</creator>
+ </RelationshipType>
+ <RelationshipType>
+ <name>Second Role</name>
+ </RelationshipType>
+ </CaseRoles>
+</CaseType>
+ ';
+ $this->fixtures['CaseTypeWithThreeRoles'] = '
+<CaseType>
+ <name>CaseTypeWithThreeRoles</name>
+ <CaseRoles>
+ <RelationshipType>
+ <name>First Role</name>
+ <creator>1</creator>
+ </RelationshipType>
+ <RelationshipType>
+ <name>Second Role</name>
+ </RelationshipType>
+ <RelationshipType>
+ <name>Third Role</name>
+ </RelationshipType>
+ </CaseRoles>
+</CaseType>
+ ';
+
+ }
+
+ 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