Merge branch 'MDL-73339-master' of https://github.com/sarjona/moodle

This commit is contained in:
Shamim Rezaie 2021-12-31 11:04:57 +11:00
commit 33f1288675
37 changed files with 1188 additions and 43 deletions

View File

@ -164,6 +164,18 @@ class block_accessreview extends block_base {
return $this->content;
}
/**
* This block shouldn't be added to a page if the accessibility tools setting is disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
global $CFG;
return $CFG->enableaccessibilitytools;
}
/**
* Fetches and groups the relevent error data for the table to display.
* @param int $courseid The ID of the course.

View File

@ -28,6 +28,7 @@ use context_course;
* @copyright 2020 onward: Learning Technology Services, www.lts.ie
* @author Jay Churchward (jay.churchward@poetopensource.org)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \block_accessreview
*/
class accessibility_review_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
@ -96,4 +97,30 @@ class accessibility_review_test extends advanced_testcase {
$result = $rm->invoke($block, context_course::instance($course->id));
$this->assertEmpty($result);
}
/**
* 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_accessreview();
// If the accessibility tools is enabled, the method should return true.
set_config('enableaccessibilitytools', true);
$this->assertTrue($block->can_block_be_added($page));
// However, if the accessibility tools is disabled, the method should return false.
set_config('enableaccessibilitytools', false);
$this->assertFalse($block->can_block_be_added($page));
}
}

View File

@ -22,17 +22,13 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . "/badgeslib.php");
/**
* Displays recent badges
*/
class block_badges extends block_base {
public function init() {
global $CFG;
require_once($CFG->libdir . "/badgeslib.php");
$this->title = get_string('pluginname', 'block_badges');
}
@ -105,4 +101,16 @@ class block_badges extends block_base {
return $this->content;
}
/**
* This block shouldn't be added to a page if the badges advanced feature is disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
global $CFG;
return $CFG->enablebadges;
}
}

View File

@ -0,0 +1,63 @@
<?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/>.
namespace block_badges\tests;
use advanced_testcase;
use block_badges;
use context_course;
/**
* PHPUnit block_badges tests
*
* @package block_badges
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \block_badges
*/
class badges_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_badges.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_badges();
// If badges advanced feature is enabled, the method should return true.
set_config('enablebadges', true);
$this->assertTrue($block->can_block_be_added($page));
// However, if the badges advanced feature is disabled, the method should return false.
set_config('enablebadges', false);
$this->assertFalse($block->can_block_be_added($page));
}
}

View File

@ -16,13 +16,12 @@ Feature: Enable Block Badges in a course without badges
| teacher1 | C1 | editingteacher |
Scenario: Add the block to a the course when badges are disabled
Given I log in as "admin"
And the following config values are set as admin:
| enablebadges | 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 "Latest badges" block
And the following config values are set as admin:
| enablebadges | 0 |
And I reload the page
Then I should see "Badges are not enabled on this site." in the "Latest badges" "block"
Scenario: Add the block to a the course when badges are enabled

View File

@ -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;
}
}

View File

@ -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

View File

@ -0,0 +1,63 @@
<?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/>.
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));
}
}

View File

@ -142,4 +142,16 @@ class block_blog_recent extends block_base {
'plugin' => new stdClass(),
];
}
/**
* 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;
}
}

View File

@ -16,13 +16,12 @@ Feature: Feature: Users can use the recent blog entries block to view recent blo
| teacher1 | C1 | editingteacher |
Scenario: Add the recent blogs block to a 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 "Recent blog entries" 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 "Recent blog entries" "block"
Scenario: Add the recent blogs block to a course when there are not any blog posts

View File

@ -0,0 +1,63 @@
<?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/>.
namespace block_blog_recent\tests;
use advanced_testcase;
use block_blog_recent;
use context_course;
/**
* PHPUnit block_blog_recent tests
*
* @package block_blog_recent
* @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_recent
*/
class blog_recent_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_blog_recent.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_recent();
// 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));
}
}

View File

@ -222,6 +222,18 @@ class block_blog_tags extends block_base {
'plugin' => new stdClass(),
];
}
/**
* This block shouldn't be added to a page if the blogs and the tags advanced features are disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
global $CFG;
return $CFG->enableblogs && $CFG->usetags;
}
}
function block_blog_tags_sort($a, $b) {
@ -241,5 +253,3 @@ function block_blog_tags_sort($a, $b) {
return 0;
}
}

View File

@ -0,0 +1,71 @@
<?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/>.
namespace block_blog_tags\tests;
use advanced_testcase;
use block_blog_tags;
use context_course;
/**
* PHPUnit block_blog_tags tests
*
* @package block_blog_tags
* @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_tags
*/
class blog_tags_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_blog_tags.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_tags();
// If blogs and tags advanced features are enabled, the method should return true.
set_config('enableblogs', true);
set_config('usetags', true);
$this->assertTrue($block->can_block_be_added($page));
// However, if any of these advanced features is disabled, the method should return false.
set_config('enableblogs', false);
$this->assertFalse($block->can_block_be_added($page));
set_config('enableblogs', true);
set_config('usetags', false);
$this->assertFalse($block->can_block_be_added($page));
set_config('enableblogs', false);
$this->assertFalse($block->can_block_be_added($page));
}
}

