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 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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