Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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 calendar functions unit tests
      19   *
      20   * @package    core_calendar
      21   * @category   external
      22   * @copyright  2012 Ankit Agarwal
      23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      24   */
      25  
      26  defined('MOODLE_INTERNAL') || die();
      27  
      28  global $CFG;
      29  
      30  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
      31  
      32  /**
      33   * External course functions unit tests
      34   *
      35   * @package    core_calendar
      36   * @category   external
      37   * @copyright  2012 Ankit Agarwal
      38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      39   * @since Moodle 2.5
      40   */
      41  class core_calendar_externallib_testcase extends externallib_advanced_testcase {
      42  
      43      /**
      44       * Tests set up
      45       */
      46      protected function setUp() {
      47          global $CFG;
      48          require_once($CFG->dirroot . '/calendar/externallib.php');
      49      }
      50  
      51      /** Create calendar events or update them
      52       * Set $prop->id, if you want to do an update instead of creating an new event
      53       *
      54       * @param string $name        Event title
      55       * @param int    $userid      User id
      56       * @param string $type        Event type
      57       * @param int    $repeats     Number of repeated events to create
      58       * @param int    $timestart   Time stamp of the event start
      59       * @param mixed  $prop        List of event properties as array or object
      60       * @return mixed              Event object or false;
      61       * @since Moodle 2.5
      62       */
      63  
      64      public static function create_calendar_event($name, $userid = 0, $type = 'user', $repeats = 0, $timestart  = null, $prop = null) {
      65          global $CFG, $DB, $USER, $SITE;
      66  
      67          require_once("$CFG->dirroot/calendar/lib.php");
      68          if (!empty($prop)) {
      69              if (is_array($prop)) {
      70                  $prop = (object)$prop;
      71              }
      72          } else {
      73              $prop = new stdClass();
      74          }
      75          $prop->name = $name;
      76          if (empty($prop->eventtype)) {
      77              $prop->eventtype = $type;
      78          }
      79          if (empty($prop->repeats)) {
      80              $prop->repeats = $repeats;
      81          }
      82          if (empty($prop->timestart)) {
      83              $prop->timestart = time();
      84          }
      85          if (empty($prop->timeduration)) {
      86              $prop->timeduration = 0;
      87          }
      88          if (empty($prop->repeats)) {
      89              $prop->repeat = 0;
      90          } else {
      91              $prop->repeat = 1;
      92          }
      93          if (empty($prop->userid)) {
      94              if (!empty($userid)) {
      95                  $prop->userid = $userid;
      96              } else {
      97                  return false;
      98              }
      99          }
     100          if (!isset($prop->courseid)) {
     101              $prop->courseid = $SITE->id;
     102          }
     103          $event = new calendar_event($prop);
     104          return $event->create($prop);
     105      }
     106  
     107      public function test_create_calendar_events () {
     108          global $DB, $USER;
     109  
     110          $this->setAdminUser();
     111          $this->resetAfterTest();
     112          $prevcount = count($DB->get_records("event"));
     113  
     114          // Create a few events and do asserts.
     115          $this->create_calendar_event('test', $USER->id);
     116          $where = $DB->sql_compare_text('name') ." = ?";
     117          $count = count($DB->get_records_select("event", $where, array('test')));
     118          $this->assertEquals(1, $count);
     119          $aftercount = count($DB->get_records("event"));
     120          $this->assertEquals($prevcount + 1, $aftercount);
     121  
     122          $this->create_calendar_event('user', $USER->id, 'user', 3);
     123          $where = $DB->sql_compare_text('name') ." = ?";
     124          $count = count($DB->get_records_select("event", $where, array('user')));
     125  
     126          $this->assertEquals(3, $count);
     127          $aftercount = count($DB->get_records("event"));
     128          $this->assertEquals($prevcount + 4, $aftercount);
     129  
     130      }
     131  
     132      /**
     133       * Test delete_calendar_events
     134       */
     135      public function test_delete_calendar_events() {
     136          global $DB, $USER;
     137  
     138          $this->resetAfterTest(true);
     139          $this->setAdminUser();
     140  
     141          // Create a few stuff to test with.
     142          $user = $this->getDataGenerator()->create_user();
     143          $course = $this->getDataGenerator()->create_course();
     144          $record = new stdClass();
     145          $record->courseid = $course->id;
     146          $group = $this->getDataGenerator()->create_group($record);
     147  
     148          $notdeletedcount = $DB->count_records('event');
     149  
     150          // Let's create a few events.
     151          $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
     152          $record = new stdClass();
     153          $record->courseid = $course->id;
     154          $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
     155          $userevent = $this->create_calendar_event('user', $USER->id);
     156          $record = new stdClass();
     157          $record->courseid = $course->id;
     158          $record->groupid = $group->id;
     159          $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
     160  
     161          // Now lets try to delete stuff with proper rights.
     162          $events = array(
     163                  array('eventid' => $siteevent->id, 'repeat' => 0),
     164                  array('eventid' => $courseevent->id, 'repeat' => 1),
     165                  array('eventid' => $userevent->id, 'repeat' => 0),
     166                  array('eventid' => $groupevent->id, 'repeat' => 0)
     167                  );
     168          core_calendar_external::delete_calendar_events($events);
     169  
     170          // Check to see if things were deleted properly.
     171          $deletedcount = $DB->count_records('event');
     172          $this->assertEquals($notdeletedcount, $deletedcount);
     173  
     174          // Let's create a few events.
     175          $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
     176          $record = new stdClass();
     177          $record->courseid = $course->id;
     178          $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
     179          $userevent = $this->create_calendar_event('user', $USER->id);
     180          $record = new stdClass();
     181          $record->courseid = $course->id;
     182          $record->groupid = $group->id;
     183          $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
     184  
     185          $this->setuser($user);
     186          $sitecontext = context_system::instance();
     187          $coursecontext = context_course::instance($course->id);
     188          $usercontext = context_user::instance($user->id);
     189          $role = $DB->get_record('role', array('shortname' => 'student'));
     190          $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
     191  
     192          // Remove all caps.
     193          $this->unassignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
     194          $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
     195          $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
     196          $this->unassignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
     197  
     198          // Assign proper caps and attempt delete.
     199           $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
     200           $events = array(
     201                  array('eventid' => $siteevent->id, 'repeat' => 0),
     202                  );
     203          core_calendar_external::delete_calendar_events($events);
     204          $deletedcount = $DB->count_records('event');
     205          $count = $notdeletedcount+5;
     206          $this->assertEquals($count, $deletedcount);
     207  
     208           $this->assignUserCapability('moodle/calendar:manageentries', $sitecontext->id, $role->id);
     209           $events = array(
     210                  array('eventid' => $courseevent->id, 'repeat' => 0),
     211                  );
     212          core_calendar_external::delete_calendar_events($events);
     213          $deletedcount = $DB->count_records('event');
     214          $count = $notdeletedcount+4;
     215          $this->assertEquals($count, $deletedcount);
     216  
     217           $this->assignUserCapability('moodle/calendar:manageownentries', $usercontext->id, $role->id);
     218           $events = array(
     219                  array('eventid' => $userevent->id, 'repeat' => 0),
     220                  );
     221          core_calendar_external::delete_calendar_events($events);
     222          $deletedcount = $DB->count_records('event');
     223          $count = $notdeletedcount+3;
     224          $this->assertEquals($count, $deletedcount);
     225  
     226           $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
     227           $events = array(
     228                  array('eventid' => $groupevent->id, 'repeat' => 0),
     229                  );
     230          core_calendar_external::delete_calendar_events($events);
     231          $deletedcount = $DB->count_records('event');
     232          $count = $notdeletedcount+2;
     233          $this->assertEquals($count, $deletedcount);
     234  
     235          $notdeletedcount = $deletedcount;
     236  
     237          // Let us try deleting without caps.
     238  
     239          $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
     240          $record = new stdClass();
     241          $record->courseid = $course->id;
     242          $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 3, time(), $record);
     243          $userevent = $this->create_calendar_event('user', $USER->id);
     244          $record = new stdClass();
     245          $record->courseid = $course->id;
     246          $record->groupid = $group->id;
     247          $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
     248  
     249          $this->setGuestUser();
     250          $this->setExpectedException('moodle_exception');
     251          $events = array(
     252              array('eventid' => $siteevent->id, 'repeat' => 0),
     253              array('eventid' => $courseevent->id, 'repeat' => 0),
     254              array('eventid' => $userevent->id, 'repeat' => 0),
     255              array('eventid' => $groupevent->id, 'repeat' => 0)
     256          );
     257          core_calendar_external::delete_calendar_events($events);
     258      }
     259  
     260      /**
     261       * Test get_calendar_events
     262       */
     263      public function test_get_calendar_events() {
     264          global $DB, $USER;
     265  
     266          $this->resetAfterTest(true);
     267          $this->setAdminUser();
     268  
     269          // Create a few stuff to test with.
     270          $user = $this->getDataGenerator()->create_user();
     271          $course = $this->getDataGenerator()->create_course();
     272          $record = new stdClass();
     273          $record->courseid = $course->id;
     274          $group = $this->getDataGenerator()->create_group($record);
     275  
     276          $beforecount = $DB->count_records('event');
     277  
     278          // Let's create a few events.
     279          $siteevent = $this->create_calendar_event('site', $USER->id, 'site');
     280          $record = new stdClass();
     281          $record->courseid = $course->id;
     282          $courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
     283          $userevent = $this->create_calendar_event('user', $USER->id);
     284          $record = new stdClass();
     285          $record->courseid = $course->id;
     286          $record->groupid = $group->id;
     287          $groupevent = $this->create_calendar_event('group', $USER->id, 'group', 0, time(), $record);
     288  
     289          $paramevents = array ('eventids' => array($siteevent->id), 'courseids' => array($course->id), 'groupids' => array($group->id));
     290          $options = array ('siteevents' => true, 'userevents' => true);
     291          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     292          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     293  
     294          // Check to see if we got all events.
     295          $this->assertEquals(4, count($events['events']));
     296          $this->assertEquals(0, count($events['warnings']));
     297          $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + 7*WEEKSECS);
     298          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     299          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     300          $this->assertEquals(5, count($events['events']));
     301          $this->assertEquals(0, count($events['warnings']));
     302  
     303          // Let's play around with caps.
     304          $this->setUser($user);
     305          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     306          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     307          $this->assertEquals(2, count($events['events'])); // site, user.
     308          $this->assertEquals(2, count($events['warnings'])); // course, group.
     309  
     310          $role = $DB->get_record('role', array('shortname' => 'student'));
     311          $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
     312          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     313          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     314          $this->assertEquals(4, count($events['events'])); // site, user, both course events.
     315          $this->assertEquals(1, count($events['warnings'])); // group.
     316  
     317          $options = array ('siteevents' => true, 'userevents' => true);
     318          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     319          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     320          $this->assertEquals(3, count($events['events'])); // site, user, one course event.
     321          $this->assertEquals(1, count($events['warnings'])); // group.
     322  
     323          groups_add_member($group, $user);
     324          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     325          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     326          $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
     327          $this->assertEquals(0, count($events['warnings']));
     328  
     329          $paramevents = array ('courseids' => array($course->id), 'groupids' => array($group->id));
     330          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     331          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     332          $this->assertEquals(4, count($events['events'])); // site, user, group, one course event.
     333          $this->assertEquals(0, count($events['warnings']));
     334  
     335          $paramevents = array ('groupids' => array($group->id, 23));
     336          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     337          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     338          $this->assertEquals(3, count($events['events'])); // site, user, group.
     339          $this->assertEquals(1, count($events['warnings']));
     340  
     341          $paramevents = array ('courseids' => array(23));
     342          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     343          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     344          $this->assertEquals(2, count($events['events'])); // site, user.
     345          $this->assertEquals(1, count($events['warnings']));
     346  
     347          $paramevents = array ();
     348          $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
     349          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     350          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     351          $this->assertEquals(0, count($events['events'])); // nothing returned.
     352          $this->assertEquals(0, count($events['warnings']));
     353  
     354          $paramevents = array ('eventids' => array($siteevent->id, $groupevent->id));
     355          $options = array ('siteevents' => false, 'userevents' => false, 'timeend' => time() + 7*WEEKSECS);
     356          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     357          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     358          $this->assertEquals(2, count($events['events'])); // site, group.
     359          $this->assertEquals(0, count($events['warnings']));
     360  
     361          $paramevents = array ('eventids' => array($siteevent->id));
     362          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     363          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     364          $this->assertEquals(1, count($events['events'])); // site.
     365          $this->assertEquals(0, count($events['warnings']));
     366  
     367          // Try getting a course event by its id.
     368          $paramevents = array ('eventids' => array($courseevent->id));
     369          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     370          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     371          $this->assertEquals(1, count($events['events']));
     372          $this->assertEquals(0, count($events['warnings']));
     373  
     374          // Now, create an activity event.
     375          $this->setAdminUser();
     376          $nexttime = time() + DAYSECS;
     377          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id, 'duedate' => $nexttime));
     378  
     379          $this->setUser($user);
     380          $paramevents = array ('courseids' => array($course->id));
     381          $options = array ('siteevents' => true, 'userevents' => true, 'timeend' => time() + WEEKSECS);
     382          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     383          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     384  
     385          $this->assertCount(5, $events['events']);
     386  
     387          // Hide the assignment.
     388          set_coursemodule_visible($assign->cmid, 0);
     389          // Empty all the caches that may be affected  by this change.
     390          accesslib_clear_all_caches_for_unit_testing();
     391          course_modinfo::clear_instance_cache();
     392  
     393          $events = core_calendar_external::get_calendar_events($paramevents, $options);
     394          $events = external_api::clean_returnvalue(core_calendar_external::get_calendar_events_returns(), $events);
     395          // Expect one less.
     396          $this->assertCount(4, $events['events']);
     397      }
     398  
     399      /**
     400       * Test core_calendar_external::create_calendar_events
     401       */
     402      public function test_core_create_calendar_events() {
     403          global $DB, $USER, $SITE;
     404  
     405          $this->resetAfterTest(true);
     406          $this->setAdminUser();
     407  
     408          // Create a few stuff to test with.
     409          $user = $this->getDataGenerator()->create_user();
     410          $course = $this->getDataGenerator()->create_course();
     411          $record = new stdClass();
     412          $record->courseid = $course->id;
     413          $group = $this->getDataGenerator()->create_group($record);
     414  
     415          $prevcount = $DB->count_records('event');
     416  
     417          // Let's create a few events.
     418          $events = array (
     419                  array('name' => 'site', 'courseid' => $SITE->id, 'eventtype' => 'site'),
     420                  array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
     421                  array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
     422                  array('name' => 'user')
     423                  );
     424          $eventsret = core_calendar_external::create_calendar_events($events);
     425          $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
     426  
     427          // Check to see if things were created properly.
     428          $aftercount = $DB->count_records('event');
     429          $this->assertEquals($prevcount + 5, $aftercount);
     430          $this->assertEquals(5, count($eventsret['events']));
     431          $this->assertEquals(0, count($eventsret['warnings']));
     432  
     433          $sitecontext = context_system::instance();
     434          $coursecontext = context_course::instance($course->id);
     435  
     436          $this->setUser($user);
     437          $prevcount = $aftercount;
     438          $events = array (
     439                  array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
     440                  array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
     441                  array('name' => 'user')
     442          );
     443          $role = $DB->get_record('role', array('shortname' => 'student'));
     444          $this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
     445          groups_add_member($group, $user);
     446          $this->assignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
     447          $this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
     448          $eventsret = core_calendar_external::create_calendar_events($events);
     449          $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
     450          // Check to see if things were created properly.
     451          $aftercount = $DB->count_records('event');
     452          $this->assertEquals($prevcount + 4, $aftercount);
     453          $this->assertEquals(4, count($eventsret['events']));
     454          $this->assertEquals(0, count($eventsret['warnings']));
     455  
     456          // Check to see nothing was created without proper permission.
     457          $this->setGuestUser();
     458          $prevcount = $DB->count_records('event');
     459          $eventsret = core_calendar_external::create_calendar_events($events);
     460          $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
     461          $aftercount = $DB->count_records('event');
     462          $this->assertEquals($prevcount, $aftercount);
     463          $this->assertEquals(0, count($eventsret['events']));
     464          $this->assertEquals(3, count($eventsret['warnings']));
     465  
     466          $this->setUser($user);
     467          $this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
     468          $this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
     469          $prevcount = $DB->count_records('event');
     470          $eventsret = core_calendar_external::create_calendar_events($events);
     471          $eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
     472          $aftercount = $DB->count_records('event');
     473          $this->assertEquals($prevcount + 1, $aftercount); // User event.
     474          $this->assertEquals(1, count($eventsret['events']));
     475          $this->assertEquals(2, count($eventsret['warnings']));
     476      }
     477  }
    

    Search This Site: