MDL-75716 phpunit: Move tests to use correct names and ns (take#5)
Applied the following changes to various testcase classes: - Namespaced with component[\level2-API] - Moved to level2-API subdirectory when required. - Fixed incorrect use statements with leading backslash. - Remove file phpdoc block - Remove MOODLE_INTERNAL if not needed. - Changed code to point to global scope when needed. - Fix some relative paths and comments here and there. - All them passing individually. - Complete runs passing too. Special mention to: - In lib/tests/blocklib_test.php 2 helper classes have been moved to tests/fixtures, because they needed to be namespace-free.
This commit is contained in:
+44
-74
@@ -14,14 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Tests for the block_manager class in ../blocklib.php.
|
||||
*
|
||||
* @package core
|
||||
* @category phpunit
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace core;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
@@ -29,22 +22,28 @@ global $CFG;
|
||||
require_once($CFG->libdir . '/pagelib.php');
|
||||
require_once($CFG->libdir . '/blocklib.php');
|
||||
require_once($CFG->dirroot . '/blocks/moodleblock.class.php');
|
||||
|
||||
require_once(__DIR__ . '/fixtures/block_ablocktype.php');
|
||||
require_once(__DIR__ . '/fixtures/testable_block_manager.php');
|
||||
|
||||
/**
|
||||
* Test various block related classes.
|
||||
* Tests for the block_manager class in ../blocklib.php.
|
||||
*
|
||||
* @package core
|
||||
* @category test
|
||||
* @copyright 2009 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class core_blocklib_testcase extends advanced_testcase {
|
||||
class blocklib_test extends \advanced_testcase {
|
||||
protected $testpage;
|
||||
protected $blockmanager;
|
||||
protected $isediting = null;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->testpage = new moodle_page();
|
||||
$this->testpage->set_context(context_system::instance());
|
||||
$this->testpage = new \moodle_page();
|
||||
$this->testpage->set_context(\context_system::instance());
|
||||
$this->testpage->set_pagetype('phpunit-block-test');
|
||||
$this->blockmanager = new testable_block_manager($this->testpage);
|
||||
$this->blockmanager = new \testable_block_manager($this->testpage);
|
||||
}
|
||||
|
||||
protected function tearDown(): void {
|
||||
@@ -113,7 +112,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$this->blockmanager->mark_loaded();
|
||||
// Exercise SUT.
|
||||
$this->expectException(coding_exception::class);
|
||||
$this->expectException(\coding_exception::class);
|
||||
$this->blockmanager->add_region('too-late', false);
|
||||
}
|
||||
|
||||
@@ -168,7 +167,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$this->blockmanager->mark_loaded();
|
||||
// Exercise SUT.
|
||||
$this->expectException(coding_exception::class);
|
||||
$this->expectException(\coding_exception::class);
|
||||
$this->blockmanager->add_region('too-late');
|
||||
}
|
||||
|
||||
@@ -183,7 +182,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
|
||||
public function test_cannot_set_unknown_region_as_default() {
|
||||
// Exercise SUT.
|
||||
$this->expectException(coding_exception::class);
|
||||
$this->expectException(\coding_exception::class);
|
||||
$this->blockmanager->set_default_region('a-region-name');
|
||||
}
|
||||
|
||||
@@ -191,7 +190,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$this->blockmanager->mark_loaded();
|
||||
// Exercise SUT.
|
||||
$this->expectException(coding_exception::class);
|
||||
$this->expectException(\coding_exception::class);
|
||||
$this->blockmanager->set_default_region('too-late');
|
||||
}
|
||||
|
||||
@@ -210,13 +209,13 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
}
|
||||
|
||||
protected function get_a_page_and_block_manager($regions, $context, $pagetype, $subpage = '') {
|
||||
$page = new moodle_page;
|
||||
$page = new \moodle_page;
|
||||
$page->set_context($context);
|
||||
$page->set_pagetype($pagetype);
|
||||
$page->set_subpage($subpage);
|
||||
$page->set_url(new moodle_url('/'));
|
||||
$page->set_url(new \moodle_url('/'));
|
||||
|
||||
$blockmanager = new testable_block_manager($page);
|
||||
$blockmanager = new \testable_block_manager($page);
|
||||
$blockmanager->add_regions($regions, false);
|
||||
$blockmanager->set_default_region($regions[0]);
|
||||
|
||||
@@ -225,7 +224,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
|
||||
protected function get_a_known_block_type() {
|
||||
global $DB;
|
||||
$block = new stdClass;
|
||||
$block = new \stdClass;
|
||||
$block->name = 'ablocktype';
|
||||
$DB->insert_record('block', $block);
|
||||
return $block->name;
|
||||
@@ -249,7 +248,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
|
||||
// Set up fixture.
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array('a-region'),
|
||||
context_system::instance(), 'page-type');
|
||||
\context_system::instance(), 'page-type');
|
||||
// Exercise SUT.
|
||||
$blockmanager->load_blocks();
|
||||
// Validate.
|
||||
@@ -263,7 +262,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$context = context_system::instance();
|
||||
$context = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$context, 'page-type');
|
||||
@@ -282,7 +281,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$context = context_system::instance();
|
||||
$context = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$context, 'page-type');
|
||||
@@ -302,7 +301,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$context = context_system::instance();
|
||||
$context = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$context, 'page-type');
|
||||
@@ -327,7 +326,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$context = context_system::instance();
|
||||
$context = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$context, 'page-type');
|
||||
@@ -345,9 +344,9 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
$this->purge_blocks();
|
||||
|
||||
// Set up fixture.
|
||||
$syscontext = context_system::instance();
|
||||
$syscontext = \context_system::instance();
|
||||
$cat = $this->getDataGenerator()->create_category(array('name' => 'testcategory'));
|
||||
$fakecontext = context_coursecat::instance($cat->id);
|
||||
$fakecontext = \context_coursecat::instance($cat->id);
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
|
||||
@@ -367,8 +366,8 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
$this->purge_blocks();
|
||||
|
||||
// Set up fixture.
|
||||
$syscontext = context_system::instance();
|
||||
$childcontext = context_coursecat::instance(1);
|
||||
$syscontext = \context_system::instance();
|
||||
$childcontext = \context_coursecat::instance(1);
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
|
||||
@@ -388,7 +387,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
$this->purge_blocks();
|
||||
|
||||
// Set up fixture.
|
||||
$syscontext = context_system::instance();
|
||||
$syscontext = \context_system::instance();
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
|
||||
@@ -410,7 +409,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$syscontext = context_system::instance();
|
||||
$syscontext = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$syscontext, 'page-type', 'sub-page');
|
||||
@@ -430,7 +429,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$syscontext = context_system::instance();
|
||||
$syscontext = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$syscontext, 'page-type', 'sub-page');
|
||||
@@ -450,7 +449,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$syscontext = context_system::instance();
|
||||
$syscontext = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$syscontext, 'page-type', 'sub-page');
|
||||
@@ -521,7 +520,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$context = context_system::instance();
|
||||
$context = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$context, 'page-type');
|
||||
@@ -572,9 +571,9 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
list($insql, $inparams) = $DB->get_in_or_equal($preferences);
|
||||
$this->assertEquals(2, $DB->count_records_select('user_preferences', "name $insql", $inparams));
|
||||
|
||||
$this->assertFalse(context_block::instance($blockids[0], IGNORE_MISSING));
|
||||
$this->assertFalse(context_block::instance($blockids[1], IGNORE_MISSING));
|
||||
context_block::instance($tokeep); // Would throw an exception if it was deleted.
|
||||
$this->assertFalse(\context_block::instance($blockids[0], IGNORE_MISSING));
|
||||
$this->assertFalse(\context_block::instance($blockids[1], IGNORE_MISSING));
|
||||
\context_block::instance($tokeep); // Would throw an exception if it was deleted.
|
||||
}
|
||||
|
||||
public function test_create_all_block_instances() {
|
||||
@@ -583,7 +582,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
$this->setAdminUser();
|
||||
$this->resetAfterTest();
|
||||
$regionname = 'side-pre';
|
||||
$context = context_system::instance();
|
||||
$context = \context_system::instance();
|
||||
|
||||
$PAGE->reset_theme_and_output();
|
||||
$CFG->theme = 'boost';
|
||||
@@ -668,7 +667,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
// Set up fixture.
|
||||
$regionname = 'a-region';
|
||||
$blockname = 'html';
|
||||
$context = context_system::instance();
|
||||
$context = \context_system::instance();
|
||||
|
||||
list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
|
||||
$context, 'page-type');
|
||||
@@ -735,8 +734,8 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
$regionname = 'a-region';
|
||||
$blockname = $this->get_a_known_block_type();
|
||||
$user = self::getDataGenerator()->create_user();
|
||||
$syscontext = context_system::instance();
|
||||
$usercontext = context_user::instance($user->id);
|
||||
$syscontext = \context_system::instance();
|
||||
$usercontext = \context_user::instance($user->id);
|
||||
// Add sitewide 'sticky' blocks. The page is not setup exactly as a site page would be...
|
||||
// but it does seem to mean that the bloacks are added correctly.
|
||||
list($sitepage, $sitebm) = $this->get_a_page_and_block_manager(array($regionname), $syscontext, 'site-index');
|
||||
@@ -772,7 +771,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
$mybm->load_blocks();
|
||||
$mybm->reposition_block($sitestickyblock2->id, $regionname, -1);
|
||||
// Reload the blocks in the managers.
|
||||
context_helper::reset_caches();
|
||||
\context_helper::reset_caches();
|
||||
$defaultmybm->reset_caches();
|
||||
$this->assertNull($defaultmybm->get_loaded_blocks());
|
||||
$defaultmybm->load_blocks();
|
||||
@@ -838,7 +837,7 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
$this->setAdminUser();
|
||||
$this->resetAfterTest();
|
||||
$regionname = 'side-pre';
|
||||
$context = context_system::instance();
|
||||
$context = \context_system::instance();
|
||||
|
||||
$PAGE->reset_theme_and_output();
|
||||
$CFG->theme = 'boost';
|
||||
@@ -864,32 +863,3 @@ class core_blocklib_testcase extends advanced_testcase {
|
||||
$this->assertEmpty($blocks);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test-specific subclass to make some protected things public.
|
||||
*/
|
||||
class testable_block_manager extends block_manager {
|
||||
/**
|
||||
* Resets the caches in the block manager.
|
||||
* This allows blocks to be reloaded correctly.
|
||||
*/
|
||||
public function reset_caches() {
|
||||
$this->birecordsbyregion = null;
|
||||
$this->blockinstances = array();
|
||||
$this->visibleblockcontent = array();
|
||||
}
|
||||
public function mark_loaded() {
|
||||
$this->birecordsbyregion = array();
|
||||
}
|
||||
public function get_loaded_blocks() {
|
||||
return $this->birecordsbyregion;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test-specific subclass to make some protected things public.
|
||||
*/
|
||||
class block_ablocktype extends block_base {
|
||||
public function init() {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user