Skip to content

Commit

Permalink
MDL-75716 phpunit: Move tests to use correct names and ns (take#5)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stronk7 committed Sep 12, 2022
1 parent 54b614c commit 65c8839
Show file tree
Hide file tree
Showing 39 changed files with 1,067 additions and 1,178 deletions.
116 changes: 43 additions & 73 deletions lib/tests/blocklib_test.php
Expand Up @@ -14,37 +14,36 @@
// 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();

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 {
Expand Down Expand Up @@ -113,7 +112,7 @@ public function test_cannot_add_region_after_loaded() {
// 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);
}

Expand Down Expand Up @@ -168,7 +167,7 @@ public function test_cannot_add_custom_region_after_loaded() {
// 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');
}

Expand All @@ -183,15 +182,15 @@ public function test_set_default_region() {

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');
}

public function test_cannot_change_default_region_after_loaded() {
// 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');
}

Expand All @@ -210,13 +209,13 @@ public function test_matching_page_type_patterns() {
}

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]);

Expand All @@ -225,7 +224,7 @@ protected function get_a_page_and_block_manager($regions, $context, $pagetype, $

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;
Expand All @@ -249,7 +248,7 @@ public function test_empty_initially() {

// 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.
Expand All @@ -263,7 +262,7 @@ public function test_adding_and_retrieving_one_block() {
// 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');
Expand All @@ -282,7 +281,7 @@ public function test_adding_and_retrieving_two_blocks() {
// 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');
Expand All @@ -302,7 +301,7 @@ public function test_adding_blocks() {
// 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');
Expand All @@ -327,7 +326,7 @@ public function test_block_instances() {
// 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');
Expand All @@ -345,9 +344,9 @@ public function test_block_not_included_in_different_context() {
$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();

Expand All @@ -367,8 +366,8 @@ public function test_block_included_in_sub_context() {
$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();

Expand All @@ -388,7 +387,7 @@ public function test_block_not_included_on_different_page_type() {
$this->purge_blocks();

// Set up fixture.
$syscontext = context_system::instance();
$syscontext = \context_system::instance();
$regionname = 'a-region';
$blockname = $this->get_a_known_block_type();

Expand All @@ -410,7 +409,7 @@ public function test_block_not_included_on_different_sub_page() {
// 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');
Expand All @@ -430,7 +429,7 @@ public function test_block_included_with_explicit_sub_page() {
// 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');
Expand All @@ -450,7 +449,7 @@ public function test_block_included_with_page_type_pattern() {
// 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');
Expand Down Expand Up @@ -521,7 +520,7 @@ public function test_delete_instances() {

$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');
Expand Down Expand Up @@ -572,9 +571,9 @@ public function test_delete_instances() {
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() {
Expand All @@ -583,7 +582,7 @@ public function test_create_all_block_instances() {
$this->setAdminUser();
$this->resetAfterTest();
$regionname = 'side-pre';
$context = context_system::instance();
$context = \context_system::instance();

$PAGE->reset_theme_and_output();
$CFG->theme = 'boost';
Expand Down Expand Up @@ -668,7 +667,7 @@ public function test_block_instance_times() {
// 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');
Expand Down Expand Up @@ -735,8 +734,8 @@ public function test_default_dashboard() {
$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');
Expand Down Expand Up @@ -772,7 +771,7 @@ public function test_default_dashboard() {
$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();
Expand Down Expand Up @@ -827,32 +826,3 @@ public function test_default_dashboard() {
$PAGE = $storedpage;
}
}

/**
* 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() {
}
}
28 changes: 28 additions & 0 deletions lib/tests/fixtures/block_ablocktype.php
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Test-specific subclass to make some protected things public.
*
* @package core
* @category test
* @copyright 2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_ablocktype extends block_base {
public function init() {
}
}
41 changes: 41 additions & 0 deletions lib/tests/fixtures/testable_block_manager.php
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Test-specific subclass to make some protected things public.
*
* @package core
* @category test
* @copyright 2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
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;
}
}

0 comments on commit 65c8839

Please sign in to comment.