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 310 and 401] [Versions 39 and 401]

   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   * Badge events tests.
  18   *
  19   * @package    core_badges
  20   * @copyright  2015 onwards Simey Lameze <simey@moodle.com>
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  defined('MOODLE_INTERNAL') || die();
  24  global $CFG;
  25  require_once($CFG->dirroot . '/badges/tests/badgeslib_test.php');
  26  
  27  /**
  28   * Badge events tests class.
  29   *
  30   * @package    core_badges
  31   * @copyright  2015 onwards Simey Lameze <simey@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class events_test extends badgeslib_test {
  35  
  36      /**
  37       * Test badge awarded event.
  38       */
  39      public function test_badge_awarded() {
  40  
  41          $systemcontext = context_system::instance();
  42  
  43          $sink = $this->redirectEvents();
  44  
  45          $badge = new badge($this->badgeid);
  46          $badge->issue($this->user->id, true);
  47          $badge->is_issued($this->user->id);
  48          $events = $sink->get_events();
  49          $this->assertCount(1, $events);
  50          $event = reset($events);
  51          $this->assertInstanceOf('\core\event\badge_awarded', $event);
  52          $this->assertEquals($this->badgeid, $event->objectid);
  53          $this->assertEquals($this->user->id, $event->relateduserid);
  54          $this->assertEquals($systemcontext, $event->get_context());
  55  
  56          $sink->close();
  57      }
  58  
  59      /**
  60       * Test the badge created event.
  61       *
  62       * There is no external API for creating a badge, so the unit test will simply
  63       * create and trigger the event and ensure data is returned as expected.
  64       */
  65      public function test_badge_created() {
  66  
  67          $badge = new badge($this->badgeid);
  68          // Trigger an event: badge created.
  69          $eventparams = array(
  70              'userid' => $badge->usercreated,
  71              'objectid' => $badge->id,
  72              'context' => $badge->get_context(),
  73          );
  74  
  75          $event = \core\event\badge_created::create($eventparams);
  76          // Trigger and capture the event.
  77          $sink = $this->redirectEvents();
  78          $event->trigger();
  79          $events = $sink->get_events();
  80          $event = reset($events);
  81  
  82          // Check that the event data is valid.
  83          $this->assertInstanceOf('\core\event\badge_created', $event);
  84          $this->assertEquals($badge->usercreated, $event->userid);
  85          $this->assertEquals($badge->id, $event->objectid);
  86          $this->assertDebuggingNotCalled();
  87          $sink->close();
  88  
  89      }
  90  
  91      /**
  92       * Test the badge archived event.
  93       *
  94       */
  95      public function test_badge_archived() {
  96          $badge = new badge($this->badgeid);
  97          $sink = $this->redirectEvents();
  98  
  99          // Trigger and capture the event.
 100          $badge->delete(true);
 101          $events = $sink->get_events();
 102          $this->assertCount(2, $events);
 103          $event = $events[1];
 104  
 105          // Check that the event data is valid.
 106          $this->assertInstanceOf('\core\event\badge_archived', $event);
 107          $this->assertEquals($badge->id, $event->objectid);
 108          $this->assertDebuggingNotCalled();
 109          $sink->close();
 110  
 111      }
 112  
 113  
 114      /**
 115       * Test the badge updated event.
 116       *
 117       */
 118      public function test_badge_updated() {
 119          $badge = new badge($this->badgeid);
 120          $sink = $this->redirectEvents();
 121  
 122          // Trigger and capture the event.
 123          $badge->save();
 124          $events = $sink->get_events();
 125          $event = reset($events);
 126          $this->assertCount(1, $events);
 127  
 128          // Check that the event data is valid.
 129          $this->assertInstanceOf('\core\event\badge_updated', $event);
 130          $this->assertEquals($badge->id, $event->objectid);
 131          $this->assertDebuggingNotCalled();
 132          $sink->close();
 133  
 134      }
 135      /**
 136       * Test the badge deleted event.
 137       */
 138      public function test_badge_deleted() {
 139          $badge = new badge($this->badgeid);
 140          $sink = $this->redirectEvents();
 141  
 142          // Trigger and capture the event.
 143          $badge->delete(false);
 144          $events = $sink->get_events();
 145          $event = reset($events);
 146          $this->assertCount(1, $events);
 147  
 148          // Check that the event data is valid.
 149          $this->assertInstanceOf('\core\event\badge_deleted', $event);
 150          $this->assertEquals($badge->id, $event->objectid);
 151          $this->assertDebuggingNotCalled();
 152          $sink->close();
 153  
 154      }
 155  
 156      /**
 157       * Test the badge duplicated event.
 158       *
 159       */
 160      public function test_badge_duplicated() {
 161          $badge = new badge($this->badgeid);
 162          $sink = $this->redirectEvents();
 163  
 164          // Trigger and capture the event.
 165          $newid = $badge->make_clone();
 166          $events = $sink->get_events();
 167          $event = reset($events);
 168          $this->assertCount(1, $events);
 169  
 170          // Check that the event data is valid.
 171          $this->assertInstanceOf('\core\event\badge_duplicated', $event);
 172          $this->assertEquals($newid, $event->objectid);
 173          $this->assertDebuggingNotCalled();
 174          $sink->close();
 175  
 176      }
 177  
 178      /**
 179       * Test the badge disabled event.
 180       *
 181       */
 182      public function test_badge_disabled() {
 183          $badge = new badge($this->badgeid);
 184          $sink = $this->redirectEvents();
 185  
 186          // Trigger and capture the event.
 187          $badge->set_status(BADGE_STATUS_INACTIVE);
 188          $events = $sink->get_events();
 189          $event = reset($events);
 190          $this->assertCount(2, $events);
 191          $event = $events[1];
 192  
 193          // Check that the event data is valid.
 194          $this->assertInstanceOf('\core\event\badge_disabled', $event);
 195          $this->assertEquals($badge->id, $event->objectid);
 196          $this->assertDebuggingNotCalled();
 197          $sink->close();
 198  
 199      }
 200  
 201      /**
 202       * Test the badge enabled event.
 203       *
 204       */
 205      public function test_badge_enabled() {
 206          $badge = new badge($this->badgeid);
 207          $sink = $this->redirectEvents();
 208  
 209          // Trigger and capture the event.
 210          $badge->set_status(BADGE_STATUS_ACTIVE);
 211          $events = $sink->get_events();
 212          $event = reset($events);
 213          $this->assertCount(2, $events);
 214          $event = $events[1];
 215  
 216          // Check that the event data is valid.
 217          $this->assertInstanceOf('\core\event\badge_enabled', $event);
 218          $this->assertEquals($badge->id, $event->objectid);
 219          $this->assertDebuggingNotCalled();
 220          $sink->close();
 221  
 222      }
 223  
 224      /**
 225       * Test the badge criteria created event.
 226       *
 227       * There is no external API for this, so the unit test will simply
 228       * create and trigger the event and ensure data is returned as expected.
 229       */
 230      public function test_badge_criteria_created() {
 231  
 232          $badge = new badge($this->badgeid);
 233  
 234          // Trigger and capture the event.
 235          $sink = $this->redirectEvents();
 236          $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
 237          $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 238          $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
 239          $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
 240          $criteriaprofile->save($params);
 241          $events = $sink->get_events();
 242          $event = reset($events);
 243  
 244          // Check that the event data is valid.
 245          $this->assertCount(1, $events);
 246          $this->assertInstanceOf('\core\event\badge_criteria_created', $event);
 247          $this->assertEquals($criteriaprofile->id, $event->objectid);
 248          $this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
 249          $this->assertDebuggingNotCalled();
 250          $sink->close();
 251  
 252      }
 253  
 254      /**
 255       * Test the badge criteria updated event.
 256       *
 257       * There is no external API for this, so the unit test will simply
 258       * create and trigger the event and ensure data is returned as expected.
 259       */
 260      public function test_badge_criteria_updated() {
 261  
 262          $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
 263          $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 264          $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
 265          $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
 266          $criteriaprofile->save($params);
 267          $badge = new badge($this->badgeid);
 268  
 269          // Trigger and capture the event.
 270          $sink = $this->redirectEvents();
 271          $criteria = $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE];
 272          $params2 = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'id' => $criteria->id);
 273          $criteria->save((array)$params2);
 274          $events = $sink->get_events();
 275          $event = reset($events);
 276  
 277          // Check that the event data is valid.
 278          $this->assertCount(1, $events);
 279          $this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
 280          $this->assertEquals($criteria->id, $event->objectid);
 281          $this->assertEquals($this->badgeid, $event->other['badgeid']);
 282          $this->assertDebuggingNotCalled();
 283          $sink->close();
 284  
 285      }
 286  
 287      /**
 288       * Test the badge criteria deleted event.
 289       *
 290       * There is no external API for this, so the unit test will simply
 291       * create and trigger the event and ensure data is returned as expected.
 292       */
 293      public function test_badge_criteria_deleted() {
 294  
 295          $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
 296          $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
 297          $badge = new badge($this->badgeid);
 298  
 299          // Trigger and capture the event.
 300          $sink = $this->redirectEvents();
 301          $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
 302          $events = $sink->get_events();
 303          $event = reset($events);
 304  
 305          // Check that the event data is valid.
 306          $this->assertCount(1, $events);
 307          $this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
 308          $this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
 309          $this->assertDebuggingNotCalled();
 310          $sink->close();
 311  
 312      }
 313  
 314      /**
 315       * Test the badge viewed event.
 316       *
 317       * There is no external API for viewing a badge, so the unit test will simply
 318       * create and trigger the event and ensure data is returned as expected.
 319       */
 320      public function test_badge_viewed() {
 321  
 322          $badge = new badge($this->badgeid);
 323          // Trigger an event: badge viewed.
 324          $other = array('badgeid' => $badge->id, 'badgehash' => '12345678');
 325          $eventparams = array(
 326              'context' => $badge->get_context(),
 327              'other' => $other,
 328          );
 329  
 330          $event = \core\event\badge_viewed::create($eventparams);
 331          // Trigger and capture the event.
 332          $sink = $this->redirectEvents();
 333          $event->trigger();
 334          $events = $sink->get_events();
 335          $event = reset($events);
 336  
 337          // Check that the event data is valid.
 338          $this->assertInstanceOf('\core\event\badge_viewed', $event);
 339          $this->assertEquals('12345678', $event->other['badgehash']);
 340          $this->assertEquals($badge->id, $event->other['badgeid']);
 341          $this->assertDebuggingNotCalled();
 342          $sink->close();
 343  
 344      }
 345  
 346      /**
 347       * Test the badge listing viewed event.
 348       *
 349       * There is no external API for viewing a badge, so the unit test will simply
 350       * create and trigger the event and ensure data is returned as expected.
 351       */
 352      public function test_badge_listing_viewed() {
 353  
 354          // Trigger an event: badge listing viewed.
 355          $context = context_system::instance();
 356          $eventparams = array(
 357              'context' => $context,
 358              'other' => array('badgetype' => BADGE_TYPE_SITE)
 359          );
 360  
 361          $event = \core\event\badge_listing_viewed::create($eventparams);
 362          // Trigger and capture the event.
 363          $sink = $this->redirectEvents();
 364          $event->trigger();
 365          $events = $sink->get_events();
 366          $event = reset($events);
 367  
 368          // Check that the event data is valid.
 369          $this->assertInstanceOf('\core\event\badge_listing_viewed', $event);
 370          $this->assertEquals(BADGE_TYPE_SITE, $event->other['badgetype']);
 371          $this->assertDebuggingNotCalled();
 372          $sink->close();
 373  
 374      }
 375  }