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
+49
View File
@@ -0,0 +1,49 @@
@core_comment @javascript
Feature: Manage comments made by users
As an admin
I want to view, filter and delete comments
Background:
Given I log in as "admin"
And the following "course" exists:
| fullname | Course 1 |
| shortname | CS101 |
And the following "core_comment > Comments" exist:
| contextlevel | reference | component | area | content |
| Course | CS101 | block_comments | page_comments | Uno |
| Course | CS101 | block_comments | page_comments | Dos |
| Course | CS101 | block_comments | page_comments | Tres |
Scenario: View and filter site comments
When I navigate to "Reports > Comments" in site administration
And the following should exist in the "reportbuilder-table" table:
| First name | Content | Context URL |
| Admin User | Uno | Course: Course 1 |
| Admin User | Dos | Course: Course 1 |
| Admin User | Tres | Course: Course 1 |
And I click on "Filters" "button"
And I set the following fields in the "Content" "core_reportbuilder > Filter" to these values:
| Content operator | Contains |
| Content value | Uno |
And I click on "Apply" "button" in the "[data-region='report-filters']" "css_element"
Then I should see "Uno" in the "reportbuilder-table" "table"
And I should not see "Dos" in the "reportbuilder-table" "table"
And I should not see "Tres" in the "reportbuilder-table" "table"
Scenario: Delete single comment
When I navigate to "Reports > Comments" in site administration
And I press "Delete" action in the "Uno" report row
And I click on "Delete" "button" in the "Delete" "dialogue"
Then I should not see "Uno" in the "reportbuilder-table" "table"
And I should see "Dos" in the "reportbuilder-table" "table"
And I should see "Tres" in the "reportbuilder-table" "table"
Scenario: Delete multiple comments
When I navigate to "Reports > Comments" in site administration
And I click on "Select" "checkbox" in the "Uno" "table_row"
And I click on "Select" "checkbox" in the "Dos" "table_row"
And I press "Delete selected"
And I click on "Delete" "button" in the "Delete selected" "dialogue"
Then I should not see "Uno" in the "reportbuilder-table" "table"
And I should not see "Dos" in the "reportbuilder-table" "table"
And I should see "Tres" in the "reportbuilder-table" "table"
+161
View File
@@ -0,0 +1,161 @@
<?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 core_comment;
use comment;
use comment_exception;
use core_comment_external;
/**
* Tests for comments when the context is frozen.
*
* @package core_comment
* @copyright 2019 University of Nottingham
* @author Neill Magill <neill.magill@nottingham.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class context_freeze_test extends \advanced_testcase {
/**
* Creates a comment by a student.
*
* Returns:
* - The comment object
* - The sudent that wrote the comment
* - The arguments used to create the comment
*
* @param \stdClass $course Moodle course from the datagenerator
* @return array
*/
protected function create_student_comment_and_freeze_course($course): array {
set_config('contextlocking', 1);
$context = \context_course::instance($course->id);
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
$args = new \stdClass;
$args->context = $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;
// Create a comment by the student.
$this->setUser($student);
$comment = new comment($args);
$newcomment = $comment->add('New comment');
// Freeze the context.
$this->setAdminUser();
$context->set_locked(true);
return [$newcomment, $student, $args];
}
/**
* Test that a student cannot delete their own comments in frozen contexts via the external service.
*/
public function test_delete_student_external(): void {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course);
// Check that a student cannot delete their own comment.
$this->setUser($student);
$studentcomment = new comment($args);
$this->assertFalse($studentcomment->can_delete($newcomment->id));
$this->assertFalse($studentcomment->can_post());
$this->expectException(comment_exception::class);
$this->expectExceptionMessage(get_string('nopermissiontodelentry', 'error'));
core_comment_external::delete_comments([$newcomment->id]);
}
/**
* Test that a student cannot delete their own comments in frozen contexts.
*/
public function test_delete_student(): void {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course);
// Check that a student cannot delete their own comment.
$this->setUser($student);
$studentcomment = new comment($args);
$this->assertFalse($studentcomment->can_delete($newcomment->id));
$this->assertFalse($studentcomment->can_post());
$this->expectException(comment_exception::class);
$this->expectExceptionMessage(get_string('nopermissiontocomment', 'error'));
$studentcomment->delete($newcomment->id);
}
/**
* Test that an admin cannot delete comments in frozen contexts via the external service.
*/
public function test_delete_admin_external(): void {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course);
// Check that the admin user cannot delete the comment.
$admincomment = new comment($args);
$this->assertFalse($admincomment->can_delete($newcomment->id));
$this->assertFalse($admincomment->can_post());
$this->expectException(comment_exception::class);
$this->expectExceptionMessage(get_string('nopermissiontodelentry', 'error'));
core_comment_external::delete_comments([$newcomment->id]);
}
/**
* Test that an admin cannot delete comments in frozen contexts.
*/
public function test_delete_admin(): void {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
list($newcomment, $student, $args) = $this->create_student_comment_and_freeze_course($course);
// Check that the admin user cannot delete the comment.
$admincomment = new comment($args);
$this->assertFalse($admincomment->can_delete($newcomment->id));
$this->assertFalse($admincomment->can_post());
$this->expectException(comment_exception::class);
$this->expectExceptionMessage(get_string('nopermissiontocomment', 'error'));
$admincomment->delete($newcomment->id);
}
}
+457
View File
@@ -0,0 +1,457 @@
<?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 core_comment;
use comment_exception;
use core_comment_external;
use core_external\external_api;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External comment functions unit tests
*
* @package core_comment
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.9
*/
class externallib_test extends externallib_advanced_testcase {
/**
* Tests set up
*/
protected function setUp(): void {
$this->resetAfterTest();
}
/**
* Helper used to set up a course, with a module, a teacher and two students.
*
* @return array the array of records corresponding to the course, teacher, and students.
*/
protected function setup_course_and_users_basic() {
global $CFG, $DB;
require_once($CFG->dirroot . '/comment/lib.php');
$CFG->usecomments = true;
$student1 = $this->getDataGenerator()->create_user();
$student2 = $this->getDataGenerator()->create_user();
$teacher1 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course(array('enablecomment' => 1));
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($student2->id, $course1->id, $studentrole->id);
$this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, $teacherrole->id);
// Create a database module instance.
$record = new \stdClass();
$record->course = $course1->id;
$record->name = "Mod data test";
$record->intro = "Some intro of some sort";
$record->comments = 1;
$module1 = $this->getDataGenerator()->create_module('data', $record);
$field = data_get_field_new('text', $module1);
$fielddetail = new \stdClass();
$fielddetail->name = 'Name';
$fielddetail->description = 'Some name';
$field->define_field($fielddetail);
$field->insert_field();
$recordid = data_add_record($module1);
$datacontent = array();
$datacontent['fieldid'] = $field->field->id;
$datacontent['recordid'] = $recordid;
$datacontent['content'] = 'Asterix';
$DB->insert_record('data_content', $datacontent);
return [$module1, $recordid, $teacher1, $student1, $student2];
}
/**
* Test get_comments
*/
public function test_get_comments(): void {
global $CFG;
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Create some comments as student 1.
$this->setUser($student1);
$inputdata = [
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
],
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'def',
'itemid' => $recordid,
'area' => 'database_entry'
]
];
$result = core_comment_external::add_comments($inputdata);
$result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
$ids = array_column($result, 'id');
// Verify we can get the comments.
$contextlevel = 'module';
$instanceid = $module1->cmid;
$component = 'mod_data';
$itemid = $recordid;
$area = 'database_entry';
$page = 0;
$result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page);
$result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(2, $result['comments']);
$this->assertEquals(2, $result['count']);
$this->assertEquals(15, $result['perpage']);
$this->assertTrue($result['canpost']);
$this->assertEquals($student1->id, $result['comments'][0]['userid']);
$this->assertEquals($student1->id, $result['comments'][1]['userid']);
$this->assertEquals($ids[1], $result['comments'][0]['id']); // Default ordering newer first.
$this->assertEquals($ids[0], $result['comments'][1]['id']);
// Test sort direction and pagination.
$CFG->commentsperpage = 1;
$result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page, 'ASC');
$result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(1, $result['comments']); // Only one per page.
$this->assertEquals(2, $result['count']);
$this->assertEquals($CFG->commentsperpage, $result['perpage']);
$this->assertEquals($ids[0], $result['comments'][0]['id']); // Comments order older first.
// Next page.
$result = core_comment_external::get_comments($contextlevel, $instanceid, $component, $itemid, $area, $page + 1, 'ASC');
$result = external_api::clean_returnvalue(core_comment_external::get_comments_returns(), $result);
$this->assertCount(0, $result['warnings']);
$this->assertCount(1, $result['comments']);
$this->assertEquals(2, $result['count']);
$this->assertEquals($CFG->commentsperpage, $result['perpage']);
$this->assertEquals($ids[1], $result['comments'][0]['id']);
}
/**
* Test add_comments not enabled site level
*/
public function test_add_comments_not_enabled_site_level(): void {
global $CFG;
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Try to add a comment, as student 1, when comments is disabled at site level.
$this->setUser($student1);
$CFG->usecomments = false;
$this->expectException(comment_exception::class);
core_comment_external::add_comments([
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
]
]);
}
/**
* Test add_comments not enabled module level
*/
public function test_add_comments_not_enabled_module_level(): void {
global $DB;
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Disable comments for the module.
$DB->set_field('data', 'comments', 0, array('id' => $module1->id));
// Verify we can't add a comment.
$this->setUser($student1);
$this->expectException(comment_exception::class);
core_comment_external::add_comments([
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
]
]);
}
/**
* Test add_comments
*/
public function test_add_comments_single(): void {
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Add a comment as student 1.
$this->setUser($student1);
$result = core_comment_external::add_comments([
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
]
]);
$result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
// Verify the result contains 1 result having the correct structure.
$this->assertCount(1, $result);
$expectedkeys = [
'id',
'content',
'format',
'timecreated',
'strftimeformat',
'profileurl',
'fullname',
'time',
'avatar',
'userid',
'delete',
];
foreach ($expectedkeys as $key) {
$this->assertArrayHasKey($key, $result[0]);
}
}
/**
* Test add_comments when one of the comments contains invalid data and cannot be created.
*
* This simply verifies that the entire operation fails.
*/
public function test_add_comments_multiple_contains_invalid(): void {
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Try to create some comments as student 1, but provide a bad area for the second comment.
$this->setUser($student1);
$this->expectException(comment_exception::class);
core_comment_external::add_comments([
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
],
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'def',
'itemid' => $recordid,
'area' => 'badarea'
],
]);
}
/**
* Test add_comments when one of the comments contains invalid data and cannot be created.
*
* This simply verifies that the entire operation fails.
*/
public function test_add_comments_multiple_all_valid(): void {
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Try to create some comments as student 1.
$this->setUser($student1);
$inputdata = [
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
],
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'def',
'itemid' => $recordid,
'area' => 'database_entry'
]
];
$result = core_comment_external::add_comments($inputdata);
$result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
// Two comments should have been created.
$this->assertCount(2, $result);
// The content for each comment should come back formatted.
foreach ($result as $index => $comment) {
$formatoptions = array('overflowdiv' => true, 'blanktarget' => true);
$expectedcontent = format_text($inputdata[$index]['content'], FORMAT_MOODLE, $formatoptions);
$this->assertEquals($expectedcontent, $comment['content']);
}
}
/**
* Test add_comments invalid area
*/
public function test_add_comments_invalid_area(): void {
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Try to create a comment with an invalid area, verifying failure.
$this->setUser($student1);
$comments = [
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'spaghetti'
]
];
$this->expectException(comment_exception::class);
core_comment_external::add_comments($comments);
}
/**
* Test delete_comment invalid comment.
*/
public function test_delete_comments_invalid_comment_id(): void {
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
$this->setUser($student1);
$this->expectException(comment_exception::class);
core_comment_external::delete_comments([-1, 0]);
}
/**
* Test delete_comment own user.
*/
public function test_delete_comments_own_user(): void {
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Create a few comments as student 1.
$this->setUser($student1);
$result = core_comment_external::add_comments([
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
],
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'def',
'itemid' => $recordid,
'area' => 'database_entry'
]
]);
$result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
// Delete those comments we just created.
$result = core_comment_external::delete_comments([
$result[0]['id'],
$result[1]['id']
]);
$result = external_api::clean_returnvalue(core_comment_external::delete_comments_returns(), $result);
$this->assertEquals([], $result);
}
/**
* Test delete_comment other student.
*/
public function test_delete_comment_other_student(): void {
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Create a comment as the student.
$this->setUser($student1);
$result = core_comment_external::add_comments([
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
]
]);
$result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
// Now, as student 2, try to delete the comment made by student 1. Verify we can't.
$this->setUser($student2);
$this->expectException(comment_exception::class);
core_comment_external::delete_comments([$result[0]['id']]);
}
/**
* Test delete_comment as teacher.
*/
public function test_delete_comments_as_teacher(): void {
[$module1, $recordid, $teacher1, $student1, $student2] = $this->setup_course_and_users_basic();
// Create a comment as the student.
$this->setUser($student1);
$result = core_comment_external::add_comments([
[
'contextlevel' => 'module',
'instanceid' => $module1->cmid,
'component' => 'mod_data',
'content' => 'abc',
'itemid' => $recordid,
'area' => 'database_entry'
]
]);
$result = external_api::clean_returnvalue(core_comment_external::add_comments_returns(), $result);
// Verify teachers can delete the comment.
$this->setUser($teacher1);
$result = core_comment_external::delete_comments([$result[0]['id']]);
$result = external_api::clean_returnvalue(core_comment_external::delete_comments_returns(), $result);
$this->assertEquals([], $result);
}
}
@@ -0,0 +1,61 @@
<?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/>.
declare(strict_types=1);
/**
* Behat data generator for comments
*
* @package core_comment
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_core_comment_generator extends behat_generator_base {
/**
* Get a list of the entities that can be created for this component
*
* @return array[]
*/
protected function get_creatable_entities(): array {
return [
'Comments' => [
'singular' => 'Comment',
'datagenerator' => 'comment',
'required' => [
'contextlevel',
'reference',
'component',
'area',
'content',
],
],
];
}
/**
* Pre-process comment, populate context property
*
* @param array $comment
* @return array
*/
protected function preprocess_comment(array $comment): array {
$comment['context'] = $this->get_context($comment['contextlevel'], $comment['reference']);
unset($comment['contextlevel'], $comment['reference']);
return $comment;
}
}
+52
View File
@@ -0,0 +1,52 @@
<?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/>.
declare(strict_types=1);
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/comment/lib.php");
/**
* Comment test generator
*
* @package core_comment
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_comment_generator extends component_generator_base {
/**
* Create comment
*
* @param array|stdClass $record
* @return comment
*/
public function create_comment($record): comment {
$record = (array) $record;
$content = (string) ($record['content'] ?? '');
unset($record['content']);
$comment = new comment((object) $record);
if ($content !== '') {
$comment->add($content);
}
return $comment;
}
}
+436
View File
@@ -0,0 +1,436 @@
<?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 tests for core_comment.
*
* @package core_comment
* @category test
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_comment\privacy;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/comment/locallib.php');
require_once($CFG->dirroot . '/comment/lib.php');
use core_privacy\local\request\approved_userlist;
use core_privacy\tests\provider_testcase;
use core_privacy\tests\request\approved_contextlist;
/**
* Unit tests for comment/classes/privacy/policy
*
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider_test extends provider_testcase {
protected function setUp(): void {
$this->resetAfterTest();
}
/**
* Check the exporting of comments for a user id in a context.
*/
public function test_export_comments(): void {
$course = $this->getDataGenerator()->create_course();
$context = \context_course::instance($course->id);
$comment = $this->get_comment_object($context, $course);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
// Add comments.
$comments = [];
$firstcomment = 'This is the first comment';
$this->setUser($user1);
$comment->add($firstcomment);
$comments[$user1->id] = $firstcomment;
$secondcomment = 'From the second user';
$this->setUser($user2);
$comment->add($secondcomment);
$comments[$user2->id] = $secondcomment;
// Retrieve comments only for user1.
$this->setUser($user1);
$writer = \core_privacy\local\request\writer::with_context($context);
provider::export_comments($context, 'block_comments', 'page_comments', 0, []);
$data = $writer->get_data([get_string('commentsubcontext', 'core_comment')]);
$exportedcomments = $data->comments;
// There is only one comment made by this user.
$this->assertCount(1, $exportedcomments);
$comment = reset($exportedcomments);
$this->assertEquals($comments[$user1->id], format_string($comment->content, FORMAT_PLAIN));
// Retrieve comments from any user.
provider::export_comments($context, 'block_comments', 'page_comments', 0, [], false);
$data = $writer->get_data([get_string('commentsubcontext', 'core_comment')]);
$exportedcomments = $data->comments;
// The whole conversation is two comments.
$this->assertCount(2, $exportedcomments);
foreach ($exportedcomments as $comment) {
$this->assertEquals($comments[$comment->userid], format_string($comment->content, FORMAT_PLAIN));
}
}
/**
* Tests the deletion of all comments in a context.
*/
public function test_delete_comments_for_all_users(): void {
global $DB;
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$coursecontext1 = \context_course::instance($course1->id);
$coursecontext2 = \context_course::instance($course2->id);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$comment1 = $this->get_comment_object($coursecontext1, $course1);
$comment2 = $this->get_comment_object($coursecontext2, $course2);
$this->setUser($user1);
$comment1->add('First comment for user 1 on comment 1');
$comment2->add('First comment for user 1 on comment 2');
$this->setUser($user2);
$comment1->add('First comment for user 2 on comment 1');
$comment2->add('First comment for user 2 on comment 2');
// Because of the way things are set up with validation, creating an entry with the same context in a different component
// or comment area is a huge pain. We're just going to jam entries into the table instead.
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'block_comments',
'commentarea' => 'other_comments',
'itemid' => 2,
'content' => 'Comment user 1 different comment area',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'tool_dataprivacy',
'commentarea' => 'page_comments',
'itemid' => 2,
'content' => 'Comment user 1 different component',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
// Delete only for the first context. All records in the comments table for this context should be removed.
provider::delete_comments_for_all_users($coursecontext1, 'block_comments', 'page_comments', 0);
// No records left here.
$this->assertCount(0, $comment1->get_comments());
// All of the records are left intact here.
$this->assertCount(2, $comment2->get_comments());
// Check the other comment area.
$result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('other_comments', $data->commentarea);
// Check the different component, same commentarea.
$result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('tool_dataprivacy', $data->component);
}
/**
* Tests the deletion of all comments in a context.
*/
public function test_delete_comments_for_all_users_select(): void {
global $DB;
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$coursecontext1 = \context_course::instance($course1->id);
$coursecontext2 = \context_course::instance($course2->id);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$comment1 = $this->get_comment_object($coursecontext1, $course1);
$comment2 = $this->get_comment_object($coursecontext2, $course2);
$this->setUser($user1);
$comment1->add('First comment for user 1 on comment 1');
$comment2->add('First comment for user 1 on comment 2');
$this->setUser($user2);
$comment1->add('First comment for user 2 on comment 1');
$comment2->add('First comment for user 2 on comment 2');
// Because of the way things are set up with validation, creating an entry with the same context in a different component
// or comment area is a huge pain. We're just going to jam entries into the table instead.
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'block_comments',
'commentarea' => 'other_comments',
'itemid' => 2,
'content' => 'Comment user 1 different comment area',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'tool_dataprivacy',
'commentarea' => 'page_comments',
'itemid' => 2,
'content' => 'Comment user 1 different component',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
// Delete only for the first context. All records in the comments table for this context should be removed.
list($sql, $params) = $DB->get_in_or_equal([0, 1, 2, 3], SQL_PARAMS_NAMED);
provider::delete_comments_for_all_users_select($coursecontext1,
'block_comments', 'page_comments', $sql, $params);
// No records left here.
$this->assertCount(0, $comment1->get_comments());
// All of the records are left intact here.
$this->assertCount(2, $comment2->get_comments());
// Check the other comment area.
$result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('other_comments', $data->commentarea);
// Check the different component, same commentarea.
$result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('tool_dataprivacy', $data->component);
}
/**
* Tests deletion of comments for a specified user and contexts.
*/
public function test_delete_comments_for_user(): void {
global $DB;
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();
$coursecontext1 = \context_course::instance($course1->id);
$coursecontext2 = \context_course::instance($course2->id);
$coursecontext3 = \context_course::instance($course3->id);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$comment1 = $this->get_comment_object($coursecontext1, $course1);
$comment2 = $this->get_comment_object($coursecontext2, $course2);
$comment3 = $this->get_comment_object($coursecontext3, $course3);
$this->setUser($user1);
$comment1->add('First comment for user 1');
$comment2->add('User 1 comment in second comment');
$this->setUser($user2);
$comment2->add('User two replied in comment two');
$comment3->add('Comment three for user 2.');
// Because of the way things are set up with validation, creating an entry with the same context in a different component
// or comment area is a huge pain. We're just going to jam entries into the table instead.
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'block_comments',
'commentarea' => 'other_comments',
'itemid' => 2,
'content' => 'Comment user 1 different comment area',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'tool_dataprivacy',
'commentarea' => 'page_comments',
'itemid' => 2,
'content' => 'Comment user 1 different component',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
// Delete the comments for user 1.
$approvedcontextlist = new approved_contextlist($user1, 'block_comments',
[$coursecontext1->id, $coursecontext2->id]);
provider::delete_comments_for_user($approvedcontextlist, 'block_comments', 'page_comments', 0);
// No comments left in comments 1 as only user 1 commented there.
$this->assertCount(0, $comment1->get_comments());
// Only user 2 comments left in comments 2.
$comment2comments = $comment2->get_comments();
$this->assertCount(1, $comment2comments);
$data = array_shift($comment2comments);
$this->assertEquals($user2->id, $data->userid);
// Nothing changed here as user 1 did not leave a comment.
$comment3comments = $comment3->get_comments();
$this->assertCount(1, $comment3comments);
$data = array_shift($comment3comments);
$this->assertEquals($user2->id, $data->userid);
// Check the other comment area.
$result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('other_comments', $data->commentarea);
// Check the different component, same commentarea.
$result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('tool_dataprivacy', $data->component);
}
/**
* Tests deletion of comments for a specified userlist and context.
*/
public function test_delete_comments_for_users(): void {
global $DB;
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();
$coursecontext1 = \context_course::instance($course1->id);
$coursecontext2 = \context_course::instance($course2->id);
$coursecontext3 = \context_course::instance($course3->id);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$comment1 = $this->get_comment_object($coursecontext1, $course1);
$comment2 = $this->get_comment_object($coursecontext2, $course2);
$comment3 = $this->get_comment_object($coursecontext3, $course3);
$this->setUser($user1);
$comment1->add('First comment for user 1');
$comment2->add('User 1 comment in second comment');
$this->setUser($user2);
$comment2->add('User two replied in comment two');
$this->setUser($user3);
$comment2->add('User 3 also writing on comment 2, but will not be deleted');
$comment3->add('Only user 3 commenting in comment 3.');
// Because of the way things are set up with validation, creating an entry with the same context in a different component
// or comment area is a huge pain. We're just going to jam entries into the table instead.
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'block_comments',
'commentarea' => 'other_comments',
'itemid' => 2,
'content' => 'Comment user 1 different comment area',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
$record = (object) [
'contextid' => $coursecontext1->id,
'component' => 'tool_dataprivacy',
'commentarea' => 'page_comments',
'itemid' => 2,
'content' => 'Comment user 1 different component',
'format' => 0,
'userid' => $user1->id,
'timecreated' => time()
];
$DB->insert_record('comments', $record);
// Delete the comments for users 1 and 2 in all 3 contexts.
$approvedusers = [$user1->id, $user2->id];
$approveduserlist = new approved_userlist($coursecontext1, 'block_comments', $approvedusers);
provider::delete_comments_for_users($approveduserlist, 'block_comments', 'page_comments');
$approveduserlist = new approved_userlist($coursecontext2, 'block_comments', $approvedusers);
provider::delete_comments_for_users($approveduserlist, 'block_comments', 'page_comments');
$approveduserlist = new approved_userlist($coursecontext3, 'block_comments', $approvedusers);
provider::delete_comments_for_users($approveduserlist, 'block_comments', 'page_comments');
// No comments left in comments 1 as only user 1 commented there.
$this->assertCount(0, $comment1->get_comments());
// Only user 3's comment left in comments 2 as user 1 and 2 were approved for deletion.
$comment2comments = $comment2->get_comments();
$this->assertCount(1, $comment2comments);
$comment2comment = array_shift($comment2comments);
$this->assertEquals($user3->id, $comment2comment->userid);
// Nothing changed here as user 1 and 2 did not leave a comment.
$comment3comments = $comment3->get_comments();
$this->assertCount(1, $comment3comments);
$data = array_shift($comment3comments);
$this->assertEquals($user3->id, $data->userid);
// Check the other comment area.
$result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('other_comments', $data->commentarea);
// Check the different component, same commentarea.
$result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
$this->assertCount(1, $result);
$data = array_shift($result);
$this->assertEquals('tool_dataprivacy', $data->component);
}
/**
* Creates a comment object
*
* @param context $context A context object.
* @param stdClass $course A course object.
* @return comment The comment object.
*/
protected function get_comment_object($context, $course) {
// Comment on course page.
$args = new \stdClass;
$args->context = $context;
$args->course = $course;
$args->area = 'page_comments';
$args->itemid = 0;
$args->component = 'block_comments';
$comment = new \comment($args);
$comment->set_post_permission(true);
return $comment;
}
}
@@ -0,0 +1,258 @@
<?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/>.
declare(strict_types=1);
namespace core_comment\reportbuilder\datasource;
use context_course;
use core_comment_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{date, select, text};
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
/**
* Unit tests for comments datasource
*
* @package core_comment
* @covers \core_comment\reportbuilder\datasource\comments
* @copyright 2022 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comments_test extends core_reportbuilder_testcase {
/**
* Test default datasource
*/
public function test_datasource_default(): void {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
// Our first user will create a single comment.
$userone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
$this->setUser($userone);
$useronecomment = $generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
])->add('Cool');
// Our second user will create a couple of comments.
$usertwo = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Amy']);
$this->setUser($usertwo);
$usertwocommentfirst = $generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
])->add('Super');
$this->waitForSecond(); // For consistent ordering we need distinct time for second user comments.
$usertwocommentsecond = $generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
])->add('Awesome');
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 1]);
$content = $this->get_custom_report_content($report->get('id'));
// Default columns are user, context, content, time created. Sorted by user and time created.
$contextname = $coursecontext->get_context_name();
$this->assertEquals([
[fullname($usertwo), $contextname, format_text('Super'), userdate($usertwocommentfirst->timecreated)],
[fullname($usertwo), $contextname, format_text('Awesome'), userdate($usertwocommentsecond->timecreated)],
[fullname($userone), $contextname, format_text('Cool'), userdate($useronecomment->timecreated)],
], array_map('array_values', $content));
}
/**
* Test datasource columns that aren't added by default
*/
public function test_datasource_non_default_columns(): void {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$courseurl = course_get_url($course);
$coursecontext = context_course::instance($course->id);
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
'content' => 'Cool',
]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
$report = $generator->create_report(['name' => 'Blogs', 'source' => comments::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'context:link']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:area']);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:itemid']);
$content = $this->get_custom_report_content($report->get('id'));
$this->assertCount(1, $content);
$this->assertEquals([
"<a href=\"{$courseurl}\">{$coursecontext->get_context_name()}</a>",
'block_comments',
'page_comments',
0,
], array_values($content[0]));
}
/**
* Data provider for {@see test_datasource_filters}
*
* @return array[]
*/
public function datasource_filters_provider(): array {
return [
// Comment.
'Filter content' => ['comment:content', [
'comment:content_operator' => text::CONTAINS,
'comment:content_value' => 'Cool',
], true],
'Filter content (no match)' => ['comment:content', [
'comment:content_operator' => text::IS_EQUAL_TO,
'comment:content_value' => 'Beans',
], false],
'Filter time created' => ['comment:timecreated', [
'comment:timecreated_operator' => date::DATE_RANGE,
'comment:timecreated_from' => 1622502000,
], true],
'Filter time created (no match)' => ['comment:timecreated', [
'comment:timecreated_operator' => date::DATE_RANGE,
'comment:timecreated_to' => 1622502000,
], false],
// Context.
'Context level' => ['context:level', [
'context:level_operator' => select::EQUAL_TO,
'context:level_value' => CONTEXT_COURSE,
], true],
'Context level (no match)' => ['context:level', [
'context:level_operator' => select::EQUAL_TO,
'context:level_value' => CONTEXT_BLOCK,
], false],
// User.
'Filter user' => ['user:username', [
'user:username_operator' => text::IS_EQUAL_TO,
'user:username_value' => 'admin',
], true],
'Filter user (no match)' => ['user:username', [
'user:username_operator' => text::IS_EQUAL_TO,
'user:username_value' => 'lionel',
], false],
];
}
/**
* Test datasource filters
*
* @param string $filtername
* @param array $filtervalues
* @param bool $expectmatch
*
* @dataProvider datasource_filters_provider
*/
public function test_datasource_filters(
string $filtername,
array $filtervalues,
bool $expectmatch
): void {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
'content' => 'Cool',
]);
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
// Create report containing single column, and given filter.
$report = $generator->create_report(['name' => 'Tasks', 'source' => comments::class, 'default' => 0]);
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'comment:component']);
// Add filter, set it's values.
$generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
$content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
if ($expectmatch) {
$this->assertCount(1, $content);
$this->assertEquals('block_comments', reset($content[0]));
} else {
$this->assertEmpty($content);
}
}
/**
* Stress test datasource
*
* In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
*/
public function test_stress_datasource(): void {
if (!PHPUNIT_LONGTEST) {
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
}
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([
'context' => $coursecontext,
'component' => 'block_comments',
'area' => 'page_comments',
'content' => 'Cool',
]);
$this->datasource_stress_test_columns(comments::class);
$this->datasource_stress_test_columns_aggregation(comments::class);
$this->datasource_stress_test_conditions(comments::class, 'comment:component');
}
}