first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test block area.
*
* @package core_search
* @copyright 2017 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace block_mockblock\search;
defined('MOODLE_INTERNAL') || die;
/**
* Test block area.
*
* @package core_search
* @copyright 2017 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class area extends \core_search\base_block {
public function get_document($record, $options = array()) {
throw new \coding_exception('Not implemented');
}
}
+147
View File
@@ -0,0 +1,147 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_mocksearch\search;
/**
* Component implementing search for testing purposes.
*
* @package core_search
* @category phpunit
* @copyright David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
class mock_search_area extends \core_search\base {
/** @var float If set, waits when doing the indexing query (seconds) */
protected $indexingdelay = 0;
/**
* Multiple context level so we can test get_areas_user_accesses.
* @var int[]
*/
protected static $levels = [CONTEXT_COURSE, CONTEXT_USER];
/**
* To make things easier, base class required config stuff.
*
* @return bool
*/
public function is_enabled() {
return true;
}
public function get_recordset_by_timestamp($modifiedfrom = 0) {
global $DB;
if ($this->indexingdelay) {
\testable_core_search::fake_current_time(
\core_search\manager::get_current_time() + $this->indexingdelay);
}
$sql = "SELECT * FROM {temp_mock_search_area} WHERE timemodified >= ? ORDER BY timemodified ASC";
return $DB->get_recordset_sql($sql, array($modifiedfrom));
}
/**
* A helper function that will turn a record into 'data array', for use with document building.
*/
public function convert_record_to_doc_array($record) {
$docdata = (array)unserialize($record->info);
$docdata['areaid'] = $this->get_area_id();
$docdata['itemid'] = $record->id;
$docdata['modified'] = $record->timemodified;
return $docdata;
}
public function get_document($record, $options = array()) {
global $USER;
$info = unserialize($record->info);
// Prepare associative array with data from DB.
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
$doc->set('title', $info->title);
$doc->set('content', $info->content);
$doc->set('description1', $info->description1);
$doc->set('description2', $info->description2);
$doc->set('contextid', $info->contextid);
$doc->set('courseid', $info->courseid);
$doc->set('userid', $info->userid);
$doc->set('owneruserid', $info->owneruserid);
$doc->set('modified', $record->timemodified);
return $doc;
}
public function attach_files($document) {
global $DB;
if (!$record = $DB->get_record('temp_mock_search_area', array('id' => $document->get('itemid')))) {
return;
}
$info = unserialize($record->info);
foreach ($info->attachfileids as $fileid) {
$document->add_stored_file($fileid);
}
}
public function uses_file_indexing() {
return true;
}
public function check_access($id) {
global $DB, $USER;
if ($record = $DB->get_record('temp_mock_search_area', array('id' => $id))) {
$info = unserialize($record->info);
if (in_array($USER->id, $info->denyuserids)) {
return \core_search\manager::ACCESS_DENIED;
}
return \core_search\manager::ACCESS_GRANTED;
}
return \core_search\manager::ACCESS_DELETED;
}
public function get_doc_url(\core_search\document $doc) {
return new \moodle_url('/index.php');
}
public function get_context_url(\core_search\document $doc) {
return new \moodle_url('/index.php');
}
public function get_visible_name($lazyload = false) {
return 'Mock search area';
}
/**
* Sets a fake delay to simulate time taken doing the indexing query.
*
* @param float $seconds Delay in seconds for each time indexing query is called
*/
public function set_indexing_delay($seconds) {
$this->indexingdelay = $seconds;
}
}
+157
View File
@@ -0,0 +1,157 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mock_search;
/**
* Search engine for testing purposes.
*
* @package core_search
* @category phpunit
* @copyright David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_search\manager;
defined('MOODLE_INTERNAL') || die;
class engine extends \core_search\engine {
/** @var float If set, waits when adding each document (seconds) */
protected $adddelay = 0;
/** @var \core_search\document[] Documents added */
protected $added = [];
/** @var array Schema updates applied */
protected $schemaupdates = [];
/** @var array delete of course index. */
protected $deletes = [];
public function is_installed() {
return true;
}
public function is_server_ready() {
return true;
}
public function add_document($document, $fileindexing = false) {
if ($this->adddelay) {
\testable_core_search::fake_current_time(manager::get_current_time() + $this->adddelay);
}
$this->added[] = $document;
return true;
}
public function execute_query($data, $usercontexts, $limit = 0) {
// No need to implement.
return [];
}
public function delete($areaid = null) {
return null;
}
public function to_document(\core_search\base $searcharea, $docdata) {
return parent::to_document($searcharea, $docdata);
}
public function get_course($courseid) {
return parent::get_course($courseid);
}
public function get_search_area($areaid) {
return parent::get_search_area($areaid);
}
public function get_query_total_count() {
return 0;
}
/**
* Sets an add delay to simulate time taken indexing.
*
* @param float $seconds Delay in seconds for each document
*/
public function set_add_delay($seconds) {
$this->adddelay = $seconds;
}
/**
* Gets the list of indexed (added) documents since last time this function
* was called.
*
* @return \core_search\document[] List of documents, in order added.
*/
public function get_and_clear_added_documents() {
$added = $this->added;
$this->added = [];
return $added;
}
public function update_schema($oldversion, $newversion) {
$this->schemaupdates[] = [$oldversion, $newversion];
}
/**
* Gets all schema updates applied, as an array. Each entry has an array with two values,
* old and new version.
*
* @return array List of schema updates for comparison
*/
public function get_and_clear_schema_updates() {
$result = $this->schemaupdates;
$this->schemaupdates = [];
return $result;
}
/**
* Records delete of course index so it can be checked later.
*
* @param int $oldcourseid Course id
* @return bool True to indicate action taken
*/
public function delete_index_for_course(int $oldcourseid) {
$this->deletes[] = ['course', $oldcourseid];
return true;
}
/**
* Records delete of context index so it can be checked later.
*
* @param int $oldcontextid Context id
* @return bool True to indicate action taken
*/
public function delete_index_for_context(int $oldcontextid) {
$this->deletes[] = ['context', $oldcontextid];
return true;
}
/**
* Gets all course/context deletes applied, as an array. Each entry is an array with two
* values, the first is either 'course' or 'context' and the second is the id deleted.
*
* @return array List of deletes for comparison
*/
public function get_and_clear_deletes() {
$deletes = $this->deletes;
$this->deletes = [];
return $deletes;
}
}
+135
View File
@@ -0,0 +1,135 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Core search class adapted to unit test.
*
* @package core_search
* @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/mock_search_engine.php');
/**
* Core search class adapted to unit test.
*
* Note that by default all core search areas are returned when calling get_search_areas_list,
* if you want to use the mock search area you can use testable_core_search::add_search_area
* although if you want to add mock search areas on top of the core ones you should call
* testable_core_search::add_core_search_areas before calling testable_core_search::add_search_area.
*
* @package core_search
* @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_core_search extends \core_search\manager {
/**
* Attaches the mock engine to search.
*
* Auto enables global search.
*
* @param \core_search\engine|bool $searchengine
* @param bool $ignored Second param just to make this compatible with base class
* @return testable_core_search
*/
public static function instance($searchengine = false, bool $ignored = false) {
// One per request, this should be purged during testing.
if (self::$instance !== null) {
return self::$instance;
}
set_config('enableglobalsearch', true);
// Default to the mock one.
if ($searchengine === false) {
$searchengine = new \mock_search\engine();
}
self::$instance = new testable_core_search($searchengine);
return self::$instance;
}
/**
* Changes visibility.
*
* @return array
*/
public function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
return parent::get_areas_user_accesses($limitcourseids, $limitcontextids);
}
/**
* Adds an enabled search component to the search areas list.
*
* @param string $areaid
* @param \core_search\base $searcharea
* @return void
*/
public function add_search_area($areaid, \core_search\base $searcharea) {
self::$enabledsearchareas[$areaid] = $searcharea;
self::$allsearchareas[$areaid] = $searcharea;
}
/**
* Loads all core search areas.
*
* @return void
*/
public function add_core_search_areas() {
self::get_search_areas_list(false);
self::get_search_areas_list(true);
}
/**
* Changes visibility.
*
* @param string $classname
* @return bool
*/
public static function is_search_area($classname) {
return parent::is_search_area($classname);
}
/**
* Fakes the current time for PHPunit. Turns off faking time if called with default parameter.
*
* Note: This should be replaced with core functionality once possible (see MDL-60644).
*
* @param float $faketime Current time
*/
public static function fake_current_time($faketime = 0.0) {
static::$phpunitfaketime = $faketime;
}
/**
* Makes build_limitcourseids method public for testing.
*
* @param \stdClass $formdata Submitted search form data.
*
* @return array|bool
*/
public function build_limitcourseids(\stdClass $formdata) {
$limitcourseids = parent::build_limitcourseids($formdata);
return $limitcourseids;
}
}