[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/chat/tests/ -> externallib_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * External mod_chat functions unit tests
  19   *
  20   * @package    mod_chat
  21   * @category   external
  22   * @copyright  2015 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.0
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  
  31  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  32  
  33  /**
  34   * External mod_chat functions unit tests
  35   *
  36   * @package    mod_chat
  37   * @category   external
  38   * @copyright  2015 Juan Leyva <juan@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   * @since      Moodle 3.0
  41   */
  42  class mod_chat_external_testcase extends externallib_advanced_testcase {
  43  
  44      /**
  45       * Test login user
  46       */
  47      public function test_login_user() {
  48          global $DB;
  49  
  50          $this->resetAfterTest(true);
  51  
  52          // Setup test data.
  53          $this->setAdminUser();
  54          $course = $this->getDataGenerator()->create_course();
  55          $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
  56  
  57          $user = self::getDataGenerator()->create_user();
  58          $this->setUser($user);
  59          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  60          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  61  
  62          $result = mod_chat_external::login_user($chat->id);
  63          $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
  64  
  65          // Test session started.
  66          $sid = $DB->get_field('chat_users', 'sid', array('userid' => $user->id, 'chatid' => $chat->id));
  67          $this->assertEquals($result['chatsid'], $sid);
  68  
  69      }
  70  
  71      /**
  72       * Test get chat users
  73       */
  74      public function test_get_chat_users() {
  75          global $DB;
  76  
  77          $this->resetAfterTest(true);
  78  
  79          // Setup test data.
  80          $this->setAdminUser();
  81          $course = $this->getDataGenerator()->create_course();
  82          $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
  83  
  84          $user1 = self::getDataGenerator()->create_user();
  85          $user2 = self::getDataGenerator()->create_user();
  86  
  87          $this->setUser($user1);
  88          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  89          $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
  90          $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
  91  
  92          $result = mod_chat_external::login_user($chat->id);
  93          $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
  94  
  95          $this->setUser($user2);
  96          $result = mod_chat_external::login_user($chat->id);
  97          $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
  98  
  99          // Get users.
 100          $result = mod_chat_external::get_chat_users($result['chatsid']);
 101          $result = external_api::clean_returnvalue(mod_chat_external::get_chat_users_returns(), $result);
 102  
 103          // Check correct users.
 104          $this->assertCount(2, $result['users']);
 105          $found = 0;
 106          foreach ($result['users'] as $user) {
 107              if ($user['id'] == $user1->id or $user['id'] == $user2->id) {
 108                  $found++;
 109              }
 110          }
 111          $this->assertEquals(2, $found);
 112  
 113      }
 114  
 115      /**
 116       * Test send and get chat messages
 117       */
 118      public function test_send_get_chat_message() {
 119          global $DB;
 120  
 121          $this->resetAfterTest(true);
 122  
 123          // Setup test data.
 124          $this->setAdminUser();
 125          $course = $this->getDataGenerator()->create_course();
 126          $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
 127  
 128          $user = self::getDataGenerator()->create_user();
 129          $this->setUser($user);
 130          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 131          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
 132  
 133          $result = mod_chat_external::login_user($chat->id);
 134          $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
 135          $chatsid = $result['chatsid'];
 136  
 137          $result = mod_chat_external::send_chat_message($chatsid, 'hello!');
 138          $result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result);
 139  
 140          // Test messages received.
 141  
 142          $result = mod_chat_external::get_chat_latest_messages($chatsid, 0);
 143          $result = external_api::clean_returnvalue(mod_chat_external::get_chat_latest_messages_returns(), $result);
 144  
 145          foreach ($result['messages'] as $message) {
 146              // Ommit system messages, like user just joined in.
 147              if ($message['system']) {
 148                  continue;
 149              }
 150              $this->assertEquals('hello!', $message['message']);
 151          }
 152      }
 153  
 154      /**
 155       * Test view_chat
 156       */
 157      public function test_view_chat() {
 158          global $DB;
 159  
 160          $this->resetAfterTest(true);
 161  
 162          // Setup test data.
 163          $this->setAdminUser();
 164          $course = $this->getDataGenerator()->create_course();
 165          $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
 166          $context = context_module::instance($chat->cmid);
 167          $cm = get_coursemodule_from_instance('chat', $chat->id);
 168  
 169          // Test invalid instance id.
 170          try {
 171              mod_chat_external::view_chat(0);
 172              $this->fail('Exception expected due to invalid mod_chat instance id.');
 173          } catch (moodle_exception $e) {
 174              $this->assertEquals('invalidrecord', $e->errorcode);
 175          }
 176  
 177          // Test not-enrolled user.
 178          $user = self::getDataGenerator()->create_user();
 179          $this->setUser($user);
 180          try {
 181              mod_chat_external::view_chat($chat->id);
 182              $this->fail('Exception expected due to not enrolled user.');
 183          } catch (moodle_exception $e) {
 184              $this->assertEquals('requireloginerror', $e->errorcode);
 185          }
 186  
 187          // Test user with full capabilities.
 188          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 189          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
 190  
 191          // Trigger and capture the event.
 192          $sink = $this->redirectEvents();
 193  
 194          $result = mod_chat_external::view_chat($chat->id);
 195          $result = external_api::clean_returnvalue(mod_chat_external::view_chat_returns(), $result);
 196  
 197          $events = $sink->get_events();
 198          $this->assertCount(1, $events);
 199          $event = array_shift($events);
 200  
 201          // Checking that the event contains the expected values.
 202          $this->assertInstanceOf('\mod_chat\event\course_module_viewed', $event);
 203          $this->assertEquals($context, $event->get_context());
 204          $moodlechat = new \moodle_url('/mod/chat/view.php', array('id' => $cm->id));
 205          $this->assertEquals($moodlechat, $event->get_url());
 206          $this->assertEventContextNotUsed($event);
 207          $this->assertNotEmpty($event->get_name());
 208  
 209          // Test user with no capabilities.
 210          // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
 211          assign_capability('mod/chat:chat', CAP_PROHIBIT, $studentrole->id, $context->id);
 212          accesslib_clear_all_caches_for_unit_testing();
 213  
 214          try {
 215              mod_chat_external::view_chat($chat->id);
 216              $this->fail('Exception expected due to missing capability.');
 217          } catch (moodle_exception $e) {
 218              $this->assertEquals('nopermissions', $e->errorcode);
 219          }
 220      }
 221  
 222      /**
 223       * Test get_chats_by_courses
 224       */
 225      public function test_get_chats_by_courses() {
 226          global $DB, $USER;
 227          $this->resetAfterTest(true);
 228          $this->setAdminUser();
 229          $course1 = self::getDataGenerator()->create_course();
 230          $chatoptions1 = array(
 231                                'course' => $course1->id,
 232                                'name' => 'First Chat'
 233                               );
 234          $chat1 = self::getDataGenerator()->create_module('chat', $chatoptions1);
 235          $course2 = self::getDataGenerator()->create_course();
 236          $chatoptions2 = array(
 237                                'course' => $course2->id,
 238                                'name' => 'Second Chat'
 239                               );
 240          $chat2 = self::getDataGenerator()->create_module('chat', $chatoptions2);
 241          $student1 = $this->getDataGenerator()->create_user();
 242          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 243  
 244          // Enroll Student1 in Course1.
 245          self::getDataGenerator()->enrol_user($student1->id,  $course1->id, $studentrole->id);
 246          $this->setUser($student1);
 247  
 248          $chats = mod_chat_external::get_chats_by_courses();
 249          // We need to execute the return values cleaning process to simulate the web service server.
 250          $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
 251          $this->assertCount(1, $chats['chats']);
 252          $this->assertEquals('First Chat', $chats['chats'][0]['name']);
 253          // We see 12 fields.
 254          $this->assertCount(12, $chats['chats'][0]);
 255  
 256          // As Student you cannot see some chat properties like 'section'.
 257          $this->assertFalse(isset($chats['chats'][0]['section']));
 258  
 259          // Student1 is not enrolled in course2. The webservice will return a warning!
 260          $chats = mod_chat_external::get_chats_by_courses(array($course2->id));
 261          // We need to execute the return values cleaning process to simulate the web service server.
 262          $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
 263          $this->assertCount(0, $chats['chats']);
 264          $this->assertEquals(1, $chats['warnings'][0]['warningcode']);
 265  
 266          // Now as admin.
 267          $this->setAdminUser();
 268          // As Admin we can see this chat.
 269          $chats = mod_chat_external::get_chats_by_courses(array($course2->id));
 270          // We need to execute the return values cleaning process to simulate the web service server.
 271          $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
 272  
 273          $this->assertCount(1, $chats['chats']);
 274          $this->assertEquals('Second Chat', $chats['chats'][0]['name']);
 275          // We see 17 fields.
 276          $this->assertCount(17, $chats['chats'][0]);
 277          // As an Admin you can see some chat properties like 'section'.
 278          $this->assertEquals(0, $chats['chats'][0]['section']);
 279  
 280          // Enrol student in the second course.
 281          self::getDataGenerator()->enrol_user($student1->id,  $course2->id, $studentrole->id);
 282          $this->setUser($student1);
 283          $chats = mod_chat_external::get_chats_by_courses();
 284          $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
 285          $this->assertCount(2, $chats['chats']);
 286  
 287      }
 288  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1