weekly release 2.2dev
[moodle.git] / lib / simpletest / testblocklib_blockmanager.php
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // NOTICE OF COPYRIGHT                                                   //
6 //                                                                       //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
8 //          http://moodle.org                                            //
9 //                                                                       //
10 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
11 //                                                                       //
12 // This program is free software; you can redistribute it and/or modify  //
13 // it under the terms of the GNU General Public License as published by  //
14 // the Free Software Foundation; either version 2 of the License, or     //
15 // (at your option) any later version.                                   //
16 //                                                                       //
17 // This program is distributed in the hope that it will be useful,       //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
20 // GNU General Public License for more details:                          //
21 //                                                                       //
22 //          http://www.gnu.org/copyleft/gpl.html                         //
23 //                                                                       //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27  * Tests for the block_manager class in ../blocklib.php.
28  *
29  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
30  * @package moodlecore
31  */
33 if (!defined('MOODLE_INTERNAL')) {
34     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
35 }
37 require_once($CFG->libdir . '/pagelib.php');
38 require_once($CFG->libdir . '/blocklib.php');
40 /** Test-specific subclass to make some protected things public. */
41 class testable_block_manager extends block_manager {
43     public function mark_loaded() {
44         $this->birecordsbyregion = array();
45     }
46     public function get_loaded_blocks() {
47         return $this->birecordsbyregion;
48     }
49 }
50 class block_ablocktype extends block_base {
51     public function init() {
52     }
53 }
55 /**
56  * Test functions that don't need to touch the database.
57  */
58 class moodle_block_manager_test extends UnitTestCase {
59     public  static $includecoverage = array('lib/pagelib.php', 'lib/blocklib.php');
60     protected $testpage;
61     protected $blockmanager;
63     public function setUp() {
64         $this->testpage = new moodle_page();
65         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
66         $this->blockmanager = new testable_block_manager($this->testpage);
67     }
69     public function tearDown() {
70         $this->testpage = NULL;
71         $this->blockmanager = NULL;
72     }
74     public function test_no_regions_initially() {
75         // Exercise SUT & Validate
76         $this->assertEqual(array(), $this->blockmanager->get_regions());
77     }
79     public function test_add_region() {
80         // Exercise SUT.
81         $this->blockmanager->add_region('a-region-name');
82         // Validate
83         $this->assertEqual(array('a-region-name'), $this->blockmanager->get_regions());
84     }
86     public function test_add_regions() {
87         // Set up fixture.
88         $regions = array('a-region', 'another-region');
89         // Exercise SUT.
90         $this->blockmanager->add_regions($regions);
91         // Validate
92         $this->assert(new ArraysHaveSameValuesExpectation($regions), $this->blockmanager->get_regions());
93     }
95     public function test_add_region_twice() {
96         // Exercise SUT.
97         $this->blockmanager->add_region('a-region-name');
98         $this->blockmanager->add_region('another-region');
99         // Validate
100         $this->assert(new ArraysHaveSameValuesExpectation(array('a-region-name', 'another-region')),
101                 $this->blockmanager->get_regions());
102     }
104     public function test_cannot_add_region_after_loaded() {
105         // Set up fixture.
106         $this->blockmanager->mark_loaded();
107         // Set expectation
108         $this->expectException();
109         // Exercise SUT.
110         $this->blockmanager->add_region('too-late');
111     }
113     public function test_set_default_region() {
114         // Set up fixture.
115         $this->blockmanager->add_region('a-region-name');
116         // Exercise SUT.
117         $this->blockmanager->set_default_region('a-region-name');
118         // Validate
119         $this->assertEqual('a-region-name', $this->blockmanager->get_default_region());
120     }
122     public function test_cannot_set_unknown_region_as_default() {
123         // Set expectation
124         $this->expectException();
125         // Exercise SUT.
126         $this->blockmanager->set_default_region('a-region-name');
127     }
129     public function test_cannot_change_default_region_after_loaded() {
130         // Set up fixture.
131         $this->blockmanager->mark_loaded();
132         // Set expectation
133         $this->expectException();
134         // Exercise SUT.
135         $this->blockmanager->set_default_region('too-late');
136     }
138     public function test_matching_page_type_patterns() {
139         $this->assert(new ArraysHaveSameValuesExpectation(
140                 array('site-index', 'site-index-*', 'site-*', '*')),
141                 matching_page_type_patterns('site-index'));
143         $this->assert(new ArraysHaveSameValuesExpectation(
144                 array('mod-quiz-report-overview', 'mod-quiz-report-overview-*', 'mod-quiz-report-*', 'mod-quiz-*', 'mod-*', '*')),
145                 matching_page_type_patterns('mod-quiz-report-overview'));
147         $this->assert(new ArraysHaveSameValuesExpectation(
148                 array('mod-forum-view', 'mod-*-view', 'mod-forum-view-*', 'mod-forum-*', 'mod-*', '*')),
149                 matching_page_type_patterns('mod-forum-view'));
151         $this->assert(new ArraysHaveSameValuesExpectation(
152                 array('mod-forum-index', 'mod-*-index', 'mod-forum-index-*', 'mod-forum-*', 'mod-*', '*')),
153                 matching_page_type_patterns('mod-forum-index'));
154     }
157 /**
158  * Test methods that load and save data from block_instances and block_positions.
159  */
160 class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase {
162     public function setUp() {
163         parent::setUp();
164         $this->create_test_tables(array('block', 'block_instances', 'block_positions', 'context'), 'lib');
165         $this->switch_to_test_db();
166     }
168     public function tearDown() {
169         parent::tearDown();
170     }
172     /**
173      * Saves the context in the DB, setting $contextid.
174      * @param $context. Context. Path should be set to /parent/path/, that is with a traling /.
175      * This context's id will be appended.
176      */
177     protected function insert_context_in_db($context) {
178         $context->id = $this->testdb->insert_record('context', $context);
179         $context->path .= $context->id;
180         $this->testdb->set_field('context', 'path', $context->path, array('id' => $context->id));
181     }
183     protected function get_a_page_and_block_manager($regions, $context, $pagetype, $subpage = '') {
184         $page = new moodle_page;
185         $page->set_context($context);
186         $page->set_pagetype($pagetype);
187         $page->set_subpage($subpage);
189         $blockmanager = new testable_block_manager($page);
190         $blockmanager->add_regions($regions);
191         $blockmanager->set_default_region($regions[0]);
193         return array($page, $blockmanager);
194     }
196     protected function get_a_known_block_type() {
197         global $DB;
198         $block = new stdClass;
199         $block->name = 'ablocktype';
200         $this->testdb->insert_record('block', $block);
201         return $block->name;
202     }
204     protected function assertContainsBlocksOfType($typearray, $blockarray) {
205         if (!$this->assertEqual(count($typearray), count($blockarray), "Blocks array contains the wrong number of elements %s.")) {
206             return;
207         }
208         $types = array_values($typearray);
209         $i = 0;
210         foreach ($blockarray as $block) {
211             $blocktype = $types[$i];
212             $this->assertEqual($blocktype, $block->name(), "Block types do not match at postition $i %s.");
213             $i++;
214         }
215     }
217     public function test_empty_initially() {
218         // Set up fixture.
219         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array('a-region'),
220                 get_context_instance(CONTEXT_SYSTEM), 'page-type');
221         // Exercise SUT.
222         $blockmanager->load_blocks();
223         // Validate.
224         $blocks = $blockmanager->get_loaded_blocks();
225         $this->assertEqual(array('a-region' => array()), $blocks);
226     }
228     public function test_adding_and_retrieving_one_block() {
229         // Set up fixture.
230         $regionname = 'a-region';
231         $blockname = $this->get_a_known_block_type();
232         $context = get_context_instance(CONTEXT_SYSTEM);
233         $context->path = '/';
234         $this->insert_context_in_db($context);
236         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
237                 $context, 'page-type');
239         // Exercise SUT.
240         $blockmanager->add_block($blockname, $regionname, 0, false);
241         $blockmanager->load_blocks();
242         // Validate.
243         $blocks = $blockmanager->get_blocks_for_region($regionname);
244         $this->assertContainsBlocksOfType(array($blockname), $blocks);
245     }
247     public function test_adding_and_retrieving_two_blocks() {
248         // Set up fixture.
249         $regionname = 'a-region';
250         $blockname = $this->get_a_known_block_type();
251         $context = get_context_instance(CONTEXT_SYSTEM);
252         $context->path = '/';
253         $this->insert_context_in_db($context);
255         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
256                 $context, 'page-type');
258         // Exercise SUT.
259         $blockmanager->add_block($blockname, $regionname, 0, false);
260         $blockmanager->add_block($blockname, $regionname, 1, false);
261         $blockmanager->load_blocks();
262         // Validate.
263         $blocks = $blockmanager->get_blocks_for_region($regionname);
264         $this->assertContainsBlocksOfType(array($blockname, $blockname), $blocks);
265     }
267     public function test_block_not_included_in_different_context() {
268         // Set up fixture.
269         $syscontext = get_context_instance(CONTEXT_SYSTEM);
270         $syscontext->path = '/';
271         $this->insert_context_in_db($syscontext);
272         $fakecontext = new stdClass;
273         $fakecontext->contextlevel = CONTEXT_COURSECAT;
274         $fakecontext->path = $syscontext->path . '/';
275         $this->insert_context_in_db($fakecontext);
276         $regionname = 'a-region';
277         $blockname = $this->get_a_known_block_type();
279         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $fakecontext, 'page-type');
280         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
282         $addbm->add_block($blockname, $regionname, 0, false);
284         // Exercise SUT.
285         $viewbm->load_blocks();
286         // Validate.
287         $blocks = $viewbm->get_blocks_for_region($regionname);
288         $this->assertContainsBlocksOfType(array(), $blocks);
289     }
291     public function test_block_included_in_sub_context() {
292         // Set up fixture.
293         $syscontext = get_context_instance(CONTEXT_SYSTEM);
294         $syscontext->path = '/';
295         $this->insert_context_in_db($syscontext);
296         $childcontext = new stdClass;
297         $childcontext->contextlevel = CONTEXT_COURSECAT;
298         $childcontext->path = $syscontext->path . '/';
299         $this->insert_context_in_db($childcontext);
300         $regionname = 'a-region';
301         $blockname = $this->get_a_known_block_type();
303         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
304         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $childcontext, 'page-type');
306         $addbm->add_block($blockname, $regionname, 0, true);
308         // Exercise SUT.
309         $viewbm->load_blocks();
310         // Validate.
311         $blocks = $viewbm->get_blocks_for_region($regionname);
312         $this->assertContainsBlocksOfType(array($blockname), $blocks);
313     }
315     public function test_block_not_included_on_different_page_type() {
316         // Set up fixture.
317         $syscontext = get_context_instance(CONTEXT_SYSTEM);
318         $syscontext->path = '/';
319         $this->insert_context_in_db($syscontext);
320         $regionname = 'a-region';
321         $blockname = $this->get_a_known_block_type();
323         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
324         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'other-page-type');
326         $addbm->add_block($blockname, $regionname, 0, true);
328         // Exercise SUT.
329         $viewbm->load_blocks();
330         // Validate.
331         $blocks = $viewbm->get_blocks_for_region($regionname);
332         $this->assertContainsBlocksOfType(array(), $blocks);
333     }
335     public function test_block_not_included_on_different_sub_page() {
336         // Set up fixture.
337         $regionname = 'a-region';
338         $blockname = $this->get_a_known_block_type();
339         $syscontext = get_context_instance(CONTEXT_SYSTEM);
340         $syscontext->path = '/';
341         $this->insert_context_in_db($syscontext);
343         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
344                 $syscontext, 'page-type', 'sub-page');
346         $blockmanager->add_block($blockname, $regionname, 0, true, $page->pagetype, 'other-sub-page');
348         // Exercise SUT.
349         $blockmanager->load_blocks();
350         // Validate.
351         $blocks = $blockmanager->get_blocks_for_region($regionname);
352         $this->assertContainsBlocksOfType(array(), $blocks);
353     }
355     public function test_block_included_with_explicit_sub_page() {
356         // Set up fixture.
357         $regionname = 'a-region';
358         $blockname = $this->get_a_known_block_type();
359         $syscontext = get_context_instance(CONTEXT_SYSTEM);
360         $syscontext->path = '/';
361         $this->insert_context_in_db($syscontext);
363         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
364                 $syscontext, 'page-type', 'sub-page');
366         $blockmanager->add_block($blockname, $regionname, 0, true, $page->pagetype, $page->subpage);
368         // Exercise SUT.
369         $blockmanager->load_blocks();
370         // Validate.
371         $blocks = $blockmanager->get_blocks_for_region($regionname);
372         $this->assertContainsBlocksOfType(array($blockname), $blocks);
373     }
375     public function test_block_included_with_page_type_pattern() {
376         // Set up fixture.
377         $regionname = 'a-region';
378         $blockname = $this->get_a_known_block_type();
379         $syscontext = get_context_instance(CONTEXT_SYSTEM);
380         $syscontext->path = '/';
381         $this->insert_context_in_db($syscontext);
383         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
384                 $syscontext, 'page-type', 'sub-page');
386         $blockmanager->add_block($blockname, $regionname, 0, true, 'page-*', $page->subpage);
388         // Exercise SUT.
389         $blockmanager->load_blocks();
390         // Validate.
391         $blocks = $blockmanager->get_blocks_for_region($regionname);
392         $this->assertContainsBlocksOfType(array($blockname), $blocks);
393     }