From b90bc54f4b41d065c5adabef73d862087f3460cd Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Wed, 15 Dec 2021 18:47:41 +0100 Subject: [PATCH] MDL-73339 mnet_hosts: Add block only if main feature is enabled The Network servers block should be added only if the MNet authentication is enabled. --- blocks/mnet_hosts/block_mnet_hosts.php | 10 +++ .../tests/behat/mnet_hosts_addblock.feature | 21 ++++++ .../mnet_hosts_addblock_disabled.feature | 41 ++++++++++++ blocks/mnet_hosts/tests/mnet_hosts_test.php | 64 +++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 blocks/mnet_hosts/tests/behat/mnet_hosts_addblock.feature create mode 100644 blocks/mnet_hosts/tests/behat/mnet_hosts_addblock_disabled.feature create mode 100644 blocks/mnet_hosts/tests/mnet_hosts_test.php diff --git a/blocks/mnet_hosts/block_mnet_hosts.php b/blocks/mnet_hosts/block_mnet_hosts.php index 9e0715cf496..b36ecc5f033 100644 --- a/blocks/mnet_hosts/block_mnet_hosts.php +++ b/blocks/mnet_hosts/block_mnet_hosts.php @@ -153,4 +153,14 @@ class block_mnet_hosts extends block_list { return $this->content; } + + /** + * This block shouldn't be added to a page if the mnet authentication method is disabled. + * + * @param moodle_page $page + * @return bool + */ + public function can_block_be_added(moodle_page $page): bool { + return is_enabled_auth('mnet'); + } } diff --git a/blocks/mnet_hosts/tests/behat/mnet_hosts_addblock.feature b/blocks/mnet_hosts/tests/behat/mnet_hosts_addblock.feature new file mode 100644 index 00000000000..7130841b83b --- /dev/null +++ b/blocks/mnet_hosts/tests/behat/mnet_hosts_addblock.feature @@ -0,0 +1,21 @@ +@block @block_mnet_hosts @javascript @addablocklink +Feature: Add the network servers block when main feature is enabled + In order to add the Network servers block to my course + As a teacher + It should be added only if the MNet authentication is enabled. + + Scenario: The network servers block can be added when mnet authentication is enabled + Given I log in as "admin" + And I navigate to "Plugins > Authentication > Manage authentication" in site administration + And I click on "Enable" "icon" in the "MNet authentication" "table_row" + And I am on site homepage + And I turn editing mode on + When I click on "Add a block" "link" + Then I should see "Network servers" + + Scenario: The network servers block cannot be added when mnet authentication is disabled + Given I log in as "admin" + And I am on site homepage + And I turn editing mode on + When I click on "Add a block" "link" + Then I should not see "Network servers" diff --git a/blocks/mnet_hosts/tests/behat/mnet_hosts_addblock_disabled.feature b/blocks/mnet_hosts/tests/behat/mnet_hosts_addblock_disabled.feature new file mode 100644 index 00000000000..b084fa2a269 --- /dev/null +++ b/blocks/mnet_hosts/tests/behat/mnet_hosts_addblock_disabled.feature @@ -0,0 +1,41 @@ +@block @block_mnet_hosts @javascript +Feature: Add the network servers block when main feature is disabled + In order to add the Network servers block to my course + As a teacher + It should be added only if the MNet authentication is enabled. + + Scenario: The network servers block is displayed even when mnet authentication is disabled + Given I log in as "admin" + And I navigate to "Plugins > Authentication > Manage authentication" in site administration + And I click on "Enable" "icon" in the "MNet authentication" "table_row" + And I am on site homepage + And I turn editing mode on + And I add the "Network servers" block + When I navigate to "Plugins > Authentication > Manage authentication" in site administration + And I click on "Disable" "icon" in the "MNet authentication" "table_row" + And I am on site homepage + And I turn editing mode on + Then I should see "Network servers" + + Scenario: The network servers block can be removed even when mnet authentication is disabled + Given I log in as "admin" + And I navigate to "Plugins > Authentication > Manage authentication" in site administration + And I click on "Enable" "icon" in the "MNet authentication" "table_row" + And I am on site homepage + And I turn editing mode on + And I add the "Network servers" block + And I turn editing mode on + And I open the "Network servers" blocks action menu + And I click on "Delete Network servers block" "link" in the "Network servers" "block" + And "Delete block?" "dialogue" should exist + And I click on "Cancel" "button" in the "Delete block?" "dialogue" + And I should see "Network servers" + And I navigate to "Plugins > Authentication > Manage authentication" in site administration + And I click on "Disable" "icon" in the "MNet authentication" "table_row" + And I am on site homepage + And I turn editing mode on + And I open the "Network servers" blocks action menu + And I click on "Delete Network servers block" "link" in the "Network servers" "block" + And "Delete block?" "dialogue" should exist + And I click on "Delete" "button" in the "Delete block?" "dialogue" + Then I should not see "Network servers" diff --git a/blocks/mnet_hosts/tests/mnet_hosts_test.php b/blocks/mnet_hosts/tests/mnet_hosts_test.php new file mode 100644 index 00000000000..e831f321cf0 --- /dev/null +++ b/blocks/mnet_hosts/tests/mnet_hosts_test.php @@ -0,0 +1,64 @@ +. + +namespace block_mnet_hosts\tests; + +use advanced_testcase; +use block_mnet_hosts; +use context_course; + +/** + * PHPUnit block_mnet_hosts tests + * + * @package block_mnet_hosts + * @category test + * @copyright 2021 Sara Arjona (sara@moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \block_mnet_hosts + */ +class mnet_hosts_test extends advanced_testcase { + public static function setUpBeforeClass(): void { + require_once(__DIR__ . '/../../moodleblock.class.php'); + require_once(__DIR__ . '/../block_mnet_hosts.php'); + } + + /** + * Test the behaviour of can_block_be_added() method. + * + * @covers ::can_block_be_added + */ + public function test_can_block_be_added(): void { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Create a course and prepare the page where the block will be added. + $course = $this->getDataGenerator()->create_course(); + $page = new \moodle_page(); + $page->set_context(context_course::instance($course->id)); + $page->set_pagelayout('course'); + + $block = new block_mnet_hosts(); + $pluginclass = \core_plugin_manager::resolve_plugininfo_class('auth'); + + // If mnet authentication is enabled, the method should return true. + $pluginclass::enable_plugin('mnet', 1); + $this->assertTrue($block->can_block_be_added($page)); + + // However, if the mnet authentication is disabled, the method should return false. + $pluginclass::enable_plugin('mnet', 0); + $this->assertFalse($block->can_block_be_added($page)); + } +}