Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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    core
  21   * @category   test
  22   * @copyright  2016 Stephen Bourget
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_my\event;
  27  
  28  /**
  29   * Unit tests for the dashboard events.
  30   *
  31   * @copyright  2016 Stephen Bourget
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class events_test extends \advanced_testcase {
  35  
  36      /** @var user cobject */
  37      protected $user;
  38  
  39      /**
  40       * Setup often used objects for the following tests.
  41       */
  42      protected function setUp(): void {
  43          global $USER;
  44  
  45          $this->resetAfterTest();
  46  
  47          // The user we are going to test this on.
  48          $this->setAdminUser();
  49          $this->user = $USER;
  50      }
  51  
  52      /**
  53       * Test the dashboard viewed event.
  54       *
  55       * There is no external API for viewing the dashboard, so the unit test will simply
  56       * create and trigger the event and ensure data is returned as expected.
  57       */
  58      public function test_dashboard_viewed() {
  59  
  60          $user = $this->user;
  61          // Trigger an event: dashboard viewed.
  62          $eventparams = array(
  63              'context' => $context = \context_user::instance($user->id)
  64          );
  65  
  66          $event = \core\event\dashboard_viewed::create($eventparams);
  67          // Trigger and capture the event.
  68          $sink = $this->redirectEvents();
  69          $event->trigger();
  70          $events = $sink->get_events();
  71          $event = reset($events);
  72  
  73          // Check that the event data is valid.
  74          $this->assertInstanceOf('\core\event\dashboard_viewed', $event);
  75          $this->assertEquals($user->id, $event->userid);
  76          $this->assertDebuggingNotCalled();
  77      }
  78  
  79      /**
  80       * Test the dashboard reset event.
  81       *
  82       * We will reset the user dashboard to
  83       * trigger the event and ensure data is returned as expected.
  84       */
  85      public function test_dashboard_reset() {
  86          global $CFG;
  87          require_once($CFG->dirroot . '/my/lib.php');
  88          $user = $this->user;
  89          $sink = $this->redirectEvents();
  90  
  91          // Reset the dashboard.
  92          my_reset_page($user->id);
  93  
  94          // Trigger and capture the event.
  95          $events = $sink->get_events();
  96          $event = reset($events);
  97  
  98          // Check that the event data is valid.
  99          $this->assertInstanceOf('\core\event\dashboard_reset', $event);
 100          $this->assertEquals($user->id, $event->userid);
 101          $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']);
 102          $this->assertEquals('my-index', $event->other['pagetype']);
 103          $this->assertDebuggingNotCalled();
 104  
 105          // Reset the dashboard with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'.
 106          $sink = $this->redirectEvents();
 107          my_reset_page($user->id, MY_PAGE_PUBLIC, 'user-profile');
 108  
 109          // Trigger and capture the event.
 110          $events = $sink->get_events();
 111          $event = reset($events);
 112          $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']);
 113          $this->assertEquals('user-profile', $event->other['pagetype']);
 114      }
 115  
 116      /**
 117       * Test the dashboards reset event.
 118       *
 119       * We will reset all user dashboards to
 120       * trigger the event and ensure data is returned as expected.
 121       */
 122      public function test_dashboards_reset() {
 123          global $CFG, $USER;
 124          require_once($CFG->dirroot . '/my/lib.php');
 125  
 126          $sink = $this->redirectEvents();
 127  
 128          // Reset all dashbaords.
 129          my_reset_page_for_all_users();
 130  
 131          // Trigger and capture the event.
 132          $events = $sink->get_events();
 133          $event = reset($events);
 134  
 135          // Check that the event data is valid.
 136          $this->assertInstanceOf('\core\event\dashboards_reset', $event);
 137          $this->assertEquals($USER->id, $event->userid);
 138          $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']);
 139          $this->assertEquals('my-index', $event->other['pagetype']);
 140          $this->assertDebuggingNotCalled();
 141  
 142          // Reset the dashboards with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'.
 143          $sink = $this->redirectEvents();
 144          my_reset_page_for_all_users(MY_PAGE_PUBLIC, 'user-profile');
 145  
 146          // Trigger and capture the event.
 147          $events = $sink->get_events();
 148          $event = reset($events);
 149          $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']);
 150          $this->assertEquals('user-profile', $event->other['pagetype']);
 151      }
 152  }