MDL-46894 make Behat backup steps more robust.
[moodle.git] / backup / util / ui / tests / behat / behat_backup.php
CommitLineData
030a606e
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 * Backup and restore actions to help behat feature files writting.
19 *
abe572e3 20 * @package core_backup
030a606e
DM
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');
29require_once(__DIR__ . '/../../../../../lib/behat/behat_field_manager.php');
ef03842a 30require_once(__DIR__ . '/../../../../../lib/tests/behat/behat_navigation.php');
030a606e
DM
31
32use Behat\Gherkin\Node\TableNode as TableNode,
33 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
34 Behat\Mink\Exception\ExpectationException as ExpectationException;
35
36/**
37 * Backup-related steps definitions.
38 *
abe572e3 39 * @package core_backup
030a606e
DM
40 * @category test
41 * @copyright 2013 David MonllaĆ³
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class behat_backup extends behat_base {
45
ef03842a
TH
46 /**
47 * Follow a link like 'Backup' or 'Import', where the link name comes from
48 * a language string, in the settings nav block of a course.
49 * @param string $langstring the lang string to look for. E.g. 'backup' or 'import'.
50 * @param string $component (optional) second argument to {@link get_string}.
51 */
52 protected function navigate_to_course_settings_link($langstring, $component = '') {
53 $behatnavigation = new behat_navigation();
54 $behatnavigation->setMink($this->getMink());
55 $behatnavigation->i_navigate_to_node_in(get_string($langstring, $component),
56 get_string('courseadministration'));
57 }
58
030a606e
DM
59 /**
60 * Backups the specified course using the provided options. If you are interested in restoring this backup would be useful to provide a 'Filename' option.
61 *
62 * @Given /^I backup "(?P<course_fullname_string>(?:[^"]|\\")*)" course using this options:$/
63 * @param string $backupcourse
64 * @param TableNode $options Backup options or false if no options provided
65 */
66 public function i_backup_course_using_this_options($backupcourse, $options = false) {
030a606e
DM
67 // We can not use other steps here as we don't know where the provided data
68 // table elements are used, and we need to catch exceptions contantly.
69
70 // Go to homepage.
71 $this->getSession()->visit($this->locate_path('/'));
72
73 // Click the course link.
74 $this->find_link($backupcourse)->click();
75
76 // Click the backup link.
ef03842a 77 $this->navigate_to_course_settings_link('backup');
c1faf86b 78 $this->wait();
030a606e
DM
79
80 // Initial settings.
87a37ebe 81 $this->fill_backup_restore_form($this->get_step_options($options, "Initial"));
dedb9738 82 $this->find_button(get_string('backupstage1action', 'backup'))->press();
c1faf86b 83 $this->wait();
030a606e
DM
84
85 // Schema settings.
87a37ebe 86 $this->fill_backup_restore_form($this->get_step_options($options, "Schema"));
dedb9738 87 $this->find_button(get_string('backupstage2action', 'backup'))->press();
c1faf86b 88 $this->wait();
030a606e
DM
89
90 // Confirmation and review, backup filename can also be specified.
87a37ebe 91 $this->fill_backup_restore_form($this->get_step_options($options, "Confirmation"));
dedb9738 92 $this->find_button(get_string('backupstage4action', 'backup'))->press();
030a606e
DM
93
94 // Waiting for it to finish.
c1faf86b 95 $this->wait(self::EXTENDED_TIMEOUT);
030a606e
DM
96
97 // Last backup continue button.
dedb9738 98 $this->find_button(get_string('backupstage16action', 'backup'))->press();
030a606e
DM
99 }
100
4bd605da
DM
101 /**
102 * Imports the specified origin course into the other course using the provided options.
103 *
104 * Keeping it separatelly from backup & restore, it the number of
105 * steps and duplicate code becomes bigger a common method should
106 * be generalized.
107 *
108 * @Given /^I import "(?P<from_course_fullname_string>(?:[^"]|\\")*)" course into "(?P<to_course_fullname_string>(?:[^"]|\\")*)" course using this options:$/
109 * @param string $fromcourse
110 * @param string $tocourse
111 * @param TableNode $options
112 */
113 public function i_import_course_into_course($fromcourse, $tocourse, $options = false) {
114
115 // We can not use other steps here as we don't know where the provided data
116 // table elements are used, and we need to catch exceptions contantly.
117
118 // Go to homepage.
119 $this->getSession()->visit($this->locate_path('/'));
c1faf86b 120 $this->wait();
4bd605da
DM
121
122 // Click the course link.
123 $this->find_link($tocourse)->click();
c1faf86b 124 $this->wait();
4bd605da 125
38976081 126 // Click the import link.
ef03842a 127 $this->navigate_to_course_settings_link('import');
c1faf86b 128 $this->wait();
4bd605da
DM
129
130 // Select the course.
131 $exception = new ExpectationException('"' . $fromcourse . '" course not found in the list of courses to import from', $this->getSession());
132
38976081
DM
133 // The argument should be converted to an xpath literal.
134 $fromcourse = $this->getSession()->getSelectorsHandler()->xpathLiteral($fromcourse);
135 $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' ics-results ')]" .
136 "/descendant::tr[contains(., $fromcourse)]" .
00ea74cb 137 "/descendant::input[@type='radio']";
4bd605da
DM
138 $radionode = $this->find('xpath', $xpath, $exception);
139 $radionode->check();
140 $radionode->click();
141
dedb9738 142 $this->find_button(get_string('continue'))->press();
c1faf86b 143 $this->wait();
4bd605da
DM
144
145 // Initial settings.
87a37ebe 146 $this->fill_backup_restore_form($this->get_step_options($options, "Initial"));
dedb9738 147 $this->find_button(get_string('importbackupstage1action', 'backup'))->press();
c1faf86b 148 $this->wait();
4bd605da
DM
149
150 // Schema settings.
87a37ebe 151 $this->fill_backup_restore_form($this->get_step_options($options, "Schema"));
dedb9738 152 $this->find_button(get_string('importbackupstage2action', 'backup'))->press();
c1faf86b 153 $this->wait();
4bd605da
DM
154
155 // Run it.
dedb9738 156 $this->find_button(get_string('importbackupstage4action', 'backup'))->press();
c1faf86b 157 $this->wait(self::EXTENDED_TIMEOUT);
4bd605da
DM
158
159 // Continue and redirect to 'to' course.
dedb9738 160 $this->find_button(get_string('continue'))->press();
4bd605da
DM
161 }
162
030a606e
DM
163 /**
164 * Restores the backup into the specified course and the provided options. You should be in the 'Restore' page where the backup is.
165 *
166 * @Given /^I restore "(?P<backup_filename_string>(?:[^"]|\\")*)" backup into "(?P<existing_course_fullname_string>(?:[^"]|\\")*)" course using this options:$/
167 * @param string $backupfilename
168 * @param string $existingcourse
169 * @param TableNode $options Restore forms options or false if no options provided
170 */
171 public function i_restore_backup_into_course_using_this_options($backupfilename, $existingcourse, $options = false) {
172
173 // Confirm restore.
174 $this->select_backup($backupfilename);
175
38976081
DM
176 // The argument should be converted to an xpath literal.
177 $existingcourse = $this->getSession()->getSelectorsHandler()->xpathLiteral($existingcourse);
178
030a606e 179 // Selecting the specified course (we can not call behat_forms::select_radio here as is in another behat subcontext).
38976081 180 $radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-existing-course ')]" .
00ea74cb 181 "/descendant::div[@class='restore-course-search']" .
38976081 182 "/descendant::tr[contains(., $existingcourse)]" .
00ea74cb 183 "/descendant::input[@type='radio']");
030a606e
DM
184 $radionode->check();
185 $radionode->click();
186
187 // Pressing the continue button of the restore into an existing course section.
38976081 188 $continuenode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-existing-course ')]" .
dedb9738 189 "/descendant::input[@type='submit'][@value='" . get_string('continue') . "']");
030a606e
DM
190 $continuenode->click();
191 $this->wait();
192
193 // Common restore process using provided key/value options.
194 $this->process_restore($options);
195 }
196
197 /**
198 * Restores the specified backup into a new course using the provided options. You should be in the 'Restore' page where the backup is.
199 *
200 * @Given /^I restore "(?P<backup_filename_string>(?:[^"]|\\")*)" backup into a new course using this options:$/
201 * @param string $backupfilename
202 * @param TableNode $options Restore forms options or false if no options provided
203 */
204 public function i_restore_backup_into_a_new_course_using_this_options($backupfilename, $options = false) {
205
206 // Confirm restore.
207 $this->select_backup($backupfilename);
208
209 // The first category in the list.
38976081 210 $radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-new-course ')]" .
00ea74cb
DM
211 "/descendant::div[@class='restore-course-search']" .
212 "/descendant::input[@type='radio']");
030a606e
DM
213 $radionode->check();
214 $radionode->click();
215
216 // Pressing the continue button of the restore into an existing course section.
38976081 217 $continuenode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-new-course ')]" .
dedb9738 218 "/descendant::input[@type='submit'][@value='" . get_string('continue') . "']");
030a606e
DM
219 $continuenode->click();
220 $this->wait();
221
222 // Common restore process using provided key/value options.
223 $this->process_restore($options);
224 }
225
226 /**
227 * Merges the backup into the current course using the provided restore options. You should be in the 'Restore' page where the backup is.
228 *
229 * @Given /^I merge "(?P<backup_filename_string>(?:[^"]|\\")*)" backup into the current course using this options:$/
230 * @param string $backupfilename
231 * @param TableNode $options Restore forms options or false if no options provided
232 */
233 public function i_merge_backup_into_the_current_course($backupfilename, $options = false) {
234
235 // Confirm restore.
236 $this->select_backup($backupfilename);
237
238 // Merge without deleting radio option.
38976081 239 $radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), 'bcs-current-course')]" .
00ea74cb 240 "/descendant::input[@type='radio'][@name='target'][@value='1']");
030a606e
DM
241 $radionode->check();
242 $radionode->click();
243
244 // Pressing the continue button of the restore merging section.
38976081 245 $continuenode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), 'bcs-current-course')]" .
dedb9738 246 "/descendant::input[@type='submit'][@value='" . get_string('continue') . "']");
030a606e
DM
247 $continuenode->click();
248 $this->wait();
249
250 // Common restore process using provided key/value options.
251 $this->process_restore($options);
252 }
253
254 /**
255 * Merges the backup into the current course after deleting this contents, using the provided restore options. You should be in the 'Restore' page where the backup is.
256 *
257 * @Given /^I merge "(?P<backup_filename_string>(?:[^"]|\\")*)" backup into the current course after deleting it's contents using this options:$/
258 * @param string $backupfilename
259 * @param TableNode $options Restore forms options or false if no options provided
260 */
261 public function i_merge_backup_into_current_course_deleting_its_contents($backupfilename, $options = false) {
262
263 // Confirm restore.
264 $this->select_backup($backupfilename);
265
266 // Delete contents radio option.
38976081 267 $radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), 'bcs-current-course')]" .
00ea74cb 268 "/descendant::input[@type='radio'][@name='target'][@value='0']");
030a606e
DM
269 $radionode->check();
270 $radionode->click();
271
272 // Pressing the continue button of the restore merging section.
38976081 273 $continuenode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), 'bcs-current-course')]" .
dedb9738 274 "/descendant::input[@type='submit'][@value='" . get_string('continue') . "']");
030a606e
DM
275 $continuenode->click();
276 $this->wait();
277
278 // Common restore process using provided key/value options.
279 $this->process_restore($options);
280 }
281
282 /**
283 * Selects the backup to restore.
284 *
285 * @throws ExpectationException
286 * @param string $backupfilename
287 * @return void
288 */
289 protected function select_backup($backupfilename) {
290
291 // Using xpath as there are other restore links before this one.
292 $exception = new ExpectationException('The "' . $backupfilename . '" backup file can not be found in this page', $this->getSession());
38976081
DM
293
294 // The argument should be converted to an xpath literal.
295 $backupfilename = $this->getSession()->getSelectorsHandler()->xpathLiteral($backupfilename);
296
297 $xpath = "//tr[contains(., $backupfilename)]/descendant::a[contains(., '" . get_string('restore') . "')]";
030a606e
DM
298 $restorelink = $this->find('xpath', $xpath, $exception);
299 $restorelink->click();
300
301 // Confirm the backup contents.
dedb9738 302 $restore = $this->find_button(get_string('continue'))->press();
030a606e
DM
303 }
304
305 /**
306 * Executes the common steps of all restore processes.
307 *
308 * @param TableNode $options The backup and restore options or false if no options provided
309 * @return void
310 */
311 protected function process_restore($options) {
312
4bd605da
DM
313 // We can not use other steps here as we don't know where the provided data
314 // table elements are used, and we need to catch exceptions contantly.
315
030a606e 316 // Settings.
87a37ebe 317 $this->fill_backup_restore_form($this->get_step_options($options, "Settings"));
dedb9738 318 $this->find_button(get_string('restorestage4action', 'backup'))->press();
c1faf86b 319 $this->wait();
030a606e
DM
320
321 // Schema.
87a37ebe 322 $this->fill_backup_restore_form($this->get_step_options($options, "Schema"));
dedb9738 323 $this->find_button(get_string('restorestage8action', 'backup'))->press();
c1faf86b 324 $this->wait();
030a606e
DM
325
326 // Review, no options here.
dedb9738 327 $this->find_button(get_string('restorestage16action', 'backup'))->press();
d1e55a47 328 $this->wait();
030a606e
DM
329
330 // Last restore continue button, redirected to restore course after this.
dedb9738 331 $this->find_button(get_string('restorestage32action', 'backup'))->press();
c1faf86b
DM
332
333 // Long wait when waiting for the restore to finish.
334 $this->wait(self::EXTENDED_TIMEOUT);
030a606e
DM
335 }
336
337 /**
338 * Tries to fill the current page form elements with the provided options.
339 *
340 * This step is slow as it spins over each provided option, we are
341 * not expected to have lots of provided options, anyways, is better
342 * to be conservative and wait for the elements to appear rather than
343 * to have false failures.
344 *
345 * @param TableNode $options The backup and restore options or false if no options provided
346 * @return void
347 */
348 protected function fill_backup_restore_form($options) {
349
350 // Nothing to fill if no options are provided.
351 if (!$options) {
352 return;
353 }
354
355 // If we find any of the provided options in the current form we should set the value.
356 $datahash = $options->getRowsHash();
357 foreach ($datahash as $locator => $value) {
87a37ebe
AN
358 $field = behat_field_manager::get_form_field_from_label($locator, $this);
359 $field->set_value($value);
360 }
361 }
030a606e 362
87a37ebe
AN
363 /**
364 * Get the options specific to this step of the backup/restore process.
365 *
366 * @param TableNode $options The options table to filter
367 * @param string $step The name of the step
368 * @return TableNode The filtered options table
369 * @throws ExpectationException
370 */
371 protected function get_step_options($options, $step) {
372 // Nothing to fill if no options are provided.
373 if (!$options) {
374 return;
375 }
030a606e 376
87a37ebe
AN
377 $pageoptions = clone $options;
378
379 $rows = $options->getRows();
380 $newrows = array();
381 foreach ($rows as $k => $data) {
382 if (count($data) !== 3) {
383 // Not enough information to guess the page.
384 throw new ExpectationException("The backup/restore step must be specified for all backup options");
385 } else if ($data[0] == $step) {
386 unset($data[0]);
387 $newrows[] = $data;
030a606e
DM
388 }
389 }
87a37ebe
AN
390 $pageoptions->setRows($newrows);
391 return $pageoptions;
030a606e
DM
392 }
393
87a37ebe 394
030a606e 395 /**
c1faf86b 396 * Waits until the DOM and the page Javascript code is ready.
030a606e 397 *
c1faf86b 398 * @param int $timeout The number of seconds that we wait.
030a606e
DM
399 * @return void
400 */
c1faf86b 401 protected function wait($timeout = false) {
030a606e 402
38976081
DM
403 if (!$this->running_javascript()) {
404 return;
405 }
406
c1faf86b
DM
407 if (!$timeout) {
408 $timeout = self::TIMEOUT;
409 }
410
411 $this->getSession()->wait($timeout * 1000, self::PAGE_READY_JS);
030a606e
DM
412 }
413
414}