Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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.
  19   *
  20   * @package    block_comments
  21   * @category   test
  22   * @copyright  2013 David MonllaĆ³
  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  
  28  require_once (__DIR__ . '/../../../../lib/behat/behat_base.php');
  29  
  30  use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
  31      Behat\Mink\Exception\ExpectationException as ExpectationException;
  32  
  33  /**
  34   * Steps definitions to deal with the commenting system
  35   *
  36   * @package    block_comments
  37   * @category   test
  38   * @copyright  2013 David MonllaĆ³
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class behat_block_comments extends behat_base {
  42  
  43      /**
  44       * Adds the specified option to the comments block of the current page.
  45       *
  46       * This method can be adapted in future to add other comments considering
  47       * that there could be more than one comment textarea per page.
  48       *
  49       * Only 1 comments block instance is allowed per page, if this changes this
  50       * steps definitions should be adapted.
  51       *
  52       * @Given /^I add "(?P<comment_text_string>(?:[^"]|\\")*)" comment to comments block$/
  53       * @throws ElementNotFoundException
  54       * @param string $comment
  55       */
  56      public function i_add_comment_to_comments_block($comment) {
  57  
  58          // Getting the textarea and setting the provided value.
  59          $exception = new ElementNotFoundException($this->getSession(), 'Comments block ');
  60  
  61          // The whole DOM structure changes depending on JS enabled/disabled.
  62          if ($this->running_javascript()) {
  63              $commentstextarea = $this->find('css', '.comment-area textarea', $exception);
  64              $commentstextarea->setValue($comment);
  65  
  66              $this->find_link(get_string('savecomment'))->click();
  67              // Delay after clicking so that additional comments will have unique time stamps.
  68              // We delay 1 second which is all we need.
  69              $this->getSession()->wait(1000);
  70  
  71          } else {
  72  
  73              $commentstextarea = $this->find('css', '.block_comments form textarea', $exception);
  74              $commentstextarea->setValue($comment);
  75  
  76              // Comments submit button
  77              $submit = $this->find('css', '.block_comments form input[type=submit]');
  78              $submit->press();
  79          }
  80      }
  81  
  82      /**
  83       * Deletes the specified comment from the current page's comments block.
  84       *
  85       * @Given /^I delete "(?P<comment_text_string>(?:[^"]|\\")*)" comment from comments block$/
  86       * @throws ElementNotFoundException
  87       * @throws ExpectationException
  88       * @param string $comment
  89       */
  90      public function i_delete_comment_from_comments_block($comment) {
  91  
  92          $exception = new ElementNotFoundException($this->getSession(), '"' . $comment . '" comment ');
  93  
  94          // Using xpath liternal to avoid possible problems with comments containing quotes.
  95          $commentliteral = behat_context_helper::escape($comment);
  96  
  97          $commentxpath = "//*[contains(concat(' ', normalize-space(@class), ' '), ' block_comments ')]" .
  98              "/descendant::div[@class='comment-message'][contains(., $commentliteral)]";
  99          $commentnode = $this->find('xpath', $commentxpath, $exception);
 100  
 101          // Click on delete icon.
 102          $this->execute('behat_general::i_click_on_in_the',
 103              array("Delete comment posted by", "icon", $this->escape($commentxpath), "xpath_element")
 104          );
 105  
 106          // Wait for the animation to finish, in theory is just 1 sec, adding 4 just in case.
 107          $this->getSession()->wait(4 * 1000);
 108      }
 109  
 110  }