diff --git a/question/bank/comment/classes/comment_count_column.php b/question/bank/comment/classes/comment_count_column.php index 6fc542f5d3c..e07f3e49089 100644 --- a/question/bank/comment/classes/comment_count_column.php +++ b/question/bank/comment/classes/comment_count_column.php @@ -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 { diff --git a/question/bank/comment/tests/behat/behat_qbank_comment.php b/question/bank/comment/tests/behat/behat_qbank_comment.php index 7b22aa2dded..dca635845b8 100644 --- a/question/bank/comment/tests/behat/behat_qbank_comment.php +++ b/question/bank/comment/tests/behat/behat_qbank_comment.php @@ -165,4 +165,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')] + ), + ]; + } } diff --git a/question/bank/comment/tests/behat/question_comment.feature b/question/bank/comment/tests/behat/question_comment.feature index 702e88df9e1..48daa153418 100644 --- a/question/bank/comment/tests/behat/question_comment.feature +++ b/question/bank/comment/tests/behat/question_comment.feature @@ -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