|
| 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 | + * Steps definitions related to mod_pdfannotator. |
| 19 | + * |
| 20 | + * @package mod_pdfannotator |
| 21 | + * @category test |
| 22 | + * @copyright 2019 HSR (http://www.hsr.ch) |
| 23 | + * @author 2019 Huong Nguyen <[email protected]> |
| 24 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 25 | + */ |
| 26 | + |
| 27 | +require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); |
| 28 | + |
| 29 | +use mod_pdfannotator\utils; |
| 30 | +use Behat\Mink\Exception\ExpectationException as ExpectationException; |
| 31 | +use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; |
| 32 | +use Behat\Gherkin\Node\TableNode as TableNode; |
| 33 | + |
| 34 | +/** |
| 35 | + * Steps definitions related to mod_pdfannotator. |
| 36 | + * |
| 37 | + * @package pdfannotator |
| 38 | + * @category test |
| 39 | + * @copyright 2024 Luca Bösch <[email protected]> |
| 40 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 41 | + */ |
| 42 | +class behat_mod_pdfannotator extends behat_base { |
| 43 | + |
| 44 | + /** |
| 45 | + * Convert page names to URLs for steps like 'When I am on the "[page name]" page'. |
| 46 | + * |
| 47 | + * Recognised page names are: |
| 48 | + * | None so far! | | |
| 49 | + * |
| 50 | + * @param string $page name of the page, with the component name removed e.g. 'Admin notification'. |
| 51 | + * @return moodle_url the corresponding URL. |
| 52 | + * @throws Exception with a meaningful error message if the specified page cannot be found. |
| 53 | + */ |
| 54 | + protected function resolve_page_url(string $page): moodle_url { |
| 55 | + switch ($page) { |
| 56 | + default: |
| 57 | + throw new Exception('Unrecognised pdfannotator page type "' . $page . '."'); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'. |
| 63 | + * |
| 64 | + * Recognised page names are: |
| 65 | + * | pagetype | name meaning | description | |
| 66 | + * | View | Student Quiz name | The student quiz info page (view.php) | |
| 67 | + * | Edit | Student Quiz name | The edit quiz page (edit.php) | |
| 68 | + * | Statistics | Student Quiz name | The Statistics report page | |
| 69 | + * | Ranking | Student Quiz name | The Ranking page | |
| 70 | + * |
| 71 | + * @param string $type identifies which type of page this is, e.g. 'View'. |
| 72 | + * @param string $identifier identifies the particular page, e.g. 'Test student quiz'. |
| 73 | + * @return moodle_url the corresponding URL. |
| 74 | + * @throws Exception with a meaningful error message if the specified page cannot be found. |
| 75 | + */ |
| 76 | + protected function resolve_page_instance_url(string $type, string $identifier): moodle_url { |
| 77 | + switch ($type) { |
| 78 | + case 'View': |
| 79 | + return new moodle_url('/mod/pdfannotator/view.php', |
| 80 | + ['id' => $this->get_cm_by_pdfannotator_name($identifier)->id]); |
| 81 | + |
| 82 | + case 'Edit': |
| 83 | + return new moodle_url('/course/modedit.php', |
| 84 | + ['update' => $this->get_cm_by_pdfannotator_name($identifier)->id]); |
| 85 | + |
| 86 | + case 'Statistics': |
| 87 | + return new moodle_url('/mod/pdfannotator/reportstat.php', |
| 88 | + ['id' => $this->get_cm_by_pdfannotator_name($identifier)->id]); |
| 89 | + |
| 90 | + case 'Ranking': |
| 91 | + return new moodle_url('/mod/pdfannotator/reportrank.php', |
| 92 | + ['id' => $this->get_cm_by_pdfannotator_name($identifier)->id]); |
| 93 | + |
| 94 | + default: |
| 95 | + throw new Exception('Unrecognised pdfannotator page type "' . $type . '."'); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get a pdfannotator by name. |
| 101 | + * |
| 102 | + * @param string $name pdfannotator name. |
| 103 | + * @return stdClass the corresponding DB row. |
| 104 | + */ |
| 105 | + protected function get_pdfannotator_by_name(string $name): stdClass { |
| 106 | + global $DB; |
| 107 | + return $DB->get_record('pdfannotator', array('name' => $name), '*', MUST_EXIST); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get cmid from the pdfannotator name. |
| 112 | + * |
| 113 | + * @param string $name pdfannotator name. |
| 114 | + * @return stdClass cm from get_coursemodule_from_instance. |
| 115 | + */ |
| 116 | + protected function get_cm_by_pdfannotator_name(string $name): stdClass { |
| 117 | + $pdfannotator = $this->get_pdfannotator_by_name($name); |
| 118 | + return get_coursemodule_from_instance('pdfannotator', $pdfannotator->id, $pdfannotator->course); |
| 119 | + } |
| 120 | +} |
0 commit comments