-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathHasOperatingSystemCapacityTest.php
More file actions
460 lines (406 loc) · 15.1 KB
/
HasOperatingSystemCapacityTest.php
File metadata and controls
460 lines (406 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
namespace tests\units\Glpi\Asset\Capacity;
use DisplayPreference;
use Glpi\Asset\Asset;
use Glpi\Asset\Capacity;
use Glpi\Asset\Capacity\HasHistoryCapacity;
use Glpi\Asset\Capacity\HasOperatingSystemCapacity;
use Glpi\Tests\DbTestCase;
use Glpi\Tests\Glpi\Asset\CapacityUsageTestTrait;
use Item_OperatingSystem;
use Log;
class HasOperatingSystemCapacityTest extends DbTestCase
{
use CapacityUsageTestTrait;
protected function getTargetCapacity(): string
{
return HasOperatingSystemCapacity::class;
}
/**
* Test that the capacity is properly registered in the configuration
* when enabled and unregistered when disabled.
*
* @return void
*/
public function testConfigRegistration(): void
{
global $CFG_GLPI;
// Create custom asset definition
$definition = $this->initAssetDefinition();
$class = $definition->getAssetClassName();
// The capacity is not yet enabled, the itemtype should not be
// registered in $CFG_GLPI["operatingsystem_types"]
$this->assertNotContains($class, $CFG_GLPI["operatingsystem_types"]);
// Enable capacity, the itemtype should now be registered
$definition = $this->enableCapacity(
$definition,
$this->getTargetCapacity()
);
$this->assertContains($class, $CFG_GLPI["operatingsystem_types"]);
// Disable capacity, the itemtype should no longer be registered
$definition = $this->disableCapacity(
$definition,
$this->getTargetCapacity()
);
$this->assertNotContains($class, $CFG_GLPI["operatingsystem_types"]);
}
/**
* Test that the "Operating system" tab is registered when the capacity
* is enabled.
*
* @return void
*/
public function testOperatingSystemTabRegistration(): void
{
// Create custom asset definition
$definition = $this->initAssetDefinition();
$class = $definition->getAssetClassName();
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
// Validate that the subject does not have the operating system tab,
// as the capacity is not enabled yet
$tab_name = "Item_OperatingSystem$1";
$this->assertArrayNotHasKey($tab_name, $subject->defineAllTabs());
// Enable capacity, the tab should now be registered
$definition = $this->enableCapacity(
$definition,
$this->getTargetCapacity()
);
$this->assertArrayHasKey($tab_name, $subject->defineAllTabs());
// Disable capacity, the tab should no longer be registered
// INFO: Can't test this case because tabs are not unregistered
// immediately, but only after a page refresh (which we can't simulate
// here).
// Could be done if we implement the code for it but not very useful
// since tabs are not used from the CLI context
}
/**
* Test that the "Item_OperatingSystem" search options are registered when
* the capacity is enabled and unregistered when disabled.
*
* @return void
*/
public function testSearchOptionRegistration(): void
{
// Create custom asset definition
$definition = $this->initAssetDefinition();
$class = $definition->getAssetClassName();
// Check that we have some valid search options to add
$item_os = new Item_OperatingSystem();
$count_to_add = count($item_os::rawSearchOptionsToAdd($class));
$this->assertGreaterThan(0, $count_to_add);
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
$base_search_options_count = count($subject->rawSearchOptions());
// Enable capactity, search option count should increase
$definition = $this->enableCapacity(
$definition,
$this->getTargetCapacity()
);
$this->assertCount(
$base_search_options_count + $count_to_add,
$subject->rawSearchOptions()
);
// Disable capacity, search option count should decrease back to base
$definition = $this->disableCapacity(
$definition,
$this->getTargetCapacity()
);
$this->assertCount(
$base_search_options_count,
$subject->rawSearchOptions()
);
}
/**
* Test that any "Item_OperatingSystem" items linked to the asset are
* deleted when the capacity is disabled.
*
* @return void
*/
public function testItemOperatingSystemDataDeletion(): void
{
// Create custom asset definition with the target capacity enabled
$definition = $this->initAssetDefinition(
capacities: [new Capacity($this->getTargetCapacity())]
);
$class = $definition->getAssetClassName();
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
// Create an Item_OperatingSystem linked to our asset
$this->createItem(Item_OperatingSystem::class, [
'itemtype' => $subject::getType(),
'items_id' => $subject->getID(),
'license_number' => '012345',
]);
// Ensure item is properly linked to our subject
$items = (new Item_OperatingSystem())->find([
'itemtype' => $subject::getType(),
'items_id' => $subject->getID(),
]);
$this->assertCount(1, $items);
// Disable capacity, linked item should be deleted
$definition = $this->disableCapacity(
$definition,
$this->getTargetCapacity()
);
$items = (new Item_OperatingSystem())->find([
'itemtype' => $subject::getType(),
'items_id' => $subject->getID(),
]);
$this->assertCount(0, $items);
}
/**
* Test that any history entries related to "Item_OperatingSystem" and
* "OperatingSystem" are deleted when the capacity is disabled.
*
* @return void
*/
public function testHistoryDataDeletion(): void
{
// Create custom asset definition with the target capacity enabled
$definition = $this->initAssetDefinition(
capacities: [
new Capacity(name: $this->getTargetCapacity()),
new Capacity(name: HasHistoryCapacity::class),
]
);
$class = $definition->getAssetClassName();
// Create our test subject
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
// Create and update the linked OS in order to generate history entries
$item = $this->createItem(Item_OperatingSystem::class, [
'itemtype' => $subject::getType(),
'items_id' => $subject->getID(),
'license_number' => '012345',
]);
$this->updateItem($item::getType(), $item->getId(), [
'license_number' => '0123456',
]);
$this->updateItem($item::getType(), $item->getId(), [
'license_number' => '01234567',
]);
// Also update some internal fields to make sure they are not deleted
// when the capacity is disabled
$this->updateItem($subject::getType(), $subject->getId(), [
'name' => 'Test asset (edited)',
]);
// Check logs number:
// - 1 log for $subject creation
// - 1 log for $subject update
// - 1 log for $item creation
// - 2 log for $item update
$count_logs = countElementsInTable(Log::getTable(), [
'itemtype' => $class,
]);
$this->assertEquals(5, $count_logs);
// Disable capacity, history entries related to "Item_OperatingSystem"
// and "OperatingSystem" should be deleted
$definition = $this->disableCapacity(
$definition,
$this->getTargetCapacity()
);
$count_logs = countElementsInTable(Log::getTable(), [
'itemtype' => $class,
]);
$this->assertEquals(2, $count_logs); // $subject creation + update
}
/**
* Test that any display preferences entries related to
* "Item_OperatingSystem" and "OperatingSystem" are deleted when the
* capacity is disabled.
*
* @return void
*/
public function testDisplayPreferencesDataDeletion(): void
{
// Create custom asset definition with the target capacity enabled
$definition = $this->initAssetDefinition(
capacities: [new Capacity(name: $this->getTargetCapacity())]
);
$class = $definition->getAssetClassName();
// Create our test subject and enable the capacity
$subject = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
// Set display preferences
$this->createItems(DisplayPreference::class, [
[
'itemtype' => $subject::getType(),
'num' => '45', // Linked OS name
'users_id' => 0,
],
[
'itemtype' => $subject::getType(),
'num' => '46', // Linked OS version
'users_id' => 0,
],
]);
// Count display preferences, should be 9 (2 for OS + 7 for asset)
$count_display_preferences = countElementsInTable(
DisplayPreference::getTable(),
[
'itemtype' => $subject::getType(),
]
);
$this->assertEquals(9, $count_display_preferences);
// Disable capacity, display preferences related to OS should be
// deleted while display preferences related to the asset should not be
// deleted
// The two OS search options should be deleted
$definition = $this->disableCapacity(
$definition,
$this->getTargetCapacity()
);
$count_display_preferences = countElementsInTable(
DisplayPreference::getTable(),
[
'itemtype' => $subject::getType(),
]
);
$this->assertEquals(7, $count_display_preferences);
}
public function testCloneAsset()
{
$definition = $this->initAssetDefinition(
capacities: [new Capacity(name: HasOperatingSystemCapacity::class)]
);
$class = $definition->getAssetClassName();
$entity = $this->getTestRootEntity(true);
/** @var Asset $asset */
$asset = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $entity,
]);
$this->createItem(Item_OperatingSystem::class, [
'itemtype' => $asset::getType(),
'items_id' => $asset->getID(),
'license_number' => '012345',
]);
$this->assertGreaterThan(0, $clone_id = $asset->clone());
$this->assertCount(
1,
getAllDataFromTable(Item_OperatingSystem::getTable(), [
'itemtype' => $asset::getType(),
'items_id' => $clone_id,
'license_number' => '012345',
])
);
}
/**
* @dataProvider provideIsUsed
*/
public function testIsUsed(string $target_classname): void
{
$definition = $this->initAssetDefinition(
capacities: [new Capacity($this->getTargetCapacity())]
);
$class = $definition->getAssetClassName();
$asset = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
$capacity_class = $this->getTargetCapacity();
$capacity = new $capacity_class();
$this->assertFalse($capacity->isUsed($class));
// An OS capacity is used only if a meaningful OS exists
$os = $this->createItem('OperatingSystem', [
'name' => 'Test OS',
]);
$input = [
'itemtype' => $asset::getType(),
'items_id' => $asset->getID(),
'operatingsystems_id' => $os->getID(),
];
$this->createItem($target_classname, $input);
$this->assertTrue($capacity->isUsed($class));
}
/**
* @dataProvider provideGetCapacityUsageDescription
*/
public function testGetCapacityUsageDescription(string $target_classname, string $expected): void
{
$definition = $this->initAssetDefinition(
capacities: [new Capacity($this->getTargetCapacity())]
);
$class = $definition->getAssetClassName();
$capacity = new ($this->getTargetCapacity());
$this->assertEquals(
sprintf($expected, 0, 0),
$capacity->getCapacityUsageDescription($class)
);
$asset = $this->createItem($class, [
'name' => 'Test asset',
'entities_id' => $this->getTestRootEntity(true),
]);
// An OS capacity is used only if a meaningful OS exists
$os = $this->createItem('OperatingSystem', [
'name' => 'Test OS',
]);
$this->createItem($target_classname, [
'itemtype' => $class,
'items_id' => $asset->getID(),
'operatingsystems_id' => $os->getID(),
]);
$this->assertEquals(
sprintf($expected, 1, 1),
$capacity->getCapacityUsageDescription($class)
);
}
public static function provideIsUsed(): iterable
{
yield [
'target_classname' => Item_OperatingSystem::class,
];
}
public static function provideGetCapacityUsageDescription(): iterable
{
yield [
'target_classname' => Item_OperatingSystem::class,
'expected' => 'Used by %d of %d assets',
];
}
}