| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class CRM_Core_ManagedEntitiesTest |
| 5 | * @group headless |
| 6 | */ |
| 7 | class CRM_Core_ManagedEntitiesTest extends CiviUnitTestCase { |
| 8 | /** |
| 9 | * @var \Civi\API\Kernel |
| 10 | */ |
| 11 | protected $apiKernel; |
| 12 | |
| 13 | /** |
| 14 | * @var \Civi\API\Provider\AdhocProvider |
| 15 | */ |
| 16 | protected $adhocProvider; |
| 17 | |
| 18 | /** |
| 19 | * @var array(string |
| 20 | */ |
| 21 | protected $modules; |
| 22 | |
| 23 | protected $fixtures; |
| 24 | |
| 25 | public function setUp() { |
| 26 | $this->useTransaction(TRUE); |
| 27 | parent::setUp(); |
| 28 | $this->modules = [ |
| 29 | 'one' => new CRM_Core_Module('com.example.one', TRUE), |
| 30 | 'two' => new CRM_Core_Module('com.example.two', TRUE), |
| 31 | ]; |
| 32 | |
| 33 | // Testing on drupal-demo fails because some extensions have mgd ents. |
| 34 | CRM_Core_DAO::singleValueQuery('DELETE FROM civicrm_managed'); |
| 35 | |
| 36 | $this->fixtures['com.example.one-foo'] = [ |
| 37 | 'module' => 'com.example.one', |
| 38 | 'name' => 'foo', |
| 39 | 'entity' => 'CustomSearch', |
| 40 | 'params' => [ |
| 41 | 'version' => 3, |
| 42 | 'class_name' => 'CRM_Example_One_Foo', |
| 43 | 'is_reserved' => 1, |
| 44 | ], |
| 45 | ]; |
| 46 | $this->fixtures['com.example.one-bar'] = [ |
| 47 | 'module' => 'com.example.one', |
| 48 | 'name' => 'bar', |
| 49 | 'entity' => 'CustomSearch', |
| 50 | 'params' => [ |
| 51 | 'version' => 3, |
| 52 | 'class_name' => 'CRM_Example_One_Bar', |
| 53 | 'is_reserved' => 1, |
| 54 | ], |
| 55 | ]; |
| 56 | $this->fixtures['com.example.one-CustomGroup'] = [ |
| 57 | 'module' => 'com.example.one', |
| 58 | 'name' => 'CustomGroup', |
| 59 | 'entity' => 'CustomGroup', |
| 60 | 'params' => [ |
| 61 | 'version' => 3, |
| 62 | 'name' => 'test_custom_group', |
| 63 | 'title' => 'Test custom group', |
| 64 | 'extends' => 'Individual', |
| 65 | ], |
| 66 | ]; |
| 67 | $this->fixtures['com.example.one-CustomField'] = [ |
| 68 | 'module' => 'com.example.one', |
| 69 | 'name' => 'CustomField', |
| 70 | 'entity' => 'CustomField', |
| 71 | 'params' => [ |
| 72 | 'version' => 3, |
| 73 | 'name' => 'test_custom_field', |
| 74 | 'label' => 'Test custom field', |
| 75 | 'custom_group_id' => 'test_custom_group', |
| 76 | 'data_type' => 'String', |
| 77 | 'html_type' => 'Text', |
| 78 | ], |
| 79 | ]; |
| 80 | |
| 81 | $this->apiKernel = \Civi::service('civi_api_kernel'); |
| 82 | $this->adhocProvider = new \Civi\API\Provider\AdhocProvider(3, 'CustomSearch'); |
| 83 | $this->apiKernel->registerApiProvider($this->adhocProvider); |
| 84 | } |
| 85 | |
| 86 | public function tearDown() { |
| 87 | parent::tearDown(); |
| 88 | \Civi::reset(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Set up an active module and, over time, the hook implementation changes |
| 93 | * to (1) create 'foo' entity, (2) create 'bar' entity', (3) remove 'foo' |
| 94 | * entity |
| 95 | */ |
| 96 | public function testAddRemoveEntitiesModule_UpdateAlways_DeleteAlways() { |
| 97 | $decls = []; |
| 98 | |
| 99 | // create first managed entity ('foo') |
| 100 | $decls[] = $this->fixtures['com.example.one-foo']; |
| 101 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 102 | $me->reconcile(); |
| 103 | $foo = $me->get('com.example.one', 'foo'); |
| 104 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 105 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 106 | |
| 107 | // later on, hook returns an extra managed entity ('bar') |
| 108 | $decls[] = $this->fixtures['com.example.one-bar']; |
| 109 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 110 | $me->reconcile(); |
| 111 | $foo = $me->get('com.example.one', 'foo'); |
| 112 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 113 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 114 | $bar = $me->get('com.example.one', 'bar'); |
| 115 | $this->assertEquals('CRM_Example_One_Bar', $bar['name']); |
| 116 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"'); |
| 117 | |
| 118 | // and then hook changes its mind, removing 'foo' (first of two entities) |
| 119 | unset($decls[0]); |
| 120 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 121 | $me->reconcile(); |
| 122 | $foo = $me->get('com.example.one', 'foo'); |
| 123 | $this->assertTrue($foo === NULL); |
| 124 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 125 | $bar = $me->get('com.example.one', 'bar'); |
| 126 | $this->assertEquals('CRM_Example_One_Bar', $bar['name']); |
| 127 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"'); |
| 128 | |
| 129 | // and then hook changes its mind, removing 'bar' (the last remaining entity) |
| 130 | unset($decls[1]); |
| 131 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 132 | $me->reconcile(); |
| 133 | $foo = $me->get('com.example.one', 'foo'); |
| 134 | $this->assertTrue($foo === NULL); |
| 135 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 136 | $bar = $me->get('com.example.one', 'bar'); |
| 137 | $this->assertTrue($bar === NULL); |
| 138 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Bar"'); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Set up an active module with one managed-entity and, over |
| 143 | * time, the content of the entity changes |
| 144 | */ |
| 145 | public function testModifyDeclaration_UpdateAlways() { |
| 146 | $decls = []; |
| 147 | |
| 148 | // create first managed entity ('foo') |
| 149 | $decls[] = $this->fixtures['com.example.one-foo']; |
| 150 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 151 | $me->reconcile(); |
| 152 | $foo = $me->get('com.example.one', 'foo'); |
| 153 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 154 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 155 | |
| 156 | // later on, hook specification changes |
| 157 | $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar'; |
| 158 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 159 | $me->reconcile(); |
| 160 | $foo2 = $me->get('com.example.one', 'foo'); |
| 161 | $this->assertEquals('CRM_Example_One_Foobar', $foo2['name']); |
| 162 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 163 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"'); |
| 164 | $this->assertEquals($foo['id'], $foo2['id']); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Set up an active module with one managed-entity and, over |
| 169 | * time, the content of the entity changes |
| 170 | */ |
| 171 | public function testModifyDeclaration_UpdateNever() { |
| 172 | $decls = []; |
| 173 | |
| 174 | // create first managed entity ('foo') |
| 175 | $decls[] = array_merge($this->fixtures['com.example.one-foo'], [ |
| 176 | // Policy is to never update after initial creation |
| 177 | 'update' => 'never', |
| 178 | ]); |
| 179 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 180 | $me->reconcile(); |
| 181 | $foo = $me->get('com.example.one', 'foo'); |
| 182 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 183 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 184 | |
| 185 | // later on, hook specification changes |
| 186 | $decls[0]['params']['class_name'] = 'CRM_Example_One_Foobar'; |
| 187 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 188 | $me->reconcile(); |
| 189 | $foo2 = $me->get('com.example.one', 'foo'); |
| 190 | $this->assertEquals('CRM_Example_One_Foo', $foo2['name']); |
| 191 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 192 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_FooBar"'); |
| 193 | $this->assertEquals($foo['id'], $foo2['id']); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Set up an active module with one managed-entity using the |
| 198 | * policy "cleanup=>never". When the managed-entity goes away, |
| 199 | * ensure that the policy is followed (ie the entity is not |
| 200 | * deleted). |
| 201 | */ |
| 202 | public function testRemoveDeclaration_CleanupNever() { |
| 203 | $decls = []; |
| 204 | |
| 205 | // create first managed entity ('foo') |
| 206 | $decls[] = array_merge($this->fixtures['com.example.one-foo'], [ |
| 207 | 'cleanup' => 'never', |
| 208 | ]); |
| 209 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 210 | $me->reconcile(); |
| 211 | $foo = $me->get('com.example.one', 'foo'); |
| 212 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 213 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 214 | |
| 215 | // later on, entity definition disappears; but we decide not to do any cleanup (per policy) |
| 216 | $decls = []; |
| 217 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 218 | $me->reconcile(); |
| 219 | $foo2 = $me->get('com.example.one', 'foo'); |
| 220 | $this->assertEquals('CRM_Example_One_Foo', $foo2['name']); |
| 221 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 222 | $this->assertEquals($foo['id'], $foo2['id']); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Set up an active module with one managed-entity using the |
| 227 | * policy "cleanup=>never". When the managed-entity goes away, |
| 228 | * ensure that the policy is followed (ie the entity is not |
| 229 | * deleted). |
| 230 | */ |
| 231 | public function testRemoveDeclaration_CleanupUnused() { |
| 232 | $decls = []; |
| 233 | |
| 234 | // create first managed entity ('foo') |
| 235 | $decls[] = array_merge($this->fixtures['com.example.one-foo'], [ |
| 236 | 'cleanup' => 'unused', |
| 237 | ]); |
| 238 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 239 | $me->reconcile(); |
| 240 | $foo = $me->get('com.example.one', 'foo'); |
| 241 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 242 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 243 | |
| 244 | // Override 'getrefcount' ==> The refcount is 1 |
| 245 | $this->adhocProvider->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) { |
| 246 | return civicrm_api3_create_success([ |
| 247 | [ |
| 248 | 'name' => 'mock', |
| 249 | 'type' => 'mock', |
| 250 | 'count' => 1, |
| 251 | ], |
| 252 | ]); |
| 253 | }); |
| 254 | |
| 255 | // Later on, entity definition disappears; but we decide not to do any cleanup (per policy) |
| 256 | $decls = []; |
| 257 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 258 | $me->reconcile(); |
| 259 | $foo2 = $me->get('com.example.one', 'foo'); |
| 260 | $this->assertEquals('CRM_Example_One_Foo', $foo2['name']); |
| 261 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 262 | $this->assertEquals($foo['id'], $foo2['id']); |
| 263 | |
| 264 | // Override 'getrefcount' ==> The refcount is 0 |
| 265 | $this->adhocProvider->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) { |
| 266 | return civicrm_api3_create_success([]); |
| 267 | }); |
| 268 | |
| 269 | // The entity definition disappeared and there's no reference; we decide to cleanup (per policy) |
| 270 | $decls = []; |
| 271 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 272 | $me->reconcile(); |
| 273 | $foo3 = $me->get('com.example.one', 'foo'); |
| 274 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 275 | $this->assertTrue($foo3 === NULL); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Setup an active module with a malformed entity declaration. |
| 280 | */ |
| 281 | public function testInvalidDeclarationModule() { |
| 282 | // create first managed entity ('foo') |
| 283 | $decls = []; |
| 284 | $decls[] = [ |
| 285 | // erroneous |
| 286 | 'module' => 'com.example.unknown', |
| 287 | 'name' => 'foo', |
| 288 | 'entity' => 'CustomSearch', |
| 289 | 'params' => [ |
| 290 | 'version' => 3, |
| 291 | 'class_name' => 'CRM_Example_One_Foo', |
| 292 | 'is_reserved' => 1, |
| 293 | ], |
| 294 | ]; |
| 295 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 296 | try { |
| 297 | $me->reconcile(); |
| 298 | $this->fail('Expected exception when using invalid declaration'); |
| 299 | } |
| 300 | catch (Exception $e) { |
| 301 | // good |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Setup an active module with a malformed entity declaration. |
| 307 | */ |
| 308 | public function testMissingName() { |
| 309 | // create first managed entity ('foo') |
| 310 | $decls = []; |
| 311 | $decls[] = [ |
| 312 | 'module' => 'com.example.unknown', |
| 313 | // erroneous |
| 314 | 'name' => NULL, |
| 315 | 'entity' => 'CustomSearch', |
| 316 | 'params' => [ |
| 317 | 'version' => 3, |
| 318 | 'class_name' => 'CRM_Example_One_Foo', |
| 319 | 'is_reserved' => 1, |
| 320 | ], |
| 321 | ]; |
| 322 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 323 | try { |
| 324 | $me->reconcile(); |
| 325 | $this->fail('Expected exception when using invalid declaration'); |
| 326 | } |
| 327 | catch (Exception $e) { |
| 328 | // good |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Setup an active module with a malformed entity declaration. |
| 334 | */ |
| 335 | public function testMissingEntity() { |
| 336 | // create first managed entity ('foo') |
| 337 | $decls = []; |
| 338 | $decls[] = [ |
| 339 | 'module' => 'com.example.unknown', |
| 340 | 'name' => 'foo', |
| 341 | // erroneous |
| 342 | 'entity' => NULL, |
| 343 | 'params' => [ |
| 344 | 'version' => 3, |
| 345 | 'class_name' => 'CRM_Example_One_Foo', |
| 346 | 'is_reserved' => 1, |
| 347 | ], |
| 348 | ]; |
| 349 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 350 | try { |
| 351 | $me->reconcile(); |
| 352 | $this->fail('Expected exception when using invalid declaration'); |
| 353 | } |
| 354 | catch (Exception $e) { |
| 355 | // good |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Setup an active module with an entity -- then disable and re-enable the |
| 361 | * module |
| 362 | */ |
| 363 | public function testDeactivateReactivateModule() { |
| 364 | // create first managed entity ('foo') |
| 365 | $decls = []; |
| 366 | $decls[] = $this->fixtures['com.example.one-foo']; |
| 367 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 368 | $me->reconcile(); |
| 369 | $foo = $me->get('com.example.one', 'foo'); |
| 370 | $this->assertEquals(1, $foo['is_active']); |
| 371 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 372 | $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 373 | |
| 374 | // now deactivate module, which has empty decls and which cascades to managed object |
| 375 | $this->modules['one']->is_active = FALSE; |
| 376 | $me = new CRM_Core_ManagedEntities($this->modules, []); |
| 377 | $me->reconcile(); |
| 378 | $foo = $me->get('com.example.one', 'foo'); |
| 379 | $this->assertEquals(0, $foo['is_active']); |
| 380 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 381 | $this->assertDBQuery(0, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 382 | |
| 383 | // and reactivate module, which again provides decls and which cascades to managed object |
| 384 | $this->modules['one']->is_active = TRUE; |
| 385 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 386 | $me->reconcile(); |
| 387 | $foo = $me->get('com.example.one', 'foo'); |
| 388 | $this->assertEquals(1, $foo['is_active']); |
| 389 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 390 | $this->assertDBQuery(1, 'SELECT is_active FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Setup an active module with an entity -- then entirely uninstall the |
| 395 | * module |
| 396 | */ |
| 397 | public function testUninstallModule() { |
| 398 | // create first managed entity ('foo') |
| 399 | $decls = []; |
| 400 | $decls[] = $this->fixtures['com.example.one-foo']; |
| 401 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 402 | $me->reconcile(); |
| 403 | $foo = $me->get('com.example.one', 'foo'); |
| 404 | $this->assertEquals('CRM_Example_One_Foo', $foo['name']); |
| 405 | $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 406 | |
| 407 | // then destroy module; note that decls go away |
| 408 | unset($this->modules['one']); |
| 409 | $me = new CRM_Core_ManagedEntities($this->modules, []); |
| 410 | $me->reconcile(); |
| 411 | $fooNew = $me->get('com.example.one', 'foo'); |
| 412 | $this->assertTrue(NULL === $fooNew); |
| 413 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); |
| 414 | } |
| 415 | |
| 416 | public function testDependentEntitiesUninstallCleanly() { |
| 417 | |
| 418 | // Install a module with two dependent managed entities |
| 419 | $decls = []; |
| 420 | $decls[] = $this->fixtures['com.example.one-CustomGroup']; |
| 421 | $decls[] = $this->fixtures['com.example.one-CustomField']; |
| 422 | $me = new CRM_Core_ManagedEntities($this->modules, $decls); |
| 423 | $me->reconcile(); |
| 424 | |
| 425 | // Uninstall the module |
| 426 | unset($this->modules['one']); |
| 427 | $me = new CRM_Core_ManagedEntities($this->modules, []); |
| 428 | $me->reconcile(); |
| 429 | |
| 430 | // Ensure that no managed entities remain in the civicrm_managed |
| 431 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_managed'); |
| 432 | |
| 433 | // Ensure that com.example.one-CustomGroup is deleted |
| 434 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_group WHERE name = "test_custom_group"'); |
| 435 | |
| 436 | // Ensure that com.example.one-CustomField is deleted |
| 437 | $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_custom_field WHERE name = "test_custom_field"'); |
| 438 | |
| 439 | } |
| 440 | |
| 441 | } |