Skip to content

Commit 919f596

Browse files
committed
Issue #66: Remove setting and add phpunit test
1 parent f5cdc44 commit 919f596

File tree

4 files changed

+215
-29
lines changed

4 files changed

+215
-29
lines changed

classes/search/cmsfield.php

+10-11
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,15 @@ public function get_document_recordset($modifiedfrom = 0, \context $context = nu
5959
return null;
6060
}
6161

62-
$searcharea = explode(',', get_config('mod_cms', 'search_area'));
63-
if (empty($searcharea)) {
64-
return null;
65-
}
66-
list($insql, $inparams) = $DB->get_in_or_equal($searcharea);
67-
68-
$sql = "SELECT mcd.*, mc.id AS cmsid, mc.course AS courseid
62+
$sql = "SELECT mcd.*, mc.id AS cmsid, mc.course AS courseid, mcf.name AS fieldname
6963
FROM {customfield_data} mcd
7064
JOIN {cms} mc ON mc.id = mcd.instanceid
65+
JOIN {customfield_field} mcf ON mcf.id = mcd.fieldid
66+
JOIN {customfield_category} mcc ON mcf.categoryid = mcc.id
7167
$contextjoin
72-
WHERE mcd.timemodified >= ? AND mcd.fieldid " . $insql . " ORDER BY mcd.timemodified ASC";
73-
return $DB->get_recordset_sql($sql, array_merge($contextparams, [$modifiedfrom], $inparams));
68+
WHERE mcd.timemodified >= ? AND mcc.component = 'mod_cms' AND mcc.area = 'cmsfield'
69+
ORDER BY mcd.timemodified ASC";
70+
return $DB->get_recordset_sql($sql, array_merge($contextparams, [$modifiedfrom]));
7471
}
7572

7673
/**
@@ -97,7 +94,7 @@ public function get_document($record, $options = []) {
9794

9895
// Prepare associative array with data from DB.
9996
$doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
100-
$doc->set('title', content_to_text($record->value, false));
97+
$doc->set('title', content_to_text($record->fieldname, false));
10198
$doc->set('content', content_to_text($record->value, $record->valueformat));
10299
$doc->set('contextid', $context->id);
103100
$doc->set('courseid', $record->courseid);
@@ -150,7 +147,9 @@ public function check_access($id) {
150147
* @return \moodle_url
151148
*/
152149
public function get_doc_url(\core_search\document $doc) {
153-
return $this->get_context_url($doc);
150+
$contextmodule = \context::instance_by_id($doc->get('contextid'));
151+
$cm = get_coursemodule_from_id('cms', $contextmodule->instanceid, $doc->get('courseid'), true);
152+
return new \moodle_url('/course/view.php', ['id' => $doc->get('courseid'), 'section' => $cm->sectionnum]);
154153
}
155154

