. /** * Contains the class responsible for step definitions related to mod_customcert. * * @package mod_customcert * @category test * @copyright 2017 Mark Nelson * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. require_once(__DIR__ . '/../../../../lib/behat/behat_base.php'); /** * The class responsible for step definitions related to mod_customcert. * * @package mod_customcert * @category test * @copyright 2017 Mark Nelson * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class behat_mod_customcert extends behat_base { /** * Adds an element to the specified page of a template. * * @codingStandardsIgnoreLine * @Given /^I add the element "(?P(?:[^"]|\\")*)" to page "(?P\d+)" of the "(?P(?:[^"]|\\")*)" certificate template$/ * @param string $elementname * @param int $pagenum * @param string $templatename */ public function i_add_the_element_to_the_certificate_template_page($elementname, $pagenum, $templatename) { global $DB; $template = $DB->get_record('customcert_templates', array('name' => $templatename), '*', MUST_EXIST); $page = $DB->get_record('customcert_pages', array('templateid' => $template->id, 'sequence' => $pagenum), '*', MUST_EXIST); $this->execute('behat_forms::i_set_the_field_to', array($this->escape('element_' . $page->id), $this->escape($elementname))); $this->execute('behat_forms::press_button', get_string('addelement', 'customcert')); } /** * Deletes an element from a specified page of a template. * * @Given /^I delete page "(?P\d+)" of the "(?P(?:[^"]|\\")*)" certificate template$/ * @param int $pagenum * @param string $templatename */ public function i_delete_the_certificate_page($pagenum, $templatename) { global $DB; $template = $DB->get_record('customcert_templates', array('name' => $templatename), '*', MUST_EXIST); $page = $DB->get_record('customcert_pages', array('templateid' => $template->id, 'sequence' => $pagenum), '*', MUST_EXIST); $this->execute('behat_general::i_click_on_in_the', array('Delete page', 'link', $this->escape('#id_page_' . $page->id), 'css_element')); $this->execute('behat_forms::press_button', get_string('continue')); } /** * Verifies the certificate code for a user. * * @Given /^I verify the "(?P(?:[^"]|\\")*)" certificate for the user "(?P(?:[^"]|\\")*)"$/ * @param string $certificatename * @param string $username */ public function i_verify_the_custom_certificate_for_user($certificatename, $username) { global $DB; $certificate = $DB->get_record('customcert', array('name' => $certificatename), '*', MUST_EXIST); $user = $DB->get_record('user', array('username' => $username), '*', MUST_EXIST); $issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $certificate->id), '*', MUST_EXIST); $this->execute('behat_forms::i_set_the_field_to', array(get_string('code', 'customcert'), $issue->code)); $this->execute('behat_forms::press_button', get_string('verify', 'customcert')); $this->execute('behat_general::assert_page_contains_text', get_string('verified', 'customcert')); $this->execute('behat_general::assert_page_not_contains_text', get_string('notverified', 'customcert')); } /** * Verifies the certificate code for a user. * * @Given /^I can not verify the "(?P(?:[^"]|\\")*)" certificate for the user "(?P(?:[^"]|\\")*)"$/ * @param string $certificatename * @param string $username */ public function i_can_not_verify_the_custom_certificate_for_user($certificatename, $username) { global $DB; $certificate = $DB->get_record('customcert', array('name' => $certificatename), '*', MUST_EXIST); $user = $DB->get_record('user', array('username' => $username), '*', MUST_EXIST); $issue = $DB->get_record('customcert_issues', array('userid' => $user->id, 'customcertid' => $certificate->id), '*', MUST_EXIST); $this->execute('behat_forms::i_set_the_field_to', array(get_string('code', 'customcert'), $issue->code)); $this->execute('behat_forms::press_button', get_string('verify', 'customcert')); $this->execute('behat_general::assert_page_contains_text', get_string('notverified', 'customcert')); $this->execute('behat_general::assert_page_not_contains_text', get_string('verified', 'customcert')); } /** * Directs the user to the URL for verifying a certificate. * * This has been created as we allow non-users to verify certificates and they can not navigate to * the page like a conventional user. * * @Given /^I visit the verification url for the "(?P(?:[^"]|\\")*)" certificate$/ * @param string $certificatename */ public function i_visit_the_verification_url_for_custom_certificate($certificatename) { global $DB; $certificate = $DB->get_record('customcert', array('name' => $certificatename), '*', MUST_EXIST); $template = $DB->get_record('customcert_templates', array('id' => $certificate->templateid), '*', MUST_EXIST); $url = new moodle_url('/mod/customcert/verify_certificate.php', array('contextid' => $template->contextid)); $this->getSession()->visit($this->locate_path($url->out_as_local_url())); } /** * Directs the user to the URL for verifying all certificates on the site. * * @Given /^I visit the verification url for the site$/ */ public function i_visit_the_verification_url_for_the_site() { $url = new moodle_url('/mod/customcert/verify_certificate.php'); $this->getSession()->visit($this->locate_path($url->out_as_local_url())); } }