formats.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @group post
  4. */
  5. class Tests_Post_Formats extends WP_UnitTestCase {
  6. function test_set_get_post_format_for_post() {
  7. $post_id = self::factory()->post->create();
  8. $format = get_post_format( $post_id );
  9. $this->assertFalse( $format );
  10. $result = set_post_format( $post_id, 'aside' );
  11. $this->assertNotWPError( $result );
  12. $this->assertInternalType( 'array', $result );
  13. $this->assertSame( 1, count( $result ) );
  14. $format = get_post_format( $post_id );
  15. $this->assertSame( 'aside', $format );
  16. $result = set_post_format( $post_id, 'standard' );
  17. $this->assertNotWPError( $result );
  18. $this->assertInternalType( 'array', $result );
  19. $this->assertSame( 0, count( $result ) );
  20. $result = set_post_format( $post_id, '' );
  21. $this->assertNotWPError( $result );
  22. $this->assertInternalType( 'array', $result );
  23. $this->assertSame( 0, count( $result ) );
  24. }
  25. /**
  26. * @ticket 22473
  27. */
  28. function test_set_get_post_format_for_page() {
  29. $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
  30. $format = get_post_format( $post_id );
  31. $this->assertFalse( $format );
  32. $result = set_post_format( $post_id, 'aside' );
  33. $this->assertNotWPError( $result );
  34. $this->assertInternalType( 'array', $result );
  35. $this->assertSame( 1, count( $result ) );
  36. // The format can be set but not retrieved until it is registered.
  37. $format = get_post_format( $post_id );
  38. $this->assertFalse( $format );
  39. // Register format support for the page post type.
  40. add_post_type_support( 'page', 'post-formats' );
  41. // The previous set can now be retrieved.
  42. $format = get_post_format( $post_id );
  43. $this->assertSame( 'aside', $format );
  44. $result = set_post_format( $post_id, 'standard' );
  45. $this->assertNotWPError( $result );
  46. $this->assertInternalType( 'array', $result );
  47. $this->assertSame( 0, count( $result ) );
  48. $result = set_post_format( $post_id, '' );
  49. $this->assertNotWPError( $result );
  50. $this->assertInternalType( 'array', $result );
  51. $this->assertSame( 0, count( $result ) );
  52. remove_post_type_support( 'page', 'post-formats' );
  53. }
  54. function test_has_format() {
  55. $post_id = self::factory()->post->create();
  56. $this->assertFalse( has_post_format( 'standard', $post_id ) );
  57. $this->assertFalse( has_post_format( '', $post_id ) );
  58. $result = set_post_format( $post_id, 'aside' );
  59. $this->assertNotWPError( $result );
  60. $this->assertInternalType( 'array', $result );
  61. $this->assertSame( 1, count( $result ) );
  62. $this->assertTrue( has_post_format( 'aside', $post_id ) );
  63. $result = set_post_format( $post_id, 'standard' );
  64. $this->assertNotWPError( $result );
  65. $this->assertInternalType( 'array', $result );
  66. $this->assertSame( 0, count( $result ) );
  67. // Standard is a special case. It shows as false when set.
  68. $this->assertFalse( has_post_format( 'standard', $post_id ) );
  69. // Dummy format type.
  70. $this->assertFalse( has_post_format( 'dummy', $post_id ) );
  71. // Dummy post ID.
  72. $this->assertFalse( has_post_format( 'aside', 12345 ) );
  73. }
  74. /**
  75. * @ticket 23570
  76. */
  77. function test_get_url_in_content() {
  78. $link = 'http://nytimes.com';
  79. $commentary = 'This is my favorite link';
  80. $link_with_commentary = <<<DATA
  81. $link
  82. $commentary
  83. DATA;
  84. $href = '<a href="http://nytimes.com">NYT</a>';
  85. $href_with_commentary = <<<DATA
  86. $href
  87. $commentary
  88. DATA;
  89. $link_post_id = self::factory()->post->create( array( 'post_content' => $link ) );
  90. $content_link = get_url_in_content( get_post_field( 'post_content', $link_post_id ) );
  91. $this->assertFalse( $content_link );
  92. $link_with_post_id = self::factory()->post->create( array( 'post_content' => $link_with_commentary ) );
  93. $content_link = get_url_in_content( get_post_field( 'post_content', $link_with_post_id ) );
  94. $this->assertFalse( $content_link );
  95. $content_link = get_url_in_content( get_post_field( 'post_content', $link_post_id ) );
  96. $this->assertFalse( $content_link );
  97. $content_link = get_url_in_content( get_post_field( 'post_content', $link_with_post_id ) );
  98. $this->assertFalse( $content_link );
  99. $empty_post_id = self::factory()->post->create( array( 'post_content' => '' ) );
  100. $content_link = get_url_in_content( get_post_field( 'post_content', $empty_post_id ) );
  101. $this->assertFalse( $content_link );
  102. $comm_post_id = self::factory()->post->create( array( 'post_content' => $commentary ) );
  103. $content_link = get_url_in_content( get_post_field( 'post_content', $comm_post_id ) );
  104. $this->assertFalse( $content_link );
  105. // Now with an href.
  106. $href_post_id = self::factory()->post->create( array( 'post_content' => $href ) );
  107. $content_link = get_url_in_content( get_post_field( 'post_content', $href_post_id ) );
  108. $this->assertSame( $link, $content_link );
  109. $href_with_post_id = self::factory()->post->create( array( 'post_content' => $href_with_commentary ) );
  110. $content_link = get_url_in_content( get_post_field( 'post_content', $href_with_post_id ) );
  111. $this->assertSame( $link, $content_link );
  112. $content_link = get_url_in_content( get_post_field( 'post_content', $href_post_id ) );
  113. $this->assertSame( $link, $content_link );
  114. $content_link = get_url_in_content( get_post_field( 'post_content', $href_with_post_id ) );
  115. $this->assertSame( $link, $content_link );
  116. $empty_post_id = self::factory()->post->create( array( 'post_content' => '' ) );
  117. $content_link = get_url_in_content( get_post_field( 'post_content', $empty_post_id ) );
  118. $this->assertFalse( $content_link );
  119. $comm_post_id = self::factory()->post->create( array( 'post_content' => $commentary ) );
  120. $content_link = get_url_in_content( get_post_field( 'post_content', $comm_post_id ) );
  121. $this->assertFalse( $content_link );
  122. }
  123. }