Exemple #1
0
    /**
     * Test send_instant_messages
     */
    public function test_send_instant_messages() {

        global $DB, $USER, $CFG;

        $this->resetAfterTest(true);

        // Turn off all message processors (so nothing is really sent)
        require_once($CFG->dirroot . '/message/lib.php');
        $messageprocessors = get_message_processors();
        foreach($messageprocessors as $messageprocessor) {
            $messageprocessor->enabled = 0;
            $DB->update_record('message_processors', $messageprocessor);
        }

        // Set the required capabilities by the external function
        $contextid = context_system::instance()->id;
        $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);

        $user1 = self::getDataGenerator()->create_user();

        // Create test message data.
        $message1 = array();
        $message1['touserid'] = $user1->id;
        $message1['text'] = 'the message.';
        $message1['clientmsgid'] = 4;
        $messages = array($message1);

        $sentmessages = core_message_external::send_instant_messages($messages);

        $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));

        // Confirm that the message was inserted correctly.
        $this->assertEquals($themessage->useridfrom, $USER->id);
        $this->assertEquals($themessage->useridto, $message1['touserid']);
        $this->assertEquals($themessage->smallmessage, $message1['text']);
        $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
    }
Exemple #2
0
 /**
  * Returns description of method result value
  *
  * @return external_description
  * @since Moodle 2.1
  * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more.
  * @see core_message_external::send_instant_messages_returns()
  */
 public static function send_instantmessages_returns()
 {
     return core_message_external::send_instant_messages_returns();
 }
 /**
  * Test search_contacts.
  */
 public function test_search_contacts()
 {
     global $DB;
     $this->resetAfterTest(true);
     $course1 = $this->getDataGenerator()->create_course();
     $course2 = $this->getDataGenerator()->create_course();
     $user1 = new stdClass();
     $user1->firstname = 'X';
     $user1->lastname = 'X';
     $user1 = $this->getDataGenerator()->create_user($user1);
     $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
     $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
     $user2 = new stdClass();
     $user2->firstname = 'Eric';
     $user2->lastname = 'Cartman';
     $user2 = self::getDataGenerator()->create_user($user2);
     $user3 = new stdClass();
     $user3->firstname = 'Stan';
     $user3->lastname = 'Marsh';
     $user3 = self::getDataGenerator()->create_user($user3);
     self::getDataGenerator()->enrol_user($user3->id, $course1->id);
     $user4 = new stdClass();
     $user4->firstname = 'Kyle';
     $user4->lastname = 'Broflovski';
     $user4 = self::getDataGenerator()->create_user($user4);
     $user5 = new stdClass();
     $user5->firstname = 'Kenny';
     $user5->lastname = 'McCormick';
     $user5 = self::getDataGenerator()->create_user($user5);
     self::getDataGenerator()->enrol_user($user5->id, $course2->id);
     $this->setUser($user1);
     $results = core_message_external::search_contacts('r');
     $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
     $this->assertCount(5, $results);
     // Users 2 through 5 + admin
     $results = core_message_external::search_contacts('r', true);
     $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
     $this->assertCount(2, $results);
     $results = core_message_external::search_contacts('Kyle', false);
     $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
     $this->assertCount(1, $results);
     $result = reset($results);
     $this->assertEquals($user4->id, $result['id']);
     $results = core_message_external::search_contacts('y', false);
     $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
     $this->assertCount(2, $results);
     $results = core_message_external::search_contacts('y', true);
     $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
     $this->assertCount(1, $results);
     $result = reset($results);
     $this->assertEquals($user5->id, $result['id']);
     // Empty query, will throw an exception.
     $this->setExpectedException('moodle_exception');
     $results = core_message_external::search_contacts('');
 }
 /**
  * Test get_blocked_users.
  */
 public function test_get_blocked_users()
 {
     $this->resetAfterTest(true);
     $user1 = self::getDataGenerator()->create_user();
     $userstranger = self::getDataGenerator()->create_user();
     $useroffline1 = self::getDataGenerator()->create_user();
     $useroffline2 = self::getDataGenerator()->create_user();
     $userblocked = self::getDataGenerator()->create_user();
     // Login as user1.
     $this->setUser($user1);
     $this->assertEquals(array(), core_message_external::create_contacts(array($useroffline1->id, $useroffline2->id)));
     // The userstranger sends a couple of messages to user1.
     $this->send_message($userstranger, $user1, 'Hello there!');
     $this->send_message($userstranger, $user1, 'How you goin?');
     // The userblocked sends a message to user1.
     // Note that this user is not blocked at this point.
     $this->send_message($userblocked, $user1, 'Here, have some spam.');
     // Retrieve the list of blocked users.
     $this->setUser($user1);
     $blockedusers = core_message_external::get_blocked_users($user1->id);
     $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
     $this->assertCount(0, $blockedusers['users']);
     // Block the $userblocked and retrieve again the list.
     core_message_external::block_contacts(array($userblocked->id));
     $blockedusers = core_message_external::get_blocked_users($user1->id);
     $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers);
     $this->assertCount(1, $blockedusers['users']);
 }
 /**
  * Test get_user_message_preferences permissions
  */
 public function test_get_user_message_preferences_permissions()
 {
     $this->resetAfterTest(true);
     $user = self::getDataGenerator()->create_user();
     $otheruser = self::getDataGenerator()->create_user();
     $this->setUser($user);
     $this->expectException('moodle_exception');
     $prefs = core_message_external::get_user_message_preferences($otheruser->id);
 }
 /**
  * Test mark_message_read.
  */
 public function test_mark_message_read()
 {
     $this->resetAfterTest(true);
     $user1 = self::getDataGenerator()->create_user();
     $user2 = self::getDataGenerator()->create_user();
     $user3 = self::getDataGenerator()->create_user();
     // Login as user1.
     $this->setUser($user1);
     $this->assertEquals(array(), core_message_external::create_contacts(array($user2->id, $user3->id)));
     // The user2 sends a couple of messages to user1.
     $this->send_message($user2, $user1, 'Hello there!');
     $this->send_message($user2, $user1, 'How you goin?');
     $this->send_message($user3, $user1, 'How you goin?');
     $this->send_message($user3, $user2, 'How you goin?');
     // Retrieve all messages sent by user2 (they are currently unread).
     $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
     $messageids = array();
     foreach ($lastmessages as $m) {
         $messageid = core_message_external::mark_message_read($m->id, time());
         $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid);
     }
     // Retrieve all messages sent (they are currently read).
     $lastmessages = message_get_messages($user1->id, $user2->id, 0, true);
     $this->assertCount(2, $lastmessages);
     $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
     $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
     // Retrieve all messages sent by any user (that are currently unread).
     $lastmessages = message_get_messages($user1->id, 0, 0, false);
     $this->assertCount(1, $lastmessages);
     // Invalid message ids.
     try {
         $messageid = core_message_external::mark_message_read($messageids[0]['messageid'] * 2, time());
         $this->fail('Exception expected due invalid messageid.');
     } catch (dml_missing_record_exception $e) {
         $this->assertEquals('invalidrecord', $e->errorcode);
     }
     // A message to a different user.
     $lastmessages = message_get_messages($user2->id, $user3->id, 0, false);
     $messageid = array_pop($lastmessages)->id;
     try {
         $messageid = core_message_external::mark_message_read($messageid, time());
         $this->fail('Exception expected due invalid messageid.');
     } catch (invalid_parameter_exception $e) {
         $this->assertEquals('invalidparameter', $e->errorcode);
     }
 }
