template.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. /**
  3. * @group template
  4. */
  5. class Tests_Post_Template extends WP_UnitTestCase {
  6. function test_wp_link_pages() {
  7. $contents = array( 'One', 'Two', 'Three' );
  8. $content = implode( '<!--nextpage-->', $contents );
  9. $post_id = self::factory()->post->create( array( 'post_content' => $content ) );
  10. $this->go_to( '?p=' . $post_id );
  11. setup_postdata( get_post( $post_id ) );
  12. $permalink = sprintf( '<a href="%s" class="post-page-numbers">', get_permalink() );
  13. $page2 = _wp_link_page( 2 );
  14. $page3 = _wp_link_page( 3 );
  15. $expected = '<p class="post-nav-links">Pages: <span class="post-page-numbers current" aria-current="page">1</span> ' . $page2 . '2</a> ' . $page3 . '3</a></p>';
  16. $output = wp_link_pages( array( 'echo' => 0 ) );
  17. $this->assertSame( $expected, $output );
  18. $before_after = " <span class=\"post-page-numbers current\" aria-current=\"page\">1</span> {$page2}2</a> {$page3}3</a>";
  19. $output = wp_link_pages(
  20. array(
  21. 'echo' => 0,
  22. 'before' => '',
  23. 'after' => '',
  24. )
  25. );
  26. $this->assertSame( $before_after, $output );
  27. $separator = " <span class=\"post-page-numbers current\" aria-current=\"page\">1</span>{$page2}2</a>{$page3}3</a>";
  28. $output = wp_link_pages(
  29. array(
  30. 'echo' => 0,
  31. 'before' => '',
  32. 'after' => '',
  33. 'separator' => '',
  34. )
  35. );
  36. $this->assertSame( $separator, $output );
  37. $link = " <span class=\"post-page-numbers current\" aria-current=\"page\"><em>1</em></span>{$page2}<em>2</em></a>{$page3}<em>3</em></a>";
  38. $output = wp_link_pages(
  39. array(
  40. 'echo' => 0,
  41. 'before' => '',
  42. 'after' => '',
  43. 'separator' => '',
  44. 'link_before' => '<em>',
  45. 'link_after' => '</em>',
  46. )
  47. );
  48. $this->assertSame( $link, $output );
  49. $next = "{$page2}<em>Next page</em></a>";
  50. $output = wp_link_pages(
  51. array(
  52. 'echo' => 0,
  53. 'before' => '',
  54. 'after' => '',
  55. 'separator' => '',
  56. 'link_before' => '<em>',
  57. 'link_after' => '</em>',
  58. 'next_or_number' => 'next',
  59. )
  60. );
  61. $this->assertSame( $next, $output );
  62. $GLOBALS['page'] = 2;
  63. $next_prev = "{$permalink}<em>Previous page</em></a>{$page3}<em>Next page</em></a>";
  64. $output = wp_link_pages(
  65. array(
  66. 'echo' => 0,
  67. 'before' => '',
  68. 'after' => '',
  69. 'separator' => '',
  70. 'link_before' => '<em>',
  71. 'link_after' => '</em>',
  72. 'next_or_number' => 'next',
  73. )
  74. );
  75. $this->assertSame( $next_prev, $output );
  76. $next_prev_link = "{$permalink}Woo page</a>{$page3}Hoo page</a>";
  77. $output = wp_link_pages(
  78. array(
  79. 'echo' => 0,
  80. 'before' => '',
  81. 'after' => '',
  82. 'separator' => '',
  83. 'next_or_number' => 'next',
  84. 'nextpagelink' => 'Hoo page',
  85. 'previouspagelink' => 'Woo page',
  86. )
  87. );
  88. $this->assertSame( $next_prev_link, $output );
  89. $GLOBALS['page'] = 1;
  90. $separator = "<p class=\"post-nav-links\">Pages: <span class=\"post-page-numbers current\" aria-current=\"page\">1</span> | {$page2}2</a> | {$page3}3</a></p>";
  91. $output = wp_link_pages(
  92. array(
  93. 'echo' => 0,
  94. 'separator' => ' | ',
  95. )
  96. );
  97. $this->assertSame( $separator, $output );
  98. $pagelink = " <span class=\"post-page-numbers current\" aria-current=\"page\">Page 1</span> | {$page2}Page 2</a> | {$page3}Page 3</a>";
  99. $output = wp_link_pages(
  100. array(
  101. 'echo' => 0,
  102. 'separator' => ' | ',
  103. 'before' => '',
  104. 'after' => '',
  105. 'pagelink' => 'Page %',
  106. )
  107. );
  108. $this->assertSame( $pagelink, $output );
  109. }
  110. function test_wp_dropdown_pages() {
  111. $none = wp_dropdown_pages( array( 'echo' => 0 ) );
  112. $this->assertEmpty( $none );
  113. $bump = '&nbsp;&nbsp;&nbsp;';
  114. $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
  115. $child_id = self::factory()->post->create(
  116. array(
  117. 'post_type' => 'page',
  118. 'post_parent' => $page_id,
  119. )
  120. );
  121. $grandchild_id = self::factory()->post->create(
  122. array(
  123. 'post_type' => 'page',
  124. 'post_parent' => $child_id,
  125. )
  126. );
  127. $title1 = get_post( $page_id )->post_title;
  128. $title2 = get_post( $child_id )->post_title;
  129. $title3 = get_post( $grandchild_id )->post_title;
  130. $lineage = <<<LINEAGE
  131. <select name='page_id' id='page_id'>
  132. <option class="level-0" value="$page_id">$title1</option>
  133. <option class="level-1" value="$child_id">{$bump}$title2</option>
  134. <option class="level-2" value="$grandchild_id">{$bump}{$bump}$title3</option>
  135. </select>
  136. LINEAGE;
  137. $output = wp_dropdown_pages( array( 'echo' => 0 ) );
  138. $this->assertSameIgnoreEOL( $lineage, $output );
  139. $depth = <<<DEPTH
  140. <select name='page_id' id='page_id'>
  141. <option class="level-0" value="$page_id">$title1</option>
  142. </select>
  143. DEPTH;
  144. $output = wp_dropdown_pages(
  145. array(
  146. 'echo' => 0,
  147. 'depth' => 1,
  148. )
  149. );
  150. $this->assertSameIgnoreEOL( $depth, $output );
  151. $option_none = <<<NONE
  152. <select name='page_id' id='page_id'>
  153. <option value="Woo">Hoo</option>
  154. <option class="level-0" value="$page_id">$title1</option>
  155. </select>
  156. NONE;
  157. $output = wp_dropdown_pages(
  158. array(
  159. 'echo' => 0,
  160. 'depth' => 1,
  161. 'show_option_none' => 'Hoo',
  162. 'option_none_value' => 'Woo',
  163. )
  164. );
  165. $this->assertSameIgnoreEOL( $option_none, $output );
  166. $option_no_change = <<<NO
  167. <select name='page_id' id='page_id'>
  168. <option value="-1">Burrito</option>
  169. <option value="Woo">Hoo</option>
  170. <option class="level-0" value="$page_id">$title1</option>
  171. </select>
  172. NO;
  173. $output = wp_dropdown_pages(
  174. array(
  175. 'echo' => 0,
  176. 'depth' => 1,
  177. 'show_option_none' => 'Hoo',
  178. 'option_none_value' => 'Woo',
  179. 'show_option_no_change' => 'Burrito',
  180. )
  181. );
  182. $this->assertSameIgnoreEOL( $option_no_change, $output );
  183. }
  184. /**
  185. * @ticket 12494
  186. */
  187. public function test_wp_dropdown_pages_value_field_should_default_to_ID() {
  188. $p = self::factory()->post->create(
  189. array(
  190. 'post_type' => 'page',
  191. )
  192. );
  193. $found = wp_dropdown_pages(
  194. array(
  195. 'echo' => 0,
  196. )
  197. );
  198. // Should contain page ID by default.
  199. $this->assertContains( 'value="' . $p . '"', $found );
  200. }
  201. /**
  202. * @ticket 12494
  203. */
  204. public function test_wp_dropdown_pages_value_field_ID() {
  205. $p = self::factory()->post->create(
  206. array(
  207. 'post_type' => 'page',
  208. )
  209. );
  210. $found = wp_dropdown_pages(
  211. array(
  212. 'echo' => 0,
  213. 'value_field' => 'ID',
  214. )
  215. );
  216. $this->assertContains( 'value="' . $p . '"', $found );
  217. }
  218. /**
  219. * @ticket 12494
  220. */
  221. public function test_wp_dropdown_pages_value_field_post_name() {
  222. $p = self::factory()->post->create(
  223. array(
  224. 'post_type' => 'page',
  225. 'post_name' => 'foo',
  226. )
  227. );
  228. $found = wp_dropdown_pages(
  229. array(
  230. 'echo' => 0,
  231. 'value_field' => 'post_name',
  232. )
  233. );
  234. $this->assertContains( 'value="foo"', $found );
  235. }
  236. /**
  237. * @ticket 12494
  238. */
  239. public function test_wp_dropdown_pages_value_field_should_fall_back_on_ID_when_an_invalid_value_is_provided() {
  240. $p = self::factory()->post->create(
  241. array(
  242. 'post_type' => 'page',
  243. 'post_name' => 'foo',
  244. )
  245. );
  246. $found = wp_dropdown_pages(
  247. array(
  248. 'echo' => 0,
  249. 'value_field' => 'foo',
  250. )
  251. );
  252. $this->assertContains( 'value="' . $p . '"', $found );
  253. }
  254. /**
  255. * @ticket 30082
  256. */
  257. public function test_wp_dropdown_pages_should_not_contain_class_attribute_when_no_class_is_passed() {
  258. $p = self::factory()->post->create(
  259. array(
  260. 'post_type' => 'page',
  261. 'post_name' => 'foo',
  262. )
  263. );
  264. $found = wp_dropdown_pages(
  265. array(
  266. 'echo' => 0,
  267. )
  268. );
  269. $this->assertNotRegExp( '/<select[^>]+class=\'/', $found );
  270. }
  271. /**
  272. * @ticket 30082
  273. */
  274. public function test_wp_dropdown_pages_should_obey_class_parameter() {
  275. $p = self::factory()->post->create(
  276. array(
  277. 'post_type' => 'page',
  278. 'post_name' => 'foo',
  279. )
  280. );
  281. $found = wp_dropdown_pages(
  282. array(
  283. 'echo' => 0,
  284. 'class' => 'bar',
  285. )
  286. );
  287. $this->assertRegExp( '/<select[^>]+class=\'bar\'/', $found );
  288. }
  289. /**
  290. * @ticket 31389
  291. */
  292. public function test_get_page_template_slug_by_id() {
  293. $page_id = self::factory()->post->create(
  294. array(
  295. 'post_type' => 'page',
  296. )
  297. );
  298. $this->assertSame( '', get_page_template_slug( $page_id ) );
  299. update_post_meta( $page_id, '_wp_page_template', 'default' );
  300. $this->assertSame( '', get_page_template_slug( $page_id ) );
  301. update_post_meta( $page_id, '_wp_page_template', 'example.php' );
  302. $this->assertSame( 'example.php', get_page_template_slug( $page_id ) );
  303. }
  304. /**
  305. * @ticket 31389
  306. */
  307. public function test_get_page_template_slug_from_loop() {
  308. $page_id = self::factory()->post->create(
  309. array(
  310. 'post_type' => 'page',
  311. )
  312. );
  313. update_post_meta( $page_id, '_wp_page_template', 'example.php' );
  314. $this->go_to( get_permalink( $page_id ) );
  315. $this->assertSame( 'example.php', get_page_template_slug() );
  316. }
  317. /**
  318. * @ticket 31389
  319. * @ticket 18375
  320. */
  321. public function test_get_page_template_slug_non_page() {
  322. $post_id = self::factory()->post->create();
  323. $this->assertSame( '', get_page_template_slug( $post_id ) );
  324. update_post_meta( $post_id, '_wp_page_template', 'default' );
  325. $this->assertSame( '', get_page_template_slug( $post_id ) );
  326. update_post_meta( $post_id, '_wp_page_template', 'example.php' );
  327. $this->assertSame( 'example.php', get_page_template_slug( $post_id ) );
  328. }
  329. /**
  330. * @ticket 18375
  331. */
  332. public function test_get_page_template_slug_non_page_from_loop() {
  333. $post_id = self::factory()->post->create();
  334. update_post_meta( $post_id, '_wp_page_template', 'example.php' );
  335. $this->go_to( get_permalink( $post_id ) );
  336. $this->assertSame( 'example.php', get_page_template_slug() );
  337. }
  338. /**
  339. * @ticket 11095
  340. * @ticket 33974
  341. */
  342. public function test_wp_page_menu_wp_nav_menu_fallback() {
  343. $pages = self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) );
  344. // No menus + wp_nav_menu() falls back to wp_page_menu().
  345. $menu = wp_nav_menu( array( 'echo' => false ) );
  346. // After falling back, the 'before' argument should be set and output as '<ul>'.
  347. $this->assertRegExp( '/<div class="menu"><ul>/', $menu );
  348. // After falling back, the 'after' argument should be set and output as '</ul>'.
  349. $this->assertRegExp( '/<\/ul><\/div>/', $menu );
  350. // After falling back, the markup should include whitespace around <li>'s.
  351. $this->assertRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
  352. $this->assertNotRegExp( '/><li.*>|<\/li></U', $menu );
  353. // No menus + wp_nav_menu() falls back to wp_page_menu(), this time without a container.
  354. $menu = wp_nav_menu(
  355. array(
  356. 'echo' => false,
  357. 'container' => false,
  358. )
  359. );
  360. // After falling back, the empty 'container' argument should still return a container element.
  361. $this->assertRegExp( '/<div class="menu">/', $menu );
  362. // No menus + wp_nav_menu() falls back to wp_page_menu(), this time without white-space.
  363. $menu = wp_nav_menu(
  364. array(
  365. 'echo' => false,
  366. 'item_spacing' => 'discard',
  367. )
  368. );
  369. // After falling back, the markup should not include whitespace around <li>'s.
  370. $this->assertNotRegExp( '/\s<li.*>|<\/li>\s/U', $menu );
  371. $this->assertRegExp( '/><li.*>|<\/li></U', $menu );
  372. }
  373. /**
  374. * @ticket 33045
  375. */
  376. public function test_get_parent_post() {
  377. $post = array(
  378. 'post_status' => 'publish',
  379. 'post_type' => 'page',
  380. );
  381. // Insert two initial posts.
  382. $parent_id = self::factory()->post->create( $post );
  383. $child_id = self::factory()->post->create( $post );
  384. // Test if child get_parent_post() post returns Null by default.
  385. $parent = get_post_parent( $child_id );
  386. $this->assertNull( $parent );
  387. // Update child post with a parent.
  388. wp_update_post(
  389. array(
  390. 'ID' => $child_id,
  391. 'post_parent' => $parent_id,
  392. )
  393. );
  394. // Test if child get_parent_post() post returns the parent object.
  395. $parent = get_post_parent( $child_id );
  396. $this->assertNotNull( $parent );
  397. $this->assertSame( $parent_id, $parent->ID );
  398. }
  399. /**
  400. * @ticket 33045
  401. */
  402. public function test_has_parent_post() {
  403. $post = array(
  404. 'post_status' => 'publish',
  405. 'post_type' => 'page',
  406. );
  407. // Insert two initial posts.
  408. $parent_id = self::factory()->post->create( $post );
  409. $child_id = self::factory()->post->create( $post );
  410. // Test if child has_parent_post() post returns False by default.
  411. $parent = has_post_parent( $child_id );
  412. $this->assertFalse( $parent );
  413. // Update child post with a parent.
  414. wp_update_post(
  415. array(
  416. 'ID' => $child_id,
  417. 'post_parent' => $parent_id,
  418. )
  419. );
  420. // Test if child has_parent_post() returns True.
  421. $parent = has_post_parent( $child_id );
  422. $this->assertTrue( $parent );
  423. }
  424. }