MDL-64506 phpunit: Update unit tests to use classic
[moodle.git] / lib / tests / blocklib_test.php
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/>.
17 /**
18  * Tests for the block_manager class in ../blocklib.php.
19  *
20  * @package   core
21  * @category  phpunit
22  * @copyright 2009 Tim Hunt
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/pagelib.php');
30 require_once($CFG->libdir . '/blocklib.php');
31 require_once($CFG->dirroot . '/blocks/moodleblock.class.php');
34 /**
35  * Test various block related classes.
36  */
37 class core_blocklib_testcase extends advanced_testcase {
38     protected $testpage;
39     protected $blockmanager;
40     protected $isediting = null;
42     protected function setUp() {
43         parent::setUp();
44         $this->testpage = new moodle_page();
45         $this->testpage->set_context(context_system::instance());
46         $this->testpage->set_pagetype('phpunit-block-test');
47         $this->blockmanager = new testable_block_manager($this->testpage);
48     }
50     protected function tearDown() {
51         $this->testpage = null;
52         $this->blockmanager = null;
53         parent::tearDown();
54     }
56     protected function purge_blocks() {
57         global $DB;
58         $this->resetAfterTest();
60         $bis = $DB->get_records('block_instances');
61         foreach ($bis as $instance) {
62             blocks_delete_instance($instance);
63         }
64     }
66     /**
67      * Gets the last block created.
68      *
69      * @return stdClass a record from block_instances
70      */
71     protected function get_last_created_block() {
72         global $DB;
73         // The newest block should be the record with the highest id.
74         $records = $DB->get_records('block_instances', [], 'id DESC', '*', 0, 1);
75         $return = null;
76         foreach ($records as $record) {
77             // There should only be one.
78             $return = $record;
79         }
80         return $return;
81     }
83     public function test_no_regions_initially() {
84         // Exercise SUT & Validate.
85         $this->assertEquals(array(), $this->blockmanager->get_regions());
86     }
88     public function test_add_region() {
89         // Exercise SUT.
90         $this->blockmanager->add_region('a-region-name', false);
91         // Validate.
92         $this->assertEquals(array('a-region-name'), $this->blockmanager->get_regions());
93     }
95     public function test_add_regions() {
96         // Set up fixture.
97         $regions = array('a-region', 'another-region');
98         // Exercise SUT.
99         $this->blockmanager->add_regions($regions, false);
100         // Validate.
101         $this->assertEquals($regions, $this->blockmanager->get_regions(), '', 0, 10, true);
102     }
104     public function test_add_region_twice() {
105         // Exercise SUT.
106         $this->blockmanager->add_region('a-region-name', false);
107         $this->blockmanager->add_region('another-region', false);
108         // Validate.
109         $this->assertEquals(array('a-region-name', 'another-region'), $this->blockmanager->get_regions(), '', 0, 10, true);
110     }
112     /**
113      * @expectedException coding_exception
114      */
115     public function test_cannot_add_region_after_loaded() {
116         // Set up fixture.
117         $this->blockmanager->mark_loaded();
118         // Exercise SUT.
119         $this->blockmanager->add_region('too-late', false);
120     }
122     /**
123      * Testing adding a custom region.
124      */
125     public function test_add_custom_region() {
126         global $SESSION;
127         // Exercise SUT.
128         $this->blockmanager->add_region('a-custom-region-name');
129         // Validate.
130         $this->assertEquals(array('a-custom-region-name'), $this->blockmanager->get_regions());
131         $this->assertTrue(isset($SESSION->custom_block_regions));
132         $this->assertArrayHasKey('phpunit-block-test', $SESSION->custom_block_regions);
133         $this->assertTrue(in_array('a-custom-region-name', $SESSION->custom_block_regions['phpunit-block-test']));
135     }
137     /**
138      * Test adding two custom regions using add_regions method.
139      */
140     public function test_add_custom_regions() {
141         global $SESSION;
142         // Set up fixture.
143         $regions = array('a-region', 'another-custom-region');
144         // Exercise SUT.
145         $this->blockmanager->add_regions($regions);
146         // Validate.
147         $this->assertEquals($regions, $this->blockmanager->get_regions(), '', 0, 10, true);
148         $this->assertTrue(isset($SESSION->custom_block_regions));
149         $this->assertArrayHasKey('phpunit-block-test', $SESSION->custom_block_regions);
150         $this->assertTrue(in_array('another-custom-region', $SESSION->custom_block_regions['phpunit-block-test']));
151     }
153     /**
154      * Test adding two custom block regions.
155      */
156     public function test_add_custom_region_twice() {
157         // Exercise SUT.
158         $this->blockmanager->add_region('a-custom-region-name');
159         $this->blockmanager->add_region('another-custom-region');
160         // Validate.
161         $this->assertEquals(
162             array('a-custom-region-name', 'another-custom-region'),
163             $this->blockmanager->get_regions(),
164             '', 0, 10, true
165         );
166     }
168     /**
169      * Test to ensure that we cannot add a region after the blocks have been loaded.
170      * @expectedException coding_exception
171      */
172     public function test_cannot_add_custom_region_after_loaded() {
173         // Set up fixture.
174         $this->blockmanager->mark_loaded();
175         // Exercise SUT.
176         $this->blockmanager->add_region('too-late');
177     }
179     public function test_set_default_region() {
180         // Set up fixture.
181         $this->blockmanager->add_region('a-region-name', false);
182         // Exercise SUT.
183         $this->blockmanager->set_default_region('a-region-name');
184         // Validate.
185         $this->assertEquals('a-region-name', $this->blockmanager->get_default_region());
186     }
188     /**
189      * @expectedException coding_exception
190      */
191     public function test_cannot_set_unknown_region_as_default() {
192         // Exercise SUT.
193         $this->blockmanager->set_default_region('a-region-name');
194     }
196     /**
197      * @expectedException coding_exception
198      */
199     public function test_cannot_change_default_region_after_loaded() {
200         // Set up fixture.
201         $this->blockmanager->mark_loaded();
202         // Exercise SUT.
203         $this->blockmanager->set_default_region('too-late');
204     }
206     public function test_matching_page_type_patterns() {
207         $this->assertEquals(array('site-index', 'site-index-*', 'site-*', '*'),
208             matching_page_type_patterns('site-index'), '', 0, 10, true);
210         $this->assertEquals(array('mod-quiz-report-overview', 'mod-quiz-report-overview-*', 'mod-quiz-report-*', 'mod-quiz-*', 'mod-*', '*'),
211             matching_page_type_patterns('mod-quiz-report-overview'), '', 0, 10, true);
213         $this->assertEquals(array('mod-forum-view', 'mod-*-view', 'mod-forum-view-*', 'mod-forum-*', 'mod-*', '*'),
214             matching_page_type_patterns('mod-forum-view'), '', 0, 10, true);
216         $this->assertEquals(array('mod-forum-index', 'mod-*-index', 'mod-forum-index-*', 'mod-forum-*', 'mod-*', '*'),
217             matching_page_type_patterns('mod-forum-index'), '', 0, 10, true);
218     }
220     protected function get_a_page_and_block_manager($regions, $context, $pagetype, $subpage = '') {
221         $page = new moodle_page;
222         $page->set_context($context);
223         $page->set_pagetype($pagetype);
224         $page->set_subpage($subpage);
225         $page->set_url(new moodle_url('/'));
227         $blockmanager = new testable_block_manager($page);
228         $blockmanager->add_regions($regions, false);
229         $blockmanager->set_default_region($regions[0]);
231         return array($page, $blockmanager);
232     }
234     protected function get_a_known_block_type() {
235         global $DB;
236         $block = new stdClass;
237         $block->name = 'ablocktype';
238         $DB->insert_record('block', $block);
239         return $block->name;
240     }
242     protected function assertContainsBlocksOfType($typearray, $blockarray) {
243         if (!$this->assertEquals(count($typearray), count($blockarray), "Blocks array contains the wrong number of elements %s.")) {
244             return;
245         }
246         $types = array_values($typearray);
247         $i = 0;
248         foreach ($blockarray as $block) {
249             $blocktype = $types[$i];
250             $this->assertEquals($blocktype, $block->name(), "Block types do not match at postition $i %s.");
251             $i++;
252         }
253     }
255     public function test_empty_initially() {
256         $this->purge_blocks();
258         // Set up fixture.
259         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array('a-region'),
260             context_system::instance(), 'page-type');
261         // Exercise SUT.
262         $blockmanager->load_blocks();
263         // Validate.
264         $blocks = $blockmanager->get_loaded_blocks();
265         $this->assertEquals(array('a-region' => array()), $blocks);
266     }
268     public function test_adding_and_retrieving_one_block() {
269         $this->purge_blocks();
271         // Set up fixture.
272         $regionname = 'a-region';
273         $blockname = $this->get_a_known_block_type();
274         $context = context_system::instance();
276         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
277             $context, 'page-type');
279         // Exercise SUT.
280         $blockmanager->add_block($blockname, $regionname, 0, false);
281         $blockmanager->load_blocks();
282         // Validate.
283         $blocks = $blockmanager->get_blocks_for_region($regionname);
284         $this->assertContainsBlocksOfType(array($blockname), $blocks);
285     }
287     public function test_adding_and_retrieving_two_blocks() {
288         $this->purge_blocks();
290         // Set up fixture.
291         $regionname = 'a-region';
292         $blockname = $this->get_a_known_block_type();
293         $context = context_system::instance();
295         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
296             $context, 'page-type');
298         // Exercise SUT.
299         $blockmanager->add_block($blockname, $regionname, 0, false);
300         $blockmanager->add_block($blockname, $regionname, 1, false);
301         $blockmanager->load_blocks();
302         // Validate.
303         $blocks = $blockmanager->get_blocks_for_region($regionname);
304         $this->assertContainsBlocksOfType(array($blockname, $blockname), $blocks);
305     }
307     public function test_adding_blocks() {
308         $this->purge_blocks();
310         // Set up fixture.
311         $regionname = 'a-region';
312         $blockname = $this->get_a_known_block_type();
313         $context = context_system::instance();
315         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
316             $context, 'page-type');
318         $blockmanager->add_blocks(array($regionname => array($blockname, $blockname)), null, null, false, 3);
319         $blockmanager->load_blocks();
321         $blocks = $blockmanager->get_blocks_for_region($regionname);
323         $this->assertEquals('3', $blocks[0]->instance->weight);
324         $this->assertEquals('4', $blocks[1]->instance->weight);
325     }
327     /**
328      * Test block instances.
329      *
330      * @return null
331      */
332     public function test_block_instances() {
333         $this->purge_blocks();
335         // Set up fixture.
336         $regionname = 'a-region';
337         $blockname = $this->get_a_known_block_type();
338         $context = context_system::instance();
340         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
341             $context, 'page-type');
343         $blockmanager->add_blocks(array($regionname => array($blockname, $blockname)), null, null, false, 3);
344         $blockmanager->load_blocks();
346         $blocks = $blockmanager->get_blocks_for_region($regionname);
348         $this->assertInstanceOf('\block_base', block_instance($blockname, $blocks[0]->instance));
349         $this->assertInstanceOf('\block_base', block_instance_by_id($blocks[0]->instance->id));
350     }
352     public function test_block_not_included_in_different_context() {
353         $this->purge_blocks();
355         // Set up fixture.
356         $syscontext = context_system::instance();
357         $cat = $this->getDataGenerator()->create_category(array('name' => 'testcategory'));
358         $fakecontext = context_coursecat::instance($cat->id);
359         $regionname = 'a-region';
360         $blockname = $this->get_a_known_block_type();
362         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $fakecontext, 'page-type');
363         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
365         $addbm->add_block($blockname, $regionname, 0, false);
367         // Exercise SUT.
368         $viewbm->load_blocks();
369         // Validate.
370         $blocks = $viewbm->get_blocks_for_region($regionname);
371         $this->assertContainsBlocksOfType(array(), $blocks);
372     }
374     public function test_block_included_in_sub_context() {
375         $this->purge_blocks();
377         // Set up fixture.
378         $syscontext = context_system::instance();
379         $childcontext = context_coursecat::instance(1);
380         $regionname = 'a-region';
381         $blockname = $this->get_a_known_block_type();
383         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
384         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $childcontext, 'page-type');
386         $addbm->add_block($blockname, $regionname, 0, true);
388         // Exercise SUT.
389         $viewbm->load_blocks();
390         // Validate.
391         $blocks = $viewbm->get_blocks_for_region($regionname);
392         $this->assertContainsBlocksOfType(array($blockname), $blocks);
393     }
395     public function test_block_not_included_on_different_page_type() {
396         $this->purge_blocks();
398         // Set up fixture.
399         $syscontext = context_system::instance();
400         $regionname = 'a-region';
401         $blockname = $this->get_a_known_block_type();
403         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
404         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'other-page-type');
406         $addbm->add_block($blockname, $regionname, 0, true);
408         // Exercise SUT.
409         $viewbm->load_blocks();
410         // Validate.
411         $blocks = $viewbm->get_blocks_for_region($regionname);
412         $this->assertContainsBlocksOfType(array(), $blocks);
413     }
415     public function test_block_not_included_on_different_sub_page() {
416         $this->purge_blocks();
418         // Set up fixture.
419         $regionname = 'a-region';
420         $blockname = $this->get_a_known_block_type();
421         $syscontext = context_system::instance();
423         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
424             $syscontext, 'page-type', 'sub-page');
426         $blockmanager->add_block($blockname, $regionname, 0, true, $page->pagetype, 'other-sub-page');
428         // Exercise SUT.
429         $blockmanager->load_blocks();
430         // Validate.
431         $blocks = $blockmanager->get_blocks_for_region($regionname);
432         $this->assertContainsBlocksOfType(array(), $blocks);
433     }
435     public function test_block_included_with_explicit_sub_page() {
436         $this->purge_blocks();
438         // Set up fixture.
439         $regionname = 'a-region';
440         $blockname = $this->get_a_known_block_type();
441         $syscontext = context_system::instance();
443         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
444             $syscontext, 'page-type', 'sub-page');
446         $blockmanager->add_block($blockname, $regionname, 0, true, $page->pagetype, $page->subpage);
448         // Exercise SUT.
449         $blockmanager->load_blocks();
450         // Validate.
451         $blocks = $blockmanager->get_blocks_for_region($regionname);
452         $this->assertContainsBlocksOfType(array($blockname), $blocks);
453     }
455     public function test_block_included_with_page_type_pattern() {
456         $this->purge_blocks();
458         // Set up fixture.
459         $regionname = 'a-region';
460         $blockname = $this->get_a_known_block_type();
461         $syscontext = context_system::instance();
463         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
464             $syscontext, 'page-type', 'sub-page');
466         $blockmanager->add_block($blockname, $regionname, 0, true, 'page-*', $page->subpage);
468         // Exercise SUT.
469         $blockmanager->load_blocks();
470         // Validate.
471         $blocks = $blockmanager->get_blocks_for_region($regionname);
472         $this->assertContainsBlocksOfType(array($blockname), $blocks);
473     }
475     public function test_matching_page_type_patterns_from_pattern() {
476         $pattern = '*';
477         $expected = array('*');
478         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
480         $pattern = 'admin-*';
481         $expected = array('admin-*', 'admin', '*');
482         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
484         $pattern = 'blog-index';
485         $expected = array('blog-index', 'blog-index-*', 'blog-*', '*');
486         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
488         $pattern = 'course-index-*';
489         $expected = array('course-index-*', 'course-index', 'course-*', '*');
490         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
492         $pattern = 'course-index-category';
493         $expected = array('course-index-category', 'course-index-category-*', 'course-index-*', 'course-*', '*');
494         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
496         $pattern = 'mod-assign-view';
497         $expected = array('mod-assign-view', 'mod-*-view', 'mod-assign-view-*', 'mod-assign-*', 'mod-*', '*');
498         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
500         $pattern = 'mod-assign-index';
501         $expected = array('mod-assign-index', 'mod-*-index', 'mod-assign-index-*', 'mod-assign-*', 'mod-*', '*');
502         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
504         $pattern = 'mod-forum-*';
505         $expected = array('mod-forum-*', 'mod-forum', 'mod-*', '*');
506         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
508         $pattern = 'mod-*-view';
509         $expected = array('mod-*-view', 'mod', 'mod-*', '*');
510         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
512         $pattern = 'mod-*-index';
513         $expected = array('mod-*-index', 'mod', 'mod-*', '*');
514         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
516         $pattern = 'my-index';
517         $expected = array('my-index', 'my-index-*', 'my-*', '*');
518         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
520         $pattern = 'user-profile';
521         $expected = array('user-profile', 'user-profile-*', 'user-*', '*');
522         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
523     }
525     public function test_delete_instances() {
526         global $DB;
527         $this->purge_blocks();
528         $this->setAdminUser();
530         $regionname = 'a-region';
531         $blockname = $this->get_a_known_block_type();
532         $context = context_system::instance();
534         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
535             $context, 'page-type');
537         $blockmanager->add_blocks(array($regionname => array($blockname, $blockname, $blockname)), null, null, false, 3);
538         $blockmanager->load_blocks();
540         $blocks = $blockmanager->get_blocks_for_region($regionname);
541         $blockids = array();
542         $preferences = array();
544         // Create block related data.
545         foreach ($blocks as $block) {
546             $instance = $block->instance;
547             $pref = 'block' . $instance->id . 'hidden';
548             set_user_preference($pref, '123', 123);
549             $preferences[] = $pref;
550             $pref = 'docked_block_instance_' . $instance->id;
551             set_user_preference($pref, '123', 123);
552             $preferences[] = $pref;
553             blocks_set_visibility($instance, $page, 1);
554             $blockids[] = $instance->id;
555         }
557         // Confirm what has been set.
558         $this->assertCount(3, $blockids);
559         list($insql, $inparams) = $DB->get_in_or_equal($blockids);
560         $this->assertEquals(3, $DB->count_records_select('block_positions', "blockinstanceid $insql", $inparams));
561         list($insql, $inparams) = $DB->get_in_or_equal($preferences);
562         $this->assertEquals(6, $DB->count_records_select('user_preferences', "name $insql", $inparams));
564         // Keep a block on the side.
565         $allblockids = $blockids;
566         $tokeep = array_pop($blockids);
568         // Delete and confirm what should have happened.
569         blocks_delete_instances($blockids);
571         // Reload the manager.
572         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
573             $context, 'page-type');
574         $blockmanager->load_blocks();
575         $blocks = $blockmanager->get_blocks_for_region($regionname);
577         $this->assertCount(1, $blocks);
578         list($insql, $inparams) = $DB->get_in_or_equal($allblockids);
579         $this->assertEquals(1, $DB->count_records_select('block_positions', "blockinstanceid $insql", $inparams));
580         list($insql, $inparams) = $DB->get_in_or_equal($preferences);
581         $this->assertEquals(2, $DB->count_records_select('user_preferences', "name $insql", $inparams));
583         $this->assertFalse(context_block::instance($blockids[0], IGNORE_MISSING));
584         $this->assertFalse(context_block::instance($blockids[1], IGNORE_MISSING));
585         context_block::instance($tokeep);   // Would throw an exception if it was deleted.
586     }
588     public function test_create_all_block_instances() {
589         global $CFG, $PAGE, $DB;
591         $this->setAdminUser();
592         $this->resetAfterTest();
593         $regionname = 'side-pre';
594         $context = context_system::instance();
596         $PAGE->reset_theme_and_output();
597         $CFG->theme = 'boost';
599         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
600             $context, 'page-type');
601         $blockmanager->load_blocks();
602         $blockmanager->create_all_block_instances();
603         $blocks = $blockmanager->get_blocks_for_region($regionname);
604         // Assert that we no auto created blocks in boost by default.
605         $this->assertEmpty($blocks);
606         // There should be no blocks in the DB.
608         $PAGE->reset_theme_and_output();
609         // Change to a theme with undeletable blocks.
610         $CFG->theme = 'classic';
612         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
613             $context, 'page-type');
615         $blockmanager->show_only_fake_blocks(true);
616         $blockmanager->load_blocks();
617         $blockmanager->create_all_block_instances();
618         $blocks = $blockmanager->get_blocks_for_region($regionname);
619         // Assert that we no auto created blocks when viewing a fake blocks only page.
620         $this->assertEmpty($blocks);
622         $PAGE->reset_theme_and_output();
623         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
624             $context, 'page-type');
626         $blockmanager->show_only_fake_blocks(false);
627         $blockmanager->load_blocks();
628         $blockmanager->create_all_block_instances();
629         $blocks = $blockmanager->get_blocks_for_region($regionname);
630         // Assert that we get the required block for this theme auto-created.
631         $this->assertCount(2, $blocks);
633         $PAGE->reset_theme_and_output();
634         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
635             $context, 'page-type');
637         $blockmanager->protect_block('html');
638         $blockmanager->load_blocks();
639         $blockmanager->create_all_block_instances();
640         $blocks = $blockmanager->get_blocks_for_region($regionname);
641         // Assert that protecting a block does not make it auto-created.
642         $this->assertCount(2, $blocks);
644         $requiredbytheme = $blockmanager->get_required_by_theme_block_types();
645         foreach ($requiredbytheme as $blockname) {
646             $instance = $DB->get_record('block_instances', array('blockname' => $blockname));
647             $this->assertEquals(1, $instance->requiredbytheme);
648         }
650         // Switch back and those auto blocks should not be returned.
651         $PAGE->reset_theme_and_output();
652         $CFG->theme = 'boost';
654         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
655             $context, 'page-type');
656         $blockmanager->load_blocks();
657         $blockmanager->create_all_block_instances();
658         $blocks = $blockmanager->get_blocks_for_region($regionname);
659         // Assert that we do not return requiredbytheme blocks when they are not required.
660         $this->assertEmpty($blocks);
661         // But they should exist in the DB.
662         foreach ($requiredbytheme as $blockname) {
663             $count = $DB->count_records('block_instances', array('blockname' => $blockname));
664             $this->assertEquals(1, $count);
665         }
666     }
668     /**
669      * Test the block instance time fields (timecreated, timemodified).
670      */
671     public function test_block_instance_times() {
672         global $DB;
674         $this->purge_blocks();
676         // Set up fixture.
677         $regionname = 'a-region';
678         $blockname = 'html';
679         $context = context_system::instance();
681         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
682                 $context, 'page-type');
684         // Add block to page.
685         $before = time();
686         $blockmanager->add_block($blockname, $regionname, 0, false);
687         $after = time();
689         // Check database table to ensure it contains created/modified times.
690         $blockdata = $DB->get_record('block_instances', ['blockname' => 'html']);
691         $this->assertTrue($blockdata->timemodified >= $before && $blockdata->timemodified <= $after);
692         $this->assertTrue($blockdata->timecreated >= $before && $blockdata->timecreated <= $after);
694         // Get block from manager.
695         $blockmanager->load_blocks();
696         $blocks = $blockmanager->get_blocks_for_region($regionname);
697         $block = reset($blocks);
699         // Wait until at least the next second.
700         while (time() === $after) {
701             usleep(100000);
702         }
704         // Update block settings.
705         $this->setAdminUser();
706         $data = (object)['text' => ['text' => 'New text', 'itemid' => 0, 'format' => FORMAT_HTML]];
707         $before = time();
708         $block->instance_config_save($data);
709         $after = time();
711         // Check time modified updated, but time created didn't.
712         $newblockdata = $DB->get_record('block_instances', ['blockname' => 'html']);
713         $this->assertTrue(
714                 $newblockdata->timemodified >= $before &&
715                 $newblockdata->timemodified <= $after &&
716                 $newblockdata->timemodified > $blockdata->timemodified);
717         $this->assertEquals($blockdata->timecreated, $newblockdata->timecreated);
719         // Also try repositioning the block.
720         while (time() === $after) {
721             usleep(100000);
722         }
723         $before = time();
724         $blockmanager->reposition_block($blockdata->id, $regionname, 10);
725         $after = time();
726         $blockdata = $newblockdata;
727         $newblockdata = $DB->get_record('block_instances', ['blockname' => 'html']);
728         $this->assertTrue(
729                 $newblockdata->timemodified >= $before &&
730                 $newblockdata->timemodified <= $after &&
731                 $newblockdata->timemodified > $blockdata->timemodified);
732         $this->assertEquals($blockdata->timecreated, $newblockdata->timecreated);
733     }
735     /**
736      * Tests that dashboard pages get their blocks loaded correctly.
737      */
738     public function test_default_dashboard() {
739         global $CFG, $PAGE, $DB;
740         $storedpage = $PAGE;
741         require_once($CFG->dirroot . '/my/lib.php');
742         $this->purge_blocks();
743         $regionname = 'a-region';
744         $blockname = $this->get_a_known_block_type();
745         $user = self::getDataGenerator()->create_user();
746         $syscontext = context_system::instance();
747         $usercontext = context_user::instance($user->id);
748         // Add sitewide 'sticky' blocks. The page is not setup exactly as a site page would be...
749         // but it does seem to mean that the bloacks are added correctly.
750         list($sitepage, $sitebm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'site-index');
751         $sitebm->add_block($blockname, $regionname, 0, true, '*');
752         $sitestickyblock1 = $this->get_last_created_block();
753         $sitebm->add_block($blockname, $regionname, 1, true, '*');
754         $sitestickyblock2 = $this->get_last_created_block();
755         $sitebm->add_block($blockname, $regionname, 8, true, '*');
756         $sitestickyblock3 = $this->get_last_created_block();
757         // Blocks that should not be picked up by any other pages in this unit test.
758         $sitebm->add_block($blockname, $regionname, -8, true, 'site-index-*');
759         $sitebm->add_block($blockname, $regionname, -9, true, 'site-index');
760         $sitebm->load_blocks();
761         // This repositioning should not be picked up.
762         $sitebm->reposition_block($sitestickyblock3->id, $regionname, 9);
763         // Setup the default dashboard page. This adds the blocks with the correct parameters, but seems to not be
764         // an exact page/blockmanager setup for the default dashboard setup page.
765         $defaultmy = my_get_page(null, MY_PAGE_PRIVATE);
766         list($defaultmypage, $defaultmybm) = $this->get_a_page_and_block_manager(array($regionname), null, 'my-index', $defaultmy->id);
767         $PAGE = $defaultmypage;
768         $defaultmybm->add_block($blockname, $regionname, -2, false, $defaultmypage->pagetype, $defaultmypage->subpage);
769         $defaultblock1 = $this->get_last_created_block();
770         $defaultmybm->add_block($blockname, $regionname, 3, false, $defaultmypage->pagetype, $defaultmypage->subpage);
771         $defaultblock2 = $this->get_last_created_block();
772         $defaultmybm->load_blocks();
773         $defaultmybm->reposition_block($sitestickyblock1->id, $regionname, 4);
774         // Setup the user's dashboard.
775         $usermy = my_copy_page($user->id);
776         list($mypage, $mybm) = $this->get_a_page_and_block_manager(array($regionname), $usercontext, 'my-index', $usermy->id);
777         $PAGE = $mypage;
778         $mybm->add_block($blockname, $regionname, 5, false, $mypage->pagetype, $mypage->subpage);
779         $block1 = $this->get_last_created_block();
780         $mybm->load_blocks();
781         $mybm->reposition_block($sitestickyblock2->id, $regionname, -1);
782         // Reload the blocks in the managers.
783         context_helper::reset_caches();
784         $defaultmybm->reset_caches();
785         $this->assertNull($defaultmybm->get_loaded_blocks());
786         $defaultmybm->load_blocks();
787         $this->assertNotNull($defaultmybm->get_loaded_blocks());
788         $defaultbr = $defaultmybm->get_blocks_for_region($regionname);
789         $mybm->reset_caches();
790         $this->assertNull($mybm->get_loaded_blocks());
791         $mybm->load_blocks();
792         $this->assertNotNull($mybm->get_loaded_blocks());
793         $mybr = $mybm->get_blocks_for_region($regionname);
794         // Test that a user dashboard when forced to use the default finds the correct blocks.
795         list($forcedmypage, $forcedmybm) = $this->get_a_page_and_block_manager(array($regionname), $usercontext, 'my-index', $defaultmy->id);
796         $forcedmybm->load_blocks();
797         $forcedmybr = $forcedmybm->get_blocks_for_region($regionname);
798         // Check that the default page is in the expected order.
799         $this->assertCount(5, $defaultbr);
800         $this->assertEquals($defaultblock1->id, $defaultbr[0]->instance->id);
801         $this->assertEquals('-2', $defaultbr[0]->instance->weight);
802         $this->assertEquals($sitestickyblock2->id, $defaultbr[1]->instance->id);
803         $this->assertEquals('1', $defaultbr[1]->instance->weight);
804         $this->assertEquals($defaultblock2->id, $defaultbr[2]->instance->id);
805         $this->assertEquals('3', $defaultbr[2]->instance->weight);
806         $this->assertEquals($sitestickyblock1->id, $defaultbr[3]->instance->id);
807         $this->assertEquals('4', $defaultbr[3]->instance->weight);
808         $this->assertEquals($sitestickyblock3->id, $defaultbr[4]->instance->id);
809         $this->assertEquals('8', $defaultbr[4]->instance->weight);
810         // Check that the correct block are present in the expected order for a.
811         $this->assertCount(5, $forcedmybr);
812         $this->assertEquals($defaultblock1->id, $forcedmybr[0]->instance->id);
813         $this->assertEquals('-2', $forcedmybr[0]->instance->weight);
814         $this->assertEquals($sitestickyblock2->id, $forcedmybr[1]->instance->id);
815         $this->assertEquals('1', $forcedmybr[1]->instance->weight);
816         $this->assertEquals($defaultblock2->id, $forcedmybr[2]->instance->id);
817         $this->assertEquals('3', $forcedmybr[2]->instance->weight);
818         $this->assertEquals($sitestickyblock1->id, $forcedmybr[3]->instance->id);
819         $this->assertEquals('4', $forcedmybr[3]->instance->weight);
820         $this->assertEquals($sitestickyblock3->id, $forcedmybr[4]->instance->id);
821         $this->assertEquals('8', $forcedmybr[4]->instance->weight);
822         // Check that the correct blocks are present in the standard my page.
823         $this->assertCount(6, $mybr);
824         $this->assertEquals('-2', $mybr[0]->instance->weight);
825         $this->assertEquals($sitestickyblock2->id, $mybr[1]->instance->id);
826         $this->assertEquals('-1', $mybr[1]->instance->weight);
827         $this->assertEquals('3', $mybr[2]->instance->weight);
828         // Test the override on the first sticky block was copied and picked up.
829         $this->assertEquals($sitestickyblock1->id, $mybr[3]->instance->id);
830         $this->assertEquals('4', $mybr[3]->instance->weight);
831         $this->assertEquals($block1->id, $mybr[4]->instance->id);
832         $this->assertEquals('5', $mybr[4]->instance->weight);
833         $this->assertEquals($sitestickyblock3->id, $mybr[5]->instance->id);
834         $this->assertEquals('8', $mybr[5]->instance->weight);
835         $PAGE = $storedpage;
836     }
839 /**
840  * Test-specific subclass to make some protected things public.
841  */
842 class testable_block_manager extends block_manager {
843     /**
844      * Resets the caches in the block manager.
845      * This allows blocks to be reloaded correctly.
846      */
847     public function reset_caches() {
848         $this->birecordsbyregion = null;
849         $this->blockinstances = array();
850         $this->visibleblockcontent = array();
851     }
852     public function mark_loaded() {
853         $this->birecordsbyregion = array();
854     }
855     public function get_loaded_blocks() {
856         return $this->birecordsbyregion;
857     }
860 /**
861  * Test-specific subclass to make some protected things public.
862  */
863 class block_ablocktype extends block_base {
864     public function init() {
865     }