Skip to content

Commit

Permalink
MDL-76591 comment: create test generators and Behat scenarios.
Browse files Browse the repository at this point in the history
The added scenarios cover the management of comments: viewing,
filtering and deletion.
  • Loading branch information
paulholden committed Jan 30, 2023
1 parent 35f3847 commit 76327f0
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 29 deletions.
17 changes: 5 additions & 12 deletions blog/tests/reportbuilder/datasource/blogs_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

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};
Expand All @@ -42,14 +42,6 @@
*/
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
*/
Expand Down Expand Up @@ -118,14 +110,15 @@ public function test_datasource_non_default_columns(): void {
'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;
Expand Down
49 changes: 49 additions & 0 deletions comment/tests/behat/manage.feature
Original file line number Diff line number Diff line change
@@ -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"
61 changes: 61 additions & 0 deletions comment/tests/generator/behat_core_comment_generator.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
49 changes: 49 additions & 0 deletions comment/tests/generator/lib.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 17 additions & 17 deletions comment/tests/reportbuilder/datasource/comments_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

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};
Expand All @@ -39,14 +39,6 @@
*/
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
*/
Expand All @@ -57,12 +49,14 @@ public function test_datasource_default(): void {
$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');
Expand Down Expand Up @@ -91,12 +85,14 @@ public function test_datasource_non_default_columns(): void {
$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');
Expand Down Expand Up @@ -175,12 +171,14 @@ public function test_datasource_filters(
$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');
Expand Down Expand Up @@ -217,12 +215,14 @@ public function test_stress_datasource(): void {
$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);
Expand Down

0 comments on commit 76327f0

Please sign in to comment.