Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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    assignsubmission_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 assignsubmission_comments\event;
  27  
  28  use mod_assign_test_generator;
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once($CFG->dirroot . '/mod/assign/lib.php');
  33  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  34  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  35  require_once($CFG->dirroot . '/comment/lib.php');
  36  
  37  /**
  38   * Events tests class.
  39   *
  40   * @package    assignsubmission_comments
  41   * @category   test
  42   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class events_test extends \advanced_testcase {
  46  
  47      // Use the generator helper.
  48      use mod_assign_test_generator;
  49  
  50      /**
  51       * Test comment_created event.
  52       */
  53      public function test_comment_created() {
  54          $this->resetAfterTest();
  55          $course = $this->getDataGenerator()->create_course();
  56          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  57          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  58  
  59          $assign = $this->create_instance($course);
  60  
  61          $this->setUser($teacher);
  62          $submission = $assign->get_user_submission($student->id, true);
  63  
  64          $context = $assign->get_context();
  65          $options = new \stdClass();
  66          $options->area = 'submission_comments';
  67          $options->course = $assign->get_course();
  68          $options->context = $context;
  69          $options->itemid = $submission->id;
  70          $options->component = 'assignsubmission_comments';
  71          $options->showcount = true;
  72          $options->displaycancel = true;
  73  
  74          $comment = new \comment($options);
  75  
  76          // Triggering and capturing the event.
  77          $sink = $this->redirectEvents();
  78          $comment->add('New comment');
  79          $events = $sink->get_events();
  80          $this->assertCount(1, $events);
  81          $event = reset($events);
  82          $sink->close();
  83  
  84          // Checking that the event contains the expected values.
  85          $this->assertInstanceOf('\assignsubmission_comments\event\comment_created', $event);
  86          $this->assertEquals($context, $event->get_context());
  87          $url = new \moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id));
  88          $this->assertEquals($url, $event->get_url());
  89          $this->assertEventContextNotUsed($event);
  90      }
  91  
  92      /**
  93       * Test comment_deleted event.
  94       */
  95      public function test_comment_deleted() {
  96          $this->resetAfterTest();
  97          $course = $this->getDataGenerator()->create_course();
  98          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
  99          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 100  
 101          $assign = $this->create_instance($course);
 102  
 103          $this->setUser($teacher);
 104          $submission = $assign->get_user_submission($student->id, true);
 105  
 106          $context = $assign->get_context();
 107          $options = new \stdClass();
 108          $options->area    = 'submission_comments';
 109          $options->course    = $assign->get_course();
 110          $options->context = $context;
 111          $options->itemid  = $submission->id;
 112          $options->component = 'assignsubmission_comments';
 113          $options->showcount = true;
 114          $options->displaycancel = true;
 115          $comment = new \comment($options);
 116          $newcomment = $comment->add('New comment 1');
 117  
 118          // Triggering and capturing the event.
 119          $sink = $this->redirectEvents();
 120          $comment->delete($newcomment->id);
 121          $events = $sink->get_events();
 122          $this->assertCount(1, $events);
 123          $event = reset($events);
 124  
 125          // Checking that the event contains the expected values.
 126          $this->assertInstanceOf('\assignsubmission_comments\event\comment_deleted', $event);
 127          $this->assertEquals($context, $event->get_context());
 128          $url = new \moodle_url('/mod/assign/view.php', array('id' => $assign->get_course_module()->id));
 129          $this->assertEquals($url, $event->get_url());
 130          $this->assertEventContextNotUsed($event);
 131      }
 132  }