mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Canonical: Introduce wp_get_canonical_url().
`wp_get_canonical_url()` encapsulates the URL logic of `rel_canonical()` to provide a consistent way to retrieve the canonical URL for a post. The new filter `get_canonical_url` allows to customize the canonical URL. Props joostdevalk, jipmoors, DrewAPicture, ocean90. Fixes #36168. git-svn-id: https://develop.svn.wordpress.org/trunk@37685 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group link
|
||||
* @group canonical
|
||||
* @covers ::wp_get_canonical_url
|
||||
*/
|
||||
class Tests_WpGetCanonicalURL extends WP_UnitTestCase {
|
||||
public static $post_id;
|
||||
|
||||
public static function wpSetUpBeforeClass( $factory ) {
|
||||
self::$post_id = $factory->post->create( array(
|
||||
'post_status' => 'publish',
|
||||
) );
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
wp_delete_post( self::$post_id, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for a non existing post.
|
||||
*/
|
||||
public function test_non_existing_post() {
|
||||
$this->assertFalse( wp_get_canonical_url( -1 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for a post that is not published.
|
||||
*/
|
||||
public function test_post_status() {
|
||||
$post_id = self::factory()->post->create( array(
|
||||
'post_status' => 'draft',
|
||||
) );
|
||||
|
||||
$this->assertFalse( wp_get_canonical_url( $post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for a page that is not the queried object.
|
||||
*/
|
||||
public function test_non_current_page() {
|
||||
$this->assertEquals( get_permalink( self::$post_id ), wp_get_canonical_url( self::$post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test non permalink structure page usage.
|
||||
*/
|
||||
public function test_paged_with_plain_permalink_structure() {
|
||||
$link = add_query_arg( array(
|
||||
'page' => 2,
|
||||
'foo' => 'bar',
|
||||
), get_permalink( self::$post_id ) );
|
||||
|
||||
$this->go_to( $link );
|
||||
|
||||
$expected = add_query_arg( array(
|
||||
'page' => 2,
|
||||
), get_permalink( self::$post_id ) );
|
||||
|
||||
$this->assertEquals( $expected, wp_get_canonical_url( self::$post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test permalink structure page usage.
|
||||
*/
|
||||
public function test_paged_with_custom_permalink_structure() {
|
||||
$this->set_permalink_structure( '/%postname%/' );
|
||||
$page = 2;
|
||||
|
||||
$link = add_query_arg( array(
|
||||
'page' => $page,
|
||||
'foo' => 'bar',
|
||||
), get_permalink( self::$post_id ) );
|
||||
|
||||
$this->go_to( $link );
|
||||
|
||||
$expected = trailingslashit( get_permalink( self::$post_id ) ) . user_trailingslashit( $page, 'single_paged' );
|
||||
|
||||
$this->assertEquals( $expected, wp_get_canonical_url( self::$post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test non permalink structure comment page usage.
|
||||
*/
|
||||
public function test_comments_paged_with_plain_permalink_structure() {
|
||||
$cpage = 2;
|
||||
|
||||
$link = add_query_arg( array(
|
||||
'cpage' => $cpage,
|
||||
'foo' => 'bar',
|
||||
), get_permalink( self::$post_id ) );
|
||||
|
||||
$this->go_to( $link );
|
||||
|
||||
$expected = add_query_arg( array(
|
||||
'cpage' => $cpage,
|
||||
), get_permalink( self::$post_id ) . '#comments' );
|
||||
|
||||
$this->assertEquals( $expected , wp_get_canonical_url( self::$post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test permalink structure comment page usage.
|
||||
*/
|
||||
public function test_comments_paged_with_pretty_permalink_structure() {
|
||||
global $wp_rewrite;
|
||||
|
||||
$this->set_permalink_structure( '/%postname%/' );
|
||||
$cpage = 2;
|
||||
|
||||
$link = add_query_arg( array(
|
||||
'cpage' => $cpage,
|
||||
'foo' => 'bar',
|
||||
), get_permalink( self::$post_id ) );
|
||||
|
||||
$this->go_to( $link );
|
||||
|
||||
$expected = user_trailingslashit( trailingslashit( get_permalink( self::$post_id ) ) . $wp_rewrite->comments_pagination_base . '-' . $cpage, 'commentpaged' ) . '#comments';
|
||||
|
||||
$this->assertEquals( $expected , wp_get_canonical_url( self::$post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test calling of filter.
|
||||
*/
|
||||
public function test_get_canonical_url_filter() {
|
||||
add_filter( 'get_canonical_url', array( $this, 'canonical_url_filter' ) );
|
||||
$canonical_url = wp_get_canonical_url( self::$post_id );
|
||||
remove_filter( 'get_canonical_url', array( $this, 'canonical_url_filter' ) );
|
||||
|
||||
$this->assertEquals( $this->canonical_url_filter(), $canonical_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter callback for testing of filter usage.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function canonical_url_filter() {
|
||||
return 'http://canonical.example.org/';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user