|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +class PiBecomeRequestTest extends TestCase |
| 6 | +{ |
| 7 | + private function assertNumberPiBecomeRequests(int $x) |
| 8 | + { |
| 9 | + global $USER, $SQL; |
| 10 | + if ($x == 0) { |
| 11 | + $this->assertFalse($SQL->requestExists($USER->getUID())); |
| 12 | + } elseif ($x > 0) { |
| 13 | + $this->assertTrue($SQL->requestExists($USER->getUID())); |
| 14 | + } else { |
| 15 | + throw new RuntimeError("x must not be negative"); |
| 16 | + } |
| 17 | + $this->assertEquals($x, $this->getNumberPiBecomeRequests()); |
| 18 | + } |
| 19 | + |
| 20 | + private function getNumberPiBecomeRequests() |
| 21 | + { |
| 22 | + global $USER, $SQL; |
| 23 | + // FIXME table name, "admin" are private constants in UnitySQL |
| 24 | + // FIXME "admin" should be something else |
| 25 | + $stmt = $SQL->getConn()->prepare( |
| 26 | + "SELECT * FROM requests WHERE uid=:uid and request_for='admin'" |
| 27 | + ); |
| 28 | + $uid = $USER->getUID(); |
| 29 | + $stmt->bindParam(":uid", $uid); |
| 30 | + $stmt->execute(); |
| 31 | + return count($stmt->fetchAll()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testRequestBecomePi() |
| 35 | + { |
| 36 | + global $USER, $SQL; |
| 37 | + switchUser(...getUserNotPiNotRequestedBecomePi()); |
| 38 | + $this->assertFalse($USER->isPI()); |
| 39 | + $this->assertNumberPiBecomeRequests(0); |
| 40 | + post( |
| 41 | + __DIR__ . "/../../webroot/panel/account.php", |
| 42 | + ["form_type" => "pi_request"] |
| 43 | + ); |
| 44 | + $this->assertNumberPiBecomeRequests(1); |
| 45 | + post( |
| 46 | + __DIR__ . "/../../webroot/panel/account.php", |
| 47 | + ["form_type" => "pi_request"] |
| 48 | + ); |
| 49 | + $this->assertNumberPiBecomeRequests(1); |
| 50 | + } |
| 51 | +} |
0 commit comments