robots.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Robots functions tests.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Tests for robots template functions and filters.
  9. *
  10. * @group robots
  11. */
  12. class Tests_Robots extends WP_UnitTestCase {
  13. public function setUp() {
  14. parent::setUp();
  15. remove_all_filters( 'wp_robots' );
  16. }
  17. /**
  18. * @ticket 51511
  19. */
  20. public function test_wp_robots_renders_when_relevant() {
  21. // Do not render robots meta tag when there are no directives.
  22. $output = get_echo( 'wp_robots' );
  23. $this->assertEmpty( $output );
  24. // Render robots meta tag with noindex.
  25. add_filter( 'wp_robots', array( $this, 'add_noindex_directive' ) );
  26. $output = get_echo( 'wp_robots' );
  27. $this->assertSame( "<meta name='robots' content='noindex' />\n", $output );
  28. // Do not render robots meta tag when there are only false-y directives.
  29. add_filter( 'wp_robots', array( $this, 'remove_noindex_directive' ), 11 );
  30. $output = get_echo( 'wp_robots' );
  31. $this->assertEmpty( $output );
  32. }
  33. /**
  34. * @ticket 51511
  35. */
  36. public function test_wp_robots_parses_directives_correctly() {
  37. add_filter(
  38. 'wp_robots',
  39. function( array $robots ) {
  40. // Directives that should have values must use strings.
  41. $robots['directive-with-value'] = 'yes';
  42. $robots['directive-with-numeric-value'] = '1';
  43. // Any non-string value will be evaluated as boolean.
  44. // False-y directives will not be included.
  45. $robots['directive-active-boolean'] = true;
  46. $robots['directive-inactive-boolean'] = false;
  47. $robots['directive-active-integer'] = 1;
  48. $robots['directive-inactive-integer'] = 0;
  49. return $robots;
  50. }
  51. );
  52. $expected_directives_string = implode(
  53. ', ',
  54. array(
  55. 'directive-with-value:yes',
  56. 'directive-with-numeric-value:1',
  57. 'directive-active-boolean',
  58. 'directive-active-integer',
  59. )
  60. );
  61. $output = get_echo( 'wp_robots' );
  62. $this->assertContains( "'{$expected_directives_string}'", $output );
  63. }
  64. /**
  65. * @ticket 51511
  66. */
  67. public function test_wp_robots_includes_basic_sanitization_follow_nofollow() {
  68. // Only follow or nofollow can be present, with follow taking precedence.
  69. add_filter( 'wp_robots', array( $this, 'add_follow_directive' ) );
  70. add_filter( 'wp_robots', array( $this, 'add_nofollow_directive' ) );
  71. $output = get_echo( 'wp_robots' );
  72. $this->assertContains( "'follow'", $output );
  73. // Consider truthyness of the directive value though.
  74. // Here nofollow is true, follow is false.
  75. add_filter( 'wp_robots', array( $this, 'remove_follow_directive' ), 11 );
  76. add_filter( 'wp_robots', array( $this, 'add_nofollow_directive' ), 11 );
  77. $output = get_echo( 'wp_robots' );
  78. $this->assertContains( "'nofollow'", $output );
  79. // Consider truthyness of the directive value though.
  80. // Here follow is true, nofollow is false.
  81. add_filter( 'wp_robots', array( $this, 'add_follow_directive' ), 12 );
  82. add_filter( 'wp_robots', array( $this, 'remove_nofollow_directive' ), 12 );
  83. $output = get_echo( 'wp_robots' );
  84. $this->assertContains( "'follow'", $output );
  85. }
  86. /**
  87. * @ticket 51511
  88. */
  89. public function test_wp_robots_includes_basic_sanitization_archive_noarchive() {
  90. // Only archive or noarchive can be present, with archive taking precedence.
  91. add_filter( 'wp_robots', array( $this, 'add_archive_directive' ) );
  92. add_filter( 'wp_robots', array( $this, 'add_noarchive_directive' ) );
  93. $output = get_echo( 'wp_robots' );
  94. $this->assertContains( "'archive'", $output );
  95. // Consider truthyness of the directive value though.
  96. // Here noarchive is true, archive is false.
  97. add_filter( 'wp_robots', array( $this, 'remove_archive_directive' ), 11 );
  98. add_filter( 'wp_robots', array( $this, 'add_noarchive_directive' ), 11 );
  99. $output = get_echo( 'wp_robots' );
  100. $this->assertContains( "'noarchive'", $output );
  101. // Consider truthyness of the directive value though.
  102. // Here archive is true, noarchive is false.
  103. add_filter( 'wp_robots', array( $this, 'add_archive_directive' ), 12 );
  104. add_filter( 'wp_robots', array( $this, 'remove_noarchive_directive' ), 12 );
  105. $output = get_echo( 'wp_robots' );
  106. $this->assertContains( "'archive'", $output );
  107. }
  108. /**
  109. * @ticket 51511
  110. */
  111. public function test_wp_robots_noindex() {
  112. add_filter( 'wp_robots', 'wp_robots_noindex' );
  113. update_option( 'blog_public', '1' );
  114. $output = get_echo( 'wp_robots' );
  115. $this->assertEmpty( $output );
  116. update_option( 'blog_public', '0' );
  117. $output = get_echo( 'wp_robots' );
  118. $this->assertContains( "'noindex, nofollow'", $output );
  119. }
  120. /**
  121. * @ticket 51511
  122. */
  123. public function test_wp_robots_no_robots() {
  124. add_filter( 'wp_robots', 'wp_robots_no_robots' );
  125. update_option( 'blog_public', '1' );
  126. $output = get_echo( 'wp_robots' );
  127. $this->assertContains( "'noindex, follow'", $output );
  128. update_option( 'blog_public', '0' );
  129. $output = get_echo( 'wp_robots' );
  130. $this->assertContains( "'noindex, nofollow'", $output );
  131. }
  132. /**
  133. * @ticket 51511
  134. */
  135. public function test_wp_robots_sensitive_page() {
  136. add_filter( 'wp_robots', 'wp_robots_sensitive_page' );
  137. $output = get_echo( 'wp_robots' );
  138. $this->assertContains( "'noindex, noarchive'", $output );
  139. }
  140. /**
  141. * @ticket 51511
  142. */
  143. public function test_wp_robots_max_image_preview_large() {
  144. add_filter( 'wp_robots', 'wp_robots_max_image_preview_large' );
  145. update_option( 'blog_public', '1' );
  146. $output = get_echo( 'wp_robots' );
  147. $this->assertContains( "'max-image-preview:large'", $output );
  148. update_option( 'blog_public', '0' );
  149. $output = get_echo( 'wp_robots' );
  150. $this->assertEmpty( $output );
  151. }
  152. /**
  153. * @ticket 52457
  154. */
  155. public function test_wp_robots_search_page() {
  156. add_filter( 'wp_robots', 'wp_robots_noindex_search' );
  157. $this->go_to( home_url( '?s=ticket+52457+core.trac.wordpress.org' ) );
  158. $output = get_echo( 'wp_robots' );
  159. $this->assertContains( 'noindex', $output );
  160. }
  161. /**
  162. * @ticket 52457
  163. */
  164. public function test_wp_robots_non_search_page() {
  165. add_filter( 'wp_robots', 'wp_robots_noindex_search' );
  166. $this->go_to( home_url() );
  167. $output = get_echo( 'wp_robots' );
  168. $this->assertNotContains( 'noindex', $output );
  169. }
  170. public function add_noindex_directive( array $robots ) {
  171. $robots['noindex'] = true;
  172. return $robots;
  173. }
  174. public function remove_noindex_directive( array $robots ) {
  175. $robots['noindex'] = false;
  176. return $robots;
  177. }
  178. public function add_follow_directive( array $robots ) {
  179. $robots['follow'] = true;
  180. return $robots;
  181. }
  182. public function remove_follow_directive( array $robots ) {
  183. $robots['follow'] = false;
  184. return $robots;
  185. }
  186. public function add_nofollow_directive( array $robots ) {
  187. $robots['nofollow'] = true;
  188. return $robots;
  189. }
  190. public function remove_nofollow_directive( array $robots ) {
  191. $robots['nofollow'] = false;
  192. return $robots;
  193. }
  194. public function add_archive_directive( array $robots ) {
  195. $robots['archive'] = true;
  196. return $robots;
  197. }
  198. public function remove_archive_directive( array $robots ) {
  199. $robots['archive'] = false;
  200. return $robots;
  201. }
  202. public function add_noarchive_directive( array $robots ) {
  203. $robots['noarchive'] = true;
  204. return $robots;
  205. }
  206. public function remove_noarchive_directive( array $robots ) {
  207. $robots['noarchive'] = false;
  208. return $robots;
  209. }
  210. }