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 310 and 311] [Versions 39 and 311]

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