Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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   * Unit tests for Web service events.
  19   *
  20   * @package    webservice
  21   * @category   phpunit
  22   * @copyright  2013 Frédéric Massart
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_webservice\event;
  27  
  28  /**
  29   * Unit tests for Web service events.
  30   *
  31   * @package    webservice
  32   * @category   phpunit
  33   * @copyright  2013 Frédéric Massart
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class events_test extends \advanced_testcase {
  37  
  38      public function setUp(): void {
  39          $this->resetAfterTest();
  40      }
  41  
  42      public function test_function_called() {
  43          // The Web service API doesn't allow the testing of the events directly by
  44          // calling some functions which trigger the events, so what we are going here
  45          // is just checking that the event returns the expected information.
  46  
  47          $sink = $this->redirectEvents();
  48  
  49          $fakelogdata = array(1, 'B', true, null);
  50          $params = array(
  51              'other' => array(
  52                  'function' => 'A function'
  53              )
  54          );
  55          $event = \core\event\webservice_function_called::create($params);
  56          $event->set_legacy_logdata($fakelogdata);
  57          $event->trigger();
  58  
  59          $events = $sink->get_events();
  60          $this->assertCount(1, $events);
  61          $event = reset($events);
  62  
  63          $this->assertEquals(\context_system::instance(), $event->get_context());
  64          $this->assertEquals('A function', $event->other['function']);
  65          $this->assertEventLegacyLogData($fakelogdata, $event);
  66          $this->assertEventContextNotUsed($event);
  67      }
  68  
  69      public function test_login_failed() {
  70          // The Web service API doesn't allow the testing of the events directly by
  71          // calling some functions which trigger the events, so what we are going here
  72          // is just checking that the event returns the expected information.
  73  
  74          $sink = $this->redirectEvents();
  75  
  76          $fakelogdata = array(1, 'B', true, null);
  77          $params = array(
  78              'other' => array(
  79                  'reason' => 'Unit Test',
  80                  'method' => 'Some method',
  81                  'tokenid' => '123'
  82              )
  83          );
  84          $event = \core\event\webservice_login_failed::create($params);
  85          $event->set_legacy_logdata($fakelogdata);
  86          $event->trigger();
  87  
  88          $events = $sink->get_events();
  89          $this->assertCount(1, $events);
  90          $event = reset($events);
  91  
  92          $this->assertEquals(\context_system::instance(), $event->get_context());
  93          $this->assertEquals($params['other']['reason'], $event->other['reason']);
  94          $this->assertEquals($params['other']['method'], $event->other['method']);
  95          $this->assertEquals($params['other']['tokenid'], $event->other['tokenid']);
  96          $this->assertEventLegacyLogData($fakelogdata, $event);
  97  
  98          // We cannot set the token in the other properties.
  99          $params['other']['token'] = 'I should not be set';
 100          try {
 101              $event = \core\event\webservice_login_failed::create($params);
 102              $this->fail('The token cannot be allowed in \core\event\webservice_login_failed');
 103          } catch (\coding_exception $e) {
 104          }
 105          $this->assertEventContextNotUsed($event);
 106      }
 107  
 108      public function test_service_created() {
 109          global $CFG, $DB;
 110  
 111          // The Web service API doesn't allow the testing of the events directly by
 112          // calling some functions which trigger the events, so what we are going here
 113          // is just checking that the event returns the expected information.
 114  
 115          $sink = $this->redirectEvents();
 116  
 117          // Creating a fake service.
 118          $service = (object) array(
 119              'name' => 'Test',
 120              'enabled' => 1,
 121              'requiredcapability' => '',
 122              'restrictedusers' => 0,
 123              'component' => null,
 124              'timecreated' => time(),
 125              'timemodified' => time(),
 126              'shortname' => null,
 127              'downloadfiles' => 0,
 128              'uploadfiles' => 0
 129          );
 130          $service->id = $DB->insert_record('external_services', $service);
 131  
 132          // Trigger the event.
 133          $params = array(
 134              'objectid' => $service->id,
 135          );
 136          $event = \core\event\webservice_service_created::create($params);
 137          $event->add_record_snapshot('external_services', $service);
 138          $event->trigger();
 139  
 140          $events = $sink->get_events();
 141          $this->assertCount(1, $events);
 142          $event = reset($events);
 143  
 144          // Assert that the event contains the right information.
 145          $this->assertEquals(\context_system::instance(), $event->get_context());
 146          $this->assertEquals($service->id, $event->objectid);
 147          $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
 148          $expected = array(SITEID, 'webservice', 'add', $returnurl, get_string('addservice', 'webservice', $service));
 149          $this->assertEventLegacyLogData($expected, $event);
 150          $this->assertEventContextNotUsed($event);
 151      }
 152  
 153      public function test_service_updated() {
 154          global $CFG, $DB;
 155  
 156          // The Web service API doesn't allow the testing of the events directly by
 157          // calling some functions which trigger the events, so what we are going here
 158          // is just checking that the event returns the expected information.
 159  
 160          $sink = $this->redirectEvents();
 161  
 162          // Creating a fake service.
 163          $service = (object) array(
 164              'name' => 'Test',
 165              'enabled' => 1,
 166              'requiredcapability' => '',
 167              'restrictedusers' => 0,
 168              'component' => null,
 169              'timecreated' => time(),
 170              'timemodified' => time(),
 171              'shortname' => null,
 172              'downloadfiles' => 0,
 173              'uploadfiles' => 0
 174          );
 175          $service->id = $DB->insert_record('external_services', $service);
 176  
 177          // Trigger the event.
 178          $params = array(
 179              'objectid' => $service->id,
 180          );
 181          $event = \core\event\webservice_service_updated::create($params);
 182          $event->add_record_snapshot('external_services', $service);
 183          $event->trigger();
 184  
 185          $events = $sink->get_events();
 186          $this->assertCount(1, $events);
 187          $event = reset($events);
 188  
 189          // Assert that the event contains the right information.
 190          $this->assertEquals(\context_system::instance(), $event->get_context());
 191          $this->assertEquals($service->id, $event->objectid);
 192          $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
 193          $expected = array(SITEID, 'webservice', 'edit', $returnurl, get_string('editservice', 'webservice', $service));
 194          $this->assertEventLegacyLogData($expected, $event);
 195          $this->assertEventContextNotUsed($event);
 196      }
 197  
 198      public function test_service_deleted() {
 199          global $CFG, $DB;
 200  
 201          // The Web service API doesn't allow the testing of the events directly by
 202          // calling some functions which trigger the events, so what we are going here
 203          // is just checking that the event returns the expected information.
 204  
 205          $sink = $this->redirectEvents();
 206  
 207          // Creating a fake service.
 208          $service = (object) array(
 209              'name' => 'Test',
 210              'enabled' => 1,
 211              'requiredcapability' => '',
 212              'restrictedusers' => 0,
 213              'component' => null,
 214              'timecreated' => time(),
 215              'timemodified' => time(),
 216              'shortname' => null,
 217              'downloadfiles' => 0,
 218              'uploadfiles' => 0
 219          );
 220          $service->id = $DB->insert_record('external_services', $service);
 221  
 222          // Trigger the event.
 223          $params = array(
 224              'objectid' => $service->id,
 225          );
 226          $event = \core\event\webservice_service_deleted::create($params);
 227          $event->add_record_snapshot('external_services', $service);
 228          $event->trigger();
 229  
 230          $events = $sink->get_events();
 231          $this->assertCount(1, $events);
 232          $event = reset($events);
 233  
 234          // Assert that the event contains the right information.
 235          $this->assertEquals(\context_system::instance(), $event->get_context());
 236          $this->assertEquals($service->id, $event->objectid);
 237          $returnurl = $CFG->wwwroot . "/" . $CFG->admin . "/settings.php?section=externalservices";
 238          $expected = array(SITEID, 'webservice', 'delete', $returnurl, get_string('deleteservice', 'webservice', $service));
 239          $this->assertEventLegacyLogData($expected, $event);
 240          $this->assertEventContextNotUsed($event);
 241      }
 242  
 243      public function test_service_user_added() {
 244          global $CFG;
 245  
 246          // The Web service API doesn't allow the testing of the events directly by
 247          // calling some functions which trigger the events, so what we are going here
 248          // is just checking that the event returns the expected information.
 249  
 250          $sink = $this->redirectEvents();
 251  
 252          $params = array(
 253              'objectid' => 1,
 254              'relateduserid' => 2
 255          );
 256          $event = \core\event\webservice_service_user_added::create($params);
 257          $event->trigger();
 258  
 259          $events = $sink->get_events();
 260          $this->assertCount(1, $events);
 261          $event = reset($events);
 262  
 263          $this->assertEquals(\context_system::instance(), $event->get_context());
 264          $this->assertEquals(1, $event->objectid);
 265          $this->assertEquals(2, $event->relateduserid);
 266          $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
 267              'add', '', $params['relateduserid']);
 268          $this->assertEventLegacyLogData($expected, $event);
 269          $this->assertEventContextNotUsed($event);
 270      }
 271  
 272      public function test_service_user_removed() {
 273          global $CFG;
 274  
 275          // The Web service API doesn't allow the testing of the events directly by
 276          // calling some functions which trigger the events, so what we are going here
 277          // is just checking that the event returns the expected information.
 278  
 279          $sink = $this->redirectEvents();
 280  
 281          $params = array(
 282              'objectid' => 1,
 283              'relateduserid' => 2
 284          );
 285          $event = \core\event\webservice_service_user_removed::create($params);
 286          $event->trigger();
 287  
 288          $events = $sink->get_events();
 289          $this->assertCount(1, $events);
 290          $event = reset($events);
 291  
 292          $this->assertEquals(\context_system::instance(), $event->get_context());
 293          $this->assertEquals(1, $event->objectid);
 294          $this->assertEquals(2, $event->relateduserid);
 295          $expected = array(SITEID, 'core', 'assign', $CFG->admin . '/webservice/service_users.php?id=' . $params['objectid'],
 296              'remove', '', $params['relateduserid']);
 297          $this->assertEventLegacyLogData($expected, $event);
 298          $this->assertEventContextNotUsed($event);
 299      }
 300  
 301      public function test_token_created() {
 302          // The Web service API doesn't allow the testing of the events directly by
 303          // calling some functions which trigger the events, so what we are going here
 304          // is just checking that the event returns the expected information.
 305  
 306          $sink = $this->redirectEvents();
 307  
 308          $params = array(
 309              'objectid' => 1,
 310              'relateduserid' => 2,
 311              'other' => array(
 312                  'auto' => true
 313              )
 314          );
 315          $event = \core\event\webservice_token_created::create($params);
 316          $event->trigger();
 317  
 318          $events = $sink->get_events();
 319          $this->assertCount(1, $events);
 320          $event = reset($events);
 321  
 322          $this->assertEquals(\context_system::instance(), $event->get_context());
 323          $this->assertEquals(1, $event->objectid);
 324          $this->assertEquals(2, $event->relateduserid);
 325          $expected = array(SITEID, 'webservice', 'automatically create user token', '' , 'User ID: ' . 2);
 326          $this->assertEventLegacyLogData($expected, $event);
 327          $this->assertEventContextNotUsed($event);
 328      }
 329  
 330      public function test_token_sent() {
 331          $user = $this->getDataGenerator()->create_user();
 332          $this->setUser($user);
 333  
 334          // The Web service API doesn't allow the testing of the events directly by
 335          // calling some functions which trigger the events, so what we are going here
 336          // is just checking that the event returns the expected information.
 337  
 338          $sink = $this->redirectEvents();
 339  
 340          $params = array(
 341              'objectid' => 1,
 342              'other' => array(
 343                  'auto' => true
 344              )
 345          );
 346          $event = \core\event\webservice_token_sent::create($params);
 347          $event->trigger();
 348  
 349          $events = $sink->get_events();
 350          $this->assertCount(1, $events);
 351          $event = reset($events);
 352  
 353          $this->assertEquals(\context_system::instance(), $event->get_context());
 354          $this->assertEquals(1, $event->objectid);
 355          $expected = array(SITEID, 'webservice', 'sending requested user token', '' , 'User ID: ' . $user->id);
 356          $this->assertEventLegacyLogData($expected, $event);
 357          $this->assertEventContextNotUsed($event);
 358      }
 359  }