From 395efe5e564fcf46307dd1e219e7a0f972100754 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Sun, 31 May 2020 18:27:27 -0400 Subject: [PATCH] tests for contact delete/restore --- .../CRM/Contact/Form/Task/DeleteTest.php | 161 ++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 tests/phpunit/CRM/Contact/Form/Task/DeleteTest.php diff --git a/tests/phpunit/CRM/Contact/Form/Task/DeleteTest.php b/tests/phpunit/CRM/Contact/Form/Task/DeleteTest.php new file mode 100644 index 0000000000..35d87903a5 --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Task/DeleteTest.php @@ -0,0 +1,161 @@ +deleted_contact_id = $this->individualCreate([ + 'first_name' => 'Delete', + 'last_name' => 'Me', + 'prefix_id' => 3, + 'suffix_id' => NULL, + ]); + } + + protected function tearDown() { + $this->quickCleanup([ + 'civicrm_contact', + ]); + + parent::tearDown(); + } + + /** + * Test delete to trash. + */ + public function testDeleteToTrash() { + $old_undelete_setting = Civi::settings()->get('contact_undelete'); + Civi::settings()->set('contact_undelete', '1'); + + $form = $this->getFormObject('CRM_Contact_Form_Task_Delete'); + $form->set('cid', $this->deleted_contact_id); + $form->preProcess(); + $form->buildQuickForm(); + $form->setDefaultValues(); + $form->postProcess(); + + $query_params = [1 => [$this->deleted_contact_id, 'Integer']]; + $is_deleted = CRM_Core_DAO::singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = %1", $query_params); + $this->assertEquals(1, $is_deleted); + + $session_status = CRM_Core_Session::singleton()->getStatus(); + $this->assertEquals('Mr. Delete Me has been moved to the trash.', $session_status[0]['text']); + + // put settings back + Civi::settings()->set('contact_undelete', $old_undelete_setting); + } + + /** + * Test restore from trash. + */ + public function testRestoreFromTrash() { + // First, put in trash. + $this->testDeleteToTrash(); + // Clear session status + CRM_Core_Session::singleton()->getStatus(TRUE); + + $old_undelete_setting = Civi::settings()->get('contact_undelete'); + Civi::settings()->set('contact_undelete', '1'); + + $form = $this->getFormObject('CRM_Contact_Form_Task_Delete'); + $form->set('cid', $this->deleted_contact_id); + $form->set('restore', '1'); + $form->preProcess(); + $form->buildQuickForm(); + $form->setDefaultValues(); + $form->postProcess(); + + $query_params = [1 => [$this->deleted_contact_id, 'Integer']]; + $is_deleted = CRM_Core_DAO::singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = %1", $query_params); + $this->assertEquals(0, $is_deleted); + + $session_status = CRM_Core_Session::singleton()->getStatus(); + $this->assertEquals('Mr. Delete Me has been restored from the trash.', $session_status[0]['text']); + + // put settings back + Civi::settings()->set('contact_undelete', $old_undelete_setting); + } + + /** + * Test delete permanently. + * + * This is different from testDeleteWithoutTrash. This is where you have + * trash enabled and first move to trash, then delete from the trash. + */ + public function testDeletePermanently() { + // First, put in trash. + $this->testDeleteToTrash(); + // Clear session status + CRM_Core_Session::singleton()->getStatus(TRUE); + + $old_undelete_setting = Civi::settings()->get('contact_undelete'); + Civi::settings()->set('contact_undelete', '1'); + + $form = $this->getFormObject('CRM_Contact_Form_Task_Delete'); + $form->set('cid', $this->deleted_contact_id); + $form->set('skip_undelete', '1'); + $form->preProcess(); + $form->buildQuickForm(); + $form->setDefaultValues(); + $form->postProcess(); + + $query_params = [1 => [$this->deleted_contact_id, 'Integer']]; + $contact_id = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = %1", $query_params); + $this->assertEmpty($contact_id); + + $session_status = CRM_Core_Session::singleton()->getStatus(); + $this->assertEquals('Mr. Delete Me has been permanently deleted.', $session_status[0]['text']); + + // put settings back + Civi::settings()->set('contact_undelete', $old_undelete_setting); + } + + /** + * Test delete when trash is not enabled. + * + * This is different from testDeletePermanently. This is where trash is + * not enabled at all. + */ + public function testDeleteWithoutTrash() { + $old_undelete_setting = Civi::settings()->get('contact_undelete'); + Civi::settings()->set('contact_undelete', '0'); + + $form = $this->getFormObject('CRM_Contact_Form_Task_Delete'); + $form->set('cid', $this->deleted_contact_id); + $form->preProcess(); + $form->buildQuickForm(); + $form->setDefaultValues(); + $form->postProcess(); + + $query_params = [1 => [$this->deleted_contact_id, 'Integer']]; + $contact_id = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = %1", $query_params); + $this->assertEmpty($contact_id); + + // @todo This is currently buggy in the UI. It deletes properly but shows + // the wrong message. + //$session_status = CRM_Core_Session::singleton()->getStatus(); + //$this->assertEquals('Mr. Delete Me has been permanently deleted.', $session_status[0]['text']); + + // put settings back + Civi::settings()->set('contact_undelete', $old_undelete_setting); + } + +} -- 2.25.1