MDL-38378 Take rid of harcoded db prefix and bump.
[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
47 /**
48 * Removes escaped argument delimiters.
49 *
50 * We use double quotes as arguments delimiters and
51 * to add the " as part of an argument we escape it
52 * with a backslash, this method removes this backslash.
53 *
54 * @Transform /^((.*)"(.*))$/
55 * @param string $string
56 * @return string The string with the arguments fixed.
57 */
58 public function arg_replace_slashes($string) {
59 return $this->replace_slashes($string);
60 }
61
62 /**
63 * Replaces $NASTYSTRING vars for a nasty string.
64 *
65 * @Transform /^((.*)\$NASTYSTRING(\d)(.*))$/
66 * @param string $argument The whole argument value.
67 * @return string
68 */
69 public function arg_replace_nasty_strings($argument) {
70 return $this->replace_nasty_strings($argument);
71 }
72
73 /**
74 * Transformations for TableNode arguments.
75 *
76 * All the transformations have to be applied to tables,
77 * adding them in a different method for Behat API restrictions.
78 *
79 * @Transform /^table:(.*)/
46ac40cd 80 * @param TableNode $tablenode
b08b0a28
DM
81 * @return TableNode The transformed table
82 */
83 public function tablenode_transformations(TableNode $tablenode) {
84
85 // Walk through all values including the optional headers.
86 $rows = $tablenode->getRows();
87 foreach ($rows as $rowkey => $row) {
88 foreach ($row as $colkey => $value) {
89
90 // TableNodes values doesn't need to be escaped, but maybe somebody does it.
91 $rows[$rowkey][$colkey] = $this->replace_slashes($value);
92
93 // Transforms vars into nasty strings.
94 if (preg_match('/\$NASTYSTRING(\d)/', $rows[$rowkey][$colkey])) {
95 $rows[$rowkey][$colkey] = $this->replace_nasty_strings($rows[$rowkey][$colkey]);
96 }
97 }
98 }
99
100 // Return the transformed TableNode.
101 $tablenode->setRows($rows);
102 return $tablenode;
103 }
104
105 /**
106 * Removes the escaped double quotes.
107 *
108 * Method reused by TableNode transformation.
109 *
110 * @param string $string
111 * @return string
112 */
113 public function replace_slashes($string) {
114 return str_replace('\"', '"', $string);
115 }
116
117 /**
118 * Replaces $NASTYSTRING vars for a nasty string.
119 *
120 * Method reused by TableNode tranformation.
121 *
122 * @param string $string
123 * @return string
124 */
125 public function replace_nasty_strings($string) {
126 return preg_replace_callback(
127 '/\$NASTYSTRING(\d)/',
128 function ($matches) {
129 return nasty_strings::get($matches[0]);
130 },
131 $string
132 );
133 }
134
135}