Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / ManagedEntitiesTest.php
index dc73e15f4afbf27f3d8792094475a72f96329bac..a8e4c2801f99efb6807c5716101bf37e1f3cc439 100644 (file)
@@ -78,6 +78,20 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
       ],
     ];
 
+    $this->fixtures['com.example.one-Job'] = [
+      'module' => 'com.example.one',
+      'name' => 'Job',
+      'entity' => 'Job',
+      'params' => [
+        'version' => 3,
+        'name' => 'test_job',
+        'run_frequency' => 'Daily',
+        'api_entity' => 'Job',
+        'api_action' => 'Get',
+        'parameters' => '',
+      ],
+    ];
+
     $this->apiKernel = \Civi::service('civi_api_kernel');
     $this->adhocProvider = new \Civi\API\Provider\AdhocProvider(3, 'CustomSearch');
     $this->apiKernel->registerApiProvider($this->adhocProvider);
@@ -361,9 +375,13 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
    * module
    */
   public function testDeactivateReactivateModule() {
+    $manager = CRM_Extension_System::singleton()->getManager();
+
     // create first managed entity ('foo')
     $decls = [];
     $decls[] = $this->fixtures['com.example.one-foo'];
+    // Mock the contextual process info that would be added by CRM_Extension_Manager::install
+    $manager->setProcessesForTesting(['com.example.one' => ['install']]);
     $me = new CRM_Core_ManagedEntities($this->modules, $decls);
     $me->reconcile();
     $foo = $me->get('com.example.one', 'foo');
@@ -373,6 +391,8 @@ 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;
+    // Mock the contextual process info that would be added by CRM_Extension_Manager::disable
+    $manager->setProcessesForTesting(['com.example.one' => ['disable']]);
     $me = new CRM_Core_ManagedEntities($this->modules, []);
     $me->reconcile();
     $foo = $me->get('com.example.one', 'foo');
@@ -382,12 +402,86 @@ class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase {
 
     // and reactivate module, which again provides decls and which cascades to managed object
     $this->modules['one']->is_active = TRUE;
+    // Mock the contextual process info that would be added by CRM_Extension_Manager::enable
+    $manager->setProcessesForTesting(['com.example.one' => ['enable']]);
     $me = new CRM_Core_ManagedEntities($this->modules, $decls);
     $me->reconcile();
     $foo = $me->get('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"');
+
+    // Special case: Job entities.
+    //
+    // First we repeat the above steps, but adding the context that
+    // CRM_Extension_Manager adds when installing/enabling extensions.
+    //
+    // The behaviour should be as above.
+    $decls = [$this->fixtures['com.example.one-Job']];
+    // Mock the contextual process info that would be added by CRM_Extension_Manager::install
+    $manager->setProcessesForTesting(['com.example.one' => ['install']]);
+    $me = new CRM_Core_ManagedEntities($this->modules, $decls);
+    $me->reconcile();
+    $job = $me->get('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"');
+    // Reset context.
+    $manager->setProcessesForTesting([]);
+
+    // now deactivate module, which has empty decls and which cascades to managed object
+    $this->modules['one']->is_active = FALSE;
+    // Mock the contextual process info that would be added by CRM_Extension_Manager::disable
+    $manager->setProcessesForTesting(['com.example.one' => ['disable']]);
+    $me = new CRM_Core_ManagedEntities($this->modules, []);
+    $me->reconcile();
+    $job = $me->get('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"');
+
+    // and reactivate module, which again provides decls and which cascades to managed object
+    $this->modules['one']->is_active = TRUE;
+    $me = new CRM_Core_ManagedEntities($this->modules, $decls);
+    // 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');
+    $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"');
+
+    // Currently: module enabled, job enabled.
+    // Test that if we now manually disable the job, calling reconcile in a
+    // normal flush situation does NOT re-enable it.
+    // ... manually disable job.
+    $this->callAPISuccess('Job', 'create', ['id' => $job['id'], 'is_active' => 0]);
+
+    // ... now call reconcile in the context of a normal flush operation.
+    // Mock the contextual process info - there would not be any
+    $manager->setProcessesForTesting([]);
+    $me = new CRM_Core_ManagedEntities($this->modules, $decls);
+    $me->reconcile();
+    $job = $me->get('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"');
+
+    // Now call reconcile again, but in the context of the job's extension being installed/enabled. This should re-enable the job.
+    foreach (['enable', 'install'] as $process) {
+      // Manually disable the job
+      $this->callAPISuccess('Job', 'create', ['id' => $job['id'], 'is_active' => 0]);
+      // Mock the contextual process info that would be added by CRM_Extension_Manager::enable
+      $manager->setProcessesForTesting(['com.example.one' => [$process]]);
+      $me = new CRM_Core_ManagedEntities($this->modules, $decls);
+      $me->reconcile();
+      $job = $me->get('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"');
+    }
+
+    // Reset context.
+    $manager->setProcessesForTesting([]);
   }
 
   /**