Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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