Merge branch 'MDL-53140-master' of git://github.com/rezaies/moodle
[moodle.git] / completion / tests / behat / behat_completion.php
CommitLineData
f076b0d4
DM
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
28require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
29
eb9ca848 30use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
f076b0d4
DM
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 */
40class 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.
8857c715
DW
52 $titleliteral = $userfullname . ", " . $activityname . ": Completed";
53 $xpath = "//table[@id='completion-progress']";
f076b0d4 54
eb9ca848 55 $this->execute("behat_completion::go_to_the_current_course_activity_completion_report");
8857c715
DW
56 $this->execute("behat_general::should_exist_in_the",
57 array($titleliteral, "icon", $xpath, "xpath_element")
f076b0d4
DM
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
38976081 70 // Will throw an exception if the element can not be hovered.
8857c715
DW
71 $titleliteral = $userfullname . ", " . $activityname . ": Not completed";
72 $xpath = "//table[@id='completion-progress']";
f076b0d4 73
eb9ca848 74 $this->execute("behat_completion::go_to_the_current_course_activity_completion_report");
8857c715
DW
75 $this->execute("behat_general::should_exist_in_the",
76 array($titleliteral, "icon", $xpath, "xpath_element")
77 );
f076b0d4
DM
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() {
10ac8baf 86 $completionnode = get_string('pluginname', 'report_progress');
02fda279 87 $reportsnode = get_string('reports');
f076b0d4 88
02fda279
VDF
89 $this->execute("behat_navigation::i_navigate_to_in_current_page_administration",
90 $reportsnode . ' > ' . $completionnode);
f076b0d4
DM
91 }
92
37ee40f3 93 /**
95d1a366 94 * Toggles completion tracking for course being in the course page.
37ee40f3 95 *
95d1a366
DM
96 * @When /^completion tracking is "(?P<completion_status_string>Enabled|Disabled)" in current course$/
97 * @param string $completionstatus The status, enabled or disabled.
37ee40f3
AB
98 */
99 public function completion_is_toggled_in_course($completionstatus) {
100
95d1a366 101 $toggle = strtolower($completionstatus) == 'enabled' ? get_string('yes') : get_string('no');
37ee40f3 102
eb9ca848
RT
103 // Go to course editing.
104 $this->execute("behat_general::click_link", get_string('editsettings'));
105
2b509dbc
MN
106 // Expand all the form fields.
107 $this->execute("behat_forms::i_expand_all_fieldsets");
108
eb9ca848
RT
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'));
37ee40f3 115 }
1a5a476b
RT
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$/
1a5a476b
RT
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 }
f4565e71
RT
128 $activityxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
129 $activityxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
1a5a476b 130
eb9ca848 131 $this->execute("behat_general::should_exist_in_the",
8857c715 132 array($imgalttext, "icon", $activityxpath, "xpath_element")
eb9ca848
RT
133 );
134
1a5a476b
RT
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$/
1a5a476b
RT
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 }
f4565e71
RT
148 $activityxpath = "//li[contains(concat(' ', @class, ' '), ' modtype_" . strtolower($activitytype) . " ')]";
149 $activityxpath .= "[descendant::*[contains(text(), '" . $activityname . "')]]";
1a5a476b 150
eb9ca848 151 $this->execute("behat_general::should_exist_in_the",
8857c715 152 array($imgalttext, "icon", $activityxpath, "xpath_element")
eb9ca848 153 );
1a5a476b 154 }
d6cea5cc
AB
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::span[@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::span[@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 }
f076b0d4 223}