PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/phpunit/tests/query/setupPostdata.php

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