1 |
efrain |
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 |
* Contains the class responsible for step definitions related to mod_customcert.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2017 Mark Nelson <markn@moodle.com>
|
|
|
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 |
/**
|
|
|
31 |
* The class responsible for step definitions related to mod_customcert.
|
|
|
32 |
*
|
|
|
33 |
* @package mod_customcert
|
|
|
34 |
* @category test
|
|
|
35 |
* @copyright 2017 Mark Nelson <markn@moodle.com>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class behat_mod_customcert extends behat_base {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Adds an element to the specified page of a template.
|
|
|
42 |
*
|
|
|
43 |
* phpcs:ignore
|
|
|
44 |
* @Given /^I add the element "(?P<element_name>(?:[^"]|\\")*)" to page "(?P<page_number>\d+)" of the "(?P<template_name>(?:[^"]|\\")*)" certificate template$/
|
|
|
45 |
* @param string $elementname
|
|
|
46 |
* @param int $pagenum
|
|
|
47 |
* @param string $templatename
|
|
|
48 |
*/
|
|
|
49 |
public function i_add_the_element_to_the_certificate_template_page($elementname, $pagenum, $templatename) {
|
|
|
50 |
global $DB;
|
|
|
51 |
|
|
|
52 |
$template = $DB->get_record('customcert_templates', ['name' => $templatename], '*', MUST_EXIST);
|
|
|
53 |
$page = $DB->get_record('customcert_pages', ['templateid' => $template->id, 'sequence' => $pagenum],
|
|
|
54 |
'*', MUST_EXIST);
|
|
|
55 |
|
|
|
56 |
$this->execute('behat_forms::i_set_the_field_to', [$this->escape('element_' . $page->id),
|
|
|
57 |
$this->escape($elementname)]);
|
|
|
58 |
$this->execute('behat_forms::press_button', get_string('addelement', 'customcert'));
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Deletes an element from a specified page of a template.
|
|
|
63 |
*
|
|
|
64 |
* @Given /^I delete page "(?P<page_number>\d+)" of the "(?P<template_name>(?:[^"]|\\")*)" certificate template$/
|
|
|
65 |
* @param int $pagenum
|
|
|
66 |
* @param string $templatename
|
|
|
67 |
*/
|
|
|
68 |
public function i_delete_the_certificate_page($pagenum, $templatename) {
|
|
|
69 |
global $DB;
|
|
|
70 |
|
|
|
71 |
$template = $DB->get_record('customcert_templates', ['name' => $templatename], '*', MUST_EXIST);
|
|
|
72 |
$page = $DB->get_record('customcert_pages', ['templateid' => $template->id, 'sequence' => $pagenum],
|
|
|
73 |
'*', MUST_EXIST);
|
|
|
74 |
|
|
|
75 |
$this->execute('behat_general::i_click_on_in_the', ['Delete page', 'link',
|
|
|
76 |
$this->escape('#id_page_' . $page->id), 'css_element']);
|
|
|
77 |
$this->execute('behat_forms::press_button', get_string('continue'));
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Verifies the certificate code for a user.
|
|
|
82 |
*
|
|
|
83 |
* @Given /^I verify the "(?P<certificate_name>(?:[^"]|\\")*)" certificate for the user "(?P<user_name>(?:[^"]|\\")*)"$/
|
|
|
84 |
* @param string $certificatename
|
|
|
85 |
* @param string $username
|
|
|
86 |
*/
|
|
|
87 |
public function i_verify_the_custom_certificate_for_user($certificatename, $username) {
|
|
|
88 |
global $DB;
|
|
|
89 |
|
|
|
90 |
$certificate = $DB->get_record('customcert', ['name' => $certificatename], '*', MUST_EXIST);
|
|
|
91 |
$user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST);
|
|
|
92 |
$issue = $DB->get_record('customcert_issues', ['userid' => $user->id, 'customcertid' => $certificate->id],
|
|
|
93 |
'*', MUST_EXIST);
|
|
|
94 |
|
|
|
95 |
$this->execute('behat_forms::i_set_the_field_to', [get_string('code', 'customcert'), $issue->code]);
|
|
|
96 |
$this->execute('behat_forms::press_button', get_string('verify', 'customcert'));
|
|
|
97 |
$this->execute('behat_general::assert_page_contains_text', get_string('verified', 'customcert'));
|
|
|
98 |
$this->execute('behat_general::assert_page_not_contains_text', get_string('notverified', 'customcert'));
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Verifies the certificate code for a user.
|
|
|
103 |
*
|
|
|
104 |
* @Given /^I can not verify the "(?P<certificate_name>(?:[^"]|\\")*)" certificate for the user "(?P<user_name>(?:[^"]|\\")*)"$/
|
|
|
105 |
* @param string $certificatename
|
|
|
106 |
* @param string $username
|
|
|
107 |
*/
|
|
|
108 |
public function i_can_not_verify_the_custom_certificate_for_user($certificatename, $username) {
|
|
|
109 |
global $DB;
|
|
|
110 |
|
|
|
111 |
$certificate = $DB->get_record('customcert', ['name' => $certificatename], '*', MUST_EXIST);
|
|
|
112 |
$user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST);
|
|
|
113 |
$issue = $DB->get_record('customcert_issues', ['userid' => $user->id, 'customcertid' => $certificate->id],
|
|
|
114 |
'*', MUST_EXIST);
|
|
|
115 |
|
|
|
116 |
$this->execute('behat_forms::i_set_the_field_to', [get_string('code', 'customcert'), $issue->code]);
|
|
|
117 |
$this->execute('behat_forms::press_button', get_string('verify', 'customcert'));
|
|
|
118 |
$this->execute('behat_general::assert_page_contains_text', get_string('notverified', 'customcert'));
|
|
|
119 |
$this->execute('behat_general::assert_page_not_contains_text', get_string('verified', 'customcert'));
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Directs the user to the URL for verifying a certificate.
|
|
|
124 |
*
|
|
|
125 |
* This has been created as we allow non-users to verify certificates and they can not navigate to
|
|
|
126 |
* the page like a conventional user.
|
|
|
127 |
*
|
|
|
128 |
* @Given /^I visit the verification url for the "(?P<certificate_name>(?:[^"]|\\")*)" certificate$/
|
|
|
129 |
* @param string $certificatename
|
|
|
130 |
*/
|
|
|
131 |
public function i_visit_the_verification_url_for_custom_certificate($certificatename) {
|
|
|
132 |
global $DB;
|
|
|
133 |
|
|
|
134 |
$certificate = $DB->get_record('customcert', ['name' => $certificatename], '*', MUST_EXIST);
|
|
|
135 |
$template = $DB->get_record('customcert_templates', ['id' => $certificate->templateid], '*', MUST_EXIST);
|
|
|
136 |
|
|
|
137 |
$url = new moodle_url('/mod/customcert/verify_certificate.php', ['contextid' => $template->contextid]);
|
|
|
138 |
$this->getSession()->visit($this->locate_path($url->out_as_local_url()));
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Directs the user to the URL for verifying all certificates on the site.
|
|
|
143 |
*
|
|
|
144 |
* @Given /^I visit the verification url for the site$/
|
|
|
145 |
*/
|
|
|
146 |
public function i_visit_the_verification_url_for_the_site() {
|
|
|
147 |
$url = new moodle_url('/mod/customcert/verify_certificate.php');
|
|
|
148 |
$this->getSession()->visit($this->locate_path($url->out_as_local_url()));
|
|
|
149 |
}
|
|
|
150 |
}
|