1 |
efrain |
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 |
namespace mod_data;
|
|
|
17 |
|
|
|
18 |
use mod_data\external\record_exporter;
|
|
|
19 |
|
|
|
20 |
defined('MOODLE_INTERNAL') || die();
|
|
|
21 |
|
|
|
22 |
global $CFG;
|
|
|
23 |
require_once($CFG->dirroot . '/mod/data/locallib.php');
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Unit tests for locallib.php
|
|
|
27 |
*
|
|
|
28 |
* @package mod_data
|
|
|
29 |
* @copyright 2022 Laurent David <laurent.david@moodle.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
*/
|
|
|
32 |
class locallib_test extends \advanced_testcase {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Confirms that search is working
|
|
|
36 |
* @covers ::data_search_entries
|
|
|
37 |
*/
|
11 |
efrain |
38 |
public function test_data_search_entries(): void {
|
1 |
efrain |
39 |
$this->resetAfterTest();
|
|
|
40 |
$this->setAdminUser();
|
|
|
41 |
$course = $this->getDataGenerator()->create_course();
|
|
|
42 |
$record = new \stdClass();
|
|
|
43 |
$record->course = $course->id;
|
|
|
44 |
$record->name = "Mod data delete test";
|
|
|
45 |
$record->intro = "Some intro of some sort";
|
|
|
46 |
|
|
|
47 |
$module = $this->getDataGenerator()->create_module('data', $record);
|
|
|
48 |
$titlefield = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field(
|
|
|
49 |
(object) [
|
|
|
50 |
'name' => 'title',
|
|
|
51 |
'type' => 'text',
|
|
|
52 |
'required' => 1
|
|
|
53 |
],
|
|
|
54 |
$module);
|
|
|
55 |
$captionfield = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field(
|
|
|
56 |
(object) [
|
|
|
57 |
'name' => 'caption',
|
|
|
58 |
'type' => 'text',
|
|
|
59 |
'required' => 1
|
|
|
60 |
],
|
|
|
61 |
$module);
|
|
|
62 |
$this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($module, [
|
|
|
63 |
$titlefield->field->id => 'Entry 1',
|
|
|
64 |
$captionfield->field->id => 'caption'
|
|
|
65 |
]);
|
|
|
66 |
$this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry($module, [
|
|
|
67 |
$titlefield->field->id => 'Entry 2',
|
|
|
68 |
$captionfield->field->id => ''
|
|
|
69 |
]);
|
|
|
70 |
$cm = get_coursemodule_from_id('data', $module->cmid);
|
|
|
71 |
// Search for entries without any search query set, we should return them all.
|
|
|
72 |
list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
|
|
|
73 |
data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0);
|
|
|
74 |
$this->assertCount(2, $records);
|
|
|
75 |
// Search for entries for "caption" we should return only one of them.
|
|
|
76 |
list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
|
|
|
77 |
data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0, 'caption');
|
|
|
78 |
$this->assertCount(1, $records);
|
|
|
79 |
// Same search but we order by title.
|
|
|
80 |
list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
|
|
|
81 |
data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0, 'caption',
|
|
|
82 |
$titlefield->field->id, 'ASC');
|
|
|
83 |
$this->assertCount(1, $records);
|
|
|
84 |
$this->assert_record_entries_contains($records, $captionfield->field->id, 'caption');
|
|
|
85 |
|
|
|
86 |
// Now with advanced search.
|
|
|
87 |
$defaults = [];
|
|
|
88 |
$fn = $ln = ''; // Defaults for first and last name.
|
|
|
89 |
// Force value for advanced search.
|
|
|
90 |
$_GET['f_' . $captionfield->field->id] = 'caption';
|
|
|
91 |
list($searcharray, $searchtext) = data_build_search_array($module, false, [], $defaults, $fn, $ln);
|
|
|
92 |
list($records, $maxcount, $totalcount, $page, $nowperpage, $sort, $mode) =
|
|
|
93 |
data_search_entries($module, $cm, \context_course::instance($course->id), 'list', 0, $searchtext,
|
|
|
94 |
$titlefield->field->id, 'ASC', 0, 0, true, $searcharray);
|
|
|
95 |
$this->assertCount(1, $records);
|
|
|
96 |
$this->assert_record_entries_contains($records, $captionfield->field->id, 'caption');
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Assert that all records contains a value for the matching field id.
|
|
|
101 |
*
|
|
|
102 |
* @param array $records
|
|
|
103 |
* @param int $fieldid
|
|
|
104 |
* @param string $content
|
|
|
105 |
* @return void
|
|
|
106 |
*/
|
|
|
107 |
private function assert_record_entries_contains($records, $fieldid, $content) {
|
|
|
108 |
global $DB;
|
|
|
109 |
foreach ($records as $record) {
|
|
|
110 |
$fieldscontent = $DB->get_records('data_content', ['recordid' => $record->id]);
|
|
|
111 |
foreach ($fieldscontent as $fieldcontent) {
|
|
|
112 |
if ($fieldcontent->id == $fieldid) {
|
|
|
113 |
$this->assertStringContainsString($fieldcontent->content, $content);
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
}
|