/**
  * This function creates the mail to be used for testing.  Only 5 mail messages
  * are created -> {one generic mail, one mail with an attachment, and three
  * mail as part of a thread}.
  *
  */
 protected function create_test_mail()
 {
     global $DB;
     ob_start();
     $this->setUser($this->craig);
     // Send first test mail -> generic.
     $params = array("subject" => 'Test Mail 1', "message" => 'Generic message goes here.', "parentmessage" => 0, "replytype" => 'single', "draftid" => 0);
     $mailto = array("{$this->friend->id}");
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     // Second test mail: used for deletion test.
     $params = array("subject" => 'Delete Mail 1', "message" => 'This mail will be deleted.', "parentmessage" => 0, "replytype" => 'single', "draftid" => 0);
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     // Here I'm spoofing adding an attachment afterwards: I think this could be condensed to 1 step.
     $query = "SELECT * FROM {course_message_mails} WHERE message = '{$params['message']}'";
     $record = $DB->get_record_sql($query);
     $record->attachment = self::FILEID;
     $DB->update_record('course_message_mails', $record);
     // Now create a thread of three.
     $params = array("subject" => 'Threaded Message Test', "message" => 'This is the parent for the thread.', "parentmessage" => 0, "replytype" => 'single', "draftid" => 0);
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     // Switch to friend and reply.
     $this->setUser($this->friend);
     $params = array("subject" => 'Threaded Message Test', "message" => 'This is the first reply.', "parentmessage" => $this->mailids[2], "replytype" => 'single', "draftid" => 0);
     $mailto = array("1");
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     // Now reply back.
     $this->setUser($this->craig);
     $params = array("subject" => 'Threaded Message Test', "message" => 'This is the second reply.', "parentmessage" => $this->mailids[3], "replytype" => 'single', "draftid" => 0);
     $mailto = array("1");
     $mailrecord = new mail_record(0, false);
     $mailrecord->set_send_params($params, $mailto);
     $mailrecord->send_mail($this->testcourseid);
     $this->mailids[] = $this->get_mail_id($params['message']);
     ob_end_clean();
 }
 /**
  * This is function sends mail using the parameters that are in the post request.
  *
  */
 private function send_mail()
 {
     $context = context_course::instance($this->courseid);
     if (!has_capability('block/course_message:sendmail', $context)) {
         echo json_encode(array('result' => false, 'text' => get_string('usercannotsendmail', BLOCK_CM_LANG_TABLE)));
         return;
     }
     // Grab the posted paramaters - slows things down, but allows for easier unit testing.
     $params = array("subject" => optional_param('subject', "no subject", PARAM_TEXT), "message" => required_param('message', PARAM_CLEANHTML), "parentmessage" => optional_param('parent', 0, PARAM_INT), "replytype" => optional_param('replytype', 'single', PARAM_TEXT), "draftid" => optional_param(BLOCK_CM_FILE_AREA_NAME, 0, PARAM_INT));
     /* If the optional param does not find mailTo (it is not sent on inline replies), then it will
      * already be setup as an array and not need to be JSON decoded. */
     $temp = optional_param('mailTo', array("1"), PARAM_TEXT);
     $mailto = !is_array($temp) ? json_decode($temp) : $temp;
     // The carboncopy field is actually optional: json decode it if it exists, set to null otherwise.
     $temp = optional_param('cc', null, PARAM_TEXT);
     $carboncopy = $temp === null ? $temp : json_decode($temp);
     $mailrecord = new mail_record(0);
     $mailrecord->set_send_params($params, $mailto, $carboncopy);
     $mailrecord->send_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);
 }