Make WordPress Core

Ticket #33821: 33821.tests.diff

File 33821.tests.diff, 4.5 KB (added by SirLouen, 6 months ago)
  • tests/phpunit/tests/canonical.php

    diff --git tests/phpunit/tests/canonical.php tests/phpunit/tests/canonical.php
    index 886b093129..8df4645bb8 100644
    class Tests_Canonical extends WP_Canonical_UnitTestCase { 
    545545                        ),
    546546                );
    547547        }
     548
     549        /**
     550         * Test domain and port redirections.
     551         *
     552         * @ticket 33821
     553         * @dataProvider data_domain_and_port_redirections
     554         *
     555         * @covers ::redirect_canonical
     556         *
     557         * @param string $home_url      The home URL to set.
     558         * @param string $site_url      The site URL to set.
     559         * @param string $request_host  The HTTP_HOST to simulate.
     560         * @param string $request_url   The URL to test redirection for.
     561         * @param string $expected_url  The expected redirect URL, or null if no redirect.
     562         * @param string $failure_msg   The message to display on test failure.
     563         */
     564        public function test_domain_and_port_redirections( $home_url, $site_url, $request_host, $request_url, $expected_url, $failure_msg ) {
     565                update_option( 'home', $home_url );
     566                update_option( 'siteurl', $site_url );
     567
     568                // Simulate a request to a non-canonical domain
     569                $_SERVER['HTTP_HOST']   = $request_host;
     570                $_SERVER['REQUEST_URI'] = '/';
     571
     572                $redirect = redirect_canonical( $request_url, false );
     573
     574                $this->assertSame( $expected_url, $redirect, $failure_msg );
     575        }
     576
     577        /**
     578         * Data provider for test_domain_and_port_redirections().
     579         *
     580         * @return array[] {
     581         *     @type string $home_url      The home URL to set.
     582         *     @type string $site_url      The site URL to set.
     583         *     @type string $request_host  The HTTP_HOST to simulate.
     584         *     @type string $request_url   The URL to test redirection for.
     585         *     @type string $expected_url  The expected redirect URL, or null if no redirect.
     586         *     @type string $failure_msg   The message to display on test failure.
     587         * }
     588         */
     589        public function data_domain_and_port_redirections() {
     590                return array(
     591                        'non-standard localhost port to canonical domain' => array(
     592                                'home_url'     => 'http://example.com',
     593                                'site_url'     => 'http://example.com',
     594                                'request_host' => 'localhost:10020',
     595                                'request_url'  => 'http://localhost:10018/',
     596                                'expected_url' => 'http://example.com/',
     597                                'failure_msg'  => 'Failed to redirect non-standard localhost port to canonical domain',
     598                        ),
     599                        'non-standard localhost port to canonical domain with SSL' => array(
     600                                'home_url'     => 'https://example.com',
     601                                'site_url'     => 'https://example.com',
     602                                'request_host' => 'localhost:10020',
     603                                'request_url'  => 'http://localhost:10018/',
     604                                'expected_url' => 'https://example.com/',
     605                                'failure_msg'  => 'Failed to redirect non-standard localhost port to canonical domain with SSL',
     606                        ),
     607                        'different host casing do not redirect' => array(
     608                                'home_url'     => 'http://example.com',
     609                                'site_url'     => 'http://example.com',
     610                                'request_host' => 'Example.com',
     611                                'request_url'  => 'http://Example.com/',
     612                                'expected_url' => null,
     613                                'failure_msg'  => 'Should not redirect when only host casing differs',
     614                        ),
     615                        'different host casing with port redirect without host change' => array(
     616                                'home_url'     => 'http://example.com:8080',
     617                                'site_url'     => 'http://example.com:8080',
     618                                'request_host' => 'Example.com:10200',
     619                                'request_url'  => 'http://Example.com:10200/',
     620                                'expected_url' => 'http://Example.com:8080/',
     621                                'failure_msg'  => 'Failed to redirect to correct port while preserving host casing',
     622                        ),
     623                        'www to non-www'                        => array(
     624                                'home_url'     => 'http://example.com',
     625                                'site_url'     => 'http://example.com',
     626                                'request_host' => 'www.example.com',
     627                                'request_url'  => 'http://www.example.com/',
     628                                'expected_url' => 'http://example.com/',
     629                                'failure_msg'  => 'Failed to redirect www to non-www domain',
     630                        ),
     631                        'non-www to www'                        => array(
     632                                'home_url'     => 'http://www.example.com',
     633                                'site_url'     => 'http://www.example.com',
     634                                'request_host' => 'example.com',
     635                                'request_url'  => 'http://example.com/',
     636                                'expected_url' => 'http://www.example.com/',
     637                                'failure_msg'  => 'Failed to redirect non-www to www domain',
     638                        ),
     639                        'port for same host'                    => array(
     640                                'home_url'     => 'http://example.com:8080',
     641                                'site_url'     => 'http://example.com:8080',
     642                                'request_host' => 'example.com:10200',
     643                                'request_url'  => 'http://example.com:10200/',
     644                                'expected_url' => 'http://example.com:8080/',
     645                                'failure_msg'  => 'Failed to redirect to correct port for same host',
     646                        ),
     647                );
     648        }
    548649}