Fix token subscriber to format the display of the custom tokens
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Task / DeleteTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @group headless
14 */
15 class CRM_Contact_Form_Task_DeleteTest extends CiviUnitTestCase {
16
17 /**
18 * @var int
19 * The contact we are deleting and resurrecting.
20 */
21 protected $deleted_contact_id;
22
23 protected function setUp() {
24 parent::setUp();
25 $this->deleted_contact_id = $this->individualCreate([
26 'first_name' => 'Delete',
27 'last_name' => 'Me',
28 'prefix_id' => 3,
29 'suffix_id' => NULL,
30 ]);
31 }
32
33 protected function tearDown() {
34 $this->quickCleanup([
35 'civicrm_contact',
36 ]);
37
38 parent::tearDown();
39 }
40
41 /**
42 * Test delete to trash.
43 */
44 public function testDeleteToTrash() {
45 $old_undelete_setting = Civi::settings()->get('contact_undelete');
46 Civi::settings()->set('contact_undelete', '1');
47
48 $form = $this->getFormObject('CRM_Contact_Form_Task_Delete');
49 $form->set('cid', $this->deleted_contact_id);
50 $form->preProcess();
51 $form->buildQuickForm();
52 $form->setDefaultValues();
53 $form->postProcess();
54
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);
58
59 $session_status = CRM_Core_Session::singleton()->getStatus();
60 $this->assertEquals('Mr. Delete Me has been moved to the trash.', $session_status[0]['text']);
61
62 // put settings back
63 Civi::settings()->set('contact_undelete', $old_undelete_setting);
64 }
65
66 /**
67 * Test restore from trash.
68 */
69 public function testRestoreFromTrash() {
70 // First, put in trash.
71 $this->testDeleteToTrash();
72 // Clear session status
73 CRM_Core_Session::singleton()->getStatus(TRUE);
74
75 $old_undelete_setting = Civi::settings()->get('contact_undelete');
76 Civi::settings()->set('contact_undelete', '1');
77
78 $form = $this->getFormObject('CRM_Contact_Form_Task_Delete');
79 $form->set('cid', $this->deleted_contact_id);
80 $form->set('restore', '1');
81 $form->preProcess();
82 $form->buildQuickForm();
83 $form->setDefaultValues();
84 $form->postProcess();
85
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);
89
90 $session_status = CRM_Core_Session::singleton()->getStatus();
91 $this->assertEquals('Mr. Delete Me has been restored from the trash.', $session_status[0]['text']);
92
93 // put settings back
94 Civi::settings()->set('contact_undelete', $old_undelete_setting);
95 }
96
97 /**
98 * Test delete permanently.
99 *
100 * This is different from testDeleteWithoutTrash. This is where you have
101 * trash enabled and first move to trash, then delete from the trash.
102 */
103 public function testDeletePermanently() {
104 // First, put in trash.
105 $this->testDeleteToTrash();
106 // Clear session status
107 CRM_Core_Session::singleton()->getStatus(TRUE);
108
109 $old_undelete_setting = Civi::settings()->get('contact_undelete');
110 Civi::settings()->set('contact_undelete', '1');
111
112 $form = $this->getFormObject('CRM_Contact_Form_Task_Delete');
113 $form->set('cid', $this->deleted_contact_id);
114 $form->set('skip_undelete', '1');
115 $form->preProcess();
116 $form->buildQuickForm();
117 $form->setDefaultValues();
118 $form->postProcess();
119
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);
123
124 $session_status = CRM_Core_Session::singleton()->getStatus();
125 $this->assertEquals('Mr. Delete Me has been permanently deleted.', $session_status[0]['text']);
126
127 // put settings back
128 Civi::settings()->set('contact_undelete', $old_undelete_setting);
129 }
130
131 /**
132 * Test delete when trash is not enabled.
133 *
134 * This is different from testDeletePermanently. This is where trash is
135 * not enabled at all.
136 */
137 public function testDeleteWithoutTrash() {
138 $old_undelete_setting = Civi::settings()->get('contact_undelete');
139 Civi::settings()->set('contact_undelete', '0');
140
141 $form = $this->getFormObject('CRM_Contact_Form_Task_Delete');
142 $form->set('cid', $this->deleted_contact_id);
143 $form->preProcess();
144 $form->buildQuickForm();
145 $form->setDefaultValues();
146 $form->postProcess();
147
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);
151
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']);
156
157 // put settings back
158 Civi::settings()->set('contact_undelete', $old_undelete_setting);
159 }
160
161 }