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); } /** * Describe a user to the `user_register` guard: which role they hold, and * whether whoever created them is studio staff. */ private function stubSignup(string $role, bool $byStaff): void { $user = Mockery::mock(\WP_User::class); $user->roles = [$role]; Functions\when('get_userdata')->justReturn($user); Functions\when('current_user_can')->justReturn($byStaff); } public function testStudentCreatedByAnUnknownSignupFormIsHeldForApproval(): void { // Open registration switches the site's own users_can_register on and // makes Student the default role, so any other signup form on the site // now mints students. They must not arrive able to book. $this->stubSignup(RoleManager::STUDENT, byStaff: false); Functions\expect('update_user_meta')->once() ->with(7, RegistrationStatus::META_AWAITING_APPROVAL, '1'); // Counted as confirmed: nobody asked them to confirm, and there is no // token for them to answer with, so blocking the login would strand them. Functions\expect('update_user_meta')->once() ->with(7, RegistrationStatus::META_EMAIL_CONFIRMED, '1'); (new RegistrationLoginGate())->holdUnknownSignup(7); } public function testStudentCreatedByStaffIsLeftActive(): void { // An administrator adding a student from wp-admin could have approved // them in the next click; making them do so is ceremony. $this->stubSignup(RoleManager::STUDENT, byStaff: true); Functions\expect('update_user_meta')->never(); (new RegistrationLoginGate())->holdUnknownSignup(7); } public function testNonStudentSignupIsNotHeld(): void { $this->stubSignup(RoleManager::INSTRUCTOR, byStaff: false); Functions\expect('update_user_meta')->never(); (new RegistrationLoginGate())->holdUnknownSignup(7); } public function testUnknownUserIdIsIgnored(): void { Functions\when('get_userdata')->justReturn(false); Functions\expect('update_user_meta')->never(); (new RegistrationLoginGate())->holdUnknownSignup(0); } }