getThePrivacyPolicyLink.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Define a class to test the `get_the_privacy_policy_link()` function.
  4. *
  5. * @package WordPress
  6. * @subpackage UnitTests
  7. * @since 4.9.6
  8. */
  9. /**
  10. * Test cases for the `get_the_privacy_policy_link()` function.
  11. *
  12. * @group link
  13. * @group privacy
  14. * @covers ::get_the_privacy_policy_link
  15. *
  16. * @since 4.9.6
  17. */
  18. class Tests_Link_GetThePrivacyPolicyLink extends WP_UnitTestCase {
  19. /**
  20. * The ID of the Privacy Policy page.
  21. *
  22. * @since 4.9.6
  23. * @var int $privacy_policy_page_id
  24. */
  25. protected static $privacy_policy_page_id;
  26. /**
  27. * The URL of the Privacy Policy page.
  28. *
  29. * @since 4.9.6
  30. * @var string $privacy_policy_url
  31. */
  32. protected static $privacy_policy_url;
  33. /**
  34. * The text that gets prepended to the `get_the_privacy_policy_link()` output.
  35. *
  36. * @since 4.9.6
  37. * @var string $before
  38. */
  39. protected static $before;
  40. /**
  41. * The text that gets appended to the `get_the_privacy_policy_link()` output.
  42. *
  43. * @since 4.9.6
  44. * @var string $after
  45. */
  46. protected static $after;
  47. /**
  48. * Create fixtures that are shared by multiple test cases.
  49. *
  50. * @param WP_UnitTest_Factory $factory The base factory object.
  51. */
  52. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  53. self::$privacy_policy_page_id = $factory->post->create(
  54. array(
  55. 'post_type' => 'page',
  56. 'post_title' => WP_TESTS_DOMAIN . ' Privacy Policy',
  57. )
  58. );
  59. // `esc_url()` is added for consistency with `get_the_privacy_policy_link()`.
  60. self::$privacy_policy_url = esc_url( get_permalink( self::$privacy_policy_page_id ) );
  61. self::$before = '<span class="privacy-policy-link-wrapper">';
  62. self::$after = '</span>';
  63. }
  64. /**
  65. * The function should return a valid link if a privacy policy page has been
  66. * created and set as the `wp_page_for_privacy_policy`. The post title should
  67. * be used as the link text.
  68. */
  69. public function test_get_the_privacy_policy_link_should_return_valid_link_when_privacy_page_set() {
  70. update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
  71. $actual_link = get_the_privacy_policy_link();
  72. $this->assertStringStartsWith( '<a', $actual_link );
  73. $this->assertContains( self::$privacy_policy_url, $actual_link );
  74. $this->assertStringEndsWith( '>' . WP_TESTS_DOMAIN . ' Privacy Policy</a>', $actual_link );
  75. }
  76. /**
  77. * The function should prepend the supplied `$before` markup and append the
  78. * supplied `$after` markup when the `wp_page_for_privacy_policy` is configured.
  79. */
  80. public function test_get_the_privacy_policy_link_should_prepend_and_append_supplied_markup_when_privacy_page_set() {
  81. update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
  82. $actual_link = get_the_privacy_policy_link( self::$before, self::$after );
  83. $this->assertStringStartsWith( self::$before . '<a', $actual_link );
  84. $this->assertContains( self::$privacy_policy_url, $actual_link );
  85. $this->assertStringEndsWith( '</a>' . self::$after, $actual_link );
  86. }
  87. /**
  88. * The function should _not_ prepend the supplied `$before` markup and append
  89. * the supplied `$after` markup when the `wp_page_for_privacy_policy` is _not_ configured.
  90. */
  91. public function test_get_the_privacy_policy_link_should_not_prepend_and_append_supplied_markup_when_privacy_page_not_set() {
  92. $actual_link = get_the_privacy_policy_link( self::$before, self::$after );
  93. $this->assertSame( '', $actual_link );
  94. }
  95. /**
  96. * The function should return an empty string when there is an empty page title
  97. * for the privacy policy.
  98. *
  99. * @ticket 44192
  100. */
  101. public function test_function_should_return_empty_string_when_privacy_page_title_empty() {
  102. $nameless_page_id = $this->factory->post->create(
  103. array(
  104. 'post_type' => 'page',
  105. 'post_title' => '',
  106. )
  107. );
  108. update_option( 'wp_page_for_privacy_policy', $nameless_page_id );
  109. $this->assertSame( '', get_the_privacy_policy_link( self::$before, self::$after ) );
  110. }
  111. /**
  112. * The function should return an empty string when `wp_page_for_privacy_policy` is _not_ configured.
  113. */
  114. public function test_get_the_privacy_policy_link_should_return_empty_string_when_privacy_page_not_set() {
  115. $this->assertSame( '', get_the_privacy_policy_link() );
  116. }
  117. /**
  118. * The output of the get_the_privacy_policy_link() function should be filterable with the 'privacy_policy_link' filter.
  119. */
  120. public function test_get_the_privacy_policy_link_should_be_filterable() {
  121. update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
  122. $expected_url = get_privacy_policy_url();
  123. $this->assertNotEmpty( $expected_url );
  124. add_filter( 'the_privacy_policy_link', array( $this, 'modify_link_markup' ), 10, 2 );
  125. $this->assertSame( 'Policy: ' . $expected_url, get_the_privacy_policy_link() );
  126. remove_filter( 'the_privacy_policy_link', array( $this, 'modify_link_markup' ), 10 );
  127. }
  128. /**
  129. * Return modified `the_privacy_policy_link` content in order to test the filter.
  130. *
  131. * @param string $link The privacy policy link. Empty string if it
  132. * doesn't exist.
  133. * @param string $privacy_policy_url The URL of the privacy policy. Empty string
  134. * if it doesn't exist.
  135. * @return string
  136. */
  137. public static function modify_link_markup( $link, $privacy_policy_url ) {
  138. return 'Policy: ' . $privacy_policy_url;
  139. }
  140. }