From 22f0c2d816efd73bf177f5f750b6616eecea300b Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Wed, 15 Oct 2025 11:26:43 +0200 Subject: [PATCH 1/4] Fix networkport connection log creation Also fix Twig display closes #21488 --- src/NetworkPortConnectionLog.php | 3 +- .../NetworkPortConnectionLogTest.php | 111 ++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 tests/functional/NetworkPortConnectionLogTest.php diff --git a/src/NetworkPortConnectionLog.php b/src/NetworkPortConnectionLog.php index 966671044cc..ff3204cdf74 100644 --- a/src/NetworkPortConnectionLog.php +++ b/src/NetworkPortConnectionLog.php @@ -46,6 +46,7 @@ class NetworkPortConnectionLog extends CommonDBChild public static $itemtype = 'NetworkPort'; public static $items_id = 'networkports_id'; public $dohistory = false; + public static $mustBeAttached = false; public static function getTypeName($nb = 0) { @@ -116,7 +117,7 @@ public function showForItem(NetworkPort $netport, $user_filters = []) htmlescape(trim($cport->fields['name']) === '' ? __('Without name') : $cport->fields['name']) ); - $entries = [ + $entries[] = [ 'status' => '', 'date' => $row['date'], 'connected_item' => sprintf( diff --git a/tests/functional/NetworkPortConnectionLogTest.php b/tests/functional/NetworkPortConnectionLogTest.php new file mode 100644 index 00000000000..355f5244c77 --- /dev/null +++ b/tests/functional/NetworkPortConnectionLogTest.php @@ -0,0 +1,111 @@ +. + * + * --------------------------------------------------------------------- + */ + +namespace tests\units; + +use DbTestCase; +use Glpi\Inventory\Asset\NetworkPort; + +/* Test for inc/networkport_networkport.class.php */ + +class NetworkPortConnectionLogTest extends DbTestCase +{ + private function createPort(\CommonDBTM $asset, string $mac): \NetworkPort + { + $port = new \NetworkPort(); + $nb_log = (int) countElementsInTable('glpi_logs'); + $ports_id = $port->add([ + 'items_id' => $asset->getID(), + 'itemtype' => $asset->getType(), + 'entities_id' => $asset->fields['entities_id'], + 'is_recursive' => 0, + 'logical_number' => 1, + 'mac' => $mac, + 'instantiation_type' => 'NetworkPortEthernet', + 'name' => 'eth1', + ]); + $this->assertGreaterThan(0, (int) $ports_id); + $this->assertGreaterThan($nb_log, countElementsInTable('glpi_logs')); + + return $port; + } + + public function testLogs() + { + $this->login(); + $neteq1 = getItemByTypeName(\NetworkEquipment::class, '_test_networkequipment_1'); + $port_1 = $this->createPort($neteq1, '00:24:81:eb:c6:d3'); + $ports_id_1 = $port_1->getID(); + + $neteq2 = getItemByTypeName(\NetworkEquipment::class, '_test_networkequipment_2'); + $port_2 = $this->createPort($neteq2, '00:24:81:eb:c6:d4'); + $ports_id_2 = $port_2->getID(); + + $wired = new \NetworkPort_NetworkPort(); + $this->assertGreaterThan( + 0, + $wired->add([ + 'networkports_id_1' => $ports_id_1, + 'networkports_id_2' => $ports_id_2, + ]) + ); + + $this->assertTrue($wired->getFromDBForNetworkPort($ports_id_1)); + $this->assertTrue($wired->getFromDBForNetworkPort($ports_id_2)); + + $this->assertSame($ports_id_2, $wired->getOppositeContact($ports_id_1)); + $this->assertSame($ports_id_1, $wired->getOppositeContact($ports_id_2)); + + + //check connection log has been created + $connection_log = new \NetworkPortConnectionLog(); + $logs = $connection_log->find(); + $this->assertCount(1, $logs); + $log = array_pop($logs); + $this->assertSame(1, $log['connected']); + $this->assertSame($ports_id_1, $log['networkports_id_source']); + $this->assertSame($ports_id_2, $log['networkports_id_destination']); + + //check for display + ob_start(); + $connection_log->showForItem($port_1); + $contents = ob_get_clean(); + $this->assertStringContainsString($neteq2->getLinkURL(), $contents); + + ob_start(); + $connection_log->showForItem($port_2); + $contents = ob_get_clean(); + $this->assertStringContainsString($neteq1->getLinkURL(), $contents); + } +} From 04e77628bc89ee5cec49dbb3cd3651d50867960a Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Thu, 16 Oct 2025 11:10:13 +0200 Subject: [PATCH 2/4] Fix CS --- tests/functional/NetworkPortConnectionLogTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/functional/NetworkPortConnectionLogTest.php b/tests/functional/NetworkPortConnectionLogTest.php index 355f5244c77..af9003c3b79 100644 --- a/tests/functional/NetworkPortConnectionLogTest.php +++ b/tests/functional/NetworkPortConnectionLogTest.php @@ -35,7 +35,6 @@ namespace tests\units; use DbTestCase; -use Glpi\Inventory\Asset\NetworkPort; /* Test for inc/networkport_networkport.class.php */ From e40f8e672455afbb6908f4be4219fa2b837c7401 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 27 Oct 2025 11:07:48 +0100 Subject: [PATCH 3/4] Migrate to CommonDBRelation --- src/CommonDBChild.php | 2 +- src/NetworkPortConnectionLog.php | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/CommonDBChild.php b/src/CommonDBChild.php index 23e60fdd944..2d2ec12a00c 100644 --- a/src/CommonDBChild.php +++ b/src/CommonDBChild.php @@ -64,7 +64,7 @@ abstract class CommonDBChild extends CommonDBConnexity /** - * Get request cirteria to search for an item + * Get request criteria to search for an item * * @since 9.4 * diff --git a/src/NetworkPortConnectionLog.php b/src/NetworkPortConnectionLog.php index ff3204cdf74..e6b5de9dfb5 100644 --- a/src/NetworkPortConnectionLog.php +++ b/src/NetworkPortConnectionLog.php @@ -37,16 +37,13 @@ /** * Store ports connections log - * - * FIXME This class should inherit from CommonDBRelation, as it is linked - * to both 'networkports_id_source' and 'networkports_id_destination' */ -class NetworkPortConnectionLog extends CommonDBChild +class NetworkPortConnectionLog extends CommonDBRelation { - public static $itemtype = 'NetworkPort'; - public static $items_id = 'networkports_id'; - public $dohistory = false; - public static $mustBeAttached = false; + public static $itemtype_1 = NetworkPort::class; + public static $items_id_1 = 'networkports_id_source'; + public static $itemtype_2 = NetworkPort::class; + public static $items_id_2 = 'networkports_id_destination'; public static function getTypeName($nb = 0) { From 7e97d36714b6f3eeac9f9fcadc44b3b8eba08357 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 27 Oct 2025 13:56:06 +0100 Subject: [PATCH 4/4] Fix relations --- inc/relation.constant.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/relation.constant.php b/inc/relation.constant.php index 1fe1aa21578..3aeb3e1266d 100644 --- a/inc/relation.constant.php +++ b/inc/relation.constant.php @@ -989,7 +989,7 @@ '_glpi_networkportaggregates' => 'networkports_id', '_glpi_networkportaliases' => 'networkports_id', 'glpi_networkportaliases' => 'networkports_id_alias', - 'glpi_networkportconnectionlogs' => [ + '_glpi_networkportconnectionlogs' => [ 'networkports_id_destination', 'networkports_id_source', ],