Skip to content

Commit 3dbd9f2

Browse files
committed
Allow plugins to register generic assets capacities
1 parent 0c60f74 commit 3dbd9f2

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

phpunit/DbTestCase.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Glpi\Asset\AssetDefinition;
3838
use Glpi\Asset\AssetDefinitionManager;
3939
use Glpi\Asset\Capacity;
40+
use Glpi\Asset\CapacityConfig;
4041
use Glpi\Dropdown\DropdownDefinition;
4142

4243
class DbTestCase extends \GLPITestCase
@@ -540,13 +541,14 @@ protected function createTxtDocument(): Document
540541
*/
541542
protected function enableCapacity(
542543
AssetDefinition $definition,
543-
string $capacity_classname
544+
string $capacity_classname,
545+
?CapacityConfig $config = new CapacityConfig()
544546
): AssetDefinition {
545547
// Add new capacity
546548
$existing_capacities = $this->callPrivateMethod($definition, 'getDecodedCapacitiesField');
549+
$existing_capacities[] = new Capacity(name: $capacity_classname, config: $config);
547550

548551
$capacity_input = array_map(fn (Capacity $capacity) => $capacity->jsonSerialize(), $existing_capacities);
549-
$capacity_input[] = ['name' => $capacity_classname];
550552

551553
$this->updateItem(
552554
AssetDefinition::class,

phpunit/functional/Glpi/Asset/AssetDefinitionManagerTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
use Change_Item;
3838
use DbTestCase;
39+
use Glpi\Asset\AssetDefinitionManager;
40+
use Glpi\Asset\Capacity;
41+
use Glpi\Asset\Capacity\AbstractCapacity;
42+
use Glpi\Asset\CapacityConfig;
3943
use Item_Problem;
4044
use Item_Ticket;
4145
use Profile;
@@ -214,4 +218,45 @@ public function testCommonITILTabRegistration(): void
214218
$this->assertEquals($expected_tabs, $tabs);
215219
}
216220
}
221+
222+
public function testRegisteredCapacity(): void
223+
{
224+
$definition = $this->initAssetDefinition();
225+
$asset_classname = $definition->getAssetClassName();
226+
227+
$capacity_implementation = $this->createMock(AbstractCapacity::class);
228+
$capacity_config = new CapacityConfig(['foo' => 'bar']);
229+
230+
// Register the capacity.
231+
$manager = AssetDefinitionManager::getInstance();
232+
$manager->registerCapacity($capacity_implementation);
233+
234+
$this->assertContains($capacity_implementation, $manager->getAvailableCapacities());
235+
$this->assertEquals($capacity_implementation, $manager->getCapacity($capacity_implementation::class));
236+
237+
// `onCapacityEnabled` method is executed when the capacity is enabled.
238+
$capacity_implementation->expects($this->once())
239+
->method('onCapacityEnabled')
240+
->with($asset_classname, $this->equalTo($capacity_config));
241+
$this->enableCapacity($definition, $capacity_implementation::class, $capacity_config);
242+
243+
// `onObjectInstanciation` method is executed when an asset constructor is used.
244+
$capacity_implementation->expects($this->once())
245+
->method('onObjectInstanciation')
246+
->with($this->isInstanceOf($asset_classname), $this->equalTo($capacity_config));
247+
new $asset_classname();
248+
249+
// `onCapacityUpdated` method is executed when the capacity config is updated.
250+
$new_config = new CapacityConfig(['bar' => 'baz']);
251+
$capacity_implementation->expects($this->once())
252+
->method('onCapacityUpdated')
253+
->with($asset_classname, $this->equalTo($capacity_config), $this->equalTo($new_config));
254+
$this->enableCapacity($definition, $capacity_implementation::class, $new_config);
255+
256+
// `onCapacityDisabled` method is executed when the capacity is disabled.
257+
$capacity_implementation->expects($this->once())
258+
->method('onCapacityDisabled')
259+
->with($asset_classname, $this->equalTo($new_config));
260+
$this->disableCapacity($definition, $capacity_implementation::class);
261+
}
217262
}

src/Glpi/Asset/AssetDefinitionManager.php

+8
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ public function getAssetTypesClassesNames(bool $with_namespace = true): array
320320
return $classes;
321321
}
322322

323+
/**
324+
* Register a capacity.
325+
*/
326+
public function registerCapacity(CapacityInterface $capacity): void
327+
{
328+
$this->capacities[$capacity::class] = $capacity;
329+
}
330+
323331
/**
324332
* Returns available capacities instances.
325333
*

0 commit comments

Comments
 (0)