View File

@ -21,15 +21,13 @@
* @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Obviously required
require_once($CFG->dirroot . '/comment/lib.php');
class block_comments extends block_base {
function init() {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
$this->title = get_string('pluginname', 'block_comments');
}
@ -47,6 +45,7 @@ class block_comments extends block_base {
function get_content() {
global $CFG;
if ($this->content !== NULL) {
return $this->content;
}
@ -85,4 +84,16 @@ class block_comments extends block_base {
$this->content->footer = '';
return $this->content;
}
/**
* This block shouldn't be added to a page if the comments advanced feature is disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
global $CFG;
return $CFG->usecomments;
}
}

View File

@ -0,0 +1,63 @@
<?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/>.
namespace block_comments\tests;
use advanced_testcase;
use block_comments;
use context_course;
/**
* PHPUnit block_comments tests
*
* @package block_comments
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \block_comments
*/
class comments_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_comments.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_comments();
// If comments advanced feature is enabled, the method should return true.
set_config('usecomments', true);
$this->assertTrue($block->can_block_be_added($page));
// However, if the comments advanced feature is disabled, the method should return false.
set_config('usecomments', false);
$this->assertFalse($block->can_block_be_added($page));
}
}

View File

@ -15,25 +15,21 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Block for displayed logged in user's course completion status
* Block for displayed logged in user's course completion status.
* Displays overall, and individual criteria status for logged in user.
*
* @package block_completionstatus
* @copyright 2009-2012 Catalyst IT Ltd
* @author Aaron Barnes <aaronb@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once("{$CFG->libdir}/completionlib.php");
/**
* Course completion status.
* Displays overall, and individual criteria status for logged in user.
*/
class block_completionstatus extends block_base {
public function init() {
global $CFG;
require_once("{$CFG->libdir}/completionlib.php");
$this->title = get_string('pluginname', 'block_completionstatus');
}
@ -253,4 +249,16 @@ class block_completionstatus extends block_base {
return $this->content;
}
/**
* This block shouldn't be added to a page if the completion tracking advanced feature is disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
global $CFG;
return $CFG->enablecompletion;
}
}

View File

@ -0,0 +1,63 @@
<?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/>.
namespace block_completionstatus\tests;
use advanced_testcase;
use block_completionstatus;
use context_course;
/**
* PHPUnit block_completionstatus tests
*
* @package block_completionstatus
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \block_completionstatus
*/
class completionstatus_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_completionstatus.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_completionstatus();
// If blogs advanced feature is enabled, the method should return true.
set_config('enablecompletion', true);
$this->assertTrue($block->can_block_be_added($page));
// However, if the blogs advanced feature is disabled, the method should return false.
set_config('enablecompletion', false);
$this->assertFalse($block->can_block_be_added($page));
}
}

View File

@ -78,4 +78,16 @@ class block_globalsearch extends block_base {
return $this->content;
}
/**
* This block shouldn't be added to a page if the global search advanced feature is disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
global $CFG;
return $CFG->enableglobalsearch;
}
}

View File

@ -0,0 +1,63 @@
<?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/>.
namespace block_globalsearch\tests;
use advanced_testcase;
use block_globalsearch;
use context_course;
/**
* PHPUnit block_globalsearch tests
*
* @package block_globalsearch
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \block_globalsearch
*/
class globalsearch_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_globalsearch.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_globalsearch();
// If global search advanced feature is enabled, the method should return true.
set_config('enableglobalsearch', true);
$this->assertTrue($block->can_block_be_added($page));
// However, if the global search advanced feature is disabled, the method should return false.
set_config('enableglobalsearch', false);
$this->assertFalse($block->can_block_be_added($page));
}
}

View File

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

View File

@ -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"

View File

@ -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"

View File

@ -0,0 +1,64 @@
<?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/>.
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));
}
}

View File

