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 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);
  91          $this->execute("behat_general::i_click_on_in_the", [$completionnode, "link", "region-main", "region"]);
  92      }
  93  
  94      /**
  95       * Toggles completion tracking for course being in the course page.
  96       *
  97       * @When /^completion tracking is "(?P<completion_status_string>Enabled|Disabled)" in current course$/
  98       * @param string $completionstatus The status, enabled or disabled.
  99       */
 100      public function completion_is_toggled_in_course($completionstatus) {
 101  
 102          $toggle = strtolower($completionstatus) == 'enabled' ? get_string('yes') : get_string('no');
 103  
 104          // Go to course editing.
 105          $this->execute("behat_general::click_link", get_string('settings'));
 106  
 107          // Expand all the form fields.
 108          $this->execute("behat_forms::i_expand_all_fieldsets");
 109  
 110          // Enable completion.
 111          $this->execute("behat_forms::i_set_the_field_to",
 112              array(get_string('enablecompletion', 'completion'), $toggle));
 113  
 114          // Save course settings.
 115          $this->execute("behat_forms::press_button", get_string('savechangesanddisplay'));
 116      }
 117  
 118      /**
 119       * Checks if the activity with specified name is maked as complete.
 120       *
 121       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion should be marked as complete$/
 122       */
 123      public function activity_marked_as_complete($activityname, $activitytype, $completiontype) {
 124          if ($completiontype == "manual") {
 125              $imgalttext = get_string("completion-alt-manual-y", 'core_completion', $activityname);
 126          } else {
 127              $imgalttext = get_string("completion-alt-auto-y", 'core_completion', $activityname);
 128          }
 129          $activityxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 130          $activityxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 131  
 132          $this->execute("behat_general::should_exist_in_the",
 133              array($imgalttext, "icon", $activityxpath, "xpath_element")
 134          );
 135  
 136      }
 137  
 138      /**
 139       * Checks if the activity with specified name is maked as complete.
 140       *
 141       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion should be marked as not complete$/
 142       */
 143      public function activity_marked_as_not_complete($activityname, $activitytype, $completiontype) {
 144          if ($completiontype == "manual") {
 145              $imgalttext = get_string("completion-alt-manual-n", 'core_completion', $activityname);
 146          } else {
 147              $imgalttext = get_string("completion-alt-auto-n", 'core_completion', $activityname);
 148          }
 149          $activityxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 150          $activityxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 151  
 152          $this->execute("behat_general::should_exist_in_the",
 153              array($imgalttext, "icon", $activityxpath, "xpath_element")
 154          );
 155      }
 156  
 157      /**
 158       * Checks if the activity with specified name is maked as complete.
 159       *
 160       * @Given /^the "(?P<conditionname>(?:[^"]|\\")*)" completion condition of "(?P<activityname>(?:[^"]|\\")*)" is displayed as "(?P<completionstatus>(?:[^"]|\\")*)"$/
 161       * @param string $conditionname The completion condition text.
 162       * @param string $activityname The activity name.
 163       * @param string $completionstatus The completion status. Must be either of the following: 'todo', 'done', 'failed'.
 164       */
 165      public function activity_completion_condition_displayed_as(string $conditionname, string $activityname,
 166              string $completionstatus): void {
 167  
 168          if (!in_array($completionstatus, ['todo', 'done', 'failed'])) {
 169              throw new coding_exception('Invalid completion status. It must be of type "todo", "done", or "failed".');
 170          }
 171  
 172          $text = get_string("completion_automatic:$completionstatus", 'core_course') . ' ' . $conditionname;
 173  
 174          $conditionslistlabel = get_string('completionrequirements', 'core_course', $activityname);
 175          $selector = "div[aria-label='$conditionslistlabel']";
 176  
 177          $this->execute("behat_general::assert_element_contains_text", [$text, $selector, "css_element"]);
 178      }
 179  
 180      /**
 181       * Checks if the activity with specified name is maked as complete.
 182       *
 183       * @Given /^the "(?P<conditionname>(?:[^"]|\\")*)" completion condition of "(?P<activityname>(?:[^"]|\\")*)" overridden by "(?P<username>(?:[^"]|\\")*)" is displayed as "(?P<completionstatus>(?:[^"]|\\")*)"$/
 184       * @param string $conditionname The completion condition text.
 185       * @param string $activityname The activity name.
 186       * @param string $username The full name of the user overriding the student's activity completion.
 187       * @param string $completionstatus The override completion status. Must be either of the following: 'todo', 'done'.
 188       */
 189      public function overridden_activity_completion_condition_displayed_as(string $conditionname, string $activityname,
 190              string $username, string $completionstatus): void {
 191          if (!in_array($completionstatus, ['todo', 'done'])) {
 192              throw new coding_exception('Invalid override completion status. It must be of type "todo" or "done".');
 193          }
 194  
 195          $conditionlabel = get_string('completion_setby:auto:' . $completionstatus, 'core_course', (object)[
 196              'condition' => $conditionname,
 197              'setby' => $username,
 198          ]);
 199          $conditionbadge = "span[aria-label='$conditionlabel']";
 200  
 201          $conditionslistlabel = get_string('completionrequirements', 'core_course', $activityname);
 202          $completionconditions = "div[aria-label='$conditionslistlabel']";
 203  
 204          $params = [$conditionbadge, 'css_element', $completionconditions, 'css_element'];
 205          $this->execute("behat_general::should_exist_in_the", $params);
 206      }
 207  
 208      /**
 209       * Checks the manual completion state of an activity.
 210       *
 211       * @Given /^the manual completion button of "(?P<activityname>(?:[^"]|\\")*)" is displayed as "(?P<completionstatus>(?:[^"]|\\")*)"$/
 212       * @param string $activityname The activity name.
 213       * @param string $completionstatus The completion status shown on the manual completion button.
 214       *                                 Must be either 'Mark as done' or 'Done'.
 215       */
 216      public function manual_completion_button_displayed_as(string $activityname, string $completionstatus): void {
 217          if (!in_array($completionstatus, ['Mark as done', 'Done'])) {
 218              throw new coding_exception('Invalid completion status. It must be "Mark as done" or "Done".');
 219          }
 220  
 221          $langstringkey = $completionstatus === 'Done' ? 'done' : 'markdone';
 222          $conditionslistlabel = get_string('completion_manual:aria:' . $langstringkey, 'core_course', $activityname);
 223          $selector = "button[aria-label='$conditionslistlabel']";
 224  
 225          $this->execute("behat_general::assert_element_contains_text", [$completionstatus, $selector, "css_element"]);
 226      }
 227  
 228      /**
 229       * Checks the manual completion state of an activity.
 230       *
 231       * @Given /^the manual completion button of "(?P<activityname>(?:[^"]|\\")*)" overridden by "(?P<username>(?:[^"]|\\")*)" is displayed as "(?P<completionstatus>(?:[^"]|\\")*)"$/
 232       * @param string $activityname The activity name.
 233       * @param string $username The full name of the user overriding the student's activity completion.
 234       * @param string $completionstatus The completion status shown on the manual completion button.
 235       *                                 Must be either 'Mark as done' or 'Done'.
 236       */
 237      public function overridden_manual_completion_button_displayed_as(string $activityname, string $username,
 238              string $completionstatus): void {
 239          if (!in_array($completionstatus, ['Mark as done', 'Done'])) {
 240              throw new coding_exception('Invalid completion status. It must be "Mark as done" or "Done".');
 241          }
 242  
 243          $langstringkey = $completionstatus === 'Done' ? 'done' : 'markdone';
 244          $conditionslistlabel = get_string('completion_setby:manual:' . $langstringkey, 'core_course', (object)[
 245              'activityname' => $activityname,
 246              'setby' => $username,
 247          ]);
 248          $selector = "button[aria-label='$conditionslistlabel']";
 249  
 250          $this->execute("behat_general::assert_element_contains_text", [$completionstatus, $selector, "css_element"]);
 251      }
 252  
 253      /**
 254       * Toggles the manual completion button for a given activity.
 255       *
 256       * @Given /^I toggle the manual completion state of "(?P<activityname>(?:[^"]|\\")*)"$/
 257       * @param string $activityname The activity name.
 258       */
 259      public function toggle_the_manual_completion_state(string $activityname): void {
 260          $selector = "button[data-action=toggle-manual-completion][data-activityname='{$activityname}']";
 261  
 262          $this->execute("behat_general::i_click_on", [$selector, "css_element"]);
 263      }
 264  
 265      /**
 266       * Check that the activity does show completion information.
 267       *
 268       * @Given /^there should be no completion information shown for "(?P<activityname>(?:[^"]|\\")*)"$/
 269       * @param string $activityname The activity name.
 270       */
 271      public function there_should_be_no_completion_for_activity(string $activityname): void {
 272          $containerselector = "div[data-region=activity-information][data-activityname='$activityname']";
 273          try {
 274              $this->find('css_element', $containerselector);
 275          } catch (ElementNotFoundException $e) {
 276              // If activity information container does not exist (activity dates not shown, completion info not shown), all good.
 277              return;
 278          }
 279  
 280          // Otherwise, ensure that the completion information does not exist.
 281          $elementselector = "div[data-region=completion-info]";
 282          $params = [$elementselector, "css_element", $containerselector, "css_element"];
 283          $this->execute("behat_general::should_not_exist_in_the", $params);
 284      }
 285  
 286      /**
 287       * Check that the manual completion button for the activity is disabled.
 288       *
 289       * @Given /^the manual completion button for "(?P<activityname>(?:[^"]|\\")*)" should be disabled$/
 290       * @param string $activityname The activity name.
 291       */
 292      public function the_manual_completion_button_for_activity_should_be_disabled(string $activityname): void {
 293          $selector = "div[data-activityname='$activityname'] button";
 294  
 295          $params = [$selector, "css_element"];
 296          $this->execute("behat_general::the_element_should_be_disabled", $params);
 297      }
 298  
 299      /**
 300       * Check that the manual completion button for the activity does not exist.
 301       *
 302       * @Given /^the manual completion button for "(?P<activityname>(?:[^"]|\\")*)" should not exist/
 303       * @param string $activityname The activity name.
 304       */
 305      public function the_manual_completion_button_for_activity_should_not_exist(string $activityname): void {
 306          $selector = "div[data-activityname='$activityname'] button";
 307  
 308          $params = [$selector, "css_element"];
 309          $this->execute('behat_general::should_not_exist', $params);
 310      }
 311  
 312      /**
 313       * Check that the manual completion button for the activity exists.
 314       *
 315       * @Given /^the manual completion button for "(?P<activityname>(?:[^"]|\\")*)" should exist/
 316       * @param string $activityname The activity name.
 317       */
 318      public function the_manual_completion_button_for_activity_should_exist(string $activityname): void {
 319          $selector = "div[data-activityname='$activityname'] button";
 320  
 321          $params = [$selector, "css_element"];
 322          $this->execute('behat_general::should_exist', $params);
 323      }
 324  
 325      /**
 326       * Check that the activity has the given automatic completion condition.
 327       *
 328       * @Given /^"(?P<activityname>(?:[^"]|\\")*)" should have the "(?P<conditionname>(?:[^"]|\\")*)" completion condition$/
 329       * @param string $activityname The activity name.
 330       * @param string $conditionname The automatic condition name.
 331       */
 332      public function activity_should_have_the_completion_condition(string $activityname, string $conditionname): void {
 333          $containerselector = "div[data-region=activity-information][data-activityname='$activityname']";
 334  
 335          $params = [$conditionname, $containerselector, 'css_element'];
 336          $this->execute("behat_general::assert_element_contains_text", $params);
 337      }
 338  
 339      /**
 340       * Checks if the activity with specified name shows a information completion checkbox (i.e. showing the completion tracking
 341       * configuration).
 342       *
 343       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion shows a configuration completion checkbox/
 344       * @param string $activityname The activity name.
 345       * @param string $activitytype The activity type.
 346       * @param string $completiontype The completion type.
 347       */
 348      public function activity_has_configuration_completion_checkbox($activityname, $activitytype, $completiontype) {
 349          if ($completiontype == "manual") {
 350              $imgname = 'i/completion-manual-enabled';
 351          } else {
 352              $imgname = 'i/completion-auto-enabled';
 353          }
 354          $iconxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 355          $iconxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 356          $iconxpath .= "/descendant::div[@class='actions']/descendant::img[contains(@src, 'i/completion-')]";
 357  
 358          $this->execute("behat_general::the_attribute_of_should_contain",
 359              array("src", $iconxpath, "xpath_element", $imgname)
 360          );
 361      }
 362  
 363      /**
 364       * Checks if the activity with specified name shows a tracking completion checkbox (i.e. showing my completion tracking status)
 365       *
 366       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion shows a status completion checkbox/
 367       * @param string $activityname The activity name.
 368       * @param string $activitytype The activity type.
 369       * @param string $completiontype The completion type.
 370       */
 371      public function activity_has_status_completion_checkbox($activityname, $activitytype, $completiontype) {
 372          if ($completiontype == "manual") {
 373              $imgname = 'i/completion-manual-';
 374          } else {
 375              $imgname = 'i/completion-auto-';
 376          }
 377          $iconxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 378          $iconxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 379          $iconxpath .= "/descendant::div[@class='actions']/descendant::img[contains(@src, 'i/completion-')]";
 380  
 381          $this->execute("behat_general::the_attribute_of_should_contain",
 382              array("src", $iconxpath, "xpath_element", $imgname)
 383          );
 384  
 385          $this->execute("behat_general::the_attribute_of_should_not_contain",
 386              array("src", $iconxpath, "xpath_element", '-enabled')
 387          );
 388      }
 389  
 390      /**
 391       * Checks if the activity with specified name does not show any completion checkbox.
 392       *
 393       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity does not show any completion checkbox/
 394       * @param string $activityname The activity name.
 395       * @param string $activitytype The activity type.
 396       */
 397      public function activity_has_not_any_completion_checkbox($activityname, $activitytype) {
 398          $iconxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 399          $iconxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 400          $iconxpath .= "/descendant::img[contains(@src, 'i/completion-')]";
 401  
 402          $this->execute("behat_general::should_not_exist",
 403              array($iconxpath, "xpath_element")
 404          );
 405      }
 406  }