diff --git a/CHANGELOG.md b/CHANGELOG.md index 350398e..4260b1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixed +- Fix mobile devices duplication during mobile sync - Fix leftover data (profile rights and config values) after plugin uninstall ## [3.2.2] - 2026-08-03 diff --git a/inc/mobilesync.class.php b/inc/mobilesync.class.php index c32bf60..091c840 100644 --- a/inc/mobilesync.class.php +++ b/inc/mobilesync.class.php @@ -720,12 +720,12 @@ public static function discover(): int $imported = []; $iterator = $DB->request([ - 'SELECT' => ['udid'], + 'SELECT' => ['jamf_items_id'], 'FROM' => 'glpi_plugin_jamf_devices', 'WHERE' => ['jamf_type' => static::$jamf_itemtype], ]); foreach ($iterator as $data) { - $imported[] = $data['udid']; + $imported[] = (int) $data['jamf_items_id']; } $pending_iterator = $DB->request([ @@ -742,7 +742,7 @@ public static function discover(): int $config = Config::getConfigurationValues('plugin:Jamf'); foreach ($jamf_devices as $jamf_device) { - if (!in_array($jamf_device['id'], $imported, true)) { + if (!in_array((int) $jamf_device['id'], $imported, true)) { $itemtype = str_contains((string) $jamf_device['model_identifier'], 'iPhone') ? 'Phone' : 'Computer'; if (isset($config['autoimport']) && $config['autoimport']) { try { diff --git a/tests/units/PluginJamfMobileSync.php b/tests/units/PluginJamfMobileSync.php index 615e8df..7d9bd33 100644 --- a/tests/units/PluginJamfMobileSync.php +++ b/tests/units/PluginJamfMobileSync.php @@ -193,6 +193,32 @@ public function testImportAsPhone() $this->assertEquals('1aec6610a9401d2cc47cb55e1a2f7b500ab75864', $ext_field['value']); } + public function testDiscoverSkipsAlreadyImportedDevices() + { + /** @var DBmysql $DB */ + global $DB; + + // Ensure the device is imported (may already be done by a previous test) + PluginJamfMobileTestSync::import('Phone', 5, false); + $this->assertGreaterThan(0, countElementsInTable('glpi_plugin_jamf_devices', [ + 'jamf_type' => 'MobileDevice', + 'jamf_items_id' => 5, + ])); + + $DB->delete(PluginJamfImport::getTable(), ['jamf_type' => 'MobileDevice']); + PluginJamfMobileTestSync::discover(); + + // The imported device must not be queued again for import + $this->assertEquals(0, countElementsInTable(PluginJamfImport::getTable(), [ + 'jamf_type' => 'MobileDevice', + 'jamf_items_id' => 5, + ])); + // The other devices are still discovered + $this->assertEquals(5, countElementsInTable(PluginJamfImport::getTable(), [ + 'jamf_type' => 'MobileDevice', + ])); + } + public static function deviceSyncEnginesProvider() { $engines = PluginJamfSync::getDeviceSyncEngines();