first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
<?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/>.
/**
* The comments block
*
* @package block_comments
* @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class block_comments extends block_base {
function init() {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
$this->title = get_string('pluginname', 'block_comments');
}
function specialization() {
// require js for commenting
comment::init();
}
function applicable_formats() {
return array('all' => true);
}
function instance_allow_multiple() {
return false;
}
function get_content() {
global $CFG;
if ($this->content !== NULL) {
return $this->content;
}
if (!$CFG->usecomments) {
$this->content = new stdClass();
$this->content->text = '';
if ($this->page->user_is_editing()) {
$this->content->text = get_string('disabledcomments');
}
return $this->content;
}
$this->content = new stdClass();
$this->content->footer = '';
$this->content->text = '';
if (empty($this->instance)) {
return $this->content;
}
list($context, $course, $cm) = get_context_info_array($this->page->context->id);
$args = new stdClass;
$args->context = $this->page->context;
$args->course = $course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new comment($args);
$comment->set_view_permission(true);
$comment->set_fullwidth();
$this->content = new stdClass();
$this->content->text = $comment->output(true);
$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;
}
}
@@ -0,0 +1,38 @@
<?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/>.
/**
* block_comments comment created event.
*
* @package block_comments
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_comments\event;
defined('MOODLE_INTERNAL') || die();
/**
* block_comments comment created event.
*
* @package block_comments
* @since Moodle 2.7
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comment_created extends \core\event\comment_created {
// No need to override any method.
}
@@ -0,0 +1,38 @@
<?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/>.
/**
* block_comments comment deleted event.
*
* @package block_comments
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_comments\event;
defined('MOODLE_INTERNAL') || die();
/**
* block_comments comment deleted event.
*
* @package block_comments
* @since Moodle 2.7
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comment_deleted extends \core\event\comment_deleted {
// No need to override any method.
}
@@ -0,0 +1,147 @@
<?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/>.
/**
* Privacy Subsystem implementation for block_comments.
*
* @package block_comments
* @category privacy
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_comments\privacy;
defined('MOODLE_INTERNAL') || die();
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\userlist;
use core_privacy\local\request\approved_userlist;
/**
* Privacy Subsystem implementation for block_comments.
*
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
// The block_comments block stores user provided data.
\core_privacy\local\metadata\provider,
\core_privacy\local\request\core_userlist_provider,
// The block_comments block provides data directly to core.
\core_privacy\local\request\plugin\provider {
/**
* Returns meta data about this system.
*
* @param collection $collection
* @return collection
*/
public static function get_metadata(collection $collection): collection {
return $collection->add_subsystem_link('core_comment', [], 'privacy:metadata:core_comment');
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid
* @return contextlist
*/
public static function get_contexts_for_userid(int $userid): contextlist {
$contextlist = new contextlist();
$sql = "SELECT contextid
FROM {comments}
WHERE component = :component
AND userid = :userid";
$params = [
'component' => 'block_comments',
'userid' => $userid
];
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Get the list of users within a specific context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
$params = [
'contextid' => $context->id,
'component' => 'block_comments',
];
$sql = "SELECT userid as userid
FROM {comments}
WHERE component = :component
AND contextid = :contextid";
$userlist->add_from_sql('userid', $sql, $params);
}
/**
* Export all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist
*/
public static function export_user_data(approved_contextlist $contextlist) {
$contexts = $contextlist->get_contexts();
foreach ($contexts as $context) {
\core_comment\privacy\provider::export_comments(
$context,
'block_comments',
'page_comments',
0,
[]
);
}
}
/**
* Delete all data for all users in the specified context.
*
* @param \context $context
*/
public static function delete_data_for_all_users_in_context(\context $context) {
\core_comment\privacy\provider::delete_comments_for_all_users($context, 'block_comments');
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
\core_comment\privacy\provider::delete_comments_for_users($userlist, 'block_comments');
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
\core_comment\privacy\provider::delete_comments_for_user($contextlist, 'block_comments');
}
}
+51
View File
@@ -0,0 +1,51 @@
<?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/>.
/**
* Comments block caps.
*
* @package block_comments
* @copyright Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
'block/comments:myaddinstance' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'user' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/my:manageblocks'
),
'block/comments:addinstance' => array(
'riskbitmask' => RISK_SPAM | RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/site:manageblocks'
),
);
@@ -0,0 +1,29 @@
<?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/>.
/**
* Strings for component 'block_comments', language 'en', branch 'MOODLE_20_STABLE'
*
* @package block_comments
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['comments:myaddinstance'] = 'Add a new comments block to Dashboard';
$string['comments:addinstance'] = 'Add a new comments block';
$string['pluginname'] = 'Comments';
$string['privacy:metadata:core_comment'] = 'A record of comments added.';
+102
View File
@@ -0,0 +1,102 @@
<?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/>.
/**
* The comments block helper functions and callbacks
*
* @package block_comments
* @copyright 2011 Dongsheng Cai <dongsheng@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Validate comment parameter before perform other comments actions
*
* @package block_comments
* @category comment
*
* @param stdClass $comment_param {
* context => context the context object
* courseid => int course id
* cm => stdClass course module object
* commentarea => string comment area
* itemid => int itemid
* }
* @return boolean
*/
function block_comments_comment_validate($comment_param) {
if ($comment_param->commentarea != 'page_comments') {
throw new comment_exception('invalidcommentarea');
}
if ($comment_param->itemid != 0) {
throw new comment_exception('invalidcommentitemid');
}
return true;
}
/**
* Running addtional permission check on plugins
*
* @package block_comments
* @category comment
*
* @param stdClass $args
* @return array
*/
function block_comments_comment_permissions($args) {
global $DB, $USER;
// By default, anyone can post and view comments.
$canpost = $canview = true;
// Check if it's the user context and not the owner's profile.
if ($args->context->contextlevel == CONTEXT_USER && $USER->id != $args->context->instanceid) {
// Check whether the context owner has a comment block in the user's profile.
$sqlparam = [
'blockname' => 'comments',
'parentcontextid' => $args->context->id,
'pagetypepattern' => 'user-profile',
];
// If the comment block is not present at the target user's profile,
// then the logged-in user cannot post or view comments.
$canpost = $canview = $DB->record_exists_select(
'block_instances',
'blockname = :blockname AND parentcontextid = :parentcontextid AND pagetypepattern = :pagetypepattern',
$sqlparam,
);
}
return ['post' => $canpost, 'view' => $canview];
}
/**
* Validate comment data before displaying comments
*
* @package block_comments
* @category comment
*
* @param stdClass $comment
* @param stdClass $args
* @return boolean
*/
function block_comments_comment_display($comments, $args) {
if ($args->commentarea != 'page_comments') {
throw new comment_exception('invalidcommentarea');
}
if ($args->itemid != 0) {
throw new comment_exception('invalidcommentitemid');
}
return $comments;
}
@@ -0,0 +1,102 @@
@block @block_comments
Feature: Add a comment to the comments block
In order to comment on a conversation or a topic
As a user
In need to add comments to courses
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | First | teacher1@example.com |
| student1 | Student | First | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| comments | Course | C1 | course-view-* | side-pre |
And I am on the "Course 1" course page logged in as student1
@javascript
Scenario: Add a comment with Javascript enabled
When I add "I'm a comment from student1" comment to comments block
Then I should see "I'm a comment from student1"
And I am on the "Course 1" course page logged in as teacher1
And I should see "I'm a comment from student1"
Scenario: Add a comment with Javascript disabled
When I follow "Show comments"
And I add "I'm a comment from student1" comment to comments block
Then I should see "I'm a comment from student1"
And I am on the "Course 1" course page logged in as teacher1
And I follow "Show comments"
And I should see "I'm a comment from student1"
@javascript
Scenario: Test comment block pagination
When I add "Super test comment 01" comment to comments block
And I change window size to "1024x4096"
And I add "Super test comment 02" comment to comments block
And I add "Super test comment 03" comment to comments block
And I add "Super test comment 04" comment to comments block
And I add "Super test comment 05" comment to comments block
And I add "Super test comment 06" comment to comments block
And I add "Super test comment 07" comment to comments block
And I add "Super test comment 08" comment to comments block
And I add "Super test comment 09" comment to comments block
And I add "Super test comment 10" comment to comments block
And I add "Super test comment 11" comment to comments block
And I add "Super test comment 12" comment to comments block
And I add "Super test comment 13" comment to comments block
And I add "Super test comment 14" comment to comments block
And I add "Super test comment 15" comment to comments block
And I add "Super test comment 16" comment to comments block
And I add "Super test comment 17" comment to comments block
And I add "Super test comment 18" comment to comments block
And I add "Super test comment 19" comment to comments block
And I add "Super test comment 20" comment to comments block
And I add "Super test comment 21" comment to comments block
And I add "Super test comment 22" comment to comments block
And I add "Super test comment 23" comment to comments block
And I add "Super test comment 24" comment to comments block
And I add "Super test comment 25" comment to comments block
And I add "Super test comment 26" comment to comments block
And I add "Super test comment 27" comment to comments block
And I add "Super test comment 28" comment to comments block
And I add "Super test comment 29" comment to comments block
And I add "Super test comment 30" comment to comments block
And I add "Super test comment 31" comment to comments block
Then I should see "Super test comment 01"
And I should see "Super test comment 31"
And I am on "Course 1" course homepage
And I should not see "Super test comment 01"
And I should not see "Super test comment 02"
And I should not see "Super test comment 16"
And I should see "Super test comment 17"
And I should see "Super test comment 31"
And I should see "1" in the ".block_comments .comment-paging" "css_element"
And I should see "2" in the ".block_comments .comment-paging" "css_element"
And I should see "3" in the ".block_comments .comment-paging" "css_element"
And I should not see "4" in the ".block_comments .comment-paging" "css_element"
And I click on "2" "link" in the ".block_comments .comment-paging" "css_element"
And I should not see "Super test comment 01"
And I should see "Super test comment 02"
And I should see "Super test comment 16"
And I should not see "Super test comment 17"
And I should not see "Super test comment 31"
And I click on "3" "link" in the ".block_comments .comment-paging" "css_element"
And I should see "Super test comment 01"
And I should not see "Super test comment 02"
And I should not see "Super test comment 16"
And I should not see "Super test comment 17"
And I should not see "Super test comment 31"
And I click on "1" "link" in the ".block_comments .comment-paging" "css_element"
And I should not see "Super test comment 01"
And I should not see "Super test comment 02"
And I should not see "Super test comment 16"
And I should see "Super test comment 17"
And I should see "Super test comment 31"
@@ -0,0 +1,110 @@
<?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/>.
/**
* Commenting system steps definitions.
*
* @package block_comments
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
Behat\Mink\Exception\ExpectationException as ExpectationException;
/**
* Steps definitions to deal with the commenting system
*
* @package block_comments
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_block_comments extends behat_base {
/**
* Adds the specified option to the comments block of the current page.
*
* This method can be adapted in future to add other comments considering
* that there could be more than one comment textarea per page.
*
* Only 1 comments block instance is allowed per page, if this changes this
* steps definitions should be adapted.
*
* @Given /^I add "(?P<comment_text_string>(?:[^"]|\\")*)" comment to comments block$/
* @throws ElementNotFoundException
* @param string $comment
*/
public function i_add_comment_to_comments_block($comment) {
// Getting the textarea and setting the provided value.
$exception = new ElementNotFoundException($this->getSession(), 'Comments block ');
// The whole DOM structure changes depending on JS enabled/disabled.
if ($this->running_javascript()) {
$commentstextarea = $this->find('css', '.comment-area textarea', $exception);
$commentstextarea->setValue($comment);
$this->find_link(get_string('savecomment'))->click();
// Delay after clicking so that additional comments will have unique time stamps.
// We delay 1 second which is all we need.
$this->getSession()->wait(1000);
} else {
$commentstextarea = $this->find('css', '.block_comments form textarea', $exception);
$commentstextarea->setValue($comment);
// Comments submit button
$submit = $this->find('css', '.block_comments form input[type=submit]');
$submit->press();
}
}
/**
* Deletes the specified comment from the current page's comments block.
*
* @Given /^I delete "(?P<comment_text_string>(?:[^"]|\\")*)" comment from comments block$/
* @throws ElementNotFoundException
* @throws ExpectationException
* @param string $comment
*/
public function i_delete_comment_from_comments_block($comment) {
$exception = new ElementNotFoundException($this->getSession(), '"' . $comment . '" comment ');
// Using xpath liternal to avoid possible problems with comments containing quotes.
$commentliteral = behat_context_helper::escape($comment);
$commentxpath = "//*[contains(concat(' ', normalize-space(@class), ' '), ' block_comments ')]" .
"/descendant::div[@class='comment-message'][contains(., $commentliteral)]";
$commentnode = $this->find('xpath', $commentxpath, $exception);
// Click on delete icon.
$this->execute('behat_general::i_click_on_in_the',
array("Delete comment posted by", "icon", $this->escape($commentxpath), "xpath_element")
);
// Wait for the animation to finish, in theory is just 1 sec, adding 4 just in case.
$this->getSession()->wait(4 * 1000);
}
}
@@ -0,0 +1,30 @@
@block @block_comments
Feature: Enable Block comments on an activity page and view comments
In order to enable the comments block on an activity page
As a teacher
I can add the comments block to an activity page
Scenario: Add the comments block on an activity page and add comments
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | First | teacher1@example.com |
| student1 | Student | First | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | course | idnumber | name | intro |
| page | C1 | page1 | Test page name | Test page description |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| comments | Activity module | page1 | mod-page-* | side-pre |
And I am on the "Test page name" "page activity" page logged in as teacher1
And I follow "Show comments"
And I add "I'm a comment from the teacher" comment to comments block
When I am on the "Test page name" "page activity" page logged in as student1
And I follow "Show comments"
Then I should see "I'm a comment from the teacher"
@@ -0,0 +1,29 @@
@block @block_comments
Feature: Enable Block comments on the dashboard and view comments
In order to enable the comments block on a the dashboard
As a teacher
I can add the comments block to my dashboard
Background:
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | First | teacher1@example.com |
Scenario: Add the comments block on the dashboard and add comments with Javascript disabled
When I log in as "teacher1"
And I turn editing mode on
And I add the "Comments" block
And I follow "Show comments"
And I add "I'm a comment from the teacher" comment to comments block
Then I should see "I'm a comment from the teacher"
@javascript
Scenario: Add the comments block on the dashboard and add comments with Javascript enabled
When I log in as "teacher1"
And I turn editing mode on
And I add the "Comments" block
And I add "I'm a comment from the teacher" comment to comments block
Then I should see "I'm a comment from the teacher"
@@ -0,0 +1,22 @@
@block @block_comments
Feature: Enable Block comments on the frontpage and view comments
In order to enable the comments block on the frontpage
As a admin
I can add the comments block to the frontpage
Scenario: Add the comments block on the frontpage and add comments
Given the following "users" exist:
| username | firstname | lastname | email | idnumber |
| teacher1 | Teacher | 1 | teacher1@example.com | T1 |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| comments | System | 1 | site-index | side-pre |
And I log in as "admin"
And I am on site homepage
And I follow "Show comments"
And I add "I'm a comment from admin" comment to comments block
And I log out
When I log in as "teacher1"
And I am on site homepage
And I follow "Show comments"
Then I should see "I'm a comment from admin"
@@ -0,0 +1,30 @@
@block @block_comments
Feature: Delete comment block messages
In order to refine comment block's contents
As a teacher
In need to delete comments from courses
@javascript
Scenario: Delete comments with Javascript enabled
Given the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | First | teacher1@example.com |
| student1 | Student | First | student1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "blocks" exist:
| blockname | contextlevel | reference | pagetypepattern | defaultregion |
| comments | Course | C1 | course-view-* | side-pre |
And I am on the "Course 1" course page logged in as student1
And I add "Comment from student1" comment to comments block
And I am on the "Course 1" course page logged in as teacher1
And I add "Comment from teacher1" comment to comments block
When I delete "Comment from student1" comment from comments block
Then I should not see "Comment from student1"
And I delete "Comment from teacher1" comment from comments block
And I should not see "Comment from teacher1"
+63
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;
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));
}
}
+184
View File
@@ -0,0 +1,184 @@
<?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/>.
/**
* Events tests.
*
* @package block_comments
* @category test
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_comments\event;
/**
* Events tests class.
*
* @package block_comments
* @category test
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class events_test extends \advanced_testcase {
/** @var stdClass Keeps course object */
private $course;
/** @var stdClass Keeps wiki object */
private $wiki;
/**
* Setup test data.
*/
public function setUp(): void {
$this->resetAfterTest();
$this->setAdminUser();
// Create course and wiki.
$this->course = $this->getDataGenerator()->create_course();
$this->wiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id));
}
/**
* Test comment_created event.
*/
public function test_comment_created(): void {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
// Comment on course page.
$context = \context_course::instance($this->course->id);
$args = new \stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new \comment($args);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->add('New comment');
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new \moodle_url('/course/view.php', array('id' => $this->course->id));
$this->assertEquals($url, $event->get_url());
// Comments when block is on module (wiki) page.
$context = \context_module::instance($this->wiki->cmid);
$args = new \stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new \comment($args);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->add('New comment 1');
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_created', $event);
$this->assertEquals($context, $event->get_context());
$url = new \moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
/**
* Test comment_deleted event.
*/
public function test_comment_deleted(): void {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
// Comment on course page.
$context = \context_course::instance($this->course->id);
$args = new \stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new \comment($args);
$newcomment = $comment->add('New comment');
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->delete($newcomment->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$url = new \moodle_url('/course/view.php', array('id' => $this->course->id));
$this->assertEquals($url, $event->get_url());
// Comments when block is on module (wiki) page.
$context = \context_module::instance($this->wiki->cmid);
$args = new \stdClass;
$args->context = $context;
$args->course = $this->course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new \comment($args);
$newcomment = $comment->add('New comment 1');
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$comment->delete($newcomment->id);
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\block_comments\event\comment_deleted', $event);
$this->assertEquals($context, $event->get_context());
$url = new \moodle_url('/mod/wiki/view.php', array('id' => $this->wiki->cmid));
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
}
@@ -0,0 +1,601 @@
<?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/>.
/**
* Privacy provider tests.
*
* @package block_comments
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_comments\privacy;
use core_privacy\local\metadata\collection;
use block_comments\privacy\provider;
use core_privacy\local\request\approved_userlist;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy provider test for block_comments.
*
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends \core_privacy\tests\provider_testcase {
/** @var stdClass A student who is only enrolled in course1. */
protected $student1;
/** @var stdClass A student who is only enrolled in course2. */
protected $student2;
/** @var stdClass A student who is enrolled in both course1 and course2. */
protected $student12;
/** @var stdClass A test course. */
protected $course1;
/** @var stdClass A test course. */
protected $course2;
protected function setUp(): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Create courses.
$generator = $this->getDataGenerator();
$this->course1 = $generator->create_course();
$this->course2 = $generator->create_course();
// Create and enrol students.
$this->student1 = $generator->create_user();
$this->student2 = $generator->create_user();
$this->student12 = $generator->create_user();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$generator->enrol_user($this->student1->id, $this->course1->id, $studentrole->id);
$generator->enrol_user($this->student2->id, $this->course2->id, $studentrole->id);
$generator->enrol_user($this->student12->id, $this->course1->id, $studentrole->id);
$generator->enrol_user($this->student12->id, $this->course2->id, $studentrole->id);
// Comment block on course pages.
$block = $this->add_comments_block_in_context(\context_course::instance($this->course1->id));
$block = $this->add_comments_block_in_context(\context_course::instance($this->course2->id));
}
/**
* Posts a comment on a given context.
*
* @param string $text The comment's text.
* @param \context $context The context on which we want to put the comment.
*/
protected function add_comment($text, \context $context) {
$args = new \stdClass;
$args->context = $context;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$args->linktext = get_string('showcomments');
$args->notoggle = true;
$args->autostart = true;
$args->displaycancel = false;
$comment = new \comment($args);
$comment->add($text);
}
/**
* Creates a comments block on a context.
*
* @param \context $context The context on which we want to put the block.
* @return \block_base The created block instance.
* @throws \coding_exception
*/
protected function add_comments_block_in_context(\context $context) {
global $DB;
$course = null;
$page = new \moodle_page();
$page->set_context($context);
switch ($context->contextlevel) {
case CONTEXT_SYSTEM:
$page->set_pagelayout('frontpage');
$page->set_pagetype('site-index');
break;
case CONTEXT_COURSE:
$page->set_pagelayout('standard');
$page->set_pagetype('course-view');
$course = $DB->get_record('course', ['id' => $context->instanceid]);
$page->set_course($course);
break;
case CONTEXT_MODULE:
$page->set_pagelayout('standard');
$mod = $DB->get_field_sql("SELECT m.name
FROM {modules} m
JOIN {course_modules} cm on cm.module = m.id
WHERE cm.id = ?", [$context->instanceid]);
$page->set_pagetype("mod-$mod-view");
break;
case CONTEXT_USER:
$page->set_pagelayout('mydashboard');
$page->set_pagetype('my-index');
break;
default:
throw new coding_exception('Unsupported context for test');
}
$page->blocks->load_blocks();
$page->blocks->add_block_at_end_of_default_region('comments');
// We need to use another page object as load_blocks() only loads the blocks once.
$page2 = new \moodle_page();
$page2->set_context($page->context);
$page2->set_pagelayout($page->pagelayout);
$page2->set_pagetype($page->pagetype);
if ($course) {
$page2->set_course($course);
}
$page->blocks->load_blocks();
$page2->blocks->load_blocks();
$blocks = $page2->blocks->get_blocks_for_region($page2->blocks->get_default_region());
$block = end($blocks);
$block = block_instance('comments', $block->instance);
return $block;
}
/**
* Test for provider::get_metadata().
*/
public function test_get_metadata(): void {
$collection = new collection('block_comments');
$newcollection = provider::get_metadata($collection);
$itemcollection = $newcollection->get_collection();
$this->assertCount(1, $itemcollection);
$link = reset($itemcollection);
$this->assertEquals('core_comment', $link->get_name());
$this->assertEmpty($link->get_privacy_fields());
$this->assertEquals('privacy:metadata:core_comment', $link->get_summary());
}
/**
* Test for provider::get_contexts_for_userid() when user had not posted any comments..
*/
public function test_get_contexts_for_userid_no_comment(): void {
$this->setUser($this->student1);
$coursecontext1 = \context_course::instance($this->course1->id);
$this->add_comment('New comment', $coursecontext1);
$this->setUser($this->student2);
$contextlist = provider::get_contexts_for_userid($this->student2->id);
$this->assertCount(0, $contextlist);
}
/**
* Test for provider::get_contexts_for_userid().
*/
public function test_get_contexts_for_userid(): void {
$coursecontext1 = \context_course::instance($this->course1->id);
$coursecontext2 = \context_course::instance($this->course2->id);
$this->setUser($this->student12);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext2);
$contextlist = provider::get_contexts_for_userid($this->student12->id);
$this->assertCount(2, $contextlist);
$contextids = $contextlist->get_contextids();
$this->assertEqualsCanonicalizing([$coursecontext1->id, $coursecontext2->id], $contextids);
}
/**
* Test for provider::export_user_data() when the user has not posted any comments.
*/
public function test_export_for_context_no_comment(): void {
$coursecontext1 = \context_course::instance($this->course1->id);
$coursecontext2 = \context_course::instance($this->course2->id);
$this->setUser($this->student1);
$this->add_comment('New comment', $coursecontext1);
$this->setUser($this->student2);
$this->setUser($this->student2);
$this->export_context_data_for_user($this->student2->id, $coursecontext2, 'block_comments');
$writer = \core_privacy\local\request\writer::with_context($coursecontext2);
$this->assertFalse($writer->has_any_data());
}
/**
* Test for provider::export_user_data().
*/
public function test_export_for_context(): void {
$coursecontext1 = \context_course::instance($this->course1->id);
$coursecontext2 = \context_course::instance($this->course2->id);
$this->setUser($this->student12);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext2);
// Export all of the data for the context.
$this->export_context_data_for_user($this->student12->id, $coursecontext1, 'block_comments');
$writer = \core_privacy\local\request\writer::with_context($coursecontext1);
$this->assertTrue($writer->has_any_data());
}
/**
* Test for provider::delete_data_for_all_users_in_context().
*/
public function test_delete_data_for_all_users_in_context(): void {
global $DB;
$coursecontext1 = \context_course::instance($this->course1->id);
$coursecontext2 = \context_course::instance($this->course2->id);
$this->setUser($this->student1);
$this->add_comment('New comment', $coursecontext1);
$this->setUser($this->student2);
$this->add_comment('New comment', $coursecontext2);
$this->setUser($this->student12);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext2);
// Before deletion, we should have 3 comments in $coursecontext1 and 2 comments in $coursecontext2.
$this->assertEquals(
3,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $coursecontext1->id])
);
$this->assertEquals(
2,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $coursecontext2->id])
);
// Delete data based on context.
provider::delete_data_for_all_users_in_context($coursecontext1);
// After deletion, the comments for $coursecontext1 should have been deleted.
$this->assertEquals(
0,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $coursecontext1->id])
);
$this->assertEquals(
2,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $coursecontext2->id])
);
}
/**
* Test for provider::delete_data_for_all_users_in_context() when there are also comments from other plugins.
*/
public function test_delete_data_for_all_users_in_context_with_comments_from_other_plugins(): void {
global $DB;
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$instance = $assigngenerator->create_instance(['course' => $this->course1]);
$cm = get_coursemodule_from_instance('assign', $instance->id);
$assigncontext = \context_module::instance($cm->id);
$assign = new \assign($assigncontext, $cm, $this->course1);
// Add a comments block in the assignment page.
$this->add_comments_block_in_context($assigncontext);
$submission = $assign->get_user_submission($this->student1->id, true);
$options = new \stdClass();
$options->area = 'submission_comments';
$options->course = $assign->get_course();
$options->context = $assigncontext;
$options->itemid = $submission->id;
$options->component = 'assignsubmission_comments';
$options->showcount = true;
$options->displaycancel = true;
$comment = new \comment($options);
$comment->set_post_permission(true);
$this->setUser($this->student1);
$comment->add('Comment from student 1');
$this->add_comment('New comment', $assigncontext);
$this->setUser($this->student2);
$this->add_comment('New comment', $assigncontext);
// Before deletion, we should have 3 comments in $assigncontext.
// One comment is for the assignment submission and 2 are for the comments block.
$this->assertEquals(
3,
$DB->count_records('comments', ['contextid' => $assigncontext->id])
);
$this->assertEquals(
2,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $assigncontext->id])
);
provider::delete_data_for_all_users_in_context($assigncontext);
// After deletion, the comments for $assigncontext in the comment block should have been deleted,
// but the assignment submission comment should be left.
$this->assertEquals(
1,
$DB->count_records('comments', ['contextid' => $assigncontext->id])
);
$this->assertEquals(
0,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $assigncontext->id])
);
}
/**
* Test for provider::delete_data_for_user().
*/
public function test_delete_data_for_user(): void {
global $DB;
$coursecontext1 = \context_course::instance($this->course1->id);
$coursecontext2 = \context_course::instance($this->course2->id);
$this->setUser($this->student1);
$this->add_comment('New comment', $coursecontext1);
$this->setUser($this->student2);
$this->add_comment('New comment', $coursecontext2);
$this->setUser($this->student12);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext2);
// Before deletion, we should have 3 comments in $coursecontext1 and 2 comments in $coursecontext2,
// and 3 comments by student12 in $coursecontext1 and $coursecontext2 combined.
$this->assertEquals(
3,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $coursecontext1->id])
);
$this->assertEquals(
2,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $coursecontext2->id])
);
$this->assertEquals(
3,
$DB->count_records('comments', ['component' => 'block_comments', 'userid' => $this->student12->id])
);
$contextlist = new \core_privacy\local\request\approved_contextlist($this->student12, 'block_comments',
[$coursecontext1->id, $coursecontext2->id]);
provider::delete_data_for_user($contextlist);
// After deletion, the comments for the student12 should have been deleted.
$this->assertEquals(
1,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $coursecontext1->id])
);
$this->assertEquals(
1,
$DB->count_records('comments', ['component' => 'block_comments', 'contextid' => $coursecontext2->id])
);
$this->assertEquals(
0,
$DB->count_records('comments', ['component' => 'block_comments', 'userid' => $this->student12->id])
);
}
/**
* Test for provider::delete_data_for_user() when there are also comments from other plugins.
*/
public function test_delete_data_for_user_with_comments_from_other_plugins(): void {
global $DB;
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$instance = $assigngenerator->create_instance(['course' => $this->course1]);
$cm = get_coursemodule_from_instance('assign', $instance->id);
$assigncontext = \context_module::instance($cm->id);
$assign = new \assign($assigncontext, $cm, $this->course1);
// Add a comments block in the assignment page.
$this->add_comments_block_in_context($assigncontext);
$submission = $assign->get_user_submission($this->student1->id, true);
$options = new \stdClass();
$options->area = 'submission_comments';
$options->course = $assign->get_course();
$options->context = $assigncontext;
$options->itemid = $submission->id;
$options->component = 'assignsubmission_comments';
$options->showcount = true;
$options->displaycancel = true;
$comment = new \comment($options);
$comment->set_post_permission(true);
$this->setUser($this->student1);
$comment->add('Comment from student 1');
$this->add_comment('New comment', $assigncontext);
$this->add_comment('New comment', $assigncontext);
// Before deletion, we should have 3 comments in $assigncontext.
// one comment is for the assignment submission and 2 are for the comments block.
$this->assertEquals(
3,
$DB->count_records('comments', ['contextid' => $assigncontext->id])
);
$contextlist = new \core_privacy\local\request\approved_contextlist($this->student1, 'block_comments',
[$assigncontext->id]);
provider::delete_data_for_user($contextlist);
// After deletion, the comments for the student1 in the comment block should have been deleted,
// but the assignment submission comment should be left.
$this->assertEquals(
1,
$DB->count_records('comments', ['contextid' => $assigncontext->id])
);
$this->assertEquals(
0,
$DB->count_records('comments', ['component' => 'block_comments', 'userid' => $this->student1->id])
);
}
/**
* Test that only users within a course context are fetched.
* @group qtesttt
*/
public function test_get_users_in_context(): void {
$component = 'block_comments';
$coursecontext1 = \context_course::instance($this->course1->id);
$coursecontext2 = \context_course::instance($this->course2->id);
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(0, $userlist1);
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(0, $userlist2);
$this->setUser($this->student12);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext2);
$this->setUser($this->student1);
$this->add_comment('New comment', $coursecontext1);
// The list of users should contain user12 and user1.
provider::get_users_in_context($userlist1);
$this->assertCount(2, $userlist1);
$this->assertTrue(in_array($this->student1->id, $userlist1->get_userids()));
$this->assertTrue(in_array($this->student12->id, $userlist1->get_userids()));
// The list of users should contain user12.
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
$expected = [$this->student12->id];
$actual = $userlist2->get_userids();
$this->assertEquals($expected, $actual);
}
/**
* Test that data for users in approved userlist is deleted.
*/
public function test_delete_data_for_users(): void {
$component = 'block_comments';
$coursecontext1 = \context_course::instance($this->course1->id);
$coursecontext2 = \context_course::instance($this->course2->id);
$this->setUser($this->student12);
$this->add_comment('New comment', $coursecontext1);
$this->add_comment('New comment', $coursecontext2);
$this->setUser($this->student1);
$this->add_comment('New comment', $coursecontext1);
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(2, $userlist1);
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
$this->assertCount(1, $userlist2);
// Convert $userlist1 into an approved_contextlist.
$approvedlist1 = new approved_userlist($coursecontext1, $component, $userlist1->get_userids());
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist1);
// Re-fetch users in coursecontext1.
$userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
provider::get_users_in_context($userlist1);
// The user data in coursecontext1 should be deleted.
$this->assertCount(0, $userlist1);
// Re-fetch users in coursecontext2.
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
provider::get_users_in_context($userlist2);
// The user data in coursecontext2 should be still present.
$this->assertCount(1, $userlist2);
}
/**
* Test for provider::delete_data_for_user() when there are also comments from other plugins.
*/
public function test_delete_data_for_users_with_comments_from_other_plugins(): void {
$component = 'block_comments';
$assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
$instance = $assigngenerator->create_instance(['course' => $this->course1]);
$cm = get_coursemodule_from_instance('assign', $instance->id);
$assigncontext = \context_module::instance($cm->id);
$assign = new \assign($assigncontext, $cm, $this->course1);
// Add a comments block in the assignment page.
$this->add_comments_block_in_context($assigncontext);
$submission = $assign->get_user_submission($this->student1->id, true);
$options = new \stdClass();
$options->area = 'submission_comments';
$options->course = $assign->get_course();
$options->context = $assigncontext;
$options->itemid = $submission->id;
$options->component = 'assignsubmission_comments';
$options->showcount = true;
$options->displaycancel = true;
$comment = new \comment($options);
$comment->set_post_permission(true);
$this->setUser($this->student1);
$comment->add('Comment from student 1');
$this->add_comment('New comment', $assigncontext);
$this->add_comment('New comment', $assigncontext);
$userlist1 = new \core_privacy\local\request\userlist($assigncontext, $component);
provider::get_users_in_context($userlist1);
$this->assertCount(1, $userlist1);
// Convert $userlist1 into an approved_contextlist.
$approvedlist = new approved_userlist($assigncontext, $component, $userlist1->get_userids());
// Delete using delete_data_for_user.
provider::delete_data_for_users($approvedlist);
// Re-fetch users in assigncontext.
$userlist1 = new \core_privacy\local\request\userlist($assigncontext, $component);
provider::get_users_in_context($userlist1);
// The user data in assigncontext should be deleted.
$this->assertCount(0, $userlist1);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?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/>.
/**
* Version details
*
* @package block_comments
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'block_comments'; // Full name of the plugin (used for diagnostics)