getPrivacyPolicyUrl.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Define a class to test `get_privacy_policy_url()`.
  4. *
  5. * @package WordPress
  6. * @subpackage UnitTests
  7. * @since 4.9.6
  8. */
  9. /**
  10. * Test cases for `get_privacy_policy_url()`.
  11. *
  12. * @group url
  13. * @group privacy
  14. * @covers ::get_privacy_policy_url
  15. *
  16. * @since 4.9.6
  17. */
  18. class Tests_Url_GetPrivacyPolicyUrl 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. * Create fixtures that are shared by multiple test cases.
  35. *
  36. * @param WP_UnitTest_Factory $factory The base factory object.
  37. */
  38. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  39. self::$privacy_policy_page_id = $factory->post->create(
  40. array(
  41. 'post_type' => 'page',
  42. 'post_title' => WP_TESTS_DOMAIN . ' Privacy Policy',
  43. )
  44. );
  45. }
  46. /**
  47. * The function should return an empty string when `wp_page_for_privacy_policy` is _not_ set.
  48. */
  49. public function test_get_privacy_policy_url_should_return_empty_string_when_policy_page_not_set() {
  50. $this->assertSame( '', get_privacy_policy_url() );
  51. }
  52. /**
  53. * The function should return the privacy policy URL when `wp_page_for_privacy_policy` is set.
  54. */
  55. public function test_get_privacy_policy_url_should_return_valid_url_when_policy_page_set() {
  56. $privacy_policy_url = get_permalink( self::$privacy_policy_page_id );
  57. update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
  58. $this->assertSame( $privacy_policy_url, get_privacy_policy_url() );
  59. }
  60. /**
  61. * The function should return an empty string when `wp_page_for_privacy_policy` is _not_ set.
  62. */
  63. public function test_get_privacy_policy_url_should_return_empty_when_privacy_policy_page_not_set() {
  64. $this->assertSame( '', get_privacy_policy_url() );
  65. }
  66. /**
  67. * The function should return an empty string for an invalid `wp_page_for_privacy_policy` value.
  68. */
  69. public function test_get_privacy_policy_url_should_return_empty_for_non_existing_page() {
  70. update_option( 'wp_page_for_privacy_policy', PHP_INT_MAX );
  71. $this->assertSame( '', get_privacy_policy_url() );
  72. }
  73. /**
  74. * The output of `get_privacy_policy_url()` should be filterable with the 'privacy_policy_url' filter.
  75. */
  76. public function test_get_privacy_policy_url_should_be_filterable() {
  77. update_option( 'wp_page_for_privacy_policy', self::$privacy_policy_page_id );
  78. add_filter( 'privacy_policy_url', array( $this, 'modify_policy_url' ), 10, 2 );
  79. $this->assertSame( 'Page ID: ' . self::$privacy_policy_page_id, get_privacy_policy_url() );
  80. remove_filter( 'privacy_policy_url', array( $this, 'modify_policy_url' ), 10 );
  81. }
  82. /**
  83. * Return modified `privacy_policy_url` content in order to test the filter.
  84. *
  85. * @param string $url The URL to the privacy policy page. Empty string
  86. * if it doesn't exist.
  87. * @param int $policy_page_id The ID of privacy policy page.
  88. * @return string
  89. */
  90. public static function modify_policy_url( $url, $policy_page_id ) {
  91. return 'Page ID: ' . $policy_page_id;
  92. }
  93. }