Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Behat question-related steps definitions.
  19   *
  20   * @package    core_question
  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__ . '/behat_question_base.php');
  29  
  30  use Behat\Gherkin\Node\TableNode as TableNode,
  31      Behat\Mink\Exception\ExpectationException as ExpectationException,
  32      Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  33  
  34  /**
  35   * Steps definitions related with the question bank management.
  36   *
  37   * @package    core_question
  38   * @category   test
  39   * @copyright  2013 David MonllaĆ³
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class behat_question extends behat_question_base {
  43  
  44      /**
  45       * Creates a question in the current course questions bank with the provided data. This step can only be used when creating question types composed by a single form.
  46       *
  47       * @Given /^I add a "(?P<question_type_name_string>(?:[^"]|\\")*)" question filling the form with:$/
  48       * @param string $questiontypename The question type name
  49       * @param TableNode $questiondata The data to fill the question type form.
  50       */
  51      public function i_add_a_question_filling_the_form_with($questiontypename, TableNode $questiondata) {
  52  
  53          // Go to question bank.
  54          $this->execute("behat_general::click_link", get_string('questionbank', 'question'));
  55  
  56          // Click on create question.
  57          $this->execute('behat_forms::press_button', get_string('createnewquestion', 'question'));
  58  
  59          // Add question.
  60          $this->finish_adding_question($questiontypename, $questiondata);
  61      }
  62  
  63      /**
  64       * Checks the state of the specified question.
  65       *
  66       * @Then /^the state of "(?P<question_description_string>(?:[^"]|\\")*)" question is shown as "(?P<state_string>(?:[^"]|\\")*)"$/
  67       * @throws ExpectationException
  68       * @throws ElementNotFoundException
  69       * @param string $questiondescription
  70       * @param string $state
  71       */
  72      public function the_state_of_question_is_shown_as($questiondescription, $state) {
  73  
  74          // Using xpath literal to avoid quotes problems.
  75          $questiondescriptionliteral = behat_context_helper::escape($questiondescription);
  76          $stateliteral = behat_context_helper::escape($state);
  77  
  78          // Split in two checkings to give more feedback in case of exception.
  79          $exception = new ElementNotFoundException($this->getSession(), 'Question "' . $questiondescription . '" ');
  80          $questionxpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]" .
  81                  "[contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')]," .
  82                  "{$questiondescriptionliteral})]";
  83          $this->find('xpath', $questionxpath, $exception);
  84  
  85          $exception = new ExpectationException('Question "' . $questiondescription . '" state is not "' . $state . '"', $this->getSession());
  86          $xpath = $questionxpath . "/div[@class='info']/div[@class='state' and contains(., {$stateliteral})]";
  87          $this->find('xpath', $xpath, $exception);
  88      }
  89  
  90      /**
  91       * Activates a particular action on a particular question in the question bank UI.
  92       *
  93       * @When I choose :action action for :questionname in the question bank
  94       * @param string $action the label for the action you want to activate.
  95       * @param string $questionname the question name.
  96       */
  97      public function i_action_the_question($action, $questionname) {
  98          // Open the menu.
  99          $this->execute("behat_general::i_click_on_in_the",
 100                  [get_string('edit'), 'link', $questionname, 'table_row']);
 101  
 102          // Click the action from the menu.
 103          $this->execute("behat_general::i_click_on_in_the",
 104                  [$action, 'link', $questionname, 'table_row']);
 105      }
 106  }