WPRelNoFollow.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Rel_No_Follow extends WP_UnitTestCase {
  6. /**
  7. * @ticket 9959
  8. */
  9. public function test_add_no_follow() {
  10. $content = '<p>This is some cool <a href="/">Code</a></p>';
  11. $expected = '<p>This is some cool <a href=\"/\" rel=\"nofollow\">Code</a></p>';
  12. $this->assertSame( $expected, wp_rel_nofollow( $content ) );
  13. }
  14. /**
  15. * @ticket 9959
  16. */
  17. public function test_convert_no_follow() {
  18. $content = '<p>This is some cool <a href="/" rel="weird">Code</a></p>';
  19. $expected = '<p>This is some cool <a href=\"/\" rel=\"weird nofollow\">Code</a></p>';
  20. $this->assertSame( $expected, wp_rel_nofollow( $content ) );
  21. }
  22. /**
  23. * @ticket 11360
  24. * @dataProvider data_wp_rel_nofollow
  25. */
  26. public function test_wp_rel_nofollow( $input, $output ) {
  27. return $this->assertSame( wp_slash( $output ), wp_rel_nofollow( $input ) );
  28. }
  29. public function data_wp_rel_nofollow() {
  30. $home_url_http = set_url_scheme( home_url(), 'http' );
  31. $home_url_https = set_url_scheme( home_url(), 'https' );
  32. return array(
  33. array(
  34. '<a href="">Double Quotes</a>',
  35. '<a href="" rel="nofollow">Double Quotes</a>',
  36. ),
  37. array(
  38. '<a href="https://wordpress.org">Double Quotes</a>',
  39. '<a href="https://wordpress.org" rel="nofollow">Double Quotes</a>',
  40. ),
  41. array(
  42. "<a href='https://wordpress.org'>Single Quotes</a>",
  43. "<a href='https://wordpress.org' rel=\"nofollow\">Single Quotes</a>",
  44. ),
  45. array(
  46. '<a href="https://wordpress.org" title="Title">Multiple attributes</a>',
  47. '<a href="https://wordpress.org" title="Title" rel="nofollow">Multiple attributes</a>',
  48. ),
  49. array(
  50. '<a title="Title" href="https://wordpress.org">Multiple attributes</a>',
  51. '<a title="Title" href="https://wordpress.org" rel="nofollow">Multiple attributes</a>',
  52. ),
  53. array(
  54. '<a data-someflag href="https://wordpress.org">Multiple attributes</a>',
  55. '<a data-someflag href="https://wordpress.org" rel="nofollow">Multiple attributes</a>',
  56. ),
  57. array(
  58. '<a data-someflag title="Title" href="https://wordpress.org" onclick="" >Everything at once</a>',
  59. '<a data-someflag title="Title" href="https://wordpress.org" onclick="" rel="nofollow">Everything at once</a>',
  60. ),
  61. array(
  62. '<a href="' . $home_url_http . '/some-url">Home URL (http)</a>',
  63. '<a href="' . $home_url_http . '/some-url">Home URL (http)</a>',
  64. ),
  65. array(
  66. '<a href="' . $home_url_https . '/some-url">Home URL (https)</a>',
  67. '<a href="' . $home_url_https . '/some-url">Home URL (https)</a>',
  68. ),
  69. );
  70. }
  71. public function test_append_no_follow_with_valueless_attribute() {
  72. $content = '<p>This is some cool <a href="demo.com" download rel="hola">Code</a></p>';
  73. $expected = '<p>This is some cool <a href=\"demo.com\" download rel=\"hola nofollow\">Code</a></p>';
  74. $this->assertSame( $expected, wp_rel_nofollow( $content ) );
  75. }
  76. }