From 1f103dc472216cd3f44c4b9b299a9d91121fe3ea Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sat, 10 May 2014 11:58:12 -0700 Subject: [PATCH] CRM-14478 - ManagedEntities - Add support for 'cleanup' policy ("always", "never") --- CRM/Core/ManagedEntities.php | 42 ++++++++++++++----- .../Incremental/sql/4.5.alpha1.mysql.tpl | 4 ++ .../phpunit/CRM/Core/ManagedEntitiesTest.php | 29 +++++++++++++ xml/schema/Core/Managed.xml | 13 ++++++ 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/CRM/Core/ManagedEntities.php b/CRM/Core/ManagedEntities.php index c06e06066a..1aca464609 100644 --- a/CRM/Core/ManagedEntities.php +++ b/CRM/Core/ManagedEntities.php @@ -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') + )); + } } /** diff --git a/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl index b176305b95..3aabe6efaf 100644 --- a/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.5.alpha1.mysql.tpl @@ -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'; diff --git a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php index b5664aa6d6..749e71baa6 100644 --- a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php +++ b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php @@ -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 */ diff --git a/xml/schema/Core/Managed.xml b/xml/schema/Core/Managed.xml index 13c59e15bf..54eba7f419 100644 --- a/xml/schema/Core/Managed.xml +++ b/xml/schema/Core/Managed.xml @@ -56,6 +56,19 @@ Foreign key to the referenced item. 4.2 + + cleanup + varchar + 32 + Policy on when to cleanup entity (always, never, unused) + + CRM_Core_ManagedEntities::getCleanupOptions + + + Select + + 4.5 + UI_managed_entity entity_type -- 2.25.1