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
// Note: Technically this namespace is incorrect, but we should be moving to namespace things and core anyway.
18
namespace core;
19
 
20
/**
21
 * Unit tests specifically for context_block.
22
 *
23
 * @package   core
24
 * @category  phpunit
25
 * @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>
26
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @coversDefaultClass \context_block
28
 */
1441 ariadna 29
final class context_block_test extends \advanced_testcase {
1 efrain 30
 
31
    /**
32
     * Test setup.
33
     */
34
    public function setUp(): void {
35
        global $CFG;
36
        require_once("{$CFG->libdir}/accesslib.php");
1441 ariadna 37
        parent::setUp();
1 efrain 38
    }
39
 
40
    /**
41
     * Ensure that block contexts are correctly created for blocks where they are missing.
42
     *
43
     * @covers ::create_level_instances
44
     */
45
    public function test_context_creation(): void {
46
        global $DB;
47
 
48
        $this->resetAfterTest();
49
 
50
        // Create some parent contexts.
51
        $generator = $this->getDataGenerator();
52
        $user = $generator->create_user();
53
        $coursecat = $generator->create_category();
54
        $course = $generator->create_course(['category' => $coursecat->id]);
55
        $activity = $generator->create_module('forum', ['course' => $course->id]);
56
 
57
        $contextlist = [
58
            \context_system::instance(),
59
            \context_user::instance($user->id),
60
            \context_coursecat::instance($coursecat->id),
61
            \context_course::instance($course->id),
62
            \context_module::instance($activity->cmid),
63
        ];
64
 
65
        // Create a number of blocks of different types in the DB only.
66
        // This is typically seen when creating large numbers in an upgrade script.
67
        $blocks = [];
68
        for ($i = 0; $i < 10; $i++) {
69
            foreach ($contextlist as $context) {
70
                $blocks[] = $DB->insert_record('block_instances', [
71
                    'blockname' => 'calendar_month',
72
                    'parentcontextid' => $context->id,
73
                    'showinsubcontexts' => 1,
74
                    'requiredbytheme' => 0,
75
                    'pagetypepattern' => 'my-index',
76
                    'subpagepattern' => 1,
77
                    'defaultregion' => 'content',
78
                    'defaultweight' => 1,
79
                    'timecreated' => time(),
80
                    'timemodified' => time(),
81
                ]);
82
            }
83
        }
84
 
85
        // Test data created. Call \context_helper::create_instances() which will create the records, and fix the paths.
86
        \context_helper::create_instances(CONTEXT_BLOCK);
87
 
88
        foreach ($blocks as $blockid) {
89
            $block = $DB->get_record('block_instances', ['id' => $blockid]);
90
            $context = \context_block::instance($block->id);
91
            $this->assertInstanceOf(\context_block::class, $context);
92
 
93
            // Note. There is no point checking the instanceid because the context was fetched using this.
94
 
95
            // Ensure that the contextlevel is correct.
96
            $this->assertEquals(CONTEXT_BLOCK, $context->contextlevel);
97
 
98
            // Fetch the parent context.
99
            $parentcontext = $context->get_parent_context();
100
 
101
            // This hsould match the parent context specified in the block instance configuration.
102
            $this->assertEquals($block->parentcontextid, $parentcontext->id);
103
 
104
            // Ensure that the path and depth are correctly specified.
105
            $this->assertEquals($parentcontext->path . "/{$context->id}", $context->path);
106
            $this->assertEquals($parentcontext->depth + 1, $context->depth);
107
        }
108
    }
109
}