|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the `get_trackback_url()` and `trackback_url()` functions. |
| 5 | + * |
| 6 | + * @group comment |
| 7 | + * @group link |
| 8 | + */ |
| 9 | +class Tests_Comment_GetTrackbackUrl extends WP_UnitTestCase { |
| 10 | + |
| 11 | + /** |
| 12 | + * Post ID. |
| 13 | + * |
| 14 | + * @var int |
| 15 | + */ |
| 16 | + private static $post_id; |
| 17 | + |
| 18 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 19 | + self::$post_id = $factory->post->create( |
| 20 | + array( |
| 21 | + 'post_name' => 'hello-trackback', |
| 22 | + 'post_date' => '2024-03-15 10:00:00', |
| 23 | + ) |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + public function set_up() { |
| 28 | + parent::set_up(); |
| 29 | + |
| 30 | + $GLOBALS['post'] = get_post( self::$post_id ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Tests the trackback URL with plain permalinks. |
| 35 | + * |
| 36 | + * @ticket 65819 |
| 37 | + * |
| 38 | + * @covers ::get_trackback_url |
| 39 | + */ |
| 40 | + public function test_plain_permalinks() { |
| 41 | + $this->set_permalink_structure( '' ); |
| 42 | + |
| 43 | + $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/wp-trackback.php?p=' . self::$post_id, get_trackback_url() ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Tests the trackback URL with pretty permalinks. |
| 48 | + * |
| 49 | + * @ticket 65819 |
| 50 | + * |
| 51 | + * @covers ::get_trackback_url |
| 52 | + * |
| 53 | + * @dataProvider data_pretty_permalinks |
| 54 | + * |
| 55 | + * @param string $structure Permalink structure. |
| 56 | + * @param string $expected Expected path relative to home_url(). |
| 57 | + */ |
| 58 | + public function test_pretty_permalinks( $structure, $expected ) { |
| 59 | + $this->set_permalink_structure( $structure ); |
| 60 | + |
| 61 | + $this->assertSame( home_url( $expected ), get_trackback_url() ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Data provider for test_pretty_permalinks(). |
| 66 | + * |
| 67 | + * @return array[] |
| 68 | + */ |
| 69 | + public static function data_pretty_permalinks() { |
| 70 | + return array( |
| 71 | + 'with trailing slash' => array( '/%postname%/', '/hello-trackback/trackback/' ), |
| 72 | + 'without trailing slash' => array( '/%postname%', '/hello-trackback/trackback' ), |
| 73 | + 'date based' => array( '/%year%/%postname%/', '/2024/hello-trackback/trackback/' ), |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Tests that plain permalinks use the site URL and pretty permalinks use the home URL. |
| 79 | + * |
| 80 | + * @ticket 65819 |
| 81 | + * |
| 82 | + * @covers ::get_trackback_url |
| 83 | + */ |
| 84 | + public function test_uses_site_url_for_plain_and_home_url_for_pretty_permalinks() { |
| 85 | + update_option( 'siteurl', 'http://' . WP_TESTS_DOMAIN . '/wp' ); |
| 86 | + |
| 87 | + $this->set_permalink_structure( '' ); |
| 88 | + $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/wp/wp-trackback.php?p=' . self::$post_id, get_trackback_url(), 'Plain permalinks should use the site URL.' ); |
| 89 | + |
| 90 | + $this->set_permalink_structure( '/%postname%/' ); |
| 91 | + $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/hello-trackback/trackback/', get_trackback_url(), 'Pretty permalinks should use the home URL.' ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Tests the argument passed to the 'trackback_url' filter and that its return value is used. |
| 96 | + * |
| 97 | + * @ticket 65819 |
| 98 | + * |
| 99 | + * @covers ::get_trackback_url |
| 100 | + */ |
| 101 | + public function test_trackback_url_filter() { |
| 102 | + $this->set_permalink_structure( '/%postname%/' ); |
| 103 | + |
| 104 | + $filter = new MockAction(); |
| 105 | + add_filter( 'trackback_url', array( $filter, 'filter' ) ); |
| 106 | + add_filter( |
| 107 | + 'trackback_url', |
| 108 | + static function ( $trackback_url ) { |
| 109 | + return $trackback_url . '?filtered=1'; |
| 110 | + }, |
| 111 | + 20 |
| 112 | + ); |
| 113 | + |
| 114 | + $trackback_url = get_trackback_url(); |
| 115 | + |
| 116 | + $this->assertSame( array( home_url( '/hello-trackback/trackback/' ) ), $filter->get_args()[0], 'The filter should receive the trackback URL.' ); |
| 117 | + $this->assertSame( home_url( '/hello-trackback/trackback/?filtered=1' ), $trackback_url, 'The filtered URL should be returned.' ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Tests that trackback_url() displays the trackback URL by default. |
| 122 | + * |
| 123 | + * @ticket 65819 |
| 124 | + * |
| 125 | + * @covers ::trackback_url |
| 126 | + */ |
| 127 | + public function test_trackback_url_displays_url() { |
| 128 | + $this->set_permalink_structure( '/%postname%/' ); |
| 129 | + |
| 130 | + $this->expectOutputString( home_url( '/hello-trackback/trackback/' ) ); |
| 131 | + |
| 132 | + $this->assertNull( trackback_url() ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Tests that trackback_url() returns the URL with a deprecation notice when $deprecated_echo is false. |
| 137 | + * |
| 138 | + * @ticket 65819 |
| 139 | + * |
| 140 | + * @covers ::trackback_url |
| 141 | + * |
| 142 | + * @expectedDeprecated trackback_url |
| 143 | + */ |
| 144 | + public function test_trackback_url_returns_url_when_echo_is_false() { |
| 145 | + $this->set_permalink_structure( '/%postname%/' ); |
| 146 | + |
| 147 | + $this->expectOutputString( '' ); |
| 148 | + |
| 149 | + $this->assertSame( home_url( '/hello-trackback/trackback/' ), trackback_url( false ) ); |
| 150 | + } |
| 151 | +} |
0 commit comments