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.

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

   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  use Behat\Gherkin\Node\TableNode as TableNode;
  30  
  31  require_once (__DIR__ . '/../../../lib/behat/behat_base.php');
  32  
  33  /**
  34   * Blocks management steps definitions.
  35   *
  36   * @package    core_block
  37   * @category   test
  38   * @copyright  2012 David MonllaĆ³
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class behat_blocks extends behat_base {
  42  
  43      /**
  44       * Adds the selected block. Editing mode must be previously enabled.
  45       *
  46       * @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block$/
  47       * @param string $blockname
  48       */
  49      public function i_add_the_block($blockname) {
  50          $addblock = get_string('addblock');
  51          $this->execute('behat_general::i_click_on_in_the', [$addblock, 'link_exact', '.add_block_button', 'css_element']);
  52  
  53          if (!$this->running_javascript()) {
  54              $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
  55          } else {
  56              $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
  57          }
  58      }
  59  
  60      /**
  61       * Adds the selected block to the specified region
  62       *
  63       * Editing mode must be previously enabled.
  64       *
  65       * @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block to the "(?P<region_string>(?:[^"]|\\")*)" region$/
  66       * @param string $blockname
  67       * @param string $region
  68       */
  69      public function i_add_the_block_to_the_region(string $blockname, string $region) {
  70          if (!$this->running_javascript()) {
  71              throw new coding_exception('Adding block to specific region is not possible with Javascript disabled');
  72          }
  73          if ($region === "default") {
  74              $region = "";
  75          }
  76          $csselement = 'a[data-key="addblock"][data-blockregion='.behat_context_helper::escape($region).']';
  77          $addblock = get_string('addblock');
  78          $this->execute('behat_general::i_click_on', [$csselement, 'css_element']);
  79          $this->execute('behat_general::i_click_on_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
  80      }
  81  
  82      /**
  83       * Adds the selected block to the specified region and fills configuration form.
  84       *
  85       * Editing mode must be previously enabled.
  86       *
  87       * @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block to the (?P<region_string>(?:[^"]|\\")*) region with:$/
  88       * @param string $blockname
  89       * @param string $region
  90       * @param TableNode $data
  91       */
  92      public function i_add_the_block_to_the_region_with(string $blockname, string $region, TableNode $data) {
  93          $blocklabel = get_string('textellipsis', 'moodle', $blockname);
  94          $this->execute('behat_blocks::i_add_the_block_to_the_region', [$blocklabel, $region]);
  95          $this->wait_for_pending_js();
  96          $dialogname = get_string('addblock', 'core_block', $blockname);
  97          $this->execute('behat_forms::i_set_the_following_fields_in_container_to_these_values',
  98              [$dialogname, "dialogue", $data]);
  99          $this->execute('behat_general::i_click_on_in_the', ["Save changes", 'button', $dialogname, 'dialogue']);
 100      }
 101  
 102      /**
 103       * Adds the selected block if it is not already present. Editing mode must be previously enabled.
 104       *
 105       * @Given /^I add the "(?P<block_name_string>(?:[^"]|\\")*)" block if not present$/
 106       * @param string $blockname
 107       */
 108      public function i_add_the_block_if_not_present($blockname) {
 109          try {
 110              $this->get_text_selector_node('block', $blockname);
 111          } catch (ElementNotFoundException $e) {
 112              $this->execute('behat_blocks::i_add_the_block', [$blockname]);
 113          }
 114      }
 115  
 116      /**
 117       * Opens a block's actions menu if it is not already opened.
 118       *
 119       * @Given /^I open the "(?P<block_name_string>(?:[^"]|\\")*)" blocks action menu$/
 120       * @throws DriverException The step is not available when Javascript is disabled
 121       * @param string $blockname
 122       */
 123      public function i_open_the_blocks_action_menu($blockname) {
 124  
 125          if (!$this->running_javascript()) {
 126              // Action menu does not need to be open if Javascript is off.
 127              return;
 128          }
 129  
 130          // If it is already opened we do nothing.
 131          $blocknode = $this->get_text_selector_node('block', $blockname);
 132          if ($blocknode->hasClass('action-menu-shown')) {
 133              return;
 134          }
 135  
 136          $this->execute('behat_general::i_click_on_in_the',
 137                  array("a[data-toggle='dropdown']", "css_element", $this->escape($blockname), "block")
 138          );
 139      }
 140  
 141      /**
 142       * Clicks on Configure block for specified block. Page must be in editing mode.
 143       *
 144       * Argument block_name may be either the name of the block or CSS class of the block.
 145       *
 146       * @Given /^I configure the "(?P<block_name_string>(?:[^"]|\\")*)" block$/
 147       * @param string $blockname
 148       */
 149      public function i_configure_the_block($blockname) {
 150          // Note that since $blockname may be either block name or CSS class, we can not use the exact label of "Configure" link.
 151  
 152          $this->execute("behat_blocks::i_open_the_blocks_action_menu", $this->escape($blockname));
 153  
 154          $this->execute('behat_general::i_click_on_in_the',
 155              array("Configure", "link", $this->escape($blockname), "block")
 156          );
 157      }
 158  
 159      /**
 160       * Ensures that block can be added to the page but does not actually add it.
 161       *
 162       * @Then /^the add block selector should contain "(?P<block_name_string>(?:[^"]|\\")*)" block$/
 163       * @param string $blockname
 164       */
 165      public function the_add_block_selector_should_contain_block($blockname) {
 166          $addblock = get_string('addblock');
 167          $this->execute('behat_general::i_click_on', [$addblock, 'link_exact']);
 168  
 169  
 170          $cancelstr = get_string('cancel');
 171          if (!$this->running_javascript()) {
 172              $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
 173              $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']);
 174          } else {
 175              $this->execute('behat_general::should_exist_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
 176              $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'button', $addblock, 'dialogue']);
 177          }
 178      }
 179  
 180      /**
 181       * Ensures that block can not be added to the page.
 182       *
 183       * @Then /^the add block selector should not contain "(?P<block_name_string>(?:[^"]|\\")*)" block$/
 184       * @param string $blockname
 185       */
 186      public function the_add_block_selector_should_not_contain_block($blockname) {
 187          $addblock = get_string('addblock');
 188          $this->execute('behat_general::i_click_on', [$addblock, 'link_exact']);
 189  
 190  
 191          $cancelstr = get_string('cancel');
 192          if (!$this->running_javascript()) {
 193              $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', '#region-main', 'css_element']);
 194              $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'link_exact', '#region-main', 'css_element']);
 195          } else {
 196              $this->execute('behat_general::should_not_exist_in_the', [$blockname, 'link_exact', $addblock, 'dialogue']);
 197              $this->execute('behat_general::i_click_on_in_the', [$cancelstr, 'button', $addblock, 'dialogue']);
 198          }
 199      }
 200  }