Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Commenting system steps definitions for question.
  19   *
  20   * @package    qbank_comment
  21   * @copyright  2021 Catalyst IT Australia Pty Ltd
  22   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  27  require_once (__DIR__ . '/../../../../../lib/behat/behat_base.php');
  28  require_once (__DIR__ . '/../../../../tests/behat/behat_question_base.php');
  29  
  30  use Behat\Mink\Exception\ExpectationException as ExpectationException,
  31      Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  32  
  33  /**
  34   * Steps definitions to deal with the commenting system in question.
  35   *
  36   * @package    qbank_comment
  37   * @copyright  2021 Catalyst IT Australia Pty Ltd
  38   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class behat_qbank_comment extends behat_question_base {
  42  
  43      /**
  44       * Looks for a table, then looks for a row that contains the given text.
  45       * Once it finds the right row, it clicks a link in that row.
  46       *
  47       * @When I click :arg1 on the row on the comments column
  48       * @param string $linkname
  49       * @param string $rowtext
  50       */
  51      public function i_click_on_the_row_containing($linkname) {
  52          $exception = new ElementNotFoundException($this->getSession(),
  53                  'Cannot find any row on the page containing the text ' . $linkname);
  54          $row = $this->find('css', sprintf('table tbody tr td.commentcount a:contains("%s")', $linkname), $exception);
  55          $row->click();
  56      }
  57  
  58      /**
  59       * Looks for the appropriate comment count in the column.
  60       *
  61       * @Then I should see :arg1 on the comments column
  62       * @param string $linkdata
  63       */
  64      public function i_should_see_on_the_column($linkdata) {
  65          $exception = new ElementNotFoundException($this->getSession(),
  66                  'Cannot find any row with the comment count of ' . $linkdata . ' on the column named Comments');
  67          $this->find('css', sprintf('table tbody tr td.commentcount a:contains("%s")', $linkdata), $exception);
  68      }
  69  
  70      /**
  71       * Adds the specified option to the question comments of the current modal.
  72       *
  73       * @Then I add :arg1 comment to question
  74       * @param string $comment
  75       */
  76      public function i_add_comment_to_question($comment) {
  77  
  78          // Getting the textarea and setting the provided value.
  79          $exception = new ElementNotFoundException($this->getSession(), 'Question ');
  80  
  81          if ($this->running_javascript()) {
  82              $commentstextarea = $this->find('css',
  83                                          '.modal-dialog .question-comment-view .comment-area textarea', $exception);
  84              $commentstextarea->setValue($comment);
  85  
  86              // We delay 1 second which is all we need.
  87              $this->getSession()->wait(1000);
  88  
  89          } else {
  90              throw new ExpectationException('JavaScript not running', $this->getSession());
  91          }
  92      }
  93  
  94      /**
  95       * Adds the specified option to the question comments of the question preview.
  96       *
  97       * @Then I add :arg1 comment to question preview
  98       * @param string $comment
  99       */
 100      public function i_add_comment_to_question_preview($comment) {
 101  
 102          // Getting the textarea and setting the provided value.
 103          $exception = new ElementNotFoundException($this->getSession(), 'Question ');
 104  
 105          if ($this->running_javascript()) {
 106              $commentstextarea = $this->find('css',
 107                      '.comment-area textarea', $exception);
 108              $commentstextarea->setValue($comment);
 109  
 110              // We delay 1 second which is all we need.
 111              $this->getSession()->wait(1000);
 112  
 113          } else {
 114              throw new ExpectationException('JavaScript not running', $this->getSession());
 115          }
 116      }
 117  
 118      /**
 119       * Deletes the specified comment from the current question comment preview.
 120       *
 121       * @Then I delete :arg comment from question preview
 122       * @param string $comment
 123       */
 124      public function i_delete_comment_from_question_preview($comment) {
 125  
 126          $exception = new ElementNotFoundException($this->getSession(), '"' . $comment . '" comment ');
 127  
 128          // Using xpath liternal to avoid possible problems with comments containing quotes.
 129          $commentliteral = behat_context_helper::escape($comment);
 130  
 131          $commentxpath = "//*[contains(concat(' ', normalize-space(@class), ' '), ' comment-ctrl ')]" .
 132              "/descendant::div[@class='comment-message'][contains(., $commentliteral)]";
 133  
 134          // Click on delete icon.
 135          $this->execute('behat_general::i_click_on_in_the',
 136              ["Delete comment posted by", "icon", $this->escape($commentxpath), "xpath_element"]
 137          );
 138  
 139          // Wait for the animation to finish, in theory is just 1 sec, adding 4 just in case.
 140          $this->getSession()->wait(4 * 1000);
 141      }
 142  
 143      /**
 144       * Deletes the specified comment from the current question comment modal.
 145       *
 146       * @Then I delete :arg comment from question
 147       * @param string $comment
 148       */
 149      public function i_delete_comment_from_question($comment) {
 150  
 151          $exception = new ElementNotFoundException($this->getSession(), '"' . $comment . '" comment ');
 152  
 153          // Using xpath liternal to avoid possible problems with comments containing quotes.
 154          $commentliteral = behat_context_helper::escape($comment);
 155  
 156          $commentxpath = "//*[contains(concat(' ', normalize-space(@class), ' '), ' question-comment-view ')]" .
 157                  "/descendant::div[@class='comment-message'][contains(., $commentliteral)]";
 158  
 159          // Click on delete icon.
 160          $this->execute('behat_general::i_click_on_in_the',
 161                  ["Delete comment posted by", "icon", $this->escape($commentxpath), "xpath_element"]
 162          );
 163  
 164          // Wait for the animation to finish, in theory is just 1 sec, adding 4 just in case.
 165          $this->getSession()->wait(4 * 1000);
 166      }
 167  
 168  }