Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletest/testblocklib_blockmanager.php
Go to the documentation of this file.
00001 <?php
00002 
00004 //                                                                       //
00005 // NOTICE OF COPYRIGHT                                                   //
00006 //                                                                       //
00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00008 //          http://moodle.org                                            //
00009 //                                                                       //
00010 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
00011 //                                                                       //
00012 // This program is free software; you can redistribute it and/or modify  //
00013 // it under the terms of the GNU General Public License as published by  //
00014 // the Free Software Foundation; either version 2 of the License, or     //
00015 // (at your option) any later version.                                   //
00016 //                                                                       //
00017 // This program is distributed in the hope that it will be useful,       //
00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00020 // GNU General Public License for more details:                          //
00021 //                                                                       //
00022 //          http://www.gnu.org/copyleft/gpl.html                         //
00023 //                                                                       //
00025 
00033 if (!defined('MOODLE_INTERNAL')) {
00034     die('Direct access to this script is forbidden.');    
00035 }
00036 
00037 require_once($CFG->libdir . '/pagelib.php');
00038 require_once($CFG->libdir . '/blocklib.php');
00039 
00041 class testable_block_manager extends block_manager {
00042 
00043     public function mark_loaded() {
00044         $this->birecordsbyregion = array();
00045     }
00046     public function get_loaded_blocks() {
00047         return $this->birecordsbyregion;
00048     }
00049 }
00050 class block_ablocktype extends block_base {
00051     public function init() {
00052     }
00053 }
00054 
00058 class moodle_block_manager_test extends UnitTestCase {
00059     public  static $includecoverage = array('lib/pagelib.php', 'lib/blocklib.php');
00060     protected $testpage;
00061     protected $blockmanager;
00062 
00063     public function setUp() {
00064         $this->testpage = new moodle_page();
00065         $this->testpage->set_context(get_context_instance(CONTEXT_SYSTEM));
00066         $this->blockmanager = new testable_block_manager($this->testpage);
00067     }
00068 
00069     public function tearDown() {
00070         $this->testpage = NULL;
00071         $this->blockmanager = NULL;
00072     }
00073 
00074     public function test_no_regions_initially() {
00075         // Exercise SUT & Validate
00076         $this->assertEqual(array(), $this->blockmanager->get_regions());
00077     }
00078 
00079     public function test_add_region() {
00080         // Exercise SUT.
00081         $this->blockmanager->add_region('a-region-name');
00082         // Validate
00083         $this->assertEqual(array('a-region-name'), $this->blockmanager->get_regions());
00084     }
00085 
00086     public function test_add_regions() {
00087         // Set up fixture.
00088         $regions = array('a-region', 'another-region');
00089         // Exercise SUT.
00090         $this->blockmanager->add_regions($regions);
00091         // Validate
00092         $this->assert(new ArraysHaveSameValuesExpectation($regions), $this->blockmanager->get_regions());
00093     }
00094 
00095     public function test_add_region_twice() {
00096         // Exercise SUT.
00097         $this->blockmanager->add_region('a-region-name');
00098         $this->blockmanager->add_region('another-region');
00099         // Validate
00100         $this->assert(new ArraysHaveSameValuesExpectation(array('a-region-name', 'another-region')),
00101                 $this->blockmanager->get_regions());
00102     }
00103 
00104     public function test_cannot_add_region_after_loaded() {
00105         // Set up fixture.
00106         $this->blockmanager->mark_loaded();
00107         // Set expectation
00108         $this->expectException();
00109         // Exercise SUT.
00110         $this->blockmanager->add_region('too-late');
00111     }
00112 
00113     public function test_set_default_region() {
00114         // Set up fixture.
00115         $this->blockmanager->add_region('a-region-name');
00116         // Exercise SUT.
00117         $this->blockmanager->set_default_region('a-region-name');
00118         // Validate
00119         $this->assertEqual('a-region-name', $this->blockmanager->get_default_region());
00120     }
00121 
00122     public function test_cannot_set_unknown_region_as_default() {
00123         // Set expectation
00124         $this->expectException();
00125         // Exercise SUT.
00126         $this->blockmanager->set_default_region('a-region-name');
00127     }
00128 
00129     public function test_cannot_change_default_region_after_loaded() {
00130         // Set up fixture.
00131         $this->blockmanager->mark_loaded();
00132         // Set expectation
00133         $this->expectException();
00134         // Exercise SUT.
00135         $this->blockmanager->set_default_region('too-late');
00136     }
00137 
00138     public function test_matching_page_type_patterns() {
00139         $this->assert(new ArraysHaveSameValuesExpectation(
00140                 array('site-index', 'site-index-*', 'site-*', '*')),
00141                 matching_page_type_patterns('site-index'));
00142 
00143         $this->assert(new ArraysHaveSameValuesExpectation(
00144                 array('mod-quiz-report-overview', 'mod-quiz-report-overview-*', 'mod-quiz-report-*', 'mod-quiz-*', 'mod-*', '*')),
00145                 matching_page_type_patterns('mod-quiz-report-overview'));
00146 
00147         $this->assert(new ArraysHaveSameValuesExpectation(
00148                 array('mod-forum-view', 'mod-*-view', 'mod-forum-view-*', 'mod-forum-*', 'mod-*', '*')),
00149                 matching_page_type_patterns('mod-forum-view'));
00150 
00151         $this->assert(new ArraysHaveSameValuesExpectation(
00152                 array('mod-forum-index', 'mod-*-index', 'mod-forum-index-*', 'mod-forum-*', 'mod-*', '*')),
00153                 matching_page_type_patterns('mod-forum-index'));
00154     }
00155 }
00156 
00160 class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase {
00161 
00162     protected $isediting = null;
00163 
00164     public function setUp() {
00165         global $USER;
00166         if (!empty($USER->editing)) {
00167             // We want to avoid the capability checks associated with
00168             // checking the user is editing.
00169             $this->isediting = $USER->editing;
00170             unset($USER->editing);
00171         }
00172         parent::setUp();
00173         $this->create_test_tables(array('block', 'block_instances', 'block_positions', 'context', 'course_categories', 'course'), 'lib');
00174         $this->switch_to_test_db();
00175         // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily)
00176         $this->create_system_context_record();
00177         // Reset all caches
00178         accesslib_clear_all_caches_for_unit_testing();
00179     }
00180 
00181     public function tearDown() {
00182         global $USER;
00183         if (!empty($this->isediting)) {
00184             // Replace $USER->editing as it was there at setup.
00185             $USER->editing = $this->isediting;
00186             $this->isediting = null;
00187         }
00188         parent::tearDown();
00189     }
00190 
00191     protected function get_a_page_and_block_manager($regions, $context, $pagetype, $subpage = '') {
00192         $page = new moodle_page;
00193         $page->set_context($context);
00194         $page->set_pagetype($pagetype);
00195         $page->set_subpage($subpage);
00196 
00197         $blockmanager = new testable_block_manager($page);
00198         $blockmanager->add_regions($regions);
00199         $blockmanager->set_default_region($regions[0]);
00200 
00201         return array($page, $blockmanager);
00202     }
00203 
00204     protected function get_a_known_block_type() {
00205         global $DB;
00206         $block = new stdClass;
00207         $block->name = 'ablocktype';
00208         $this->testdb->insert_record('block', $block);
00209         return $block->name;
00210     }
00211 
00212     protected function assertContainsBlocksOfType($typearray, $blockarray) {
00213         if (!$this->assertEqual(count($typearray), count($blockarray), "Blocks array contains the wrong number of elements %s.")) {
00214             return;
00215         }
00216         $types = array_values($typearray);
00217         $i = 0;
00218         foreach ($blockarray as $block) {
00219             $blocktype = $types[$i];
00220             $this->assertEqual($blocktype, $block->name(), "Block types do not match at postition $i %s.");
00221             $i++;
00222         }
00223     }
00224 
00225     public function test_empty_initially() {
00226         // Set up fixture.
00227         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array('a-region'),
00228                 get_context_instance(CONTEXT_SYSTEM), 'page-type');
00229         // Exercise SUT.
00230         $blockmanager->load_blocks();
00231         // Validate.
00232         $blocks = $blockmanager->get_loaded_blocks();
00233         $this->assertEqual(array('a-region' => array()), $blocks);
00234     }
00235 
00236     public function test_adding_and_retrieving_one_block() {
00237         // Set up fixture.
00238         $regionname = 'a-region';
00239         $blockname = $this->get_a_known_block_type();
00240         $context = get_context_instance(CONTEXT_SYSTEM);
00241 
00242         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
00243                 $context, 'page-type');
00244 
00245         // Exercise SUT.
00246         $blockmanager->add_block($blockname, $regionname, 0, false);
00247         $blockmanager->load_blocks();
00248         // Validate.
00249         $blocks = $blockmanager->get_blocks_for_region($regionname);
00250         $this->assertContainsBlocksOfType(array($blockname), $blocks);
00251     }
00252 
00253     public function test_adding_and_retrieving_two_blocks() {
00254         // Set up fixture.
00255         $regionname = 'a-region';
00256         $blockname = $this->get_a_known_block_type();
00257         $context = get_context_instance(CONTEXT_SYSTEM);
00258 
00259         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
00260                 $context, 'page-type');
00261 
00262         // Exercise SUT.
00263         $blockmanager->add_block($blockname, $regionname, 0, false);
00264         $blockmanager->add_block($blockname, $regionname, 1, false);
00265         $blockmanager->load_blocks();
00266         // Validate.
00267         $blocks = $blockmanager->get_blocks_for_region($regionname);
00268         $this->assertContainsBlocksOfType(array($blockname, $blockname), $blocks);
00269     }
00270 
00271     public function test_block_not_included_in_different_context() {
00272         global $DB;
00273         // Set up fixture.
00274         $syscontext = get_context_instance(CONTEXT_SYSTEM);
00275         $cat = new stdClass();
00276         $cat->name         = 'testcategory';
00277         $cat->parent       = 0;
00278         $cat->depth        = 1;
00279         $cat->sortorder    = 100;
00280         $cat->timemodified = time();
00281         $catid = $DB->insert_record('course_categories', $cat);
00282         $DB->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid));
00283         $fakecontext = context_coursecat::instance($catid);
00284         $regionname = 'a-region';
00285         $blockname = $this->get_a_known_block_type();
00286 
00287         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $fakecontext, 'page-type');
00288         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
00289 
00290         $addbm->add_block($blockname, $regionname, 0, false);
00291 
00292         // Exercise SUT.
00293         $viewbm->load_blocks();
00294         // Validate.
00295         $blocks = $viewbm->get_blocks_for_region($regionname);
00296         $this->assertContainsBlocksOfType(array(), $blocks);
00297     }
00298 
00299     public function test_block_included_in_sub_context() {
00300         global $DB;
00301         // Set up fixture.
00302         $syscontext = get_context_instance(CONTEXT_SYSTEM);
00303         $cat = new stdClass();
00304         $cat->name         = 'testcategory';
00305         $cat->parent       = 0;
00306         $cat->depth        = 1;
00307         $cat->sortorder    = 100;
00308         $cat->timemodified = time();
00309         $catid = $DB->insert_record('course_categories', $cat);
00310         $DB->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid));
00311         $childcontext = context_coursecat::instance($catid);
00312         $regionname = 'a-region';
00313         $blockname = $this->get_a_known_block_type();
00314 
00315         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
00316         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $childcontext, 'page-type');
00317 
00318         $addbm->add_block($blockname, $regionname, 0, true);
00319 
00320         // Exercise SUT.
00321         $viewbm->load_blocks();
00322         // Validate.
00323         $blocks = $viewbm->get_blocks_for_region($regionname);
00324         $this->assertContainsBlocksOfType(array($blockname), $blocks);
00325     }
00326 
00327     public function test_block_not_included_on_different_page_type() {
00328         // Set up fixture.
00329         $syscontext = get_context_instance(CONTEXT_SYSTEM);
00330         $regionname = 'a-region';
00331         $blockname = $this->get_a_known_block_type();
00332 
00333         list($addpage, $addbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'page-type');
00334         list($viewpage, $viewbm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'other-page-type');
00335 
00336         $addbm->add_block($blockname, $regionname, 0, true);
00337 
00338         // Exercise SUT.
00339         $viewbm->load_blocks();
00340         // Validate.
00341         $blocks = $viewbm->get_blocks_for_region($regionname);
00342         $this->assertContainsBlocksOfType(array(), $blocks);
00343     }
00344 
00345     public function test_block_not_included_on_different_sub_page() {
00346         // Set up fixture.
00347         $regionname = 'a-region';
00348         $blockname = $this->get_a_known_block_type();
00349         $syscontext = get_context_instance(CONTEXT_SYSTEM);
00350 
00351         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
00352                 $syscontext, 'page-type', 'sub-page');
00353 
00354         $blockmanager->add_block($blockname, $regionname, 0, true, $page->pagetype, 'other-sub-page');
00355 
00356         // Exercise SUT.
00357         $blockmanager->load_blocks();
00358         // Validate.
00359         $blocks = $blockmanager->get_blocks_for_region($regionname);
00360         $this->assertContainsBlocksOfType(array(), $blocks);
00361     }
00362 
00363     public function test_block_included_with_explicit_sub_page() {
00364         // Set up fixture.
00365         $regionname = 'a-region';
00366         $blockname = $this->get_a_known_block_type();
00367         $syscontext = get_context_instance(CONTEXT_SYSTEM);
00368 
00369         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
00370                 $syscontext, 'page-type', 'sub-page');
00371 
00372         $blockmanager->add_block($blockname, $regionname, 0, true, $page->pagetype, $page->subpage);
00373 
00374         // Exercise SUT.
00375         $blockmanager->load_blocks();
00376         // Validate.
00377         $blocks = $blockmanager->get_blocks_for_region($regionname);
00378         $this->assertContainsBlocksOfType(array($blockname), $blocks);
00379     }
00380 
00381     public function test_block_included_with_page_type_pattern() {
00382         // Set up fixture.
00383         $regionname = 'a-region';
00384         $blockname = $this->get_a_known_block_type();
00385         $syscontext = get_context_instance(CONTEXT_SYSTEM);
00386 
00387         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
00388                 $syscontext, 'page-type', 'sub-page');
00389 
00390         $blockmanager->add_block($blockname, $regionname, 0, true, 'page-*', $page->subpage);
00391 
00392         // Exercise SUT.
00393         $blockmanager->load_blocks();
00394         // Validate.
00395         $blocks = $blockmanager->get_blocks_for_region($regionname);
00396         $this->assertContainsBlocksOfType(array($blockname), $blocks);
00397     }
00398 }
00399 
 All Data Structures Namespaces Files Functions Variables Enumerations