Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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 search-related step definitions.
  19   *
  20   * @package core_search
  21   * @category test
  22   * @copyright 2017 The Open University
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL used, this file may be required by behat before including /config.php.
  27  require_once (__DIR__ . '/../../../lib/behat/behat_base.php');
  28  
  29  use Behat\Gherkin\Node\TableNode as TableNode;
  30  use Moodle\BehatExtension\Exception\SkippedException;
  31  
  32  /**
  33   * Behat search-related step definitions.
  34   *
  35   * @package core_search
  36   * @category test
  37   * @copyright 2017 The Open University
  38   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class behat_search extends behat_base {
  41      /**
  42       * Create event when starting on the front page.
  43       *
  44       * @Given /^I search for "(?P<query>[^"]*)" using the header global search box$/
  45       * @param string $query Query to search for
  46       */
  47      public function i_search_for_using_the_header_global_search_box($query) {
  48          // Click the search icon.
  49          $this->execute("behat_general::i_click_on", [get_string('togglesearch', 'core'), 'button']);
  50  
  51          // Set the field.
  52          $this->execute('behat_forms::i_set_the_field_to', ['q', $query]);
  53  
  54          // Submit the form.
  55          $this->execute_script('return document.querySelector(".searchform-navbar").submit();');
  56      }
  57  
  58      /**
  59       * Sets results which will be returned for the next search. It will only return links to
  60       * activities at present.
  61       *
  62       * @Given /^global search expects the query "(?P<query>[^"]*)" and will return:$/
  63       * @param string $query Expected query value (just used to check the query passed to the engine)
  64       * @param TableNode $data Data rows
  65       */
  66      public function global_search_expects_the_query_and_will_return($query, TableNode $data) {
  67          global $DB;
  68          $outdata = new stdClass();
  69          $outdata->query = $query;
  70          $outdata->results = [];
  71          foreach ($data->getHash() as $rowdata) {
  72              // Check and get the data from the user-entered row.
  73              $input = [
  74                  'type' => '',
  75                  'idnumber' => '',
  76                  'title' => '',
  77                  'content' => '',
  78                  'modified' => ''
  79              ];
  80              foreach ($rowdata as $key => $value) {
  81                  if (!array_key_exists($key, $input)) {
  82                      throw new Exception('Field ' . $key . '" does not exist');
  83                  }
  84                  $input[$key] = $value;
  85              }
  86              foreach (['idnumber', 'type'] as $requiredfield) {
  87                  if (!$input[$requiredfield]) {
  88                      throw new Exception('Must specify required field: ' . $requiredfield);
  89                  }
  90              }
  91  
  92              // Check type (we only support activity at present, this could be extended to allow
  93              // faking other types of search results such as a user, course, or forum post).
  94              if ($input['type'] !== 'activity') {
  95                  throw new Exception('Unsupported type: ' . $input['type']);
  96              }
  97  
  98              // Find the specified activity.
  99              $idnumber = $input['idnumber'];
 100              $cmid = $DB->get_field('course_modules', 'id', ['idnumber' => $idnumber], IGNORE_MISSING);
 101              if (!$cmid) {
 102                  throw new Exception('Cannot find activity with idnumber: ' . $idnumber);
 103              }
 104              list ($course, $cm) = get_course_and_cm_from_cmid($cmid);
 105              $rec = $DB->get_record($cm->modname, ['id' => $cm->instance], '*', MUST_EXIST);
 106              $context = \context_module::instance($cm->id);
 107  
 108              // Set up the internal fields used in creating the search document.
 109              $out = new stdClass();
 110              $out->itemid = $cm->instance;
 111              $out->componentname = 'mod_' . $cm->modname;
 112              $out->areaname = 'activity';
 113              $out->fields = new stdClass();
 114              $out->fields->contextid = $context->id;
 115              $out->fields->courseid = $course->id;
 116              if ($input['title']) {
 117                  $out->fields->title = $input['title'];
 118              } else {
 119                  $out->fields->title = $cm->name;
 120              }
 121              if ($input['content']) {
 122                  $out->fields->content = $input['content'];
 123              } else {
 124                  $out->fields->content = content_to_text($rec->intro, $rec->introformat);
 125              }
 126              if ($input['modified']) {
 127                  $out->fields->modified = strtotime($input['modified']);
 128              } else {
 129                  $out->fields->modified = $cm->added;
 130              }
 131              $out->extrafields = new stdClass();
 132              $out->extrafields->coursefullname = $course->fullname;
 133  
 134              $outdata->results[] = $out;
 135          }
 136  
 137          set_config('behat_fakeresult', json_encode($outdata), 'core_search');
 138      }
 139  
 140      /**
 141       * Updates the global search index to take account of any added activities.
 142       *
 143       * @Given /^I update the global search index$/
 144       * @throws moodle_exception
 145       */
 146      public function i_update_the_global_search_index() {
 147          \core_search\manager::instance()->index(false);
 148      }
 149  
 150      /**
 151       * This step looks to see if Solr is installed or skip the rest of the scenario otherwise
 152       *
 153       * @Given /^solr is installed/
 154       */
 155      public function solr_is_installed() {
 156          if (!function_exists('solr_get_version')) {
 157              throw new SkippedException('Skipping this scenario because Solr is not installed.');
 158          }
 159      }
 160  }