ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilPluginStateDBOverIlDBInterfaceTest.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\TestCase;
20use ILIAS\Data;
21
23{
24 public static array $plugin_data = [
25 [
26 "plugin_id" => "plg1",
27 "active" => true,
28 "last_update_version" => "1.0.1",
29 "db_version" => 12
30 ],
31 [
32 "plugin_id" => "plg2",
33 "active" => false,
34 "last_update_version" => "2.3.4",
35 "db_version" => 0
36 ]
37 ];
38
39 protected \ilDBInterface $il_db;
40 protected Data\Factory $data_factory;
41 protected \ilPluginStateDBOverIlDBInterface $db;
42
43 protected function setUp(): void
44 {
45 $this->il_db = $this->createMock(\ilDBInterface::class);
46 $this->data_factory = new Data\Factory();
47 $this->db = new \ilPluginStateDBOverIlDBInterface(
48 $this->data_factory,
49 $this->il_db
50 );
51 }
52
53 public function testIsPluginActivated(): void
54 {
55 $handle = $this->createMock(\ilDBStatement::class);
56
57 $this->il_db->expects($this->once())
58 ->method("query")
59 ->with("SELECT * FROM il_plugin")
60 ->willReturn($handle);
61 $this->il_db->expects($this->once())
62 ->method("fetchAll")
63 ->with($handle)
64 ->willReturn(self::$plugin_data);
65
66 $this->assertTrue($this->db->isPluginActivated("plg1"));
67 $this->assertFalse($this->db->isPluginActivated("plg2"));
68 $this->assertFalse($this->db->isPluginActivated("plg3"));
69 }
70
71 public function testGetCurrentPluginVersion(): void
72 {
73 $handle = $this->createMock(\ilDBStatement::class);
74
75 $this->il_db->expects($this->once())
76 ->method("query")
77 ->with("SELECT * FROM il_plugin")
78 ->willReturn($handle);
79 $this->il_db->expects($this->once())
80 ->method("fetchAll")
81 ->with($handle)
82 ->willReturn(self::$plugin_data);
83
84 $this->assertEquals($this->data_factory->version("1.0.1"), $this->db->getCurrentPluginVersion("plg1"));
85 $this->assertEquals($this->data_factory->version("2.3.4"), $this->db->getCurrentPluginVersion("plg2"));
86 $this->assertEquals(null, $this->db->getCurrentPluginVersion("plg3"));
87 }
88
89 public function testGetCurrentPluginDBVersion(): void
90 {
91 $handle = $this->createMock(\ilDBStatement::class);
92
93 $this->il_db->expects($this->once())
94 ->method("query")
95 ->with("SELECT * FROM il_plugin")
96 ->willReturn($handle);
97 $this->il_db->expects($this->once())
98 ->method("fetchAll")
99 ->with($handle)
100 ->willReturn(self::$plugin_data);
101
102 $this->assertEquals(12, $this->db->getCurrentPluginDBVersion("plg1"));
103 $this->assertEquals(0, $this->db->getCurrentPluginDBVersion("plg2"));
104 $this->assertEquals(null, $this->db->getCurrentPluginVersion("plg3"));
105 }
106
108 {
109 $handle = $this->createMock(\ilDBStatement::class);
110
111 $this->il_db->expects($this->once())
112 ->method("query")
113 ->with("SELECT * FROM il_plugin")
114 ->willReturn($handle);
115 $this->il_db->expects($this->once())
116 ->method("fetchAll")
117 ->with($handle)
118 ->willReturn(self::$plugin_data);
119
120 $PLUGIN_ID = "plg2";
121 $VERSION = $this->data_factory->version("1.0.0");
122 $DB_VERSION = 23;
123
124 $this->il_db->expects($this->once())
125 ->method("update")
126 ->with(
127 "il_plugin",
128 [
129 "last_update_version" => ["text", (string) $VERSION],
130 "db_version" => ["integer", $DB_VERSION]
131 ],
132 [
133 "plugin_id" => ["text", $PLUGIN_ID]
134 ]
135 );
136
137 $this->db->setCurrentPluginVersion($PLUGIN_ID, $VERSION, $DB_VERSION);
138 }
139
141 {
142 $handle = $this->createMock(\ilDBStatement::class);
143
144 $this->il_db->expects($this->once())
145 ->method("query")
146 ->with("SELECT * FROM il_plugin")
147 ->willReturn($handle);
148 $this->il_db->expects($this->once())
149 ->method("fetchAll")
150 ->with($handle)
151 ->willReturn(self::$plugin_data);
152
153 $PLUGIN_ID = "plg3";
154 $VERSION = $this->data_factory->version("1.0.0");
155 $DB_VERSION = 23;
156
157 $this->il_db->expects($this->once())
158 ->method("insert")
159 ->with(
160 "il_plugin",
161 [
162 "plugin_id" => ["text", $PLUGIN_ID],
163 "active" => ["integer", 0],
164 "last_update_version" => ["text", (string) $VERSION],
165 "db_version" => ["integer", $DB_VERSION]
166 ]
167 );
168
169 $this->db->setCurrentPluginVersion($PLUGIN_ID, $VERSION, $DB_VERSION);
170 }
171
173 {
174 $this->expectException(\InvalidArgumentException::class);
175 $this->db->setActivation("SOME_ID", true);
176 }
177
178 public function testSetActivationTrue(): void
179 {
180 $handle = $this->createMock(\ilDBStatement::class);
181
182 $this->il_db->expects($this->once())
183 ->method("query")
184 ->with("SELECT * FROM il_plugin")
185 ->willReturn($handle);
186 $this->il_db->expects($this->once())
187 ->method("fetchAll")
188 ->with($handle)
189 ->willReturn(self::$plugin_data);
190
191 $PLUGIN_ID = "plg1";
192
193 $this->il_db->expects($this->once())
194 ->method("update")
195 ->with(
196 "il_plugin",
197 [
198 "active" => ["integer", 1],
199 ],
200 [
201 "plugin_id" => ["text", $PLUGIN_ID],
202 ]
203 );
204
205 $this->db->setActivation($PLUGIN_ID, true);
206 }
207
208 public function testSetActivationFalse(): void
209 {
210 $handle = $this->createMock(\ilDBStatement::class);
211
212 $this->il_db->expects($this->once())
213 ->method("query")
214 ->with("SELECT * FROM il_plugin")
215 ->willReturn($handle);
216 $this->il_db->expects($this->once())
217 ->method("fetchAll")
218 ->with($handle)
219 ->willReturn(self::$plugin_data);
220
221 $PLUGIN_ID = "plg1";
222
223 $this->il_db->expects($this->once())
224 ->method("update")
225 ->with(
226 "il_plugin",
227 [
228 "active" => ["integer", 0],
229 ],
230 [
231 "plugin_id" => ["text", $PLUGIN_ID],
232 ]
233 );
234
235 $this->db->setActivation($PLUGIN_ID, false);
236 }
237
238
239 public function testRemove(): void
240 {
241 $PLUGIN_ID = "plg1";
242
243 $this->il_db->expects($this->once())
244 ->method("quote")
245 ->with($PLUGIN_ID, "text")
246 ->willReturn("PLUGIN_ID");
247 $this->il_db->expects($this->once())
248 ->method("manipulate")
249 ->with("DELETE FROM il_plugin WHERE plugin_id = PLUGIN_ID");
250
251 $this->db->remove($PLUGIN_ID);
252 }
253}