diff --git a/tests/phpunit/tests/blocks/renderLoginout.php b/tests/phpunit/tests/blocks/renderLoginout.php
new file mode 100644
index 0000000000000..6f38ca19a3d16
--- /dev/null
+++ b/tests/phpunit/tests/blocks/renderLoginout.php
@@ -0,0 +1,297 @@
+original_block_to_render = WP_Block_Supports::$block_to_render;
+ $this->original_server = $_SERVER;
+
+ $_SERVER['HTTP_HOST'] = 'example.org';
+ $_SERVER['REQUEST_URI'] = '/current-page/?a=1';
+
+ WP_Block_Supports::$block_to_render = array(
+ 'blockName' => 'core/loginout',
+ 'attrs' => array(),
+ );
+ }
+
+ public function tear_down() {
+ WP_Block_Supports::$block_to_render = $this->original_block_to_render;
+ $_SERVER = $this->original_server;
+
+ parent::tear_down();
+ }
+
+ /**
+ * Returns the attributes of the first element of the output.
+ *
+ * @param string $output The output.
+ * @param string $tag The tag name.
+ * @return array|null The attributes, or null when there is no element.
+ */
+ private function get_element( $output, $tag ) {
+ $processor = new WP_HTML_Tag_Processor( $output );
+
+ if ( ! $processor->next_tag( $tag ) ) {
+ return null;
+ }
+
+ $attributes = array();
+
+ foreach ( $processor->get_attribute_names_with_prefix( '' ) as $name ) {
+ $attributes[ $name ] = $processor->get_attribute( $name );
+ }
+
+ return $attributes;
+ }
+
+ /**
+ * Returns the sorted classes of the wrapper of the output.
+ *
+ * @param string $output The output.
+ * @return string[] The classes.
+ */
+ private function get_classes( $output ) {
+ $classes = explode( ' ', $this->get_element( $output, 'div' )['class'] );
+
+ sort( $classes );
+
+ return $classes;
+ }
+
+ /**
+ * Tests the link to log in for a visitor.
+ *
+ * @ticket 66577
+ */
+ public function test_should_render_the_login_link_for_a_visitor() {
+ wp_set_current_user( 0 );
+
+ $output = render_block_core_loginout( array() );
+ $link = $this->get_element( $output, 'a' );
+
+ $this->assertSame( array( 'logged-out', 'wp-block-loginout' ), $this->get_classes( $output ) );
+ $this->assertStringStartsWith( '
assertStringEndsWith( '
', $output );
+ $this->assertStringContainsString( '>Log in', $output );
+ $this->assertStringContainsString( 'wp-login.php', $link['href'] );
+ $this->assertStringNotContainsString( 'redirect_to', $link['href'] );
+ }
+
+ /**
+ * Tests the link to log out for a user.
+ *
+ * @ticket 66577
+ */
+ public function test_should_render_the_logout_link_for_a_user() {
+ wp_set_current_user( self::factory()->user->create() );
+
+ $output = render_block_core_loginout( array() );
+ $link = $this->get_element( $output, 'a' );
+
+ $this->assertSame( array( 'logged-in', 'wp-block-loginout' ), $this->get_classes( $output ) );
+ $this->assertStringContainsString( '>Log out', $output );
+ $this->assertStringContainsString( 'action=logout', $link['href'] );
+ $this->assertStringContainsString( '_wpnonce=', $link['href'] );
+ }
+
+ /**
+ * Tests the redirect to the current page.
+ *
+ * @ticket 66577
+ *
+ * @dataProvider data_users
+ *
+ * @param bool $logged_in Whether the user is logged in.
+ */
+ public function test_should_redirect_to_the_current_page( $logged_in ) {
+ wp_set_current_user( $logged_in ? self::factory()->user->create() : 0 );
+
+ $link = $this->get_element( render_block_core_loginout( array( 'redirectToCurrent' => true ) ), 'a' );
+
+ parse_str( (string) wp_parse_url( $link['href'], PHP_URL_QUERY ), $query );
+
+ $this->assertSame( 'http://example.org/current-page/?a=1', $query['redirect_to'] );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public static function data_users() {
+ return array(
+ 'visitor' => array( false ),
+ 'user' => array( true ),
+ );
+ }
+
+ /**
+ * Tests that the redirect to the current page uses the protocol of the request.
+ *
+ * @ticket 66577
+ */
+ public function test_should_redirect_to_the_current_page_with_https() {
+ wp_set_current_user( 0 );
+
+ $_SERVER['HTTPS'] = 'on';
+
+ $link = $this->get_element( render_block_core_loginout( array( 'redirectToCurrent' => true ) ), 'a' );
+
+ parse_str( (string) wp_parse_url( $link['href'], PHP_URL_QUERY ), $query );
+
+ $this->assertSame( 'https://example.org/current-page/?a=1', $query['redirect_to'] );
+ }
+
+ /**
+ * Tests that there is no redirect when the setting is off.
+ *
+ * @ticket 66577
+ *
+ * @dataProvider data_no_redirect_attributes
+ *
+ * @param array $attributes The attributes of the block.
+ */
+ public function test_should_not_redirect_without_the_setting( $attributes ) {
+ wp_set_current_user( 0 );
+
+ $link = $this->get_element( render_block_core_loginout( $attributes ), 'a' );
+
+ $this->assertStringNotContainsString( 'redirect_to', $link['href'] );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public static function data_no_redirect_attributes() {
+ return array(
+ 'not set' => array( array() ),
+ 'false' => array( array( 'redirectToCurrent' => false ) ),
+ );
+ }
+
+ /**
+ * Tests the login form for a visitor.
+ *
+ * @ticket 66577
+ */
+ public function test_should_render_the_login_form_for_a_visitor() {
+ wp_set_current_user( 0 );
+
+ $output = render_block_core_loginout( array( 'displayLoginAsForm' => true ) );
+
+ $this->assertSame( array( 'has-login-form', 'logged-out', 'wp-block-loginout' ), $this->get_classes( $output ) );
+ $this->assertStringContainsString( '