Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 39 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  namespace tool_usertours;
  18  
  19  use tool_usertours\local\filter\accessdate;
  20  
  21  /**
  22   * Tests for time filter.
  23   *
  24   * @package    tool_usertours
  25   * @copyright  2019 Tom Dickman <tomdickman@catalyst-au.net>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class accessdate_filter_test extends \advanced_testcase {
  29  
  30      public function setUp(): void {
  31          $this->resetAfterTest(true);
  32      }
  33  
  34      /**
  35       * Data Provider for filter_matches method.
  36       *
  37       * @return array
  38       */
  39      public function filter_matches_provider() {
  40          return [
  41              'No config set; Matches' => [
  42                  [],
  43                  [],
  44                  true,
  45              ],
  46              'Filter is not enabled; Match' => [
  47                  ['filter_accessdate' => accessdate::FILTER_ACCOUNT_CREATION, 'filter_accessdate_range' => 90 * DAYSECS,
  48                      'filter_accessdate_enabled' => 0],
  49                  ['timecreated' => time() - (89 * DAYSECS)],
  50                  true,
  51              ],
  52              'Filter is not enabled (tour would not be displayed if it was); Match' => [
  53                  ['filter_accessdate' => accessdate::FILTER_ACCOUNT_CREATION, 'filter_accessdate_range' => 90 * DAYSECS,
  54                      'filter_accessdate_enabled' => 0],
  55                  ['timecreated' => time() - (91 * DAYSECS)],
  56                  true,
  57              ],
  58              'Inside range of account creation date; Match' => [
  59                  ['filter_accessdate' => accessdate::FILTER_ACCOUNT_CREATION, 'filter_accessdate_range' => 90 * DAYSECS,
  60                      'filter_accessdate_enabled' => 1],
  61                  ['timecreated' => time() - (89 * DAYSECS)],
  62                  true,
  63              ],
  64              'Outside range of account creation date; No match' => [
  65                  ['filter_accessdate' => accessdate::FILTER_ACCOUNT_CREATION, 'filter_accessdate_range' => 90 * DAYSECS,
  66                      'filter_accessdate_enabled' => 1],
  67                  ['timecreated' => time() - (91 * DAYSECS)],
  68                  false,
  69              ],
  70              'Inside range of first login date; Match' => [
  71                  ['filter_accessdate' => accessdate::FILTER_FIRST_LOGIN, 'filter_accessdate_range' => 90 * DAYSECS,
  72                      'filter_accessdate_enabled' => 1],
  73                  ['firstaccess' => time() - (89 * DAYSECS)],
  74                  true,
  75              ],
  76              'Outside range of first login date; No match' => [
  77                  ['filter_accessdate' => accessdate::FILTER_FIRST_LOGIN, 'filter_accessdate_range' => 90 * DAYSECS,
  78                      'filter_accessdate_enabled' => 1],
  79                  ['firstaccess' => time() - (91 * DAYSECS)],
  80                  false,
  81              ],
  82              'Inside range of last login date; Match' => [
  83                  ['filter_accessdate' => accessdate::FILTER_LAST_LOGIN, 'filter_accessdate_range' => 90 * DAYSECS,
  84                      'filter_accessdate_enabled' => 1],
  85                  ['lastlogin' => time() - (89 * DAYSECS)],
  86                  true,
  87              ],
  88              'Outside range of last login date; No match' => [
  89                  ['filter_accessdate' => accessdate::FILTER_LAST_LOGIN, 'filter_accessdate_range' => 90 * DAYSECS,
  90                      'filter_accessdate_enabled' => 1],
  91                  ['lastlogin' => time() - (91 * DAYSECS)],
  92                  false,
  93              ],
  94              'User has never logged in, but tour should be visible; Match' => [
  95                  ['filter_accessdate' => accessdate::FILTER_LAST_LOGIN, 'filter_accessdate_range' => 90 * DAYSECS,
  96                      'filter_accessdate_enabled' => 1],
  97                  ['lastlogin' => 0, 'timecreated' => time() - (89 * DAYSECS)],
  98                  true,
  99              ],
 100              'User has never logged in, and tour should not be visible; No match' => [
 101                  ['filter_accessdate' => accessdate::FILTER_LAST_LOGIN, 'filter_accessdate_range' => 90 * DAYSECS,
 102                      'filter_accessdate_enabled' => 1],
 103                  ['lastlogin' => 0, 'timecreated' => time() - (91 * DAYSECS)],
 104                  false,
 105              ],
 106          ];
 107      }
 108  
 109      /**
 110       * Test filter matches.
 111       *
 112       * @dataProvider    filter_matches_provider
 113       *
 114       * @param array $filtervalues the filter values set.
 115       * @param array $userstate any user state required for test.
 116       * @param bool $expected result expected.
 117       */
 118      public function test_filter_matches($filtervalues, $userstate, $expected) {
 119          $course = $this->getDataGenerator()->create_course();
 120          $context = \context_course::instance($course->id);
 121  
 122          $user = $this->getDataGenerator()->create_user($userstate);
 123          $this->setUser($user);
 124  
 125          $tour = new tour();
 126          $tour->set_filter_values('accessdate', $filtervalues);
 127  
 128          $this->assertEquals($expected, accessdate::filter_matches($tour, $context));
 129      }
 130  
 131  }