Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 mod_lti\task;
  18  
  19  /**
  20   * Tests cleaning up the access tokens task.
  21   *
  22   * @package mod_lti
  23   * @category test
  24   * @copyright 2019 Mark Nelson <markn@moodle.com>
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class clean_access_tokens_test extends \advanced_testcase {
  28  
  29      /**
  30       * Test set up.
  31       *
  32       * This is executed before running any test in this file.
  33       */
  34      public function setUp(): void {
  35          $this->resetAfterTest();
  36      }
  37  
  38      /**
  39       * Test the cleanup task.
  40       */
  41      public function test_cleanup_task() {
  42          global $DB;
  43  
  44          $time = time();
  45  
  46          // Create an expired access token.
  47          $token = new \stdClass();
  48          $token->typeid = 1;
  49          $token->scope = 'scope';
  50          $token->token = 'token';
  51          $token->validuntil = $time - DAYSECS;
  52          $token->timecreated = $time - DAYSECS;
  53  
  54          $t1id = $DB->insert_record('lti_access_tokens', $token);
  55  
  56          // New token, in the future.
  57          $token->validuntil = $time + DAYSECS;
  58  
  59          $token->token = 'token2';
  60          $t2id = $DB->insert_record('lti_access_tokens', $token);
  61  
  62          // Run the task.
  63          $task = new clean_access_tokens();
  64          $task->execute();
  65  
  66          // Check there is only one token now.
  67          $tokens = $DB->get_records('lti_access_tokens');
  68  
  69          $this->assertCount(1, $tokens);
  70  
  71          $token = reset($tokens);
  72  
  73          $this->assertEquals($t2id, $token->id);
  74      }
  75  }