/lib/tests/behat/behat_transformations.php

https://github.com/markn86/moodle · PHP · 179 lines · 65 code · 18 blank · 96 comment · 10 complexity · 4ccd84da3d680f27c3d1e3beba33572b MD5 · raw file

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