Merge pull request #14077 from pradpnayak/AutoComplete
[civicrm-core.git] / tests / phpunit / CRM / Core / ManagedEntitiesTest.php
index 4977a05e8a5d802dd7e717db43496f6e533c7375..7e95163fa55573d4f528cd46b2ff9973aea07425 100644 (file)
@@ -1,9 +1,8 @@
 <?php
 
-require_once 'CiviTest/CiviUnitTestCase.php';
-
 /**
  * Class CRM_Core_ManagedEntitiesTest
+ * @group headless
  */
 class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
   /**
@@ -17,7 +16,7 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
   protected $adhocProvider;
 
   /**
-   * @var array(string $shortName => CRM_Core_Module $module)
+   * @var array(string
    */
   protected $modules;
 
@@ -54,6 +53,30 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
         'is_reserved' => 1,
       ),
     );
+    $this->fixtures['com.example.one-CustomGroup'] = array(
+      'module' => 'com.example.one',
+      'name' => 'CustomGroup',
+      'entity' => 'CustomGroup',
+      'params' => array(
+        'version' => 3,
+        'name' => 'test_custom_group',
+        'title' => 'Test custom group',
+        'extends' => 'Individual',
+      ),
+    );
+    $this->fixtures['com.example.one-CustomField'] = array(
+      'module' => 'com.example.one',
+      'name' => 'CustomField',
+      'entity' => 'CustomField',
+      'params' => array(
+        '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');
@@ -150,7 +173,8 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
 
     // 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
+      // Policy is to never update after initial creation
+      'update' => 'never',
     ));
     $me = new CRM_Core_ManagedEntities($this->modules, $decls);
     $me->reconcile();
@@ -258,7 +282,8 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
     // create first managed entity ('foo')
     $decls = array();
     $decls[] = array(
-      'module' => 'com.example.unknown', // erroneous
+      // erroneous
+      'module' => 'com.example.unknown',
       'name' => 'foo',
       'entity' => 'CustomSearch',
       'params' => array(
@@ -285,7 +310,8 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
     $decls = array();
     $decls[] = array(
       'module' => 'com.example.unknown',
-      'name' => NULL, // erroneous
+      // erroneous
+      'name' => NULL,
       'entity' => 'CustomSearch',
       'params' => array(
         'version' => 3,
@@ -312,7 +338,8 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
     $decls[] = array(
       'module' => 'com.example.unknown',
       'name' => 'foo',
-      'entity' => NULL, // erroneous
+      // erroneous
+      'entity' => NULL,
       'params' => array(
         'version' => 3,
         'class_name' => 'CRM_Example_One_Foo',
@@ -386,4 +413,29 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
     $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 = array();
+    $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"');
+
+  }
+
 }