| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-20 08:41:14 +00:00
										 |  |  | require_once dirname(__DIR__, 3) . '/app/classes/host.php'; | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | use PHPUnit\Framework\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class HostTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     private $db; | 
					
						
							|  |  |  |     private $host; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function setUp(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::setUp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Set development environment for detailed errors
 | 
					
						
							|  |  |  |         global $config; | 
					
						
							|  |  |  |         $config['environment'] = 'development'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Set up test database
 | 
					
						
							|  |  |  |         $this->db = new \Database([ | 
					
						
							|  |  |  |             'type' => 'sqlite', | 
					
						
							|  |  |  |             'dbFile' => ':memory:' | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |         // Create host table
 | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |         $this->db->getConnection()->exec("
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |             CREATE TABLE host ( | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |                 id INTEGER PRIMARY KEY AUTOINCREMENT, | 
					
						
							|  |  |  |                 platform_id INTEGER NOT NULL, | 
					
						
							|  |  |  |                 name TEXT NOT NULL, | 
					
						
							|  |  |  |                 address TEXT NOT NULL | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ");
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |         // Create jilo_agent table for relationship testing
 | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |         $this->db->getConnection()->exec("
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |             CREATE TABLE jilo_agent ( | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |                 id INTEGER PRIMARY KEY AUTOINCREMENT, | 
					
						
							|  |  |  |                 host_id INTEGER NOT NULL, | 
					
						
							|  |  |  |                 agent_type_id INTEGER NOT NULL, | 
					
						
							|  |  |  |                 url TEXT NOT NULL, | 
					
						
							|  |  |  |                 secret_key TEXT, | 
					
						
							|  |  |  |                 check_period INTEGER DEFAULT 60 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->host = new \Host($this->db); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testAddHost() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             'platform_id' => 1, | 
					
						
							|  |  |  |             'name' => 'Test host', | 
					
						
							|  |  |  |             'address' => '192.168.1.100' | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $this->host->addHost($data); | 
					
						
							|  |  |  |         $this->assertTrue($result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Verify host was created
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |         $stmt = $this->db->getConnection()->prepare('SELECT * FROM host WHERE platform_id = ? AND name = ?'); | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |         $stmt->execute([$data['platform_id'], $data['name']]); | 
					
						
							|  |  |  |         $host = $stmt->fetch(\PDO::FETCH_ASSOC); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals($data['name'], $host['name']); | 
					
						
							|  |  |  |         $this->assertEquals($data['address'], $host['address']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testGetHostDetails() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Add test host
 | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             'platform_id' => 1, | 
					
						
							|  |  |  |             'name' => 'Test host', | 
					
						
							|  |  |  |             'address' => '192.168.1.100' | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->host->addHost($data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Test getting all hosts
 | 
					
						
							|  |  |  |         $hosts = $this->host->getHostDetails(); | 
					
						
							|  |  |  |         $this->assertIsArray($hosts); | 
					
						
							|  |  |  |         $this->assertCount(1, $hosts); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Test getting hosts by platform
 | 
					
						
							|  |  |  |         $hosts = $this->host->getHostDetails(1); | 
					
						
							|  |  |  |         $this->assertIsArray($hosts); | 
					
						
							|  |  |  |         $this->assertCount(1, $hosts); | 
					
						
							|  |  |  |         $this->assertEquals($data['name'], $hosts[0]['name']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Test getting specific host
 | 
					
						
							|  |  |  |         $hosts = $this->host->getHostDetails(1, $hosts[0]['id']); | 
					
						
							|  |  |  |         $this->assertIsArray($hosts); | 
					
						
							|  |  |  |         $this->assertCount(1, $hosts); | 
					
						
							|  |  |  |         $this->assertEquals($data['name'], $hosts[0]['name']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testEditHost() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Add test host
 | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             'platform_id' => 1, | 
					
						
							|  |  |  |             'name' => 'Test host', | 
					
						
							|  |  |  |             'address' => '192.168.1.100' | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->host->addHost($data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Get host ID
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |         $stmt = $this->db->getConnection()->prepare('SELECT id FROM host WHERE platform_id = ? AND name = ?'); | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |         $stmt->execute([$data['platform_id'], $data['name']]); | 
					
						
							|  |  |  |         $hostId = $stmt->fetch(\PDO::FETCH_COLUMN); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Update host
 | 
					
						
							|  |  |  |         $updateData = [ | 
					
						
							|  |  |  |             'id' => $hostId, | 
					
						
							|  |  |  |             'name' => 'Updated host', | 
					
						
							|  |  |  |             'address' => '192.168.1.200' | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $this->host->editHost($data['platform_id'], $updateData); | 
					
						
							|  |  |  |         $this->assertTrue($result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Verify update
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |         $stmt = $this->db->getConnection()->prepare('SELECT * FROM host WHERE id = ?'); | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |         $stmt->execute([$hostId]); | 
					
						
							|  |  |  |         $host = $stmt->fetch(\PDO::FETCH_ASSOC); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertEquals($updateData['name'], $host['name']); | 
					
						
							|  |  |  |         $this->assertEquals($updateData['address'], $host['address']); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testDeleteHost() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Add test host
 | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             'platform_id' => 1, | 
					
						
							|  |  |  |             'name' => 'Test host', | 
					
						
							|  |  |  |             'address' => '192.168.1.100' | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $this->host->addHost($data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Get host ID
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |         $stmt = $this->db->getConnection()->prepare('SELECT id FROM host WHERE platform_id = ? AND name = ?'); | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |         $stmt->execute([$data['platform_id'], $data['name']]); | 
					
						
							|  |  |  |         $hostId = $stmt->fetch(\PDO::FETCH_COLUMN); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Add test agent to the host
 | 
					
						
							|  |  |  |         $this->db->getConnection()->exec("
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |             INSERT INTO jilo_agent (host_id, agent_type_id, url, secret_key) | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |             VALUES ($hostId, 1, 'http://test:8080', 'secret') | 
					
						
							|  |  |  |         ");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Delete host
 | 
					
						
							|  |  |  |         $result = $this->host->deleteHost($hostId); | 
					
						
							|  |  |  |         $this->assertTrue($result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Verify host deletion
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |         $stmt = $this->db->getConnection()->prepare('SELECT COUNT(*) FROM host WHERE id = ?'); | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |         $stmt->execute([$hostId]); | 
					
						
							|  |  |  |         $hostCount = $stmt->fetch(\PDO::FETCH_COLUMN); | 
					
						
							|  |  |  |         $this->assertEquals(0, $hostCount); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Verify agent deletion
 | 
					
						
							| 
									
										
										
										
											2025-04-25 13:16:38 +00:00
										 |  |  |         $stmt = $this->db->getConnection()->prepare('SELECT COUNT(*) FROM jilo_agent WHERE host_id = ?'); | 
					
						
							| 
									
										
										
										
											2025-02-18 14:36:31 +00:00
										 |  |  |         $stmt->execute([$hostId]); | 
					
						
							|  |  |  |         $agentCount = $stmt->fetch(\PDO::FETCH_COLUMN); | 
					
						
							|  |  |  |         $this->assertEquals(0, $agentCount); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testEditNonexistentHost() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $updateData = [ | 
					
						
							|  |  |  |             'id' => 999, | 
					
						
							|  |  |  |             'name' => 'Nonexistent host', | 
					
						
							|  |  |  |             'address' => '192.168.1.200' | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = $this->host->editHost(1, $updateData); | 
					
						
							|  |  |  |         $this->assertIsString($result); | 
					
						
							|  |  |  |         $this->assertStringContainsString('No host found', $result); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |