Skip to content

Commit

Permalink
Tests: Improve get current URL tests
Browse files Browse the repository at this point in the history
The focus of these tests is on the parsing issues related to the domain, port, and potential path, and this is represented in the data provider.

However, it's good to be able to confirm that going to the homepage, random URL and a post via it's ID, also work well for each of the combinations in the first parts of the URL.
  • Loading branch information
GaryJones committed Sep 24, 2021
1 parent cb96150 commit 835a8a8
Showing 1 changed file with 41 additions and 64 deletions.
105 changes: 41 additions & 64 deletions tests/GetCurrentUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,83 @@ public function data_for_test_get_current_url() {
yield 'Home is http with force HTTPS true' => array(
'force_https' => true,
'home' => 'http://example.com',
'expected' => 'https://example.com/',
'expected' => 'https://example.com',
);

yield 'Home is https with force HTTPS true' => array(
'force_https' => true,
'home' => 'https://example.com',
'expected' => 'https://example.com/',
'expected' => 'https://example.com',
);

yield 'Home is http with port with force HTTPS true' => array(
'force_https' => true,
'home' => 'http://example.com:1234',
'expected' => 'https://example.com:1234/',
'expected' => 'https://example.com:1234',
);

yield 'Home is https with port with force HTTPS true' => array(
'force_https' => true,
'home' => 'https://example.com:1234',
'expected' => 'https://example.com:1234/',
'expected' => 'https://example.com:1234',
);

yield 'Home is http with port and path with force HTTPS true' => array(
'force_https' => true,
'home' => 'http://example.com:1234/foo/bar',
'expected' => 'https://example.com:1234/foo/bar/',
'expected' => 'https://example.com:1234/foo/bar',
);

yield 'Home is https with port and path with force HTTPS true' => array(
'force_https' => true,
'home' => 'https://example.com:1234/foo/bar',
'expected' => 'https://example.com:1234/foo/bar/',
'expected' => 'https://example.com:1234/foo/bar',
);

// Start cases with 'force_https_canonicals' = false.
yield 'Home is http with force HTTPS false' => array(
'force_https' => false,
'home' => 'http://example.com',
'expected' => 'http://example.com/',
'expected' => 'http://example.com',
);

yield 'Home is https with force HTTPS false' => array(
'force_https' => false,
'home' => 'https://example.com',
'expected' => 'http://example.com/',
'expected' => 'http://example.com',
);

yield 'Home is http with port with force HTTPS false' => array(
'force_https' => false,
'home' => 'http://example.com:1234',
'expected' => 'http://example.com:1234/',
'expected' => 'http://example.com:1234',
);

yield 'Home is https with port with force HTTPS false' => array(
'force_https' => false,
'home' => 'https://example.com:1234',
'expected' => 'http://example.com:1234/',
'expected' => 'http://example.com:1234',
);

yield 'Home is http with port and path with force HTTPS false' => array(
'force_https' => false,
'home' => 'http://example.com:1234/foo/bar',
'expected' => 'http://example.com:1234/foo/bar/',
'expected' => 'http://example.com:1234/foo/bar',
);

yield 'Home is https with port and path with force HTTPS false' => array(
'force_https' => false,
'home' => 'https://example.com:1234/foo/bar',
'expected' => 'http://example.com:1234/foo/bar/',
'expected' => 'http://example.com:1234/foo/bar',
);
}

/**
* Test the get_current_url() method.
*
* @testdox Given Force HTTPS is $force_https, when home is $home, then expect $expected.
* Assert that homepage, a specific page, and a random URL return the expected URL.
*
* @testdox Given Force HTTPS is $force_https, when home is $home, then expect URLs starting with $expected.
* @dataProvider data_for_test_get_current_url
* @covers \Parsely::get_current_url
* @uses \Parsely::get_options
Expand All @@ -107,76 +109,51 @@ public function data_for_test_get_current_url() {
* @param string $expected Expected current URL.
*/
public function test_get_current_url( $force_https, $home, $expected ) {
$parsely = new Parsely();
$options = get_option( Parsely::OPTIONS_KEY );
$options['force_https_canonicals'] = $force_https;
update_option( Parsely::OPTIONS_KEY, $options );

$this->set_options( array( 'force_https_canonicals' => $force_https ) );
update_option( 'home', $home );

// Test homepage.
$this->go_to( '/' );
$res = $parsely->get_current_url();
self::assertEquals( $expected, $res );
$this->assertCurrentUrlForHomepage( $expected );
$this->assertCurrentUrlForSpecificPostWithId( $expected );
$this->assertCurrentUrlForRandomUrl( $expected );
}

/**
* Test the get_current_url() method with a specific post (query parameter).
*
* @testdox Given Force HTTPS is $force_https, when home is $home, then expect $expected.
* @dataProvider data_for_test_get_current_url
* @covers \Parsely::get_current_url
* @uses \Parsely::get_options
* @uses \Parsely::update_metadata_endpoint
* Assert the correct current URL for the homepage.
*
* @param bool $force_https Force HTTPS Canonical setting value.
* @param string $home Home URL.
* @param string $expected Expected current URL.
* @param string $expected
*/
public function test_get_current_url_specific_post( $force_https, $home, $expected ) {
$parsely = new Parsely();
$options = get_option( Parsely::OPTIONS_KEY );
$options['force_https_canonicals'] = $force_https;
update_option( Parsely::OPTIONS_KEY, $options );
private function assertCurrentUrlForHomepage( $expected ) {
$this->go_to( '/' );

update_option( 'home', $home );
$parsely = new Parsely();
$res = $parsely->get_current_url();

self::assertEquals( $expected . '/', $res, 'Homepage page does not match.' );
}

// Test a specific post.
/**
* @param string $expected
*/
private function assertCurrentUrlForSpecificPostWithId( $expected ) {
$post_array = $this->create_test_post_array();
$post_id = $this->factory->post->create( $post_array );
$this->go_to( '/?p=' . $post_id );

$parsely = new Parsely();
$res = $parsely->get_current_url( 'post', $post_id );

$constructed_expected = $expected . '?p=' . $post_id;
self::assertEquals( $constructed_expected, $res );
self::assertEquals( $expected . '/?p=' . $post_id, $res, 'Specific post by ID does not match.' );
}

/**
* Test the get_current_url() method with a random URL.
*
* @testdox Given Force HTTPS is $force_https, when home is $home, then expect $expected.
* @dataProvider data_for_test_get_current_url
* @covers \Parsely::get_current_url
* @uses \Parsely::get_options
* @uses \Parsely::update_metadata_endpoint
*
* @param bool $force_https Force HTTPS Canonical setting value.
* @param string $home Home URL.
* @param string $expected Expected current URL.
* @param string $expected
*/
public function test_get_current_url_random( $force_https, $home, $expected ) {
$parsely = new Parsely();
$options = get_option( Parsely::OPTIONS_KEY );
$options['force_https_canonicals'] = $force_https;
update_option( Parsely::OPTIONS_KEY, $options );

update_option( 'home', $home );

// Test a random URL.
$this->go_to( '/random-url' );
private function assertCurrentUrlForRandomUrl( $expected ) {
$parsely = new Parsely();
$this->go_to( '/random/url' );
$res = $parsely->get_current_url();

$constructed_expected = $expected . 'random-url';
self::assertEquals( $constructed_expected, $res );
$constructed_expected = $expected . '/random/url';
self::assertEquals( $constructed_expected, $res, 'Random URL does not match.' );
}
}

0 comments on commit 835a8a8

Please sign in to comment.