Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 403]

   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  declare(strict_types=1);
  18  
  19  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  20  require_once (__DIR__ . '/../../../lib/behat/behat_base.php');
  21  
  22  use core_reportbuilder\local\aggregation\groupconcatdistinct;
  23  use core_reportbuilder\local\models\report;
  24  use core_reportbuilder\local\report\column;
  25  
  26  /**
  27   * Behat step definitions for Report builder
  28   *
  29   * @package     core_reportbuilder
  30   * @copyright   2021 Paul Holden <paulh@moodle.com>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class behat_reportbuilder extends behat_base {
  34  
  35      /**
  36       * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
  37       *
  38       * Recognised page names are:
  39       * | type   | identifier  | description          |
  40       * | Editor | Report name | Custom report editor |
  41       * | View   | Report name | Custom report view   |
  42       *
  43       * @param string $type
  44       * @param string $identifier
  45       * @return moodle_url
  46       * @throws Exception for unrecognised report or page type
  47       */
  48      protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
  49          if (!$report = report::get_record(['name' => $identifier])) {
  50              throw new Exception("Unknown report '{$identifier}'");
  51          }
  52  
  53          switch ($type) {
  54              case 'Editor':
  55                  return new moodle_url('/reportbuilder/edit.php', ['id' => $report->get('id')]);
  56  
  57              case 'View':
  58                  return new moodle_url('/reportbuilder/view.php', ['id' => $report->get('id')]);
  59  
  60              default:
  61                  throw new Exception("Unrecognised reportbuilder page type '{$type}'");
  62          }
  63      }
  64  
  65      /**
  66       * Return the list of partial named selectors
  67       *
  68       * @return behat_component_named_selector[]
  69       */
  70      public static function get_partial_named_selectors(): array {
  71          return [
  72              new behat_component_named_selector('Filter', [
  73                  ".//*[@data-region='filters-form']//*[@data-filter-for=%locator%]",
  74              ]),
  75              new behat_component_named_selector('Condition', [
  76                  ".//*[@data-region='conditions-form']//*[@data-condition-name=%locator%]",
  77              ]),
  78          ];
  79      }
  80  
  81      /**
  82       * Set aggregation for given column in report editor (proxied so we can skip if aggregation type not available)
  83       *
  84       * @When I set the :column column aggregation to :aggregation
  85       *
  86       * @param string $column
  87       * @param string $aggregation
  88       *
  89       * @throws \Moodle\BehatExtension\Exception\SkippedException
  90       */
  91      public function i_set_the_column_aggregation_to(string $column, string $aggregation): void {
  92  
  93          // Skip if aggregation type unavailable.
  94          $aggregationgroupconcatdistinct = (string) groupconcatdistinct::get_name();
  95          if ($aggregation === $aggregationgroupconcatdistinct && !groupconcatdistinct::compatible(column::TYPE_TEXT)) {
  96              throw new \Moodle\BehatExtension\Exception\SkippedException("{$aggregationgroupconcatdistinct} not available");
  97          }
  98  
  99          $editlabel = get_string('aggregatecolumn', 'core_reportbuilder', $column);
 100          $this->execute('behat_forms::i_set_the_field_to', [$this->escape($editlabel), $this->escape($aggregation)]);
 101      }
 102  
 103      /**
 104       * Press a given action from the action menu in a given report row
 105       *
 106       * @When I press :action action in the :row report row
 107       *
 108       * @param string $action
 109       * @param string $row
 110       */
 111      public function i_press_action_in_the_report_row(string $action, string $row): void {
 112          $this->execute('behat_action_menu::i_choose_in_the_named_menu_in_container', [
 113              $this->escape($action),
 114              get_string('actions', 'core_reportbuilder'),
 115              $this->escape($row),
 116              'table_row',
 117          ]);
 118      }
 119  }