92c94d4c50a65c43b799f232a50fcff21d162364
[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     public function test_block_not_included_in_different_context() {
328         $this->purge_blocks();
330         // Set up fixture.
331         $syscontext = context_system::instance();
332         $cat = $this->getDataGenerator()->create_category(array('name' => 'testcategory'));
333         $fakecontext = context_coursecat::instance($cat->id);
334         $regionname = 'a-region';
335         $blockname = $this->get_a_known_block_type();
337         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $fakecontext, 'page-type');
338         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
340         $addbm->add_block($blockname, $regionname, 0, false);
342         // Exercise SUT.
343         $viewbm->load_blocks();
344         // Validate.
345         $blocks = $viewbm->get_blocks_for_region($regionname);
346         $this->assertContainsBlocksOfType(array(), $blocks);
347     }
349     public function test_block_included_in_sub_context() {
350         $this->purge_blocks();
352         // Set up fixture.
353         $syscontext = context_system::instance();
354         $childcontext = context_coursecat::instance(1);
355         $regionname = 'a-region';
356         $blockname = $this->get_a_known_block_type();
358         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
359         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $childcontext, 'page-type');
361         $addbm->add_block($blockname, $regionname, 0, true);
363         // Exercise SUT.
364         $viewbm->load_blocks();
365         // Validate.
366         $blocks = $viewbm->get_blocks_for_region($regionname);
367         $this->assertContainsBlocksOfType(array($blockname), $blocks);
368     }
370     public function test_block_not_included_on_different_page_type() {
371         $this->purge_blocks();
373         // Set up fixture.
374         $syscontext = context_system::instance();
375         $regionname = 'a-region';
376         $blockname = $this->get_a_known_block_type();
378         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
379         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'other-page-type');
381         $addbm->add_block($blockname, $regionname, 0, true);
383         // Exercise SUT.
384         $viewbm->load_blocks();
385         // Validate.
386         $blocks = $viewbm->get_blocks_for_region($regionname);
387         $this->assertContainsBlocksOfType(array(), $blocks);
388     }
390     public function test_block_not_included_on_different_sub_page() {
391         $this->purge_blocks();
393         // Set up fixture.
394         $regionname = 'a-region';
395         $blockname = $this->get_a_known_block_type();
396         $syscontext = context_system::instance();
398         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
399             $syscontext, 'page-type', 'sub-page');
401         $blockmanager->add_block($blockname, $regionname, 0, true, $page->pagetype, 'other-sub-page');
403         // Exercise SUT.
404         $blockmanager->load_blocks();
405         // Validate.
406         $blocks = $blockmanager->get_blocks_for_region($regionname);
407         $this->assertContainsBlocksOfType(array(), $blocks);
408     }
410     public function test_block_included_with_explicit_sub_page() {
411         $this->purge_blocks();
413         // Set up fixture.
414         $regionname = 'a-region';
415         $blockname = $this->get_a_known_block_type();
416         $syscontext = context_system::instance();
418         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
419             $syscontext, 'page-type', 'sub-page');
421         $blockmanager->add_block($blockname, $regionname, 0, true, $page->pagetype, $page->subpage);
423         // Exercise SUT.
424         $blockmanager->load_blocks();
425         // Validate.
426         $blocks = $blockmanager->get_blocks_for_region($regionname);
427         $this->assertContainsBlocksOfType(array($blockname), $blocks);
428     }
430     public function test_block_included_with_page_type_pattern() {
431         $this->purge_blocks();
433         // Set up fixture.
434         $regionname = 'a-region';
435         $blockname = $this->get_a_known_block_type();
436         $syscontext = context_system::instance();
438         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
439             $syscontext, 'page-type', 'sub-page');
441         $blockmanager->add_block($blockname, $regionname, 0, true, 'page-*', $page->subpage);
443         // Exercise SUT.
444         $blockmanager->load_blocks();
445         // Validate.
446         $blocks = $blockmanager->get_blocks_for_region($regionname);
447         $this->assertContainsBlocksOfType(array($blockname), $blocks);
448     }
450     public function test_matching_page_type_patterns_from_pattern() {
451         $pattern = '*';
452         $expected = array('*');
453         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
455         $pattern = 'admin-*';
456         $expected = array('admin-*', 'admin', '*');
457         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
459         $pattern = 'blog-index';
460         $expected = array('blog-index', 'blog-index-*', 'blog-*', '*');
461         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
463         $pattern = 'course-index-*';
464         $expected = array('course-index-*', 'course-index', 'course-*', '*');
465         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
467         $pattern = 'course-index-category';
468         $expected = array('course-index-category', 'course-index-category-*', 'course-index-*', 'course-*', '*');
469         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
471         $pattern = 'mod-assign-view';
472         $expected = array('mod-assign-view', 'mod-*-view', 'mod-assign-view-*', 'mod-assign-*', 'mod-*', '*');
473         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
475         $pattern = 'mod-assign-index';
476         $expected = array('mod-assign-index', 'mod-*-index', 'mod-assign-index-*', 'mod-assign-*', 'mod-*', '*');
477         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
479         $pattern = 'mod-forum-*';
480         $expected = array('mod-forum-*', 'mod-forum', 'mod-*', '*');
481         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
483         $pattern = 'mod-*-view';
484         $expected = array('mod-*-view', 'mod', 'mod-*', '*');
485         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
487         $pattern = 'mod-*-index';
488         $expected = array('mod-*-index', 'mod', 'mod-*', '*');
489         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
491         $pattern = 'my-index';
492         $expected = array('my-index', 'my-index-*', 'my-*', '*');
493         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
495         $pattern = 'user-profile';
496         $expected = array('user-profile', 'user-profile-*', 'user-*', '*');
497         $this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
498     }
500     public function test_delete_instances() {
501         global $DB;
502         $this->purge_blocks();
503         $this->setAdminUser();
505         $regionname = 'a-region';
506         $blockname = $this->get_a_known_block_type();
507         $context = context_system::instance();
509         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
510             $context, 'page-type');
512         $blockmanager->add_blocks(array($regionname => array($blockname, $blockname, $blockname)), null, null, false, 3);
513         $blockmanager->load_blocks();
515         $blocks = $blockmanager->get_blocks_for_region($regionname);
516         $blockids = array();
517         $preferences = array();
519         // Create block related data.
520         foreach ($blocks as $block) {
521             $instance = $block->instance;
522             $pref = 'block' . $instance->id . 'hidden';
523             set_user_preference($pref, '123', 123);
524             $preferences[] = $pref;
525             $pref = 'docked_block_instance_' . $instance->id;
526             set_user_preference($pref, '123', 123);
527             $preferences[] = $pref;
528             blocks_set_visibility($instance, $page, 1);
529             $blockids[] = $instance->id;
530         }
532         // Confirm what has been set.
533         $this->assertCount(3, $blockids);
534         list($insql, $inparams) = $DB->get_in_or_equal($blockids);
535         $this->assertEquals(3, $DB->count_records_select('block_positions', "blockinstanceid $insql", $inparams));
536         list($insql, $inparams) = $DB->get_in_or_equal($preferences);
537         $this->assertEquals(6, $DB->count_records_select('user_preferences', "name $insql", $inparams));
539         // Keep a block on the side.
540         $allblockids = $blockids;
541         $tokeep = array_pop($blockids);
543         // Delete and confirm what should have happened.
544         blocks_delete_instances($blockids);
546         // Reload the manager.
547         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
548             $context, 'page-type');
549         $blockmanager->load_blocks();
550         $blocks = $blockmanager->get_blocks_for_region($regionname);
552         $this->assertCount(1, $blocks);
553         list($insql, $inparams) = $DB->get_in_or_equal($allblockids);
554         $this->assertEquals(1, $DB->count_records_select('block_positions', "blockinstanceid $insql", $inparams));
555         list($insql, $inparams) = $DB->get_in_or_equal($preferences);
556         $this->assertEquals(2, $DB->count_records_select('user_preferences', "name $insql", $inparams));
558         $this->assertFalse(context_block::instance($blockids[0], IGNORE_MISSING));
559         $this->assertFalse(context_block::instance($blockids[1], IGNORE_MISSING));
560         context_block::instance($tokeep);   // Would throw an exception if it was deleted.
561     }
563     public function test_create_all_block_instances() {
564         global $CFG, $PAGE, $DB;
566         $this->setAdminUser();
567         $this->resetAfterTest();
568         $regionname = 'side-pre';
569         $context = context_system::instance();
571         $PAGE->reset_theme_and_output();
572         $CFG->theme = 'boost';
574         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
575             $context, 'page-type');
576         $blockmanager->load_blocks();
577         $blockmanager->create_all_block_instances();
578         $blocks = $blockmanager->get_blocks_for_region($regionname);
579         // Assert that we no auto created blocks in boost by default.
580         $this->assertEmpty($blocks);
581         // There should be no blocks in the DB.
583         $PAGE->reset_theme_and_output();
584         // Change to a theme with undeletable blocks.
585         $CFG->theme = 'clean';
587         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
588             $context, 'page-type');
590         $blockmanager->show_only_fake_blocks(true);
591         $blockmanager->load_blocks();
592         $blockmanager->create_all_block_instances();
593         $blocks = $blockmanager->get_blocks_for_region($regionname);
594         // Assert that we no auto created blocks when viewing a fake blocks only page.
595         $this->assertEmpty($blocks);
597         $PAGE->reset_theme_and_output();
598         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
599             $context, 'page-type');
601         $blockmanager->show_only_fake_blocks(false);
602         $blockmanager->load_blocks();
603         $blockmanager->create_all_block_instances();
604         $blocks = $blockmanager->get_blocks_for_region($regionname);
605         // Assert that we get the required block for this theme auto-created.
606         $this->assertCount(2, $blocks);
608         $PAGE->reset_theme_and_output();
609         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
610             $context, 'page-type');
612         $blockmanager->protect_block('html');
613         $blockmanager->load_blocks();
614         $blockmanager->create_all_block_instances();
615         $blocks = $blockmanager->get_blocks_for_region($regionname);
616         // Assert that protecting a block does not make it auto-created.
617         $this->assertCount(2, $blocks);
619         $requiredbytheme = $blockmanager->get_required_by_theme_block_types();
620         foreach ($requiredbytheme as $blockname) {
621             $instance = $DB->get_record('block_instances', array('blockname' => $blockname));
622             $this->assertEquals(1, $instance->requiredbytheme);
623         }
625         // Switch back and those auto blocks should not be returned.
626         $PAGE->reset_theme_and_output();
627         $CFG->theme = 'boost';
629         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
630             $context, 'page-type');
631         $blockmanager->load_blocks();
632         $blockmanager->create_all_block_instances();
633         $blocks = $blockmanager->get_blocks_for_region($regionname);
634         // Assert that we do not return requiredbytheme blocks when they are not required.
635         $this->assertEmpty($blocks);
636         // But they should exist in the DB.
637         foreach ($requiredbytheme as $blockname) {
638             $count = $DB->count_records('block_instances', array('blockname' => $blockname));
639             $this->assertEquals(1, $count);
640         }
641     }
643     /**
644      * Test the block instance time fields (timecreated, timemodified).
645      */
646     public function test_block_instance_times() {
647         global $DB;
649         $this->purge_blocks();
651         // Set up fixture.
652         $regionname = 'a-region';
653         $blockname = 'html';
654         $context = context_system::instance();
656         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
657                 $context, 'page-type');
659         // Add block to page.
660         $before = time();
661         $blockmanager->add_block($blockname, $regionname, 0, false);
662         $after = time();
664         // Check database table to ensure it contains created/modified times.
665         $blockdata = $DB->get_record('block_instances', ['blockname' => 'html']);
666         $this->assertTrue($blockdata->timemodified >= $before && $blockdata->timemodified <= $after);
667         $this->assertTrue($blockdata->timecreated >= $before && $blockdata->timecreated <= $after);
669         // Get block from manager.
670         $blockmanager->load_blocks();
671         $blocks = $blockmanager->get_blocks_for_region($regionname);
672         $block = reset($blocks);
674         // Wait until at least the next second.
675         while (time() === $after) {
676             usleep(100000);
677         }
679         // Update block settings.
680         $this->setAdminUser();
681         $data = (object)['text' => ['text' => 'New text', 'itemid' => 0, 'format' => FORMAT_HTML]];
682         $before = time();
683         $block->instance_config_save($data);
684         $after = time();
686         // Check time modified updated, but time created didn't.
687         $newblockdata = $DB->get_record('block_instances', ['blockname' => 'html']);
688         $this->assertTrue(
689                 $newblockdata->timemodified >= $before &&
690                 $newblockdata->timemodified <= $after &&
691                 $newblockdata->timemodified > $blockdata->timemodified);
692         $this->assertEquals($blockdata->timecreated, $newblockdata->timecreated);
694         // Also try repositioning the block.
695         while (time() === $after) {
696             usleep(100000);
697         }
698         $before = time();
699         $blockmanager->reposition_block($blockdata->id, $regionname, 10);
700         $after = time();
701         $blockdata = $newblockdata;
702         $newblockdata = $DB->get_record('block_instances', ['blockname' => 'html']);
703         $this->assertTrue(
704                 $newblockdata->timemodified >= $before &&
705                 $newblockdata->timemodified <= $after &&
706                 $newblockdata->timemodified > $blockdata->timemodified);
707         $this->assertEquals($blockdata->timecreated, $newblockdata->timecreated);
708     }
710     /**
711      * Tests that dashboard pages get their blocks loaded correctly.
712      */
713     public function test_default_dashboard() {
714         global $CFG, $PAGE, $DB;
715         $storedpage = $PAGE;
716         require_once($CFG->dirroot . '/my/lib.php');
717         $this->purge_blocks();
718         $regionname = 'a-region';
719         $blockname = $this->get_a_known_block_type();
720         $user = self::getDataGenerator()->create_user();
721         $syscontext = context_system::instance();
722         $usercontext = context_user::instance($user->id);
723         // Add sitewide 'sticky' blocks. The page is not setup exactly as a site page would be...
724         // but it does seem to mean that the bloacks are added correctly.
725         list($sitepage, $sitebm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'site-index');
726         $sitebm->add_block($blockname, $regionname, 0, true, '*');
727         $sitestickyblock1 = $this->get_last_created_block();
728         $sitebm->add_block($blockname, $regionname, 1, true, '*');
729         $sitestickyblock2 = $this->get_last_created_block();
730         $sitebm->add_block($blockname, $regionname, 8, true, '*');
731         $sitestickyblock3 = $this->get_last_created_block();
732         // Blocks that should not be picked up by any other pages in this unit test.
733         $sitebm->add_block($blockname, $regionname, -8, true, 'site-index-*');
734         $sitebm->add_block($blockname, $regionname, -9, true, 'site-index');
735         $sitebm->load_blocks();
736         // This repositioning should not be picked up.
737         $sitebm->reposition_block($sitestickyblock3->id, $regionname, 9);
738         // Setup the default dashboard page. This adds the blocks with the correct parameters, but seems to not be
739         // an exact page/blockmanager setup for the default dashboard setup page.
740         $defaultmy = my_get_page(null, MY_PAGE_PRIVATE);
741         list($defaultmypage, $defaultmybm) = $this->get_a_page_and_block_manager(array($regionname), null, 'my-index', $defaultmy->id);
742         $PAGE = $defaultmypage;
743         $defaultmybm->add_block($blockname, $regionname, -2, false, $defaultmypage->pagetype, $defaultmypage->subpage);
744         $defaultblock1 = $this->get_last_created_block();
745         $defaultmybm->add_block($blockname, $regionname, 3, false, $defaultmypage->pagetype, $defaultmypage->subpage);
746         $defaultblock2 = $this->get_last_created_block();
747         $defaultmybm->load_blocks();
748         $defaultmybm->reposition_block($sitestickyblock1->id, $regionname, 4);
749         // Setup the user's dashboard.
750         $usermy = my_copy_page($user->id);
751         list($mypage, $mybm) = $this->get_a_page_and_block_manager(array($regionname), $usercontext, 'my-index', $usermy->id);
752         $PAGE = $mypage;
753         $mybm->add_block($blockname, $regionname, 5, false, $mypage->pagetype, $mypage->subpage);
754         $block1 = $this->get_last_created_block();
755         $mybm->load_blocks();
756         $mybm->reposition_block($sitestickyblock2->id, $regionname, -1);
757         // Reload the blocks in the managers.
758         context_helper::reset_caches();
759         $defaultmybm->reset_caches();
760         $this->assertNull($defaultmybm->get_loaded_blocks());
761         $defaultmybm->load_blocks();
762         $this->assertNotNull($defaultmybm->get_loaded_blocks());
763         $defaultbr = $defaultmybm->get_blocks_for_region($regionname);
764         $mybm->reset_caches();
765         $this->assertNull($mybm->get_loaded_blocks());
766         $mybm->load_blocks();
767         $this->assertNotNull($mybm->get_loaded_blocks());
768         $mybr = $mybm->get_blocks_for_region($regionname);
769         // Test that a user dashboard when forced to use the default finds the correct blocks.
770         list($forcedmypage, $forcedmybm) = $this->get_a_page_and_block_manager(array($regionname), $usercontext, 'my-index', $defaultmy->id);
771         $forcedmybm->load_blocks();
772         $forcedmybr = $forcedmybm->get_blocks_for_region($regionname);
773         // Check that the default page is in the expected order.
774         $this->assertCount(5, $defaultbr);
775         $this->assertEquals($defaultblock1->id, $defaultbr[0]->instance->id);
776         $this->assertEquals('-2', $defaultbr[0]->instance->weight);
777         $this->assertEquals($sitestickyblock2->id, $defaultbr[1]->instance->id);
778         $this->assertEquals('1', $defaultbr[1]->instance->weight);
779         $this->assertEquals($defaultblock2->id, $defaultbr[2]->instance->id);
780         $this->assertEquals('3', $defaultbr[2]->instance->weight);
781         $this->assertEquals($sitestickyblock1->id, $defaultbr[3]->instance->id);
782         $this->assertEquals('4', $defaultbr[3]->instance->weight);
783         $this->assertEquals($sitestickyblock3->id, $defaultbr[4]->instance->id);
784         $this->assertEquals('8', $defaultbr[4]->instance->weight);
785         // Check that the correct block are present in the expected order for a.
786         $this->assertCount(5, $forcedmybr);
787         $this->assertEquals($defaultblock1->id, $forcedmybr[0]->instance->id);
788         $this->assertEquals('-2', $forcedmybr[0]->instance->weight);
789         $this->assertEquals($sitestickyblock2->id, $forcedmybr[1]->instance->id);
790         $this->assertEquals('1', $forcedmybr[1]->instance->weight);
791         $this->assertEquals($defaultblock2->id, $forcedmybr[2]->instance->id);
792         $this->assertEquals('3', $forcedmybr[2]->instance->weight);
793         $this->assertEquals($sitestickyblock1->id, $forcedmybr[3]->instance->id);
794         $this->assertEquals('4', $forcedmybr[3]->instance->weight);
795         $this->assertEquals($sitestickyblock3->id, $forcedmybr[4]->instance->id);
796         $this->assertEquals('8', $forcedmybr[4]->instance->weight);
797         // Check that the correct blocks are present in the standard my page.
798         $this->assertCount(6, $mybr);
799         $this->assertEquals('-2', $mybr[0]->instance->weight);
800         $this->assertEquals($sitestickyblock2->id, $mybr[1]->instance->id);
801         $this->assertEquals('-1', $mybr[1]->instance->weight);
802         $this->assertEquals('3', $mybr[2]->instance->weight);
803         // Test the override on the first sticky block was copied and picked up.
804         $this->assertEquals($sitestickyblock1->id, $mybr[3]->instance->id);
805         $this->assertEquals('4', $mybr[3]->instance->weight);
806         $this->assertEquals($block1->id, $mybr[4]->instance->id);
807         $this->assertEquals('5', $mybr[4]->instance->weight);
808         $this->assertEquals($sitestickyblock3->id, $mybr[5]->instance->id);
809         $this->assertEquals('8', $mybr[5]->instance->weight);
810         $PAGE = $storedpage;
811     }
814 /**
815  * Test-specific subclass to make some protected things public.
816  */
817 class testable_block_manager extends block_manager {
818     /**
819      * Resets the caches in the block manager.
820      * This allows blocks to be reloaded correctly.
821      */
822     public function reset_caches() {
823         $this->birecordsbyregion = null;
824         $this->blockinstances = array();
825         $this->visibleblockcontent = array();
826     }
827     public function mark_loaded() {
828         $this->birecordsbyregion = array();
829     }
830     public function get_loaded_blocks() {
831         return $this->birecordsbyregion;
832     }
835 /**
836  * Test-specific subclass to make some protected things public.
837  */
838 class block_ablocktype extends block_base {
839     public function init() {
840     }