MDL-43738 behat: Updating features files
[moodle.git] / admin / tool / behat / renderer.php
CommitLineData
d46340eb
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 * Behat tool renderer
19 *
20 * @package tool_behat
21 * @copyright 2012 David MonllaĆ³
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
096858ed
DM
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
17344d4c 28require_once($CFG->libdir . '/behat/classes/behat_selectors.php');
d46340eb
DM
29
30/**
31 * Renderer for behat tool web features
32 *
33 * @package tool_behat
34 * @copyright 2012 David MonllaĆ³
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_behat_renderer extends plugin_renderer_base {
38
39 /**
5f470b28 40 * Renders the list of available steps according to the submitted filters.
d46340eb 41 *
5f470b28 42 * @param mixed $stepsdefinitions Available steps array.
d46340eb
DM
43 * @param moodleform $form
44 * @return string HTML code
45 */
46 public function render_stepsdefinitions($stepsdefinitions, $form) {
47
b8c4d91c 48 $html = $this->generic_info();
d46340eb
DM
49
50 // Form.
51 ob_start();
52 $form->display();
53 $html .= ob_get_contents();
54 ob_end_clean();
55
5f470b28
DM
56 if (empty($stepsdefinitions)) {
57 $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
58 } else {
59
40923977
DM
60 $stepsdefinitions = implode('', $stepsdefinitions);
61
5f470b28 62 // Replace text selector type arguments with a user-friendly select.
28435f1a 63 $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
5f470b28 64 function ($matches) {
17344d4c 65 return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid());
5f470b28
DM
66 },
67 $stepsdefinitions
68 );
69
70 // Replace selector type arguments with a user-friendly select.
28435f1a 71 $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
5f470b28 72 function ($matches) {
17344d4c 73 return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid());
5f470b28
DM
74 },
75 $stepsdefinitions
76 );
77
5a26fb20
SH
78 // Replace simple OR options.
79 $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
80 $stepsdefinitions = preg_replace_callback($regex,
81 function($matches){
82 return html_writer::select(explode('|', $matches[1]), uniqid());
83 },
84 $stepsdefinitions
85 );
86
5f470b28
DM
87 }
88
d46340eb 89 // Steps definitions.
096858ed 90 $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
d46340eb
DM
91
92 $html .= $this->output->footer();
93
94 return $html;
95 }
b8c4d91c
DM
96
97 /**
98 * Renders an error message adding the generic info about the tool purpose and setup.
99 *
100 * @param string $msg The error message
101 * @return string HTML
102 */
103 public function render_error($msg) {
104
105 $html = $this->generic_info();
106
107 $a = new stdClass();
108 $a->errormsg = $msg;
109 $a->behatcommand = behat_command::get_behat_command();
110 $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR .
111 'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php';
112
113 $msg = get_string('wrongbehatsetup', 'tool_behat', $a);
114
115 // Error box including generic error string + specific error msg.
116 $html .= $this->output->box_start('box errorbox');
117 $html .= html_writer::tag('div', $msg);
118 $html .= $this->output->box_end();
119
120 $html .= $this->output->footer();
121
122 return $html;
123 }
124
125 /**
126 * Generic info about the tool.
127 *
128 * @return string
129 */
130 protected function generic_info() {
131
132 $title = get_string('pluginname', 'tool_behat');
133
134 // Header.
135 $html = $this->output->header();
136 $html .= $this->output->heading($title);
137
138 // Info.
139 $installurl = behat_command::DOCS_URL . '#Installation';
140 $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
141 $writetestsurl = behat_command::DOCS_URL . '#Writting_features';
142 $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
143 $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions';
144 $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
145 $infos = array(
146 get_string('installinfo', 'tool_behat', $installlink),
147 get_string('newtestsinfo', 'tool_behat', $writetestslink),
148 get_string('newstepsinfo', 'tool_behat', $writestepslink)
149 );
150
151 // List of steps.
152 $html .= $this->output->box_start();
153 $html .= html_writer::tag('h1', get_string('infoheading', 'tool_behat'));
154 $html .= html_writer::tag('div', get_string('aim', 'tool_behat'));
155 $html .= html_writer::empty_tag('div');
156 $html .= html_writer::empty_tag('ul');
157 $html .= html_writer::empty_tag('li');
158 $html .= implode(html_writer::end_tag('li') . html_writer::empty_tag('li'), $infos);
159 $html .= html_writer::end_tag('li');
160 $html .= html_writer::end_tag('ul');
161 $html .= html_writer::end_tag('div');
162 $html .= $this->output->box_end();
163
164 return $html;
165 }
166
d46340eb 167}