+--------------------------------------------------------------------+
*/
+use Civi\Api4\Group;
+
/**
*
* @package CRM
$query = "DELETE FROM civicrm_acl_entity_role where entity_table = 'civicrm_group' AND entity_id = %1";
CRM_Core_DAO::executeQuery($query, $params);
+ //check whether this group contains any saved searches and check if that saved search is appropriate to delete.
+ $groupDetails = Group::get(FALSE)->addWhere('id', '=', $id)->execute();
+ if (!empty($groupDetails[0]['saved_search_id'])) {
+ $savedSearch = new CRM_Contact_DAO_SavedSearch();
+ $savedSearch->id = $groupDetails[0]['saved_search_id'];
+ $savedSearch->find(TRUE);
+ // If it is a traditional saved search i.e has form values and there is no linked api_entity then delete the saved search as well.
+ if (!empty($savedSearch->form_values) && empty($savedSearch->api_entity) && empty($savedSearch->api_params)) {
+ $savedSearch->delete();
+ }
+ }
+
// delete from group table
$group = new CRM_Contact_DAO_Group();
$group->id = $id;
* (change the x in the function name):
*/
+ /**
+ * Upgrade function.
+ *
+ * @param string $rev
+ */
+ public function upgrade_5_37_alpha1($rev) {
+ $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
+ $this->addTask('core-issue#1845 - Alter Foreign key on civicrm_group to delete when the associated group when the saved search is deleted', 'alterSavedSearchFK');
+ }
+
// /**
// * Upgrade function.
// *
// return TRUE;
// }
+ /**
+ * @param \CRM_Queue_TaskContext $ctx
+ *
+ * @return bool
+ */
+ public static function alterSavedSearchFK(CRM_Queue_TaskContext $ctx) {
+ CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_group', 'FK_civicrm_group_saved_search_id');
+ CRM_Core_DAO::executeQuery('DELETE civicrm_saved_search FROM civicrm_saved_search LEFT JOIN civicrm_group ON civicrm_saved_search.id = civicrm_group.saved_search_id WHERE civicrm_group.id IS NULL AND form_values IS NOT NULL and api_params IS NULL');
+ CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_group ADD CONSTRAINT `FK_civicrm_group_saved_search_id` FOREIGN KEY (`saved_search_id`) REFERENCES `civicrm_saved_search`(`id`) ON DELETE CASCADE', [], TRUE, NULL, FALSE, FALSE);
+ return TRUE;
+ }
+
}
protected $_entity;
public $DBResetRequired = FALSE;
- public function setUp() {
+ public function setUp(): void {
parent::setUp();
// The line below makes it unneccessary to do cleanup after a test,
/**
* Create a saved search, and see whether the returned values make sense.
*/
- public function testCreateSavedSearch() {
+ public function testCreateSavedSearch(): void {
$contactID = $this->createLoggedInUser();
$result = $this->callAPIAndDocument(
$this->_entity, 'create', $this->params, __FUNCTION__, __FILE__)['values'];
* Create a saved search, retrieve it again, and check for ID and one of
* the field values.
*/
- public function testCreateAndGetSavedSearch() {
+ public function testCreateAndGetSavedSearch(): void {
// Arrange:
// (create a saved search)
$create_result = $this->callAPISuccess(
* Create a saved search, and test whether it can be used for a smart
* group.
*/
- public function testCreateSavedSearchWithSmartGroup() {
+ public function testCreateSavedSearchWithSmartGroup(): void {
// First create a volunteer for the default organization
$result = $this->callAPISuccess('Contact', 'create', [
$this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']);
}
- public function testDeleteSavedSearch() {
+ /**
+ * Create a saved search, and test whether it can be used for a smart
+ * group. Also check that when the Group is deleted the associated saved search gets deleted.
+ * @dataProvider versionThreeAndFour
+ */
+ public function testSavedSearchIsDeletedWhenSmartGroupIs($apiVersion): void {
+ $this->_apiVersion = $apiVersion;
+ // First create a volunteer for the default organization
+
+ $result = $this->callAPISuccess('Contact', 'create', [
+ 'first_name' => 'Joe',
+ 'last_name' => 'Schmoe',
+ 'contact_type' => 'Individual',
+ 'api.Relationship.create' => [
+ 'contact_id_a' => '$value.id',
+ // default organization:
+ 'contact_id_b' => 1,
+ // volunteer relationship:
+ 'relationship_type_id' => 6,
+ 'is_active' => 1,
+ ],
+ ]);
+ $contact_id = $result['id'];
+
+ // Now create our saved search, and chain the creation of a smart group.
+ $params = $this->params;
+ $params['api.Group.create'] = [
+ 'name' => 'my_smartgroup',
+ 'title' => 'my smartgroup',
+ 'description' => 'Volunteers for the default organization',
+ 'saved_search_id' => '$value.id',
+ 'is_active' => 1,
+ 'visibility' => 'User and User Admin Only',
+ 'is_hidden' => 0,
+ 'is_reserved' => 0,
+ ];
+
+ $create_result = $this->callAPISuccess($this->_entity, 'create', $params);
+
+ $created_search = CRM_Utils_Array::first($create_result['values']);
+ $group_id = $created_search['api.Group.create']['id'];
+
+ // Search for contacts in our new smart group
+ $get_result = $this->callAPISuccess('Contact', 'get', ['group' => $group_id]);
+
+ // Expect our contact to be there.
+ $this->assertEquals(1, $get_result['count']);
+ $this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']);
+
+ $this->callAPISuccess('Group', 'delete', ['id' => $group_id]);
+ $savedSearch = $this->callAPISuccess('SavedSearch', 'get', ['id' => $created_search['id']]);
+ $this->assertCount(0, $savedSearch['values']);
+ }
+
+ public function testDeleteSavedSearch(): void {
// Create saved search, delete it again, and try to get it
$create_result = $this->callAPISuccess($this->_entity, 'create', $this->params);
$delete_params = ['id' => $create_result['id']];