getTheExcerpt.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @group post
  4. * @group formatting
  5. */
  6. class Tests_Post_GetTheExcerpt extends WP_UnitTestCase {
  7. /**
  8. * @ticket 27246
  9. */
  10. public function test_the_excerpt_invalid_post() {
  11. $this->assertSame( '', get_echo( 'the_excerpt' ) );
  12. $this->assertSame( '', get_the_excerpt() );
  13. }
  14. /**
  15. * @ticket 27246
  16. * @expectedDeprecated get_the_excerpt
  17. */
  18. public function test_the_excerpt_deprecated() {
  19. $this->assertSame( '', get_the_excerpt( true ) );
  20. $this->assertSame( '', get_the_excerpt( false ) );
  21. }
  22. /**
  23. * @ticket 27246
  24. */
  25. public function test_the_excerpt() {
  26. $GLOBALS['post'] = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Post excerpt' ) );
  27. $this->assertSame( "<p>Post excerpt</p>\n", get_echo( 'the_excerpt' ) );
  28. $this->assertSame( 'Post excerpt', get_the_excerpt() );
  29. }
  30. /**
  31. * @ticket 27246
  32. * @ticket 35486
  33. */
  34. public function test_the_excerpt_password_protected_post() {
  35. $post = self::factory()->post->create_and_get(
  36. array(
  37. 'post_excerpt' => 'Post excerpt',
  38. 'post_password' => '1234',
  39. )
  40. );
  41. $this->assertSame( 'There is no excerpt because this is a protected post.', get_the_excerpt( $post ) );
  42. $GLOBALS['post'] = $post;
  43. $this->assertSame( "<p>There is no excerpt because this is a protected post.</p>\n", get_echo( 'the_excerpt' ) );
  44. }
  45. /**
  46. * @ticket 27246
  47. */
  48. public function test_the_excerpt_specific_post() {
  49. $GLOBALS['post'] = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Foo' ) );
  50. $post_id = self::factory()->post->create( array( 'post_excerpt' => 'Bar' ) );
  51. $this->assertSame( 'Bar', get_the_excerpt( $post_id ) );
  52. }
  53. /**
  54. * @ticket 42814
  55. */
  56. public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_inferred_from_context() {
  57. $post_id = self::factory()->post->create(
  58. array(
  59. 'post_content' => 'Foo',
  60. 'post_excerpt' => '',
  61. )
  62. );
  63. $q = new WP_Query(
  64. array(
  65. 'p' => $post_id,
  66. )
  67. );
  68. while ( $q->have_posts() ) {
  69. $q->the_post();
  70. $found = get_the_excerpt();
  71. }
  72. $this->assertSame( 'Foo', $found );
  73. }
  74. /**
  75. * @ticket 42814
  76. */
  77. public function test_should_fall_back_on_post_content_if_excerpt_is_empty_and_post_is_provided() {
  78. $GLOBALS['post'] = self::factory()->post->create_and_get(
  79. array(
  80. 'post_content' => 'Foo',
  81. 'post_excerpt' => '',
  82. )
  83. );
  84. $this->assertSame( 'Foo', get_the_excerpt( $GLOBALS['post'] ) );
  85. }
  86. /**
  87. * @ticket 42814
  88. */
  89. public function test_should_respect_post_parameter_in_the_loop() {
  90. $p1 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Foo' ) );
  91. $p2 = self::factory()->post->create_and_get( array( 'post_excerpt' => 'Bar' ) );
  92. $q = new WP_Query(
  93. array(
  94. 'p' => $p1->ID,
  95. )
  96. );
  97. while ( $q->have_posts() ) {
  98. $q->the_post();
  99. $found = get_the_excerpt( $p2 );
  100. }
  101. $this->assertSame( 'Bar', $found );
  102. }
  103. /**
  104. * @ticket 42814
  105. */
  106. public function test_should_respect_post_parameter_in_the_loop_when_falling_back_on_post_content() {
  107. $p1 = self::factory()->post->create_and_get(
  108. array(
  109. 'post_content' => 'Foo',
  110. 'post_excerpt' => '',
  111. )
  112. );
  113. $p2 = self::factory()->post->create_and_get(
  114. array(
  115. 'post_content' => 'Bar',
  116. 'post_excerpt' => '',
  117. )
  118. );
  119. $q = new WP_Query(
  120. array(
  121. 'p' => $p1->ID,
  122. )
  123. );
  124. while ( $q->have_posts() ) {
  125. $q->the_post();
  126. $found = get_the_excerpt( $p2 );
  127. }
  128. $this->assertSame( 'Bar', $found );
  129. }
  130. }