diff --git a/.upgradenotes/MDL-7987-2024120515332266.yml b/.upgradenotes/MDL-7987-2024120515332266.yml new file mode 100644 index 00000000000..cda58041945 --- /dev/null +++ b/.upgradenotes/MDL-7987-2024120515332266.yml @@ -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 diff --git a/question/bank/comment/classes/comment_count_column.php b/question/bank/comment/classes/comment_count_column.php index 4c449d85b04..be24288ec7b 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..65df1ed9078 100644 --- a/question/bank/comment/tests/behat/behat_qbank_comment.php +++ b/question/bank/comment/tests/behat/behat_qbank_comment.php @@ -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')] + ), + ]; + } } diff --git a/question/bank/comment/tests/behat/behat_qbank_comment_deprecated.php b/question/bank/comment/tests/behat/behat_qbank_comment_deprecated.php new file mode 100644 index 00000000000..2ee33e50265 --- /dev/null +++ b/question/bank/comment/tests/behat/behat_qbank_comment_deprecated.php @@ -0,0 +1,54 @@ +. + +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 + * @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']); + } +} diff --git a/question/bank/comment/tests/behat/question_comment.feature b/question/bank/comment/tests/behat/question_comment.feature index f5958879fbf..da8dab44cce 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