diff options
| author | Simon Rettberg | 2025-11-26 10:46:51 +0100 |
|---|---|---|
| committer | Simon Rettberg | 2025-12-12 15:16:59 +0100 |
| commit | 7c173411785f959d250d3dfbd7d4cfcb0e20f0e0 (patch) | |
| tree | 242157791a76afb7af23ec2cd3d22b599e54ce9d /tests/Modules/Locations/LocationTest.php | |
| parent | [exams] Fix incorrect count() clause (diff) | |
| download | slx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.tar.gz slx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.tar.xz slx-admin-7c173411785f959d250d3dfbd7d4cfcb0e20f0e0.zip | |
Add tests using PHPUnit
Tests generated by Junie AI. Might not have the best possible quality
but at least we got something, and if it turns out to be complete
rubbish, we can just throw it out again without any issues, as this is
independent of the actual code base.
Diffstat (limited to 'tests/Modules/Locations/LocationTest.php')
| -rw-r--r-- | tests/Modules/Locations/LocationTest.php | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/Modules/Locations/LocationTest.php b/tests/Modules/Locations/LocationTest.php new file mode 100644 index 00000000..86369691 --- /dev/null +++ b/tests/Modules/Locations/LocationTest.php @@ -0,0 +1,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']); + } +} |
