From: Tim Otten Date: Sat, 10 May 2014 00:22:22 +0000 (-0700) Subject: CRM-14478 - ManagedEntities - Add support for 'update' policy ('always' or 'never') X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=0dd545869c9046003e81e4a18e1ece1bd15f868d;p=civicrm-core.git CRM-14478 - ManagedEntities - Add support for 'update' policy ('always' or 'never') --- diff --git a/CRM/Core/ManagedEntities.php b/CRM/Core/ManagedEntities.php index e655675815..c06e06066a 100644 --- a/CRM/Core/ManagedEntities.php +++ b/CRM/Core/ManagedEntities.php @@ -180,20 +180,22 @@ class CRM_Core_ManagedEntities { * * @param CRM_Core_DAO_Managed $dao * @param array $todo entity specification (per hook_civicrm_managedEntities) - * @return array|int API result */ public function updateExistingEntity($dao, $todo) { - $defaults = array( - 'id' => $dao->entity_id, - 'is_active' => 1, // FIXME: test whether is_active is valid - ); - $params = array_merge($defaults, $todo['params']); - $result = civicrm_api($dao->entity_type, 'create', $params); - if ($result['is_error']) { - $this->onApiError($params, $result); - return $result; + $policy = CRM_Utils_Array::value('update', $todo, 'always'); + $doUpdate = ($policy == 'always'); + + if ($doUpdate) { + $defaults = array( + 'id' => $dao->entity_id, + 'is_active' => 1, // FIXME: test whether is_active is valid + ); + $params = array_merge($defaults, $todo['params']); + $result = civicrm_api($dao->entity_type, 'create', $params); + if ($result['is_error']) { + $this->onApiError($params, $result); + } } - return $result; } /** diff --git a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php index a4855bee40..b5664aa6d6 100644 --- a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php +++ b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php @@ -63,7 +63,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo' * entity */ - function testAddRemoveEntitiesModule() { + function testAddRemoveEntitiesModule_UpdateAlways_DeleteAlways() { $decls = array(); // create first managed entity ('foo') @@ -112,7 +112,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { * Set up an active module with one managed-entity and, over * time, the content of the entity changes */ - function testModifyDeclaration() { + function testModifyDeclaration_UpdateAlways() { $decls = array(); // create first managed entity ('foo') @@ -134,6 +134,34 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->assertEquals($foo['id'], $foo2['id']); } + /** + * Set up an active module with one managed-entity and, over + * time, the content of the entity changes + */ + function testModifyDeclaration_UpdateNever() { + $decls = array(); + + // create first managed entity ('foo') + $decls[] = array_merge($this->fixtures['com.example.one-foo'], array( + 'update' => 'never', // Policy is to never update after initial creation + )); + $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, hook specification changes + $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar'; + $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->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"'); + $this->assertEquals($foo['id'], $foo2['id']); + } + /** * Setup an active module with a malformed entity declaration */