query.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. class Tests_Query extends WP_UnitTestCase {
  3. /**
  4. * @ticket 16746
  5. */
  6. function test_nextpage_at_start_of_content() {
  7. $post = $this->factory->post->create_and_get( array( 'post_content' => '<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
  8. setup_postdata( $post );
  9. $this->assertEquals( 1, $GLOBALS['multipage'] );
  10. $this->assertCount( 3, $GLOBALS['pages'] );
  11. $this->assertEquals( 3, $GLOBALS['numpages'] );
  12. $this->assertEquals( array( 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] );
  13. }
  14. function test_setup_postdata_single_page() {
  15. $post = $this->factory->post->create_and_get( array( 'post_content' => 'Page 0' ) );
  16. setup_postdata( $post );
  17. $this->assertEquals( 0, $GLOBALS['multipage'] );
  18. $this->assertCount( 1, $GLOBALS['pages'] );
  19. $this->assertEquals( 1, $GLOBALS['numpages'] );
  20. $this->assertEquals( array( 'Page 0' ), $GLOBALS['pages'] );
  21. }
  22. function test_setup_postdata_multi_page() {
  23. $post = $this->factory->post->create_and_get( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
  24. setup_postdata( $post );
  25. $this->assertEquals( 1, $GLOBALS['multipage'] );
  26. $this->assertCount( 4, $GLOBALS['pages'] );
  27. $this->assertEquals( 4, $GLOBALS['numpages'] );
  28. $this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] );
  29. }
  30. /**
  31. * @ticket 24330
  32. *
  33. * setup_postdata( $a_post ) followed by the_content() in a loop that does not update
  34. * global $post should use the content of $a_post rather then the global post.
  35. */
  36. function test_setup_postdata_loop() {
  37. $post_id = $this->factory->post->create( array( 'post_content' => 'global post' ) );
  38. $GLOBALS['wp_query']->post = $GLOBALS['post'] = get_post( $post_id );
  39. $ids = $this->factory->post->create_many(5);
  40. foreach ( $ids as $id ) {
  41. $page = get_post( $id );
  42. if ( $page ) {
  43. setup_postdata( $page );
  44. $content = get_echo( 'the_content', array() );
  45. $this->assertEquals( $post_id, $GLOBALS['post']->ID );
  46. $this->assertNotEquals( '<p>global post</p>', strip_ws( $content ) );
  47. wp_reset_postdata();
  48. }
  49. }
  50. }
  51. /**
  52. * @ticket 24785
  53. *
  54. */
  55. function test_nested_loop_reset_postdata() {
  56. $post_id = $this->factory->post->create();
  57. $nested_post_id = $this->factory->post->create();
  58. $first_query = new WP_Query( array( 'post__in' => array( $post_id ) ) );
  59. while ( $first_query->have_posts() ) { $first_query->the_post();
  60. $second_query = new WP_Query( array( 'post__in' => array( $nested_post_id ) ) );
  61. while ( $second_query->have_posts() ) {
  62. $second_query->the_post();
  63. $this->assertEquals( get_the_ID(), $nested_post_id );
  64. }
  65. $first_query->reset_postdata();
  66. $this->assertEquals( get_the_ID(), $post_id );
  67. }
  68. }
  69. }