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   * Tests cleaning up the access tokens task.
  19   *
  20   * @package mod_lti
  21   * @category test
  22   * @copyright 2019 Mark Nelson <markn@moodle.com>
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Tests cleaning up the access tokens task.
  30   *
  31   * @package mod_lti
  32   * @category test
  33   * @copyright 2019 Mark Nelson <markn@moodle.com>
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class mod_lti_clean_access_tokens_testcase extends advanced_testcase {
  37  
  38      /**
  39       * Test set up.
  40       *
  41       * This is executed before running any test in this file.
  42       */
  43      public function setUp(): void {
  44          $this->resetAfterTest();
  45      }
  46  
  47      /**
  48       * Test the cleanup task.
  49       */
  50      public function test_cleanup_task() {
  51          global $DB;
  52  
  53          $time = time();
  54  
  55          // Create an expired access token.
  56          $token = new stdClass();
  57          $token->typeid = 1;
  58          $token->scope = 'scope';
  59          $token->token = 'token';
  60          $token->validuntil = $time - DAYSECS;
  61          $token->timecreated = $time - DAYSECS;
  62  
  63          $t1id = $DB->insert_record('lti_access_tokens', $token);
  64  
  65          // New token, in the future.
  66          $token->validuntil = $time + DAYSECS;
  67  
  68          $token->token = 'token2';
  69          $t2id = $DB->insert_record('lti_access_tokens', $token);
  70  
  71          // Run the task.
  72          $task = new \mod_lti\task\clean_access_tokens();
  73          $task->execute();
  74  
  75          // Check there is only one token now.
  76          $tokens = $DB->get_records('lti_access_tokens');
  77  
  78          $this->assertCount(1, $tokens);
  79  
  80          $token = reset($tokens);
  81  
  82          $this->assertEquals($t2id, $token->id);
  83      }
  84  }