/** * This is function deletes a mail according to the parameter in the post request. * the records to the page. * */ private function delete_mail() { $msgid = required_param('messageid', PARAM_INT); $mailrecord = new mail_record($msgid); $mailrecord->delete_mail($this->courseid); }
/** * This function tests deleting a mail. One of the emails in the starter * DB is deleted, then I check to make sure it is gone. * */ public function test_delete_mail() { global $DB; $courseid = $this->testcourseid; // Try deleting a mail the user does not own. $this->setUser($this->martha); // Grab the mail with an attachment. $query = "SELECT * FROM {course_message_mails} WHERE message = 'This mail will be deleted.'"; $record = $DB->get_record_sql($query); $mailid = intval($record->id); // Note the explicit conversion to an int here. $mailrecord = new mail_record($mailid, false); $mailrecord->delete_mail($courseid); // Make sure no copies were deleted. $res = $DB->get_records('course_message_mail_map', array('mailid' => $mailid)); $this->assertEquals(count($res), 2); // Now work on mails the user owns. $this->setUser($this->friend); // Use same mail. $mailrecord = new mail_record($mailid, false); $mailrecord->delete_mail($courseid); // See if Friend's copy was deleted. $res = $DB->get_records('course_message_mail_map', array('mailid' => $mailid, 'userid' => $this->friend->id)); $this->assertEquals(count($res), 0); // But ensure that Craig still has his copy. $res = $DB->get_records('course_message_mail_map', array('mailid' => $mailid, 'userid' => $this->craig->id)); $this->assertEquals(count($res), 1); // Ensure that the mails table copy is still there. $res = $DB->get_records('course_message_mails', array('id' => $mailid)); $this->assertEquals(count($res), 1); // And that the attachment is as well. $res = $DB->get_records('files', array('itemid' => $record->attachment)); $this->assertEquals(count($res), 2); // Now delete Craig's copy -> this should chain to delete the mail entirely. $this->setUser($this->craig); $mailrecord = new mail_record($mailid, false); $mailrecord->delete_mail($courseid); // Check to see if map record was deleted. $res = $DB->get_records('course_message_mail_map', array('mailid' => $mailid, 'userid' => $this->craig->id)); $this->assertEquals(count($res), 0); // Now the record in the mails table should be gone too -> no remaining references. $res = $DB->get_records('course_message_mails', array('id' => $mailid)); $this->assertEquals(count($res), 0); // And that the attachment should be gone as well. $res = $DB->get_records('files', array('itemid' => $record->attachment)); $this->assertEquals(count($res), 0); }