MDL-68483 contentbank: improve search API
[moodle.git] / contentbank / tests / generator / lib.php
CommitLineData
7fc73af6
SA
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 * Generator for the core_contentbank subsystem.
19 *
20 * @package core_contentbank
21 * @category test
22 * @copyright 2020 Sara Arjona <sara@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_contenttype.php');
29
30/**
31 * Generator for the core_contentbank subsystem.
32 *
33 * @package core_contentbank
34 * @copyright 2020 Sara Arjona <sara@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_contentbank_generator extends \component_generator_base {
38
39 /**
40 * Populate contentbank database tables with relevant data to simulate the process of adding items to the content bank.
41 *
42 * @param string $contenttype Content bank plugin type to add. If none is defined, contenttype_testable is used.
43 * @param int $itemstocreate Number of items to add to the content bank.
44 * @param int $userid The user identifier creating the content.
45 * @param context $context The context where the content will be created.
46 * @param bool $convert2class Whether the class should return stdClass or plugin instance.
47 * @param string $filepath The filepath of the file associated to the content to create.
48 * @return array An array with all the records added to the content bank.
49 */
50 public function generate_contentbank_data(?string $contenttype, int $itemstocreate = 1, int $userid = 0,
51 ?\context $context = null, bool $convert2class = true, string $filepath = 'contentfile.h5p'): array {
52 global $DB, $USER;
53
54 $records = [];
55
56 $contenttype = $contenttype ?? 'contenttype_testable';
57 $contenttypeclass = "\\$contenttype\\contenttype";
58 if (!class_exists($contenttypeclass)) {
59 // Early return with empty array because the contenttype doesn't exist.
60 return $records;
61 }
62 if (empty($context)) {
63 $context = \context_system::instance();
64 }
65 $type = new $contenttypeclass($context);
66 $fs = get_file_storage();
67 for ($i = 0; $i < $itemstocreate; $i++) {
68 // Create content.
69 $record = new stdClass();
70 $record->name = 'Test content ' . $i;
71 $record->configdata = '';
72 $record->usercreated = $userid ?? $USER->id;
73
74 $content = $type->create_content($record);
75 $record = $content->get_content();
76
77 // Create a dummy file.
78 $filerecord = array(
79 'contextid' => $context->id,
80 'component' => 'contentbank',
81 'filearea' => 'public',
82 'itemid' => $record->id,
83 'filepath' => '/',
84 'filename' => basename($filepath)
85 );
86 if (file_exists($filepath)) {
87 $fs->create_file_from_pathname($filerecord, $filepath);
88 } else {
89 $fs->create_file_from_string($filerecord, 'Dummy content ' . $i);
90 }
91
92 // Prepare the return value.
93 if ($convert2class) {
94 $records[$record->id] = $content;
95 } else {
96 $records[$record->id] = $record;
97 }
98 }
99
100 return $records;
101 }
102}