156155
/**

settings.php

-18
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,5 @@
5656

5757
$ADMIN->add('modcmsfolder', $settings);
5858

59-
$settings = new admin_settingpage('mod_cms_search_settings', new lang_string('search:settings', 'mod_cms'));
60-
if ($ADMIN->fulltree) {
61-
$sql = "SELECT mcf.id, mcf.name
62-
FROM {customfield_category} mcc
63-
JOIN {customfield_field} mcf ON mcf.categoryid = mcc.id
64-
WHERE mcc.component = 'mod_cms' AND mcc.area = 'cmsfield' AND mcf.type IN ('text', 'textarea')";
65-
$areas = $DB->get_records_sql_menu($sql);
66-
67-
$settings->add(new admin_setting_configmulticheckbox(
68-
'mod_cms/search_area',
69-
get_string('search:settings:area', 'mod_cms'),
70-
get_string('search:settings:area_desc', 'mod_cms'),
71-
null,
72-
$areas
73-
));
74-
}
75-
$ADMIN->add('modcmsfolder', $settings);
76-
7759
// Tell core we already added the settings structure.
7860
$settings = null;

tests/generator/lib.php

+20
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use mod_cms\local\model\cms_types;
18+
use mod_cms\customfield\cmsfield_handler;
1819
use core_customfield\category_controller;
1920
use core_customfield\field_controller;
2021

@@ -47,6 +48,25 @@ public function create_instance($record = null, array $options = null) {
4748
return parent::create_instance($record, (array) $options);
4849
}
4950

51+
/**
52+
* Create new cms module instance with custom data
53+
*
54+
* @param array|stdClass $record
55+
* @param null|array $options
56+
* @return stdClass
57+
*/
58+
public function create_instance_with_data($record = null, ?array $options = null): object {
59+
$record = (object)$record;
60+
$cms = parent::create_instance($record, $options);
61+
62+
// Save customfield.
63+
$handler = cmsfield_handler::create($cms->typeid);
64+
$record->id = $cms->id;
65+
$handler->instance_form_save($record);
66+
67+
return $cms;
68+
}
69+
5070
/**
5171
* Get generator for custom fields.
5272
* @return core_customfield_generator

tests/search/search_test.php

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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+
* CMS search unit tests.
19+
*
20+
* @package mod_cms
21+
* @category test
22+
* @author Tomo Tsuyuki <[email protected]>
23+
* @copyright 2024 Catalyst IT
24+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
* @covers \mod_cms\search\cms
26+
*/
27+
28+
namespace mod_cms\search;
29+
30+
use mod_cms\local\model\cms_types;
31+
use mod_cms\customfield\cmsfield_handler;
32+
33+
defined('MOODLE_INTERNAL') || die();
34+
35+
global $CFG;
36+
require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
37+
38+
class search_test extends \advanced_testcase {
39+
40+
/**
41+
* @var string Area id
42+
*/
43+
protected $cmsareaid = null;
44+
45+
/**
46+
* Set up.
47+
*/
48+
public function setUp(): void {
49+
$this->resetAfterTest();
50+
set_config('enableglobalsearch', true);
51+
52+
$this->cmsareaid = \core_search\manager::generate_areaid('mod_cms', 'cmsfield');
53+
54+
// Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
55+
$search = \testable_core_search::instance();
56+
}
57+
58+
/**
59+
* Test search enabled.
60+
*
61+
* @return void
62+
*/
63+
public function test_search_enabled() {
64+
65+
$searcharea = \core_search\manager::get_search_area($this->cmsareaid);
66+
list($componentname, $varname) = $searcharea->get_config_var_name();
67+
68+
// Enabled by default once global search is enabled.
69+
$this->assertTrue($searcharea->is_enabled());
70+
71+
set_config($varname . '_enabled', 0, $componentname);
72+
$this->assertFalse($searcharea->is_enabled());
73+
74+
set_config($varname . '_enabled', 1, $componentname);
75+
$this->assertTrue($searcharea->is_enabled());
76+
}
77+
78+
/**
79+
* Indexing mod cms contents.
80+
*
81+
* @return void
82+
*/
83+
public function test_get_document_recordset() {
84+
global $DB;
85+
86+
// Returns the instance as long as the area is supported.
87+
$searcharea = \core_search\manager::get_search_area($this->cmsareaid);
88+
$this->assertInstanceOf('\mod_cms\search\cmsfield', $searcharea);
89+
90+
$course = self::getDataGenerator()->create_course();
91+
92+
$cmstype = new cms_types();
93+
$cmstype->set('name', 'Overview')
94+
->set('idnumber', 'overview')
95+
->set('title_mustache', 'Overview');
96+
$cmstype->save();
97+
98+
$fieldcategory = self::getDataGenerator()->create_custom_field_category([
99+
'name' => 'Other fields',
100+
'component' => 'mod_cms',
101+
'area' => 'cmsfield',
102+
'itemid' => $cmstype->get('id'),
103+
]);
104+
$field = self::getDataGenerator()->create_custom_field([
105+
'name' => 'Overview',
106+
'shortname' => 'overview',
107+
'type' => 'text',
108+
'categoryid' => $fieldcategory->get('id'),
109+
]);
110+
111+
// Name for cms is coming from "title_mustache" in cms_type.
112+
$generator = self::getDataGenerator()->get_plugin_generator('mod_cms');
113+
$record = new \stdClass();
114+
$record->course = $course->id;
115+
$record->customfield_overview = 'Test overview text 1';
116+
$record->typeid = $cmstype->get('id');
117+
$generator->create_instance_with_data($record);
118+
119+
$record = new \stdClass();
120+
$record->course = $course->id;
121+
$record->customfield_overview = 'Test overview text 2';
122+
$record->typeid = $cmstype->get('id');
123+
$generator->create_instance_with_data($record);
124+
125+
// All records.
126+
$recordset = $searcharea->get_document_recordset();
127+
$this->assertTrue($recordset->valid());
128+
foreach ($recordset as $record) {
129+
$this->assertInstanceOf('stdClass', $record);
130+
$data = $DB->get_record('customfield_data', ['id' => $record->id]);
131+
$doc = $searcharea->get_document($record);
132+
$this->assertInstanceOf('\core_search\document', $doc);
133+
$this->assertEquals('mod_cms-cmsfield-' . $data->id, $doc->get('id'));
134+
$this->assertEquals($data->id, $doc->get('itemid'));
135+
$this->assertEquals($course->id, $doc->get('courseid'));
136+
$this->assertEquals($data->contextid, $doc->get('contextid'));
137+
$this->assertEquals($field->get('name'), $doc->get('title'));
138+
$this->assertEquals($data->value, $doc->get('content'));
139+
140+
// Static caches are working.
141+
$dbreads = $DB->perf_get_reads();
142+
$doc = $searcharea->get_document($record);
143+
$this->assertEquals($dbreads, $DB->perf_get_reads());
144+
$this->assertInstanceOf('\core_search\document', $doc);
145+
}
146+
$recordset->close();
147+
148+
// The +2 is to prevent race conditions.
149+
$recordset = $searcharea->get_document_recordset(time() + 2);
150+
151+
// No new records.
152+
$this->assertFalse($recordset->valid());
153+
$recordset->close();
154+
155+
// Wait 1 sec to have new search string.
156+
sleep(1);
157+
$record = new \stdClass();
158+
$record->course = $course->id;
159+
$record->customfield_overview = 'Test overview text 3';
160+
$record->typeid = $cmstype->get('id');
161+
$generator->create_instance_with_data($record);
162+
163+
// Return only new search.
164+
$recordset = $searcharea->get_document_recordset(time());
165+
foreach ($recordset as $record) {
166+
$this->assertInstanceOf('stdClass', $record);
167+
$data = $DB->get_record('customfield_data', ['id' => $record->id]);
168+
$doc = $searcharea->get_document($record);
169+
$this->assertInstanceOf('\core_search\document', $doc);
170+
$this->assertEquals('mod_cms-cmsfield-' . $data->id, $doc->get('id'));
171+
$this->assertEquals($data->id, $doc->get('itemid'));
172+
$this->assertEquals($course->id, $doc->get('courseid'));
173+
$this->assertEquals($data->contextid, $doc->get('contextid'));
174+
$this->assertEquals($field->get('name'), $doc->get('title'));
175+
$this->assertEquals($data->value, $doc->get('content'));
176+
177+
// Static caches are working.
178+
$dbreads = $DB->perf_get_reads();
179+
$doc = $searcharea->get_document($record);
180+
$this->assertEquals($dbreads, $DB->perf_get_reads());
181+
$this->assertInstanceOf('\core_search\document', $doc);
182+
}
183+
$recordset->close();
184+
}
185+
}

0 commit comments

Comments
 (0)