|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the_privacy_policy_link(). |
| 5 | + * |
| 6 | + * @group link |
| 7 | + * @group privacy |
| 8 | + * |
| 9 | + * @covers ::the_privacy_policy_link |
| 10 | + */ |
| 11 | +class Tests_Link_ThePrivacyPolicyLink extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Privacy policy page ID. |
| 15 | + * |
| 16 | + * @var int |
| 17 | + */ |
| 18 | + private static $page_id = 0; |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates shared privacy policy page fixture. |
| 22 | + * |
| 23 | + * @param WP_UnitTest_Factory $factory Test factory. |
| 24 | + */ |
| 25 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 26 | + self::$page_id = $factory->post->create( |
| 27 | + array( |
| 28 | + 'post_type' => 'page', |
| 29 | + 'post_title' => 'Privacy Policy', |
| 30 | + 'post_status' => 'publish', |
| 31 | + ) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Sets the privacy policy page option before each test. |
| 37 | + */ |
| 38 | + public function set_up() { |
| 39 | + parent::set_up(); |
| 40 | + |
| 41 | + update_option( 'wp_page_for_privacy_policy', self::$page_id ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Tests that the_privacy_policy_link() echoes the privacy policy link. |
| 46 | + * |
| 47 | + * @ticket 65819 |
| 48 | + */ |
| 49 | + public function test_echoes_privacy_policy_link() { |
| 50 | + $output = get_echo( 'the_privacy_policy_link' ); |
| 51 | + |
| 52 | + $this->assertSame( get_the_privacy_policy_link(), $output ); |
| 53 | + $this->assertStringContainsString( 'class="privacy-policy-link"', $output ); |
| 54 | + $this->assertStringContainsString( 'rel="privacy-policy"', $output ); |
| 55 | + $this->assertStringContainsString( get_permalink( self::$page_id ), $output ); |
| 56 | + $this->assertStringContainsString( '>Privacy Policy</a>', $output ); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Tests that the_privacy_policy_link() includes before and after markup. |
| 61 | + * |
| 62 | + * @ticket 65819 |
| 63 | + */ |
| 64 | + public function test_includes_before_and_after_markup() { |
| 65 | + $before = '<span class="before">'; |
| 66 | + $after = '</span>'; |
| 67 | + $output = get_echo( 'the_privacy_policy_link', array( $before, $after ) ); |
| 68 | + |
| 69 | + $this->assertSame( get_the_privacy_policy_link( $before, $after ), $output ); |
| 70 | + $this->assertStringStartsWith( $before, $output ); |
| 71 | + $this->assertStringEndsWith( $after, $output ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Tests that the_privacy_policy_link() outputs nothing when privacy policy page is not configured. |
| 76 | + * |
| 77 | + * @ticket 65819 |
| 78 | + */ |
| 79 | + public function test_outputs_empty_string_when_page_is_not_configured() { |
| 80 | + delete_option( 'wp_page_for_privacy_policy' ); |
| 81 | + |
| 82 | + $this->assertSame( '', get_echo( 'the_privacy_policy_link' ) ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Tests that the_privacy_policy_link() outputs nothing when privacy policy page is not published. |
| 87 | + * |
| 88 | + * @ticket 65819 |
| 89 | + */ |
| 90 | + public function test_outputs_empty_string_when_page_is_not_published() { |
| 91 | + $draft_page_id = self::factory()->post->create( |
| 92 | + array( |
| 93 | + 'post_type' => 'page', |
| 94 | + 'post_title' => 'Draft Privacy Policy', |
| 95 | + 'post_status' => 'draft', |
| 96 | + ) |
| 97 | + ); |
| 98 | + |
| 99 | + update_option( 'wp_page_for_privacy_policy', $draft_page_id ); |
| 100 | + |
| 101 | + $this->assertSame( '', get_echo( 'the_privacy_policy_link' ) ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Tests that the_privacy_policy_link() reflects the 'the_privacy_policy_link' filter. |
| 106 | + * |
| 107 | + * @ticket 65819 |
| 108 | + */ |
| 109 | + public function test_applies_the_privacy_policy_link_filter() { |
| 110 | + $filter = static function () { |
| 111 | + return '<a href="https://example.org/privacy">Custom Privacy Link</a>'; |
| 112 | + }; |
| 113 | + |
| 114 | + add_filter( 'the_privacy_policy_link', $filter ); |
| 115 | + |
| 116 | + $output = get_echo( 'the_privacy_policy_link' ); |
| 117 | + |
| 118 | + remove_filter( 'the_privacy_policy_link', $filter ); |
| 119 | + |
| 120 | + $this->assertSame( '<a href="https://example.org/privacy">Custom Privacy Link</a>', $output ); |
| 121 | + } |
| 122 | +} |
0 commit comments