Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Completion steps definitions.
  19   *
  20   * @package    core_completion
  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  
  32  /**
  33   * Steps definitions to deal with course and activities completion.
  34   *
  35   * @package    core_completion
  36   * @category   test
  37   * @copyright  2013 David MonllaĆ³
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class behat_completion extends behat_base {
  41  
  42      /**
  43       * Checks that the specified user has completed the specified activity of the current course.
  44       *
  45       * @Then /^"(?P<user_fullname_string>(?:[^"]|\\")*)" user has completed "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
  46       * @param string $userfullname
  47       * @param string $activityname
  48       */
  49      public function user_has_completed_activity($userfullname, $activityname) {
  50  
  51          // Will throw an exception if the element can not be hovered.
  52          $titleliteral = $userfullname . ", " . $activityname . ": Completed";
  53          $xpath = "//table[@id='completion-progress']";
  54  
  55          $this->execute("behat_completion::go_to_the_current_course_activity_completion_report");
  56          $this->execute("behat_general::should_exist_in_the",
  57              array($titleliteral, "icon", $xpath, "xpath_element")
  58          );
  59      }
  60  
  61      /**
  62       * Checks that the specified user has not completed the specified activity of the current course.
  63       *
  64       * @Then /^"(?P<user_fullname_string>(?:[^"]|\\")*)" user has not completed "(?P<activity_name_string>(?:[^"]|\\")*)" activity$/
  65       * @param string $userfullname
  66       * @param string $activityname
  67       */
  68      public function user_has_not_completed_activity($userfullname, $activityname) {
  69  
  70          // Will throw an exception if the element can not be hovered.
  71          $titleliteral = $userfullname . ", " . $activityname . ": Not completed";
  72          $xpath = "//table[@id='completion-progress']";
  73  
  74          $this->execute("behat_completion::go_to_the_current_course_activity_completion_report");
  75          $this->execute("behat_general::should_exist_in_the",
  76              array($titleliteral, "icon", $xpath, "xpath_element")
  77          );
  78      }
  79  
  80      /**
  81       * Goes to the current course activity completion report.
  82       *
  83       * @Given /^I go to the current course activity completion report$/
  84       */
  85      public function go_to_the_current_course_activity_completion_report() {
  86          $completionnode = get_string('pluginname', 'report_progress');
  87          $reportsnode = get_string('reports');
  88  
  89          $this->execute("behat_navigation::i_navigate_to_in_current_page_administration",
  90                  $reportsnode . ' > ' . $completionnode);
  91      }
  92  
  93      /**
  94       * Toggles completion tracking for course being in the course page.
  95       *
  96       * @When /^completion tracking is "(?P<completion_status_string>Enabled|Disabled)" in current course$/
  97       * @param string $completionstatus The status, enabled or disabled.
  98       */
  99      public function completion_is_toggled_in_course($completionstatus) {
 100  
 101          $toggle = strtolower($completionstatus) == 'enabled' ? get_string('yes') : get_string('no');
 102  
 103          // Go to course editing.
 104          $this->execute("behat_general::click_link", get_string('editsettings'));
 105  
 106          // Expand all the form fields.
 107          $this->execute("behat_forms::i_expand_all_fieldsets");
 108  
 109          // Enable completion.
 110          $this->execute("behat_forms::i_set_the_field_to",
 111              array(get_string('enablecompletion', 'completion'), $toggle));
 112  
 113          // Save course settings.
 114          $this->execute("behat_forms::press_button", get_string('savechangesanddisplay'));
 115      }
 116  
 117      /**
 118       * Checks if the activity with specified name is maked as complete.
 119       *
 120       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion should be marked as complete$/
 121       */
 122      public function activity_marked_as_complete($activityname, $activitytype, $completiontype) {
 123          if ($completiontype == "manual") {
 124              $imgalttext = get_string("completion-alt-manual-y", 'core_completion', $activityname);
 125          } else {
 126              $imgalttext = get_string("completion-alt-auto-y", 'core_completion', $activityname);
 127          }
 128          $activityxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 129          $activityxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 130  
 131          $this->execute("behat_general::should_exist_in_the",
 132              array($imgalttext, "icon", $activityxpath, "xpath_element")
 133          );
 134  
 135      }
 136  
 137      /**
 138       * Checks if the activity with specified name is maked as complete.
 139       *
 140       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion should be marked as not complete$/
 141       */
 142      public function activity_marked_as_not_complete($activityname, $activitytype, $completiontype) {
 143          if ($completiontype == "manual") {
 144              $imgalttext = get_string("completion-alt-manual-n", 'core_completion', $activityname);
 145          } else {
 146              $imgalttext = get_string("completion-alt-auto-n", 'core_completion', $activityname);
 147          }
 148          $activityxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 149          $activityxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 150  
 151          $this->execute("behat_general::should_exist_in_the",
 152              array($imgalttext, "icon", $activityxpath, "xpath_element")
 153          );
 154      }
 155  
 156      /**
 157       * Checks if the activity with specified name shows a information completion checkbox (i.e. showing the completion tracking
 158       * configuration).
 159       *
 160       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion shows a configuration completion checkbox/
 161       * @param string $activityname The activity name.
 162       * @param string $activitytype The activity type.
 163       * @param string $completiontype The completion type.
 164       */
 165      public function activity_has_configuration_completion_checkbox($activityname, $activitytype, $completiontype) {
 166          if ($completiontype == "manual") {
 167              $imgname = 'i/completion-manual-enabled';
 168          } else {
 169              $imgname = 'i/completion-auto-enabled';
 170          }
 171          $iconxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 172          $iconxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 173          $iconxpath .= "/descendant::div[@class='actions']/descendant::img[contains(@src, 'i/completion-')]";
 174  
 175          $this->execute("behat_general::the_attribute_of_should_contain",
 176              array("src", $iconxpath, "xpath_element", $imgname)
 177          );
 178      }
 179  
 180      /**
 181       * Checks if the activity with specified name shows a tracking completion checkbox (i.e. showing my completion tracking status)
 182       *
 183       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion shows a status completion checkbox/
 184       * @param string $activityname The activity name.
 185       * @param string $activitytype The activity type.
 186       * @param string $completiontype The completion type.
 187       */
 188      public function activity_has_status_completion_checkbox($activityname, $activitytype, $completiontype) {
 189          if ($completiontype == "manual") {
 190              $imgname = 'i/completion-manual-';
 191          } else {
 192              $imgname = 'i/completion-auto-';
 193          }
 194          $iconxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 195          $iconxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 196          $iconxpath .= "/descendant::div[@class='actions']/descendant::img[contains(@src, 'i/completion-')]";
 197  
 198          $this->execute("behat_general::the_attribute_of_should_contain",
 199              array("src", $iconxpath, "xpath_element", $imgname)
 200          );
 201  
 202          $this->execute("behat_general::the_attribute_of_should_not_contain",
 203              array("src", $iconxpath, "xpath_element", '-enabled')
 204          );
 205      }
 206  
 207      /**
 208       * Checks if the activity with specified name does not show any completion checkbox.
 209       *
 210       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity does not show any completion checkbox/
 211       * @param string $activityname The activity name.
 212       * @param string $activitytype The activity type.
 213       */
 214      public function activity_has_not_any_completion_checkbox($activityname, $activitytype) {
 215          $iconxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 216          $iconxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 217          $iconxpath .= "/descendant::img[contains(@src, 'i/completion-')]";
 218  
 219          $this->execute("behat_general::should_not_exist",
 220              array($iconxpath, "xpath_element")
 221          );
 222      }
 223  }