From 1da327b735bad4ff7a5e9fc0c46bdcf91505a17f Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 16 Oct 2026 11:33:09 -0400 Subject: [PATCH] test: cover handler-free device row and sites filter markup The coverage gate for #394 reported 0/5 changed lines in lib/mactrack_functions.php: the rescan icon in mactrack_format_device_row() and the filter selects in mactrack_site_filter(). Add unit tests that render both and assert the class/data-attribute hooks, the absence of inline onClick/onChange attributes, and the $(function() {}) bindings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Unit/MactrackInlineHandlerMarkupTest.php | 157 ++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 tests/Unit/MactrackInlineHandlerMarkupTest.php diff --git a/tests/Unit/MactrackInlineHandlerMarkupTest.php b/tests/Unit/MactrackInlineHandlerMarkupTest.php new file mode 100644 index 00000000..7a93fdf5 --- /dev/null +++ b/tests/Unit/MactrackInlineHandlerMarkupTest.php @@ -0,0 +1,157 @@ +'; + } +} + +if (!function_exists('form_end_row')) { + function form_end_row() { + print ''; + } +} + +final class MactrackInlineHandlerMarkupTest extends TestCase { + /** + * @return void + */ + public static function setUpBeforeClass(): void { + self::loadPluginSource('lib/mactrack_functions.php'); + } + + /** + * @param array $over + * + * @return array + */ + private function device(array $over = []): array { + return array_merge([ + 'device_id' => 7, + 'disabled' => '', + 'device_name' => 'sw1', + 'site_name' => 'HQ', + 'snmp_status' => 3, + 'hostname' => 'sw1.example.com', + 'device_type' => 'Switch', + 'scan_type' => '2', + 'ips_total' => 1, + 'ports_total' => 48, + 'ports_active' => 12, + 'ports_trunk' => 2, + 'macs_active' => 30, + 'last_runduration' => 4.2, + ], $over); + } + + /** + * @param array $device + * + * @return string + */ + private function renderRow(array $device): string { + ob_start(); + mactrack_format_device_row($device, true); + + return (string) ob_get_clean(); + } + + /** + * @return void + */ + public function test_rescan_icon_uses_class_and_data_attribute_not_onclick(): void { + $html = $this->renderRow($this->device()); + + $this->assertStringContainsString("class='mactrackRescanDevice'", $html); + $this->assertStringContainsString("data-device-id='7'", $html); + $this->assertStringNotContainsStringIgnoringCase('onclick', $html); + } + + /** + * @return void + */ + public function test_disabled_device_has_no_rescan_icon(): void { + $html = $this->renderRow($this->device(['disabled' => 'on'])); + + $this->assertStringNotContainsString('mactrackRescanDevice', $html); + $this->assertStringContainsString('view_none.gif', $html); + } + + /** + * @return void + */ + public function test_site_filter_has_no_inline_handlers_and_binds_in_ready_block(): void { + global $item_rows; + + $item_rows = [10 => '10', 20 => '20']; + + mactrack_test_queue_return('db_fetch_assoc', [['site_id' => 1, 'site_name' => 'HQ']]); + + ob_start(); + mactrack_site_filter('mactrack_sites.php'); + $html = (string) ob_get_clean(); + + $this->assertDoesNotMatchRegularExpression('/\son(change|click)\s*=/i', $html); + + foreach (['rows', 'site_id', 'device_type_id', 'detail'] as $id) { + $this->assertStringContainsString("id='$id'", $html); + } + + $this->assertStringContainsString("\$('#rows, #site_id, #device_type_id').on('change'", $html); + $this->assertStringContainsString("\$('#detail').on('click'", $html); + } +}