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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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   * Steps definitions related with blocks.
  19   *
  20   * @package   core_block
  21   * @category  test
  22   * @copyright 2012 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  use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  29  
  30  require_once (__DIR__ . '/../../../lib/behat/behat_base.php');
  31  
  32  /**
  33   * Blocks management steps definitions.
  34   *
  35   * @package    core_block
  36   * @category   test
  37   * @copyright  2012 David MonllaĆ³
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class behat_blocks extends behat_base {
  41  
  42      /**
  43       * Adds the selected block. Editing mode must be previously enabled.
  44       *
  45       * @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block$/
  46       * @param string $blockname
  47       */
  48      public function i_add_the_block($blockname) {
  49          $addblock = get_string('addblock');
  50          $this->execute('behat_general::i_click_on_in_the', [$addblock, 'link_exact', '.add_block_button', 'css_element']);
  51  
  52          if (!$this->running_javascript()) {
  53              $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
  54          } else {
  55              $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
  56          }
  57      }
  58  
  59      /**
  60       * Adds the selected block if it is not already present. Editing mode must be previously enabled.
  61       *
  62       * @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block if not present$/
  63       * @param string $blockname
  64       */
  65      public function i_add_the_block_if_not_present($blockname) {
  66          try {
  67              $this->get_text_selector_node('block', $blockname);
  68          } catch (ElementNotFoundException $e) {
  69              $this->execute('behat_blocks::i_add_the_block', [$blockname]);
  70          }
  71      }
  72  
  73      /**
  74       * Opens a block's actions menu if it is not already opened.
  75       *
  76       * @Given /^I open the "(?P<block_name_string>(?:[^"]|\\")*)" blocks action menu$/
  77       * @throws DriverException The step is not available when Javascript is disabled
  78       * @param string $blockname
  79       */
  80      public function i_open_the_blocks_action_menu($blockname) {
  81  
  82          if (!$this->running_javascript()) {
  83              // Action menu does not need to be open if Javascript is off.
  84              return;
  85          }
  86  
  87          // If it is already opened we do nothing.
  88          $blocknode = $this->get_text_selector_node('block', $blockname);
  89          if ($blocknode->hasClass('action-menu-shown')) {
  90              return;
  91          }
  92  
  93          $this->execute('behat_general::i_click_on_in_the',
  94                  array("a[data-toggle='dropdown']", "css_element", $this->escape($blockname), "block")
  95          );
  96      }
  97  
  98      /**
  99       * Clicks on Configure block for specified block. Page must be in editing mode.
 100       *
 101       * Argument block_name may be either the name of the block or CSS class of the block.
 102       *
 103       * @Given /^I configure the "(?P<block_name_string>(?:[^"]|\\")*)" block$/
 104       * @param string $blockname
 105       */
 106      public function i_configure_the_block($blockname) {
 107          // Note that since $blockname may be either block name or CSS class, we can not use the exact label of "Configure" link.
 108  
 109          $this->execute("behat_blocks::i_open_the_blocks_action_menu", $this->escape($blockname));
 110  
 111          $this->execute('behat_general::i_click_on_in_the',
 112              array("Configure", "link", $this->escape($blockname), "block")
 113          );
 114      }
 115  
 116      /**
 117       * Ensures that block can be added to the page but does not actually add it.
 118       *
 119       * @Then /^the add block selector should contain "(?P<block_name_string>(?:[^"]|\\")*)" block$/
 120       * @param string $blockname
 121       */
 122      public function the_add_block_selector_should_contain_block($blockname) {
 123          $addblock = get_string('addblock');
 124          $this->execute('behat_general::i_click_on', [$addblock, 'link_exact']);
 125  
 126  
 127          $cancelstr = get_string('cancel');
 128          if (!$this->running_javascript()) {
 129              $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
 130              $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']);
 131          } else {
 132              $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
 133              $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'button', $addblock, 'dialogue']);
 134          }
 135      }
 136  
 137      /**
 138       * Ensures that block can not be added to the page.
 139       *
 140       * @Then /^the add block selector should not contain "(?P<block_name_string>(?:[^"]|\\")*)" block$/
 141       * @param string $blockname
 142       */
 143      public function the_add_block_selector_should_not_contain_block($blockname) {
 144          $addblock = get_string('addblock');
 145          $this->execute('behat_general::i_click_on', [$addblock, 'link_exact']);
 146  
 147  
 148          $cancelstr = get_string('cancel');
 149          if (!$this->running_javascript()) {
 150              $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
 151              $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']);
 152          } else {
 153              $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
 154              $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'button', $addblock, 'dialogue']);
 155          }
 156      }
 157  }