MDL-79122 question: Correct capability check for question comments link.

This makes the rendering of the question comments count additionally
check the core moodle/comment:post capability, since this capability
is required to make comments. Previously, this was not being checked
so users could open the modal to make a comment, which was causing
a javascript error due to expected elements being missing.
This commit is contained in:
Conn Warwicker 2024-12-04 10:03:48 +00:00 committed by Mark Johnson
parent a798b1db39
commit d9d2388965
No known key found for this signature in database
GPG Key ID: EB30E1468CFAE242
5 changed files with 139 additions and 46 deletions

View File

@ -0,0 +1,12 @@
issueNumber: MDL-79122
notes:
core_question:
- message: >-
Behat steps `behat_qbank_comment::i_should_see_on_the_column`
and `behat_qbank_comment::i_click_on_the_row_containing` have been
deprecated in favour of the new component named selectors,
`qbank_comment > Comment count link` and
`qbank_comment > Comment count text` which can be used with the
standard `should exist` and `I click on` steps to replace the
custom steps.
type: deprecated

View File

@ -84,16 +84,29 @@ class comment_count_column extends column_base {
*/
protected function display_content($question, $rowclasses): void {
global $DB;
$syscontext = \context_system::instance();
$args = [
'component' => 'qbank_comment',
'commentarea' => 'question',
'itemid' => $question->id,
'contextid' => $syscontext->id,
$args = new \stdClass;
$args->contextid = $syscontext->id;
$args->courseid = $this->qbank->course->id;
$args->area = 'question';
$args->itemid = $question->id;
$args->component = 'qbank_comment';
$params = [
'component' => $args->component,
'commentarea' => $args->area,
'itemid' => $args->itemid,
'contextid' => $args->contextid,
];
$commentcount = $DB->count_records('comments', $args);
$commentcount = $DB->count_records('comments', $params);
$attributes = [];
if (question_has_capability_on($question, 'comment')) {
// Build up the comment object to see if we have correct permissions to post.
$comment = new \comment($args);
if (question_has_capability_on($question, 'comment') && $comment->can_post()) {
$tag = 'a';
$target = 'questioncommentpreview_' . $question->id;
$attributes = [
'href' => '#',
@ -102,8 +115,10 @@ class comment_count_column extends column_base {
'data-courseid' => $this->qbank->course->id,
'data-contextid' => $syscontext->id,
];
} else {
$tag = 'span';
}
echo \html_writer::tag('a', $commentcount, $attributes);
echo \html_writer::tag($tag, $commentcount, $attributes);
}
public function get_extra_classes(): array {

View File

@ -40,33 +40,6 @@ use Behat\Mink\Exception\ExpectationException as ExpectationException,
*/
class behat_qbank_comment extends behat_question_base {
/**
* Looks for a table, then looks for a row that contains the given text.
* Once it finds the right row, it clicks a link in that row.
*
* @When I click :arg1 on the row on the comments column
* @param string $linkname
* @param string $rowtext
*/
public function i_click_on_the_row_containing($linkname) {
$exception = new ElementNotFoundException($this->getSession(),
'Cannot find any row on the page containing the text ' . $linkname);
$row = $this->find('css', sprintf('table tbody tr td.commentcount a:contains("%s")', $linkname), $exception);
$row->click();
}
/**
* Looks for the appropriate comment count in the column.
*
* @Then I should see :arg1 on the comments column
* @param string $linkdata
*/
public function i_should_see_on_the_column($linkdata) {
$exception = new ElementNotFoundException($this->getSession(),
'Cannot find any row with the comment count of ' . $linkdata . ' on the column named Comments');
$this->find('css', sprintf('table tbody tr td.commentcount a:contains("%s")', $linkdata), $exception);
}
/**
* Adds the specified option to the question comments of the current modal.
*
@ -165,4 +138,26 @@ class behat_qbank_comment extends behat_question_base {
$this->getSession()->wait(4 * 1000);
}
/**
* Define named selectors for the comments column.
*
* Supported selectors are:
* - "qbank_comment > Comment count link" a comment count displayed as a link.
* - "qbank_comment > Comment count text" a comment count displayed as un-linked text.
*
* @return behat_component_named_selector[]
*/
public static function get_exact_named_selectors(): array {
$commentcountxpath = "//table/tbody/tr/td[contains(@class, 'commentcount')]/%s[text() = %%locator%%]";
return [
new behat_component_named_selector(
'Comment count link',
[sprintf($commentcountxpath, 'a')]
),
new behat_component_named_selector(
'Comment count text',
[sprintf($commentcountxpath, 'span')]
),
];
}
}

View File

@ -0,0 +1,54 @@
<?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/>.
require_once(__DIR__ . '/../../../../../lib/behat/behat_deprecated_base.php');
/**
* Deprecated behat steps for qbank_comment
*
* @package qbank_comment
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_qbank_comment_deprecated extends behat_deprecated_base {
/**
* Looks for the appropriate hyperlink comment count in the column.
*
* @Then I should see :arg1 on the comments column
* @param string $linkdata
* @deprecated Since Moodle 5.0 MDL-79122 in favour of the "qbank_comment > Comment count link" named selector.
* @todo Final removal in Moodle 6.0 MDL-82413.
*/
public function i_should_see_on_the_column(string $linkdata): void {
$this->deprecated_message("Use '\"{$linkdata}\" \"qbank_comment > Comment count link\" should exist'");
$this->execute('behat_general::should_exist', [$linkdata, 'qbank_comment > Comment count link']);
}
/**
* Looks for a table, then looks for a row that contains the given text.
* Once it finds the right row, it clicks a link in that row.
*
* @When I click :arg1 on the row on the comments column
* @param string $linkname
* @deprecated Since Moodle 5.0 MDL-79122 in favour of the "qbank_comment > Comment count link" named selector.
* @todo Final removal in Moodle 6.0 MDL-82413.
*/
public function i_click_on_the_row_containing(string $linkname): void {
$this->deprecated_message("Use 'I click on \"{$linkname}\" \"qbank_comment > Comment count link\"'");
$this->execute('behat_general::i_click_on', [$linkname, 'qbank_comment > Comment count link']);
}
}

View File

@ -27,30 +27,35 @@ Feature: A Teacher can comment in a question
Scenario: Add a comment in question
Given I am on the "Test quiz" "mod_quiz > question bank" page logged in as "teacher1"
And I apply question bank filter "Category" with value "Test questions"
And I should see "0" on the comments column
When I click "0" on the row on the comments column
And "0" "qbank_comment > Comment count link" should exist
And "1" "qbank_comment > Comment count link" should not exist
And I click on "0" "qbank_comment > Comment count link"
And I add "Super test comment 01" comment to question
And I click on "Add comment" "button" in the ".modal-dialog" "css_element"
And I should see "Super test comment 01"
And I click on "Close" "button" in the ".modal-dialog" "css_element"
Then I should see "1" on the comments column
And "1" "qbank_comment > Comment count link" should exist
And "0" "qbank_comment > Comment count link" should not exist
@javascript
Scenario: Delete a comment from question
Given I am on the "Test quiz" "mod_quiz > question bank" page logged in as "teacher1"
And I apply question bank filter "Category" with value "Test questions"
And I should see "0" on the comments column
When I click "0" on the row on the comments column
And "0" "qbank_comment > Comment count link" should exist
And "1" "qbank_comment > Comment count link" should not exist
And I click on "0" "qbank_comment > Comment count link"
And I add "Super test comment 01 to be deleted" comment to question
And I click on "Add comment" "button" in the ".modal-dialog" "css_element"
And I should see "Super test comment 01 to be deleted"
And I click on "Close" "button" in the ".modal-dialog" "css_element"
Then I should see "1" on the comments column
And I click "1" on the row on the comments column
And "1" "qbank_comment > Comment count link" should exist
And "0" "qbank_comment > Comment count link" should not exist
And I click on "1" "qbank_comment > Comment count link"
And I delete "Super test comment 01 to be deleted" comment from question
And I should not see "Super test comment 01 to be deleted"
And I click on "Close" "button" in the ".modal-dialog" "css_element"
But I should see "0" on the comments column
And "0" "qbank_comment > Comment count link" should exist
And "1" "qbank_comment > Comment count link" should not exist
@javascript
Scenario: Preview question with comments
@ -64,13 +69,15 @@ Feature: A Teacher can comment in a question
And I wait "1" seconds
Then I should see "Super test comment 01"
And I click on "Close preview" "button"
Then I should see "1" on the comments column
And "1" "qbank_comment > Comment count link" should exist
And "0" "qbank_comment > Comment count link" should not exist
And I choose "Preview" action for "First question" in the question bank
And I click on "Comments" "link"
And I delete "Super test comment 01" comment from question preview
And I should not see "Super test comment 01"
And I click on "Close preview" "button"
Then I should see "0" on the comments column
And "0" "qbank_comment > Comment count link" should exist
And "1" "qbank_comment > Comment count link" should not exist
@javascript
Scenario: Teacher with comment permissions for their own questions but not others questions
@ -143,9 +150,19 @@ Feature: A Teacher can comment in a question
And I press "id_submitbutton"
And I should not see "First question"
And I should see "Renamed question v2"
And I click "0" on the row on the comments column
And I click on "0" "qbank_comment > Comment count link"
And I should see "Version 2"
Then I should see "edited question"
And I should see "Version 1"
And I set the field "question_version_dropdown" to "Version 1"
And I should see "Answer the first question"
@javascript
Scenario: User without system moodle/comment:post capability cannot post comments on question
Given the following "role capability" exists:
| role | user |
| moodle/comment:post | prohibit |
Given I am on the "Test quiz" "mod_quiz > question bank" page logged in as "teacher1"
And I apply question bank filter "Category" with value "Test questions"
And "0" "qbank_comment > Comment count text" should exist
And "0" "qbank_comment > Comment count link" should not exist