Exemple #7
0
 /**
  * Test delete_message.
  */
 public function test_delete_message()
 {
     global $DB;
     $this->resetAfterTest(true);
     $user1 = self::getDataGenerator()->create_user();
     $user2 = self::getDataGenerator()->create_user();
     $user3 = self::getDataGenerator()->create_user();
     $user4 = self::getDataGenerator()->create_user();
     // Login as user1.
     $this->setUser($user1);
     $this->assertEquals(array(), core_message_external::create_contacts(array($user2->id, $user3->id)));
     // User user1 does not interchange messages with user3.
     $m1to2 = message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
     $m2to3 = message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
     $m3to2 = message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
     $m3to4 = message_post_message($user3, $user4, 'some random text 4', FORMAT_MOODLE);
     // Retrieve all messages sent by user2 (they are currently unread).
     $lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
     // Delete a message not read, as a user from.
     $result = core_message_external::delete_message($m1to2, $user1->id, false);
     $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
     $this->assertTrue($result['status']);
     $this->assertCount(0, $result['warnings']);
     $deletedmessage = $DB->get_record('message', array('id' => $m1to2));
     $this->assertNotEquals(0, $deletedmessage->timeuserfromdeleted);
     // Try to delete the same message again.
     $result = core_message_external::delete_message($m1to2, $user1->id, false);
     $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
     $this->assertFalse($result['status']);
     // Try to delete a message that does not belong to me.
     try {
         $messageid = core_message_external::delete_message($m2to3, $user3->id, false);
         $this->fail('Exception expected due invalid messageid.');
     } catch (moodle_exception $e) {
         $this->assertEquals('You do not have permission to delete this message', $e->errorcode);
     }
     $this->setUser($user3);
     // Delete a message not read, as a user to.
     $result = core_message_external::delete_message($m2to3, $user3->id, false);
     $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
     $this->assertTrue($result['status']);
     $this->assertCount(0, $result['warnings']);
     $deletedmessage = $DB->get_record('message', array('id' => $m2to3));
     $this->assertNotEquals(0, $deletedmessage->timeusertodeleted);
     // Delete a message read.
     $message = $DB->get_record('message', array('id' => $m3to2));
     $messageid = message_mark_message_read($message, time());
     $result = core_message_external::delete_message($messageid, $user3->id);
     $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
     $this->assertTrue($result['status']);
     $this->assertCount(0, $result['warnings']);
     $deletedmessage = $DB->get_record('message_read', array('id' => $messageid));
     $this->assertNotEquals(0, $deletedmessage->timeuserfromdeleted);
     // Invalid message ids.
     try {
         $result = core_message_external::delete_message(-1, $user1->id);
         $this->fail('Exception expected due invalid messageid.');
     } catch (dml_missing_record_exception $e) {
         $this->assertEquals('invalidrecord', $e->errorcode);
     }
     // Invalid user.
     try {
         $result = core_message_external::delete_message($m1to2, -1, false);
         $this->fail('Exception expected due invalid user.');
     } catch (moodle_exception $e) {
         $this->assertEquals('invaliduser', $e->errorcode);
     }
     // Not active user.
     delete_user($user2);
     try {
         $result = core_message_external::delete_message($m1to2, $user2->id, false);
         $this->fail('Exception expected due invalid user.');
     } catch (moodle_exception $e) {
         $this->assertEquals('userdeleted', $e->errorcode);
     }
     // Now, as an admin, try to delete any message.
     $this->setAdminUser();
     $result = core_message_external::delete_message($m3to4, $user4->id, false);
     $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result);
     $this->assertTrue($result['status']);
     $this->assertCount(0, $result['warnings']);
     $deletedmessage = $DB->get_record('message', array('id' => $m3to4));
     $this->assertNotEquals(0, $deletedmessage->timeusertodeleted);
 }
 /**
  * Test get_messages.
  */
 public function test_get_messages()
 {
     global $CFG;
     $this->resetAfterTest(true);
     $this->preventResetByRollback();
     // This mark the messages as read!.
     $sink = $this->redirectMessages();
     $user1 = self::getDataGenerator()->create_user();
     $user2 = self::getDataGenerator()->create_user();
     $user3 = self::getDataGenerator()->create_user();
     $course = self::getDataGenerator()->create_course();
     // Send a message from one user to another.
     message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
     message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
     message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
     message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
     message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
     $this->setUser($user1);
     // Get read conversations from user1 to user2.
     $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Get unread conversations from user1 to user2.
     $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(0, $messages['messages']);
     // Get read messages send from user1.
     $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(2, $messages['messages']);
     $this->setUser($user2);
     // Get read conversations from any user to user2.
     $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(2, $messages['messages']);
     $this->setUser($user3);
     // Get read notifications received by user3.
     $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(0, $messages['messages']);
     // Now, create some notifications...
     // We are creating fake notifications but based on real ones.
     // This one omits notification = 1.
     $eventdata = new stdClass();
     $eventdata->modulename = 'moodle';
     $eventdata->component = 'enrol_paypal';
     $eventdata->name = 'paypal_enrolment';
     $eventdata->userfrom = get_admin();
     $eventdata->userto = $user1;
     $eventdata->subject = "Moodle: PayPal payment";
     $eventdata->fullmessage = "Your PayPal payment is pending.";
     $eventdata->fullmessageformat = FORMAT_PLAIN;
     $eventdata->fullmessagehtml = '';
     $eventdata->smallmessage = '';
     message_send($eventdata);
     $message = new stdClass();
     $message->notification = 1;
     $message->component = 'enrol_manual';
     $message->name = 'expiry_notification';
     $message->userfrom = $user2;
     $message->userto = $user1;
     $message->subject = 'Enrolment expired';
     $message->fullmessage = 'Enrolment expired blah blah blah';
     $message->fullmessageformat = FORMAT_MARKDOWN;
     $message->fullmessagehtml = markdown_to_html($message->fullmessage);
     $message->smallmessage = $message->subject;
     $message->contexturlname = $course->fullname;
     $message->contexturl = (string) new moodle_url('/course/view.php', array('id' => $course->id));
     message_send($message);
     $userfrom = core_user::get_noreply_user();
     $userfrom->maildisplay = true;
     $eventdata = new stdClass();
     $eventdata->component = 'moodle';
     $eventdata->name = 'badgecreatornotice';
     $eventdata->userfrom = $userfrom;
     $eventdata->userto = $user1;
     $eventdata->notification = 1;
     $eventdata->subject = 'New badge';
     $eventdata->fullmessage = format_text_email($eventdata->subject, FORMAT_HTML);
     $eventdata->fullmessageformat = FORMAT_PLAIN;
     $eventdata->fullmessagehtml = $eventdata->subject;
     $eventdata->smallmessage = $eventdata->subject;
     message_send($eventdata);
     $eventdata = new stdClass();
     $eventdata->name = 'submission';
     $eventdata->component = 'mod_feedback';
     $eventdata->userfrom = $user1;
     $eventdata->userto = $user2;
     $eventdata->subject = 'Feedback submitted';
     $eventdata->fullmessage = 'Feedback submitted from an user';
     $eventdata->fullmessageformat = FORMAT_PLAIN;
     $eventdata->fullmessagehtml = '<strong>Feedback submitted</strong>';
     $eventdata->smallmessage = '';
     message_send($eventdata);
     $this->setUser($user1);
     // Get read notifications from any user to user1.
     $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(3, $messages['messages']);
     // Get one read notifications from any user to user1.
     $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Get unread notifications from any user to user1.
     $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(0, $messages['messages']);
     // Get read both type of messages from any user to user1.
     $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(4, $messages['messages']);
     // Get read notifications from no-reply-user to user1.
     $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Get notifications send by user1 to any user.
     $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Test warnings.
     $CFG->messaging = 0;
     $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['warnings']);
     // Test exceptions.
     // Messaging disabled.
     try {
         $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
         $this->fail('Exception expected due messaging disabled.');
     } catch (moodle_exception $e) {
         $this->assertEquals('disabled', $e->errorcode);
     }
     $CFG->messaging = 1;
     // Invalid users.
     try {
         $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
         $this->fail('Exception expected due invalid users.');
     } catch (moodle_exception $e) {
         $this->assertEquals('accessdenied', $e->errorcode);
     }
     // Invalid user ids.
     try {
         $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
         $this->fail('Exception expected due invalid users.');
     } catch (moodle_exception $e) {
         $this->assertEquals('invaliduser', $e->errorcode);
     }
     // Invalid users (permissions).
     $this->setUser($user2);
     try {
         $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
         $this->fail('Exception expected due invalid user.');
     } catch (moodle_exception $e) {
         $this->assertEquals('accessdenied', $e->errorcode);
     }
 }
Exemple #9
0
 /**
  * Test get message processor.
  */
 public function test_get_message_processor()
 {
     $this->resetAfterTest(true);
     // Create a user.
     $user1 = self::getDataGenerator()->create_user();
     // Set you as the user.
     $this->setUser($user1);
     // Get the message processors.
     $result = core_message_external::get_message_processor($user1->id, 'popup');
     // We need to execute the return values cleaning process to simulate the web service server.
     $result = external_api::clean_returnvalue(core_message_external::get_message_processor_returns(), $result);
     $this->assertNotEmpty($result['systemconfigured']);
     $this->assertNotEmpty($result['userconfigured']);
 }