X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCRM%2FCore%2FManagedEntitiesTest.php;h=da9da77bca7f4a6d30de6d4a4bbcba875a2f0f97;hb=6d57abc32b7dacd16fa048881fbc2cafd463aaf1;hp=4977a05e8a5d802dd7e717db43496f6e533c7375;hpb=7f943ee5de42b28405828b118700c96aad4516e5;p=civicrm-core.git diff --git a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php index 4977a05e8a..dc73e15f4a 100644 --- a/tests/phpunit/CRM/Core/ManagedEntitiesTest.php +++ b/tests/phpunit/CRM/Core/ManagedEntitiesTest.php @@ -1,9 +1,8 @@ CRM_Core_Module $module) + * @var String[] */ protected $modules; @@ -26,34 +25,58 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { public function setUp() { $this->useTransaction(TRUE); parent::setUp(); - $this->modules = array( + $this->modules = [ 'one' => new CRM_Core_Module('com.example.one', TRUE), 'two' => new CRM_Core_Module('com.example.two', TRUE), - ); + ]; // Testing on drupal-demo fails because some extensions have mgd ents. CRM_Core_DAO::singleValueQuery('DELETE FROM civicrm_managed'); - $this->fixtures['com.example.one-foo'] = array( + $this->fixtures['com.example.one-foo'] = [ 'module' => 'com.example.one', 'name' => 'foo', 'entity' => 'CustomSearch', - 'params' => array( + 'params' => [ 'version' => 3, 'class_name' => 'CRM_Example_One_Foo', 'is_reserved' => 1, - ), - ); - $this->fixtures['com.example.one-bar'] = array( + ], + ]; + $this->fixtures['com.example.one-bar'] = [ 'module' => 'com.example.one', 'name' => 'bar', 'entity' => 'CustomSearch', - 'params' => array( + 'params' => [ 'version' => 3, 'class_name' => 'CRM_Example_One_Bar', 'is_reserved' => 1, - ), - ); + ], + ]; + $this->fixtures['com.example.one-CustomGroup'] = [ + 'module' => 'com.example.one', + 'name' => 'CustomGroup', + 'entity' => 'CustomGroup', + 'params' => [ + 'version' => 3, + 'name' => 'test_custom_group', + 'title' => 'Test custom group', + 'extends' => 'Individual', + ], + ]; + $this->fixtures['com.example.one-CustomField'] = [ + 'module' => 'com.example.one', + 'name' => 'CustomField', + 'entity' => 'CustomField', + 'params' => [ + 'version' => 3, + 'name' => 'test_custom_field', + 'label' => 'Test custom field', + 'custom_group_id' => 'test_custom_group', + 'data_type' => 'String', + 'html_type' => 'Text', + ], + ]; $this->apiKernel = \Civi::service('civi_api_kernel'); $this->adhocProvider = new \Civi\API\Provider\AdhocProvider(3, 'CustomSearch'); @@ -71,7 +94,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { * entity */ public function testAddRemoveEntitiesModule_UpdateAlways_DeleteAlways() { - $decls = array(); + $decls = []; // create first managed entity ('foo') $decls[] = $this->fixtures['com.example.one-foo']; @@ -120,7 +143,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { * time, the content of the entity changes */ public function testModifyDeclaration_UpdateAlways() { - $decls = array(); + $decls = []; // create first managed entity ('foo') $decls[] = $this->fixtures['com.example.one-foo']; @@ -146,12 +169,13 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { * time, the content of the entity changes */ public function testModifyDeclaration_UpdateNever() { - $decls = array(); + $decls = []; // 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 - )); + $decls[] = array_merge($this->fixtures['com.example.one-foo'], [ + // Policy is to never update after initial creation + 'update' => 'never', + ]); $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo = $me->get('com.example.one', 'foo'); @@ -176,12 +200,12 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { * deleted). */ public function testRemoveDeclaration_CleanupNever() { - $decls = array(); + $decls = []; // create first managed entity ('foo') - $decls[] = array_merge($this->fixtures['com.example.one-foo'], array( + $decls[] = array_merge($this->fixtures['com.example.one-foo'], [ 'cleanup' => 'never', - )); + ]); $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo = $me->get('com.example.one', 'foo'); @@ -189,7 +213,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { $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(); + $decls = []; $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo2 = $me->get('com.example.one', 'foo'); @@ -205,12 +229,12 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { * deleted). */ public function testRemoveDeclaration_CleanupUnused() { - $decls = array(); + $decls = []; // create first managed entity ('foo') - $decls[] = array_merge($this->fixtures['com.example.one-foo'], array( + $decls[] = array_merge($this->fixtures['com.example.one-foo'], [ 'cleanup' => 'unused', - )); + ]); $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo = $me->get('com.example.one', 'foo'); @@ -219,17 +243,17 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { // Override 'getrefcount' ==> The refcount is 1 $this->adhocProvider->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) { - return civicrm_api3_create_success(array( - array( + return civicrm_api3_create_success([ + [ 'name' => 'mock', 'type' => 'mock', 'count' => 1, - ), - )); + ], + ]); }); // Later on, entity definition disappears; but we decide not to do any cleanup (per policy) - $decls = array(); + $decls = []; $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo2 = $me->get('com.example.one', 'foo'); @@ -239,11 +263,11 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { // Override 'getrefcount' ==> The refcount is 0 $this->adhocProvider->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) { - return civicrm_api3_create_success(array()); + return civicrm_api3_create_success([]); }); // The entity definition disappeared and there's no reference; we decide to cleanup (per policy) - $decls = array(); + $decls = []; $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo3 = $me->get('com.example.one', 'foo'); @@ -256,17 +280,18 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { */ public function testInvalidDeclarationModule() { // create first managed entity ('foo') - $decls = array(); - $decls[] = array( - 'module' => 'com.example.unknown', // erroneous + $decls = []; + $decls[] = [ + // erroneous + 'module' => 'com.example.unknown', 'name' => 'foo', 'entity' => 'CustomSearch', - 'params' => array( + 'params' => [ 'version' => 3, 'class_name' => 'CRM_Example_One_Foo', 'is_reserved' => 1, - ), - ); + ], + ]; $me = new CRM_Core_ManagedEntities($this->modules, $decls); try { $me->reconcile(); @@ -282,17 +307,18 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { */ public function testMissingName() { // create first managed entity ('foo') - $decls = array(); - $decls[] = array( + $decls = []; + $decls[] = [ 'module' => 'com.example.unknown', - 'name' => NULL, // erroneous + // erroneous + 'name' => NULL, 'entity' => 'CustomSearch', - 'params' => array( + 'params' => [ 'version' => 3, 'class_name' => 'CRM_Example_One_Foo', 'is_reserved' => 1, - ), - ); + ], + ]; $me = new CRM_Core_ManagedEntities($this->modules, $decls); try { $me->reconcile(); @@ -308,17 +334,18 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { */ public function testMissingEntity() { // create first managed entity ('foo') - $decls = array(); - $decls[] = array( + $decls = []; + $decls[] = [ 'module' => 'com.example.unknown', 'name' => 'foo', - 'entity' => NULL, // erroneous - 'params' => array( + // erroneous + 'entity' => NULL, + 'params' => [ 'version' => 3, 'class_name' => 'CRM_Example_One_Foo', 'is_reserved' => 1, - ), - ); + ], + ]; $me = new CRM_Core_ManagedEntities($this->modules, $decls); try { $me->reconcile(); @@ -335,7 +362,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { */ public function testDeactivateReactivateModule() { // create first managed entity ('foo') - $decls = array(); + $decls = []; $decls[] = $this->fixtures['com.example.one-foo']; $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); @@ -346,7 +373,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { // now deactivate module, which has empty decls and which cascades to managed object $this->modules['one']->is_active = FALSE; - $me = new CRM_Core_ManagedEntities($this->modules, array()); + $me = new CRM_Core_ManagedEntities($this->modules, []); $me->reconcile(); $foo = $me->get('com.example.one', 'foo'); $this->assertEquals(0, $foo['is_active']); @@ -369,7 +396,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { */ public function testUninstallModule() { // create first managed entity ('foo') - $decls = array(); + $decls = []; $decls[] = $this->fixtures['com.example.one-foo']; $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); @@ -379,11 +406,36 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { // then destroy module; note that decls go away unset($this->modules['one']); - $me = new CRM_Core_ManagedEntities($this->modules, array()); + $me = new CRM_Core_ManagedEntities($this->modules, []); $me->reconcile(); $fooNew = $me->get('com.example.one', 'foo'); $this->assertTrue(NULL === $fooNew); $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); } + public function testDependentEntitiesUninstallCleanly() { + + // Install a module with two dependent managed entities + $decls = []; + $decls[] = $this->fixtures['com.example.one-CustomGroup']; + $decls[] = $this->fixtures['com.example.one-CustomField']; + $me = new CRM_Core_ManagedEntities($this->modules, $decls); + $me->reconcile(); + + // Uninstall the module + unset($this->modules['one']); + $me = new CRM_Core_ManagedEntities($this->modules, []); + $me->reconcile(); + + // Ensure that no managed entities remain in the civicrm_managed + $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_managed'); + + // Ensure that com.example.one-CustomGroup is deleted + $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE name = "test_custom_group"'); + + // Ensure that com.example.one-CustomField is deleted + $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_field WHERE name = "test_custom_field"'); + + } + }