Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 */
1441 ariadna 27
final class clean_access_tokens_test extends \advanced_testcase {
1 efrain 28
 
29
    /**
30
     * Test set up.
31
     *
32
     * This is executed before running any test in this file.
33
     */
34
    public function setUp(): void {
1441 ariadna 35
        parent::setUp();
1 efrain 36
        $this->resetAfterTest();
37
    }
38
 
39
    /**
40
     * Test the cleanup task.
41
     */
11 efrain 42
    public function test_cleanup_task(): void {
1 efrain 43
        global $DB;
44
 
45
        $time = time();
46
 
47
        // Create an expired access token.
48
        $token = new \stdClass();
49
        $token->typeid = 1;
50
        $token->scope = 'scope';
51
        $token->token = 'token';
52
        $token->validuntil = $time - DAYSECS;
53
        $token->timecreated = $time - DAYSECS;
54
 
55
        $t1id = $DB->insert_record('lti_access_tokens', $token);
56
 
57
        // New token, in the future.
58
        $token->validuntil = $time + DAYSECS;
59
 
60
        $token->token = 'token2';
61
        $t2id = $DB->insert_record('lti_access_tokens', $token);
62
 
63
        // Run the task.
64
        $task = new clean_access_tokens();
65
        $task->execute();
66
 
67
        // Check there is only one token now.
68
        $tokens = $DB->get_records('lti_access_tokens');
69
 
70
        $this->assertCount(1, $tokens);
71
 
72
        $token = reset($tokens);
73
 
74
        $this->assertEquals($t2id, $token->id);
75
    }
76
}