1 |
efrain |
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 arguments transformations.
|
|
|
19 |
*
|
|
|
20 |
* This methods are used by Behat CLI command.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2012 David Monllaó
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
|
|
|
29 |
|
|
|
30 |
require_once(__DIR__ . '/../../behat/behat_base.php');
|
|
|
31 |
|
|
|
32 |
use Behat\Gherkin\Node\TableNode;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Transformations to apply to steps arguments.
|
|
|
36 |
*
|
|
|
37 |
* This methods are applied to the steps arguments that matches
|
|
|
38 |
* the regular expressions specified in the @Transform tag.
|
|
|
39 |
*
|
|
|
40 |
* @package core
|
|
|
41 |
* @category test
|
|
|
42 |
* @copyright 2013 David Monllaó
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
*/
|
|
|
45 |
class behat_transformations extends behat_base {
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* @deprecated since Moodle 3.2
|
|
|
49 |
*/
|
|
|
50 |
public function prefixed_tablenode_transformations() {
|
|
|
51 |
throw new coding_exception('prefixed_tablenode_transformations() can not be used anymore. ' .
|
|
|
52 |
'Please use tablenode_transformations() instead.');
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Removes escaped argument delimiters.
|
|
|
57 |
*
|
|
|
58 |
* We use double quotes as arguments delimiters and
|
|
|
59 |
* to add the " as part of an argument we escape it
|
|
|
60 |
* with a backslash, this method removes this backslash.
|
|
|
61 |
*
|
|
|
62 |
* @Transform /^((.*)"(.*))$/
|
|
|
63 |
* @param string $string
|
|
|
64 |
* @return string The string with the arguments fixed.
|
|
|
65 |
*/
|
|
|
66 |
public function arg_replace_slashes($string) {
|
|
|
67 |
if (!is_scalar($string)) {
|
|
|
68 |
return $string;
|
|
|
69 |
}
|
|
|
70 |
return str_replace('\"', '"', $string);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Replaces $NASTYSTRING vars for a nasty string.
|
|
|
75 |
*
|
|
|
76 |
* @Transform /^((.*)\$NASTYSTRING(\d)(.*))$/
|
|
|
77 |
* @param string $argument The whole argument value.
|
|
|
78 |
* @return string
|
|
|
79 |
*/
|
|
|
80 |
public function arg_replace_nasty_strings($argument) {
|
|
|
81 |
if (!is_scalar($argument)) {
|
|
|
82 |
return $argument;
|
|
|
83 |
}
|
|
|
84 |
return $this->replace_nasty_strings($argument);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Convert string time to timestamp.
|
|
|
89 |
* Use ::time::STRING_TIME_TO_CONVERT::DATE_FORMAT::
|
|
|
90 |
*
|
|
|
91 |
* @Transform /^##(.*)##$/
|
|
|
92 |
* @param string $time
|
|
|
93 |
* @return int timestamp.
|
|
|
94 |
*/
|
|
|
95 |
public function arg_time_to_string($time) {
|
|
|
96 |
return $this->get_transformed_timestamp($time);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Transformations for TableNode arguments.
|
|
|
101 |
*
|
|
|
102 |
* Transformations applicable to TableNode arguments should also
|
|
|
103 |
* be applied, adding them in a different method for Behat API restrictions.
|
|
|
104 |
*
|
|
|
105 |
* @Transform table:*
|
|
|
106 |
* @param TableNode $tablenode
|
|
|
107 |
* @return TableNode The transformed table
|
|
|
108 |
*/
|
|
|
109 |
public function tablenode_transformations(TableNode $tablenode) {
|
|
|
110 |
global $CFG;
|
|
|
111 |
// Walk through all values including the optional headers.
|
|
|
112 |
$rows = $tablenode->getRows();
|
|
|
113 |
foreach ($rows as $rowkey => $row) {
|
|
|
114 |
foreach ($row as $colkey => $value) {
|
|
|
115 |
|
|
|
116 |
// Transforms vars into nasty strings.
|
|
|
117 |
if (preg_match('/\$NASTYSTRING(\d)/', $rows[$rowkey][$colkey])) {
|
|
|
118 |
$rows[$rowkey][$colkey] = $this->replace_nasty_strings($rows[$rowkey][$colkey]);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// Transform time.
|
|
|
122 |
if (preg_match('/^##(.*)##$/', $rows[$rowkey][$colkey], $match)) {
|
|
|
123 |
if (isset($match[1])) {
|
|
|
124 |
$rows[$rowkey][$colkey] = $this->get_transformed_timestamp($match[1]);
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// Transform wwwroot.
|
|
|
129 |
if (preg_match('/#wwwroot#/', $rows[$rowkey][$colkey])) {
|
|
|
130 |
$rows[$rowkey][$colkey] = $this->replace_wwwroot($rows[$rowkey][$colkey]);
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
// Return the transformed TableNode.
|
|
|
136 |
unset($tablenode);
|
|
|
137 |
$tablenode = new TableNode($rows);
|
|
|
138 |
|
|
|
139 |
return $tablenode;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Convert #wwwroot# to the wwwroot config value, so it is
|
|
|
144 |
* possible to reference fully qualified URLs within the site.
|
|
|
145 |
*
|
|
|
146 |
* @Transform /^((.*)#wwwroot#(.*))$/
|
|
|
147 |
* @param string $string
|
|
|
148 |
* @return string
|
|
|
149 |
*/
|
|
|
150 |
public function arg_insert_wwwroot(string $string): string {
|
|
|
151 |
return $this->replace_wwwroot($string);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Replaces $NASTYSTRING vars for a nasty string.
|
|
|
156 |
*
|
|
|
157 |
* Method reused by TableNode tranformation.
|
|
|
158 |
*
|
|
|
159 |
* @param string $string
|
|
|
160 |
* @return string
|
|
|
161 |
*/
|
|
|
162 |
public function replace_nasty_strings($string) {
|
|
|
163 |
return preg_replace_callback(
|
|
|
164 |
'/\$NASTYSTRING(\d)/',
|
|
|
165 |
function ($matches) {
|
|
|
166 |
return nasty_strings::get($matches[0]);
|
|
|
167 |
},
|
|
|
168 |
$string
|
|
|
169 |
);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Return timestamp for the time passed.
|
|
|
174 |
*
|
|
|
175 |
* @param string $time time to convert
|
|
|
176 |
* @return string
|
|
|
177 |
*/
|
|
|
178 |
protected function get_transformed_timestamp($time) {
|
|
|
179 |
$timepassed = explode('##', $time);
|
|
|
180 |
|
|
|
181 |
// If not a valid time string, then just return what was passed.
|
|
|
182 |
if ((($timestamp = strtotime($timepassed[0])) === false)) {
|
|
|
183 |
return $time;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
$count = count($timepassed);
|
|
|
187 |
if ($count === 2) {
|
|
|
188 |
// If timestamp with specified strftime format, then return formatted date string.
|
|
|
189 |
return userdate($timestamp, $timepassed[1]);
|
|
|
190 |
} else if ($count === 1) {
|
|
|
191 |
return $timestamp;
|
|
|
192 |
} else {
|
|
|
193 |
// If not a valid time string, then just return what was passed.
|
|
|
194 |
return $time;
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
/**
|
|
|
199 |
* Replace #wwwroot# with the actual wwwroot config value.
|
|
|
200 |
*
|
|
|
201 |
* @param string $string String to attempt the replacement in.
|
|
|
202 |
* @return string
|
|
|
203 |
*/
|
|
|
204 |
protected function replace_wwwroot(string $string): string {
|
|
|
205 |
global $CFG;
|
|
|
206 |
return str_replace('#wwwroot#', $CFG->wwwroot, $string);
|
|
|
207 |
}
|
|
|
208 |
}
|