diff --git a/blocks/blog_menu/block_blog_menu.php b/blocks/blog_menu/block_blog_menu.php index 3c4d9c9166c..8c9fb89e23f 100644 --- a/blocks/blog_menu/block_blog_menu.php +++ b/blocks/blog_menu/block_blog_menu.php @@ -123,4 +123,16 @@ class block_blog_menu extends block_base { public function get_aria_role() { return 'navigation'; } + + /** + * This block shouldn't be added to a page if the blogs advanced feature is disabled. + * + * @param moodle_page $page + * @return bool + */ + public function can_block_be_added(moodle_page $page): bool { + global $CFG; + + return $CFG->enableblogs; + } } diff --git a/blocks/blog_menu/tests/behat/block_blog_menu.feature b/blocks/blog_menu/tests/behat/block_blog_menu.feature index 419406b29fc..d3ab90545a7 100644 --- a/blocks/blog_menu/tests/behat/block_blog_menu.feature +++ b/blocks/blog_menu/tests/behat/block_blog_menu.feature @@ -16,13 +16,12 @@ Feature: Enable Block blog menu in a course | teacher1 | C1 | editingteacher | Scenario: Add the block to a the course when blogs are disabled - Given I log in as "admin" - And the following config values are set as admin: - | enableblogs | 0 | - And I log out - And I log in as "teacher1" + Given I log in as "teacher1" And I am on "Course 1" course homepage with editing mode on When I add the "Blog menu" block + And the following config values are set as admin: + | enableblogs | 0 | + And I reload the page Then I should see "Blogging is disabled!" in the "Blog menu" "block" Scenario: Add the block to a the course when blog associations are disabled diff --git a/blocks/blog_menu/tests/blog_menu_test.php b/blocks/blog_menu/tests/blog_menu_test.php new file mode 100644 index 00000000000..2a5e66f2d05 --- /dev/null +++ b/blocks/blog_menu/tests/blog_menu_test.php @@ -0,0 +1,63 @@ +. + +namespace block_blog_menu\tests; + +use advanced_testcase; +use block_blog_menu; +use context_course; + +/** + * PHPUnit block_blog_menu tests + * + * @package block_blog_menu + * @category test + * @copyright 2021 Sara Arjona (sara@moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @coversDefaultClass \block_blog_menu + */ +class blog_menu_test extends advanced_testcase { + public static function setUpBeforeClass(): void { + require_once(__DIR__ . '/../../moodleblock.class.php'); + require_once(__DIR__ . '/../block_blog_menu.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_blog_menu(); + + // If blogs advanced feature is enabled, the method should return true. + set_config('enableblogs', true); + $this->assertTrue($block->can_block_be_added($page)); + + // However, if the blogs advanced feature is disabled, the method should return false. + set_config('enableblogs', false); + $this->assertFalse($block->can_block_be_added($page)); + } +}