@ -81,4 +81,13 @@ class block_lp extends block_base {
return $this->content;
}
/**
* This block shouldn't be added to a page if the competencies advanced feature is disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
return get_config('core_competency', 'enabled');
}
}

View File

@ -0,0 +1,63 @@
<?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/>.
namespace block_lp\tests;
use advanced_testcase;
use block_lp;
use context_course;
/**
* PHPUnit block_lp tests
*
* @package block_lp
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \block_lp
*/
class lp_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_lp.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_lp();
// If blogs advanced feature is enabled, the method should return true.
set_config('enabled', true, 'core_competency');
$this->assertTrue($block->can_block_be_added($page));
// However, if the blogs advanced feature is disabled, the method should return false.
set_config('enabled', false, 'core_competency');
$this->assertFalse($block->can_block_be_added($page));
}
}

View File

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

View File

@ -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"

View File

@ -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"

View File

@ -0,0 +1,64 @@
<?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/>.
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));
}
}

View File

@ -747,6 +747,18 @@ EOD;
public function get_aria_role() {
return 'complementary';
}
/**
* This method can be overriden to add some extra checks to decide whether the block can be added or not to a page.
* It doesn't need to do the standard capability checks as they will be performed by has_add_block_capability().
* This method is user agnostic. If you want to check if a user can add a block or not, you should use user_can_addto().
*
* @param moodle_page $page The page where this block will be added.
* @return bool Whether the block can be added or not to the given page.
*/
public function can_block_be_added(moodle_page $page): bool {
return true;
}
}
/**

View File

@ -125,4 +125,16 @@ class block_tags extends block_base {
'plugin' => new stdClass(),
];
}
/**
* This block shouldn't be added to a page if the tags advanced feature is disabled.
*
* @param moodle_page $page
* @return bool
*/
public function can_block_be_added(moodle_page $page): bool {
global $CFG;
return $CFG->usetags;
}
}

View File

