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

Canonical URL Tests for Attachments #8571

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/wp-includes/link-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -4063,7 +4063,7 @@ function wp_get_canonical_url( $post = null ) {
return false;
}

if ( 'publish' !== $post->post_status ) {
if ( 'publish' !== get_post_status( $post ) ) {
return false;
}

Expand Down
49 changes: 48 additions & 1 deletion tests/phpunit/tests/link/wpGetCanonicalUrl.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
<?php
/**
* Tests for the Get Canonical Url function.
*
* @package WordPress
* @subpackage Link
*/

/**
* Class for Testing the Get Canonical Url function.
*
* @group link
* @group canonical
* @covers ::wp_get_canonical_url
*/
class Tests_Link_wpGetCanonicalUrl extends WP_UnitTestCase {
class Tests_Link_WpGetCanonicalUrl extends WP_UnitTestCase {

/**
* The ID of the post.
*
* @var int
*/
public static $post_id;

/**
* The ID of the attachment.
*
* @var int
*/
public static $attachment_id;

/**
* Sets up the test environment before any tests are run.
*
* @param WP_UnitTest_Factory $factory The factory object.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create(
array(
'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3',
'post_status' => 'publish',
)
);

self::$attachment_id = $factory->attachment->create_object(
array(
'file' => DIR_TESTDATA . '/images/canola.jpg',
'post_parent' => self::$post_id,
'post_status' => 'inherit',
)
);
}

/**
Expand Down Expand Up @@ -140,6 +174,19 @@ public function test_comments_paged_with_pretty_permalink_structure() {
$this->assertSame( $expected, wp_get_canonical_url( self::$post_id ) );
}

/**
* This test ensures that attachments with 'inherit' status properly receive a canonical URL.
*
* @ticket 63041
*/
public function test_attachment_canonical_url() {
$this->go_to( get_attachment_link( self::$attachment_id ) );
$canonical_url = wp_get_canonical_url( self::$attachment_id );

$this->assertNotFalse( $canonical_url, 'Attachment should have a canonical URL' );
$this->assertSame( get_attachment_link( self::$attachment_id ), $canonical_url, 'Canonical URL should match the attachment permalink' );
}

/**
* Test calling of filter.
*/
Expand Down
Loading