summaryrefslogtreecommitdiffstats
path: root/tests/Modules/Locations/LocationTest.php
blob: 86369691a0a7298c3dfa5c5e3eff952ee46fe4c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

namespace Locations;

use Database;
use Location;
use PHPUnit\Framework\TestCase;

/**
 * Demonstrates using the SQLite-backed Database test backend to run code against
 * real SQL tables without a MySQL server. This is analogous to DbUnit-style tests.
 *
 * We test the real Location helper here with real SQL data.
 *
 */
class LocationWithSqliteTest extends TestCase
{
	protected function setUp(): void
	{
		// Load real Location class
		require_once 'modules-available/locations/inc/location.inc.php';

		// Fresh DB schema and canonical seed for this test
		Database::resetSchema();
	}

	public function testTreeAndAssocAndMapping(): void
	{
		$tree = Location::getTree();
		$this->assertSame(['Campus', 'Offsite'], array_column($tree, 'locationname'));
		$assoc = Location::getLocationsAssoc();
		$this->assertTrue($assoc[4]['isleaf']);
		$this->assertSame([1, 2], $assoc[4]['parents']);

		// Mapping respects depth and subnet size
		$this->assertSame(2, Location::mapIpToLocation('10.0.0.150'));
		$this->assertSame(1, Location::mapIpToLocation('10.0.0.50'));
		$this->assertSame(5, Location::mapIpToLocation('192.168.1.22'));
	}

	public function testUpdateMapIpToLocationIssuesUpdateSql(): void
	{
		$ret = Location::updateMapIpToLocation('uuid-1', '10.0.0.5');
		$this->assertSame(1, $ret);

		$row = Database::queryFirst('SELECT subnetlocationid FROM machine WHERE machineuuid = :uuid', ['uuid' => 'uuid-1']);
		$this->assertSame(1, (int)$row['subnetlocationid']);
	}
}