| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | namespace tool_generator\local\testscenario;
 | 
        
           |  |  | 18 |   | 
        
           | 1441 | ariadna | 19 | use behat_base;
 | 
        
           | 1 | efrain | 20 | use Behat\Gherkin\Node\StepNode;
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | /**
 | 
        
           |  |  | 23 |  * Class to validate and process a scenario step.
 | 
        
           |  |  | 24 |  *
 | 
        
           |  |  | 25 |  * @package    tool_generator
 | 
        
           |  |  | 26 |  * @copyright  2023 Ferran Recio <ferran@moodle.com>
 | 
        
           |  |  | 27 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 28 |  */
 | 
        
           |  |  | 29 | class steprunner {
 | 
        
           | 1441 | ariadna | 30 |     /** @var behat_base|null the behat step class instance. */
 | 
        
           |  |  | 31 |     private ?behat_base $generator = null;
 | 
        
           | 1 | efrain | 32 |   | 
        
           |  |  | 33 |     /** @var array the valid steps indexed by given expression tag. */
 | 
        
           |  |  | 34 |     private array $validsteps;
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |     /** @var StepNode the step node to process. */
 | 
        
           |  |  | 37 |     private StepNode $stepnode;
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     /** @var string|null the generator method to call. */
 | 
        
           |  |  | 40 |     private ?string $method = null;
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |     /** @var array the parameters to pass to the generator method. */
 | 
        
           |  |  | 43 |     private array $params = [];
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 |     /** @var bool if the step is valid. */
 | 
        
           |  |  | 46 |     private bool $isvalid = false;
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |     /** @var bool if the step has been executed. */
 | 
        
           |  |  | 49 |     private bool $executed = false;
 | 
        
           |  |  | 50 |   | 
        
           |  |  | 51 |     /** @var string the error message if any. */
 | 
        
           |  |  | 52 |     private string $error = '';
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     /**
 | 
        
           |  |  | 55 |      * Constructor.
 | 
        
           | 1441 | ariadna | 56 |      * @param behat_base|null $unused This does nothing, do not use it.
 | 
        
           | 1 | efrain | 57 |      * @param array $validsteps the valid steps indexed by given expression tag.
 | 
        
           |  |  | 58 |      * @param StepNode $stepnode the step node to process.
 | 
        
           |  |  | 59 |      */
 | 
        
           | 1441 | ariadna | 60 |     public function __construct($unused, array $validsteps, StepNode $stepnode) {
 | 
        
           |  |  | 61 |         if ($unused !== null) {
 | 
        
           |  |  | 62 |             debugging('Deprecated argument passed to ' . __FUNCTION__, DEBUG_DEVELOPER);
 | 
        
           |  |  | 63 |         }
 | 
        
           | 1 | efrain | 64 |         $this->validsteps = $validsteps;
 | 
        
           |  |  | 65 |         $this->stepnode = $stepnode;
 | 
        
           |  |  | 66 |         $this->init();
 | 
        
           |  |  | 67 |     }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |     /**
 | 
        
           |  |  | 70 |      * Init the step runner.
 | 
        
           |  |  | 71 |      *
 | 
        
           |  |  | 72 |      * This method will check if the step is valid and all the needed information
 | 
        
           |  |  | 73 |      * in case it is executed.
 | 
        
           |  |  | 74 |      */
 | 
        
           |  |  | 75 |     private function init() {
 | 
        
           |  |  | 76 |         $matches = [];
 | 
        
           |  |  | 77 |         $linetext = $this->stepnode->getText();
 | 
        
           | 1441 | ariadna | 78 |         foreach ($this->validsteps as $method) {
 | 
        
           |  |  | 79 |             if (!$this->match_given($method->given, $linetext, $matches)) {
 | 
        
           | 1 | efrain | 80 |                 continue;
 | 
        
           |  |  | 81 |             }
 | 
        
           | 1441 | ariadna | 82 |             $this->method = $method->name;
 | 
        
           |  |  | 83 |             $this->params = $this->build_method_params($method->name, $matches, $method->generator);
 | 
        
           |  |  | 84 |             $this->generator = $method->generator;
 | 
        
           | 1 | efrain | 85 |             $this->isvalid = true;
 | 
        
           |  |  | 86 |             return;
 | 
        
           |  |  | 87 |         }
 | 
        
           |  |  | 88 |         $this->error = get_string('testscenario_invalidstep', 'tool_generator');
 | 
        
           |  |  | 89 |     }
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |     /**
 | 
        
           |  |  | 92 |      * Build the method parameters.
 | 
        
           |  |  | 93 |      * @param string $methodname the method name.
 | 
        
           |  |  | 94 |      * @param array $matches the matches.
 | 
        
           | 1441 | ariadna | 95 |      * @param behat_base $generator the method class.
 | 
        
           | 1 | efrain | 96 |      * @return array the method parameters.
 | 
        
           |  |  | 97 |      */
 | 
        
           | 1441 | ariadna | 98 |     private function build_method_params(string $methodname, array $matches, behat_base $generator) {
 | 
        
           |  |  | 99 |         $method = new \ReflectionMethod($generator, $methodname);
 | 
        
           | 1 | efrain | 100 |         $params = [];
 | 
        
           |  |  | 101 |         foreach ($method->getParameters() as $param) {
 | 
        
           |  |  | 102 |             $paramname = $param->getName();
 | 
        
           |  |  | 103 |             if (isset($matches[$paramname])) {
 | 
        
           |  |  | 104 |                 $params[] = $matches[$paramname];
 | 
        
           |  |  | 105 |                 unset($matches[$paramname]);
 | 
        
           | 1441 | ariadna | 106 |             } else if (isset($matches["{$paramname}_string"])) {
 | 
        
           |  |  | 107 |                 // If the param uses a regular expression with a name.
 | 
        
           |  |  | 108 |                 $params[] = $matches["{$paramname}_string"];
 | 
        
           |  |  | 109 |                 unset($matches["{$paramname}_string"]);
 | 
        
           | 1 | efrain | 110 |             } else if (count($matches) > 0) {
 | 
        
           |  |  | 111 |                 // If the param is not present means the regular expressions does not use
 | 
        
           |  |  | 112 |                 // proper names. So we will try to find the param by position.
 | 
        
           |  |  | 113 |                 $params[] = array_pop($matches);
 | 
        
           |  |  | 114 |             } else {
 | 
        
           |  |  | 115 |                 // No more params to match.
 | 
        
           |  |  | 116 |                 break;
 | 
        
           |  |  | 117 |             }
 | 
        
           |  |  | 118 |         }
 | 
        
           |  |  | 119 |         return array_merge($params, $this->stepnode->getArguments());
 | 
        
           |  |  | 120 |     }
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 |     /**
 | 
        
           |  |  | 123 |      * Return if the step is valid.
 | 
        
           |  |  | 124 |      * @return bool
 | 
        
           |  |  | 125 |      */
 | 
        
           |  |  | 126 |     public function is_valid(): bool {
 | 
        
           |  |  | 127 |         return $this->isvalid;
 | 
        
           |  |  | 128 |     }
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |     /**
 | 
        
           |  |  | 131 |      * Return if the step has been executed.
 | 
        
           |  |  | 132 |      * @return bool
 | 
        
           |  |  | 133 |      */
 | 
        
           |  |  | 134 |     public function is_executed(): bool {
 | 
        
           |  |  | 135 |         return $this->executed;
 | 
        
           |  |  | 136 |     }
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |     /**
 | 
        
           |  |  | 139 |      * Return the step text.
 | 
        
           |  |  | 140 |      * @return string
 | 
        
           |  |  | 141 |      */
 | 
        
           |  |  | 142 |     public function get_text(): string {
 | 
        
           |  |  | 143 |         return $this->stepnode->getText();
 | 
        
           |  |  | 144 |     }
 | 
        
           |  |  | 145 |   | 
        
           |  |  | 146 |     /**
 | 
        
           |  |  | 147 |      * Return the step error message.
 | 
        
           |  |  | 148 |      * @return string
 | 
        
           |  |  | 149 |      */
 | 
        
           |  |  | 150 |     public function get_error(): string {
 | 
        
           |  |  | 151 |         return $this->error;
 | 
        
           |  |  | 152 |     }
 | 
        
           |  |  | 153 |   | 
        
           |  |  | 154 |     /**
 | 
        
           |  |  | 155 |      * Return the step arguments as string.
 | 
        
           |  |  | 156 |      * @return string
 | 
        
           |  |  | 157 |      */
 | 
        
           |  |  | 158 |     public function get_arguments_string(): string {
 | 
        
           |  |  | 159 |         $result = '';
 | 
        
           |  |  | 160 |         foreach ($this->stepnode->getArguments() as $argument) {
 | 
        
           |  |  | 161 |             $result .= $argument->getTableAsString();
 | 
        
           |  |  | 162 |         }
 | 
        
           |  |  | 163 |         return $result;
 | 
        
           |  |  | 164 |     }
 | 
        
           |  |  | 165 |   | 
        
           |  |  | 166 |     /**
 | 
        
           |  |  | 167 |      * Match a given expression with a text.
 | 
        
           |  |  | 168 |      * @param string $pattern the given expression.
 | 
        
           |  |  | 169 |      * @param string $text the text to match.
 | 
        
           |  |  | 170 |      * @param array $matches the matches.
 | 
        
           |  |  | 171 |      * @return bool if the step matched the generator given expression.
 | 
        
           |  |  | 172 |      */
 | 
        
           |  |  | 173 |     private function match_given(string $pattern, $text, array &$matches) {
 | 
        
           |  |  | 174 |         $internalmatcher = [];
 | 
        
           |  |  | 175 |         if (substr($pattern, 0, 1) === '/') {
 | 
        
           |  |  | 176 |             // Pattern is a regular expression.
 | 
        
           |  |  | 177 |             $result = preg_match($pattern, $text, $matches);
 | 
        
           |  |  | 178 |             foreach ($matches as $key => $value) {
 | 
        
           |  |  | 179 |                 if (is_int($key)) {
 | 
        
           |  |  | 180 |                     unset($matches[$key]);
 | 
        
           |  |  | 181 |                 }
 | 
        
           |  |  | 182 |             }
 | 
        
           |  |  | 183 |             return $result;
 | 
        
           |  |  | 184 |         }
 | 
        
           |  |  | 185 |   | 
        
           |  |  | 186 |         // Patter is a string with parameters.
 | 
        
           |  |  | 187 |         $elementmatches = [];
 | 
        
           |  |  | 188 |         preg_match_all('/:([^ ]+)/', $pattern, $elementmatches, PREG_SET_ORDER, 0);
 | 
        
           |  |  | 189 |   | 
        
           |  |  | 190 |         $pattern = preg_replace('/:([^ ]+)/', '(?P<$1>"[^"]+"|[^" ]+)', $pattern);
 | 
        
           |  |  | 191 |         $pattern = '/^' . $pattern . '$/';
 | 
        
           |  |  | 192 |         $result = preg_match($pattern, $text, $internalmatcher);
 | 
        
           |  |  | 193 |         if (!$result) {
 | 
        
           |  |  | 194 |             return false;
 | 
        
           |  |  | 195 |         }
 | 
        
           |  |  | 196 |         foreach ($elementmatches as $elementmatch) {
 | 
        
           |  |  | 197 |             // Remove any possible " at the beggining and end of $internalmatcher[$elementmatch[1]].
 | 
        
           |  |  | 198 |             $paramvalue = preg_replace('/^"(.*)"$/', '$1', $internalmatcher[$elementmatch[1]]);
 | 
        
           |  |  | 199 |             $matches[$elementmatch[1]] = $paramvalue;
 | 
        
           |  |  | 200 |         }
 | 
        
           |  |  | 201 |         return true;
 | 
        
           |  |  | 202 |     }
 | 
        
           |  |  | 203 |   | 
        
           |  |  | 204 |     /**
 | 
        
           |  |  | 205 |      * Execute the step.
 | 
        
           |  |  | 206 |      * @return bool if the step is executed or not.
 | 
        
           |  |  | 207 |      */
 | 
        
           |  |  | 208 |     public function execute(): bool {
 | 
        
           |  |  | 209 |         if (!$this->isvalid) {
 | 
        
           |  |  | 210 |             return false;
 | 
        
           |  |  | 211 |         }
 | 
        
           |  |  | 212 |         $this->executed = true;
 | 
        
           |  |  | 213 |         try {
 | 
        
           |  |  | 214 |             call_user_func_array(
 | 
        
           |  |  | 215 |                 [$this->generator, $this->method],
 | 
        
           |  |  | 216 |                 $this->params
 | 
        
           |  |  | 217 |             );
 | 
        
           |  |  | 218 |         } catch (\moodle_exception $exception) {
 | 
        
           |  |  | 219 |             $this->error = $exception->getMessage();
 | 
        
           |  |  | 220 |             $this->isvalid = false;
 | 
        
           |  |  | 221 |             return false;
 | 
        
           |  |  | 222 |         }
 | 
        
           |  |  | 223 |         return true;
 | 
        
           |  |  | 224 |     }
 | 
        
           |  |  | 225 | }
 |