alias(static function (int $id, string $key) use ($awaiting, $confirmed) { if ($key === RegistrationStatus::META_AWAITING_APPROVAL) { return $awaiting; } if ($key === RegistrationStatus::META_EMAIL_CONFIRMED) { return $confirmed; } return ''; }); } private function user(int $id): \WP_User { $user = Mockery::mock(\WP_User::class); $user->ID = $id; return $user; } public function testBlocksLoginWhileEmailUnconfirmed(): void { $this->stubMeta('1', ''); $result = (new RegistrationLoginGate())->blockUnconfirmed($this->user(7)); self::assertInstanceOf(\WP_Error::class, $result); self::assertSame('us_email_unconfirmed', $result->get_error_code()); } public function testAllowsLoginWhenConfirmedButAwaitingApproval(): void { $this->stubMeta('1', '1'); $user = $this->user(7); self::assertSame($user, (new RegistrationLoginGate())->blockUnconfirmed($user)); } public function testAllowsLoginForApprovedStudent(): void { $this->stubMeta('', '1'); $user = $this->user(7); self::assertSame($user, (new RegistrationLoginGate())->blockUnconfirmed($user)); } public function testIgnoresUsersWithNoPendingMeta(): void { // Invite/admin-created students carry no meta at all. $this->stubMeta('', ''); $user = $this->user(7); self::assertSame($user, (new RegistrationLoginGate())->blockUnconfirmed($user)); } public function testPassesThroughAnEarlierError(): void { $error = new \WP_Error('some_earlier_error', 'nope'); self::assertSame($error, (new RegistrationLoginGate())->blockUnconfirmed($error)); } public function testWithholdsBookingCapWhileAwaitingApproval(): void { $this->stubMeta('1', '1'); $allcaps = [ RoleManager::CAP_BOOK_LESSON => true, 'read' => true ]; $result = (new RegistrationLoginGate()) ->withholdBookingWhilePending($allcaps, [], [], $this->user(7)); self::assertArrayNotHasKey(RoleManager::CAP_BOOK_LESSON, $result); self::assertTrue($result['read']); } public function testKeepsBookingCapForApprovedStudent(): void { $this->stubMeta('', '1'); $allcaps = [ RoleManager::CAP_BOOK_LESSON => true ]; $result = (new RegistrationLoginGate()) ->withholdBookingWhilePending($allcaps, [], [], $this->user(7)); self::assertTrue($result[RoleManager::CAP_BOOK_LESSON]); } public function testLeavesCapsUntouchedForNonUserArg(): void { $allcaps = [ RoleManager::CAP_BOOK_LESSON => true ]; $result = (new RegistrationLoginGate()) ->withholdBookingWhilePending($allcaps, [], [], null); self::assertSame($allcaps, $result); } }