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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [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   * Tests for manager.
  19   *
  20   * @package    tool_usertours
  21   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->libdir . '/formslib.php');
  29  require_once (__DIR__ . '/helper_trait.php');
  30  
  31  /**
  32   * Tests for step.
  33   *
  34   * @package    tool_usertours
  35   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class tool_usertours_manager_testcase extends advanced_testcase {
  39      // There are shared helpers for these tests in the helper trait.
  40      use tool_usertours_helper_trait;
  41  
  42      /**
  43       * @var moodle_database
  44       */
  45      protected $db;
  46  
  47      /**
  48       * Setup to store the DB reference.
  49       */
  50      public function setUp(): void {
  51          global $DB;
  52  
  53          $this->db = $DB;
  54      }
  55  
  56      /**
  57       * Tear down to restore the original DB reference.
  58       */
  59      public function tearDown(): void {
  60          global $DB;
  61  
  62          $DB = $this->db;
  63      }
  64  
  65      /**
  66       * Helper to mock the database.
  67       *
  68       * @return moodle_database
  69       */
  70      public function mock_database() {
  71          global $DB;
  72  
  73          $DB = $this->getMockBuilder('moodle_database')->getMock();
  74  
  75          return $DB;
  76      }
  77  
  78      /**
  79       * Data provider to ensure that all modification actions require the session key.
  80       *
  81       * @return array
  82       */
  83      public function sesskey_required_provider() {
  84          $tourid = rand(1, 100);
  85          $stepid = rand(1, 100);
  86  
  87          return [
  88                  'Tour removal' => [
  89                          'delete_tour',
  90                          [$tourid],
  91                      ],
  92                  'Step removal' => [
  93                          'delete_step',
  94                          [$stepid],
  95                      ],
  96                  'Tour visibility' => [
  97                          'show_hide_tour',
  98                          [$tourid, true],
  99                      ],
 100                  'Move step' => [
 101                          'move_step',
 102                          [$stepid],
 103                      ],
 104              ];
 105      }
 106  
 107      /**
 108       * Ensure that all modification actions require the session key.
 109       *
 110       * @dataProvider sesskey_required_provider
 111       * @param   string  $function   The function to test
 112       * @param   array   $arguments  The arguments to pass with it
 113       */
 114      public function test_sesskey_required($function, $arguments) {
 115          $manager = new \tool_usertours\manager();
 116  
 117          $rc = new \ReflectionClass('\tool_usertours\manager');
 118          $rcm = $rc->getMethod($function);
 119          $rcm->setAccessible(true);
 120  
 121          $this->expectException('moodle_exception');
 122          $rcm->invokeArgs($manager, $arguments);
 123      }
 124  
 125      /**
 126       * Data provider for test_move_tour
 127       *
 128       * @return array
 129       */
 130      public function move_tour_provider() {
 131          $alltours = [
 132              ['name' => 'Tour 1'],
 133              ['name' => 'Tour 2'],
 134              ['name' => 'Tour 3'],
 135          ];
 136  
 137          return [
 138              'Move up' => [
 139                  $alltours,
 140                  'Tour 2',
 141                  \tool_usertours\helper::MOVE_UP,
 142                  0,
 143              ],
 144              'Move down' => [
 145                  $alltours,
 146                  'Tour 2',
 147                  \tool_usertours\helper::MOVE_DOWN,
 148                  2,
 149              ],
 150              'Move up (first)' => [
 151                  $alltours,
 152                  'Tour 1',
 153                  \tool_usertours\helper::MOVE_UP,
 154                  0,
 155              ],
 156              'Move down (last)' => [
 157                  $alltours,
 158                  'Tour 3',
 159                  \tool_usertours\helper::MOVE_DOWN,
 160                  2,
 161              ],
 162          ];
 163      }
 164  
 165      /**
 166       * Test moving tours (changing sortorder)
 167       *
 168       * @dataProvider move_tour_provider
 169       *
 170       * @param array $alltours
 171       * @param string $movetourname
 172       * @param int $direction
 173       * @param int $expectedsortorder
 174       * @return void
 175       */
 176      public function test_move_tour($alltours, $movetourname, $direction, $expectedsortorder) {
 177          global $DB;
 178  
 179          $this->resetAfterTest();
 180  
 181          // Clear out existing tours so ours are the only ones, otherwise we can't predict the sortorder.
 182          $DB->delete_records('tool_usertours_tours');
 183  
 184          foreach ($alltours as $tourconfig) {
 185              $this->helper_create_tour((object) $tourconfig);
 186          }
 187  
 188          // Load our tour to move.
 189          $record = $DB->get_record('tool_usertours_tours', ['name' => $movetourname]);
 190          $tour = \tool_usertours\tour::load_from_record($record);
 191  
 192          // Call protected method via reflection.
 193          $class = new ReflectionClass(\tool_usertours\manager::class);
 194          $method = $class->getMethod('_move_tour');
 195          $method->setAccessible(true);
 196          $method->invokeArgs(null, [$tour, $direction]);
 197  
 198          // Assert expected sortorder.
 199          $this->assertEquals($expectedsortorder, $tour->get_sortorder());
 200      }
 201  
 202      /**
 203       * Data Provider for get_matching_tours tests.
 204       *
 205       * @return array
 206       */
 207      public function get_matching_tours_provider() {
 208          global $CFG;
 209  
 210          $alltours = [
 211              [
 212                      'pathmatch'     => '/my/%',
 213                      'enabled'       => false,
 214                      'name'          => 'Failure',
 215                      'description'   => '',
 216                      'configdata'    => '',
 217                  ],
 218              [
 219                      'pathmatch'     => '/my/%',
 220                      'enabled'       => true,
 221                      'name'          => 'My tour enabled',
 222                      'description'   => '',
 223                      'configdata'    => '',
 224                  ],
 225              [
 226                      'pathmatch'     => '/my/%',
 227                      'enabled'       => true,
 228                      'name'          => 'My tour enabled 2',
 229                      'description'   => '',
 230                      'configdata'    => '',
 231                  ],
 232              [
 233                      'pathmatch'     => '/my/%',
 234                      'enabled'       => false,
 235                      'name'          => 'Failure',
 236                      'description'   => '',
 237                      'configdata'    => '',
 238                  ],
 239              [
 240                      'pathmatch'     => '/course/?id=%foo=bar',
 241                      'enabled'       => false,
 242                      'name'          => 'Failure',
 243                      'description'   => '',
 244                      'configdata'    => '',
 245                  ],
 246              [
 247                      'pathmatch'     => '/course/?id=%foo=bar',
 248                      'enabled'       => true,
 249                      'name'          => 'course tour with additional params enabled',
 250                      'description'   => '',
 251                      'configdata'    => '',
 252                  ],
 253              [
 254                      'pathmatch'     => '/course/?id=%foo=bar',
 255                      'enabled'       => false,
 256                      'name'          => 'Failure',
 257                      'description'   => '',
 258                      'configdata'    => '',
 259                  ],
 260              [
 261                      'pathmatch'     => '/course/?id=%',
 262                      'enabled'       => false,
 263                      'name'          => 'Failure',
 264                      'description'   => '',
 265                      'configdata'    => '',
 266                  ],
 267              [
 268                      'pathmatch'     => '/course/?id=%',
 269                      'enabled'       => true,
 270                      'name'          => 'course tour enabled',
 271                      'description'   => '',
 272                      'configdata'    => '',
 273                  ],
 274              [
 275                      'pathmatch'     => '/course/?id=%',
 276                      'enabled'       => false,
 277                      'name'          => 'Failure',
 278                      'description'   => '',
 279                      'configdata'    => '',
 280                  ],
 281          ];
 282  
 283          return [
 284                  'No matches found' => [
 285                          $alltours,
 286                          $CFG->wwwroot . '/some/invalid/value',
 287                          [],
 288                      ],
 289                  'Never return a disabled tour' => [
 290                          $alltours,
 291                          $CFG->wwwroot . '/my/index.php',
 292                          ['My tour enabled', 'My tour enabled 2'],
 293                      ],
 294                  'My not course' => [
 295                          $alltours,
 296                          $CFG->wwwroot . '/my/index.php',
 297                          ['My tour enabled', 'My tour enabled 2'],
 298                      ],
 299                  'My with params' => [
 300                          $alltours,
 301                          $CFG->wwwroot . '/my/index.php?id=42',
 302                          ['My tour enabled', 'My tour enabled 2'],
 303                      ],
 304                  'Course with params' => [
 305                          $alltours,
 306                          $CFG->wwwroot . '/course/?id=42',
 307                          ['course tour enabled'],
 308                      ],
 309                  'Course with params and trailing content' => [
 310                          $alltours,
 311                          $CFG->wwwroot . '/course/?id=42&foo=bar',
 312                          ['course tour with additional params enabled', 'course tour enabled'],
 313                      ],
 314              ];
 315      }
 316  
 317      /**
 318       * Tests for the get_matching_tours function.
 319       *
 320       * @dataProvider get_matching_tours_provider
 321       * @param   array   $alltours   The list of tours to insert.
 322       * @param   string  $url        The URL to test.
 323       * @param   array   $expected   List of names of the expected matching tours.
 324       */
 325      public function test_get_matching_tours(array $alltours, string $url, array $expected) {
 326          $this->resetAfterTest();
 327  
 328          $this->setGuestUser();
 329  
 330          foreach ($alltours as $tourconfig) {
 331              $tour = $this->helper_create_tour((object) $tourconfig);
 332              $this->helper_create_step((object) ['tourid' => $tour->get_id()]);
 333          }
 334  
 335          $matches = \tool_usertours\manager::get_matching_tours(new moodle_url($url));
 336          $this->assertEquals(count($expected), count($matches));
 337          for ($i = 0; $i < count($matches); $i++) {
 338              $this->assertEquals($expected[$i], $matches[$i]->get_name());
 339          }
 340      }
 341  
 342      /**
 343       * Test that no matching tours are returned if there is pending site policy agreement.
 344       */
 345      public function test_get_matching_tours_for_user_without_site_policy_agreed() {
 346          global $CFG;
 347  
 348          $this->resetAfterTest();
 349          $this->setGuestUser();
 350  
 351          $tour = $this->helper_create_tour((object) [
 352              'pathmatch' => '/%',
 353              'enabled' => true,
 354              'name' => 'Test tour',
 355              'description' => '',
 356              'configdata' => '',
 357          ]);
 358  
 359          $this->helper_create_step((object) [
 360              'tourid' => $tour->get_id(),
 361          ]);
 362  
 363          $matches = \tool_usertours\manager::get_matching_tours(new moodle_url('/'));
 364          $this->assertEquals(1, count($matches));
 365  
 366          $CFG->sitepolicyguest = 'https://example.com';
 367  
 368          $matches = \tool_usertours\manager::get_matching_tours(new moodle_url('/'));
 369          $this->assertEmpty($matches);
 370      }
 371  }