2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Backup and restore actions to help behat feature files writting.
20 * @package core_backup
22 * @copyright 2013 David Monllaó
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
28 require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php');
29 require_once(__DIR__ . '/../../../../../lib/behat/behat_field_manager.php');
30 require_once(__DIR__ . '/../../../../../lib/tests/behat/behat_navigation.php');
32 use Behat\Gherkin\Node\TableNode as TableNode,
33 Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
34 Behat\Mink\Exception\ExpectationException as ExpectationException;
37 * Backup-related steps definitions.
39 * @package core_backup
41 * @copyright 2013 David Monllaó
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class behat_backup extends behat_base {
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}.
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'));
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.
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
66 public function i_backup_course_using_this_options($backupcourse, $options = false) {
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.
71 $this->getSession()->visit($this->locate_path('/'));
73 // Click the course link.
74 $this->find_link($backupcourse)->click();
76 // Click the backup link.
77 $this->navigate_to_course_settings_link('backup');
80 // Expand the backup settings section.
81 $this->find_link(get_string('backupsettings', 'backup'))->click();
84 $this->fill_backup_restore_form($this->get_step_options($options, "Initial"));
85 $this->find_button(get_string('backupstage1action', 'backup'))->press();
89 $this->fill_backup_restore_form($this->get_step_options($options, "Schema"));
90 $this->find_button(get_string('backupstage2action', 'backup'))->press();
93 // Confirmation and review, backup filename can also be specified.
94 $this->fill_backup_restore_form($this->get_step_options($options, "Confirmation"));
95 $this->find_button(get_string('backupstage4action', 'backup'))->press();
97 // Waiting for it to finish.
98 $this->wait(self::EXTENDED_TIMEOUT);
100 // Last backup continue button.
101 $this->find_button(get_string('backupstage16action', 'backup'))->press();
105 * Performs a quick (one click) backup of a course.
107 * Please note that because you can't set settings with this there is no way to know what the filename
108 * that was produced was. It contains a timestamp making it hard to find.
110 * @Given /^I perform a quick backup of course "(?P<course_fullname_string>(?:[^"]|\\")*)"$/
111 * @param string $backupcourse
113 public function i_perform_a_quick_backup_of_course($backupcourse) {
114 // We can not use other steps here as we don't know where the provided data
115 // table elements are used, and we need to catch exceptions contantly.
118 $this->getSession()->visit($this->locate_path('/'));
120 // Click the course link.
121 $this->find_link($backupcourse)->click();
123 // Click the backup link.
124 $this->find_link(get_string('backup'))->click();
128 $this->find_button(get_string('performoneclickbackup', 'backup'))->press();
131 // Waiting for it to finish.
132 $this->wait(self::EXTENDED_TIMEOUT);
134 // Last backup continue button.
135 $this->find_button(get_string('backupstage16action', 'backup'))->press();
139 * Imports the specified origin course into the other course using the provided options.
141 * Keeping it separatelly from backup & restore, it the number of
142 * steps and duplicate code becomes bigger a common method should
145 * @Given /^I import "(?P<from_course_fullname_string>(?:[^"]|\\")*)" course into "(?P<to_course_fullname_string>(?:[^"]|\\")*)" course using this options:$/
146 * @param string $fromcourse
147 * @param string $tocourse
148 * @param TableNode $options
150 public function i_import_course_into_course($fromcourse, $tocourse, $options = false) {
152 // We can not use other steps here as we don't know where the provided data
153 // table elements are used, and we need to catch exceptions contantly.
156 $this->getSession()->visit($this->locate_path('/'));
159 // Click the course link.
160 $this->find_link($tocourse)->click();
163 // Click the import link.
164 $this->navigate_to_course_settings_link('import');
167 // Select the course.
168 $exception = new ExpectationException('"' . $fromcourse . '" course not found in the list of courses to import from', $this->getSession());
170 // The argument should be converted to an xpath literal.
171 $fromcourse = $this->getSession()->getSelectorsHandler()->xpathLiteral($fromcourse);
172 $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' ics-results ')]" .
173 "/descendant::tr[contains(., $fromcourse)]" .
174 "/descendant::input[@type='radio']";
175 $radionode = $this->find('xpath', $xpath, $exception);
179 $this->find_button(get_string('continue'))->press();
183 $this->fill_backup_restore_form($this->get_step_options($options, "Initial"));
184 $this->find_button(get_string('importbackupstage1action', 'backup'))->press();
188 $this->fill_backup_restore_form($this->get_step_options($options, "Schema"));
189 $this->find_button(get_string('importbackupstage2action', 'backup'))->press();
193 $this->find_button(get_string('importbackupstage4action', 'backup'))->press();
194 $this->wait(self::EXTENDED_TIMEOUT);
196 // Continue and redirect to 'to' course.
197 $this->find_button(get_string('continue'))->press();
201 * Restores the backup into the specified course and the provided options. You should be in the 'Restore' page where the backup is.
203 * @Given /^I restore "(?P<backup_filename_string>(?:[^"]|\\")*)" backup into "(?P<existing_course_fullname_string>(?:[^"]|\\")*)" course using this options:$/
204 * @param string $backupfilename
205 * @param string $existingcourse
206 * @param TableNode $options Restore forms options or false if no options provided
208 public function i_restore_backup_into_course_using_this_options($backupfilename, $existingcourse, $options = false) {
211 $this->select_backup($backupfilename);
213 // The argument should be converted to an xpath literal.
214 $existingcourse = $this->getSession()->getSelectorsHandler()->xpathLiteral($existingcourse);
216 // Selecting the specified course (we can not call behat_forms::select_radio here as is in another behat subcontext).
217 $radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-existing-course ')]" .
218 "/descendant::div[@class='restore-course-search']" .
219 "/descendant::tr[contains(., $existingcourse)]" .
220 "/descendant::input[@type='radio']");
224 // Pressing the continue button of the restore into an existing course section.
225 $continuenode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-existing-course ')]" .
226 "/descendant::input[@type='submit'][@value='" . get_string('continue') . "']");
227 $continuenode->click();
230 // Common restore process using provided key/value options.
231 $this->process_restore($options);
235 * Restores the specified backup into a new course using the provided options. You should be in the 'Restore' page where the backup is.
237 * @Given /^I restore "(?P<backup_filename_string>(?:[^"]|\\")*)" backup into a new course using this options:$/
238 * @param string $backupfilename
239 * @param TableNode $options Restore forms options or false if no options provided
241 public function i_restore_backup_into_a_new_course_using_this_options($backupfilename, $options = false) {
244 $this->select_backup($backupfilename);
246 // The first category in the list.
247 $radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-new-course ')]" .
248 "/descendant::div[@class='restore-course-search']" .
249 "/descendant::input[@type='radio']");
253 // Pressing the continue button of the restore into an existing course section.
254 $continuenode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' bcs-new-course ')]" .
255 "/descendant::input[@type='submit'][@value='" . get_string('continue') . "']");
256 $continuenode->click();
259 // Common restore process using provided key/value options.
260 $this->process_restore($options);
264 * Merges the backup into the current course using the provided restore options. You should be in the 'Restore' page where the backup is.
266 * @Given /^I merge "(?P<backup_filename_string>(?:[^"]|\\")*)" backup into the current course using this options:$/
267 * @param string $backupfilename
268 * @param TableNode $options Restore forms options or false if no options provided
270 public function i_merge_backup_into_the_current_course($backupfilename, $options = false) {
273 $this->select_backup($backupfilename);
275 // Merge without deleting radio option.
276 $radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), 'bcs-current-course')]" .
277 "/descendant::input[@type='radio'][@name='target'][@value='1']");
281 // Pressing the continue button of the restore merging section.
282 $continuenode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), 'bcs-current-course')]" .
283 "/descendant::input[@type='submit'][@value='" . get_string('continue') . "']");
284 $continuenode->click();
287 // Common restore process using provided key/value options.
288 $this->process_restore($options);
292 * 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.
294 * @Given /^I merge "(?P<backup_filename_string>(?:[^"]|\\")*)" backup into the current course after deleting it's contents using this options:$/
295 * @param string $backupfilename
296 * @param TableNode $options Restore forms options or false if no options provided
298 public function i_merge_backup_into_current_course_deleting_its_contents($backupfilename, $options = false) {
301 $this->select_backup($backupfilename);
303 // Delete contents radio option.
304 $radionode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), 'bcs-current-course')]" .
305 "/descendant::input[@type='radio'][@name='target'][@value='0']");
309 // Pressing the continue button of the restore merging section.
310 $continuenode = $this->find('xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), 'bcs-current-course')]" .
311 "/descendant::input[@type='submit'][@value='" . get_string('continue') . "']");
312 $continuenode->click();
315 // Common restore process using provided key/value options.
316 $this->process_restore($options);
320 * Selects the backup to restore.
322 * @throws ExpectationException
323 * @param string $backupfilename
326 protected function select_backup($backupfilename) {
328 // Using xpath as there are other restore links before this one.
329 $exception = new ExpectationException('The "' . $backupfilename . '" backup file can not be found in this page', $this->getSession());
331 // The argument should be converted to an xpath literal.
332 $backupfilename = $this->getSession()->getSelectorsHandler()->xpathLiteral($backupfilename);
334 $xpath = "//tr[contains(., $backupfilename)]/descendant::a[contains(., '" . get_string('restore') . "')]";
335 $restorelink = $this->find('xpath', $xpath, $exception);
336 $restorelink->click();
338 // Confirm the backup contents.
339 $restore = $this->find_button(get_string('continue'))->press();
343 * Executes the common steps of all restore processes.
345 * @param TableNode $options The backup and restore options or false if no options provided
348 protected function process_restore($options) {
350 // We can not use other steps here as we don't know where the provided data
351 // table elements are used, and we need to catch exceptions contantly.
354 $this->fill_backup_restore_form($this->get_step_options($options, "Settings"));
355 $this->find_button(get_string('restorestage4action', 'backup'))->press();
359 $this->fill_backup_restore_form($this->get_step_options($options, "Schema"));
360 $this->find_button(get_string('restorestage8action', 'backup'))->press();
363 // Review, no options here.
364 $this->find_button(get_string('restorestage16action', 'backup'))->press();
367 // Last restore continue button, redirected to restore course after this.
368 $this->find_button(get_string('restorestage32action', 'backup'))->press();
370 // Long wait when waiting for the restore to finish.
371 $this->wait(self::EXTENDED_TIMEOUT);
375 * Tries to fill the current page form elements with the provided options.
377 * This step is slow as it spins over each provided option, we are
378 * not expected to have lots of provided options, anyways, is better
379 * to be conservative and wait for the elements to appear rather than
380 * to have false failures.
382 * @param TableNode $options The backup and restore options or false if no options provided
385 protected function fill_backup_restore_form($options) {
387 // Nothing to fill if no options are provided.
392 // If we find any of the provided options in the current form we should set the value.
393 $datahash = $options->getRowsHash();
394 foreach ($datahash as $locator => $value) {
395 $field = behat_field_manager::get_form_field_from_label($locator, $this);
396 $field->set_value($value);
401 * Get the options specific to this step of the backup/restore process.
403 * @param TableNode $options The options table to filter
404 * @param string $step The name of the step
405 * @return TableNode The filtered options table
406 * @throws ExpectationException
408 protected function get_step_options($options, $step) {
409 // Nothing to fill if no options are provided.
414 $pageoptions = clone $options;
416 $rows = $options->getRows();
418 foreach ($rows as $k => $data) {
419 if (count($data) !== 3) {
420 // Not enough information to guess the page.
421 throw new ExpectationException("The backup/restore step must be specified for all backup options");
422 } else if ($data[0] == $step) {
427 $pageoptions->setRows($newrows);
433 * Waits until the DOM and the page Javascript code is ready.
435 * @param int $timeout The number of seconds that we wait.
438 protected function wait($timeout = false) {
440 if (!$this->running_javascript()) {
445 $timeout = self::TIMEOUT;
448 $this->getSession()->wait($timeout * 1000, self::PAGE_READY_JS);