Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 block_recentlyaccesseditems;
18
 
19
use externallib_advanced_testcase;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
 
25
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
26
 
27
/**
28
 * Test Recently accessed items block external functions
29
 *
30
 * @package    block_recentlyaccesseditems
31
 * @category   external
32
 * @copyright  2018 Victor Deniz <victor@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @since      Moodle 3.6
35
 */
36
class externallib_test extends externallib_advanced_testcase {
37
 
38
    /**
39
     * Test the get_recent_items function.
40
     */
11 efrain 41
    public function test_get_recent_items(): void {
1 efrain 42
 
43
        $this->resetAfterTest();
44
        $generator = $this->getDataGenerator();
45
        $this->setAdminUser();
46
 
47
        // Add courses.
48
        $courses = array();
49
        for ($i = 1; $i < 4; $i++) {
50
            $courses[] = $generator->create_course();
51
        };
52
 
53
        // Add users.
54
        $student = $generator->create_user();
55
        $teacher = $generator->create_user();
56
 
57
        // Enrol users and add items to courses.
58
        foreach ($courses as $course) {
59
            $generator->enrol_user($student->id, $course->id, 'student');
60
            $forum[] = $this->getDataGenerator()->create_module('forum', array('course' => $course));
61
            $glossary[] = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
62
            $assign[] = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
63
            $h5pactivity[] = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
64
        }
65
        $generator->enrol_user($teacher->id, $courses[0]->id, 'teacher');
66
 
67
        $this->setUser($student);
68
 
69
        // No recent items.
70
        $result = \block_recentlyaccesseditems\external::get_recent_items();
71
        $this->assertCount(0, $result);
72
 
73
        // Student access all forums.
74
        foreach ($forum as $module) {
75
            $event = \mod_forum\event\course_module_viewed::create(array('context' => \context_module::instance($module->cmid),
76
                    'objectid' => $module->id));
77
            $event->trigger();
78
            $this->waitForSecond();
79
        }
80
 
81
        // Test that only access to forums are returned.
82
        $result = \block_recentlyaccesseditems\external::get_recent_items();
83
        $this->assertCount(count($forum), $result);
84
 
85
        // Student access all assignments.
86
        foreach ($assign as $module) {
87
            $event = \mod_chat\event\course_module_viewed::create(array('context' => \context_module::instance($module->cmid),
88
                    'objectid' => $module->id));
89
            $event->trigger();
90
            $this->waitForSecond();
91
        }
92
 
93
        // Student access all h5p.
94
        foreach ($h5pactivity as $module) {
95
            $event = \mod_h5pactivity\event\course_module_viewed::create(
96
                ['context' => \context_module::instance($module->cmid), 'objectid' => $module->id]
97
            );
98
            $event->trigger();
99
            $this->waitForSecond();
100
        }
101
 
102
        // Test that results are sorted by timeaccess DESC (default).
103
        $result = \block_recentlyaccesseditems\external::get_recent_items();
104
        $this->assertCount((count($forum) + count($assign) + count($h5pactivity)), $result);
105
        foreach ($result as $key => $record) {
106
            if ($key == 0) {
107
                continue;
108
            }
109
            $this->assertTrue($record->timeaccess < $result[$key - 1]->timeaccess);
110
            // Check that the branded property is set correctly.
111
            if ($record->modname == 'h5pactivity') {
112
                $this->assertTrue($record->branded);
113
            } else {
114
                $this->assertFalse($record->branded);
115
            }
116
        }
117
 
118
        // Delete a course and confirm it's activities don't get returned.
119
        delete_course($courses[0], false);
120
        $result = \block_recentlyaccesseditems\external::get_recent_items();
121
        $this->assertCount((count($forum) + count($assign) + count($h5pactivity)) - 3, $result);
122
 
123
        // Delete a single course module should still return.
124
        course_delete_module($forum[1]->cmid);
125
        $result = \block_recentlyaccesseditems\external::get_recent_items();
126
        $this->assertCount((count($forum) + count($assign) + count($h5pactivity)) - 4, $result);
127
    }
128
}