MDL-76591 comment: create test generators and Behat scenarios.

The added scenarios cover the management of comments: viewing,
filtering and deletion.
This commit is contained in:
Paul Holden 2022-12-08 10:44:58 +00:00
parent 35f3847064
commit 76327f04ac
5 changed files with 181 additions and 29 deletions

View File

@ -18,11 +18,11 @@ declare(strict_types=1);
namespace core_blog\reportbuilder\datasource;
use comment;
use context_system;
use context_user;
use core_blog_generator;
use core_collator;
use core_comment_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
@ -42,14 +42,6 @@ require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
*/
class blogs_test extends core_reportbuilder_testcase {
/**
* Required test libraries
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once("{$CFG->dirroot}/comment/lib.php");
}
/**
* Test default datasource
*/
@ -118,14 +110,15 @@ class blogs_test extends core_reportbuilder_testcase {
'filename' => 'hello.txt',
], 'hello');
// Add a comment.
$comment = new comment((object) [
/** @var core_comment_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_comment');
$generator->create_comment([
'context' => context_user::instance($user->id),
'component' => 'blog',
'area' => 'format_blog',
'itemid' => $blog->id,
'content' => 'Cool',
]);
$comment->add('Cool');
// Manually update the created/modified date of the blog.
$blog->created = 1654038000;

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:
| -0- | 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"

View File

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

View File

@ -0,0 +1,49 @@
<?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
*/
public function create_comment($record): comment {
$record = (array) $record;
$content = $record['content'] ?? '';
unset($record['content']);
$comment = new comment((object) $record);
$comment->add($content);
return $comment;
}
}

View File

@ -18,8 +18,8 @@ declare(strict_types=1);
namespace core_comment\reportbuilder\datasource;
use comment;
use context_course;
use core_comment_generator;
use core_reportbuilder_generator;
use core_reportbuilder_testcase;
use core_reportbuilder\local\filters\{date, text};
@ -39,14 +39,6 @@ require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
*/
class comments_test extends core_reportbuilder_testcase {
/**
* Require test libraries
*/
public static function setUpBeforeClass(): void {
global $CFG;
require_once("{$CFG->dirroot}/comment/lib.php");
}
/**
* Test default datasource
*/
@ -57,12 +49,14 @@ class comments_test extends core_reportbuilder_testcase {
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$comment = new comment((object) [
/** @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',
]);
$comment->add('Cool');
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
@ -91,12 +85,14 @@ class comments_test extends core_reportbuilder_testcase {
$courseurl = course_get_url($course);
$coursecontext = context_course::instance($course->id);
$comment = new comment((object) [
/** @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',
]);
$comment->add('Cool');
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
@ -175,12 +171,14 @@ class comments_test extends core_reportbuilder_testcase {
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$comment = new comment((object) [
/** @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',
]);
$comment->add('Cool');
/** @var core_reportbuilder_generator $generator */
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
@ -217,12 +215,14 @@ class comments_test extends core_reportbuilder_testcase {
$course = $this->getDataGenerator()->create_course();
$coursecontext = context_course::instance($course->id);
$comment = new comment((object) [
/** @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',
]);
$comment->add('Cool');
$this->datasource_stress_test_columns(comments::class);
$this->datasource_stress_test_columns_aggregation(comments::class);