weekly release 3.6dev
[moodle.git] / lib / tests / behat / behat_transformations.php
CommitLineData
b08b0a28
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 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
30require_once(__DIR__ . '/../../behat/behat_base.php');
31
32use 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 */
45class behat_transformations extends behat_base {
46
b08b0a28
DM
47 /**
48 * Transformations for TableNode arguments.
49 *
e88d47b4
DM
50 * Transformations applicable to TableNode arguments should also
51 * be applied, adding them in a different method for Behat API restrictions.
b08b0a28 52 *
45ff0ff5 53 * @deprecated since Moodle 3.2 MDL-56335 - please do not use this function any more.
46ac40cd 54 * @param TableNode $tablenode
b08b0a28
DM
55 * @return TableNode The transformed table
56 */
42ad096f 57 public function prefixed_tablenode_transformations(TableNode $tablenode) {
45ff0ff5
RT
58 debugging('prefixed_tablenode_transformations() is deprecated. Please use tablenode_transformations() instead.',
59 DEBUG_DEVELOPER);
60
42ad096f
RT
61 return $this->tablenode_transformations($tablenode);
62 }
b08b0a28 63
45ff0ff5
RT
64 /**
65 * Removes escaped argument delimiters.
66 *
67 * We use double quotes as arguments delimiters and
68 * to add the " as part of an argument we escape it
69 * with a backslash, this method removes this backslash.
70 *
71 * @Transform /^((.*)"(.*))$/
72 * @param string $string
73 * @return string The string with the arguments fixed.
74 */
75 public function arg_replace_slashes($string) {
76 if (!is_scalar($string)) {
77 return $string;
78 }
79 return str_replace('\"', '"', $string);
80 }
81
c5a566dc
RT
82 /**
83 * Replaces $NASTYSTRING vars for a nasty string.
84 *
85 * @Transform /^((.*)\$NASTYSTRING(\d)(.*))$/
86 * @param string $argument The whole argument value.
87 * @return string
88 */
89 public function arg_replace_nasty_strings($argument) {
90 if (!is_scalar($argument)) {
91 return $argument;
92 }
93 return $this->replace_nasty_strings($argument);
94 }
95
96 /**
97 * Convert string time to timestamp.
98 * Use ::time::STRING_TIME_TO_CONVERT::DATE_FORMAT::
99 *
100 * @Transform /^##(.*)##$/
101 * @param string $time
102 * @return int timestamp.
103 */
104 public function arg_time_to_string($time) {
105 return $this->get_transformed_timestamp($time);
106 }
107
42ad096f
RT
108 /**
109 * Transformations for TableNode arguments.
110 *
111 * Transformations applicable to TableNode arguments should also
112 * be applied, adding them in a different method for Behat API restrictions.
113 *
45ff0ff5 114 * @Transform table:*
42ad096f
RT
115 * @param TableNode $tablenode
116 * @return TableNode The transformed table
117 */
118 public function tablenode_transformations(TableNode $tablenode) {
b08b0a28
DM
119 // Walk through all values including the optional headers.
120 $rows = $tablenode->getRows();
121 foreach ($rows as $rowkey => $row) {
122 foreach ($row as $colkey => $value) {
123
b08b0a28
DM
124 // Transforms vars into nasty strings.
125 if (preg_match('/\$NASTYSTRING(\d)/', $rows[$rowkey][$colkey])) {
126 $rows[$rowkey][$colkey] = $this->replace_nasty_strings($rows[$rowkey][$colkey]);
127 }
45ff0ff5
RT
128
129 // Transform time.
130 if (preg_match('/^##(.*)##$/', $rows[$rowkey][$colkey], $match)) {
131 if (isset($match[1])) {
132 $rows[$rowkey][$colkey] = $this->get_transformed_timestamp($match[1]);
133 }
134 }
b08b0a28
DM
135 }
136 }
137
138 // Return the transformed TableNode.
42ad096f
RT
139 unset($tablenode);
140 $tablenode = new TableNode($rows);
141
b08b0a28
DM
142 return $tablenode;
143 }
144
b08b0a28
DM
145 /**
146 * Replaces $NASTYSTRING vars for a nasty string.
147 *
148 * Method reused by TableNode tranformation.
149 *
150 * @param string $string
151 * @return string
152 */
153 public function replace_nasty_strings($string) {
154 return preg_replace_callback(
155 '/\$NASTYSTRING(\d)/',
156 function ($matches) {
157 return nasty_strings::get($matches[0]);
158 },
159 $string
160 );
161 }
162
45ff0ff5
RT
163 /**
164 * Return timestamp for the time passed.
165 *
166 * @param string $time time to convert
167 * @return string
168 */
169 protected function get_transformed_timestamp($time) {
170 $timepassed = explode('##', $time);
171
172 // If not a valid time string, then just return what was passed.
173 if ((($timestamp = strtotime($timepassed[0])) === false)) {
174 return $time;
175 }
176
177 $count = count($timepassed);
178 if ($count === 2) {
179 // If timestamp with spcified format, then retrun date.
180 return date($timepassed[1], $timestamp);
181 } else if ($count === 1) {
182 return $timestamp;
183 } else {
184 // If not a valid time string, then just return what was passed.
185 return $time;
186 }
187 }
b08b0a28 188}