Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fix the uploaded file name being lost after the client injection upload step
- Fix models mapped to additional fields (Fields plugin) becoming inaccessible and their values being rejected on injection
- Fix entity injection failing when additional fields (Fields plugin) are mapped
- Fix additional information sharing a field name across itemtypes (e.g. two device designations) overwriting each other, and show the itemtype next to each field
- Stop flagging a network port injection as partial when no connection to a network equipment port is requested
- Made several generic warnings more explicit. They explain why the mapping failed.
Expand Down
5 changes: 4 additions & 1 deletion inc/commoninjectionlib.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,10 @@ private function addNeededFields($injectionClass, $itemtype)

//Add entities_id if itemtype can be assigned to an entity
if ($injectionClass->isEntityAssign()) {
$entities_id = $this->getValueByItemtypeAndName($this->primary_type, 'entities_id');
//An entity belongs to itself: its entities_id field holds its parent
$entities_id = $this->primary_type === Entity::class
? $id
: $this->getValueByItemtypeAndName($this->primary_type, 'entities_id');
$this->setValueForItemtype($itemtype, 'entities_id', $entities_id);
}

Expand Down
103 changes: 73 additions & 30 deletions tests/fixtures/fields_stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,43 @@

//Minimal stand-ins for the Fields plugin classes the rights checks rely on, loaded only when Fields is absent

//Mirrors the injection class Fields generates for each container
trait PluginFieldsStubInjectionTrait
{
public static function getTable($classname = null): string
{
return getTableForItemType(get_parent_class(self::class));
}

public function isPrimaryType(): bool
{
return false;
}

public function getOptions($primary_type = ''): array
{
return [
76665 => [
'table' => self::getTable(),
'field' => self::VALUE_FIELD,
'name' => 'Stub - Value',
'linkfield' => self::VALUE_FIELD,
'datatype' => 'string',
'displaytype' => 'text',
'injectable' => PluginDatainjectionCommonInjectionLib::FIELD_INJECTABLE,
],
];
}

public function addOrUpdateObject($values = [], $options = []): array
{
$lib = new PluginDatainjectionCommonInjectionLib($this, $values, $options);
$lib->processAddOrUpdate();

return $lib->getInjectionResults();
}
}

abstract class PluginFieldsAbstractContainerInstance extends CommonDBChild
{
public static $itemtype = 'itemtype';
Expand All @@ -45,6 +82,20 @@ public static function canUpdate(): bool
{
return false;
}

//Like Fields, the stored entity always follows the parent item, overriding any given one
public function addNeededInfoToInput($input)
{
$parent = static::getItemFromArray(static::$itemtype, static::$items_id, array_merge($this->fields, $input));
if ($parent) {
$input['entities_id'] = $parent->getEntityID();
if ($parent->maybeRecursive()) {
$input['is_recursive'] = (int) $parent->isRecursive();
}
}

return $input;
}
}

class PluginFieldsProfile
Expand All @@ -53,7 +104,9 @@ class PluginFieldsProfile

public static function getRightOnContainer(int $profile_id, int $container_id): int
{
return $container_id === PluginFieldsComputerstub::CONTAINER_ID ? self::$right : 0;
return in_array($container_id, [PluginFieldsComputerstub::CONTAINER_ID, PluginFieldsEntitystub::CONTAINER_ID], true)
? self::$right
: 0;
}
}

Expand All @@ -68,43 +121,33 @@ class PluginFieldsComputerstub extends PluginFieldsAbstractContainerInstance

class PluginFieldsComputerstubInjection extends PluginFieldsComputerstub implements PluginDatainjectionInjectionInterface
{
public static $rightname = 'plugin_datainjection_model';
use PluginFieldsStubInjectionTrait;

public static function getTable($classname = null): string
{
return getTableForItemType(PluginFieldsComputerstub::class);
}

public function isPrimaryType(): bool
{
return false;
}
public static $rightname = 'plugin_datainjection_model';

public function connectedTo(): array
{
return [Computer::class];
}
}

public function getOptions($primary_type = ''): array
{
return [
76665 => [
'table' => self::getTable(),
'field' => self::VALUE_FIELD,
'name' => 'Stub - Value',
'linkfield' => self::VALUE_FIELD,
'datatype' => 'string',
'displaytype' => 'text',
'injectable' => PluginDatainjectionCommonInjectionLib::FIELD_INJECTABLE,
],
];
}
class PluginFieldsEntitystub extends PluginFieldsAbstractContainerInstance
{
public const CONTAINER_ID = 43;

public function addOrUpdateObject($values = [], $options = []): array
{
$lib = new PluginDatainjectionCommonInjectionLib($this, $values, $options);
$lib->processAddOrUpdate();
public const VALUE_FIELD = 'stubvaluefield';

return $lib->getInjectionResults();
public static $rightname = 'entity';
}

class PluginFieldsEntitystubInjection extends PluginFieldsEntitystub implements PluginDatainjectionInjectionInterface
{
use PluginFieldsStubInjectionTrait;

public static $rightname = 'plugin_datainjection_model';

public function connectedTo(): array
{
return [Entity::class];
}
}
76 changes: 72 additions & 4 deletions tests/unit/FieldsContainerRightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
use PluginDatainjectionComputerInjection;
use PluginDatainjectionMapping;
use PluginDatainjectionModel;
use PluginDatainjectionEntityInjection;
use PluginFieldsComputerstub;
use PluginFieldsEntitystub;
use PluginFieldsProfile;
use Session;

Expand All @@ -59,21 +61,30 @@ public static function setUpBeforeClass(): void

require_once __DIR__ . '/../fixtures/fields_stubs.php';

self::createContainerTable(PluginFieldsComputerstub::getTable(), Computer::class, PluginFieldsComputerstub::CONTAINER_ID);
self::createContainerTable(PluginFieldsEntitystub::getTable(), Entity::class, PluginFieldsEntitystub::CONTAINER_ID);
}

