guardians = Mockery::mock(GuardianService::class); $this->page = new AccountPage($this->guardians); Functions\when('is_user_logged_in')->justReturn(true); Functions\when('get_current_user_id')->justReturn(5); Functions\when('wp_enqueue_style')->justReturn(null); Functions\when('get_permalink')->alias( static fn (int $id = 0): string => $id > 0 ? 'https://studio.test/sign-in/' : 'https://studio.test/current/' ); Functions\when('wp_logout_url')->alias( static fn (string $redirect): string => 'https://studio.test/wp-login.php?action=logout&redirect_to=' . rawurlencode($redirect) ); Functions\when('wp_get_current_user')->justReturn($this->user('Grace', 'Hopper', 'grace@studio.test')); } private function user(string $first, string $last, string $email): \WP_User { $user = Mockery::mock(\WP_User::class); $user->ID = 5; $user->first_name = $first; $user->last_name = $last; $user->nickname = ''; $user->user_email = $email; return $user; } /** @param list $students */ private function bookable(array $students): void { $this->guardians->shouldReceive('bookableStudents')->with(5)->andReturn($students); } public function testShowsTheSignedInNameAndEmail(): void { $this->bookable([['id' => 5, 'name' => 'Grace Hopper', 'is_self' => true]]); $html = $this->page->render([]); self::assertStringContainsString('Grace Hopper', $html); self::assertStringContainsString('grace@studio.test', $html); self::assertStringContainsString('Sign out', $html); } public function testNamesTheStudentsTheAccountBooksFor(): void { $this->bookable([ ['id' => 42, 'name' => 'Ada', 'is_self' => false], ['id' => 43, 'name' => 'Alan', 'is_self' => false], ['id' => 5, 'name' => 'Grace Hopper', 'is_self' => true], ]); self::assertStringContainsString('Booking for Ada, Alan', $this->page->render([])); } /** * An account that only books for itself has nothing to add — "Booking for * Grace Hopper" under "Grace Hopper" is noise. */ public function testSaysNothingAboutStudentsOnAPlainAccount(): void { $this->bookable([['id' => 5, 'name' => 'Grace Hopper', 'is_self' => true]]); self::assertStringNotContainsString('Booking for', $this->page->render([])); } public function testSigningOutReturnsToTheConfiguredLoginPage(): void { $this->bookable([['id' => 5, 'name' => 'Grace Hopper', 'is_self' => true]]); self::assertStringContainsString( rawurlencode('https://studio.test/sign-in/'), $this->page->render(['loginPageId' => 9]) ); } /** * With no page chosen, signing out from a header link should leave the * visitor where they were rather than navigating them somewhere. */ public function testSigningOutReturnsToTheCurrentPageWhenNoLoginPageIsSet(): void { $this->bookable([['id' => 5, 'name' => 'Grace Hopper', 'is_self' => true]]); self::assertStringContainsString( rawurlencode('https://studio.test/current/'), $this->page->render([]) ); } public function testTheShortcodeAttributeNameIsAccepted(): void { $this->bookable([['id' => 5, 'name' => 'Grace Hopper', 'is_self' => true]]); self::assertStringContainsString( rawurlencode('https://studio.test/sign-in/'), $this->page->render(['login_page_id' => 9]) ); } /** * A block whose whole job is "you are signed in as X" has nothing to say to * a stranger, and a bare notice in a site header cannot be acted on. */ public function testRendersNothingForASignedOutVisitorWithNoLoginPage(): void { Functions\when('is_user_logged_in')->justReturn(false); $this->guardians->shouldNotReceive('bookableStudents'); self::assertSame('', $this->page->render([])); } public function testOffersASignInLinkToASignedOutVisitorWhenAPageIsChosen(): void { Functions\when('is_user_logged_in')->justReturn(false); $this->guardians->shouldNotReceive('bookableStudents'); $html = $this->page->render(['loginPageId' => 9]); self::assertStringContainsString('https://studio.test/sign-in/', $html); self::assertStringContainsString('Sign in', $html); self::assertStringNotContainsString('Sign out', $html); } /** * A page can be deleted after it has been chosen in the block, which * get_permalink() reports as false. */ public function testTreatsADeletedLoginPageAsNoneChosen(): void { Functions\when('is_user_logged_in')->justReturn(false); Functions\when('get_permalink')->justReturn(false); self::assertSame('', $this->page->render(['loginPageId' => 9])); } }