Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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   * Tests for forum events.
  19   *
  20   * @package    mod_forum
  21   * @category   test
  22   * @copyright  2014 Dan Poltawski <dan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Tests for forum events.
  30   *
  31   * @package    mod_forum
  32   * @category   test
  33   * @copyright  2014 Dan Poltawski <dan@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class mod_forum_events_testcase extends advanced_testcase {
  37  
  38      /**
  39       * Tests set up.
  40       */
  41      public function setUp() {
  42          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  43          // tests using these functions.
  44          \mod_forum\subscriptions::reset_forum_cache();
  45  
  46          $this->resetAfterTest();
  47      }
  48  
  49      public function tearDown() {
  50          // We must clear the subscription caches. This has to be done both before each test, and after in case of other
  51          // tests using these functions.
  52          \mod_forum\subscriptions::reset_forum_cache();
  53      }
  54  
  55      /**
  56       * Ensure course_searched event validates that searchterm is set.
  57       *
  58       * @expectedException        coding_exception
  59       * @expectedExceptionMessage The 'searchterm' value must be set in other.
  60       */
  61      public function test_course_searched_searchterm_validation() {
  62          $course = $this->getDataGenerator()->create_course();
  63          $coursectx = context_course::instance($course->id);
  64          $params = array(
  65              'context' => $coursectx,
  66          );
  67  
  68          \mod_forum\event\course_searched::create($params);
  69      }
  70  
  71      /**
  72       * Ensure course_searched event validates that context is the correct level.
  73       *
  74       * @expectedException        coding_exception
  75       * @expectedExceptionMessage Context level must be CONTEXT_COURSE.
  76       */
  77      public function test_course_searched_context_validation() {
  78          $course = $this->getDataGenerator()->create_course();
  79          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
  80          $context = context_module::instance($forum->cmid);
  81          $params = array(
  82              'context' => $context,
  83              'other' => array('searchterm' => 'testing'),
  84          );
  85  
  86          \mod_forum\event\course_searched::create($params);
  87      }
  88  
  89      /**
  90       * Test course_searched event.
  91       */
  92      public function test_course_searched() {
  93  
  94          // Setup test data.
  95          $course = $this->getDataGenerator()->create_course();
  96          $coursectx = context_course::instance($course->id);
  97          $searchterm = 'testing123';
  98  
  99          $params = array(
 100              'context' => $coursectx,
 101              'other' => array('searchterm' => $searchterm),
 102          );
 103  
 104          // Create event.
 105          $event = \mod_forum\event\course_searched::create($params);
 106  
 107          // Trigger and capture the event.
 108          $sink = $this->redirectEvents();
 109          $event->trigger();
 110          $events = $sink->get_events();
 111          $this->assertCount(1, $events);
 112          $event = reset($events);
 113  
 114           // Checking that the event contains the expected values.
 115          $this->assertInstanceOf('\mod_forum\event\course_searched', $event);
 116          $this->assertEquals($coursectx, $event->get_context());
 117          $expected = array($course->id, 'forum', 'search', "search.php?id={$course->id}&amp;search={$searchterm}", $searchterm);
 118          $this->assertEventLegacyLogData($expected, $event);
 119          $this->assertEventContextNotUsed($event);
 120  
 121          $this->assertNotEmpty($event->get_name());
 122      }
 123  
 124      /**
 125       * Ensure discussion_created event validates that forumid is set.
 126       *
 127       * @expectedException        coding_exception
 128       * @expectedExceptionMessage The 'forumid' value must be set in other.
 129       */
 130      public function test_discussion_created_forumid_validation() {
 131          $course = $this->getDataGenerator()->create_course();
 132          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 133          $context = context_module::instance($forum->cmid);
 134  
 135          $params = array(
 136              'context' => $context,
 137          );
 138  
 139          \mod_forum\event\discussion_created::create($params);
 140      }
 141  
 142      /**
 143       * Ensure discussion_created event validates that the context is the correct level.
 144       *
 145       * @expectedException        coding_exception
 146       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 147       */
 148      public function test_discussion_created_context_validation() {
 149          $course = $this->getDataGenerator()->create_course();
 150          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 151  
 152          $params = array(
 153              'context' => context_system::instance(),
 154              'other' => array('forumid' => $forum->id),
 155          );
 156  
 157          \mod_forum\event\discussion_created::create($params);
 158      }
 159  
 160      /**
 161       * Test discussion_created event.
 162       */
 163      public function test_discussion_created() {
 164  
 165          // Setup test data.
 166          $course = $this->getDataGenerator()->create_course();
 167          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 168          $user = $this->getDataGenerator()->create_user();
 169  
 170          // Add a discussion.
 171          $record = array();
 172          $record['course'] = $course->id;
 173          $record['forum'] = $forum->id;
 174          $record['userid'] = $user->id;
 175          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 176  
 177          $context = context_module::instance($forum->cmid);
 178  
 179          $params = array(
 180              'context' => $context,
 181              'objectid' => $discussion->id,
 182              'other' => array('forumid' => $forum->id),
 183          );
 184  
 185          // Create the event.
 186          $event = \mod_forum\event\discussion_created::create($params);
 187  
 188          // Trigger and capturing the event.
 189          $sink = $this->redirectEvents();
 190          $event->trigger();
 191          $events = $sink->get_events();
 192          $this->assertCount(1, $events);
 193          $event = reset($events);
 194  
 195          // Check that the event contains the expected values.
 196          $this->assertInstanceOf('\mod_forum\event\discussion_created', $event);
 197          $this->assertEquals($context, $event->get_context());
 198          $expected = array($course->id, 'forum', 'add discussion', "discuss.php?d={$discussion->id}", $discussion->id, $forum->cmid);
 199          $this->assertEventLegacyLogData($expected, $event);
 200          $this->assertEventContextNotUsed($event);
 201  
 202          $this->assertNotEmpty($event->get_name());
 203      }
 204  
 205      /**
 206       * Ensure discussion_updated event validates that forumid is set.
 207       *
 208       * @expectedException        coding_exception
 209       * @expectedExceptionMessage The 'forumid' value must be set in other.
 210       */
 211      public function test_discussion_updated_forumid_validation() {
 212          $course = $this->getDataGenerator()->create_course();
 213          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 214          $context = context_module::instance($forum->cmid);
 215  
 216          $params = array(
 217              'context' => $context,
 218          );
 219  
 220          \mod_forum\event\discussion_updated::create($params);
 221      }
 222  
 223      /**
 224       * Ensure discussion_created event validates that the context is the correct level.
 225       *
 226       * @expectedException        coding_exception
 227       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 228       */
 229      public function test_discussion_updated_context_validation() {
 230          $course = $this->getDataGenerator()->create_course();
 231          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 232  
 233          $params = array(
 234              'context' => context_system::instance(),
 235              'other' => array('forumid' => $forum->id),
 236          );
 237  
 238          \mod_forum\event\discussion_updated::create($params);
 239      }
 240  
 241      /**
 242       * Test discussion_created event.
 243       */
 244      public function test_discussion_updated() {
 245  
 246          // Setup test data.
 247          $course = $this->getDataGenerator()->create_course();
 248          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 249          $user = $this->getDataGenerator()->create_user();
 250  
 251          // Add a discussion.
 252          $record = array();
 253          $record['course'] = $course->id;
 254          $record['forum'] = $forum->id;
 255          $record['userid'] = $user->id;
 256          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 257  
 258          $context = context_module::instance($forum->cmid);
 259  
 260          $params = array(
 261              'context' => $context,
 262              'objectid' => $discussion->id,
 263              'other' => array('forumid' => $forum->id),
 264          );
 265  
 266          // Create the event.
 267          $event = \mod_forum\event\discussion_updated::create($params);
 268  
 269          // Trigger and capturing the event.
 270          $sink = $this->redirectEvents();
 271          $event->trigger();
 272          $events = $sink->get_events();
 273          $this->assertCount(1, $events);
 274          $event = reset($events);
 275  
 276          // Check that the event contains the expected values.
 277          $this->assertInstanceOf('\mod_forum\event\discussion_updated', $event);
 278          $this->assertEquals($context, $event->get_context());
 279          $this->assertEventContextNotUsed($event);
 280  
 281          $this->assertNotEmpty($event->get_name());
 282      }
 283  
 284      /**
 285       * Ensure discussion_deleted event validates that forumid is set.
 286       *
 287       * @expectedException        coding_exception
 288       * @expectedExceptionMessage The 'forumid' value must be set in other.
 289       */
 290      public function test_discussion_deleted_forumid_validation() {
 291          $course = $this->getDataGenerator()->create_course();
 292          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 293          $context = context_module::instance($forum->cmid);
 294  
 295          $params = array(
 296              'context' => $context,
 297          );
 298  
 299          \mod_forum\event\discussion_deleted::create($params);
 300      }
 301  
 302      /**
 303       * Ensure discussion_deleted event validates that context is of the correct level.
 304       *
 305       * @expectedException        coding_exception
 306       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 307       */
 308      public function test_discussion_deleted_context_validation() {
 309          $course = $this->getDataGenerator()->create_course();
 310          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 311  
 312          $params = array(
 313              'context' => context_system::instance(),
 314              'other' => array('forumid' => $forum->id),
 315          );
 316  
 317          \mod_forum\event\discussion_deleted::create($params);
 318      }
 319  
 320      /**
 321       * Test discussion_deleted event.
 322       */
 323      public function test_discussion_deleted() {
 324  
 325          // Setup test data.
 326          $course = $this->getDataGenerator()->create_course();
 327          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 328          $user = $this->getDataGenerator()->create_user();
 329  
 330          // Add a discussion.
 331          $record = array();
 332          $record['course'] = $course->id;
 333          $record['forum'] = $forum->id;
 334          $record['userid'] = $user->id;
 335          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 336  
 337          $context = context_module::instance($forum->cmid);
 338  
 339          $params = array(
 340              'context' => $context,
 341              'objectid' => $discussion->id,
 342              'other' => array('forumid' => $forum->id),
 343          );
 344  
 345          $event = \mod_forum\event\discussion_deleted::create($params);
 346  
 347          // Trigger and capture the event.
 348          $sink = $this->redirectEvents();
 349          $event->trigger();
 350          $events = $sink->get_events();
 351          $this->assertCount(1, $events);
 352          $event = reset($events);
 353  
 354          // Checking that the event contains the expected values.
 355          $this->assertInstanceOf('\mod_forum\event\discussion_deleted', $event);
 356          $this->assertEquals($context, $event->get_context());
 357          $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid);
 358          $this->assertEventLegacyLogData($expected, $event);
 359          $this->assertEventContextNotUsed($event);
 360  
 361          $this->assertNotEmpty($event->get_name());
 362      }
 363  
 364      /**
 365       * Ensure discussion_moved event validates that fromforumid is set.
 366       *
 367       * @expectedException        coding_exception
 368       * @expectedExceptionMessage The 'fromforumid' value must be set in other.
 369       */
 370      public function test_discussion_moved_fromforumid_validation() {
 371          $course = $this->getDataGenerator()->create_course();
 372          $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 373  
 374          $context = context_module::instance($toforum->cmid);
 375  
 376          $params = array(
 377              'context' => $context,
 378              'other' => array('toforumid' => $toforum->id)
 379          );
 380  
 381          \mod_forum\event\discussion_moved::create($params);
 382      }
 383  
 384      /**
 385       * Ensure discussion_moved event validates that toforumid is set.
 386       *
 387       * @expectedException        coding_exception
 388       * @expectedExceptionMessage The 'toforumid' value must be set in other.
 389       */
 390      public function test_discussion_moved_toforumid_validation() {
 391          $course = $this->getDataGenerator()->create_course();
 392          $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 393          $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 394          $context = context_module::instance($toforum->cmid);
 395  
 396          $params = array(
 397              'context' => $context,
 398              'other' => array('fromforumid' => $fromforum->id)
 399          );
 400  
 401          \mod_forum\event\discussion_moved::create($params);
 402      }
 403  
 404      /**
 405       * Ensure discussion_moved event validates that the context level is correct.
 406       *
 407       * @expectedException        coding_exception
 408       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 409       */
 410      public function test_discussion_moved_context_validation() {
 411          $course = $this->getDataGenerator()->create_course();
 412          $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 413          $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 414          $user = $this->getDataGenerator()->create_user();
 415  
 416          // Add a discussion.
 417          $record = array();
 418          $record['course'] = $course->id;
 419          $record['forum'] = $fromforum->id;
 420          $record['userid'] = $user->id;
 421          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 422  
 423          $params = array(
 424              'context' => context_system::instance(),
 425              'objectid' => $discussion->id,
 426              'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
 427          );
 428  
 429          \mod_forum\event\discussion_moved::create($params);
 430      }
 431  
 432      /**
 433       * Test discussion_moved event.
 434       */
 435      public function test_discussion_moved() {
 436          // Setup test data.
 437          $course = $this->getDataGenerator()->create_course();
 438          $fromforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 439          $toforum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 440          $user = $this->getDataGenerator()->create_user();
 441  
 442          // Add a discussion.
 443          $record = array();
 444          $record['course'] = $course->id;
 445          $record['forum'] = $fromforum->id;
 446          $record['userid'] = $user->id;
 447          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 448  
 449          $context = context_module::instance($toforum->cmid);
 450  
 451          $params = array(
 452              'context' => $context,
 453              'objectid' => $discussion->id,
 454              'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id)
 455          );
 456  
 457          $event = \mod_forum\event\discussion_moved::create($params);
 458  
 459          // Trigger and capture the event.
 460          $sink = $this->redirectEvents();
 461          $event->trigger();
 462          $events = $sink->get_events();
 463          $this->assertCount(1, $events);
 464          $event = reset($events);
 465  
 466          // Checking that the event contains the expected values.
 467          $this->assertInstanceOf('\mod_forum\event\discussion_moved', $event);
 468          $this->assertEquals($context, $event->get_context());
 469          $expected = array($course->id, 'forum', 'move discussion', "discuss.php?d={$discussion->id}",
 470              $discussion->id, $toforum->cmid);
 471          $this->assertEventLegacyLogData($expected, $event);
 472          $this->assertEventContextNotUsed($event);
 473  
 474          $this->assertNotEmpty($event->get_name());
 475      }
 476  
 477  
 478      /**
 479       * Ensure discussion_viewed event validates that the contextlevel is correct.
 480       *
 481       * @expectedException        coding_exception
 482       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 483       */
 484      public function test_discussion_viewed_context_validation() {
 485          $course = $this->getDataGenerator()->create_course();
 486          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 487          $user = $this->getDataGenerator()->create_user();
 488  
 489          // Add a discussion.
 490          $record = array();
 491          $record['course'] = $course->id;
 492          $record['forum'] = $forum->id;
 493          $record['userid'] = $user->id;
 494          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 495  
 496          $params = array(
 497              'context' => context_system::instance(),
 498              'objectid' => $discussion->id,
 499          );
 500  
 501          \mod_forum\event\discussion_viewed::create($params);
 502      }
 503  
 504      /**
 505       * Test discussion_viewed event.
 506       */
 507      public function test_discussion_viewed() {
 508          // Setup test data.
 509          $course = $this->getDataGenerator()->create_course();
 510          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 511          $user = $this->getDataGenerator()->create_user();
 512  
 513          // Add a discussion.
 514          $record = array();
 515          $record['course'] = $course->id;
 516          $record['forum'] = $forum->id;
 517          $record['userid'] = $user->id;
 518          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
 519  
 520          $context = context_module::instance($forum->cmid);
 521  
 522          $params = array(
 523              'context' => $context,
 524              'objectid' => $discussion->id,
 525          );
 526  
 527          $event = \mod_forum\event\discussion_viewed::create($params);
 528  
 529          // Trigger and capture the event.
 530          $sink = $this->redirectEvents();
 531          $event->trigger();
 532          $events = $sink->get_events();
 533          $this->assertCount(1, $events);
 534          $event = reset($events);
 535  
 536          // Checking that the event contains the expected values.
 537          $this->assertInstanceOf('\mod_forum\event\discussion_viewed', $event);
 538          $this->assertEquals($context, $event->get_context());
 539          $expected = array($course->id, 'forum', 'view discussion', "discuss.php?d={$discussion->id}",
 540              $discussion->id, $forum->cmid);
 541          $this->assertEventLegacyLogData($expected, $event);
 542          $this->assertEventContextNotUsed($event);
 543  
 544          $this->assertNotEmpty($event->get_name());
 545      }
 546  
 547      /**
 548       * Ensure course_module_viewed event validates that the contextlevel is correct.
 549       *
 550       * @expectedException        coding_exception
 551       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 552       */
 553      public function test_course_module_viewed_context_validation() {
 554          $course = $this->getDataGenerator()->create_course();
 555          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 556  
 557          $params = array(
 558              'context' => context_system::instance(),
 559              'objectid' => $forum->id,
 560          );
 561  
 562          \mod_forum\event\course_module_viewed::create($params);
 563      }
 564  
 565      /**
 566       * Test the course_module_viewed event.
 567       */
 568      public function test_course_module_viewed() {
 569          // Setup test data.
 570          $course = $this->getDataGenerator()->create_course();
 571          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 572  
 573          $context = context_module::instance($forum->cmid);
 574  
 575          $params = array(
 576              'context' => $context,
 577              'objectid' => $forum->id,
 578          );
 579  
 580          $event = \mod_forum\event\course_module_viewed::create($params);
 581  
 582          // Trigger and capture the event.
 583          $sink = $this->redirectEvents();
 584          $event->trigger();
 585          $events = $sink->get_events();
 586          $this->assertCount(1, $events);
 587          $event = reset($events);
 588  
 589          // Checking that the event contains the expected values.
 590          $this->assertInstanceOf('\mod_forum\event\course_module_viewed', $event);
 591          $this->assertEquals($context, $event->get_context());
 592          $expected = array($course->id, 'forum', 'view forum', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
 593          $this->assertEventLegacyLogData($expected, $event);
 594          $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
 595          $this->assertEquals($url, $event->get_url());
 596          $this->assertEventContextNotUsed($event);
 597  
 598          $this->assertNotEmpty($event->get_name());
 599      }
 600  
 601      /**
 602       * Ensure subscription_created event validates that the forumid is set.
 603       *
 604       * @expectedException        coding_exception
 605       * @expectedExceptionMessage The 'forumid' value must be set in other.
 606       */
 607      public function test_subscription_created_forumid_validation() {
 608          $user = $this->getDataGenerator()->create_user();
 609          $course = $this->getDataGenerator()->create_course();
 610          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 611  
 612          $params = array(
 613              'context' => context_module::instance($forum->cmid),
 614              'relateduserid' => $user->id,
 615          );
 616  
 617          \mod_forum\event\subscription_created::create($params);
 618      }
 619  
 620      /**
 621       * Ensure subscription_created event validates that the relateduserid is set.
 622       *
 623       * @expectedException        coding_exception
 624       * @expectedExceptionMessage The 'relateduserid' must be set.
 625       */
 626      public function test_subscription_created_relateduserid_validation() {
 627          $course = $this->getDataGenerator()->create_course();
 628          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 629  
 630          $params = array(
 631              'context' => context_module::instance($forum->cmid),
 632              'objectid' => $forum->id,
 633          );
 634  
 635          \mod_forum\event\subscription_created::create($params);
 636      }
 637  
 638      /**
 639       * Ensure subscription_created event validates that the contextlevel is correct.
 640       *
 641       * @expectedException        coding_exception
 642       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 643       */
 644      public function test_subscription_created_contextlevel_validation() {
 645          $user = $this->getDataGenerator()->create_user();
 646          $course = $this->getDataGenerator()->create_course();
 647          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 648  
 649          $params = array(
 650              'context' => context_system::instance(),
 651              'other' => array('forumid' => $forum->id),
 652              'relateduserid' => $user->id,
 653          );
 654  
 655          \mod_forum\event\subscription_created::create($params);
 656      }
 657  
 658      /**
 659       * Test the subscription_created event.
 660       */
 661      public function test_subscription_created() {
 662          // Setup test data.
 663          $user = $this->getDataGenerator()->create_user();
 664          $course = $this->getDataGenerator()->create_course();
 665          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 666          $user = $this->getDataGenerator()->create_user();
 667          $context = context_module::instance($forum->cmid);
 668  
 669          // Add a subscription.
 670          $record = array();
 671          $record['course'] = $course->id;
 672          $record['forum'] = $forum->id;
 673          $record['userid'] = $user->id;
 674          $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
 675  
 676          $params = array(
 677              'context' => $context,
 678              'objectid' => $subscription->id,
 679              'other' => array('forumid' => $forum->id),
 680              'relateduserid' => $user->id,
 681          );
 682  
 683          $event = \mod_forum\event\subscription_created::create($params);
 684  
 685          // Trigger and capturing the event.
 686          $sink = $this->redirectEvents();
 687          $event->trigger();
 688          $events = $sink->get_events();
 689          $this->assertCount(1, $events);
 690          $event = reset($events);
 691  
 692          // Checking that the event contains the expected values.
 693          $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
 694          $this->assertEquals($context, $event->get_context());
 695          $expected = array($course->id, 'forum', 'subscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
 696          $this->assertEventLegacyLogData($expected, $event);
 697          $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
 698          $this->assertEquals($url, $event->get_url());
 699          $this->assertEventContextNotUsed($event);
 700  
 701          $this->assertNotEmpty($event->get_name());
 702      }
 703  
 704      /**
 705       * Ensure subscription_deleted event validates that the forumid is set.
 706       *
 707       * @expectedException        coding_exception
 708       * @expectedExceptionMessage The 'forumid' value must be set in other.
 709       */
 710      public function test_subscription_deleted_forumid_validation() {
 711          $user = $this->getDataGenerator()->create_user();
 712          $course = $this->getDataGenerator()->create_course();
 713          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 714  
 715          $params = array(
 716              'context' => context_module::instance($forum->cmid),
 717              'relateduserid' => $user->id,
 718          );
 719  
 720          \mod_forum\event\subscription_deleted::create($params);
 721      }
 722  
 723      /**
 724       * Ensure subscription_deleted event validates that the relateduserid is set.
 725       *
 726       * @expectedException        coding_exception
 727       * @expectedExceptionMessage The 'relateduserid' must be set.
 728       */
 729      public function test_subscription_deleted_relateduserid_validation() {
 730          $course = $this->getDataGenerator()->create_course();
 731          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 732  
 733          $params = array(
 734              'context' => context_module::instance($forum->cmid),
 735              'objectid' => $forum->id,
 736          );
 737  
 738          \mod_forum\event\subscription_deleted::create($params);
 739      }
 740  
 741      /**
 742       * Ensure subscription_deleted event validates that the contextlevel is correct.
 743       *
 744       * @expectedException        coding_exception
 745       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 746       */
 747      public function test_subscription_deleted_contextlevel_validation() {
 748          $user = $this->getDataGenerator()->create_user();
 749          $course = $this->getDataGenerator()->create_course();
 750          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 751  
 752          $params = array(
 753              'context' => context_system::instance(),
 754              'other' => array('forumid' => $forum->id),
 755              'relateduserid' => $user->id,
 756          );
 757  
 758          \mod_forum\event\subscription_deleted::create($params);
 759      }
 760  
 761      /**
 762       * Test the subscription_deleted event.
 763       */
 764      public function test_subscription_deleted() {
 765          // Setup test data.
 766          $user = $this->getDataGenerator()->create_user();
 767          $course = $this->getDataGenerator()->create_course();
 768          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 769          $user = $this->getDataGenerator()->create_user();
 770          $context = context_module::instance($forum->cmid);
 771  
 772          // Add a subscription.
 773          $record = array();
 774          $record['course'] = $course->id;
 775          $record['forum'] = $forum->id;
 776          $record['userid'] = $user->id;
 777          $subscription = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_subscription($record);
 778  
 779          $params = array(
 780              'context' => $context,
 781              'objectid' => $subscription->id,
 782              'other' => array('forumid' => $forum->id),
 783              'relateduserid' => $user->id,
 784          );
 785  
 786          $event = \mod_forum\event\subscription_deleted::create($params);
 787  
 788          // Trigger and capturing the event.
 789          $sink = $this->redirectEvents();
 790          $event->trigger();
 791          $events = $sink->get_events();
 792          $this->assertCount(1, $events);
 793          $event = reset($events);
 794  
 795          // Checking that the event contains the expected values.
 796          $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
 797          $this->assertEquals($context, $event->get_context());
 798          $expected = array($course->id, 'forum', 'unsubscribe', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
 799          $this->assertEventLegacyLogData($expected, $event);
 800          $url = new \moodle_url('/mod/forum/subscribers.php', array('id' => $forum->id));
 801          $this->assertEquals($url, $event->get_url());
 802          $this->assertEventContextNotUsed($event);
 803  
 804          $this->assertNotEmpty($event->get_name());
 805      }
 806  
 807      /**
 808       * Ensure readtracking_enabled event validates that the forumid is set.
 809       *
 810       * @expectedException        coding_exception
 811       * @expectedExceptionMessage The 'forumid' value must be set in other.
 812       */
 813      public function test_readtracking_enabled_forumid_validation() {
 814          $user = $this->getDataGenerator()->create_user();
 815          $course = $this->getDataGenerator()->create_course();
 816          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 817  
 818          $params = array(
 819              'context' => context_module::instance($forum->cmid),
 820              'relateduserid' => $user->id,
 821          );
 822  
 823          \mod_forum\event\readtracking_enabled::create($params);
 824      }
 825  
 826      /**
 827       * Ensure readtracking_enabled event validates that the relateduserid is set.
 828       *
 829       * @expectedException        coding_exception
 830       * @expectedExceptionMessage The 'relateduserid' must be set.
 831       */
 832      public function test_readtracking_enabled_relateduserid_validation() {
 833          $course = $this->getDataGenerator()->create_course();
 834          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 835  
 836          $params = array(
 837              'context' => context_module::instance($forum->cmid),
 838              'objectid' => $forum->id,
 839          );
 840  
 841          \mod_forum\event\readtracking_enabled::create($params);
 842      }
 843  
 844      /**
 845       * Ensure readtracking_enabled event validates that the contextlevel is correct.
 846       *
 847       * @expectedException        coding_exception
 848       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 849       */
 850      public function test_readtracking_enabled_contextlevel_validation() {
 851          $user = $this->getDataGenerator()->create_user();
 852          $course = $this->getDataGenerator()->create_course();
 853          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 854  
 855          $params = array(
 856              'context' => context_system::instance(),
 857              'other' => array('forumid' => $forum->id),
 858              'relateduserid' => $user->id,
 859          );
 860  
 861          \mod_forum\event\readtracking_enabled::create($params);
 862      }
 863  
 864      /**
 865       * Test the readtracking_enabled event.
 866       */
 867      public function test_readtracking_enabled() {
 868          // Setup test data.
 869          $user = $this->getDataGenerator()->create_user();
 870          $course = $this->getDataGenerator()->create_course();
 871          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 872          $context = context_module::instance($forum->cmid);
 873  
 874          $params = array(
 875              'context' => $context,
 876              'other' => array('forumid' => $forum->id),
 877              'relateduserid' => $user->id,
 878          );
 879  
 880          $event = \mod_forum\event\readtracking_enabled::create($params);
 881  
 882          // Trigger and capture the event.
 883          $sink = $this->redirectEvents();
 884          $event->trigger();
 885          $events = $sink->get_events();
 886          $this->assertCount(1, $events);
 887          $event = reset($events);
 888  
 889          // Checking that the event contains the expected values.
 890          $this->assertInstanceOf('\mod_forum\event\readtracking_enabled', $event);
 891          $this->assertEquals($context, $event->get_context());
 892          $expected = array($course->id, 'forum', 'start tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
 893          $this->assertEventLegacyLogData($expected, $event);
 894          $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
 895          $this->assertEquals($url, $event->get_url());
 896          $this->assertEventContextNotUsed($event);
 897  
 898          $this->assertNotEmpty($event->get_name());
 899      }
 900  
 901      /**
 902       *  Ensure readtracking_disabled event validates that the forumid is set.
 903       *
 904       * @expectedException        coding_exception
 905       * @expectedExceptionMessage The 'forumid' value must be set in other.
 906       */
 907      public function test_readtracking_disabled_forumid_validation() {
 908          $user = $this->getDataGenerator()->create_user();
 909          $course = $this->getDataGenerator()->create_course();
 910          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 911  
 912          $params = array(
 913              'context' => context_module::instance($forum->cmid),
 914              'relateduserid' => $user->id,
 915          );
 916  
 917          \mod_forum\event\readtracking_disabled::create($params);
 918      }
 919  
 920      /**
 921       *  Ensure readtracking_disabled event validates that the relateduserid is set.
 922       *
 923       * @expectedException        coding_exception
 924       * @expectedExceptionMessage The 'relateduserid' must be set.
 925       */
 926      public function test_readtracking_disabled_relateduserid_validation() {
 927          $course = $this->getDataGenerator()->create_course();
 928          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 929  
 930          $params = array(
 931              'context' => context_module::instance($forum->cmid),
 932              'objectid' => $forum->id,
 933          );
 934  
 935          \mod_forum\event\readtracking_disabled::create($params);
 936      }
 937  
 938      /**
 939       *  Ensure readtracking_disabled event validates that the contextlevel is correct
 940       *
 941       * @expectedException        coding_exception
 942       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
 943       */
 944      public function test_readtracking_disabled_contextlevel_validation() {
 945          $user = $this->getDataGenerator()->create_user();
 946          $course = $this->getDataGenerator()->create_course();
 947          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 948  
 949          $params = array(
 950              'context' => context_system::instance(),
 951              'other' => array('forumid' => $forum->id),
 952              'relateduserid' => $user->id,
 953          );
 954  
 955          \mod_forum\event\readtracking_disabled::create($params);
 956      }
 957  
 958      /**
 959       *  Test the readtracking_disabled event.
 960       */
 961      public function test_readtracking_disabled() {
 962          // Setup test data.
 963          $user = $this->getDataGenerator()->create_user();
 964          $course = $this->getDataGenerator()->create_course();
 965          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
 966          $context = context_module::instance($forum->cmid);
 967  
 968          $params = array(
 969              'context' => $context,
 970              'other' => array('forumid' => $forum->id),
 971              'relateduserid' => $user->id,
 972          );
 973  
 974          $event = \mod_forum\event\readtracking_disabled::create($params);
 975  
 976          // Trigger and capture the event.
 977          $sink = $this->redirectEvents();
 978          $event->trigger();
 979          $events = $sink->get_events();
 980          $this->assertCount(1, $events);
 981          $event = reset($events);
 982  
 983          // Checking that the event contains the expected values.
 984          $this->assertInstanceOf('\mod_forum\event\readtracking_disabled', $event);
 985          $this->assertEquals($context, $event->get_context());
 986          $expected = array($course->id, 'forum', 'stop tracking', "view.php?f={$forum->id}", $forum->id, $forum->cmid);
 987          $this->assertEventLegacyLogData($expected, $event);
 988          $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
 989          $this->assertEquals($url, $event->get_url());
 990          $this->assertEventContextNotUsed($event);
 991  
 992          $this->assertNotEmpty($event->get_name());
 993      }
 994  
 995      /**
 996       *  Ensure subscribers_viewed event validates that the forumid is set.
 997       *
 998       * @expectedException        coding_exception
 999       * @expectedExceptionMessage The 'forumid' value must be set in other.
1000       */
1001      public function test_subscribers_viewed_forumid_validation() {
1002          $user = $this->getDataGenerator()->create_user();
1003          $course = $this->getDataGenerator()->create_course();
1004          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1005  
1006          $params = array(
1007              'context' => context_module::instance($forum->cmid),
1008              'relateduserid' => $user->id,
1009          );
1010  
1011          \mod_forum\event\subscribers_viewed::create($params);
1012      }
1013  
1014      /**
1015       *  Ensure subscribers_viewed event validates that the contextlevel is correct.
1016       *
1017       * @expectedException        coding_exception
1018       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
1019       */
1020      public function test_subscribers_viewed_contextlevel_validation() {
1021          $user = $this->getDataGenerator()->create_user();
1022          $course = $this->getDataGenerator()->create_course();
1023          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1024  
1025          $params = array(
1026              'context' => context_system::instance(),
1027              'other' => array('forumid' => $forum->id),
1028              'relateduserid' => $user->id,
1029          );
1030  
1031          \mod_forum\event\subscribers_viewed::create($params);
1032      }
1033  
1034      /**
1035       *  Test the subscribers_viewed event.
1036       */
1037      public function test_subscribers_viewed() {
1038          // Setup test data.
1039          $course = $this->getDataGenerator()->create_course();
1040          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1041          $context = context_module::instance($forum->cmid);
1042  
1043          $params = array(
1044              'context' => $context,
1045              'other' => array('forumid' => $forum->id),
1046          );
1047  
1048          $event = \mod_forum\event\subscribers_viewed::create($params);
1049  
1050          // Trigger and capture the event.
1051          $sink = $this->redirectEvents();
1052          $event->trigger();
1053          $events = $sink->get_events();
1054          $this->assertCount(1, $events);
1055          $event = reset($events);
1056  
1057          // Checking that the event contains the expected values.
1058          $this->assertInstanceOf('\mod_forum\event\subscribers_viewed', $event);
1059          $this->assertEquals($context, $event->get_context());
1060          $expected = array($course->id, 'forum', 'view subscribers', "subscribers.php?id={$forum->id}", $forum->id, $forum->cmid);
1061          $this->assertEventLegacyLogData($expected, $event);
1062          $this->assertEventContextNotUsed($event);
1063  
1064          $this->assertNotEmpty($event->get_name());
1065      }
1066  
1067      /**
1068       * Ensure user_report_viewed event validates that the reportmode is set.
1069       *
1070       * @expectedException        coding_exception
1071       * @expectedExceptionMessage The 'reportmode' value must be set in other.
1072       */
1073      public function test_user_report_viewed_reportmode_validation() {
1074          $user = $this->getDataGenerator()->create_user();
1075          $course = $this->getDataGenerator()->create_course();
1076  
1077          $params = array(
1078              'context' => context_course::instance($course->id),
1079              'relateduserid' => $user->id,
1080          );
1081  
1082          \mod_forum\event\user_report_viewed::create($params);
1083      }
1084  
1085      /**
1086       * Ensure user_report_viewed event validates that the contextlevel is correct.
1087       *
1088       * @expectedException        coding_exception
1089       * @expectedExceptionMessage Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER.
1090       */
1091      public function test_user_report_viewed_contextlevel_validation() {
1092          $user = $this->getDataGenerator()->create_user();
1093          $course = $this->getDataGenerator()->create_course();
1094          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1095  
1096          $params = array(
1097              'context' => context_module::instance($forum->cmid),
1098              'other' => array('reportmode' => 'posts'),
1099              'relateduserid' => $user->id,
1100          );
1101  
1102          \mod_forum\event\user_report_viewed::create($params);
1103      }
1104  
1105      /**
1106       *  Ensure user_report_viewed event validates that the relateduserid is set.
1107       *
1108       * @expectedException        coding_exception
1109       * @expectedExceptionMessage The 'relateduserid' must be set.
1110       */
1111      public function test_user_report_viewed_relateduserid_validation() {
1112  
1113          $params = array(
1114              'context' => context_system::instance(),
1115              'other' => array('reportmode' => 'posts'),
1116          );
1117  
1118          \mod_forum\event\user_report_viewed::create($params);
1119      }
1120  
1121      /**
1122       * Test the user_report_viewed event.
1123       */
1124      public function test_user_report_viewed() {
1125          // Setup test data.
1126          $user = $this->getDataGenerator()->create_user();
1127          $course = $this->getDataGenerator()->create_course();
1128          $context = context_course::instance($course->id);
1129  
1130          $params = array(
1131              'context' => $context,
1132              'relateduserid' => $user->id,
1133              'other' => array('reportmode' => 'discussions'),
1134          );
1135  
1136          $event = \mod_forum\event\user_report_viewed::create($params);
1137  
1138          // Trigger and capture the event.
1139          $sink = $this->redirectEvents();
1140          $event->trigger();
1141          $events = $sink->get_events();
1142          $this->assertCount(1, $events);
1143          $event = reset($events);
1144  
1145          // Checking that the event contains the expected values.
1146          $this->assertInstanceOf('\mod_forum\event\user_report_viewed', $event);
1147          $this->assertEquals($context, $event->get_context());
1148          $expected = array($course->id, 'forum', 'user report',
1149              "user.php?id={$user->id}&amp;mode=discussions&amp;course={$course->id}", $user->id);
1150          $this->assertEventLegacyLogData($expected, $event);
1151          $this->assertEventContextNotUsed($event);
1152  
1153          $this->assertNotEmpty($event->get_name());
1154      }
1155  
1156      /**
1157       *  Ensure post_created event validates that the postid is set.
1158       */
1159      public function test_post_created_postid_validation() {
1160          $course = $this->getDataGenerator()->create_course();
1161          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1162          $user = $this->getDataGenerator()->create_user();
1163  
1164          // Add a discussion.
1165          $record = array();
1166          $record['course'] = $course->id;
1167          $record['forum'] = $forum->id;
1168          $record['userid'] = $user->id;
1169          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1170  
1171          $params = array(
1172              'context' => context_module::instance($forum->cmid),
1173              'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1174          );
1175  
1176          \mod_forum\event\post_created::create($params);
1177      }
1178  
1179      /**
1180       * Ensure post_created event validates that the discussionid is set.
1181       *
1182       * @expectedException        coding_exception
1183       * @expectedExceptionMessage The 'discussionid' value must be set in other.
1184       */
1185      public function test_post_created_discussionid_validation() {
1186          $course = $this->getDataGenerator()->create_course();
1187          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1188          $user = $this->getDataGenerator()->create_user();
1189  
1190          // Add a discussion.
1191          $record = array();
1192          $record['course'] = $course->id;
1193          $record['forum'] = $forum->id;
1194          $record['userid'] = $user->id;
1195          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1196  
1197          // Add a post.
1198          $record = array();
1199          $record['discussion'] = $discussion->id;
1200          $record['userid'] = $user->id;
1201          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1202  
1203          $params = array(
1204              'context' => context_module::instance($forum->cmid),
1205              'objectid' => $post->id,
1206              'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1207          );
1208  
1209          \mod_forum\event\post_created::create($params);
1210      }
1211  
1212      /**
1213       *  Ensure post_created event validates that the forumid is set.
1214       *
1215       * @expectedException        coding_exception
1216       * @expectedExceptionMessage The 'forumid' value must be set in other.
1217       */
1218      public function test_post_created_forumid_validation() {
1219          $course = $this->getDataGenerator()->create_course();
1220          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1221          $user = $this->getDataGenerator()->create_user();
1222  
1223          // Add a discussion.
1224          $record = array();
1225          $record['course'] = $course->id;
1226          $record['forum'] = $forum->id;
1227          $record['userid'] = $user->id;
1228          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1229  
1230          // Add a post.
1231          $record = array();
1232          $record['discussion'] = $discussion->id;
1233          $record['userid'] = $user->id;
1234          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1235  
1236          $params = array(
1237              'context' => context_module::instance($forum->cmid),
1238              'objectid' => $post->id,
1239              'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1240          );
1241  
1242          \mod_forum\event\post_created::create($params);
1243      }
1244  
1245      /**
1246       * Ensure post_created event validates that the forumtype is set.
1247       *
1248       * @expectedException        coding_exception
1249       * @expectedExceptionMessage The 'forumtype' value must be set in other.
1250       */
1251      public function test_post_created_forumtype_validation() {
1252          $course = $this->getDataGenerator()->create_course();
1253          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1254          $user = $this->getDataGenerator()->create_user();
1255  
1256          // Add a discussion.
1257          $record = array();
1258          $record['course'] = $course->id;
1259          $record['forum'] = $forum->id;
1260          $record['userid'] = $user->id;
1261          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1262  
1263          // Add a post.
1264          $record = array();
1265          $record['discussion'] = $discussion->id;
1266          $record['userid'] = $user->id;
1267          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1268  
1269          $params = array(
1270              'context' => context_module::instance($forum->cmid),
1271              'objectid' => $post->id,
1272              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1273          );
1274  
1275          \mod_forum\event\post_created::create($params);
1276      }
1277  
1278      /**
1279       *  Ensure post_created event validates that the contextlevel is correct.
1280       *
1281       * @expectedException        coding_exception
1282       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
1283       */
1284      public function test_post_created_context_validation() {
1285          $course = $this->getDataGenerator()->create_course();
1286          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1287          $user = $this->getDataGenerator()->create_user();
1288  
1289          // Add a discussion.
1290          $record = array();
1291          $record['course'] = $course->id;
1292          $record['forum'] = $forum->id;
1293          $record['userid'] = $user->id;
1294          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1295  
1296          // Add a post.
1297          $record = array();
1298          $record['discussion'] = $discussion->id;
1299          $record['userid'] = $user->id;
1300          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1301  
1302          $params = array(
1303              'context' => context_system::instance(),
1304              'objectid' => $post->id,
1305              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1306          );
1307  
1308          \mod_forum\event\post_created::create($params);
1309      }
1310  
1311      /**
1312       * Test the post_created event.
1313       */
1314      public function test_post_created() {
1315          // Setup test data.
1316          $course = $this->getDataGenerator()->create_course();
1317          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1318          $user = $this->getDataGenerator()->create_user();
1319  
1320          // Add a discussion.
1321          $record = array();
1322          $record['course'] = $course->id;
1323          $record['forum'] = $forum->id;
1324          $record['userid'] = $user->id;
1325          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1326  
1327          // Add a post.
1328          $record = array();
1329          $record['discussion'] = $discussion->id;
1330          $record['userid'] = $user->id;
1331          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1332  
1333          $context = context_module::instance($forum->cmid);
1334  
1335          $params = array(
1336              'context' => $context,
1337              'objectid' => $post->id,
1338              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1339          );
1340  
1341          $event = \mod_forum\event\post_created::create($params);
1342  
1343          // Trigger and capturing the event.
1344          $sink = $this->redirectEvents();
1345          $event->trigger();
1346          $events = $sink->get_events();
1347          $this->assertCount(1, $events);
1348          $event = reset($events);
1349  
1350          // Checking that the event contains the expected values.
1351          $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1352          $this->assertEquals($context, $event->get_context());
1353          $expected = array($course->id, 'forum', 'add post', "discuss.php?d={$discussion->id}#p{$post->id}",
1354              $forum->id, $forum->cmid);
1355          $this->assertEventLegacyLogData($expected, $event);
1356          $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1357          $url->set_anchor('p'.$event->objectid);
1358          $this->assertEquals($url, $event->get_url());
1359          $this->assertEventContextNotUsed($event);
1360  
1361          $this->assertNotEmpty($event->get_name());
1362      }
1363  
1364      /**
1365       * Test the post_created event for a single discussion forum.
1366       */
1367      public function test_post_created_single() {
1368          // Setup test data.
1369          $course = $this->getDataGenerator()->create_course();
1370          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1371          $user = $this->getDataGenerator()->create_user();
1372  
1373          // Add a discussion.
1374          $record = array();
1375          $record['course'] = $course->id;
1376          $record['forum'] = $forum->id;
1377          $record['userid'] = $user->id;
1378          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1379  
1380          // Add a post.
1381          $record = array();
1382          $record['discussion'] = $discussion->id;
1383          $record['userid'] = $user->id;
1384          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1385  
1386          $context = context_module::instance($forum->cmid);
1387  
1388          $params = array(
1389              'context' => $context,
1390              'objectid' => $post->id,
1391              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1392          );
1393  
1394          $event = \mod_forum\event\post_created::create($params);
1395  
1396          // Trigger and capturing the event.
1397          $sink = $this->redirectEvents();
1398          $event->trigger();
1399          $events = $sink->get_events();
1400          $this->assertCount(1, $events);
1401          $event = reset($events);
1402  
1403          // Checking that the event contains the expected values.
1404          $this->assertInstanceOf('\mod_forum\event\post_created', $event);
1405          $this->assertEquals($context, $event->get_context());
1406          $expected = array($course->id, 'forum', 'add post', "view.php?f={$forum->id}#p{$post->id}",
1407              $forum->id, $forum->cmid);
1408          $this->assertEventLegacyLogData($expected, $event);
1409          $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1410          $url->set_anchor('p'.$event->objectid);
1411          $this->assertEquals($url, $event->get_url());
1412          $this->assertEventContextNotUsed($event);
1413  
1414          $this->assertNotEmpty($event->get_name());
1415      }
1416  
1417      /**
1418       *  Ensure post_deleted event validates that the postid is set.
1419       */
1420      public function test_post_deleted_postid_validation() {
1421          $course = $this->getDataGenerator()->create_course();
1422          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1423          $user = $this->getDataGenerator()->create_user();
1424  
1425          // Add a discussion.
1426          $record = array();
1427          $record['course'] = $course->id;
1428          $record['forum'] = $forum->id;
1429          $record['userid'] = $user->id;
1430          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1431  
1432          $params = array(
1433              'context' => context_module::instance($forum->cmid),
1434              'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type, 'discussionid' => $discussion->id)
1435          );
1436  
1437          \mod_forum\event\post_deleted::create($params);
1438      }
1439  
1440      /**
1441       * Ensure post_deleted event validates that the discussionid is set.
1442       *
1443       * @expectedException        coding_exception
1444       * @expectedExceptionMessage The 'discussionid' value must be set in other.
1445       */
1446      public function test_post_deleted_discussionid_validation() {
1447          $course = $this->getDataGenerator()->create_course();
1448          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1449          $user = $this->getDataGenerator()->create_user();
1450  
1451          // Add a discussion.
1452          $record = array();
1453          $record['course'] = $course->id;
1454          $record['forum'] = $forum->id;
1455          $record['userid'] = $user->id;
1456          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1457  
1458          // Add a post.
1459          $record = array();
1460          $record['discussion'] = $discussion->id;
1461          $record['userid'] = $user->id;
1462          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1463  
1464          $params = array(
1465              'context' => context_module::instance($forum->cmid),
1466              'objectid' => $post->id,
1467              'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1468          );
1469  
1470          \mod_forum\event\post_deleted::create($params);
1471      }
1472  
1473      /**
1474       *  Ensure post_deleted event validates that the forumid is set.
1475       *
1476       * @expectedException        coding_exception
1477       * @expectedExceptionMessage The 'forumid' value must be set in other.
1478       */
1479      public function test_post_deleted_forumid_validation() {
1480          $course = $this->getDataGenerator()->create_course();
1481          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1482          $user = $this->getDataGenerator()->create_user();
1483  
1484          // Add a discussion.
1485          $record = array();
1486          $record['course'] = $course->id;
1487          $record['forum'] = $forum->id;
1488          $record['userid'] = $user->id;
1489          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1490  
1491          // Add a post.
1492          $record = array();
1493          $record['discussion'] = $discussion->id;
1494          $record['userid'] = $user->id;
1495          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1496  
1497          $params = array(
1498              'context' => context_module::instance($forum->cmid),
1499              'objectid' => $post->id,
1500              'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1501          );
1502  
1503          \mod_forum\event\post_deleted::create($params);
1504      }
1505  
1506      /**
1507       * Ensure post_deleted event validates that the forumtype is set.
1508       *
1509       * @expectedException        coding_exception
1510       * @expectedExceptionMessage The 'forumtype' value must be set in other.
1511       */
1512      public function test_post_deleted_forumtype_validation() {
1513          $course = $this->getDataGenerator()->create_course();
1514          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1515          $user = $this->getDataGenerator()->create_user();
1516  
1517          // Add a discussion.
1518          $record = array();
1519          $record['course'] = $course->id;
1520          $record['forum'] = $forum->id;
1521          $record['userid'] = $user->id;
1522          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1523  
1524          // Add a post.
1525          $record = array();
1526          $record['discussion'] = $discussion->id;
1527          $record['userid'] = $user->id;
1528          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1529  
1530          $params = array(
1531              'context' => context_module::instance($forum->cmid),
1532              'objectid' => $post->id,
1533              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1534          );
1535  
1536          \mod_forum\event\post_deleted::create($params);
1537      }
1538  
1539      /**
1540       *  Ensure post_deleted event validates that the contextlevel is correct.
1541       *
1542       * @expectedException        coding_exception
1543       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
1544       */
1545      public function test_post_deleted_context_validation() {
1546          $course = $this->getDataGenerator()->create_course();
1547          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1548          $user = $this->getDataGenerator()->create_user();
1549  
1550          // Add a discussion.
1551          $record = array();
1552          $record['course'] = $course->id;
1553          $record['forum'] = $forum->id;
1554          $record['userid'] = $user->id;
1555          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1556  
1557          // Add a post.
1558          $record = array();
1559          $record['discussion'] = $discussion->id;
1560          $record['userid'] = $user->id;
1561          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1562  
1563          $params = array(
1564              'context' => context_system::instance(),
1565              'objectid' => $post->id,
1566              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1567          );
1568  
1569          \mod_forum\event\post_deleted::create($params);
1570      }
1571  
1572      /**
1573       * Test post_deleted event.
1574       */
1575      public function test_post_deleted() {
1576          global $DB;
1577  
1578          // Setup test data.
1579          $course = $this->getDataGenerator()->create_course();
1580          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1581          $user = $this->getDataGenerator()->create_user();
1582          $cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course);
1583  
1584          // Add a discussion.
1585          $record = array();
1586          $record['course'] = $course->id;
1587          $record['forum'] = $forum->id;
1588          $record['userid'] = $user->id;
1589          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1590  
1591          // When creating a discussion we also create a post, so get the post.
1592          $discussionpost = $DB->get_records('forum_posts');
1593          // Will only be one here.
1594          $discussionpost = reset($discussionpost);
1595  
1596          // Add a few posts.
1597          $record = array();
1598          $record['discussion'] = $discussion->id;
1599          $record['userid'] = $user->id;
1600          $posts = array();
1601          $posts[$discussionpost->id] = $discussionpost;
1602          for ($i = 0; $i < 3; $i++) {
1603              $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1604              $posts[$post->id] = $post;
1605          }
1606  
1607          // Delete the last post and capture the event.
1608          $lastpost = end($posts);
1609          $sink = $this->redirectEvents();
1610          forum_delete_post($lastpost, true, $course, $cm, $forum);
1611          $events = $sink->get_events();
1612          $this->assertCount(1, $events);
1613          $event = reset($events);
1614  
1615          // Check that the events contain the expected values.
1616          $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1617          $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
1618          $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $lastpost->id, $forum->cmid);
1619          $this->assertEventLegacyLogData($expected, $event);
1620          $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1621          $this->assertEquals($url, $event->get_url());
1622          $this->assertEventContextNotUsed($event);
1623          $this->assertNotEmpty($event->get_name());
1624  
1625          // Delete the whole discussion and capture the events.
1626          $sink = $this->redirectEvents();
1627          forum_delete_discussion($discussion, true, $course, $cm, $forum);
1628          $events = $sink->get_events();
1629          // We will have 4 events. One for the discussion, another one for the discussion topic post, and two for the posts.
1630          $this->assertCount(4, $events);
1631  
1632          // Loop through the events and check they are valid.
1633          foreach ($events as $event) {
1634              if ($event instanceof \mod_forum\event\discussion_deleted) {
1635                  // Check that the event contains the expected values.
1636                  $this->assertEquals($event->objectid, $discussion->id);
1637                  $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
1638                  $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}",
1639                      $forum->id, $forum->cmid);
1640                  $this->assertEventLegacyLogData($expected, $event);
1641                  $url = new \moodle_url('/mod/forum/view.php', array('id' => $forum->cmid));
1642                  $this->assertEquals($url, $event->get_url());
1643                  $this->assertEventContextNotUsed($event);
1644                  $this->assertNotEmpty($event->get_name());
1645              } else {
1646                  $post = $posts[$event->objectid];
1647                  // Check that the event contains the expected values.
1648                  $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1649                  $this->assertEquals($event->objectid, $post->id);
1650                  $this->assertEquals(context_module::instance($forum->cmid), $event->get_context());
1651                  $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid);
1652                  $this->assertEventLegacyLogData($expected, $event);
1653                  $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1654                  $this->assertEquals($url, $event->get_url());
1655                  $this->assertEventContextNotUsed($event);
1656                  $this->assertNotEmpty($event->get_name());
1657              }
1658          }
1659      }
1660  
1661      /**
1662       * Test post_deleted event for a single discussion forum.
1663       */
1664      public function test_post_deleted_single() {
1665          // Setup test data.
1666          $course = $this->getDataGenerator()->create_course();
1667          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1668          $user = $this->getDataGenerator()->create_user();
1669  
1670          // Add a discussion.
1671          $record = array();
1672          $record['course'] = $course->id;
1673          $record['forum'] = $forum->id;
1674          $record['userid'] = $user->id;
1675          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1676  
1677          // Add a post.
1678          $record = array();
1679          $record['discussion'] = $discussion->id;
1680          $record['userid'] = $user->id;
1681          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1682  
1683          $context = context_module::instance($forum->cmid);
1684  
1685          $params = array(
1686              'context' => $context,
1687              'objectid' => $post->id,
1688              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1689          );
1690  
1691          $event = \mod_forum\event\post_deleted::create($params);
1692  
1693          // Trigger and capture the event.
1694          $sink = $this->redirectEvents();
1695          $event->trigger();
1696          $events = $sink->get_events();
1697          $this->assertCount(1, $events);
1698          $event = reset($events);
1699  
1700          // Checking that the event contains the expected values.
1701          $this->assertInstanceOf('\mod_forum\event\post_deleted', $event);
1702          $this->assertEquals($context, $event->get_context());
1703          $expected = array($course->id, 'forum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid);
1704          $this->assertEventLegacyLogData($expected, $event);
1705          $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1706          $this->assertEquals($url, $event->get_url());
1707          $this->assertEventContextNotUsed($event);
1708  
1709          $this->assertNotEmpty($event->get_name());
1710      }
1711  
1712      /**
1713       * Ensure post_updated event validates that the discussionid is set.
1714       *
1715       * @expectedException        coding_exception
1716       * @expectedExceptionMessage The 'discussionid' value must be set in other.
1717       */
1718      public function test_post_updated_discussionid_validation() {
1719          $course = $this->getDataGenerator()->create_course();
1720          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1721          $user = $this->getDataGenerator()->create_user();
1722  
1723          // Add a discussion.
1724          $record = array();
1725          $record['course'] = $course->id;
1726          $record['forum'] = $forum->id;
1727          $record['userid'] = $user->id;
1728          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1729  
1730          // Add a post.
1731          $record = array();
1732          $record['discussion'] = $discussion->id;
1733          $record['userid'] = $user->id;
1734          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1735  
1736          $params = array(
1737              'context' => context_module::instance($forum->cmid),
1738              'objectid' => $post->id,
1739              'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type)
1740          );
1741  
1742          \mod_forum\event\post_updated::create($params);
1743      }
1744  
1745      /**
1746       * Ensure post_updated event validates that the forumid is set.
1747       *
1748       * @expectedException        coding_exception
1749       * @expectedExceptionMessage The 'forumid' value must be set in other.
1750       */
1751      public function test_post_updated_forumid_validation() {
1752          $course = $this->getDataGenerator()->create_course();
1753          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1754          $user = $this->getDataGenerator()->create_user();
1755  
1756          // Add a discussion.
1757          $record = array();
1758          $record['course'] = $course->id;
1759          $record['forum'] = $forum->id;
1760          $record['userid'] = $user->id;
1761          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1762  
1763          // Add a post.
1764          $record = array();
1765          $record['discussion'] = $discussion->id;
1766          $record['userid'] = $user->id;
1767          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1768  
1769          $params = array(
1770              'context' => context_module::instance($forum->cmid),
1771              'objectid' => $post->id,
1772              'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type)
1773          );
1774  
1775          \mod_forum\event\post_updated::create($params);
1776      }
1777  
1778      /**
1779       * Ensure post_updated event validates that the forumtype is set.
1780       *
1781       * @expectedException        coding_exception
1782       * @expectedExceptionMessage The 'forumtype' value must be set in other.
1783       */
1784      public function test_post_updated_forumtype_validation() {
1785          $course = $this->getDataGenerator()->create_course();
1786          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1787          $user = $this->getDataGenerator()->create_user();
1788  
1789          // Add a discussion.
1790          $record = array();
1791          $record['course'] = $course->id;
1792          $record['forum'] = $forum->id;
1793          $record['userid'] = $user->id;
1794          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1795  
1796          // Add a post.
1797          $record = array();
1798          $record['discussion'] = $discussion->id;
1799          $record['userid'] = $user->id;
1800          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1801  
1802          $params = array(
1803              'context' => context_module::instance($forum->cmid),
1804              'objectid' => $post->id,
1805              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id)
1806          );
1807  
1808          \mod_forum\event\post_updated::create($params);
1809      }
1810  
1811      /**
1812       *  Ensure post_updated event validates that the contextlevel is correct.
1813       *
1814       * @expectedException        coding_exception
1815       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
1816       */
1817      public function test_post_updated_context_validation() {
1818          $course = $this->getDataGenerator()->create_course();
1819          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1820          $user = $this->getDataGenerator()->create_user();
1821  
1822          // Add a discussion.
1823          $record = array();
1824          $record['course'] = $course->id;
1825          $record['forum'] = $forum->id;
1826          $record['userid'] = $user->id;
1827          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1828  
1829          // Add a post.
1830          $record = array();
1831          $record['discussion'] = $discussion->id;
1832          $record['userid'] = $user->id;
1833          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1834  
1835          $params = array(
1836              'context' => context_system::instance(),
1837              'objectid' => $post->id,
1838              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1839          );
1840  
1841          \mod_forum\event\post_updated::create($params);
1842      }
1843  
1844      /**
1845       * Test post_updated event.
1846       */
1847      public function test_post_updated() {
1848          // Setup test data.
1849          $course = $this->getDataGenerator()->create_course();
1850          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id));
1851          $user = $this->getDataGenerator()->create_user();
1852  
1853          // Add a discussion.
1854          $record = array();
1855          $record['course'] = $course->id;
1856          $record['forum'] = $forum->id;
1857          $record['userid'] = $user->id;
1858          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1859  
1860          // Add a post.
1861          $record = array();
1862          $record['discussion'] = $discussion->id;
1863          $record['userid'] = $user->id;
1864          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1865  
1866          $context = context_module::instance($forum->cmid);
1867  
1868          $params = array(
1869              'context' => $context,
1870              'objectid' => $post->id,
1871              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1872          );
1873  
1874          $event = \mod_forum\event\post_updated::create($params);
1875  
1876          // Trigger and capturing the event.
1877          $sink = $this->redirectEvents();
1878          $event->trigger();
1879          $events = $sink->get_events();
1880          $this->assertCount(1, $events);
1881          $event = reset($events);
1882  
1883          // Checking that the event contains the expected values.
1884          $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1885          $this->assertEquals($context, $event->get_context());
1886          $expected = array($course->id, 'forum', 'update post', "discuss.php?d={$discussion->id}#p{$post->id}",
1887              $post->id, $forum->cmid);
1888          $this->assertEventLegacyLogData($expected, $event);
1889          $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id));
1890          $url->set_anchor('p'.$event->objectid);
1891          $this->assertEquals($url, $event->get_url());
1892          $this->assertEventContextNotUsed($event);
1893  
1894          $this->assertNotEmpty($event->get_name());
1895      }
1896  
1897      /**
1898       * Test post_updated event.
1899       */
1900      public function test_post_updated_single() {
1901          // Setup test data.
1902          $course = $this->getDataGenerator()->create_course();
1903          $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id, 'type' => 'single'));
1904          $user = $this->getDataGenerator()->create_user();
1905  
1906          // Add a discussion.
1907          $record = array();
1908          $record['course'] = $course->id;
1909          $record['forum'] = $forum->id;
1910          $record['userid'] = $user->id;
1911          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1912  
1913          // Add a post.
1914          $record = array();
1915          $record['discussion'] = $discussion->id;
1916          $record['userid'] = $user->id;
1917          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1918  
1919          $context = context_module::instance($forum->cmid);
1920  
1921          $params = array(
1922              'context' => $context,
1923              'objectid' => $post->id,
1924              'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)
1925          );
1926  
1927          $event = \mod_forum\event\post_updated::create($params);
1928  
1929          // Trigger and capturing the event.
1930          $sink = $this->redirectEvents();
1931          $event->trigger();
1932          $events = $sink->get_events();
1933          $this->assertCount(1, $events);
1934          $event = reset($events);
1935  
1936          // Checking that the event contains the expected values.
1937          $this->assertInstanceOf('\mod_forum\event\post_updated', $event);
1938          $this->assertEquals($context, $event->get_context());
1939          $expected = array($course->id, 'forum', 'update post', "view.php?f={$forum->id}#p{$post->id}",
1940              $post->id, $forum->cmid);
1941          $this->assertEventLegacyLogData($expected, $event);
1942          $url = new \moodle_url('/mod/forum/view.php', array('f' => $forum->id));
1943          $url->set_anchor('p'.$post->id);
1944          $this->assertEquals($url, $event->get_url());
1945          $this->assertEventContextNotUsed($event);
1946  
1947          $this->assertNotEmpty($event->get_name());
1948      }
1949  
1950      /**
1951       * Test discussion_subscription_created event.
1952       */
1953      public function test_discussion_subscription_created() {
1954          global $CFG;
1955          require_once($CFG->dirroot . '/mod/forum/lib.php');
1956  
1957          // Setup test data.
1958          $course = $this->getDataGenerator()->create_course();
1959          $user = $this->getDataGenerator()->create_user();
1960          $this->getDataGenerator()->enrol_user($user->id, $course->id);
1961  
1962          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
1963          $forum = $this->getDataGenerator()->create_module('forum', $options);
1964  
1965          // Add a discussion.
1966          $record = array();
1967          $record['course'] = $course->id;
1968          $record['forum'] = $forum->id;
1969          $record['userid'] = $user->id;
1970          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
1971  
1972          // Add a post.
1973          $record = array();
1974          $record['discussion'] = $discussion->id;
1975          $record['userid'] = $user->id;
1976          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
1977  
1978          // Trigger and capturing the event.
1979          $sink = $this->redirectEvents();
1980  
1981          // Trigger the event by subscribing the user to the forum discussion.
1982          \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
1983  
1984          $events = $sink->get_events();
1985          $this->assertCount(1, $events);
1986          $event = reset($events);
1987  
1988          // Checking that the event contains the expected values.
1989          $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
1990  
1991  
1992          $cm = get_coursemodule_from_instance('forum', $discussion->forum);
1993          $context = \context_module::instance($cm->id);
1994          $this->assertEquals($context, $event->get_context());
1995  
1996          $url = new \moodle_url('/mod/forum/subscribe.php', array(
1997              'id' => $forum->id,
1998              'd' => $discussion->id
1999          ));
2000  
2001          $this->assertEquals($url, $event->get_url());
2002          $this->assertEventContextNotUsed($event);
2003          $this->assertNotEmpty($event->get_name());
2004      }
2005  
2006      /**
2007       * Test validation of discussion_subscription_created event.
2008       */
2009      public function test_discussion_subscription_created_validation() {
2010          global $CFG, $DB;
2011          require_once($CFG->dirroot . '/mod/forum/lib.php');
2012  
2013          // Setup test data.
2014          $course = $this->getDataGenerator()->create_course();
2015          $user = $this->getDataGenerator()->create_user();
2016          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2017  
2018          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2019          $forum = $this->getDataGenerator()->create_module('forum', $options);
2020  
2021          // Add a discussion.
2022          $record = array();
2023          $record['course'] = $course->id;
2024          $record['forum'] = $forum->id;
2025          $record['userid'] = $user->id;
2026          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2027  
2028          // Add a post.
2029          $record = array();
2030          $record['discussion'] = $discussion->id;
2031          $record['userid'] = $user->id;
2032          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2033  
2034          // The user is not subscribed to the forum. Insert a new discussion subscription.
2035          $subscription = new \stdClass();
2036          $subscription->userid  = $user->id;
2037          $subscription->forum = $forum->id;
2038          $subscription->discussion = $discussion->id;
2039          $subscription->preference = time();
2040  
2041          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2042  
2043          $context = context_module::instance($forum->cmid);
2044  
2045          $params = array(
2046              'context' => $context,
2047              'objectid' => $subscription->id,
2048              'relateduserid' => $user->id,
2049              'other' => array(
2050                  'forumid' => $forum->id,
2051                  'discussion' => $discussion->id,
2052              )
2053          );
2054  
2055          $event = \mod_forum\event\discussion_subscription_created::create($params);
2056  
2057          // Trigger and capturing the event.
2058          $sink = $this->redirectEvents();
2059          $event->trigger();
2060          $events = $sink->get_events();
2061          $this->assertCount(1, $events);
2062          $event = reset($events);
2063      }
2064  
2065      /**
2066       * Test contextlevel validation of discussion_subscription_created event.
2067       *
2068       * @expectedException        coding_exception
2069       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
2070       */
2071      public function test_discussion_subscription_created_validation_contextlevel() {
2072          global $CFG, $DB;
2073          require_once($CFG->dirroot . '/mod/forum/lib.php');
2074  
2075          // Setup test data.
2076          $course = $this->getDataGenerator()->create_course();
2077          $user = $this->getDataGenerator()->create_user();
2078          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2079  
2080          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2081          $forum = $this->getDataGenerator()->create_module('forum', $options);
2082  
2083          // Add a discussion.
2084          $record = array();
2085          $record['course'] = $course->id;
2086          $record['forum'] = $forum->id;
2087          $record['userid'] = $user->id;
2088          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2089  
2090          // Add a post.
2091          $record = array();
2092          $record['discussion'] = $discussion->id;
2093          $record['userid'] = $user->id;
2094          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2095  
2096          // The user is not subscribed to the forum. Insert a new discussion subscription.
2097          $subscription = new \stdClass();
2098          $subscription->userid  = $user->id;
2099          $subscription->forum = $forum->id;
2100          $subscription->discussion = $discussion->id;
2101          $subscription->preference = time();
2102  
2103          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2104  
2105          $context = context_module::instance($forum->cmid);
2106  
2107          $params = array(
2108              'context' => \context_course::instance($course->id),
2109              'objectid' => $subscription->id,
2110              'relateduserid' => $user->id,
2111              'other' => array(
2112                  'forumid' => $forum->id,
2113                  'discussion' => $discussion->id,
2114              )
2115          );
2116  
2117          // Without an invalid context.
2118          \mod_forum\event\discussion_subscription_created::create($params);
2119      }
2120  
2121      /**
2122       * Test discussion validation of discussion_subscription_created event.
2123       *
2124       * @expectedException        coding_exception
2125       * @expectedExceptionMessage The 'discussion' value must be set in other.
2126       */
2127      public function test_discussion_subscription_created_validation_discussion() {
2128          global $CFG, $DB;
2129          require_once($CFG->dirroot . '/mod/forum/lib.php');
2130  
2131          // Setup test data.
2132          $course = $this->getDataGenerator()->create_course();
2133          $user = $this->getDataGenerator()->create_user();
2134          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2135  
2136          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2137          $forum = $this->getDataGenerator()->create_module('forum', $options);
2138  
2139          // Add a discussion.
2140          $record = array();
2141          $record['course'] = $course->id;
2142          $record['forum'] = $forum->id;
2143          $record['userid'] = $user->id;
2144          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2145  
2146          // Add a post.
2147          $record = array();
2148          $record['discussion'] = $discussion->id;
2149          $record['userid'] = $user->id;
2150          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2151  
2152          // The user is not subscribed to the forum. Insert a new discussion subscription.
2153          $subscription = new \stdClass();
2154          $subscription->userid  = $user->id;
2155          $subscription->forum = $forum->id;
2156          $subscription->discussion = $discussion->id;
2157          $subscription->preference = time();
2158  
2159          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2160  
2161          // Without the discussion.
2162          $params = array(
2163              'context' => context_module::instance($forum->cmid),
2164              'objectid' => $subscription->id,
2165              'relateduserid' => $user->id,
2166              'other' => array(
2167                  'forumid' => $forum->id,
2168              )
2169          );
2170  
2171          \mod_forum\event\discussion_subscription_created::create($params);
2172      }
2173  
2174      /**
2175       * Test forumid validation of discussion_subscription_created event.
2176       *
2177       * @expectedException        coding_exception
2178       * @expectedExceptionMessage The 'forumid' value must be set in other.
2179       */
2180      public function test_discussion_subscription_created_validation_forumid() {
2181          global $CFG, $DB;
2182          require_once($CFG->dirroot . '/mod/forum/lib.php');
2183  
2184          // Setup test data.
2185          $course = $this->getDataGenerator()->create_course();
2186          $user = $this->getDataGenerator()->create_user();
2187          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2188  
2189          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2190          $forum = $this->getDataGenerator()->create_module('forum', $options);
2191  
2192          // Add a discussion.
2193          $record = array();
2194          $record['course'] = $course->id;
2195          $record['forum'] = $forum->id;
2196          $record['userid'] = $user->id;
2197          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2198  
2199          // Add a post.
2200          $record = array();
2201          $record['discussion'] = $discussion->id;
2202          $record['userid'] = $user->id;
2203          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2204  
2205          // The user is not subscribed to the forum. Insert a new discussion subscription.
2206          $subscription = new \stdClass();
2207          $subscription->userid  = $user->id;
2208          $subscription->forum = $forum->id;
2209          $subscription->discussion = $discussion->id;
2210          $subscription->preference = time();
2211  
2212          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2213  
2214          // Without the forumid.
2215          $params = array(
2216              'context' => context_module::instance($forum->cmid),
2217              'objectid' => $subscription->id,
2218              'relateduserid' => $user->id,
2219              'other' => array(
2220                  'discussion' => $discussion->id,
2221              )
2222          );
2223  
2224          \mod_forum\event\discussion_subscription_created::create($params);
2225      }
2226  
2227      /**
2228       * Test relateduserid validation of discussion_subscription_created event.
2229       *
2230       * @expectedException        coding_exception
2231       * @expectedExceptionMessage The 'relateduserid' must be set.
2232       */
2233      public function test_discussion_subscription_created_validation_relateduserid() {
2234          global $CFG, $DB;
2235          require_once($CFG->dirroot . '/mod/forum/lib.php');
2236  
2237          // Setup test data.
2238          $course = $this->getDataGenerator()->create_course();
2239          $user = $this->getDataGenerator()->create_user();
2240          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2241  
2242          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2243          $forum = $this->getDataGenerator()->create_module('forum', $options);
2244  
2245          // Add a discussion.
2246          $record = array();
2247          $record['course'] = $course->id;
2248          $record['forum'] = $forum->id;
2249          $record['userid'] = $user->id;
2250          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2251  
2252          // Add a post.
2253          $record = array();
2254          $record['discussion'] = $discussion->id;
2255          $record['userid'] = $user->id;
2256          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2257  
2258          // The user is not subscribed to the forum. Insert a new discussion subscription.
2259          $subscription = new \stdClass();
2260          $subscription->userid  = $user->id;
2261          $subscription->forum = $forum->id;
2262          $subscription->discussion = $discussion->id;
2263          $subscription->preference = time();
2264  
2265          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2266  
2267          $context = context_module::instance($forum->cmid);
2268  
2269          // Without the relateduserid.
2270          $params = array(
2271              'context' => context_module::instance($forum->cmid),
2272              'objectid' => $subscription->id,
2273              'other' => array(
2274                  'forumid' => $forum->id,
2275                  'discussion' => $discussion->id,
2276              )
2277          );
2278  
2279          \mod_forum\event\discussion_subscription_created::create($params);
2280      }
2281  
2282      /**
2283       * Test discussion_subscription_deleted event.
2284       */
2285      public function test_discussion_subscription_deleted() {
2286          global $CFG;
2287          require_once($CFG->dirroot . '/mod/forum/lib.php');
2288  
2289          // Setup test data.
2290          $course = $this->getDataGenerator()->create_course();
2291          $user = $this->getDataGenerator()->create_user();
2292          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2293  
2294          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2295          $forum = $this->getDataGenerator()->create_module('forum', $options);
2296  
2297          // Add a discussion.
2298          $record = array();
2299          $record['course'] = $course->id;
2300          $record['forum'] = $forum->id;
2301          $record['userid'] = $user->id;
2302          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2303  
2304          // Add a post.
2305          $record = array();
2306          $record['discussion'] = $discussion->id;
2307          $record['userid'] = $user->id;
2308          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2309  
2310          // Trigger and capturing the event.
2311          $sink = $this->redirectEvents();
2312  
2313          // Trigger the event by unsubscribing the user to the forum discussion.
2314          \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2315  
2316          $events = $sink->get_events();
2317          $this->assertCount(1, $events);
2318          $event = reset($events);
2319  
2320          // Checking that the event contains the expected values.
2321          $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2322  
2323  
2324          $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2325          $context = \context_module::instance($cm->id);
2326          $this->assertEquals($context, $event->get_context());
2327  
2328          $url = new \moodle_url('/mod/forum/subscribe.php', array(
2329              'id' => $forum->id,
2330              'd' => $discussion->id
2331          ));
2332  
2333          $this->assertEquals($url, $event->get_url());
2334          $this->assertEventContextNotUsed($event);
2335          $this->assertNotEmpty($event->get_name());
2336      }
2337  
2338      /**
2339       * Test validation of discussion_subscription_deleted event.
2340       */
2341      public function test_discussion_subscription_deleted_validation() {
2342          global $CFG, $DB;
2343          require_once($CFG->dirroot . '/mod/forum/lib.php');
2344  
2345          // Setup test data.
2346          $course = $this->getDataGenerator()->create_course();
2347          $user = $this->getDataGenerator()->create_user();
2348          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2349  
2350          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2351          $forum = $this->getDataGenerator()->create_module('forum', $options);
2352  
2353          // Add a discussion.
2354          $record = array();
2355          $record['course'] = $course->id;
2356          $record['forum'] = $forum->id;
2357          $record['userid'] = $user->id;
2358          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2359  
2360          // Add a post.
2361          $record = array();
2362          $record['discussion'] = $discussion->id;
2363          $record['userid'] = $user->id;
2364          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2365  
2366          // The user is not subscribed to the forum. Insert a new discussion subscription.
2367          $subscription = new \stdClass();
2368          $subscription->userid  = $user->id;
2369          $subscription->forum = $forum->id;
2370          $subscription->discussion = $discussion->id;
2371          $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
2372  
2373          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2374  
2375          $context = context_module::instance($forum->cmid);
2376  
2377          $params = array(
2378              'context' => $context,
2379              'objectid' => $subscription->id,
2380              'relateduserid' => $user->id,
2381              'other' => array(
2382                  'forumid' => $forum->id,
2383                  'discussion' => $discussion->id,
2384              )
2385          );
2386  
2387          $event = \mod_forum\event\discussion_subscription_deleted::create($params);
2388  
2389          // Trigger and capturing the event.
2390          $sink = $this->redirectEvents();
2391          $event->trigger();
2392          $events = $sink->get_events();
2393          $this->assertCount(1, $events);
2394          $event = reset($events);
2395  
2396          // Without an invalid context.
2397          $params['context'] = \context_course::instance($course->id);
2398          $this->expectException('coding_exception');
2399          $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.');
2400          \mod_forum\event\discussion_deleted::create($params);
2401  
2402          // Without the discussion.
2403          unset($params['discussion']);
2404          $this->expectException('coding_exception');
2405          $this->expectExceptionMessage('The \'discussion\' value must be set in other.');
2406          \mod_forum\event\discussion_deleted::create($params);
2407  
2408          // Without the forumid.
2409          unset($params['forumid']);
2410          $this->expectException('coding_exception');
2411          $this->expectExceptionMessage('The \'forumid\' value must be set in other.');
2412          \mod_forum\event\discussion_deleted::create($params);
2413  
2414          // Without the relateduserid.
2415          unset($params['relateduserid']);
2416          $this->expectException('coding_exception');
2417          $this->expectExceptionMessage('The \'relateduserid\' value must be set in other.');
2418          \mod_forum\event\discussion_deleted::create($params);
2419      }
2420  
2421      /**
2422       * Test contextlevel validation of discussion_subscription_deleted event.
2423       *
2424       * @expectedException        coding_exception
2425       * @expectedExceptionMessage Context level must be CONTEXT_MODULE.
2426       */
2427      public function test_discussion_subscription_deleted_validation_contextlevel() {
2428          global $CFG, $DB;
2429          require_once($CFG->dirroot . '/mod/forum/lib.php');
2430  
2431          // Setup test data.
2432          $course = $this->getDataGenerator()->create_course();
2433          $user = $this->getDataGenerator()->create_user();
2434          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2435  
2436          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2437          $forum = $this->getDataGenerator()->create_module('forum', $options);
2438  
2439          // Add a discussion.
2440          $record = array();
2441          $record['course'] = $course->id;
2442          $record['forum'] = $forum->id;
2443          $record['userid'] = $user->id;
2444          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2445  
2446          // Add a post.
2447          $record = array();
2448          $record['discussion'] = $discussion->id;
2449          $record['userid'] = $user->id;
2450          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2451  
2452          // The user is not subscribed to the forum. Insert a new discussion subscription.
2453          $subscription = new \stdClass();
2454          $subscription->userid  = $user->id;
2455          $subscription->forum = $forum->id;
2456          $subscription->discussion = $discussion->id;
2457          $subscription->preference = time();
2458  
2459          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2460  
2461          $context = context_module::instance($forum->cmid);
2462  
2463          $params = array(
2464              'context' => \context_course::instance($course->id),
2465              'objectid' => $subscription->id,
2466              'relateduserid' => $user->id,
2467              'other' => array(
2468                  'forumid' => $forum->id,
2469                  'discussion' => $discussion->id,
2470              )
2471          );
2472  
2473          // Without an invalid context.
2474          \mod_forum\event\discussion_subscription_deleted::create($params);
2475      }
2476  
2477      /**
2478       * Test discussion validation of discussion_subscription_deleted event.
2479       *
2480       * @expectedException        coding_exception
2481       * @expectedExceptionMessage The 'discussion' value must be set in other.
2482       */
2483      public function test_discussion_subscription_deleted_validation_discussion() {
2484          global $CFG, $DB;
2485          require_once($CFG->dirroot . '/mod/forum/lib.php');
2486  
2487          // Setup test data.
2488          $course = $this->getDataGenerator()->create_course();
2489          $user = $this->getDataGenerator()->create_user();
2490          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2491  
2492          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2493          $forum = $this->getDataGenerator()->create_module('forum', $options);
2494  
2495          // Add a discussion.
2496          $record = array();
2497          $record['course'] = $course->id;
2498          $record['forum'] = $forum->id;
2499          $record['userid'] = $user->id;
2500          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2501  
2502          // Add a post.
2503          $record = array();
2504          $record['discussion'] = $discussion->id;
2505          $record['userid'] = $user->id;
2506          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2507  
2508          // The user is not subscribed to the forum. Insert a new discussion subscription.
2509          $subscription = new \stdClass();
2510          $subscription->userid  = $user->id;
2511          $subscription->forum = $forum->id;
2512          $subscription->discussion = $discussion->id;
2513          $subscription->preference = time();
2514  
2515          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2516  
2517          // Without the discussion.
2518          $params = array(
2519              'context' => context_module::instance($forum->cmid),
2520              'objectid' => $subscription->id,
2521              'relateduserid' => $user->id,
2522              'other' => array(
2523                  'forumid' => $forum->id,
2524              )
2525          );
2526  
2527          \mod_forum\event\discussion_subscription_deleted::create($params);
2528      }
2529  
2530      /**
2531       * Test forumid validation of discussion_subscription_deleted event.
2532       *
2533       * @expectedException        coding_exception
2534       * @expectedExceptionMessage The 'forumid' value must be set in other.
2535       */
2536      public function test_discussion_subscription_deleted_validation_forumid() {
2537          global $CFG, $DB;
2538          require_once($CFG->dirroot . '/mod/forum/lib.php');
2539  
2540          // Setup test data.
2541          $course = $this->getDataGenerator()->create_course();
2542          $user = $this->getDataGenerator()->create_user();
2543          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2544  
2545          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2546          $forum = $this->getDataGenerator()->create_module('forum', $options);
2547  
2548          // Add a discussion.
2549          $record = array();
2550          $record['course'] = $course->id;
2551          $record['forum'] = $forum->id;
2552          $record['userid'] = $user->id;
2553          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2554  
2555          // Add a post.
2556          $record = array();
2557          $record['discussion'] = $discussion->id;
2558          $record['userid'] = $user->id;
2559          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2560  
2561          // The user is not subscribed to the forum. Insert a new discussion subscription.
2562          $subscription = new \stdClass();
2563          $subscription->userid  = $user->id;
2564          $subscription->forum = $forum->id;
2565          $subscription->discussion = $discussion->id;
2566          $subscription->preference = time();
2567  
2568          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2569  
2570          // Without the forumid.
2571          $params = array(
2572              'context' => context_module::instance($forum->cmid),
2573              'objectid' => $subscription->id,
2574              'relateduserid' => $user->id,
2575              'other' => array(
2576                  'discussion' => $discussion->id,
2577              )
2578          );
2579  
2580          \mod_forum\event\discussion_subscription_deleted::create($params);
2581      }
2582  
2583      /**
2584       * Test relateduserid validation of discussion_subscription_deleted event.
2585       *
2586       * @expectedException        coding_exception
2587       * @expectedExceptionMessage The 'relateduserid' must be set.
2588       */
2589      public function test_discussion_subscription_deleted_validation_relateduserid() {
2590          global $CFG, $DB;
2591          require_once($CFG->dirroot . '/mod/forum/lib.php');
2592  
2593          // Setup test data.
2594          $course = $this->getDataGenerator()->create_course();
2595          $user = $this->getDataGenerator()->create_user();
2596          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2597  
2598          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2599          $forum = $this->getDataGenerator()->create_module('forum', $options);
2600  
2601          // Add a discussion.
2602          $record = array();
2603          $record['course'] = $course->id;
2604          $record['forum'] = $forum->id;
2605          $record['userid'] = $user->id;
2606          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2607  
2608          // Add a post.
2609          $record = array();
2610          $record['discussion'] = $discussion->id;
2611          $record['userid'] = $user->id;
2612          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2613  
2614          // The user is not subscribed to the forum. Insert a new discussion subscription.
2615          $subscription = new \stdClass();
2616          $subscription->userid  = $user->id;
2617          $subscription->forum = $forum->id;
2618          $subscription->discussion = $discussion->id;
2619          $subscription->preference = time();
2620  
2621          $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription);
2622  
2623          $context = context_module::instance($forum->cmid);
2624  
2625          // Without the relateduserid.
2626          $params = array(
2627              'context' => context_module::instance($forum->cmid),
2628              'objectid' => $subscription->id,
2629              'other' => array(
2630                  'forumid' => $forum->id,
2631                  'discussion' => $discussion->id,
2632              )
2633          );
2634  
2635          \mod_forum\event\discussion_subscription_deleted::create($params);
2636      }
2637  
2638      /**
2639       * Test that the correct context is used in the events when subscribing
2640       * users.
2641       */
2642      public function test_forum_subscription_page_context_valid() {
2643          global $CFG, $PAGE;
2644          require_once($CFG->dirroot . '/mod/forum/lib.php');
2645  
2646          // Setup test data.
2647          $course = $this->getDataGenerator()->create_course();
2648          $user = $this->getDataGenerator()->create_user();
2649          $this->getDataGenerator()->enrol_user($user->id, $course->id);
2650  
2651          $options = array('course' => $course->id, 'forcesubscribe' => FORUM_CHOOSESUBSCRIBE);
2652          $forum = $this->getDataGenerator()->create_module('forum', $options);
2653          $quiz = $this->getDataGenerator()->create_module('quiz', $options);
2654  
2655          // Add a discussion.
2656          $record = array();
2657          $record['course'] = $course->id;
2658          $record['forum'] = $forum->id;
2659          $record['userid'] = $user->id;
2660          $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
2661  
2662          // Add a post.
2663          $record = array();
2664          $record['discussion'] = $discussion->id;
2665          $record['userid'] = $user->id;
2666          $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record);
2667  
2668          // Set up the default page event to use this forum.
2669          $PAGE = new moodle_page();
2670          $cm = get_coursemodule_from_instance('forum', $discussion->forum);
2671          $context = \context_module::instance($cm->id);
2672          $PAGE->set_context($context);
2673          $PAGE->set_cm($cm, $course, $forum);
2674  
2675          // Trigger and capturing the event.
2676          $sink = $this->redirectEvents();
2677  
2678          // Trigger the event by subscribing the user to the forum.
2679          \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2680  
2681          $events = $sink->get_events();
2682          $sink->clear();
2683          $this->assertCount(1, $events);
2684          $event = reset($events);
2685  
2686          // Checking that the event contains the expected values.
2687          $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2688          $this->assertEquals($context, $event->get_context());
2689  
2690          // Trigger the event by unsubscribing the user to the forum.
2691          \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2692  
2693          $events = $sink->get_events();
2694          $sink->clear();
2695          $this->assertCount(1, $events);
2696          $event = reset($events);
2697  
2698          // Checking that the event contains the expected values.
2699          $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2700          $this->assertEquals($context, $event->get_context());
2701  
2702          // Trigger the event by subscribing the user to the discussion.
2703          \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2704  
2705          $events = $sink->get_events();
2706          $sink->clear();
2707          $this->assertCount(1, $events);
2708          $event = reset($events);
2709  
2710          // Checking that the event contains the expected values.
2711          $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2712          $this->assertEquals($context, $event->get_context());
2713  
2714          // Trigger the event by unsubscribing the user from the discussion.
2715          \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2716  
2717          $events = $sink->get_events();
2718          $sink->clear();
2719          $this->assertCount(1, $events);
2720          $event = reset($events);
2721  
2722          // Checking that the event contains the expected values.
2723          $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2724          $this->assertEquals($context, $event->get_context());
2725  
2726          // Now try with the context for a different module (quiz).
2727          $PAGE = new moodle_page();
2728          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
2729          $quizcontext = \context_module::instance($cm->id);
2730          $PAGE->set_context($quizcontext);
2731          $PAGE->set_cm($cm, $course, $quiz);
2732  
2733          // Trigger and capturing the event.
2734          $sink = $this->redirectEvents();
2735  
2736          // Trigger the event by subscribing the user to the forum.
2737          \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2738  
2739          $events = $sink->get_events();
2740          $sink->clear();
2741          $this->assertCount(1, $events);
2742          $event = reset($events);
2743  
2744          // Checking that the event contains the expected values.
2745          $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2746          $this->assertEquals($context, $event->get_context());
2747  
2748          // Trigger the event by unsubscribing the user to the forum.
2749          \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2750  
2751          $events = $sink->get_events();
2752          $sink->clear();
2753          $this->assertCount(1, $events);
2754          $event = reset($events);
2755  
2756          // Checking that the event contains the expected values.
2757          $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2758          $this->assertEquals($context, $event->get_context());
2759  
2760          // Trigger the event by subscribing the user to the discussion.
2761          \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2762  
2763          $events = $sink->get_events();
2764          $sink->clear();
2765          $this->assertCount(1, $events);
2766          $event = reset($events);
2767  
2768          // Checking that the event contains the expected values.
2769          $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2770          $this->assertEquals($context, $event->get_context());
2771  
2772          // Trigger the event by unsubscribing the user from the discussion.
2773          \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2774  
2775          $events = $sink->get_events();
2776          $sink->clear();
2777          $this->assertCount(1, $events);
2778          $event = reset($events);
2779  
2780          // Checking that the event contains the expected values.
2781          $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2782          $this->assertEquals($context, $event->get_context());
2783  
2784          // Now try with the course context - the module context should still be used.
2785          $PAGE = new moodle_page();
2786          $coursecontext = \context_course::instance($course->id);
2787          $PAGE->set_context($coursecontext);
2788  
2789          // Trigger the event by subscribing the user to the forum.
2790          \mod_forum\subscriptions::subscribe_user($user->id, $forum);
2791  
2792          $events = $sink->get_events();
2793          $sink->clear();
2794          $this->assertCount(1, $events);
2795          $event = reset($events);
2796  
2797          // Checking that the event contains the expected values.
2798          $this->assertInstanceOf('\mod_forum\event\subscription_created', $event);
2799          $this->assertEquals($context, $event->get_context());
2800  
2801          // Trigger the event by unsubscribing the user to the forum.
2802          \mod_forum\subscriptions::unsubscribe_user($user->id, $forum);
2803  
2804          $events = $sink->get_events();
2805          $sink->clear();
2806          $this->assertCount(1, $events);
2807          $event = reset($events);
2808  
2809          // Checking that the event contains the expected values.
2810          $this->assertInstanceOf('\mod_forum\event\subscription_deleted', $event);
2811          $this->assertEquals($context, $event->get_context());
2812  
2813          // Trigger the event by subscribing the user to the discussion.
2814          \mod_forum\subscriptions::subscribe_user_to_discussion($user->id, $discussion);
2815  
2816          $events = $sink->get_events();
2817          $sink->clear();
2818          $this->assertCount(1, $events);
2819          $event = reset($events);
2820  
2821          // Checking that the event contains the expected values.
2822          $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event);
2823          $this->assertEquals($context, $event->get_context());
2824  
2825          // Trigger the event by unsubscribing the user from the discussion.
2826          \mod_forum\subscriptions::unsubscribe_user_from_discussion($user->id, $discussion);
2827  
2828          $events = $sink->get_events();
2829          $sink->clear();
2830          $this->assertCount(1, $events);
2831          $event = reset($events);
2832  
2833          // Checking that the event contains the expected values.
2834          $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event);
2835          $this->assertEquals($context, $event->get_context());
2836  
2837      }
2838  
2839      /**
2840       * Test mod_forum_observer methods.
2841       */
2842      public function test_observers() {
2843          global $DB, $CFG;
2844  
2845          require_once($CFG->dirroot . '/mod/forum/lib.php');
2846  
2847          $forumgen = $this->getDataGenerator()->get_plugin_generator('mod_forum');
2848  
2849          $course = $this->getDataGenerator()->create_course();
2850          $trackedrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2851          $untrackedrecord = array('course' => $course->id, 'type' => 'general');
2852          $trackedforum = $this->getDataGenerator()->create_module('forum', $trackedrecord);
2853          $untrackedforum = $this->getDataGenerator()->create_module('forum', $untrackedrecord);
2854  
2855          // Used functions don't require these settings; adding
2856          // them just in case there are APIs changes in future.
2857          $user = $this->getDataGenerator()->create_user(array(
2858              'maildigest' => 1,
2859              'trackforums' => 1
2860          ));
2861  
2862          $manplugin = enrol_get_plugin('manual');
2863          $manualenrol = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'));
2864          $student = $DB->get_record('role', array('shortname' => 'student'));
2865  
2866          // The role_assign observer does it's job adding the forum_subscriptions record.
2867          $manplugin->enrol_user($manualenrol, $user->id, $student->id);
2868  
2869          // They are not required, but in a real environment they are supposed to be required;
2870          // adding them just in case there are APIs changes in future.
2871          set_config('forum_trackingtype', 1);
2872          set_config('forum_trackreadposts', 1);
2873  
2874          $record = array();
2875          $record['course'] = $course->id;
2876          $record['forum'] = $trackedforum->id;
2877          $record['userid'] = $user->id;
2878          $discussion = $forumgen->create_discussion($record);
2879  
2880          $record = array();
2881          $record['discussion'] = $discussion->id;
2882          $record['userid'] = $user->id;
2883          $post = $forumgen->create_post($record);
2884  
2885          forum_tp_add_read_record($user->id, $post->id);
2886          forum_set_user_maildigest($trackedforum, 2, $user);
2887          forum_tp_stop_tracking($untrackedforum->id, $user->id);
2888  
2889          $this->assertEquals(1, $DB->count_records('forum_subscriptions'));
2890          $this->assertEquals(1, $DB->count_records('forum_digests'));
2891          $this->assertEquals(1, $DB->count_records('forum_track_prefs'));
2892          $this->assertEquals(1, $DB->count_records('forum_read'));
2893  
2894          // The course_module_created observer does it's job adding a subscription.
2895          $forumrecord = array('course' => $course->id, 'type' => 'general', 'forcesubscribe' => FORUM_INITIALSUBSCRIBE);
2896          $extraforum = $this->getDataGenerator()->create_module('forum', $forumrecord);
2897          $this->assertEquals(2, $DB->count_records('forum_subscriptions'));
2898  
2899          $manplugin->unenrol_user($manualenrol, $user->id);
2900  
2901          $this->assertEquals(0, $DB->count_records('forum_digests'));
2902          $this->assertEquals(0, $DB->count_records('forum_subscriptions'));
2903          $this->assertEquals(0, $DB->count_records('forum_track_prefs'));
2904          $this->assertEquals(0, $DB->count_records('forum_read'));
2905      }
2906  
2907  }