CRM-14478 - ManagedEntities - Add support for 'cleanup' policy ("always", "never")
authorTim Otten <totten@civicrm.org>
Sat, 10 May 2014 18:58:12 +0000 (11:58 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 26 May 2014 20:04:27 +0000 (13:04 -0700)
CRM/Core/ManagedEntities.php
CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl
tests/phpunit/CRM/Core/ManagedEntitiesTest.php
xml/schema/Core/Managed.xml

index c06e06066a555096ac01187034a1426e3801f83f..1aca464609fcfc316960bf1a8333f4bd584e4e58 100644 (file)
@@ -6,6 +6,15 @@
  * deactivated, and deleted in tandem with their modules.
  */
 class CRM_Core_ManagedEntities {
+
+  public static function getCleanupOptions() {
+    return array(
+      'always' => ts('Always'),
+      'never' => ts('Never'),
+      'unused' => ts('If Unused'),
+    );
+  }
+
   /**
    * @var array($status => array($name => CRM_Core_Module))
    */
@@ -172,6 +181,7 @@ class CRM_Core_ManagedEntities {
     $dao->name = $todo['name'];
     $dao->entity_type = $todo['entity'];
     $dao->entity_id = $result['id'];
+    $dao->cleanup = CRM_Utils_Array::value('cleanup', $todo);
     $dao->save();
   }
 
@@ -196,6 +206,11 @@ class CRM_Core_ManagedEntities {
         $this->onApiError($params, $result);
       }
     }
+
+    if (isset($todo['cleanup'])) {
+      $dao->cleanup = $todo['cleanup'];
+      $dao->update();
+    }
   }
 
   /**
@@ -226,18 +241,23 @@ class CRM_Core_ManagedEntities {
    * @param CRM_Core_DAO_Managed $dao
    */
   public function removeStaleEntity($dao) {
-    $params = array(
-      'version' => 3,
-      'id' => $dao->entity_id,
-    );
-    $result = civicrm_api($dao->entity_type, 'delete', $params);
-    if ($result['is_error']) {
-      $this->onApiError($params, $result);
-    }
+    $policy = empty($dao->cleanup) ? 'always' : $dao->cleanup;
+    $doDelete = ($policy == 'always');
 
-    CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', array(
-      1 => array($dao->id, 'Integer')
-    ));
+    if ($doDelete) {
+      $params = array(
+        'version' => 3,
+        'id' => $dao->entity_id,
+      );
+      $result = civicrm_api($dao->entity_type, 'delete', $params);
+      if ($result['is_error']) {
+        $this->onApiError($params, $result);
+      }
+
+      CRM_Core_DAO::executeQuery('DELETE FROM civicrm_managed WHERE id = %1', array(
+        1 => array($dao->id, 'Integer')
+      ));
+    }
   }
 
   /**
index b176305b951d84f3117635856f02adb5ba0642fa..3aabe6efaf10471b509f6559a853b4928afb7ecf 100644 (file)
@@ -436,6 +436,10 @@ UPDATE civicrm_navigation
 SET civicrm_navigation.url = CONCAT(SUBSTRING(url FROM 1 FOR LOCATE('&', url) - 1), '?', SUBSTRING(url FROM LOCATE('&', url) + 1))
 WHERE civicrm_navigation.url LIKE "%&%" AND civicrm_navigation.url NOT LIKE "%?%";
 
+-- CRM-14478 Add a "cleanup" policy for managed entities
+ALTER TABLE `civicrm_managed`
+ADD COLUMN `cleanup` varchar(32) COMMENT 'Policy on when to cleanup entity (always, never, unused)';
+
 -- CRM-14639
 
 SELECT @option_grant_status := id  FROM civicrm_option_group WHERE name = 'grant_status';
index b5664aa6d618eaa95facc8fd4e9de2292d62450e..749e71baa643eb823c204285596c3d2ce5541970 100644 (file)
@@ -162,6 +162,35 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
     $this->assertEquals($foo['id'], $foo2['id']);
   }
 
+  /**
+   * Set up an active module with one managed-entity using the
+   * policy "cleanup=>never". When the managed-entity goes away,
+   * ensure that the policy is followed (ie the entity is not
+   * deleted).
+   */
+  function testRemoveDeclaration_CleanupNever() {
+    $decls = array();
+
+    // create first managed entity ('foo')
+    $decls[] = array_merge($this->fixtures['com.example.one-foo'], array(
+      'cleanup' => 'never'
+    ));
+    $me = new CRM_Core_ManagedEntities($this->modules, $decls);
+    $me->reconcile();
+    $foo = $me->get('com.example.one', 'foo');
+    $this->assertEquals('CRM_Example_One_Foo', $foo['name']);
+    $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
+
+    // later on, entity definition disappears; but we decide not to do any cleanup (per policy)
+    $decls = array();
+    $me = new CRM_Core_ManagedEntities($this->modules, $decls);
+    $me->reconcile();
+    $foo2 = $me->get('com.example.one', 'foo');
+    $this->assertEquals('CRM_Example_One_Foo', $foo2['name']);
+    $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"');
+    $this->assertEquals($foo['id'], $foo2['id']);
+  }
+
   /**
    * Setup an active module with a malformed entity declaration
    */
index 13c59e15bfa6adde347e97437b4e7edc7c1eaa2e..54eba7f4199edb8de14fc566776934549efbec8c 100644 (file)
     <comment>Foreign key to the referenced item.</comment>
     <add>4.2</add>
   </field>
+  <field>
+    <name>cleanup</name>
+    <type>varchar</type>
+    <length>32</length>
+    <comment>Policy on when to cleanup entity (always, never, unused)</comment>
+    <pseudoconstant>
+      <callback>CRM_Core_ManagedEntities::getCleanupOptions</callback>
+    </pseudoconstant>
+    <html>
+      <type>Select</type>
+    </html>
+    <add>4.5</add>
+  </field>
   <index>
     <name>UI_managed_entity</name>
     <fieldName>entity_type</fieldName>