From 264006a9b2e8202f4ae1e35300b1c75e730c12d2 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 1 Sep 2022 11:24:21 -0400 Subject: [PATCH] ManagedEntities - Move deprecated function --- CRM/Case/ManagedEntities.php | 32 ++++++++- CRM/Core/ManagedEntities.php | 1 + .../phpunit/CRM/Core/ManagedEntitiesTest.php | 71 ++++++++++++------- 3 files changed, 76 insertions(+), 28 deletions(-) diff --git a/CRM/Case/ManagedEntities.php b/CRM/Case/ManagedEntities.php index be0d846cb1..4f253e430e 100644 --- a/CRM/Case/ManagedEntities.php +++ b/CRM/Case/ManagedEntities.php @@ -90,7 +90,7 @@ class CRM_Case_ManagedEntities { if (!in_array($actType, $validActTypes)) { $result[] = $managed; } - elseif ($me->get($managed['module'], $managed['name'])) { + elseif (self::getManagedEntity($managed['module'], $managed['name'])) { $result[] = $managed; } } @@ -154,7 +154,7 @@ class CRM_Case_ManagedEntities { if (!in_array($relType, $validRelTypes)) { $result[] = $managed; } - elseif ($me->get($managed['module'], $managed['name'])) { + elseif (self::getManagedEntity($managed['module'], $managed['name'])) { $result[] = $managed; } } @@ -162,4 +162,32 @@ class CRM_Case_ManagedEntities { return $result; } + /** + * Read a managed entity using APIv3. + * + * @param string $moduleName + * The name of the module which declared entity. + * @param string $managedName + * The symbolic name of the entity. + * @return array|NULL + * API representation, or NULL if the entity does not exist + */ + private static function getManagedEntity($moduleName, $managedName) { + $dao = new CRM_Core_DAO_Managed(); + $dao->module = $moduleName; + $dao->name = $managedName; + $result = NULL; + if ($dao->find(TRUE)) { + $params = [ + 'id' => $dao->entity_id, + ]; + try { + $result = civicrm_api3($dao->entity_type, 'getsingle', $params); + } + catch (Exception $e) { + } + } + return $result; + } + } diff --git a/CRM/Core/ManagedEntities.php b/CRM/Core/ManagedEntities.php index 25a4e8ff5b..82a94554a3 100644 --- a/CRM/Core/ManagedEntities.php +++ b/CRM/Core/ManagedEntities.php @@ -76,6 +76,7 @@ class CRM_Core_ManagedEntities { * API representation, or NULL if the entity does not exist */ public function get($moduleName, $name) { + CRM_Core_Error::deprecatedFunctionWarning('api'); $dao = new CRM_Core_DAO_Managed(); $dao->module = $moduleName; $dao->name = $name; diff --git a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php index b890f2374c..6ae28ea9c2 100644 --- a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php +++ b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php @@ -151,6 +151,25 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $entities = $this->managedEntities; } + /** + * @param string $moduleName + * @param string $managedName + * @return array|null + * @throws CiviCRM_API3_Exception + */ + private function getManagedEntity(string $moduleName, string $managedName):? array { + $dao = new CRM_Core_DAO_Managed(); + $dao->module = $moduleName; + $dao->name = $managedName; + if ($dao->find(TRUE)) { + $params = [ + 'id' => $dao->entity_id, + ]; + return civicrm_api3($dao->entity_type, 'getsingle', $params); + } + return NULL; + } + /** * Set up an active module and, over time, the hook implementation changes * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo' @@ -161,7 +180,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = [$this->fixtures['com.example.one-foo']]; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('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"'); @@ -169,10 +188,10 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities[] = $this->fixtures['com.example.one-bar']; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('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"'); - $bar = $me->get('com.example.one', 'bar'); + $bar = $this->getManagedEntity('com.example.one', 'bar'); $this->assertEquals('CRM_Example_One_Bar', $bar['name']); $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"'); @@ -180,10 +199,10 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { unset($this->managedEntities[0]); $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('com.example.one', 'foo'); $this->assertTrue($foo === NULL); $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); - $bar = $me->get('com.example.one', 'bar'); + $bar = $this->getManagedEntity('com.example.one', 'bar'); $this->assertEquals('CRM_Example_One_Bar', $bar['name']); $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"'); @@ -191,10 +210,10 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { unset($this->managedEntities[1]); $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('com.example.one', 'foo'); $this->assertTrue($foo === NULL); $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); - $bar = $me->get('com.example.one', 'bar'); + $bar = $this->getManagedEntity('com.example.one', 'bar'); $this->assertNull($bar); $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"'); } @@ -210,7 +229,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = [$this->fixtures['com.example.one-foo']]; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('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"'); @@ -218,7 +237,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities[0]['params']['class_name'] = 'CRM_Example_One_Foobar'; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo2 = $me->get('com.example.one', 'foo'); + $foo2 = $this->getManagedEntity('com.example.one', 'foo'); $this->assertEquals('CRM_Example_One_Foobar', $foo2['name']); $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"'); @@ -239,7 +258,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { ]); $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('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"'); @@ -247,7 +266,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities[0]['params']['class_name'] = 'CRM_Example_One_Foobar'; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo2 = $me->get('com.example.one', 'foo'); + $foo2 = $this->getManagedEntity('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"'); @@ -269,7 +288,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { ]; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('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"'); @@ -277,7 +296,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = []; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo2 = $me->get('com.example.one', 'foo'); + $foo2 = $this->getManagedEntity('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']); @@ -296,7 +315,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = [array_merge($this->fixtures['com.example.one-foo'], ['cleanup' => 'unused'])]; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('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"'); @@ -315,7 +334,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = []; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo2 = $me->get('com.example.one', 'foo'); + $foo2 = $this->getManagedEntity('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']); @@ -329,7 +348,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = []; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo3 = $me->get('com.example.one', 'foo'); + $foo3 = $this->getManagedEntity('com.example.one', 'foo'); $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); $this->assertNull($foo3); } @@ -432,7 +451,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $manager->setProcessesForTesting(['com.example.one' => ['install']]); $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('com.example.one', 'foo'); $this->assertEquals(1, $foo['is_active']); $this->assertEquals('CRM_Example_One_Foo', $foo['name']); $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); @@ -444,7 +463,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = []; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('com.example.one', 'foo'); $this->assertEquals(0, $foo['is_active']); $this->assertEquals('CRM_Example_One_Foo', $foo['name']); $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); @@ -457,7 +476,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('com.example.one', 'foo'); $this->assertEquals(1, $foo['is_active']); $this->assertEquals('CRM_Example_One_Foo', $foo['name']); $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); @@ -473,7 +492,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $manager->setProcessesForTesting(['com.example.one' => ['install']]); $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $job = $me->get('com.example.one', 'Job'); + $job = $this->getManagedEntity('com.example.one', 'Job'); $this->assertEquals(1, $job['is_active']); $this->assertEquals('test_job', $job['name']); $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"'); @@ -487,7 +506,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = []; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $job = $me->get('com.example.one', 'Job'); + $job = $this->getManagedEntity('com.example.one', 'Job'); $this->assertEquals(0, $job['is_active']); $this->assertEquals('test_job', $job['name']); $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"'); @@ -499,7 +518,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { // Mock the contextual process info that would be added by CRM_Extension_Manager::enable $manager->setProcessesForTesting(['com.example.one' => ['enable']]); $me->reconcile(); - $job = $me->get('com.example.one', 'Job'); + $job = $this->getManagedEntity('com.example.one', 'Job'); $this->assertEquals(1, $job['is_active']); $this->assertEquals('test_job', $job['name']); $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"'); @@ -515,7 +534,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $manager->setProcessesForTesting([]); $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $job = $me->get('com.example.one', 'Job'); + $job = $this->getManagedEntity('com.example.one', 'Job'); $this->assertEquals(0, $job['is_active'], "Job that was manually set inactive should not have been set active again, but it was."); $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"'); @@ -527,7 +546,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $manager->setProcessesForTesting(['com.example.one' => [$process]]); $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $job = $me->get('com.example.one', 'Job'); + $job = $this->getManagedEntity('com.example.one', 'Job'); $this->assertEquals(1, $job['is_active']); $this->assertEquals('test_job', $job['name']); $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_job WHERE name = "test_job"'); @@ -561,7 +580,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = [$this->fixtures['com.example.one-foo']]; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $foo = $me->get('com.example.one', 'foo'); + $foo = $this->getManagedEntity('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"'); @@ -570,7 +589,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $this->managedEntities = []; $me = new CRM_Core_ManagedEntities($this->modules); $me->reconcile(); - $fooNew = $me->get('com.example.one', 'foo'); + $fooNew = $this->getManagedEntity('com.example.one', 'foo'); $this->assertNull($fooNew); $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); } -- 2.25.1