private static function createContainerTable(string $table, string $itemtype, int $container_id): void
{
/** @var DBmysql $DB */
global $DB;
//Same unique key as the tables Fields generates
$DB->doQuery(sprintf(
'CREATE TABLE IF NOT EXISTS %s (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`items_id` INT UNSIGNED NOT NULL,
`itemtype` VARCHAR(255) DEFAULT %s,
`plugin_fields_containers_id` INT UNSIGNED NOT NULL DEFAULT %d,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
`is_recursive` TINYINT NOT NULL DEFAULT 0,
`%s` VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
UNIQUE KEY `itemtype_item_container` (`itemtype`, `items_id`, `plugin_fields_containers_id`)
) ENGINE=InnoDB DEFAULT CHARSET=%s COLLATE=%s ROW_FORMAT=DYNAMIC',
$DB->quoteName(PluginFieldsComputerstub::getTable()),
$DB->quoteValue(Computer::class),
PluginFieldsComputerstub::CONTAINER_ID,
$DB->quoteName($table),
$DB->quoteValue($itemtype),
$container_id,
PluginFieldsComputerstub::VALUE_FIELD,
DBConnection::getDefaultCharset(),
DBConnection::getDefaultCollation(),
Expand All @@ -86,6 +97,7 @@ public static function tearDownAfterClass(): void
/** @var DBmysql $DB */
global $DB;
$DB->dropTable(PluginFieldsComputerstub::getTable(), true);
$DB->dropTable(PluginFieldsEntitystub::getTable(), true);
$DB->clearSchemaCache();

parent::tearDownAfterClass();
Expand Down Expand Up @@ -235,4 +247,60 @@ public function testFieldsValueUpdateIsRejectedWithReadRightOnContainer(): void
]));
$this->assertSame('Injected value', $container_item->fields[PluginFieldsComputerstub::VALUE_FIELD]);
}

private function injectEntityWithFieldValue(string $name, string $value): array
{
$injection = new PluginDatainjectionEntityInjection();
//Entities are imported from their complete name, created under _test_root_entity
$completename = PluginDatainjectionEntityInjection::getRootEntityName() . ' > _test_root_entity > ' . $name;

return $injection->addOrUpdateObject(
[
Entity::class => ['completename' => $completename],
PluginFieldsEntitystub::class => [PluginFieldsEntitystub::VALUE_FIELD => $value],
],
[
'rights' => [
'can_add' => true,
'can_update' => true,
'add_dropdown' => true,
'overwrite_notempty_fields' => true,
],
'mandatory_fields' => [Entity::class => ['completename' => true]],
'entities_id' => $this->entities_id,
],
);
}

private function assertEntityFieldValue(int $entities_id, string $expected): void
{
$rows = getAllDataFromTable(PluginFieldsEntitystub::getTable(), [
'itemtype' => Entity::class,
'items_id' => $entities_id,
]);
$this->assertCount(1, $rows);
$this->assertSame($expected, current($rows)[PluginFieldsEntitystub::VALUE_FIELD]);
}

public function testFieldsValueIsInjectedOnNewEntity(): void
{
$name = 'Test_Entity_fields_' . random_int(1, PHP_INT_MAX);

$results = $this->injectEntityWithFieldValue($name, 'Injected value');

$this->assertSame(PluginDatainjectionCommonInjectionLib::SUCCESS, $results['status']);
$this->assertEntityFieldValue($results[Entity::class], 'Injected value');
}

public function testFieldsValueIsUpdatedOnExistingEntity(): void
{
$name = 'Test_Entity_fields_' . random_int(1, PHP_INT_MAX);
$this->injectEntityWithFieldValue($name, 'Injected value');

$results = $this->injectEntityWithFieldValue($name, 'Updated value');

$this->assertSame(PluginDatainjectionCommonInjectionLib::IMPORT_UPDATE, $results['type']);
$this->assertSame(PluginDatainjectionCommonInjectionLib::SUCCESS, $results['status']);
$this->assertEntityFieldValue($results[Entity::class], 'Updated value');
}
}
Loading