Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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 tool renderer
  19   *
  20   * @package    tool_behat
  21   * @copyright  2012 David MonllaĆ³
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->libdir . '/behat/classes/behat_generator_base.php');
  28  
  29  /**
  30   * Renderer for behat tool web features
  31   *
  32   * @package    tool_behat
  33   * @copyright  2012 David MonllaĆ³
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class tool_behat_renderer extends plugin_renderer_base {
  37  
  38      /**
  39       * Renders the list of available steps according to the submitted filters.
  40       *
  41       * @param mixed $stepsdefinitions Available steps array.
  42       * @param moodleform $form
  43       * @return string HTML code
  44       */
  45      public function render_stepsdefinitions($stepsdefinitions, $form) {
  46          global $CFG;
  47          require_once($CFG->libdir . '/behat/classes/behat_selectors.php');
  48  
  49          $html = $this->output->header();
  50          $html .= $this->output->heading(get_string('pluginname', 'tool_behat'));
  51          $html .= $form->render();
  52  
  53          if (empty($stepsdefinitions)) {
  54              $stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
  55          } else {
  56  
  57              $stepsdefinitions = implode('', $stepsdefinitions);
  58  
  59              // Replace text selector type arguments with a user-friendly select.
  60              $stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR\d?_STRING)/',
  61                  function ($matches) {
  62                      return html_writer::select(behat_selectors::get_allowed_text_selectors(), uniqid());
  63                  },
  64                  $stepsdefinitions
  65              );
  66  
  67              // Replace selector type arguments with a user-friendly select.
  68              $stepsdefinitions = preg_replace_callback('/(SELECTOR\d?_STRING)/',
  69                  function ($matches) {
  70                      return html_writer::select(behat_selectors::get_allowed_selectors(), uniqid());
  71                  },
  72                  $stepsdefinitions
  73              );
  74  
  75              // Replace simple OR options.
  76              $regex = '#\(\?P<[^>]+>([^\)|]+\|[^\)]+)\)#';
  77              $stepsdefinitions = preg_replace_callback($regex,
  78                  function($matches){
  79                      return html_writer::select(explode('|', $matches[1]), uniqid());
  80                  },
  81                  $stepsdefinitions
  82              );
  83  
  84              $stepsdefinitions = preg_replace_callback('/(FIELD_VALUE_STRING)/',
  85                  function ($matches) {
  86                      global $CFG;
  87  
  88                      // Creating a link to a popup with the help.
  89                      $url = new moodle_url(
  90                          '/help.php',
  91                          array(
  92                              'component' => 'tool_behat',
  93                              'identifier' => 'fieldvalueargument',
  94                              'lang' => current_language()
  95                          )
  96                      );
  97  
  98                      // Note: this title is displayed only if JS is disabled,
  99                      // otherwise the link will have the new ajax tooltip.
 100                      $title = get_string('fieldvalueargument', 'tool_behat');
 101                      $title = get_string('helpprefix2', '', trim($title, ". \t"));
 102  
 103                      $attributes = array('href' => $url, 'title' => $title,
 104                          'aria-haspopup' => 'true', 'target' => '_blank');
 105  
 106                      $output = html_writer::tag('a', 'FIELD_VALUE_STRING', $attributes);
 107                      return html_writer::tag('span', $output, array('class' => 'helptooltip'));
 108                  },
 109                  $stepsdefinitions
 110              );
 111  
 112              $elementstrings = [];
 113              $count = 1;
 114              $stepsdefinitions = preg_replace_callback('/(the following ")ELEMENT\d?_STRING(" exist:)/',
 115                  function($matches) use (&$elementstrings, &$count) {
 116                      // Replace element type arguments with a user-friendly select.
 117                      if (empty($elementstrings)) {
 118                          $behatgenerators = new behat_data_generators();
 119                          $componententities = $behatgenerators->get_all_entities();
 120                          ksort($componententities);
 121                          $elementstrings = [];
 122                          foreach ($componententities as $component => $entities) {
 123                              asort($entities);
 124                              foreach ($entities as $entity) {
 125                                  $string = ($component === 'core') ? $entity : $component . ' > ' . $entity;
 126                                  $elementstrings[$string] = $string;
 127                              }
 128                          }
 129                      }
 130                      $select = html_writer::select($elementstrings, 'entities' . $count, '', ['' => 'choosedots'],
 131                              ['class' => 'entities']);
 132                      $count++;
 133                      return $matches[1] . $select . $matches[2];
 134                  },
 135                  $stepsdefinitions
 136              );
 137          }
 138  
 139          // Steps definitions.
 140          $html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
 141  
 142          $html .= $this->output->footer();
 143  
 144          return $html;
 145      }
 146  
 147      /**
 148       * Renders an error message adding the generic info about the tool purpose and setup.
 149       *
 150       * @param string $msg The error message
 151       * @return string HTML
 152       */
 153      public function render_error($msg) {
 154  
 155          $html = $this->output->header();
 156          $html .= $this->output->heading(get_string('pluginname', 'tool_behat'));
 157          $html .= $this->generic_info();
 158  
 159          $a = new stdClass();
 160          $a->errormsg = $msg;
 161          $a->behatcommand = behat_command::get_behat_command();
 162          $a->behatinit = 'php admin' . DIRECTORY_SEPARATOR . 'tool' . DIRECTORY_SEPARATOR .
 163              'behat' . DIRECTORY_SEPARATOR . 'cli' . DIRECTORY_SEPARATOR . 'init.php';
 164  
 165          $msg = get_string('wrongbehatsetup', 'tool_behat', $a);
 166  
 167          // Error box including generic error string + specific error msg.
 168          $html .= $this->output->box_start('box errorbox alert alert-danger');
 169          $html .= html_writer::tag('div', $msg);
 170          $html .= $this->output->box_end();
 171  
 172          $html .= $this->output->footer();
 173  
 174          return $html;
 175      }
 176  
 177      /**
 178       * Generic info about the tool.
 179       *
 180       * @return string
 181       */
 182      public function generic_info() {
 183  
 184          // Info.
 185          $installurl = behat_command::DOCS_URL;
 186          $installlink = html_writer::tag('a', $installurl, array('href' => $installurl, 'target' => '_blank'));
 187          $writetestsurl = 'https://moodledev.io/general/development/tools/behat/writing';
 188          $writetestslink = html_writer::tag('a', $writetestsurl, array('href' => $writetestsurl, 'target' => '_blank'));
 189          $writestepsurl = 'https://moodledev.io/general/development/tools/behat/writing#' .
 190              'writing-new-acceptance-test-step-definitions';
 191          $writestepslink = html_writer::tag('a', $writestepsurl, array('href' => $writestepsurl, 'target' => '_blank'));
 192          $infos = array(
 193              get_string('installinfo', 'tool_behat', $installlink),
 194              get_string('newtestsinfo', 'tool_behat', $writetestslink),
 195              get_string('newstepsinfo', 'tool_behat', $writestepslink)
 196          );
 197  
 198          // List of steps.
 199          $html = $this->output->box_start();
 200          $html .= html_writer::tag('div', get_string('aim', 'tool_behat'));
 201          $html .= html_writer::start_tag('div');
 202          $html .= html_writer::start_tag('ul');
 203          $html .= html_writer::start_tag('li');
 204          $html .= implode(html_writer::end_tag('li') . html_writer::start_tag('li'), $infos);
 205          $html .= html_writer::end_tag('li');
 206          $html .= html_writer::end_tag('ul');
 207          $html .= html_writer::end_tag('div');
 208          $html .= $this->output->box_end();
 209  
 210          return $html;
 211      }
 212  
 213  }