Skip to content

Commit eb9d233

Browse files
committed
Tests: Cover login URL generation and filtering.
Test the default `wp_login_url()` result, redirect and reauthentication parameters separately and together, and the arguments and return value of the `login_url` filter. Developed in: #15225 Props devmuhib, huzaifaalmesbah. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65076 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9865c4a commit eb9d233

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/**
4+
* Tests for wp_login_url().
5+
*
6+
* @group general
7+
* @group login
8+
*
9+
* @covers ::wp_login_url
10+
*/
11+
class Tests_General_WpLoginUrl extends WP_UnitTestCase {
12+
13+
/**
14+
* Tests that wp_login_url() returns the default login URL.
15+
*
16+
* @ticket 65819
17+
*/
18+
public function test_wp_login_url_default() {
19+
$this->assertSame( site_url( 'wp-login.php', 'login' ), wp_login_url() );
20+
}
21+
22+
/**
23+
* Tests that wp_login_url() appends the redirect_to parameter when a redirect is provided.
24+
*
25+
* @ticket 65819
26+
*/
27+
public function test_wp_login_url_with_redirect() {
28+
$redirect = home_url( '/custom-dashboard/' );
29+
$login_url = wp_login_url( $redirect );
30+
31+
$query = array();
32+
wp_parse_str( wp_parse_url( $login_url, PHP_URL_QUERY ), $query );
33+
34+
$this->assertSame( $redirect, $query['redirect_to'] );
35+
$this->assertArrayNotHasKey( 'reauth', $query );
36+
}
37+
38+
/**
39+
* Tests that wp_login_url() appends reauth=1 when $force_reauth is true.
40+
*
41+
* @ticket 65819
42+
*/
43+
public function test_wp_login_url_with_force_reauth() {
44+
$login_url = wp_login_url( '', true );
45+
46+
$query = array();
47+
wp_parse_str( wp_parse_url( $login_url, PHP_URL_QUERY ), $query );
48+
49+
$this->assertSame( '1', $query['reauth'] );
50+
$this->assertArrayNotHasKey( 'redirect_to', $query );
51+
}
52+
53+
/**
54+
* Tests that wp_login_url() appends both redirect_to and reauth when both parameters are set.
55+
*
56+
* @ticket 65819
57+
*/
58+
public function test_wp_login_url_with_redirect_and_force_reauth() {
59+
$redirect = home_url( '/profile/' );
60+
$login_url = wp_login_url( $redirect, true );
61+
62+
$query = array();
63+
wp_parse_str( wp_parse_url( $login_url, PHP_URL_QUERY ), $query );
64+
65+
$this->assertSame( $redirect, $query['redirect_to'] );
66+
$this->assertSame( '1', $query['reauth'] );
67+
}
68+
69+
/**
70+
* Tests that the 'login_url' filter receives the expected arguments and modifies the URL.
71+
*
72+
* @ticket 65819
73+
*/
74+
public function test_login_url_filter() {
75+
$filter = new MockAction();
76+
add_filter( 'login_url', array( $filter, 'filter' ), 10, 3 );
77+
78+
$redirect = home_url( '/landing/' );
79+
$url = wp_login_url( $redirect, true );
80+
81+
$this->assertSame( 1, $filter->get_call_count() );
82+
83+
$filter_args = $filter->get_args();
84+
$this->assertSame( $url, $filter_args[0][0] );
85+
$this->assertSame( $redirect, $filter_args[0][1] );
86+
$this->assertTrue( $filter_args[0][2] );
87+
88+
$custom_login_callback = static function ( $login_url, $redirect, $force_reauth ) {
89+
return 'https://sso.example.com/login';
90+
};
91+
92+
add_filter( 'login_url', $custom_login_callback, 10, 3 );
93+
$this->assertSame( 'https://sso.example.com/login', wp_login_url() );
94+
remove_filter( 'login_url', $custom_login_callback, 10 );
95+
}
96+
}

0 commit comments

Comments
 (0)