@ -0,0 +1,63 @@
<?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/>.
namespace block_tags\tests;
use advanced_testcase;
use block_tags;
use context_course;
/**
* PHPUnit block_tags tests
*
* @package block_tags
* @category test
* @copyright 2021 Sara Arjona (sara@moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \block_tags
*/
class tags_test extends advanced_testcase {
public static function setUpBeforeClass(): void {
require_once(__DIR__ . '/../../moodleblock.class.php');
require_once(__DIR__ . '/../block_tags.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_tags();
// If blogs advanced feature is enabled, the method should return true.
set_config('usetags', true);
$this->assertTrue($block->can_block_be_added($page));
// However, if the blogs advanced feature is disabled, the method should return false.
set_config('usetags', false);
$this->assertFalse($block->can_block_be_added($page));
}
}

View File

@ -0,0 +1,53 @@
@block @core_block @javascript @addablocklink
Feature: Add a block when main feature is enabled
In order to add a block to my course
As a teacher
Some blocks should be only added to courses if the main feature they are based on 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 Outline: The block can be added when main feature is enabled
Given the following config values are set as admin:
| <settingname1> | 1 | <settingplugin1> |
| <settingname2> | 1 | |
And I turn editing mode on
When I click on "Add a block" "link"
Then I should see "<blockname>"
Examples:
| blockname | settingname1 | settingname2 | settingplugin1 |
| Accessibility review | enableaccessibilitytools | | |
| Blog menu | enableblogs | | |
| Recent blog entries | enableblogs | | |
| Blog tags | enableblogs | usetags | |
| Comments | usecomments | | |
| Course completion status | enablecompletion | | |
| Global search | enableglobalsearch | | |
| Latest badges | enablebadges | | |
| Tags | usetags | | |
| Learning plans | enabled | | core_competency |
Scenario Outline: The block cannot be added when main feature is disabled
Given the following config values are set as admin:
| <settingname1> | 0 | <settingplugin1> |
| <settingname2> | 0 | |
And I turn editing mode on
When I click on "Add a block" "link"
Then I should not see "<blockname>"
Examples:
| blockname | settingname1 | settingname2 | settingplugin1 |
| Accessibility review | enableaccessibilitytools | | |
| Blog menu | enableblogs | | |
| Recent blog entries | enableblogs | | |
| Blog tags | enableblogs | usetags | |
| Comments | usecomments | | |
| Course completion status | enablecompletion | | |
| Global search | enableglobalsearch | | |
| Latest badges | enablebadges | | |
| Tags | usetags | | |
| Learning plans | enabled | | core_competency |

View File

@ -0,0 +1,101 @@
@block @core_block @javascript
Feature: Add a block when main feature is disabled
In order to add a block to my course
As a teacher
Some blocks should be only added to courses if the main feature they are based on 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 Outline: The block is displayed even when main feature is disabled
Given the following config values are set as admin:
| <settingname1> | 1 | <settingplugin1> |
And I turn editing mode on
And I add the "<blockname>" block
When the following config values are set as admin:
| <settingname1> | 0 | <settingplugin1> |
Then I should see "<blockname>"
Examples:
| blockname | settingname1 | settingplugin1 |
| Accessibility review | enableaccessibilitytools | |
| Blog menu | enableblogs | |
| Recent blog entries | enableblogs | |
| Comments | usecomments | |
| Course completion status | enablecompletion | |
| Global search | enableglobalsearch | |
| Latest badges | enablebadges | |
| Tags | usetags | |
| Learning plans | enabled | core_competency |
Scenario Outline: The block is displayed even when main feature is disabled (2 settings)
Given the following config values are set as admin:
| <settingname1> | 1 |
| <settingname2> | 1 |
And I turn editing mode on
And I add the "<blockname>" block
When the following config values are set as admin:
| <settingname1> | 0 |
| <settingname2> | 0 |
Then I should see "<blockname>"
Examples:
| blockname | settingname1 | settingname2 |
| Blog tags | enableblogs | usetags |
Scenario Outline: The block can be removed even when main feature is disabled
Given the following config values are set as admin:
| <settingname1> | 1 | <settingplugin1> |
And I turn editing mode on
And I add the "<blockname>" block
And I open the "<blockname>" blocks action menu
And I click on "Delete <blockname> block" "link" in the "<blockname>" "block"
And "Delete block?" "dialogue" should exist
And I click on "Cancel" "button" in the "Delete block?" "dialogue"
And I should see "<blockname>"
When the following config values are set as admin:
| <settingname1> | 0 | <settingplugin1> |
And I open the "<blockname>" blocks action menu
And I click on "Delete <blockname> block" "link" in the "<blockname>" "block"
And "Delete block?" "dialogue" should exist
And I click on "Delete" "button" in the "Delete block?" "dialogue"
Then I should not see "<blockname>"
Examples:
| blockname | settingname1 | settingplugin1 |
| Accessibility review | enableaccessibilitytools | |
| Blog menu | enableblogs | |
| Recent blog entries | enableblogs | |
| Comments | usecomments | |
| Course completion status | enablecompletion | |
| Global search | enableglobalsearch | |
| Latest badges | enablebadges | |
| Tags | usetags | |
| Learning plans | enabled | core_competency |
Scenario Outline: The block can be removed even when main feature is disabled (2 settings)
Given the following config values are set as admin:
| <settingname1> | 1 |
| <settingname2> | 1 |
And I turn editing mode on
And I add the "<blockname>" block
And I open the "<blockname>" blocks action menu
And I click on "Delete <blockname> block" "link" in the "<blockname>" "block"
And "Delete block?" "dialogue" should exist
And I click on "Cancel" "button" in the "Delete block?" "dialogue"
And I should see "<blockname>"
When the following config values are set as admin:
| <settingname1> | 0 |
| <settingname2> | 0 |
And I open the "<blockname>" blocks action menu
And I click on "Delete <blockname> block" "link" in the "<blockname>" "block"
And "Delete block?" "dialogue" should exist
And I click on "Delete" "button" in the "Delete block?" "dialogue"
Then I should not see "<blockname>"
Examples:
| blockname | settingname1 | settingname2 |
| Blog tags | enableblogs | usetags |

View File

@ -8,6 +8,8 @@ The Quiz results block is hidden by default since Moodle 2.9. It is recommended
* External function core_block::get_dashboard_blocks has a new parameter to indicate if you want to receive the block on the my/courses page.
* The `core_block_fetch_addable_blocks` external method accepts an optional `subpage` parameter, in order to correctly
calculate available blocks for pages that use this property (e.g. the user dashboard)
* A new method, can_block_be_added(), has been added to let blocks override it when they want to include some extra checks
to decide whether the block can be added to a page or not.
=== 3.8 ===
* Block block_community is no longer a part of core.
@ -108,4 +110,3 @@ required changes in code:
* use 'pluginname' lang pack identifier instead of 'blockname'
* move cron and version number into standard version.php
* removed support for old config_global.html, use settings.php

View File

@ -223,6 +223,7 @@ class block_manager {
}
if ($block->visible && !in_array($block->name, $unaddableblocks) &&
!in_array($block->name, $requiredbythemeblocks) &&
$bi->can_block_be_added($this->page) &&
($bi->instance_allow_multiple() || !$this->is_block_present($block->name)) &&
blocks_name_allowed_in_format($block->name, $pageformat) &&
$bi->user_can_addto($this->page)) {

View File

@ -11,5 +11,8 @@
"course/format/tests/behat/courseindex_keyboardnav.feature",
"course/format/tests/behat/courseindex_completion.feature",
"badges/tests/behat/badge_navigation.feature"
],
"tags": [
"addablocklink"
]
}