[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/quiz/tests/behat/ -> behat_mod_quiz.php (source)

   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 to mod_quiz.
  19   *
  20   * @package   mod_quiz
  21   * @category  test
  22   * @copyright 2014 Marina Glancy
  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  require_once (__DIR__ . '/../../../../question/tests/behat/behat_question_base.php');
  30  
  31  use Behat\Behat\Context\Step\Given as Given,
  32      Behat\Gherkin\Node\TableNode as TableNode;
  33  
  34  /**
  35   * Steps definitions related to mod_quiz.
  36   *
  37   * @copyright 2014 Marina Glancy
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class behat_mod_quiz extends behat_question_base {
  41      /**
  42       * Adds a question to the existing quiz with filling the form.
  43       *
  44       * The form for creating a question should be on one page.
  45       *
  46       * @When /^I add a "(?P<question_type_string>(?:[^"]|\\")*)" question to the "(?P<quiz_name_string>(?:[^"]|\\")*)" quiz with:$/
  47       * @param string $questiontype
  48       * @param string $quizname
  49       * @param TableNode $questiondata with data for filling the add question form
  50       */
  51      public function i_add_question_to_the_quiz_with($questiontype, $quizname, TableNode $questiondata) {
  52          $quizname = $this->escape($quizname);
  53          $editquiz = $this->escape(get_string('editquiz', 'quiz'));
  54          $addaquestion = $this->escape(get_string('addaquestion', 'quiz'));
  55          $menuxpath = "//div[contains(@class, ' page-add-actions ')][last()]//a[contains(@class, ' textmenu')]";
  56          $itemxpath = "//div[contains(@class, ' page-add-actions ')][last()]//a[contains(@class, ' addquestion ')]";
  57          return array_merge(array(
  58              new Given("I follow \"$quizname\""),
  59              new Given("I follow \"$editquiz\""),
  60              new Given("I click on \"$menuxpath\" \"xpath_element\""),
  61              new Given("I click on \"$itemxpath\" \"xpath_element\""),
  62                  ), $this->finish_adding_question($questiontype, $questiondata));
  63      }
  64  
  65      /**
  66       * Set the max mark for a question on the Edit quiz page.
  67       *
  68       * @When /^I set the max mark for question "(?P<question_name_string>(?:[^"]|\\")*)" to "(?P<new_mark_string>(?:[^"]|\\")*)"$/
  69       * @param string $questionname the name of the question to set the max mark for.
  70       * @param string $newmark the mark to set
  71       */
  72      public function i_set_the_max_mark_for_quiz_question($questionname, $newmark) {
  73          return array(
  74              new Given('I follow "' . $this->escape(get_string('editmaxmark', 'quiz')) . '"'),
  75              new Given('I wait until "li input[name=maxmark]" "css_element" exists'),
  76              new Given('I should see "' . $this->escape(get_string('edittitleinstructions')) . '"'),
  77              new Given('I set the field "maxmark" to "' . $this->escape($newmark) . chr(10) . '"'),
  78          );
  79      }
  80  
  81      /**
  82       * Open the add menu on a given page, or at the end of the Edit quiz page.
  83       * @Given /^I open the "(?P<page_n_or_last_string>(?:[^"]|\\")*)" add to quiz menu$/
  84       * @param string $pageorlast either "Page n" or "last".
  85       */
  86      public function i_open_the_add_to_quiz_menu_for($pageorlast) {
  87  
  88          if (!$this->running_javascript()) {
  89              throw new DriverException('Activities actions menu not available when Javascript is disabled');
  90          }
  91  
  92          if ($pageorlast == 'last') {
  93              $xpath = "//div[@class = 'last-add-menu']//a[contains(@class, 'textmenu') and contains(., 'Add')]";
  94          } else if (preg_match('~Page (\d+)~', $pageorlast, $matches)) {
  95              $xpath = "//li[@id = 'page-{$matches[1]}']//a[contains(@class, 'textmenu') and contains(., 'Add')]";
  96          } else {
  97              throw new ExpectationException("The I open the add to quiz menu step must specify either 'Page N' or 'last'.");
  98          }
  99          $menu = $this->find('xpath', $xpath)->click();
 100      }
 101  
 102      /**
 103       * Click on a given link in the moodle-actionmenu that is currently open.
 104       * @Given /^I follow "(?P<link_string>(?:[^"]|\\")*)" in the open menu$/
 105       * @param string $linkstring the text (or id, etc.) of the link to click.
 106       * @return array of steps.
 107       */
 108      public function i_follow_in_the_open_menu($linkstring) {
 109          $openmenuxpath = "//div[contains(@class, 'moodle-actionmenu') and contains(@class, 'show')]";
 110          return array(
 111              new Given('I click on "' . $linkstring . '" "link" in the "' . $openmenuxpath . '" "xpath_element"'),
 112          );
 113      }
 114  
 115      /**
 116       * Check whether a particular question is on a particular page of the quiz on the Edit quiz page.
 117       * @Given /^I should see "(?P<question_name>(?:[^"]|\\")*)" on quiz page "(?P<page_number>\d+)"$/
 118       * @param string $questionname the name of the question we are looking for.
 119       * @param number $pagenumber the page it should be found on.
 120       * @return array of steps.
 121       */
 122      public function i_should_see_on_quiz_page($questionname, $pagenumber) {
 123          $xpath = "//li[contains(., '" . $this->escape($questionname) .
 124                  "')][./preceding-sibling::li[contains(@class, 'pagenumber')][1][contains(., 'Page " .
 125                  $pagenumber . "')]]";
 126          return array(
 127              new Given('"' . $xpath . '" "xpath_element" should exist'),
 128          );
 129      }
 130  
 131      /**
 132       * Check whether a particular question is not on a particular page of the quiz on the Edit quiz page.
 133       * @Given /^I should not see "(?P<question_name>(?:[^"]|\\")*)" on quiz page "(?P<page_number>\d+)"$/
 134       * @param string $questionname the name of the question we are looking for.
 135       * @param number $pagenumber the page it should be found on.
 136       * @return array of steps.
 137       */
 138      public function i_should_not_see_on_quiz_page($questionname, $pagenumber) {
 139          $xpath = "//li[contains(., '" . $this->escape($questionname) .
 140                  "')][./preceding-sibling::li[contains(@class, 'pagenumber')][1][contains(., 'Page " .
 141                  $pagenumber . "')]]";
 142          return array(
 143              new Given('"' . $xpath . '" "xpath_element" should not exist'),
 144          );
 145      }
 146  
 147      /**
 148       * Check whether one question comes before another on the Edit quiz page.
 149       * The two questions must be on the same page.
 150       * @Given /^I should see "(?P<first_q_name>(?:[^"]|\\")*)" before "(?P<second_q_name>(?:[^"]|\\")*)" on the edit quiz page$/
 151       * @param string $firstquestionname the name of the question that should come first in order.
 152       * @param string $secondquestionname the name of the question that should come immediately after it in order.
 153       * @return array of steps.
 154       */
 155      public function i_should_see_before_on_the_edit_quiz_page($firstquestionname, $secondquestionname) {
 156          $xpath = "//li[contains(@class, ' slot ') and contains(., '" . $this->escape($firstquestionname) .
 157                  "')]/following-sibling::li[contains(@class, ' slot ')][1]" .
 158                  "[contains(., '" . $this->escape($secondquestionname) . "')]";
 159          return array(
 160              new Given('"' . $xpath . '" "xpath_element" should exist'),
 161          );
 162      }
 163  
 164      /**
 165       * Check the number displayed alongside a question on the Edit quiz page.
 166       * @Given /^"(?P<question_name>(?:[^"]|\\")*)" should have number "(?P<number>(?:[^"]|\\")*)" on the edit quiz page$/
 167       * @param string $questionname the name of the question we are looking for.
 168       * @param number $number the number (or 'i') that should be displayed beside that question.
 169       * @return array of steps.
 170       */
 171      public function should_have_number_on_the_edit_quiz_page($questionname, $number) {
 172          $xpath = "//li[contains(@class, 'slot') and contains(., '" . $this->escape($questionname) .
 173                  "')]//span[contains(@class, 'slotnumber') and normalize-space(text()) = '" . $this->escape($number) . "']";
 174          return array(
 175              new Given('"' . $xpath . '" "xpath_element" should exist'),
 176          );
 177      }
 178  
 179      /**
 180       * Get the xpath for a partcular add/remove page-break icon.
 181       * @param string $addorremoves 'Add' or 'Remove'.
 182       * @param string $questionname the name of the question before the icon.
 183       * @return string the requried xpath.
 184       */
 185      protected function get_xpath_page_break_icon_after_question($addorremoves, $questionname) {
 186          return "//li[contains(@class, 'slot') and contains(., '" . $this->escape($questionname) .
 187                  "')]//a[contains(@class, 'page_split_join') and @title = '" . $addorremoves . " page break']";
 188      }
 189  
 190      /**
 191       * Click the add or remove page-break icon after a particular question.
 192       * @When /^I click on the "(Add|Remove)" page break icon after question "(?P<question_name>(?:[^"]|\\")*)"$/
 193       * @param string $addorremoves 'Add' or 'Remove'.
 194       * @param string $questionname the name of the question before the icon to click.
 195       * @return array of steps.
 196       */
 197      public function i_click_on_the_page_break_icon_after_question($addorremoves, $questionname) {
 198          $xpath = $this->get_xpath_page_break_icon_after_question($addorremoves, $questionname);
 199          return array(
 200              new Given('I click on "' . $xpath . '" "xpath_element"'),
 201          );
 202      }
 203  
 204      /**
 205       * Assert the add or remove page-break icon after a particular question exists.
 206       * @When /^the "(Add|Remove)" page break icon after question "(?P<question_name>(?:[^"]|\\")*)" should exist$/
 207       * @param string $addorremoves 'Add' or 'Remove'.
 208       * @param string $questionname the name of the question before the icon to click.
 209       * @return array of steps.
 210       */
 211      public function the_page_break_icon_after_question_should_exist($addorremoves, $questionname) {
 212          $xpath = $this->get_xpath_page_break_icon_after_question($addorremoves, $questionname);
 213          return array(
 214              new Given('"' . $xpath . '" "xpath_element" should exist'),
 215          );
 216      }
 217  
 218      /**
 219       * Assert the add or remove page-break icon after a particular question does not exist.
 220       * @When /^the "(Add|Remove)" page break icon after question "(?P<question_name>(?:[^"]|\\")*)" should not exist$/
 221       * @param string $addorremoves 'Add' or 'Remove'.
 222       * @param string $questionname the name of the question before the icon to click.
 223       * @return array of steps.
 224       */
 225      public function the_page_break_icon_after_question_should_not_exist($addorremoves, $questionname) {
 226          $xpath = $this->get_xpath_page_break_icon_after_question($addorremoves, $questionname);
 227          return array(
 228              new Given('"' . $xpath . '" "xpath_element" should not exist'),
 229          );
 230      }
 231  
 232      /**
 233       * Check the add or remove page-break link after a particular question contains the given parameters in its url.
 234       * @When /^the "(Add|Remove)" page break link after question "(?P<question_name>(?:[^"]|\\")*) should contain:"$/
 235       * @param string $addorremoves 'Add' or 'Remove'.
 236       * @param string $questionname the name of the question before the icon to click.
 237       * @param TableNode $paramdata with data for checking the page break url
 238       * @return array of steps.
 239       */
 240      public function the_page_break_link_after_question_should_contain($addorremoves, $questionname, $paramdata) {
 241          $xpath = $this->get_xpath_page_break_icon_after_question($addorremoves, $questionname);
 242          return array(
 243              new Given('I click on "' . $xpath . '" "xpath_element"'),
 244          );
 245      }
 246  
 247      /**
 248       * Move a question on the Edit quiz page by first clicking on the Move icon,
 249       * then clicking one of the "After ..." links.
 250       * @When /^I move "(?P<question_name>(?:[^"]|\\")*)" to "(?P<target>(?:[^"]|\\")*)" in the quiz by clicking the move icon$/
 251       * @param string $questionname the name of the question we are looking for.
 252       * @param string $target the target place to move to. One of the links in the pop-up like
 253       *      "After Page 1" or "After Question N".
 254       * @return array of steps.
 255       */
 256      public function i_move_question_after_item_by_clicking_the_move_icon($questionname, $target) {
 257          $iconxpath = "//li[contains(@class, ' slot ') and contains(., '" . $this->escape($questionname) .
 258                  "')]//span[contains(@class, 'editing_move')]";
 259          return array(
 260              new Given('I click on "' . $iconxpath . '" "xpath_element"'),
 261              new Given('I click on "' . $this->escape($target) . '" "text"'),
 262          );
 263      }
 264  
 265      /**
 266       * Move a question on the Edit quiz page by dragging a given question on top of another item.
 267       * @When /^I move "(?P<question_name>(?:[^"]|\\")*)" to "(?P<target>(?:[^"]|\\")*)" in the quiz by dragging$/
 268       * @param string $questionname the name of the question we are looking for.
 269       * @param string $target the target place to move to. Ether a question name, or "Page N"
 270       * @return array of steps.
 271       */
 272      public function i_move_question_after_item_by_dragging($questionname, $target) {
 273          $iconxpath = "//li[contains(@class, ' slot ') and contains(., '" . $this->escape($questionname) .
 274                  "')]//span[contains(@class, 'editing_move')]//img";
 275          $destinationxpath = "//li[contains(@class, ' slot ') or contains(@class, 'pagenumber ')]" .
 276                  "[contains(., '" . $this->escape($target) . "')]";
 277          return array(
 278              new Given('I drag "' . $iconxpath . '" "xpath_element" ' .
 279                  'and I drop it in "' . $destinationxpath . '" "xpath_element"'),
 280          );
 281      }
 282  
 283      /**
 284       * Delete a question on the Edit quiz page by first clicking on the Delete icon,
 285       * then clicking one of the "After ..." links.
 286       * @When /^I delete "(?P<question_name>(?:[^"]|\\")*)" in the quiz by clicking the delete icon$/
 287       * @param string $questionname the name of the question we are looking for.
 288       * @return array of steps.
 289       */
 290      public function i_delete_question_by_clicking_the_delete_icon($questionname) {
 291          $slotxpath = "//li[contains(@class, ' slot ') and contains(., '" . $this->escape($questionname) .
 292                  "')]";
 293          $deletexpath = "//a[contains(@class, 'editing_delete')]";
 294          return array(
 295              new Given('I click on "' . $slotxpath . $deletexpath . '" "xpath_element"'),
 296              new Given('I click on "Yes" "button" in the "Confirm" "dialogue"'),
 297          );
 298      }
 299  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1