Search moodle.org's
Developer Documentation


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

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