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