Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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
/**
18
 * Contains the test class for the caching_content_item_readonly_repository class.
19
 *
20
 * @package    core
21
 * @subpackage course
22
 * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace core_course;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use core_course\local\repository\content_item_readonly_repository;
30
use core_course\local\repository\caching_content_item_readonly_repository;
31
 
32
/**
33
 * The test class for the caching_content_item_readonly_repository class.
34
 *
35
 * @copyright  2020 Jake Dallimore <jrhdallimore@gmail.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
38
class caching_content_item_readonly_repository_test extends \advanced_testcase {
39
    /**
40
     * Test verifying that content items are cached and returned from the cache in subsequent same-request calls.
41
     */
42
    public function test_find_all_for_course() {
43
        $this->resetAfterTest();
44
        global $DB;
45
 
46
        $course = $this->getDataGenerator()->create_course();
47
        $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
48
        $cir = new content_item_readonly_repository();
49
        $ccir = new caching_content_item_readonly_repository(\cache::make('core', 'user_course_content_items'), $cir);
50
 
51
        // Get the content items using both the live and the caching repos.
52
        $items = $cir->find_all_for_course($course, $user);
53
        $cacheditems = $ccir->find_all_for_course($course, $user);
54
        $itemsfiltered = array_values(array_filter($items, function($item) {
55
            return $item->get_component_name() == 'mod_book';
56
        }));
57
        $cacheditemsfiltered = array_values(array_filter($cacheditems, function($item) {
58
            return $item->get_component_name() == 'mod_book';
59
        }));
60
 
61
        // Verify the book module is in both result sets.
62
        $module = $DB->get_record('modules', ['name' => 'book']);
63
        $this->assertEquals($module->name, $itemsfiltered[0]->get_name());
64
        $this->assertEquals($module->name, $cacheditemsfiltered[0]->get_name());
65
 
66
        // Hide a module and get the content items again.
67
        $DB->set_field("modules", "visible", "0", ["id" => $module->id]);
68
        $items = $cir->find_all_for_course($course, $user);
69
        $cacheditems = $ccir->find_all_for_course($course, $user);
70
        $itemsfiltered = array_values(array_filter($items, function($item) {
71
            return $item->get_component_name() == 'mod_book';
72
        }));
73
        $cacheditemsfiltered = array_values(array_filter($cacheditems, function($item) {
74
            return $item->get_component_name() == 'mod_book';
75
        }));
76
 
77
        // The caching repo should return the same list, while the live repo will return the updated list.
78
        $this->assertEquals($module->name, $cacheditemsfiltered[0]->get_name());
79
        $this->assertEmpty($itemsfiltered);
80
    }
81
}