summaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorJb Audras <audrasjb@git.wordpress.org>2025-03-30 23:32:38 +0000
committerJb Audras <audrasjb@git.wordpress.org>2025-03-30 23:32:38 +0000
commitca8b2f54f13354c83d34aecd359159548b5e2612 (patch)
tree6455d4a513010fab2fcbe2b82b4e3296bdae828a /tests
parentfc1990dbd27523d4dccee56b539da4a55d2ce0c1 (diff)
downloadwordpress-ca8b2f54f13354c83d34aecd359159548b5e2612.tar.gz
wordpress-ca8b2f54f13354c83d34aecd359159548b5e2612.zip
Canonical: Use `get_post_status()` to determine whether the post has a `publish` post status in `wp_get_canonical_url()`.
This changeset fixes an issue where `wp_get_canonical_url()` always returned `false` for attachments. Attachments have a `inherit` post status rather than `publish`. The `wp_get_canonical_url()` function was directly checking `$post->post_status === 'publish'`, causing it to always return `false` for attachments. Using `get_post_status()` fixes the issue as if the post is an attachment, then the parent post status will be given instead. Follow-up to [37685]. Props othernoel, ankitkumarshah, SirLouen, Fixes #63041. git-svn-id: https://develop.svn.wordpress.org/trunk@60108 602fd350-edb4-49c9-b593-d223f7449a82
Diffstat (limited to 'tests')
-rw-r--r--tests/phpunit/tests/link/wpGetCanonicalUrl.php48
1 files changed, 47 insertions, 1 deletions
diff --git a/tests/phpunit/tests/link/wpGetCanonicalUrl.php b/tests/phpunit/tests/link/wpGetCanonicalUrl.php
index 5b7eddaada..fb1f536e3f 100644
--- a/tests/phpunit/tests/link/wpGetCanonicalUrl.php
+++ b/tests/phpunit/tests/link/wpGetCanonicalUrl.php
@@ -1,13 +1,38 @@
<?php
+/**
+ * Tests for the wp_get_canonical_url() function.
+ *
+ * @package WordPress
+ * @subpackage Link
+ */
/**
+ * Class for Testing the wp_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(
@@ -15,6 +40,14 @@ class Tests_Link_wpGetCanonicalUrl extends WP_UnitTestCase {
'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',
+ )
+ );
}
/**
@@ -141,6 +174,19 @@ class Tests_Link_wpGetCanonicalUrl extends WP_UnitTestCase {
}
/**
+ * 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.
*/
public function test_get_canonical_url_filter() {