Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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   * Events tests.
  19   *
  20   * @package    block_comments
  21   * @category   test
  22   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace block_comments\event;
  27  
  28  /**
  29   * Events tests class.
  30   *
  31   * @package    block_comments
  32   * @category   test
  33   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class events_test extends \advanced_testcase {
  37      /** @var stdClass Keeps course object */
  38      private $course;
  39  
  40      /** @var stdClass Keeps wiki object */
  41      private $wiki;
  42  
  43      /**
  44       * Setup test data.
  45       */
  46      public function setUp(): void {
  47          $this->resetAfterTest();
  48          $this->setAdminUser();
  49  
  50          // Create course and wiki.
  51          $this->course = $this->getDataGenerator()->create_course();
  52          $this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
  53      }
  54  
  55      /**
  56       * Test comment_created event.
  57       */
  58      public function test_comment_created() {
  59          global $CFG;
  60  
  61          require_once($CFG->dirroot . '/comment/lib.php');
  62  
  63          // Comment on course page.
  64          $context = \context_course::instance($this->course->id);
  65          $args = new \stdClass;
  66          $args->context = $context;
  67          $args->course = $this->course;
  68          $args->area = 'page_comments';
  69          $args->itemid = 0;
  70          $args->component = 'block_comments';
  71          $args->linktext = get_string('showcomments');
  72          $args->notoggle = true;
  73          $args->autostart = true;
  74          $args->displaycancel = false;
  75          $comment = new \comment($args);
  76  
  77          // Triggering and capturing the event.
  78          $sink = $this->redirectEvents();
  79          $comment->add('New comment');
  80          $events = $sink->get_events();
  81          $this->assertCount(1, $events);
  82          $event = reset($events);
  83  
  84          // Checking that the event contains the expected values.
  85          $this->assertInstanceOf('\block_comments\event\comment_created', $event);
  86          $this->assertEquals($context, $event->get_context());
  87          $url = new \moodle_url('/course/view.php', array('id' => $this->course->id));
  88          $this->assertEquals($url, $event->get_url());
  89  
  90          // Comments when block is on module (wiki) page.
  91          $context = \context_module::instance($this->wiki->cmid);
  92          $args = new \stdClass;
  93          $args->context   = $context;
  94          $args->course    = $this->course;
  95          $args->area      = 'page_comments';
  96          $args->itemid    = 0;
  97          $args->component = 'block_comments';
  98          $args->linktext  = get_string('showcomments');
  99          $args->notoggle  = true;
 100          $args->autostart = true;
 101          $args->displaycancel = false;
 102          $comment = new \comment($args);
 103  
 104          // Triggering and capturing the event.
 105          $sink = $this->redirectEvents();
 106          $comment->add('New comment 1');
 107          $events = $sink->get_events();
 108          $this->assertCount(1, $events);
 109          $event = reset($events);
 110  
 111          // Checking that the event contains the expected values.
 112          $this->assertInstanceOf('\block_comments\event\comment_created', $event);
 113          $this->assertEquals($context, $event->get_context());
 114          $url = new \moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
 115          $this->assertEquals($url, $event->get_url());
 116          $this->assertEventContextNotUsed($event);
 117      }
 118  
 119      /**
 120       * Test comment_deleted event.
 121       */
 122      public function test_comment_deleted() {
 123          global $CFG;
 124  
 125          require_once($CFG->dirroot . '/comment/lib.php');
 126  
 127          // Comment on course page.
 128          $context = \context_course::instance($this->course->id);
 129          $args = new \stdClass;
 130          $args->context   = $context;
 131          $args->course    = $this->course;
 132          $args->area      = 'page_comments';
 133          $args->itemid    = 0;
 134          $args->component = 'block_comments';
 135          $args->linktext  = get_string('showcomments');
 136          $args->notoggle  = true;
 137          $args->autostart = true;
 138          $args->displaycancel = false;
 139          $comment = new \comment($args);
 140          $newcomment = $comment->add('New comment');
 141  
 142          // Triggering and capturing the event.
 143          $sink = $this->redirectEvents();
 144          $comment->delete($newcomment->id);
 145          $events = $sink->get_events();
 146          $this->assertCount(1, $events);
 147          $event = reset($events);
 148  
 149          // Checking that the event contains the expected values.
 150          $this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
 151          $this->assertEquals($context, $event->get_context());
 152          $url = new \moodle_url('/course/view.php', array('id' => $this->course->id));
 153          $this->assertEquals($url, $event->get_url());
 154  
 155          // Comments when block is on module (wiki) page.
 156          $context = \context_module::instance($this->wiki->cmid);
 157          $args = new \stdClass;
 158          $args->context   = $context;
 159          $args->course    = $this->course;
 160          $args->area      = 'page_comments';
 161          $args->itemid    = 0;
 162          $args->component = 'block_comments';
 163          $args->linktext  = get_string('showcomments');
 164          $args->notoggle  = true;
 165          $args->autostart = true;
 166          $args->displaycancel = false;
 167          $comment = new \comment($args);
 168          $newcomment = $comment->add('New comment 1');
 169  
 170          // Triggering and capturing the event.
 171          $sink = $this->redirectEvents();
 172          $comment->delete($newcomment->id);
 173          $events = $sink->get_events();
 174          $this->assertCount(1, $events);
 175          $event = reset($events);
 176  
 177          // Checking that the event contains the expected values.
 178          $this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
 179          $this->assertEquals($context, $event->get_context());
 180          $url = new \moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
 181          $this->assertEquals($url, $event->get_url());
 182          $this->assertEventContextNotUsed($event);
 183      }
 184  }