Proyectos de Subversion Moodle

Rev

| 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 report_themeusage;
18
 
19
use testing_data_generator;
20
use core\output\theme_usage;
21
 
22
/**
23
 * Unit tests for theme usage.
24
 *
25
 * @package    report_themeusage
26
 * @copyright  2023 David Woloszyn <david.woloszyn@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class theme_usage_test extends \advanced_testcase {
30
 
31
    /** @var testing_data_generator Data generator. */
32
    private testing_data_generator $generator;
33
 
34
    /**
35
     * Set up function for tests.
36
     */
37
    protected function setUp(): void {
38
        parent::setUp();
39
 
40
        $this->resetAfterTest();
41
        $this->generator = $this->getDataGenerator();
42
    }
43
 
44
    /**
45
     * Test is_theme_used_in_any_context method.
46
     *
47
     * @covers ::is_theme_used_in_any_context
48
     * @covers ::theme_purge_used_in_context_caches
49
     */
50
    public function test_is_theme_used_in_any_context(): void {
51
        // Enable theme overrides.
52
        set_config('allowuserthemes', 1);
53
        set_config('allowcoursethemes', 1);
54
        set_config('allowcohortthemes', 1);
55
        set_config('allowcategorythemes', 1);
56
 
57
        $theme = 'boost';
58
 
59
        // Check there are no contexts using 'boost' as their preferred theme yet.
60
        $usedinanycontext = theme_usage::is_theme_used_in_any_context($theme);
61
        $this->assertEquals(theme_usage::THEME_IS_NOT_USED, $usedinanycontext);
62
 
63
        // Create a user and set its theme preference to 'boost'.
64
        // The outcome of this test should be the same if we use a cohort/course/category.
65
        $this->generator->create_user(['theme' => $theme]);
66
 
67
        // Because we have already checked and cached a response, purge this cache.
68
        theme_purge_used_in_context_caches();
69
 
70
        $usedinanycontext = theme_usage::is_theme_used_in_any_context($theme);
71
        $this->assertEquals(theme_usage::THEME_IS_USED, $usedinanycontext);
72
 
73
        // Double-check the the cache is set for the theme.
74
        $cache = \cache::make('core', 'theme_usedincontext')->get($theme);
75
        $this->assertEquals(theme_usage::THEME_IS_USED, $cache);
76
    }
77
 
78
    /**
79
     * Test the deleting of cache using theme_delete_used_in_context_cache.
80
     *
81
     * @covers ::theme_delete_used_in_context_cache
82
     */
83
    public function test_theme_delete_used_in_context_cache(): void {
84
        // Enable theme override.
85
        set_config('allowuserthemes', 1);
86
 
87
        // Create a user and set its theme preference to 'boost'.
88
        $theme = 'boost';
89
        $user = $this->generator->create_user(['theme' => $theme]);
90
 
91
        // Check for theme usage. This will create a cached result.
92
        theme_usage::is_theme_used_in_any_context($theme);
93
        $cache = \cache::make('core', 'theme_usedincontext')->get($theme);
94
        $this->assertEquals(theme_usage::THEME_IS_USED, $cache);
95
 
96
        // Delete the cache by switching themes.
97
        theme_delete_used_in_context_cache('classic', $user->theme);
98
        $cache = \cache::make('core', 'theme_usedincontext')->get($theme);
99
        $this->assertFalse($cache);
100
    }
101
}