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