3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 class CRM_Contact_Form_Task_DeleteTest
extends CiviUnitTestCase
{
19 * The contact we are deleting and resurrecting.
21 protected $deleted_contact_id;
23 protected function setUp() {
25 $this->deleted_contact_id
= $this->individualCreate([
26 'first_name' => 'Delete',
33 protected function tearDown() {
42 * Test delete to trash.
44 public function testDeleteToTrash() {
45 $old_undelete_setting = Civi
::settings()->get('contact_undelete');
46 Civi
::settings()->set('contact_undelete', '1');
48 $form = $this->getFormObject('CRM_Contact_Form_Task_Delete');
49 $form->set('cid', $this->deleted_contact_id
);
51 $form->buildQuickForm();
52 $form->setDefaultValues();
55 $query_params = [1 => [$this->deleted_contact_id
, 'Integer']];
56 $is_deleted = CRM_Core_DAO
::singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = %1", $query_params);
57 $this->assertEquals(1, $is_deleted);
59 $session_status = CRM_Core_Session
::singleton()->getStatus();
60 $this->assertEquals('Mr. Delete Me has been moved to the trash.', $session_status[0]['text']);
63 Civi
::settings()->set('contact_undelete', $old_undelete_setting);
67 * Test restore from trash.
69 public function testRestoreFromTrash() {
70 // First, put in trash.
71 $this->testDeleteToTrash();
72 // Clear session status
73 CRM_Core_Session
::singleton()->getStatus(TRUE);
75 $old_undelete_setting = Civi
::settings()->get('contact_undelete');
76 Civi
::settings()->set('contact_undelete', '1');
78 $form = $this->getFormObject('CRM_Contact_Form_Task_Delete');
79 $form->set('cid', $this->deleted_contact_id
);
80 $form->set('restore', '1');
82 $form->buildQuickForm();
83 $form->setDefaultValues();
86 $query_params = [1 => [$this->deleted_contact_id
, 'Integer']];
87 $is_deleted = CRM_Core_DAO
::singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = %1", $query_params);
88 $this->assertEquals(0, $is_deleted);
90 $session_status = CRM_Core_Session
::singleton()->getStatus();
91 $this->assertEquals('Mr. Delete Me has been restored from the trash.', $session_status[0]['text']);
94 Civi
::settings()->set('contact_undelete', $old_undelete_setting);
98 * Test delete permanently.
100 * This is different from testDeleteWithoutTrash. This is where you have
101 * trash enabled and first move to trash, then delete from the trash.
103 public function testDeletePermanently() {
104 // First, put in trash.
105 $this->testDeleteToTrash();
106 // Clear session status
107 CRM_Core_Session
::singleton()->getStatus(TRUE);
109 $old_undelete_setting = Civi
::settings()->get('contact_undelete');
110 Civi
::settings()->set('contact_undelete', '1');
112 $form = $this->getFormObject('CRM_Contact_Form_Task_Delete');
113 $form->set('cid', $this->deleted_contact_id
);
114 $form->set('skip_undelete', '1');
116 $form->buildQuickForm();
117 $form->setDefaultValues();
118 $form->postProcess();
120 $query_params = [1 => [$this->deleted_contact_id
, 'Integer']];
121 $contact_id = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = %1", $query_params);
122 $this->assertEmpty($contact_id);
124 $session_status = CRM_Core_Session
::singleton()->getStatus();
125 $this->assertEquals('Mr. Delete Me has been permanently deleted.', $session_status[0]['text']);
128 Civi
::settings()->set('contact_undelete', $old_undelete_setting);
132 * Test delete when trash is not enabled.
134 * This is different from testDeletePermanently. This is where trash is
135 * not enabled at all.
137 public function testDeleteWithoutTrash() {
138 $old_undelete_setting = Civi
::settings()->get('contact_undelete');
139 Civi
::settings()->set('contact_undelete', '0');
141 $form = $this->getFormObject('CRM_Contact_Form_Task_Delete');
142 $form->set('cid', $this->deleted_contact_id
);
144 $form->buildQuickForm();
145 $form->setDefaultValues();
146 $form->postProcess();
148 $query_params = [1 => [$this->deleted_contact_id
, 'Integer']];
149 $contact_id = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = %1", $query_params);
150 $this->assertEmpty($contact_id);
152 // @todo This is currently buggy in the UI. It deletes properly but shows
153 // the wrong message.
154 //$session_status = CRM_Core_Session::singleton()->getStatus();
155 //$this->assertEquals('Mr. Delete Me has been permanently deleted.', $session_status[0]['text']);
158 Civi
::settings()->set('contact_undelete', $old_undelete_setting);