helpers.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. /**
  3. * @group phpunit
  4. */
  5. class Tests_TestHelpers extends WP_UnitTestCase {
  6. /**
  7. * @ticket 30522
  8. */
  9. function data_assertSameSets() {
  10. return array(
  11. array(
  12. array( 1, 2, 3 ), // Test expected.
  13. array( 1, 2, 3 ), // Test actual.
  14. false, // Exception expected.
  15. ),
  16. array(
  17. array( 1, 2, 3 ),
  18. array( 2, 3, 1 ),
  19. false,
  20. ),
  21. array(
  22. array( 1, 2, 3 ),
  23. array( 1, 2, 3, 4 ),
  24. true,
  25. ),
  26. array(
  27. array( 1, 2, 3, 4 ),
  28. array( 1, 2, 3 ),
  29. true,
  30. ),
  31. array(
  32. array( 1, 2, 3 ),
  33. array( 3, 4, 2, 1 ),
  34. true,
  35. ),
  36. array(
  37. array( 1, 2, 3 ),
  38. array( 1, 2, 3, 3 ),
  39. true,
  40. ),
  41. array(
  42. array( 1, 2, 3 ),
  43. array( 2, 3, 1, 3 ),
  44. true,
  45. ),
  46. );
  47. }
  48. /**
  49. * @dataProvider data_assertSameSets
  50. * @ticket 30522
  51. */
  52. function test_assertSameSets( $expected, $actual, $exception ) {
  53. if ( $exception ) {
  54. try {
  55. $this->assertSameSets( $expected, $actual );
  56. } catch ( PHPUnit_Framework_ExpectationFailedException $ex ) {
  57. return;
  58. }
  59. $this->fail();
  60. } else {
  61. $this->assertSameSets( $expected, $actual );
  62. }
  63. }
  64. /**
  65. * @ticket 30522
  66. */
  67. function data_assertSameSetsWithIndex() {
  68. return array(
  69. array(
  70. array( 1, 2, 3 ), // Test expected.
  71. array( 1, 2, 3 ), // Test actual.
  72. false, // Exception expected.
  73. ),
  74. array(
  75. array(
  76. 'a' => 1,
  77. 'b' => 2,
  78. 'c' => 3,
  79. ),
  80. array(
  81. 'a' => 1,
  82. 'b' => 2,
  83. 'c' => 3,
  84. ),
  85. false,
  86. ),
  87. array(
  88. array( 1, 2, 3 ),
  89. array( 2, 3, 1 ),
  90. true,
  91. ),
  92. array(
  93. array(
  94. 'a' => 1,
  95. 'b' => 2,
  96. 'c' => 3,
  97. ),
  98. array(
  99. 'b' => 2,
  100. 'c' => 3,
  101. 'a' => 1,
  102. ),
  103. false,
  104. ),
  105. array(
  106. array( 1, 2, 3 ),
  107. array( 1, 2, 3, 4 ),
  108. true,
  109. ),
  110. array(
  111. array( 1, 2, 3, 4 ),
  112. array( 1, 2, 3 ),
  113. true,
  114. ),
  115. array(
  116. array(
  117. 'a' => 1,
  118. 'b' => 2,
  119. 'c' => 3,
  120. ),
  121. array(
  122. 'a' => 1,
  123. 'b' => 2,
  124. 'c' => 3,
  125. 'd' => 4,
  126. ),
  127. true,
  128. ),
  129. array(
  130. array(
  131. 'a' => 1,
  132. 'b' => 2,
  133. 'c' => 3,
  134. 'd' => 4,
  135. ),
  136. array(
  137. 'a' => 1,
  138. 'b' => 2,
  139. 'c' => 3,
  140. ),
  141. true,
  142. ),
  143. array(
  144. array( 1, 2, 3 ),
  145. array( 3, 4, 2, 1 ),
  146. true,
  147. ),
  148. array(
  149. array(
  150. 'a' => 1,
  151. 'b' => 2,
  152. 'c' => 3,
  153. ),
  154. array(
  155. 'c' => 3,
  156. 'b' => 2,
  157. 'd' => 4,
  158. 'a' => 1,
  159. ),
  160. true,
  161. ),
  162. array(
  163. array( 1, 2, 3 ),
  164. array( 1, 2, 3, 3 ),
  165. true,
  166. ),
  167. array(
  168. array(
  169. 'a' => 1,
  170. 'b' => 2,
  171. 'c' => 3,
  172. ),
  173. array(
  174. 'a' => 1,
  175. 'b' => 2,
  176. 'c' => 3,
  177. 'd' => 3,
  178. ),
  179. true,
  180. ),
  181. array(
  182. array( 1, 2, 3 ),
  183. array( 2, 3, 1, 3 ),
  184. true,
  185. ),
  186. array(
  187. array(
  188. 'a' => 1,
  189. 'b' => 2,
  190. 'c' => 3,
  191. ),
  192. array(
  193. 'c' => 3,
  194. 'b' => 2,
  195. 'd' => 3,
  196. 'a' => 1,
  197. ),
  198. true,
  199. ),
  200. );
  201. }
  202. /**
  203. * @dataProvider data_assertSameSetsWithIndex
  204. * @ticket 30522
  205. */
  206. function test_assertSameSetsWithIndex( $expected, $actual, $exception ) {
  207. if ( $exception ) {
  208. try {
  209. $this->assertSameSetsWithIndex( $expected, $actual );
  210. } catch ( PHPUnit_Framework_ExpectationFailedException $ex ) {
  211. return;
  212. }
  213. $this->fail();
  214. } else {
  215. $this->assertSameSetsWithIndex( $expected, $actual );
  216. }
  217. }
  218. public function test__unregister_post_status() {
  219. register_post_status( 'foo' );
  220. _unregister_post_status( 'foo' );
  221. $stati = get_post_stati();
  222. $this->assertFalse( isset( $stati['foo'] ) );
  223. }
  224. /**
  225. * @ticket 28486
  226. */
  227. public function test_setExpectedDeprecated() {
  228. $this->setExpectedDeprecated( 'Tests_TestHelpers::mock_deprecated' );
  229. $this->assertTrue( $this->mock_deprecated() );
  230. }
  231. /**
  232. * @ticket 28486
  233. */
  234. public function test_setExpectedIncorrectUsage() {
  235. $this->setExpectedIncorrectUsage( 'Tests_TestHelpers::mock_incorrect_usage' );
  236. $this->assertTrue( $this->mock_incorrect_usage() );
  237. }
  238. /**
  239. * @ticket 31417
  240. */
  241. public function test_go_to_should_go_to_home_page_when_passing_the_untrailingslashed_home_url() {
  242. $this->assertFalse( is_home() );
  243. $home = untrailingslashit( get_option( 'home' ) );
  244. $this->go_to( $home );
  245. $this->assertTrue( is_home() );
  246. }
  247. protected function mock_deprecated() {
  248. _deprecated_function( __METHOD__, '2.5' );
  249. return true;
  250. }
  251. protected function mock_incorrect_usage() {
  252. _doing_it_wrong( __METHOD__, __( 'Incorrect usage test' ), '2.5' );
  253. return true;
  254. }
  255. /**
  256. * @ticket 36166
  257. */
  258. public function test_die_handler_should_handle_wp_error() {
  259. $this->expectException( 'WPDieException' );
  260. wp_die( new WP_Error( 'test', 'test' ) );
  261. }
  262. /**
  263. * @ticket 46813
  264. */
  265. public function test_die_handler_should_not_cause_doing_it_wrong_notice_without_wp_query_set() {
  266. $this->expectException( 'WPDieException' );
  267. unset( $GLOBALS['wp_query'] );
  268. wp_die();
  269. $this->assertEmpty( $this->caught_doing_it_wrong );
  270. }
  271. /**
  272. * @ticket 45933
  273. * @dataProvider data_die_process_input
  274. */
  275. public function test_die_process_input( $input, $expected ) {
  276. $defaults = array(
  277. 'message' => '',
  278. 'title' => '',
  279. 'args' => array(),
  280. );
  281. $input = wp_parse_args(
  282. $input,
  283. $defaults
  284. );
  285. $expected = wp_parse_args(
  286. $expected,
  287. $defaults
  288. );
  289. list( $message, $title, $args ) = _wp_die_process_input( $input['message'], $input['title'], $input['args'] );
  290. $this->assertSame( $expected['message'], $message );
  291. $this->assertSame( $expected['title'], $title );
  292. // Only check arguments that are explicitly asked for.
  293. $this->assertSameSets( $expected['args'], array_intersect_key( $args, $expected['args'] ) );
  294. }
  295. public function data_die_process_input() {
  296. return array(
  297. array(
  298. array(
  299. 'message' => 'Broken.',
  300. ),
  301. array(
  302. 'message' => 'Broken.',
  303. 'title' => 'WordPress &rsaquo; Error',
  304. 'args' => array(
  305. 'response' => 500,
  306. 'code' => 'wp_die',
  307. 'text_direction' => 'ltr',
  308. ),
  309. ),
  310. ),
  311. array(
  312. array(
  313. 'message' => 'Broken.',
  314. 'title' => 'Fatal Error',
  315. 'args' => array(
  316. 'response' => null,
  317. ),
  318. ),
  319. array(
  320. 'message' => 'Broken.',
  321. 'title' => 'Fatal Error',
  322. 'args' => array(
  323. 'response' => 500,
  324. ),
  325. ),
  326. ),
  327. array(
  328. array(
  329. 'message' => 'More breakage.',
  330. 'args' => array(
  331. 'response' => 400,
  332. 'code' => 'custom_code',
  333. 'text_direction' => 'rtl',
  334. ),
  335. ),
  336. array(
  337. 'message' => 'More breakage.',
  338. 'title' => 'WordPress &rsaquo; Error',
  339. 'args' => array(
  340. 'response' => 400,
  341. 'code' => 'custom_code',
  342. 'text_direction' => 'rtl',
  343. ),
  344. ),
  345. ),
  346. array(
  347. array(
  348. 'message' => new WP_Error(
  349. 'no_access',
  350. 'You do not have access.',
  351. array(
  352. 'status' => 403,
  353. 'title' => 'Permission Error',
  354. )
  355. ),
  356. ),
  357. array(
  358. 'message' => 'You do not have access.',
  359. 'title' => 'Permission Error',
  360. 'args' => array(
  361. 'response' => 403,
  362. 'code' => 'no_access',
  363. ),
  364. ),
  365. ),
  366. );
  367. }
  368. /**
  369. * This test is just a setup for the one that follows.
  370. *
  371. * @ticket 38196
  372. */
  373. public function test_setup_postdata_globals_should_be_reset_on_teardown__setup() {
  374. $post = self::factory()->post->create_and_get();
  375. $GLOBALS['wp_query'] = new WP_Query();
  376. $GLOBALS['wp_query']->setup_postdata( $post );
  377. $this->assertNotEmpty( $post );
  378. }
  379. /**
  380. * @ticket 38196
  381. */
  382. public function test_setup_postdata_globals_should_be_reset_on_teardown() {
  383. $globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
  384. foreach ( $globals as $global ) {
  385. $this->assertTrue( ! isset( $GLOBALS[ $global ] ), $global );
  386. }
  387. }
  388. }