Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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       * @When the :conditionname completion condition of :activityname is displayed as :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');
 173  
 174          $conditionslistlabel = get_string('completionrequirements', 'core_course', $activityname);
 175          $selector = "div[aria-label='$conditionslistlabel']";
 176  
 177          try {
 178              // If there is a dropdown, open it.
 179              $dropdownnode = $this->find('css', $selector . ' .dropdown-menu');
 180              if (!$dropdownnode->hasClass('show')) {
 181                  $params = ["button.dropdown-toggle", "css_element", $selector, "css_element"];
 182                  $this->execute("behat_general::i_click_on_in_the", $params);
 183              }
 184          } catch (ElementNotFoundException $e) {
 185              // If the dropdown does not exist, we are in the activity page, all good.
 186          }
 187  
 188          $xpath = "//div[@aria-label='$conditionslistlabel']//span[text()='$conditionname']/..";
 189          $this->execute("behat_general::assert_element_contains_text", [$text, $xpath, "xpath_element"]);
 190      }
 191  
 192      /**
 193       * Checks if the activity with specified name is maked as complete.
 194       *
 195       * @When the :conditionname completion condition of :activityname overridden by :username is displayed as :completionstatus
 196       * @param string $conditionname The completion condition text.
 197       * @param string $activityname The activity name.
 198       * @param string $username The full name of the user overriding the student's activity completion.
 199       * @param string $completionstatus The override completion status. Must be either of the following: 'todo', 'done'.
 200       */
 201      public function overridden_activity_completion_condition_displayed_as(string $conditionname, string $activityname,
 202              string $username, string $completionstatus): void {
 203          if (!in_array($completionstatus, ['todo', 'done'])) {
 204              throw new coding_exception('Invalid override completion status. It must be of type "todo" or "done".');
 205          }
 206  
 207          $conditionlabel = get_string('completion_setby:auto:' . $completionstatus, 'core_course', (object)[
 208              'condition' => $conditionname,
 209              'setby' => $username,
 210          ]);
 211          $conditionbadge = "div[aria-label='$conditionlabel']";
 212  
 213          $conditionslistlabel = get_string('completionrequirements', 'core_course', $activityname);
 214          $completionconditions = "div[aria-label='$conditionslistlabel']";
 215  
 216          $params = [$conditionbadge, 'css_element', $completionconditions, 'css_element'];
 217          $this->execute("behat_general::should_exist_in_the", $params);
 218      }
 219  
 220      /**
 221       * Checks the manual completion state of an activity.
 222       *
 223       * @Given /^the manual completion button of "(?P<activityname>(?:[^"]|\\")*)" is displayed as "(?P<completionstatus>(?:[^"]|\\")*)"$/
 224       * @param string $activityname The activity name.
 225       * @param string $completionstatus The completion status shown on the manual completion button.
 226       *                                 Must be either 'Mark as done' or 'Done'.
 227       */
 228      public function manual_completion_button_displayed_as(string $activityname, string $completionstatus): void {
 229          if (!in_array($completionstatus, ['Mark as done', 'Done'])) {
 230              throw new coding_exception('Invalid completion status. It must be "Mark as done" or "Done".');
 231          }
 232  
 233          $langstringkey = $completionstatus === 'Done' ? 'done' : 'markdone';
 234          $conditionslistlabel = get_string('completion_manual:aria:' . $langstringkey, 'core_course', $activityname);
 235          $selector = "button[aria-label='$conditionslistlabel']";
 236  
 237          $this->execute("behat_general::assert_element_contains_text", [$completionstatus, $selector, "css_element"]);
 238      }
 239  
 240      /**
 241       * Checks the manual completion state of an activity.
 242       *
 243       * @Given /^the manual completion button of "(?P<activityname>(?:[^"]|\\")*)" overridden by "(?P<username>(?:[^"]|\\")*)" is displayed as "(?P<completionstatus>(?:[^"]|\\")*)"$/
 244       * @param string $activityname The activity name.
 245       * @param string $username The full name of the user overriding the student's activity completion.
 246       * @param string $completionstatus The completion status shown on the manual completion button.
 247       *                                 Must be either 'Mark as done' or 'Done'.
 248       */
 249      public function overridden_manual_completion_button_displayed_as(string $activityname, string $username,
 250              string $completionstatus): void {
 251          if (!in_array($completionstatus, ['Mark as done', 'Done'])) {
 252              throw new coding_exception('Invalid completion status. It must be "Mark as done" or "Done".');
 253          }
 254  
 255          $langstringkey = $completionstatus === 'Done' ? 'done' : 'markdone';
 256          $conditionslistlabel = get_string('completion_setby:manual:' . $langstringkey, 'core_course', (object)[
 257              'activityname' => $activityname,
 258              'setby' => $username,
 259          ]);
 260          $selector = "button[aria-label='$conditionslistlabel']";
 261  
 262          $this->execute("behat_general::assert_element_contains_text", [$completionstatus, $selector, "css_element"]);
 263      }
 264  
 265      /**
 266       * Toggles the manual completion button for a given activity.
 267       *
 268       * @Given /^I toggle the manual completion state of "(?P<activityname>(?:[^"]|\\")*)"$/
 269       * @param string $activityname The activity name.
 270       */
 271      public function toggle_the_manual_completion_state(string $activityname): void {
 272          $selector = "button[data-action=toggle-manual-completion][data-activityname='{$activityname}']";
 273  
 274          $this->execute("behat_general::i_click_on", [$selector, "css_element"]);
 275      }
 276  
 277      /**
 278       * Check that the activity does show completion information.
 279       *
 280       * @Given /^there should be no completion information shown for "(?P<activityname>(?:[^"]|\\")*)"$/
 281       * @param string $activityname The activity name.
 282       */
 283      public function there_should_be_no_completion_for_activity(string $activityname): void {
 284          $containerselector = "div[data-region=activity-information][data-activityname='$activityname']";
 285          try {
 286              $this->find('css_element', $containerselector);
 287          } catch (ElementNotFoundException $e) {
 288              // If activity information container does not exist (activity dates not shown, completion info not shown), all good.
 289              return;
 290          }
 291  
 292          // Otherwise, ensure that the completion information does not exist.
 293          $elementselector = "div[data-region=completion-info]";
 294          $params = [$elementselector, "css_element", $containerselector, "css_element"];
 295          $this->execute("behat_general::should_not_exist_in_the", $params);
 296      }
 297  
 298      /**
 299       * Check that the manual completion button for the activity is disabled.
 300       *
 301       * @Given /^the manual completion button for "(?P<activityname>(?:[^"]|\\")*)" should be disabled$/
 302       * @param string $activityname The activity name.
 303       */
 304      public function the_manual_completion_button_for_activity_should_be_disabled(string $activityname): void {
 305          $selector = "div[data-region='activity-information'][data-activityname='$activityname'] button";
 306  
 307          $params = [$selector, "css_element"];
 308          $this->execute("behat_general::the_element_should_be_disabled", $params);
 309      }
 310  
 311      /**
 312       * Check that the manual completion button for the activity does not exist.
 313       *
 314       * @Given /^the manual completion button for "(?P<activityname>(?:[^"]|\\")*)" should not exist/
 315       * @param string $activityname The activity name.
 316       */
 317      public function the_manual_completion_button_for_activity_should_not_exist(string $activityname): void {
 318          $selector = "div[data-region=activity-information][data-activityname='$activityname'] button";
 319  
 320          $params = [$selector, "css_element"];
 321          $this->execute('behat_general::should_not_exist', $params);
 322      }
 323  
 324      /**
 325       * Check that the manual completion button for the activity exists.
 326       *
 327       * @Given /^the manual completion button for "(?P<activityname>(?:[^"]|\\")*)" should exist/
 328       * @param string $activityname The activity name.
 329       */
 330      public function the_manual_completion_button_for_activity_should_exist(string $activityname): void {
 331          $selector = "div[data-region=activity-information][data-activityname='$activityname'] button";
 332  
 333          $params = [$selector, "css_element"];
 334          $this->execute('behat_general::should_exist', $params);
 335      }
 336  
 337      /**
 338       * Check that the activity has the given automatic completion condition.
 339       *
 340       * @When :activityname should have the :conditionname completion condition
 341       * @param string $activityname The activity name.
 342       * @param string $conditionname The automatic condition name.
 343       */
 344      public function activity_should_have_the_completion_condition(string $activityname, string $conditionname): void {
 345          $containerselector = "div[data-region=activity-information][data-activityname='$activityname']";
 346  
 347          try {
 348              // If there is a dropdown, open it.
 349              $dropdownnode = $this->find('css', $containerselector . ' .dropdown-menu');
 350              if (!$dropdownnode->hasClass('show')) {
 351                  $params = ["button.dropdown-toggle", "css_element", $containerselector, "css_element"];
 352                  $this->execute("behat_general::i_click_on_in_the", $params);
 353              }
 354          } catch (ElementNotFoundException $e) {
 355              // If the dropdown does not exist, we are in the activity page, all good.
 356          }
 357  
 358          $params = [$conditionname, $containerselector, 'css_element'];
 359          $this->execute("behat_general::assert_element_contains_text", $params);
 360      }
 361  
 362      /**
 363       * Checks if the activity with specified name shows a information completion checkbox (i.e. showing the completion tracking
 364       * configuration).
 365       *
 366       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion shows a configuration 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_configuration_completion_checkbox($activityname, $activitytype, $completiontype) {
 372          if ($completiontype == "manual") {
 373              $imgname = 'i/completion-manual-enabled';
 374          } else {
 375              $imgname = 'i/completion-auto-enabled';
 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  
 386      /**
 387       * Checks if the activity with specified name shows a tracking completion checkbox (i.e. showing my completion tracking status)
 388       *
 389       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity with "(manual|auto)" completion shows a status completion checkbox/
 390       * @param string $activityname The activity name.
 391       * @param string $activitytype The activity type.
 392       * @param string $completiontype The completion type.
 393       */
 394      public function activity_has_status_completion_checkbox($activityname, $activitytype, $completiontype) {
 395          if ($completiontype == "manual") {
 396              $imgname = 'i/completion-manual-';
 397          } else {
 398              $imgname = 'i/completion-auto-';
 399          }
 400          $iconxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 401          $iconxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 402          $iconxpath .= "/descendant::div[@class='actions']/descendant::img[contains(@src, 'i/completion-')]";
 403  
 404          $this->execute("behat_general::the_attribute_of_should_contain",
 405              array("src", $iconxpath, "xpath_element", $imgname)
 406          );
 407  
 408          $this->execute("behat_general::the_attribute_of_should_not_contain",
 409              array("src", $iconxpath, "xpath_element", '-enabled')
 410          );
 411      }
 412  
 413      /**
 414       * Checks if the activity with specified name does not show any completion checkbox.
 415       *
 416       * @Given /^the "(?P<activityname_string>(?:[^"]|\\")*)" "(?P<activitytype_string>(?:[^"]|\\")*)" activity does not show any completion checkbox/
 417       * @param string $activityname The activity name.
 418       * @param string $activitytype The activity type.
 419       */
 420      public function activity_has_not_any_completion_checkbox($activityname, $activitytype) {
 421          $iconxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
 422          $iconxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
 423          $iconxpath .= "/descendant::img[contains(@src, 'i/completion-')]";
 424  
 425          $this->execute("behat_general::should_not_exist",
 426              array($iconxpath, "xpath_element")
 427          );
 428      }
 429  }