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