MDL-43738 behat: More info about how to get/set fields
[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
decf1e14
DM
87 $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/',
88 function ($matches) {
89 global $CFG;
90
91 // Creating a link to a popup with the help.
92 $url = new moodle_url(
93 '/help.php',
94 array(
95 'component' => 'tool_behat',
96 'identifier' => 'fieldvalueargument',
97 'lang' => current_language()
98 )
99 );
100
101 // Note: this title is displayed only if JS is disabled,
102 // otherwise the link will have the new ajax tooltip.
103 $title = get_string('fieldvalueargument', 'tool_behat');
104 $title = get_string('helpprefix2', '', trim($title, ". \t"));
105
106 $attributes = array('href' => $url, 'title' => $title,
107 'aria-haspopup' => 'true', 'target' => '_blank');
108
109 $output = html_writer::tag('a', 'FIELD_VALUE_STRING', $attributes);
110 return html_writer::tag('span', $output, array('class' => 'helptooltip'));
111 },
112 $stepsdefinitions
113 );
5f470b28
DM
114 }
115
d46340eb 116 // Steps definitions.
096858ed 117 $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
d46340eb
DM
118
119 $html .= $this->output->footer();
120
121 return $html;
122 }
b8c4d91c
DM
123
124 /**
125 * Renders an error message adding the generic info about the tool purpose and setup.
126 *
127 * @param string $msg The error message
128 * @return string HTML
129 */
130 public function render_error($msg) {
131
132 $html = $this->generic_info();
133
134 $a = new stdClass();
135 $a->errormsg = $msg;
136 $a->behatcommand = behat_command::get_behat_command();
137 $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR .
138 'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php';
139
140 $msg = get_string('wrongbehatsetup', 'tool_behat', $a);
141
142 // Error box including generic error string + specific error msg.
143 $html .= $this->output->box_start('box errorbox');
144 $html .= html_writer::tag('div', $msg);
145 $html .= $this->output->box_end();
146
147 $html .= $this->output->footer();
148
149 return $html;
150 }
151
152 /**
153 * Generic info about the tool.
154 *
155 * @return string
156 */
157 protected function generic_info() {
158
159 $title = get_string('pluginname', 'tool_behat');
160
161 // Header.
162 $html = $this->output->header();
163 $html .= $this->output->heading($title);
164
165 // Info.
166 $installurl = behat_command::DOCS_URL . '#Installation';
167 $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
168 $writetestsurl = behat_command::DOCS_URL . '#Writting_features';
169 $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
170 $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions';
171 $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
172 $infos = array(
173 get_string('installinfo', 'tool_behat', $installlink),
174 get_string('newtestsinfo', 'tool_behat', $writetestslink),
175 get_string('newstepsinfo', 'tool_behat', $writestepslink)
176 );
177
178 // List of steps.
179 $html .= $this->output->box_start();
180 $html .= html_writer::tag('h1', get_string('infoheading', 'tool_behat'));
181 $html .= html_writer::tag('div', get_string('aim', 'tool_behat'));
182 $html .= html_writer::empty_tag('div');
183 $html .= html_writer::empty_tag('ul');
184 $html .= html_writer::empty_tag('li');
185 $html .= implode(html_writer::end_tag('li') . html_writer::empty_tag('li'), $infos);
186 $html .= html_writer::end_tag('li');
187 $html .= html_writer::end_tag('ul');
188 $html .= html_writer::end_tag('div');
189 $html .= $this->output->box_end();
190
191 return $html;
192 }
193
d46340eb 194}