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
declare(strict_types=1);
18
 
19
namespace core_admin\external;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
25
 
26
/**
27
 * Unit tests to test block protection changes.
28
 *
29
 * @package     core
30
 * @covers      \core_admin\external\set_block_protection
31
 * @copyright   2023 Andrew Lyons <andrew@nicols.co.uk>
32
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
class set_block_protection_test extends \externallib_advanced_testcase {
35
    /**
36
     * Test execute method with no login.
37
     */
38
    public function test_execute_no_login(): void {
39
        $this->expectException(\require_login_exception::class);
40
        set_block_protection::execute('block_login', 1);
41
    }
42
 
43
    /**
44
     * Test execute method with no login.
45
     */
46
    public function test_execute_no_capability(): void {
47
        $this->resetAfterTest();
48
        $user = $this->getDataGenerator()->create_user();
49
        $this->setUser($user);
50
        $this->expectException(\required_capability_exception::class);
51
        set_block_protection::execute('block_login', 1);
52
    }
53
 
54
    /**
55
     * Test the execute function with a range of parameters.
56
     *
57
     * @dataProvider execute_provider
58
     * @param string $block
59
     * @param int $targetstate
60
     * @param bool $isundeletable
61
     */
62
    public function test_execute(
63
        string $block,
64
        int $targetstate,
65
        bool $isundeletable,
66
    ): void {
67
        $this->resetAfterTest();
68
        $this->setAdminUser();
69
 
70
        set_block_protection::execute($block, $targetstate);
71
 
72
        $undeletable = \block_manager::get_undeletable_block_types();
73
        [, $pluginname] = explode('_', $block, 2);
74
 
75
        if ($isundeletable) {
76
            $this->assertNotFalse(array_search($pluginname, $undeletable));
77
        } else {
78
            $this->assertFalse(array_search($pluginname, $undeletable));
79
        }
80
        $this->assertCount(1, \core\notification::fetch());
81
    }
82
 
83
    /**
84
     * Data provider for test_execute.
85
     *
86
     * @return array
87
     */
88
    public function execute_provider(): array {
89
        return [
90
            [
91
                'block_login',
92
                1,
93
                true,
94
            ],
95
            [
96
                'block_login',
97
                0,
98
                false,
99
            ],
100
        ];
101
    }
102
 
103
    /**
104
     * Assert that an exception is thrown when the block does not exist.
105
     */
106
    public function execute_block_does_not_exist(): void {
107
        $this->expectException(\dml_missing_record_exception::class);
108
 
109
        set_block_protection::execute('fake_block', 1);
110
        $this->assertDebuggingCalledCount(1);
111
    }
112
}