Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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              new behat_component_named_selector('Audience', [
  79                  ".//*[@data-region='audiences']//*[@data-audience-title=%locator%]",
  80              ]),
  81          ];
  82      }
  83  
  84      /**
  85       * Set aggregation for given column in report editor (proxied so we can skip if aggregation type not available)
  86       *
  87       * @When I set the :column column aggregation to :aggregation
  88       *
  89       * @param string $column
  90       * @param string $aggregation
  91       *
  92       * @throws \Moodle\BehatExtension\Exception\SkippedException
  93       */
  94      public function i_set_the_column_aggregation_to(string $column, string $aggregation): void {
  95  
  96          // Skip if aggregation type unavailable.
  97          $aggregationgroupconcatdistinct = (string) groupconcatdistinct::get_name();
  98          if ($aggregation === $aggregationgroupconcatdistinct && !groupconcatdistinct::compatible(column::TYPE_TEXT)) {
  99              throw new \Moodle\BehatExtension\Exception\SkippedException("{$aggregationgroupconcatdistinct} not available");
 100          }
 101  
 102          $editlabel = get_string('aggregatecolumn', 'core_reportbuilder', $column);
 103          $this->execute('behat_forms::i_set_the_field_to', [$this->escape($editlabel), $this->escape($aggregation)]);
 104      }
 105  
 106      /**
 107       * Press a given action from the action menu in a given report row
 108       *
 109       * @When I press :action action in the :row report row
 110       *
 111       * @param string $action
 112       * @param string $row
 113       */
 114      public function i_press_action_in_the_report_row(string $action, string $row): void {
 115          $this->execute('behat_action_menu::i_choose_in_the_named_menu_in_container', [
 116              $this->escape($action),
 117              get_string('actions', 'core_reportbuilder'),
 118              $this->escape($row),
 119              'table_row',
 120          ]);
 121      }
 122  }