Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving reliability of get_current_url tests #398

Merged
merged 3 commits into from
Sep 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions tests/GetCurrentUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public function data_for_test_get_current_url() {
/**
* 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,28 +109,55 @@ 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->assertCurrentUrlForHomepage( $expected );
$this->assertCurrentUrlForSpecificPostWithId( $expected );
$this->assertCurrentUrlForRandomUrl( $expected );
}

/**
* Assert the correct current URL for the homepage.
*
* @param string $expected Expected start of the URL.
*/
private function assertCurrentUrlForHomepage( $expected ) {
$this->go_to( '/' );
$res = $parsely->get_current_url();
self::assertStringStartsWith( $expected, $res );

// Test a specific post.
$parsely = new Parsely();
$res = $parsely->get_current_url();

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

/**
* Assert the correct current URL for a post by ID.
*
* @param string $expected Expected start of the URL.
*/
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 );
$res = $parsely->get_current_url( 'post', $post_id );
self::assertStringStartsWith( $expected, $res );

// Test a random URL.
$this->go_to( '/random-url' );
$parsely = new Parsely();
$res = $parsely->get_current_url( 'post', $post_id );

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

/**
* Assert the correct current URL for a random URL with trailing slash.
*
* @param string $expected Expected start of the URL.
*/
private function assertCurrentUrlForRandomUrl( $expected ) {
$parsely = new Parsely();
$this->go_to( '/random/url/' );
$res = $parsely->get_current_url();
self::assertStringStartsWith( $expected, $res );

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