| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group query |
| 5 | * @covers setup_postdata |
| 6 | */ |
| 7 | class Tests_Query_SetupPostdata extends WP_UnitTestCase { |
| 8 | protected $global_keys = array( 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' ); |
| 9 | |
| 10 | protected $global_data = array(); |
| 11 | |
| 12 | public function setUp() { |
| 13 | parent::setUp(); |
| 14 | return; |
| 15 | |
| 16 | foreach ( $this->global_keys as $global_key ) { |
| 17 | if ( isset( $GLOBALS[ $global_key ] ) ) { |
| 18 | $this->global_data[ $global_key ] = $GLOBALS[ $global_key ]; |
| 19 | unset( $GLOBALS[ $global_key ] ); |
| 20 | } else { |
| 21 | $this->global_data[ $global_key ] = null; |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public function tearDown() { |
| 27 | parent::tearDown(); |
| 28 | return; |
| 29 | |
| 30 | foreach ( $this->global_keys as $global_key ) { |
| 31 | if ( ! is_null( $this->global_data[ $global_key ] ) ) { |
| 32 | $GLOBALS[ $global_key ] = $this->global_data[ $global_key ]; |
| 33 | } else { |
| 34 | unset( $GLOBALS[ $global_key ] ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | $this->global_data = array(); |
| 39 | } |
| 40 | |
| 41 | public function test_id() { |
| 42 | $p = $this->factory->post->create_and_get(); |
| 43 | setup_postdata( $p ); |
| 44 | |
| 45 | $this->assertNotEmpty( $p->ID ); |
| 46 | $this->assertSame( $p->ID, $GLOBALS['id'] ); |
| 47 | } |
| 48 | |
| 49 | public function test_authordata() { |
| 50 | $u = $this->factory->user->create_and_get(); |
| 51 | $p = $this->factory->post->create_and_get( array( |
| 52 | 'post_author' => $u->ID, |
| 53 | ) ); |
| 54 | setup_postdata( $p ); |
| 55 | |
| 56 | $this->assertNotEmpty( $GLOBALS['authordata'] ); |
| 57 | $this->assertEquals( $u, $GLOBALS['authordata'] ); |
| 58 | } |
| 59 | |
| 60 | public function test_currentday() { |
| 61 | $p = $this->factory->post->create_and_get( array( |
| 62 | 'post_date' => '1980-09-09 06:30:00', |
| 63 | ) ); |
| 64 | setup_postdata( $p ); |
| 65 | |
| 66 | $this->assertSame( '09.09.80', $GLOBALS['currentday'] ); |
| 67 | } |
| 68 | |
| 69 | public function test_currentmonth() { |
| 70 | $p = $this->factory->post->create_and_get( array( |
| 71 | 'post_date' => '1980-09-09 06:30:00', |
| 72 | ) ); |
| 73 | setup_postdata( $p ); |
| 74 | |
| 75 | $this->assertSame( '09', $GLOBALS['currentmonth'] ); |
| 76 | } |
| 77 | |
| 78 | public function test_secondary_query_post_vars() { |
| 79 | $users = $this->factory->user->create_many( 2 ); |
| 80 | |
| 81 | $post1 = $this->factory->post->create_and_get( array( |
| 82 | 'post_author' => $users[0], |
| 83 | 'post_date' => '2012-02-02 02:00:00', |
| 84 | ) ); |
| 85 | |
| 86 | $post2 = $this->factory->post->create_and_get( array( |
| 87 | 'post_author' => $users[1], |
| 88 | 'post_date' => '2013-03-03 03:00:00', |
| 89 | ) ); |
| 90 | |
| 91 | $this->go_to( get_permalink( $post1 ) ); |
| 92 | setup_postdata( $post1 ); |
| 93 | |
| 94 | // Main loop. |
| 95 | $this->assertSame( $post1->ID, $GLOBALS['id'] ); |
| 96 | $this->assertEquals( get_userdata( $users[0] ), $GLOBALS['authordata'] ); |
| 97 | $this->assertSame( '02.02.12', $GLOBALS['currentday'] ); |
| 98 | $this->assertSame( '02', $GLOBALS['currentmonth'] ); |
| 99 | |
| 100 | // Secondary loop. |
| 101 | $q = new WP_Query( array( |
| 102 | 'posts_per_page' => 1, |
| 103 | ) ); |
| 104 | if ( $q->have_posts() ) { |
| 105 | while ( $q->have_posts() ) { |
| 106 | $q->the_post(); |
| 107 | |
| 108 | // Should refer to the current loop. |
| 109 | $this->assertSame( $post2->ID, $GLOBALS['id'] ); |
| 110 | $this->assertEquals( get_userdata( $users[1] ), $GLOBALS['authordata'] ); |
| 111 | $this->assertSame( '03.03.13', $GLOBALS['currentday'] ); |
| 112 | $this->assertSame( '03', $GLOBALS['currentmonth'] ); |
| 113 | } |
| 114 | } |
| 115 | wp_reset_postdata(); |
| 116 | |
| 117 | // Should be reset to main loop. |
| 118 | $this->assertSame( $post1->ID, $GLOBALS['id'] ); |
| 119 | $this->assertEquals( get_userdata( $users[0] ), $GLOBALS['authordata'] ); |
| 120 | $this->assertSame( '02.02.12', $GLOBALS['currentday'] ); |
| 121 | $this->assertSame( '02', $GLOBALS['currentmonth'] ); |
| 122 | } |
| 123 | |
| 124 | public function test_single_page() { |
| 125 | $post = $this->factory->post->create_and_get( array( |
| 126 | 'post_content' => 'Page 0', |
| 127 | ) ); |
| 128 | setup_postdata( $post ); |
| 129 | |
| 130 | $this->assertSame( 0, $GLOBALS['multipage'] ); |
| 131 | $this->assertSame( 1, $GLOBALS['numpages'] ); |
| 132 | $this->assertEquals( array( 'Page 0' ), $GLOBALS['pages'] ); |
| 133 | } |
| 134 | |
| 135 | public function test_multi_page() { |
| 136 | $post = $this->factory->post->create_and_get( array( |
| 137 | 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3', |
| 138 | ) ); |
| 139 | setup_postdata( $post ); |
| 140 | |
| 141 | $this->assertSame( 1, $GLOBALS['multipage'] ); |
| 142 | $this->assertSame( 4, $GLOBALS['numpages'] ); |
| 143 | $this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @ticket 16746 |
| 148 | */ |
| 149 | public function test_nextpage_at_start_of_content() { |
| 150 | $post = $this->factory->post->create_and_get( array( |
| 151 | 'post_content' => '<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3', |
| 152 | ) ); |
| 153 | setup_postdata( $post ); |
| 154 | |
| 155 | $this->assertSame( 1, $GLOBALS['multipage'] ); |
| 156 | $this->assertSame( 3, $GLOBALS['numpages'] ); |
| 157 | $this->assertEquals( array( 'Page 1', 'Page 2', 'Page 3' ), $GLOBALS['pages'] ); |
| 158 | } |
| 159 | |
| 160 | public function test_trim_nextpage_linebreaks() { |
| 161 | $post = $this->factory->post->create_and_get( array( |
| 162 | 'post_content' => "Page 0\n<!--nextpage-->\nPage 1\nhas a line break\n<!--nextpage-->Page 2<!--nextpage-->\n\nPage 3", |
| 163 | ) ); |
| 164 | setup_postdata( $post ); |
| 165 | |
| 166 | $this->assertEquals( array( 'Page 0', "Page 1\nhas a line break", 'Page 2', "\nPage 3" ), $GLOBALS['pages'] ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @ticket 25349 |
| 171 | */ |
| 172 | public function test_secondary_query_nextpage() { |
| 173 | $post1 = $this->factory->post->create( array( |
| 174 | 'post_content' => 'Post 1 Page 1<!--nextpage-->Post 1 Page 2', |
| 175 | ) ); |
| 176 | $post2 = $this->factory->post->create( array( |
| 177 | 'post_content' => 'Post 2 Page 1<!--nextpage-->Post 2 Page 2', |
| 178 | ) ); |
| 179 | |
| 180 | $this->go_to( '/?p=' . $post1 ); |
| 181 | setup_postdata( get_post( $post1 ) ); |
| 182 | |
| 183 | // Main loop. |
| 184 | $this->assertSame( array( 'Post 1 Page 1', 'Post 1 Page 2' ), $GLOBALS['pages'] ); |
| 185 | |
| 186 | // Secondary loop. |
| 187 | $q = new WP_Query( array( |
| 188 | 'post__in' => array( $post2 ), |
| 189 | ) ); |
| 190 | if ( $q->have_posts() ) { |
| 191 | while ( $q->have_posts() ) { |
| 192 | $q->the_post(); |
| 193 | |
| 194 | // Should refer to the current loop. |
| 195 | $this->assertSame( array( 'Post 2 Page 1', 'Post 2 Page 2' ), $GLOBALS['pages'] ); |
| 196 | } |
| 197 | } |
| 198 | wp_reset_postdata(); |
| 199 | |
| 200 | // Should be reset to main loop. |
| 201 | $this->assertSame( array( 'Post 1 Page 1', 'Post 1 Page 2' ), $GLOBALS['pages'] ); |
| 202 | } |
| 203 | |
| 204 | public function test_page_from_wp_query() { |
| 205 | $page = $this->factory->post->create_and_get( array( |
| 206 | 'post_type' => 'page', |
| 207 | ) ); |
| 208 | |
| 209 | $this->go_to( '/?page=78' ); |
| 210 | |
| 211 | $GLOBALS['wp_query']->query_vars['page'] = 78; |
| 212 | setup_postdata( $page ); |
| 213 | |
| 214 | $this->assertSame( 78, $GLOBALS['page'] ); |
| 215 | } |
| 216 | |
| 217 | public function test_page_when_on_page() { |
| 218 | $page = $this->factory->post->create_and_get( array( |
| 219 | 'post_type' => 'page', |
| 220 | ) ); |
| 221 | $this->go_to( get_permalink( $page ) ); |
| 222 | setup_postdata( $page ); |
| 223 | |
| 224 | $this->assertSame( 1, $GLOBALS['page'] ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @ticket 20904 |
| 229 | */ |
| 230 | public function test_secondary_query_page() { |
| 231 | $post = $this->factory->post->create_and_get(); |
| 232 | $this->go_to( '/?page=3' ); |
| 233 | setup_postdata( $post ); |
| 234 | |
| 235 | // Main loop. |
| 236 | $this->assertSame( 3, $GLOBALS['page'] ); |
| 237 | |
| 238 | // Secondary loop. |
| 239 | $posts = $this->factory->post->create_many( 5 ); |
| 240 | $q = new WP_Query( array( |
| 241 | 'page' => 4, |
| 242 | 'posts_per_page' => 1, |
| 243 | ) ); |
| 244 | if ( $q->have_posts() ) { |
| 245 | while ( $q->have_posts() ) { |
| 246 | $q->the_post(); |
| 247 | |
| 248 | // $page should refer to the current loop. |
| 249 | $this->assertSame( 4, $GLOBALS['page'] ); |
| 250 | } |
| 251 | } |
| 252 | wp_reset_postdata(); |
| 253 | |
| 254 | // $page should be reset to main loop. |
| 255 | $this->assertSame( 3, $GLOBALS['page'] ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @ticket 20904 |
| 260 | */ |
| 261 | public function test_more_when_on_setup_post() { |
| 262 | $post = $this->factory->post->create_and_get(); |
| 263 | $this->go_to( get_permalink( $post ) ); |
| 264 | setup_postdata( $post ); |
| 265 | |
| 266 | $this->assertSame( 1, $GLOBALS['more'] ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * @ticket 20904 |
| 271 | * |
| 272 | * $more should not be true when the set-up post is not the same as the current post. |
| 273 | */ |
| 274 | public function test_more_when_on_single() { |
| 275 | $post1 = $this->factory->post->create_and_get(); |
| 276 | $post2 = $this->factory->post->create_and_get(); |
| 277 | $this->go_to( get_permalink( $post1 ) ); |
| 278 | setup_postdata( $post2 ); |
| 279 | |
| 280 | $this->assertTrue( empty( $GLOBALS['more'] ) ); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @ticket 20904 |
| 285 | * |
| 286 | * $more should not be true when the set-up post is not the same as the current page. |
| 287 | */ |
| 288 | public function test_more_when_on_page() { |
| 289 | $post = $this->factory->post->create_and_get(); |
| 290 | $page = $this->factory->post->create_and_get( array( |
| 291 | 'post_type' => 'page', |
| 292 | ) ); |
| 293 | $this->go_to( get_permalink( $page ) ); |
| 294 | setup_postdata( $post ); |
| 295 | |
| 296 | $this->assertTrue( empty( $GLOBALS['more'] ) ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @ticket 20904 |
| 301 | */ |
| 302 | public function test_more_when_on_feed() { |
| 303 | $post = $this->factory->post->create_and_get(); |
| 304 | $this->go_to( '/?feed=rss' ); |
| 305 | setup_postdata( $post ); |
| 306 | |
| 307 | $this->assertSame( 1, $GLOBALS['more'] ); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * @ticket 20904 |
| 312 | * @ticket 25349 |
| 313 | */ |
| 314 | public function test_secondary_query_more() { |
| 315 | $post = $this->factory->post->create_and_get(); |
| 316 | $this->go_to( get_permalink( $post ) ); |
| 317 | setup_postdata( $post ); |
| 318 | |
| 319 | // Main loop. |
| 320 | $this->assertSame( 1, $GLOBALS['more'] ); |
| 321 | |
| 322 | // Secondary loop. |
| 323 | $q = new WP_Query( array( |
| 324 | 'posts_per_page' => 1, |
| 325 | ) ); |
| 326 | if ( $q->have_posts() ) { |
| 327 | while ( $q->have_posts() ) { |
| 328 | $q->the_post(); |
| 329 | |
| 330 | // $more should refer to the current loop. |
| 331 | $this->assertTrue( empty( $GLOBALS['more'] ) ); |
| 332 | } |
| 333 | } |
| 334 | wp_reset_postdata(); |
| 335 | |
| 336 | // $page should be reset to main loop. |
| 337 | $this->assertSame( 1, $GLOBALS['more'] ); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @ticket 24330 |
| 342 | * |
| 343 | * setup_postdata( $a_post ) followed by the_content() in a loop that does not update |
| 344 | * global $post should use the content of $a_post rather then the global post. |
| 345 | */ |
| 346 | function test_setup_postdata_loop() { |
| 347 | $post_id = $this->factory->post->create( array( 'post_content' => 'global post' ) ); |
| 348 | $GLOBALS['wp_query']->post = $GLOBALS['post'] = get_post( $post_id ); |
| 349 | |
| 350 | $ids = $this->factory->post->create_many(5); |
| 351 | foreach ( $ids as $id ) { |
| 352 | $page = get_post( $id ); |
| 353 | if ( $page ) { |
| 354 | setup_postdata( $page ); |
| 355 | $content = get_echo( 'the_content', array() ); |
| 356 | $this->assertEquals( $post_id, $GLOBALS['post']->ID ); |
| 357 | $this->assertNotEquals( '<p>global post</p>', strip_ws( $content ) ); |
| 358 | wp_reset_postdata(); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | } |