CRM-14478 - CRM_Case_XMLRepository - Add getActivityReferenceCount(), getRelationship...
authorTim Otten <totten@civicrm.org>
Fri, 23 May 2014 04:17:44 +0000 (21:17 -0700)
committerTim Otten <totten@civicrm.org>
Fri, 23 May 2014 22:19:34 +0000 (15:19 -0700)
CRM/Case/XMLProcessor/Process.php
CRM/Case/XMLRepository.php
tests/phpunit/CRM/Case/XMLRepositoryTest.php [new file with mode: 0644]

index fa05dac55f80141f9599c5339ae05f61bae32788..bacd687b780e2eb91782c8315bd6320ff1465d81 100644 (file)
@@ -253,6 +253,52 @@ class CRM_Case_XMLProcessor_Process extends CRM_Case_XMLProcessor {
     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);
index fec0ff2f32fe00a5c91192ab6dd8e3e1de694070..0214254bef118b4a275bb291bd83c29468ab084f 100644 (file)
@@ -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<String,SimpleXMLElement> $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<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;
+  }
+
 }
diff --git a/tests/phpunit/CRM/Case/XMLRepositoryTest.php b/tests/phpunit/CRM/Case/XMLRepositoryTest.php
new file mode 100644 (file)
index 0000000..46cd28d
--- /dev/null
@@ -0,0 +1,162 @@
+<?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