/** * Set up an active module with one managed-entity using the * policy "cleanup=>never". When the managed-entity goes away, * ensure that the policy is followed (ie the entity is not * deleted). */ public function testRemoveDeclaration_CleanupUnused() { $decls = array(); // create first managed entity ('foo') $decls[] = array_merge($this->fixtures['com.example.one-foo'], array('cleanup' => 'unused')); $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo = $me->get('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"'); // Override 'getrefcount' ==> The refcount is 1 $this->adhocProvider->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) { return civicrm_api3_create_success(array(array('name' => 'mock', 'type' => 'mock', 'count' => 1))); }); // Later on, entity definition disappears; but we decide not to do any cleanup (per policy) $decls = array(); $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo2 = $me->get('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']); // Override 'getrefcount' ==> The refcount is 0 $this->adhocProvider->addAction('getrefcount', 'access CiviCRM', function ($apiRequest) { return civicrm_api3_create_success(array()); }); // The entity definition disappeared and there's no reference; we decide to cleanup (per policy) $decls = array(); $me = new CRM_Core_ManagedEntities($this->modules, $decls); $me->reconcile(); $foo3 = $me->get('com.example.one', 'foo'); $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value WHERE name = "CRM_Example_One_Foo"'); $this->assertTrue($foo3 === NULL); }
/** * @param int $version * API version. * @param string $entity * API entity. * @param array $fields * List of fields in this fake entity. * @param array $perms * Array(string $action => string $perm). * @param array $records * List of mock records to be read/updated by API calls. */ public function __construct($version, $entity, $fields, $perms = array(), $records = array()) { parent::__construct($version, $entity); $perms = array_merge(array('create' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, 'get' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, 'delete' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION), $perms); $this->records = \CRM_Utils_Array::index(array('id'), $records); $this->fields = $fields; $this->addAction('create', $perms['create'], array($this, 'doCreate')); $this->addAction('get', $perms['get'], array($this, 'doGet')); $this->addAction('delete', $perms['delete'], array($this, 'doDelete')); }