Skip to content

Commit b692f27

Browse files
authored
MBS-10395: Remove tools from db when uninstalling subplugin (#107)
1 parent a12b0f8 commit b692f27

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

classes/plugininfo/aitool.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public function uninstall(\progress_trace $progress) {
127127
return true;
128128
}
129129
$sqllike = $DB->sql_like('configkey', '?');
130-
$params = ['purpose_%_tool'];
130+
$underscoreescaped = $DB->sql_like_escape('_');
131+
$params = ["purpose{$underscoreescaped}%{$underscoreescaped}tool%"];
131132
$select = $sqllike;
132133
[$insql, $inparams] = $DB->get_in_or_equal($deletedinstanceids);
133134
$params = array_merge($params, $inparams);

tests/plugininfo/aitool_test.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
namespace local_ai_manager\plugininfo;
18+
19+
use advanced_testcase;
20+
21+
/**
22+
* Test class for the pluginfo/aitool functions.
23+
*
24+
* @package local_ai_manager
25+
* @copyright 2026 ISB Bayern
26+
* @author Johannes Funk
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
final class aitool_test extends advanced_testcase {
30+
/**
31+
* Tests the method uninstall
32+
*
33+
* @covers \local_ai_manager\plugininfo\aitool::uninstall
34+
*/
35+
public function test_uninstall(): void {
36+
global $DB;
37+
38+
$this->resetAfterTest();
39+
40+
$connectorname = "testconnector";
41+
42+
$instance = [
43+
'connector' => $connectorname,
44+
];
45+
46+
$id1 = $DB->insert_record('local_ai_manager_instance', (object)$instance);
47+
$id2 = $DB->insert_record('local_ai_manager_instance', (object)$instance);
48+
// Fake id not equal to either id1 or id2.
49+
$otherid = $id1 + $id2;
50+
51+
// Generate three example configs: one to be deleted, two to be kept.
52+
$config1delete = [
53+
'configkey' => 'purpose_somepurpose_tool_role_somerole',
54+
'configvalue' => $id1,
55+
];
56+
$config2keep = [
57+
'configkey' => 'another_configkey',
58+
'configvalue' => $id2,
59+
];
60+
$config3keep = [
61+
'configkey' => 'purpose_somepurpose_tool_role_somerole',
62+
'configvalue' => $otherid,
63+
];
64+
65+
$DB->insert_record('local_ai_manager_config', $config1delete);
66+
$DB->insert_record('local_ai_manager_config', $config2keep);
67+
$DB->insert_record('local_ai_manager_config', $config3keep);
68+
69+
// Assert all records exist before uninstall.
70+
$this->assertTrue($DB->record_exists('local_ai_manager_instance', ['id' => $id1, 'connector' => $connectorname]));
71+
$this->assertTrue($DB->record_exists('local_ai_manager_instance', ['id' => $id2, 'connector' => $connectorname]));
72+
$this->assertTrue($DB->record_exists('local_ai_manager_config', $config1delete));
73+
$this->assertTrue($DB->record_exists('local_ai_manager_config', $config2keep));
74+
$this->assertTrue($DB->record_exists('local_ai_manager_config', $config3keep));
75+
76+
$pluginfo = new aitool();
77+
$pluginfo->name = $connectorname;
78+
79+
$result = $pluginfo->uninstall(new \null_progress_trace());
80+
$this->assertTrue($result);
81+
82+
// No more instances of this connector.
83+
$instancecount = $DB->count_records('local_ai_manager_instance', $instance);
84+
$this->assertEquals(0, $instancecount);
85+
86+
$this->assertEquals(0, $DB->count_records('local_ai_manager_config', $config1delete));
87+
$this->assertEquals(1, $DB->count_records('local_ai_manager_config', $config2keep));
88+
$this->assertEquals(1, $DB->count_records('local_ai_manager_config', $config3keep));
89+
}
90+
}

0 commit comments

Comments
 (0)