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/>.
21 * @copyright 2012 David MonllaĆ³
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Renderer for behat tool web features
31 * @copyright 2012 David MonllaĆ³
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class tool_behat_renderer
extends plugin_renderer_base
{
37 * Renders the list of available steps according to the submitted filters.
39 * @param mixed $stepsdefinitions Available steps array.
40 * @param moodleform $form
41 * @return string HTML code
43 public function render_stepsdefinitions($stepsdefinitions, $form) {
45 require_once($CFG->libdir
. '/behat/classes/behat_selectors.php');
47 $html = $this->output
->header();
48 $html .= $this->output
->heading(get_string('pluginname', 'tool_behat'));
49 $html .= $form->render();
51 if (empty($stepsdefinitions)) {
52 $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
55 $stepsdefinitions = implode('', $stepsdefinitions);
57 // Replace text selector type arguments with a user-friendly select.
58 $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
60 return html_writer
::select(behat_selectors
::get_allowed_text_selectors(), uniqid());
65 // Replace selector type arguments with a user-friendly select.
66 $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
68 return html_writer
::select(behat_selectors
::get_allowed_selectors(), uniqid());
73 // Replace simple OR options.
74 $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
75 $stepsdefinitions = preg_replace_callback($regex,
77 return html_writer
::select(explode('|', $matches[1]), uniqid());
82 $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/',
86 // Creating a link to a popup with the help.
87 $url = new moodle_url(
90 'component' => 'tool_behat',
91 'identifier' => 'fieldvalueargument',
92 'lang' => current_language()
96 // Note: this title is displayed only if JS is disabled,
97 // otherwise the link will have the new ajax tooltip.
98 $title = get_string('fieldvalueargument', 'tool_behat');
99 $title = get_string('helpprefix2', '', trim($title, ". \t"));
101 $attributes = array('href' => $url, 'title' => $title,
102 'aria-haspopup' => 'true', 'target' => '_blank');
104 $output = html_writer
::tag('a', 'FIELD_VALUE_STRING', $attributes);
105 return html_writer
::tag('span', $output, array('class' => 'helptooltip'));
111 // Steps definitions.
112 $html .= html_writer
::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
114 $html .= $this->output
->footer();
120 * Renders an error message adding the generic info about the tool purpose and setup.
122 * @param string $msg The error message
123 * @return string HTML
125 public function render_error($msg) {
127 $html = $this->output
->header();
128 $html .= $this->output
->heading(get_string('pluginname', 'tool_behat'));
129 $html .= $this->generic_info();
133 $a->behatcommand
= behat_command
::get_behat_command();
134 $a->behatinit
= 'php admin' . DIRECTORY_SEPARATOR
. 'tool' . DIRECTORY_SEPARATOR
.
135 'behat' . DIRECTORY_SEPARATOR
. 'cli' . DIRECTORY_SEPARATOR
. 'init.php';
137 $msg = get_string('wrongbehatsetup', 'tool_behat', $a);
139 // Error box including generic error string + specific error msg.
140 $html .= $this->output
->box_start('box errorbox alert alert-danger');
141 $html .= html_writer
::tag('div', $msg);
142 $html .= $this->output
->box_end();
144 $html .= $this->output
->footer();
150 * Generic info about the tool.
154 public function generic_info() {
157 $installurl = behat_command
::DOCS_URL
;
158 $installlink = html_writer
::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
159 $writetestsurl = 'https://docs.moodle.org/dev/Writing acceptance tests';
160 $writetestslink = html_writer
::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
161 $writestepsurl = 'https://docs.moodle.org/dev/Writing_new_acceptance_test_step_definitions';
162 $writestepslink = html_writer
::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
164 get_string('installinfo', 'tool_behat', $installlink),
165 get_string('newtestsinfo', 'tool_behat', $writetestslink),
166 get_string('newstepsinfo', 'tool_behat', $writestepslink)
170 $html = $this->output
->box_start();
171 $html .= html_writer
::tag('div', get_string('aim', 'tool_behat'));
172 $html .= html_writer
::start_tag('div');
173 $html .= html_writer
::start_tag('ul');
174 $html .= html_writer
::start_tag('li');
175 $html .= implode(html_writer
::end_tag('li') . html_writer
::start_tag('li'), $infos);
176 $html .= html_writer
::end_tag('li');
177 $html .= html_writer
::end_tag('ul');
178 $html .= html_writer
::end_tag('div');
179 $html .= $this->output
->box_end();