weekly release 2.7dev
[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
48 $title = get_string('pluginname', 'tool_behat');
49
50 // Header.
51 $html = $this->output->header();
52 $html .= $this->output->heading($title);
53
54 // Info.
096858ed 55 $installurl = behat_command::DOCS_URL . '#Installation';
d46340eb 56 $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
096858ed 57 $writetestsurl = behat_command::DOCS_URL . '#Writting_features';
d46340eb 58 $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
096858ed 59 $writestepsurl = behat_command::DOCS_URL . '#Adding_steps_definitions';
d46340eb
DM
60 $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
61 $infos = array(
95167121
DM
62 get_string('installinfo', 'tool_behat', $installlink),
63 get_string('newtestsinfo', 'tool_behat', $writetestslink),
64 get_string('newstepsinfo', 'tool_behat', $writestepslink)
d46340eb 65 );
096858ed 66
cb9c703c 67 // List of steps.
d46340eb 68 $html .= $this->output->box_start();
cb9c703c
DM
69 $html .= html_writer::tag('h1', get_string('infoheading', 'tool_behat'));
70 $html .= html_writer::tag('div', get_string('aim', 'tool_behat'));
096858ed
DM
71 $html .= html_writer::empty_tag('div');
72 $html .= html_writer::empty_tag('ul');
73 $html .= html_writer::empty_tag('li');
74 $html .= implode(html_writer::end_tag('li') . html_writer::empty_tag('li'), $infos);
75 $html .= html_writer::end_tag('li');
76 $html .= html_writer::end_tag('ul');
77 $html .= html_writer::end_tag('div');
d46340eb
DM
78 $html .= $this->output->box_end();
79
80 // Form.
81 ob_start();
82 $form->display();
83 $html .= ob_get_contents();
84 ob_end_clean();
85
5f470b28
DM
86 if (empty($stepsdefinitions)) {
87 $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
88 } else {
89
40923977
DM
90 $stepsdefinitions = implode('', $stepsdefinitions);
91
5f470b28 92 // Replace text selector type arguments with a user-friendly select.
28435f1a 93 $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
5f470b28 94 function ($matches) {
17344d4c 95 return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid());
5f470b28
DM
96 },
97 $stepsdefinitions
98 );
99
100 // Replace selector type arguments with a user-friendly select.
28435f1a 101 $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
5f470b28 102 function ($matches) {
17344d4c 103 return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid());
5f470b28
DM
104 },
105 $stepsdefinitions
106 );
107
5a26fb20
SH
108 // Replace simple OR options.
109 $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
110 $stepsdefinitions = preg_replace_callback($regex,
111 function($matches){
112 return html_writer::select(explode('|', $matches[1]), uniqid());
113 },
114 $stepsdefinitions
115 );
116
5f470b28
DM
117 }
118
d46340eb 119 // Steps definitions.
096858ed 120 $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
d46340eb
DM
121
122 $html .= $this->output->footer();
123
124 return $html;
125 }
126}