post.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. <?php
  2. /**
  3. * test wp-includes/post.php
  4. *
  5. * @group post
  6. */
  7. class Tests_Post extends WP_UnitTestCase {
  8. function setUp() {
  9. parent::setUp();
  10. $this->author_id = $this->factory->user->create( array( 'role' => 'editor' ) );
  11. $this->old_current_user = get_current_user_id();
  12. wp_set_current_user( $this->author_id );
  13. _set_cron_array(array());
  14. $this->post_ids = array();
  15. }
  16. function tearDown() {
  17. wp_set_current_user( $this->old_current_user );
  18. parent::tearDown();
  19. }
  20. // helper function: return the timestamp(s) of cron jobs for the specified hook and post
  21. function _next_schedule_for_post($hook, $id) {
  22. return wp_next_scheduled('publish_future_post', array(0=>intval($id)));
  23. }
  24. // helper function, unsets current user globally
  25. function _unset_current_user() {
  26. global $current_user, $user_ID;
  27. $current_user = $user_ID = null;
  28. }
  29. // test simple valid behavior: insert and get a post
  30. function test_vb_insert_get_delete() {
  31. register_post_type( 'cpt', array( 'taxonomies' => array( 'post_tag', 'ctax' ) ) );
  32. register_taxonomy( 'ctax', 'cpt' );
  33. $post_types = array( 'post', 'cpt' );
  34. foreach ( $post_types as $post_type ) {
  35. $post = array(
  36. 'post_author' => $this->author_id,
  37. 'post_status' => 'publish',
  38. 'post_content' => rand_str(),
  39. 'post_title' => rand_str(),
  40. 'tax_input' => array( 'post_tag' => 'tag1,tag2', 'ctax' => 'cterm1,cterm2' ),
  41. 'post_type' => $post_type
  42. );
  43. // insert a post and make sure the ID is ok
  44. $id = wp_insert_post($post);
  45. $this->assertTrue(is_numeric($id));
  46. $this->assertTrue($id > 0);
  47. // fetch the post and make sure it matches
  48. $out = get_post($id);
  49. $this->assertEquals($post['post_content'], $out->post_content);
  50. $this->assertEquals($post['post_title'], $out->post_title);
  51. $this->assertEquals($post['post_status'], $out->post_status);
  52. $this->assertEquals($post['post_author'], $out->post_author);
  53. // test cache state
  54. $pcache = wp_cache_get( $id, 'posts' );
  55. $this->assertInstanceOf( 'stdClass', $pcache );
  56. $this->assertEquals( $id, $pcache->ID );
  57. update_object_term_cache( $id, $post_type );
  58. $tcache = wp_cache_get( $id, "post_tag_relationships" );
  59. $this->assertInternalType( 'array', $tcache );
  60. $this->assertEquals( 2, count( $tcache ) );
  61. $tcache = wp_cache_get( $id, "ctax_relationships" );
  62. if ( 'cpt' == $post_type ) {
  63. $this->assertInternalType( 'array', $tcache );
  64. $this->assertEquals( 2, count( $tcache ) );
  65. } else {
  66. $this->assertFalse( $tcache );
  67. }
  68. wp_delete_post( $id, true );
  69. $this->assertFalse( wp_cache_get( $id, 'posts' ) );
  70. $this->assertFalse( wp_cache_get( $id, "post_tag_relationships" ) );
  71. $this->assertFalse( wp_cache_get( $id, "ctax_relationships" ) );
  72. }
  73. $GLOBALS['wp_taxonomies']['post_tag']->object_type = array( 'post' );
  74. }
  75. function test_vb_insert_future() {
  76. // insert a post with a future date, and make sure the status and cron schedule are correct
  77. $future_date = strtotime('+1 day');
  78. $post = array(
  79. 'post_author' => $this->author_id,
  80. 'post_status' => 'publish',
  81. 'post_content' => rand_str(),
  82. 'post_title' => rand_str(),
  83. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date),
  84. );
  85. // insert a post and make sure the ID is ok
  86. $id = $this->post_ids[] = wp_insert_post($post);
  87. #dmp(_get_cron_array());
  88. $this->assertTrue(is_numeric($id));
  89. $this->assertTrue($id > 0);
  90. // fetch the post and make sure it matches
  91. $out = get_post($id);
  92. $this->assertEquals($post['post_content'], $out->post_content);
  93. $this->assertEquals($post['post_title'], $out->post_title);
  94. $this->assertEquals('future', $out->post_status);
  95. $this->assertEquals($post['post_author'], $out->post_author);
  96. $this->assertEquals($post['post_date'], $out->post_date);
  97. // there should be a publish_future_post hook scheduled on the future date
  98. $this->assertEquals($future_date, $this->_next_schedule_for_post('publish_future_post', $id));
  99. }
  100. function test_vb_insert_future_over_dst() {
  101. // insert a post with a future date, and make sure the status and cron schedule are correct
  102. // Some magic days - one dst one not
  103. $future_date_1 = strtotime('June 21st +1 year');
  104. $future_date_2 = strtotime('Jan 11th +1 year');
  105. $post = array(
  106. 'post_author' => $this->author_id,
  107. 'post_status' => 'publish',
  108. 'post_content' => rand_str(),
  109. 'post_title' => rand_str(),
  110. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date_1),
  111. );
  112. // insert a post and make sure the ID is ok
  113. $id = $this->post_ids[] = wp_insert_post($post);
  114. // fetch the post and make sure has the correct date and status
  115. $out = get_post($id);
  116. $this->assertEquals('future', $out->post_status);
  117. $this->assertEquals($post['post_date'], $out->post_date);
  118. // check that there's a publish_future_post job scheduled at the right time
  119. $this->assertEquals($future_date_1, $this->_next_schedule_for_post('publish_future_post', $id));
  120. // now save it again with a date further in the future
  121. $post['ID'] = $id;
  122. $post['post_date'] = strftime("%Y-%m-%d %H:%M:%S", $future_date_2);
  123. $post['post_date_gmt'] = NULL;
  124. wp_update_post($post);
  125. // fetch the post again and make sure it has the new post_date
  126. $out = get_post($id);
  127. $this->assertEquals('future', $out->post_status);
  128. $this->assertEquals($post['post_date'], $out->post_date);
  129. // and the correct date on the cron job
  130. $this->assertEquals($future_date_2, $this->_next_schedule_for_post('publish_future_post', $id));
  131. }
  132. function test_vb_insert_future_edit_bug() {
  133. // future post bug: posts get published at the wrong time if you edit the timestamp
  134. // http://trac.wordpress.org/ticket/4710
  135. $future_date_1 = strtotime('+1 day');
  136. $future_date_2 = strtotime('+2 day');
  137. $post = array(
  138. 'post_author' => $this->author_id,
  139. 'post_status' => 'publish',
  140. 'post_content' => rand_str(),
  141. 'post_title' => rand_str(),
  142. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date_1),
  143. );
  144. // insert a post and make sure the ID is ok
  145. $id = $this->post_ids[] = wp_insert_post($post);
  146. // fetch the post and make sure has the correct date and status
  147. $out = get_post($id);
  148. $this->assertEquals('future', $out->post_status);
  149. $this->assertEquals($post['post_date'], $out->post_date);
  150. // check that there's a publish_future_post job scheduled at the right time
  151. $this->assertEquals($future_date_1, $this->_next_schedule_for_post('publish_future_post', $id));
  152. // now save it again with a date further in the future
  153. $post['ID'] = $id;
  154. $post['post_date'] = strftime("%Y-%m-%d %H:%M:%S", $future_date_2);
  155. $post['post_date_gmt'] = NULL;
  156. wp_update_post($post);
  157. // fetch the post again and make sure it has the new post_date
  158. $out = get_post($id);
  159. $this->assertEquals('future', $out->post_status);
  160. $this->assertEquals($post['post_date'], $out->post_date);
  161. // and the correct date on the cron job
  162. $this->assertEquals($future_date_2, $this->_next_schedule_for_post('publish_future_post', $id));
  163. }
  164. function test_vb_insert_future_draft() {
  165. // insert a draft post with a future date, and make sure no cron schedule is set
  166. $future_date = strtotime('+1 day');
  167. $post = array(
  168. 'post_author' => $this->author_id,
  169. 'post_status' => 'draft',
  170. 'post_content' => rand_str(),
  171. 'post_title' => rand_str(),
  172. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date),
  173. );
  174. // insert a post and make sure the ID is ok
  175. $id = $this->post_ids[] = wp_insert_post($post);
  176. #dmp(_get_cron_array());
  177. $this->assertTrue(is_numeric($id));
  178. $this->assertTrue($id > 0);
  179. // fetch the post and make sure it matches
  180. $out = get_post($id);
  181. $this->assertEquals($post['post_content'], $out->post_content);
  182. $this->assertEquals($post['post_title'], $out->post_title);
  183. $this->assertEquals('draft', $out->post_status);
  184. $this->assertEquals($post['post_author'], $out->post_author);
  185. $this->assertEquals($post['post_date'], $out->post_date);
  186. // there should be a publish_future_post hook scheduled on the future date
  187. $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
  188. }
  189. function test_vb_insert_future_change_to_draft() {
  190. // insert a future post, then edit and change it to draft, and make sure cron gets it right
  191. $future_date_1 = strtotime('+1 day');
  192. $post = array(
  193. 'post_author' => $this->author_id,
  194. 'post_status' => 'publish',
  195. 'post_content' => rand_str(),
  196. 'post_title' => rand_str(),
  197. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date_1),
  198. );
  199. // insert a post and make sure the ID is ok
  200. $id = $this->post_ids[] = wp_insert_post($post);
  201. // fetch the post and make sure has the correct date and status
  202. $out = get_post($id);
  203. $this->assertEquals('future', $out->post_status);
  204. $this->assertEquals($post['post_date'], $out->post_date);
  205. // check that there's a publish_future_post job scheduled at the right time
  206. $this->assertEquals($future_date_1, $this->_next_schedule_for_post('publish_future_post', $id));
  207. // now save it again with status set to draft
  208. $post['ID'] = $id;
  209. $post['post_status'] = 'draft';
  210. wp_update_post($post);
  211. // fetch the post again and make sure it has the new post_date
  212. $out = get_post($id);
  213. $this->assertEquals('draft', $out->post_status);
  214. $this->assertEquals($post['post_date'], $out->post_date);
  215. // and the correct date on the cron job
  216. $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
  217. }
  218. function test_vb_insert_future_change_status() {
  219. // insert a future post, then edit and change the status, and make sure cron gets it right
  220. $future_date_1 = strtotime('+1 day');
  221. $statuses = array('draft', 'static', 'object', 'attachment', 'inherit', 'pending');
  222. foreach ($statuses as $status) {
  223. $post = array(
  224. 'post_author' => $this->author_id,
  225. 'post_status' => 'publish',
  226. 'post_content' => rand_str(),
  227. 'post_title' => rand_str(),
  228. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date_1),
  229. );
  230. // insert a post and make sure the ID is ok
  231. $id = $this->post_ids[] = wp_insert_post($post);
  232. // fetch the post and make sure has the correct date and status
  233. $out = get_post($id);
  234. $this->assertEquals('future', $out->post_status);
  235. $this->assertEquals($post['post_date'], $out->post_date);
  236. // check that there's a publish_future_post job scheduled at the right time
  237. $this->assertEquals($future_date_1, $this->_next_schedule_for_post('publish_future_post', $id));
  238. // now save it again with status changed
  239. $post['ID'] = $id;
  240. $post['post_status'] = $status;
  241. wp_update_post($post);
  242. // fetch the post again and make sure it has the new post_date
  243. $out = get_post($id);
  244. $this->assertEquals($status, $out->post_status);
  245. $this->assertEquals($post['post_date'], $out->post_date);
  246. // and the correct date on the cron job
  247. $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
  248. }
  249. }
  250. function test_vb_insert_future_private() {
  251. // insert a draft post with a future date, and make sure no cron schedule is set
  252. $future_date = strtotime('+1 day');
  253. $post = array(
  254. 'post_author' => $this->author_id,
  255. 'post_status' => 'private',
  256. 'post_content' => rand_str(),
  257. 'post_title' => rand_str(),
  258. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date),
  259. );
  260. // insert a post and make sure the ID is ok
  261. $id = $this->post_ids[] = wp_insert_post($post);
  262. #dmp(_get_cron_array());
  263. $this->assertTrue(is_numeric($id));
  264. $this->assertTrue($id > 0);
  265. // fetch the post and make sure it matches
  266. $out = get_post($id);
  267. $this->assertEquals($post['post_content'], $out->post_content);
  268. $this->assertEquals($post['post_title'], $out->post_title);
  269. $this->assertEquals('private', $out->post_status);
  270. $this->assertEquals($post['post_author'], $out->post_author);
  271. $this->assertEquals($post['post_date'], $out->post_date);
  272. // there should be a publish_future_post hook scheduled on the future date
  273. $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
  274. }
  275. /**
  276. * @ticket 17180
  277. */
  278. function test_vb_insert_invalid_date() {
  279. // insert a post with an invalid date, make sure it fails
  280. $post = array(
  281. 'post_author' => $this->author_id,
  282. 'post_status' => 'public',
  283. 'post_content' => rand_str(),
  284. 'post_title' => rand_str(),
  285. 'post_date' => '2012-02-30 00:00:00',
  286. );
  287. // Test both return paths with or without WP_Error
  288. $insert_post = wp_insert_post( $post, true );
  289. $this->assertTrue( is_wp_error( $insert_post ), 'Did not get a WP_Error back from wp_insert_post' );
  290. $this->assertEquals( 'invalid_date', $insert_post->get_error_code() );
  291. $insert_post = wp_insert_post( $post );
  292. $this->assertEquals( 0, $insert_post );
  293. }
  294. function test_vb_insert_future_change_to_private() {
  295. // insert a future post, then edit and change it to private, and make sure cron gets it right
  296. $future_date_1 = strtotime('+1 day');
  297. $post = array(
  298. 'post_author' => $this->author_id,
  299. 'post_status' => 'publish',
  300. 'post_content' => rand_str(),
  301. 'post_title' => rand_str(),
  302. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date_1),
  303. );
  304. // insert a post and make sure the ID is ok
  305. $id = $this->post_ids[] = wp_insert_post($post);
  306. // fetch the post and make sure has the correct date and status
  307. $out = get_post($id);
  308. $this->assertEquals('future', $out->post_status);
  309. $this->assertEquals($post['post_date'], $out->post_date);
  310. // check that there's a publish_future_post job scheduled at the right time
  311. $this->assertEquals($future_date_1, $this->_next_schedule_for_post('publish_future_post', $id));
  312. // now save it again with status set to draft
  313. $post['ID'] = $id;
  314. $post['post_status'] = 'private';
  315. wp_update_post($post);
  316. // fetch the post again and make sure it has the new post_date
  317. $out = get_post($id);
  318. $this->assertEquals('private', $out->post_status);
  319. $this->assertEquals($post['post_date'], $out->post_date);
  320. // and the correct date on the cron job
  321. $this->assertEquals(false, $this->_next_schedule_for_post('publish_future_post', $id));
  322. }
  323. /**
  324. * @ticket 5364
  325. */
  326. function test_delete_future_post_cron() {
  327. // "When I delete a future post using wp_delete_post($post->ID) it does not update the cron correctly."
  328. $future_date = strtotime('+1 day');
  329. $post = array(
  330. 'post_author' => $this->author_id,
  331. 'post_status' => 'publish',
  332. 'post_content' => rand_str(),
  333. 'post_title' => rand_str(),
  334. 'post_date' => strftime("%Y-%m-%d %H:%M:%S", $future_date),
  335. );
  336. // insert a post and make sure the ID is ok
  337. $id = $this->post_ids[] = wp_insert_post($post);
  338. // check that there's a publish_future_post job scheduled at the right time
  339. $this->assertEquals($future_date, $this->_next_schedule_for_post('publish_future_post', $id));
  340. // now delete the post and make sure the cron entry is removed
  341. wp_delete_post($id);
  342. $this->assertFalse($this->_next_schedule_for_post('publish_future_post', $id));
  343. }
  344. /**
  345. * @ticket 5305
  346. */
  347. function test_permalink_without_title() {
  348. // bug: permalink doesn't work if post title is empty
  349. // might only fail if the post ID is greater than four characters
  350. global $wp_rewrite;
  351. $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
  352. $post = array(
  353. 'post_author' => $this->author_id,
  354. 'post_status' => 'publish',
  355. 'post_content' => rand_str(),
  356. 'post_title' => '',
  357. 'post_date' => '2007-10-31 06:15:00',
  358. );
  359. // insert a post and make sure the ID is ok
  360. $id = $this->post_ids[] = wp_insert_post($post);
  361. $plink = get_permalink($id);
  362. // permalink should include the post ID at the end
  363. $this->assertEquals(get_option('siteurl').'/2007/10/31/'.$id.'/', $plink);
  364. $wp_rewrite->set_permalink_structure('');
  365. }
  366. /**
  367. * @ticket 21013
  368. */
  369. function test_wp_unique_post_slug_with_non_latin_slugs() {
  370. $inputs = array(
  371. 'Αρνάκι άσπρο και παχύ της μάνας του καμάρι, και άλλα τραγούδια',
  372. 'Предлагаем супер металлообрабатывающее оборудование',
  373. );
  374. $outputs = array(
  375. 'αρνάκι-άσπρο-και-παχύ-της-μάνας-του-κα-2',
  376. 'предлагаем-супер-металлообрабатыва-2',
  377. );
  378. foreach ( $inputs as $k => $post_title ) {
  379. for ( $i = 0; $i < 2; $i++ ) {
  380. $post = array(
  381. 'post_author' => $this->author_id,
  382. 'post_status' => 'publish',
  383. 'post_content' => rand_str(),
  384. 'post_title' => $post_title,
  385. );
  386. $id = $this->post_ids[] = wp_insert_post( $post );
  387. }
  388. $post = get_post( $id );
  389. $this->assertEquals( $outputs[$k], urldecode( $post->post_name ) );
  390. }
  391. }
  392. /**
  393. * @ticket 15665
  394. */
  395. function test_get_page_by_path_priority() {
  396. $attachment = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'attachment' ) );
  397. $page = $this->factory->post->create_and_get( array( 'post_title' => 'some-page', 'post_type' => 'page' ) );
  398. $other_att = $this->factory->post->create_and_get( array( 'post_title' => 'some-other-page', 'post_type' => 'attachment' ) );
  399. $this->assertEquals( 'some-page', $attachment->post_name );
  400. $this->assertEquals( 'some-page', $page->post_name );
  401. // get_page_by_path() should return a post of the requested type before returning an attachment.
  402. $this->assertEquals( $page, get_page_by_path( 'some-page' ) );
  403. // Make sure get_page_by_path() will still select an attachment when a post of the requested type doesn't exist.
  404. $this->assertEquals( $other_att, get_page_by_path( 'some-other-page' ) );
  405. }
  406. function test_wp_publish_post() {
  407. $draft_id = $this->factory->post->create( array( 'post_status' => 'draft' ) );
  408. $post = get_post( $draft_id );
  409. $this->assertEquals( 'draft', $post->post_status );
  410. wp_publish_post( $draft_id );
  411. $post = get_post( $draft_id );
  412. $this->assertEquals( 'publish', $post->post_status );
  413. }
  414. /**
  415. * @ticket 22944
  416. */
  417. function test_wp_insert_post_and_wp_publish_post_with_future_date() {
  418. $future_date = gmdate( 'Y-m-d H:i:s', time() + 10000000 );
  419. $post_id = $this->factory->post->create( array(
  420. 'post_status' => 'publish',
  421. 'post_date' => $future_date,
  422. ) );
  423. $post = get_post( $post_id );
  424. $this->assertEquals( 'future', $post->post_status );
  425. $this->assertEquals( $future_date, $post->post_date );
  426. wp_publish_post( $post_id );
  427. $post = get_post( $post_id );
  428. $this->assertEquals( 'publish', $post->post_status );
  429. $this->assertEquals( $future_date, $post->post_date );
  430. }
  431. /**
  432. * @ticket 22944
  433. */
  434. function test_publish_post_with_content_filtering() {
  435. kses_remove_filters();
  436. $post_id = wp_insert_post( array( 'post_title' => '<script>Test</script>' ) );
  437. $post = get_post( $post_id );
  438. $this->assertEquals( '<script>Test</script>', $post->post_title );
  439. $this->assertEquals( 'draft', $post->post_status );
  440. kses_init_filters();
  441. wp_update_post( array( 'ID' => $post->ID, 'post_status' => 'publish' ) );
  442. $post = get_post( $post->ID );
  443. $this->assertEquals( 'Test', $post->post_title );
  444. kses_remove_filters();
  445. }
  446. /**
  447. * @ticket 22944
  448. */
  449. function test_wp_publish_post_and_avoid_content_filtering() {
  450. kses_remove_filters();
  451. $post_id = wp_insert_post( array( 'post_title' => '<script>Test</script>' ) );
  452. $post = get_post( $post_id );
  453. $this->assertEquals( '<script>Test</script>', $post->post_title );
  454. $this->assertEquals( 'draft', $post->post_status );
  455. kses_init_filters();
  456. wp_publish_post( $post->ID );
  457. $post = get_post( $post->ID );
  458. $this->assertEquals( '<script>Test</script>', $post->post_title );
  459. kses_remove_filters();
  460. }
  461. /**
  462. * @ticket 22883
  463. */
  464. function test_get_page_uri_with_stdclass_post_object() {
  465. $post_id = $this->factory->post->create( array( 'post_name' => 'get-page-uri-post-name' ) );
  466. // Mimick an old stdClass post object, missing the ancestors field.
  467. $post_array = (object) get_post( $post_id, ARRAY_A );
  468. unset( $post_array->ancestors );
  469. // Dummy assertion. If this test fails, it will actually error out on an E_WARNING.
  470. $this->assertEquals( 'get-page-uri-post-name', get_page_uri( $post_array ) );
  471. }
  472. /**
  473. * @ticket 24491
  474. */
  475. function test_get_page_uri_with_nonexistent_post() {
  476. global $wpdb;
  477. $post_id = $wpdb->get_var( "SELECT MAX(ID) FROM $wpdb->posts" ) + 1;
  478. $this->assertFalse( get_page_uri( $post_id ) );
  479. }
  480. /**
  481. * @ticket 23708
  482. */
  483. function test_get_post_ancestors_within_loop() {
  484. global $post;
  485. $parent_id = $this->factory->post->create();
  486. $post = $this->factory->post->create_and_get( array( 'post_parent' => $parent_id ) );
  487. $this->assertEquals( array( $parent_id ), get_post_ancestors( 0 ) );
  488. }
  489. /**
  490. * @ticket 23474
  491. */
  492. function test_update_invalid_post_id() {
  493. $post_id = $this->factory->post->create( array( 'post_name' => 'get-page-uri-post-name' ) );
  494. $post = get_post( $post_id, ARRAY_A );
  495. $post['ID'] = 123456789;
  496. $this->assertEquals( 0, wp_insert_post( $post ) );
  497. $this->assertEquals( 0, wp_update_post( $post ) );
  498. $this->assertInstanceOf( 'WP_Error', wp_insert_post( $post, true ) );
  499. $this->assertInstanceOf( 'WP_Error', wp_update_post( $post, true ) );
  500. }
  501. function test_parse_post_content_single_page() {
  502. global $multipage, $pages, $numpages;
  503. $post_id = $this->factory->post->create( array( 'post_content' => 'Page 0' ) );
  504. $post = get_post( $post_id );
  505. setup_postdata( $post );
  506. $this->assertEquals( 0, $multipage );
  507. $this->assertCount( 1, $pages );
  508. $this->assertEquals( 1, $numpages );
  509. $this->assertEquals( array( 'Page 0' ), $pages );
  510. }
  511. function test_parse_post_content_multi_page() {
  512. global $multipage, $pages, $numpages;
  513. $post_id = $this->factory->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
  514. $post = get_post( $post_id );
  515. setup_postdata( $post );
  516. $this->assertEquals( 1, $multipage );
  517. $this->assertCount( 4, $pages );
  518. $this->assertEquals( 4, $numpages );
  519. $this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $pages );
  520. }
  521. function test_parse_post_content_remaining_single_page() {
  522. global $multipage, $pages, $numpages;
  523. $post_id = $this->factory->post->create( array( 'post_content' => 'Page 0' ) );
  524. $post = get_post( $post_id );
  525. setup_postdata( $post );
  526. $this->assertEquals( 0, $multipage );
  527. $this->assertCount( 1, $pages );
  528. $this->assertEquals( 1, $numpages );
  529. $this->assertEquals( array( 'Page 0' ), $pages );
  530. }
  531. function test_parse_post_content_remaining_multi_page() {
  532. global $multipage, $pages, $numpages;
  533. $post_id = $this->factory->post->create( array( 'post_content' => 'Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
  534. $post = get_post( $post_id );
  535. setup_postdata( $post );
  536. $this->assertEquals( 1, $multipage );
  537. $this->assertCount( 4, $pages );
  538. $this->assertEquals( 4, $numpages );
  539. $this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $pages );
  540. }
  541. /**
  542. * @ticket 16746
  543. */
  544. function test_parse_post_content_starting_with_nextpage() {
  545. global $multipage, $pages, $numpages;
  546. $post_id = $this->factory->post->create( array( 'post_content' => '<!--nextpage-->Page 0<!--nextpage-->Page 1<!--nextpage-->Page 2<!--nextpage-->Page 3' ) );
  547. $post = get_post( $post_id );
  548. setup_postdata( $post );
  549. $this->assertEquals( 1, $multipage );
  550. $this->assertCount( 4, $pages );
  551. $this->assertEquals( 4, $numpages );
  552. $this->assertEquals( array( 'Page 0', 'Page 1', 'Page 2', 'Page 3' ), $pages );
  553. }
  554. /**
  555. * @ticket 16746
  556. */
  557. function test_parse_post_content_starting_with_nextpage_multi() {
  558. global $multipage, $pages, $numpages;
  559. $post_id = $this->factory->post->create( array( 'post_content' => '<!--nextpage-->Page 0' ) );
  560. $post = get_post( $post_id );
  561. setup_postdata( $post );
  562. $this->assertEquals( 0, $multipage );
  563. $this->assertCount( 1, $pages );
  564. $this->assertEquals( 1, $numpages );
  565. $this->assertEquals( array( 'Page 0' ), $pages );
  566. }
  567. /**
  568. * @ticket 19373
  569. */
  570. function test_insert_programmatic_sanitized() {
  571. $this->_unset_current_user();
  572. register_taxonomy( 'test_tax', 'post' );
  573. $title = rand_str();
  574. $post_data = array(
  575. 'post_author' => $this->author_id,
  576. 'post_status' => 'public',
  577. 'post_content' => rand_str(),
  578. 'post_title' => $title,
  579. 'tax_input' => array(
  580. 'test_tax' => array( 'term', 'term2', 'term3' )
  581. )
  582. );
  583. $insert_post_id = wp_insert_post( $post_data, true, true );
  584. $this->assertTrue( ( is_int($insert_post_id) && $insert_post_id > 0 ) );
  585. $post = get_post( $insert_post_id );
  586. $this->assertEquals( $post->post_author, $this->author_id );
  587. $this->assertEquals( $post->post_title, $title );
  588. }
  589. /**
  590. * @ticket 19373
  591. */
  592. function test_insert_programmatic_without_current_user_success() {
  593. $this->_unset_current_user();
  594. register_taxonomy( 'test_tax', 'post' );
  595. $title = rand_str();
  596. $post_data = array(
  597. 'post_author' => $this->author_id,
  598. 'post_status' => 'public',
  599. 'post_content' => rand_str(),
  600. 'post_title' => $title,
  601. 'tax_input' => array(
  602. 'test_tax' => array( 'term', 'term2', 'term3' )
  603. )
  604. );
  605. // with sanitize set to false
  606. $insert_post_id = wp_insert_post( $post_data, true, false );
  607. $post = get_post( $insert_post_id );
  608. $this->assertEquals( $post->post_author, $this->author_id );
  609. $this->assertEquals( $post->post_title, $title );
  610. $terms = wp_get_object_terms( $insert_post_id, 'test_tax' );
  611. $this->assertTrue( ( is_array( $terms ) && count( $terms ) == 3 ) );
  612. }
  613. /**
  614. * @ticket 19373
  615. */
  616. function test_insert_programmatic_without_current_user_fail() {
  617. $this->_unset_current_user();
  618. register_taxonomy( 'test_tax', 'post' );
  619. $title = rand_str();
  620. $post_data = array(
  621. // post_author not set
  622. 'post_status' => 'public',
  623. 'post_content' => rand_str(),
  624. 'post_title' => $title,
  625. 'tax_input' => array(
  626. 'test_tax' => array( 'term', 'term2', 'term3' )
  627. )
  628. );
  629. // with sanitize set to false
  630. $insert_post_id = wp_insert_post( $post_data, true, false );
  631. // should error because no default user exists and no post author is passed in
  632. $this->assertInstanceOf( 'WP_Error', $insert_post_id );
  633. $this->assertEquals( 'empty_author', $insert_post_id->get_error_code() );
  634. }
  635. /**
  636. * @ticket 24803
  637. */
  638. function test_wp_count_posts() {
  639. $post_type = rand_str(20);
  640. register_post_type( $post_type );
  641. $this->factory->post->create( array(
  642. 'post_type' => $post_type,
  643. 'post_author' => $this->author_id
  644. ) );
  645. $count = wp_count_posts( $post_type, 'readable' );
  646. $this->assertEquals( 1, $count->publish );
  647. _unregister_post_type( $post_type );
  648. $this->assertEquals( new stdClass, wp_count_posts( $post_type, 'readable' ) );
  649. }
  650. function test_wp_count_posts_filtered() {
  651. $post_type = rand_str(20);
  652. register_post_type( $post_type );
  653. $this->factory->post->create_many( 10, array(
  654. 'post_type' => $post_type,
  655. 'post_author' => $this->author_id
  656. ) );
  657. $count1 = wp_count_posts( $post_type, 'readable' );
  658. $this->assertEquals( 10, $count1->publish );
  659. add_filter( 'wp_count_posts', array( $this, 'filter_wp_count_posts' ) );
  660. $count2 = wp_count_posts( $post_type, 'readable' );
  661. $this->assertEquals( 7, $count2->publish );
  662. remove_filter( 'wp_count_posts', array( $this, 'filter_wp_count_posts' ) );
  663. }
  664. function filter_wp_count_posts( $counts ) {
  665. $counts->publish = 7;
  666. return $counts;
  667. }
  668. }