|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 4 | +use UnityWebPortal\lib\UnityUser; |
| 5 | +use UnityWebPortal\lib\UserFlag; |
| 6 | + |
| 7 | +class ExpiryTest extends UnityWebPortalTestCase |
| 8 | +{ |
| 9 | + public static function provider() |
| 10 | + { |
| 11 | + return [ |
| 12 | + [self::$NICKNAME2UID["Blank"], self::$UID2ATTRIBUTES[self::$NICKNAME2UID["Blank"]][3]], |
| 13 | + [ |
| 14 | + self::$NICKNAME2UID["EmptyPIGroupOwner"], |
| 15 | + self::$UID2ATTRIBUTES[self::$NICKNAME2UID["EmptyPIGroupOwner"]][3], |
| 16 | + ], |
| 17 | + ]; |
| 18 | + } |
| 19 | + |
| 20 | + #[DataProvider("provider")] |
| 21 | + public function testExpiry(string $uid, string $mail) |
| 22 | + { |
| 23 | + global $LDAP, $SQL, $MAILER, $WEBHOOK; |
| 24 | + $this->switchUser("Admin"); |
| 25 | + $user = new UnityUser($uid, $LDAP, $SQL, $MAILER, $WEBHOOK); |
| 26 | + $this->assertFalse($user->getFlag(UserFlag::IDLELOCKED)); |
| 27 | + $this->assertFalse($user->getFlag(UserFlag::DISABLED)); |
| 28 | + $warnings_sent = $SQL->getUserExpirationWarningDaysSent($uid); |
| 29 | + $this->assertEmpty($warnings_sent["idlelock"]); |
| 30 | + $this->assertEmpty($warnings_sent["disable"]); |
| 31 | + // see deployment/overrides/phpunit/config/config.ini |
| 32 | + $this->assertEquals(CONFIG["expiry"]["idlelock_warning_days"], [2, 3]); |
| 33 | + $this->assertEquals(CONFIG["expiry"]["idlelock_day"], 4); |
| 34 | + $this->assertEquals(CONFIG["expiry"]["disable_warning_days"], [6, 7]); |
| 35 | + $this->assertEquals(CONFIG["expiry"]["disable_day"], 8); |
| 36 | + try { |
| 37 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 38 | + $output = trim(implode("\n", $output_lines)); |
| 39 | + $this->assertEquals("", $output); |
| 40 | + // 1 /////////////////////////////////////////////////////////////////////////////////// |
| 41 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 1); |
| 42 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 43 | + $output = trim(implode("\n", $output_lines)); |
| 44 | + $this->assertEquals("", $output); |
| 45 | + // 2 /////////////////////////////////////////////////////////////////////////////////// |
| 46 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 2); |
| 47 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 48 | + $output = trim(implode("\n", $output_lines)); |
| 49 | + $this->assertMatchesRegularExpression( |
| 50 | + "/^sending idlelock email to \"$mail\" with data \{\"idle_days\":2,\"expiration_date\":\"[\d\/]+\",\"warning_number\":1,\"is_final_warning\":false\}$/", |
| 51 | + $output, |
| 52 | + ); |
| 53 | + // 3 /////////////////////////////////////////////////////////////////////////////////// |
| 54 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 3); |
| 55 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 56 | + $output = trim(implode("\n", $output_lines)); |
| 57 | + $this->assertMatchesRegularExpression( |
| 58 | + "/^sending idlelock email to \"$mail\" with data \{\"idle_days\":3,\"expiration_date\":\"[\d\/]+\",\"warning_number\":2,\"is_final_warning\":true\}$/", |
| 59 | + $output, |
| 60 | + ); |
| 61 | + // 4 /////////////////////////////////////////////////////////////////////////////////// |
| 62 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 4); |
| 63 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 64 | + $output = trim(implode("\n", $output_lines)); |
| 65 | + // 5 /////////////////////////////////////////////////////////////////////////////////// |
| 66 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 5); |
| 67 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 68 | + $output = trim(implode("\n", $output_lines)); |
| 69 | + $this->assertEquals("", $output); |
| 70 | + // 6 /////////////////////////////////////////////////////////////////////////////////// |
| 71 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 6); |
| 72 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 73 | + $output = trim(implode("\n", $output_lines)); |
| 74 | + $this->assertMatchesRegularExpression( |
| 75 | + "/^sending disable email to \"$mail\" with data \{\"idle_days\":6,\"expiration_date\":\"[\d\/]+\",\"warning_number\":1,\"is_final_warning\":false\}$/", |
| 76 | + $output, |
| 77 | + ); |
| 78 | + // 7 /////////////////////////////////////////////////////////////////////////////////// |
| 79 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 7); |
| 80 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 81 | + $output = trim(implode("\n", $output_lines)); |
| 82 | + $this->assertMatchesRegularExpression( |
| 83 | + "/^sending disable email to \"$mail\" with data \{\"idle_days\":7,\"expiration_date\":\"[\d\/]+\",\"warning_number\":2,\"is_final_warning\":true\}$/", |
| 84 | + $output, |
| 85 | + ); |
| 86 | + // 8 /////////////////////////////////////////////////////////////////////////////////// |
| 87 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 8); |
| 88 | + [$_, $output_lines] = executeWorker("user-expiry.php", "--verbose"); |
| 89 | + $output = trim(implode("\n", $output_lines)); |
| 90 | + } finally { |
| 91 | + $SQL->resetUserExpirationWarningDaysSent($uid); |
| 92 | + $user->setFlag(UserFlag::IDLELOCKED, false); |
| 93 | + if ($user->getFlag(UserFlag::DISABLED)) { |
| 94 | + $user->reEnable(); |
| 95 | + } |
| 96 | + callPrivateMethod($SQL, "setUserLastLoginDaysAgo", $uid, 0); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments