From 8246271f7cd2016bd595f9b9a82a25a7f19228e2 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Wed, 15 Dec 2021 17:00:49 +0100 Subject: [PATCH] MDL-73339 glossary_random: Add block only if main feature is enabled The Random glossary entry block should be added only if the glossary module is enabled. --- .../glossary_random/block_glossary_random.php | 12 +++- .../behat/glossary_random_addblock.feature | 23 +++++++ .../glossary_random_addblock_disabled.feature | 36 +++++++++++ .../tests/glossary_random_test.php | 64 +++++++++++++++++++ 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 blocks/glossary_random/tests/behat/glossary_random_addblock.feature create mode 100644 blocks/glossary_random/tests/behat/glossary_random_addblock_disabled.feature create mode 100644 blocks/glossary_random/tests/glossary_random_test.php diff --git a/blocks/glossary_random/block_glossary_random.php b/blocks/glossary_random/block_glossary_random.php index 42a6ebc3575..4b1e1493dc5 100644 --- a/blocks/glossary_random/block_glossary_random.php +++ b/blocks/glossary_random/block_glossary_random.php @@ -270,5 +270,15 @@ class block_glossary_random extends block_base { 'plugin' => new stdClass(), ]; } -} + /** + * This block shouldn't be added to a page if the glossary module is disabled. + * + * @param moodle_page $page + * @return bool + */ + public function can_block_be_added(moodle_page $page): bool { + $pluginclass = \core_plugin_manager::resolve_plugininfo_class('mod'); + return $pluginclass::get_enabled_plugin('glossary'); + } +} diff --git a/blocks/glossary_random/tests/behat/glossary_random_addblock.feature b/blocks/glossary_random/tests/behat/glossary_random_addblock.feature new file mode 100644 index 00000000000..05984b77aae --- /dev/null +++ b/blocks/glossary_random/tests/behat/glossary_random_addblock.feature @@ -0,0 +1,23 @@ +@block @block_glossary_random @javascript @addablocklink +Feature: Add the glossary random block when main feature is enabled + In order to add the glossary random block to my course + As a teacher + It should be added to courses only if the glossary module is enabled. + + Background: + Given the following "courses" exist: + | fullname | shortname | format | + | Course 1 | C1 | topics | + And I am on the "C1" "course" page logged in as "admin" + + Scenario: The glossary random block can be added when glossary module is enabled + Given I turn editing mode on + When I click on "Add a block" "link" + Then I should see "Random glossary entry" + + Scenario: The glossary random block cannot be added when glossary module is disabled + Given I navigate to "Plugins > Activity modules > Manage activities" in site administration + And I click on "Hide" "icon" in the "Glossary" "table_row" + And I am on "Course 1" course homepage with editing mode on + When I click on "Add a block" "link" + Then I should not see "Random glossary entry" diff --git a/blocks/glossary_random/tests/behat/glossary_random_addblock_disabled.feature b/blocks/glossary_random/tests/behat/glossary_random_addblock_disabled.feature new file mode 100644 index 00000000000..0b20b1f3901 --- /dev/null +++ b/blocks/glossary_random/tests/behat/glossary_random_addblock_disabled.feature @@ -0,0 +1,36 @@ +@block @block_glossary_random @javascript +Feature: Add the glossary random block when main feature is disabled + In order to add the glossary random block to my course + As a teacher + It should be added to courses only if the glossary module is enabled. + + Background: + Given the following "courses" exist: + | fullname | shortname | format | + | Course 1 | C1 | topics | + And I am on the "C1" "course" page logged in as "admin" + + Scenario: The glossary random block is displayed even when glossary module is disabled + Given I turn editing mode on + And I add the "Random glossary entry" block + When I navigate to "Plugins > Activity modules > Manage activities" in site administration + And I click on "Hide" "icon" in the "Glossary" "table_row" + And I am on "Course 1" course homepage with editing mode on + Then I should see "Random glossary entry" + + Scenario: The glossary random block can be removed even when glossary module is disabled + Given I turn editing mode on + And I add the "Random glossary entry" block + And I open the "Random glossary entry" blocks action menu + And I click on "Delete Random glossary entry block" "link" in the "Random glossary entry" "block" + And "Delete block?" "dialogue" should exist + And I click on "Cancel" "button" in the "Delete block?" "dialogue" + And I should see "Random glossary entry" + When I navigate to "Plugins > Activity modules > Manage activities" in site administration + And I click on "Hide" "icon" in the "Glossary" "table_row" + And I am on "Course 1" course homepage with editing mode on + And I open the "Random glossary entry" blocks action menu + And I click on "Delete Random glossary entry block" "link" in the "Random glossary entry" "block" + And "Delete block?" "dialogue" should exist + And I click on "Delete" "button" in the "Delete block?" "dialogue" + Then I should not see "Random glossary entry" diff --git a/blocks/glossary_random/tests/glossary_random_test.php b/blocks/glossary_random/tests/glossary_random_test.php new file mode 100644 index 00000000000..2b9e2b2d0d3 --- /dev/null +++ b/blocks/glossary_random/tests/glossary_random_test.php @@ -0,0 +1,64 @@ +. + +namespace block_glossary_random\tests; + +use advanced_testcase; +use block_glossary_random; +use context_course; + +/** + * PHPUnit block_glossary_random tests + * + * @package block_glossary_random + * @category test + * @copyright 2021 Sara Arjona (sara@moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \block_glossary_random + */ +class glossary_random_test extends advanced_testcase { + public static function setUpBeforeClass(): void { + require_once(__DIR__ . '/../../moodleblock.class.php'); + require_once(__DIR__ . '/../block_glossary_random.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_glossary_random(); + $pluginclass = \core_plugin_manager::resolve_plugininfo_class('mod'); + + // If glossary module is enabled, the method should return true. + $pluginclass::enable_plugin('glossary', 1); + $this->assertTrue($block->can_block_be_added($page)); + + // However, if the glossary module is disabled, the method should return false. + $pluginclass::enable_plugin('glossary', 0); + $this->assertFalse($block->can_block_be_added($page)); + } +}