MDL-59535 tool_behat: fix fatal error if there is no setup
[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
d46340eb
DM
27/**
28 * Renderer for behat tool web features
29 *
30 * @package tool_behat
31 * @copyright 2012 David MonllaĆ³
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class tool_behat_renderer extends plugin_renderer_base {
35
36 /**
5f470b28 37 * Renders the list of available steps according to the submitted filters.
d46340eb 38 *
5f470b28 39 * @param mixed $stepsdefinitions Available steps array.
d46340eb
DM
40 * @param moodleform $form
41 * @return string HTML code
42 */
43 public function render_stepsdefinitions($stepsdefinitions, $form) {
026bfb76
SL
44 global $CFG;
45 require_once($CFG->libdir . '/behat/classes/behat_selectors.php');
d46340eb 46
b8c4d91c 47 $html = $this->generic_info();
d46340eb
DM
48
49 // Form.
50 ob_start();
51 $form->display();
52 $html .= ob_get_contents();
53 ob_end_clean();
54
5f470b28
DM
55 if (empty($stepsdefinitions)) {
56 $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
57 } else {
58
40923977
DM
59 $stepsdefinitions = implode('', $stepsdefinitions);
60
5f470b28 61 // Replace text selector type arguments with a user-friendly select.
28435f1a 62 $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
5f470b28 63 function ($matches) {
17344d4c 64 return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid());
5f470b28
DM
65 },
66 $stepsdefinitions
67 );
68
69 // Replace selector type arguments with a user-friendly select.
28435f1a 70 $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
5f470b28 71 function ($matches) {
17344d4c 72 return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid());
5f470b28
DM
73 },
74 $stepsdefinitions
75 );
76
5a26fb20
SH
77 // Replace simple OR options.
78 $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
79 $stepsdefinitions = preg_replace_callback($regex,
80 function($matches){
81 return html_writer::select(explode('|', $matches[1]), uniqid());
82 },
83 $stepsdefinitions
84 );
85
decf1e14
DM
86 $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/',
87 function ($matches) {
88 global $CFG;
89
90 // Creating a link to a popup with the help.
91 $url = new moodle_url(
92 '/help.php',
93 array(
94 'component' => 'tool_behat',
95 'identifier' => 'fieldvalueargument',
96 'lang' => current_language()
97 )
98 );
99
100 // Note: this title is displayed only if JS is disabled,
101 // otherwise the link will have the new ajax tooltip.
102 $title = get_string('fieldvalueargument', 'tool_behat');
103 $title = get_string('helpprefix2', '', trim($title, ". \t"));
104
105 $attributes = array('href' => $url, 'title' => $title,
106 'aria-haspopup' => 'true', 'target' => '_blank');
107
108 $output = html_writer::tag('a', 'FIELD_VALUE_STRING', $attributes);
109 return html_writer::tag('span', $output, array('class' => 'helptooltip'));
110 },
111 $stepsdefinitions
112 );
5f470b28
DM
113 }
114
d46340eb 115 // Steps definitions.
096858ed 116 $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
d46340eb
DM
117
118 $html .= $this->output->footer();
119
120 return $html;
121 }
b8c4d91c
DM
122
123 /**
124 * Renders an error message adding the generic info about the tool purpose and setup.
125 *
126 * @param string $msg The error message
127 * @return string HTML
128 */
129 public function render_error($msg) {
130
131 $html = $this->generic_info();
132
133 $a = new stdClass();
134 $a->errormsg = $msg;
135 $a->behatcommand = behat_command::get_behat_command();
136 $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR .
137 'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php';
138
139 $msg = get_string('wrongbehatsetup', 'tool_behat', $a);
140
141 // Error box including generic error string + specific error msg.
142 $html .= $this->output->box_start('box errorbox');
143 $html .= html_writer::tag('div', $msg);
144 $html .= $this->output->box_end();
145
146 $html .= $this->output->footer();
147
148 return $html;
149 }
150
151 /**
152 * Generic info about the tool.
153 *
154 * @return string
155 */
156 protected function generic_info() {
157
158 $title = get_string('pluginname', 'tool_behat');
159
160 // Header.
161 $html = $this->output->header();
162 $html .= $this->output->heading($title);
163
164 // Info.
165 $installurl = behat_command::DOCS_URL . '#Installation';
166 $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
0f523b2b 167 $writetestsurl = behat_command::DOCS_URL . '#Writing_features';
b8c4d91c
DM
168 $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
169 $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions';
170 $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
171 $infos = array(
172 get_string('installinfo', 'tool_behat', $installlink),
173 get_string('newtestsinfo', 'tool_behat', $writetestslink),
174 get_string('newstepsinfo', 'tool_behat', $writestepslink)
175 );
176
177 // List of steps.
178 $html .= $this->output->box_start();
caf02d3d 179 $html .= html_writer::tag('h3', get_string('infoheading', 'tool_behat'));
b8c4d91c 180 $html .= html_writer::tag('div', get_string('aim', 'tool_behat'));
caf02d3d
DM
181 $html .= html_writer::start_tag('div');
182 $html .= html_writer::start_tag('ul');
183 $html .= html_writer::start_tag('li');
184 $html .= implode(html_writer::end_tag('li') . html_writer::start_tag('li'), $infos);
b8c4d91c
DM
185 $html .= html_writer::end_tag('li');
186 $html .= html_writer::end_tag('ul');
187 $html .= html_writer::end_tag('div');
188 $html .= $this->output->box_end();
189
190 return $html;
191 }
192
d46340eb 193}