Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * 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  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  /**
  30   * Unit tests for the dashboard events.
  31   *
  32   * @copyright  2016 Stephen Bourget
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class dashboard_events_testcase extends advanced_testcase {
  36  
  37      /** @var user cobject */
  38      protected $user;
  39  
  40      /**
  41       * Setup often used objects for the following tests.
  42       */
  43      protected function setUp(): void {
  44          global $USER;
  45  
  46          $this->resetAfterTest();
  47  
  48          // The user we are going to test this on.
  49          $this->setAdminUser();
  50          $this->user = $USER;
  51      }
  52  
  53      /**
  54       * Test the dashboard viewed event.
  55       *
  56       * There is no external API for viewing the dashboard, so the unit test will simply
  57       * create and trigger the event and ensure data is returned as expected.
  58       */
  59      public function test_dashboard_viewed() {
  60  
  61          $user = $this->user;
  62          // Trigger an event: dashboard viewed.
  63          $eventparams = array(
  64              'context' => $context = context_user::instance($user->id)
  65          );
  66  
  67          $event = \core\event\dashboard_viewed::create($eventparams);
  68          // Trigger and capture the event.
  69          $sink = $this->redirectEvents();
  70          $event->trigger();
  71          $events = $sink->get_events();
  72          $event = reset($events);
  73  
  74          // Check that the event data is valid.
  75          $this->assertInstanceOf('\core\event\dashboard_viewed', $event);
  76          $this->assertEquals($user->id, $event->userid);
  77          $this->assertDebuggingNotCalled();
  78      }
  79  
  80      /**
  81       * Test the dashboard reset event.
  82       *
  83       * We will reset the user dashboard to
  84       * trigger the event and ensure data is returned as expected.
  85       */
  86      public function test_dashboard_reset() {
  87          global $CFG;
  88          require_once($CFG->dirroot . '/my/lib.php');
  89          $user = $this->user;
  90          $sink = $this->redirectEvents();
  91  
  92          // Reset the dashboard.
  93          my_reset_page($user->id);
  94  
  95          // Trigger and capture the event.
  96          $events = $sink->get_events();
  97          $event = reset($events);
  98  
  99          // Check that the event data is valid.
 100          $this->assertInstanceOf('\core\event\dashboard_reset', $event);
 101          $this->assertEquals($user->id, $event->userid);
 102          $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']);
 103          $this->assertEquals('my-index', $event->other['pagetype']);
 104          $this->assertDebuggingNotCalled();
 105  
 106          // Reset the dashboard with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'.
 107          $sink = $this->redirectEvents();
 108          my_reset_page($user->id, MY_PAGE_PUBLIC, 'user-profile');
 109  
 110          // Trigger and capture the event.
 111          $events = $sink->get_events();
 112          $event = reset($events);
 113          $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']);
 114          $this->assertEquals('user-profile', $event->other['pagetype']);
 115      }
 116  
 117      /**
 118       * Test the dashboards reset event.
 119       *
 120       * We will reset all user dashboards to
 121       * trigger the event and ensure data is returned as expected.
 122       */
 123      public function test_dashboards_reset() {
 124          global $CFG, $USER;
 125          require_once($CFG->dirroot . '/my/lib.php');
 126  
 127          $sink = $this->redirectEvents();
 128  
 129          // Reset all dashbaords.
 130          my_reset_page_for_all_users();
 131  
 132          // Trigger and capture the event.
 133          $events = $sink->get_events();
 134          $event = reset($events);
 135  
 136          // Check that the event data is valid.
 137          $this->assertInstanceOf('\core\event\dashboards_reset', $event);
 138          $this->assertEquals($USER->id, $event->userid);
 139          $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']);
 140          $this->assertEquals('my-index', $event->other['pagetype']);
 141          $this->assertDebuggingNotCalled();
 142  
 143          // Reset the dashboards with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'.
 144          $sink = $this->redirectEvents();
 145          my_reset_page_for_all_users(MY_PAGE_PUBLIC, 'user-profile');
 146  
 147          // Trigger and capture the event.
 148          $events = $sink->get_events();
 149          $event = reset($events);
 150          $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']);
 151          $this->assertEquals('user-profile', $event->other['pagetype']);
 152      }
 153  }