Merge branch 'MDL-37354-master' of git://github.com/mouneyrac/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 15 Jan 2013 20:50:31 +0000 (21:50 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 15 Jan 2013 20:50:31 +0000 (21:50 +0100)
Conflicts:
message/tests/externallib_test.php

1  2 
lib/external/tests/externallib_test.php
message/tests/externallib_test.php
mod/assign/tests/externallib_test.php
user/tests/externallib_test.php

@@@ -94,284 -75,4 +97,283 @@@ class core_message_external_testcase ex
          $this->assertEquals($themessage->smallmessage, $message1['text']);
          $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
      }
 +
 +    /**
 +     * Test create_contacts.
 +     */
 +    public function test_create_contacts() {
 +        $this->resetAfterTest(true);
 +
 +        $user1 = self::getDataGenerator()->create_user();
 +        $user2 = self::getDataGenerator()->create_user();
 +        $user3 = self::getDataGenerator()->create_user();
 +        $user4 = self::getDataGenerator()->create_user();
 +        $user5 = self::getDataGenerator()->create_user();
 +        $this->setUser($user1);
 +
 +        // Adding a contact.
 +        $return = core_message_external::create_contacts(array($user2->id));
 +        $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
 +        $this->assertEquals(array(), $return);
 +
 +        // Adding a contact who is already a contact.
 +        $return = core_message_external::create_contacts(array($user2->id));
 +        $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
 +        $this->assertEquals(array(), $return);
 +
 +        // Adding multiple contacts.
 +        $return = core_message_external::create_contacts(array($user3->id, $user4->id));
 +        $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
 +        $this->assertEquals(array(), $return);
 +
 +        // Adding a non-existing user.
 +        $return = core_message_external::create_contacts(array(99999));
 +        $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
 +        $this->assertCount(1, $return);
 +        $return = array_pop($return);
 +        $this->assertEquals($return['warningcode'], 'contactnotcreated');
 +        $this->assertEquals($return['itemid'], 99999);
 +
 +        // Adding contacts with valid and invalid parameters.
 +        $return = core_message_external::create_contacts(array($user5->id, 99999));
 +        $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return);
 +        $this->assertCount(1, $return);
 +        $return = array_pop($return);
 +        $this->assertEquals($return['warningcode'], 'contactnotcreated');
 +        $this->assertEquals($return['itemid'], 99999);
 +    }
 +
 +    /**
 +     * Test delete_contacts.
 +     */
 +    public function test_delete_contacts() {
 +        $this->resetAfterTest(true);
 +
 +        $user1 = self::getDataGenerator()->create_user();
 +        $user2 = self::getDataGenerator()->create_user();
 +        $user3 = self::getDataGenerator()->create_user();
 +        $user4 = self::getDataGenerator()->create_user();
 +        $user5 = self::getDataGenerator()->create_user();
 +        $user6 = self::getDataGenerator()->create_user();
 +        $this->setUser($user1);
 +        $this->assertEquals(array(), core_message_external::create_contacts(
 +            array($user3->id, $user4->id, $user5->id, $user6->id)));
 +
 +        // Removing a non-contact.
 +        $return = core_message_external::delete_contacts(array($user2->id));
 +        $this->assertNull($return);
 +
 +        // Removing one contact.
 +        $return = core_message_external::delete_contacts(array($user3->id));
 +        $this->assertNull($return);
 +
 +        // Removing multiple contacts.
 +        $return = core_message_external::delete_contacts(array($user4->id, $user5->id));
 +        $this->assertNull($return);
 +
 +        // Removing contact from unexisting user.
 +        $return = core_message_external::delete_contacts(array(99999));
 +        $this->assertNull($return);
 +
 +        // Removing mixed valid and invalid data.
 +        $return = core_message_external::delete_contacts(array($user6->id, 99999));
 +        $this->assertNull($return);
 +    }
 +
 +    /**
 +     * Test block_contacts.
 +     */
 +    public function test_block_contacts() {
 +        $this->resetAfterTest(true);
 +
 +        $user1 = self::getDataGenerator()->create_user();
 +        $user2 = self::getDataGenerator()->create_user();
 +        $user3 = self::getDataGenerator()->create_user();
 +        $user4 = self::getDataGenerator()->create_user();
 +        $user5 = self::getDataGenerator()->create_user();
 +        $this->setUser($user1);
 +        $this->assertEquals(array(), core_message_external::create_contacts(array($user3->id, $user4->id, $user5->id)));
 +
 +        // Blocking a contact.
 +        $return = core_message_external::block_contacts(array($user2->id));
 +        $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
 +        $this->assertEquals(array(), $return);
 +
 +        // Blocking a contact who is already a contact.
 +        $return = core_message_external::block_contacts(array($user2->id));
 +        $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
 +        $this->assertEquals(array(), $return);
 +
 +        // Blocking multiple contacts.
 +        $return = core_message_external::block_contacts(array($user3->id, $user4->id));
 +        $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
 +        $this->assertEquals(array(), $return);
 +
 +        // Blocking a non-existing user.
 +        $return = core_message_external::block_contacts(array(99999));
 +        $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
 +        $this->assertCount(1, $return);
 +        $return = array_pop($return);
 +        $this->assertEquals($return['warningcode'], 'contactnotblocked');
 +        $this->assertEquals($return['itemid'], 99999);
 +
 +        // Blocking contacts with valid and invalid parameters.
 +        $return = core_message_external::block_contacts(array($user5->id, 99999));
 +        $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return);
 +        $this->assertCount(1, $return);
 +        $return = array_pop($return);
 +        $this->assertEquals($return['warningcode'], 'contactnotblocked');
 +        $this->assertEquals($return['itemid'], 99999);
 +    }
 +
 +    /**
 +     * Test unblock_contacts.
 +     */
 +    public function test_unblock_contacts() {
 +        $this->resetAfterTest(true);
 +
 +        $user1 = self::getDataGenerator()->create_user();
 +        $user2 = self::getDataGenerator()->create_user();
 +        $user3 = self::getDataGenerator()->create_user();
 +        $user4 = self::getDataGenerator()->create_user();
 +        $user5 = self::getDataGenerator()->create_user();
 +        $user6 = self::getDataGenerator()->create_user();
 +        $this->setUser($user1);
 +        $this->assertEquals(array(), core_message_external::create_contacts(
 +            array($user3->id, $user4->id, $user5->id, $user6->id)));
 +
 +        // Removing a non-contact.
 +        $return = core_message_external::unblock_contacts(array($user2->id));
 +        $this->assertNull($return);
 +
 +        // Removing one contact.
 +        $return = core_message_external::unblock_contacts(array($user3->id));
 +        $this->assertNull($return);
 +
 +        // Removing multiple contacts.
 +        $return = core_message_external::unblock_contacts(array($user4->id, $user5->id));
 +        $this->assertNull($return);
 +
 +        // Removing contact from unexisting user.
 +        $return = core_message_external::unblock_contacts(array(99999));
 +        $this->assertNull($return);
 +
 +        // Removing mixed valid and invalid data.
 +        $return = core_message_external::unblock_contacts(array($user6->id, 99999));
 +        $this->assertNull($return);
 +
 +    }
 +
 +    /**
 +     * Test get_contacts.
 +     */
 +    public function test_get_contacts() {
 +        $this->resetAfterTest(true);
 +
 +        $user1 = self::getDataGenerator()->create_user();
 +        $user_stranger = self::getDataGenerator()->create_user();
 +        $user_offline1 = self::getDataGenerator()->create_user();
 +        $user_offline2 = self::getDataGenerator()->create_user();
 +        $user_offline3 = self::getDataGenerator()->create_user();
 +        $user_online = new stdClass();
 +        $user_online->lastaccess = time();
 +        $user_online = self::getDataGenerator()->create_user($user_online);
 +        $user_blocked = self::getDataGenerator()->create_user();
 +
 +        // Login as user1.
 +        $this->setUser($user1);
 +        $this->assertEquals(array(), core_message_external::create_contacts(
 +            array($user_offline1->id, $user_offline2->id, $user_offline3->id, $user_online->id)));
 +
 +        // User_stranger sends a couple of messages to user1.
 +        $this->send_message($user_stranger, $user1, 'Hello there!');
 +        $this->send_message($user_stranger, $user1, 'How you goin?');
 +        $this->send_message($user_stranger, $user1, 'Cya!');
 +
 +        // User_blocked sends a message to user1.
 +        $this->send_message($user_blocked, $user1, 'Here, have some spam.');
 +
 +        // Retrieve the contacts of the user.
 +        $this->setUser($user1);
 +        $contacts = core_message_external::get_contacts();
 +        $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
 +        $this->assertCount(3, $contacts['offline']);
 +        $this->assertCount(1, $contacts['online']);
 +        $this->assertCount(2, $contacts['strangers']);
 +        core_message_external::block_contacts(array($user_blocked->id));
 +        $contacts = core_message_external::get_contacts();
 +        $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts);
 +        $this->assertCount(3, $contacts['offline']);
 +        $this->assertCount(1, $contacts['online']);
 +        $this->assertCount(1, $contacts['strangers']);
 +
 +        // Checking some of the fields returned.
 +        $stranger = array_pop($contacts['strangers']);
 +        $this->assertEquals($user_stranger->id, $stranger['id']);
 +        $this->assertEquals(3, $stranger['unread']);
 +    }
 +
 +    /**
 +     * 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);
 +
 +        // Searching for users, keep in mind that 'Admin User' and 'Guest user' can be returned for now.
 +        // See MDL-37164 which should fix that. Once fixed, remove the +2's.
 +        $this->setUser($user1);
 +        $results = core_message_external::search_contacts('r');
 +        $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results);
 +        $this->assertCount(4 + 2, $results);
 +        $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('');
 +    }
  }
Simple merge
Simple merge