wpError.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. <?php
  2. /**
  3. * Tests for error handling and the WP_Error class.
  4. *
  5. * @group general
  6. * @group errors
  7. *
  8. * @covers WP_Error
  9. * @coversDefaultClass WP_Error
  10. */
  11. class Tests_General_wpError extends WP_UnitTestCase {
  12. /**
  13. * WP_Error fixture.
  14. *
  15. * @var WP_Error
  16. */
  17. public $wp_error;
  18. /**
  19. * Set up.
  20. */
  21. public function setUp() {
  22. parent::setUp();
  23. $this->wp_error = new WP_Error();
  24. }
  25. /**
  26. * @covers ::__construct
  27. */
  28. public function test_WP_Error_should_be_of_type_WP_Error() {
  29. $this->assertWPError( $this->wp_error );
  30. }
  31. /**
  32. * @covers ::__construct
  33. */
  34. public function test_WP_Error_with_default_empty_parameters_should_add_no_errors() {
  35. $this->assertEmpty( $this->wp_error->errors );
  36. }
  37. /**
  38. * @covers ::__construct
  39. * @covers ::get_error_code
  40. */
  41. public function test_WP_Error_with_empty_code_should_add_no_code() {
  42. $this->assertSame( '', $this->wp_error->get_error_code() );
  43. }
  44. /**
  45. * @covers ::__construct
  46. * @covers ::get_error_message
  47. */
  48. public function test_WP_Error_with_empty_code_should_add_no_message() {
  49. $this->assertSame( '', $this->wp_error->get_error_message() );
  50. }
  51. /**
  52. * @covers ::__construct
  53. */
  54. public function test_WP_Error_with_empty_code_should_add_no_error_data() {
  55. $this->assertEmpty( $this->wp_error->error_data );
  56. }
  57. /**
  58. * @covers ::__construct
  59. * @covers ::get_error_code
  60. */
  61. public function test_WP_Error_with_code_and_empty_message_should_add_error_with_that_code() {
  62. $wp_error = new WP_Error( 'code' );
  63. $this->assertSame( 'code', $wp_error->get_error_code() );
  64. }
  65. /**
  66. * @covers ::__construct
  67. * @covers ::get_error_message
  68. */
  69. public function test_WP_Error_with_code_and_empty_message_should_add_error_with_that_code_and_empty_message() {
  70. $wp_error = new WP_Error( 'code' );
  71. $this->assertSame( '', $wp_error->get_error_message( 'code' ) );
  72. }
  73. /**
  74. * @covers ::__construct
  75. * @covers ::get_error_data
  76. */
  77. public function test_WP_Error_with_code_and_empty_message_and_empty_data_should_add_error_but_not_associated_data() {
  78. $wp_error = new WP_Error( 'code' );
  79. $this->assertNull( $wp_error->get_error_data( 'code' ) );
  80. }
  81. /**
  82. * @covers ::__construct
  83. * @covers ::get_error_data
  84. */
  85. public function test_WP_Error_with_code_and_empty_message_and_non_empty_data_should_add_error_with_empty_message_and_that_stored_data() {
  86. $wp_error = new WP_Error( 'code', '', 'data' );
  87. $this->assertSame( 'data', $wp_error->get_error_data( 'code' ) );
  88. }
  89. /**
  90. * @covers ::__construct
  91. * @covers ::get_error_code
  92. */
  93. public function test_WP_Error_with_code_and_message_should_add_error_with_that_code() {
  94. $wp_error = new WP_Error( 'code', 'message' );
  95. $this->assertSame( 'code', $wp_error->get_error_code() );
  96. }
  97. /**
  98. * @covers ::__construct
  99. * @covers ::get_error_message
  100. */
  101. public function test_WP_Error_with_code_and_message_should_add_error_with_that_message() {
  102. $wp_error = new WP_Error( 'code', 'message' );
  103. $this->assertSame( 'message', $wp_error->get_error_message( 'code' ) );
  104. }
  105. /**
  106. * @covers ::__construct
  107. * @covers ::get_error_code
  108. */
  109. public function test_WP_Error_with_code_and_message_and_data_should_add_error_with_that_code() {
  110. $wp_error = new WP_Error( 'code', 'message', 'data' );
  111. $this->assertSame( 'code', $wp_error->get_error_code() );
  112. }
  113. /**
  114. * @covers ::__construct
  115. * @covers ::get_error_message
  116. */
  117. public function test_WP_Error_with_code_and_message_and_data_should_add_error_with_that_message() {
  118. $wp_error = new WP_Error( 'code', 'message', 'data' );
  119. $this->assertSame( 'message', $wp_error->get_error_message( 'code' ) );
  120. }
  121. /**
  122. * @covers ::__construct
  123. * @covers ::get_error_data
  124. */
  125. public function test_WP_Error_with_code_and_message_and_data_should_add_error_with_that_data() {
  126. $wp_error = new WP_Error( 'code', 'message', 'data' );
  127. $this->assertSame( 'data', $wp_error->get_error_data( 'code' ) );
  128. }
  129. /**
  130. * @covers ::__construct
  131. * @covers ::get_error_codes
  132. */
  133. public function test_get_error_codes_with_no_errors_should_return_empty_array() {
  134. $this->assertEmpty( $this->wp_error->get_error_codes() );
  135. }
  136. /**
  137. * @covers ::add
  138. * @covers ::get_error_codes
  139. */
  140. public function test_get_error_codes_with_one_error_should_return_an_array_with_only_that_code() {
  141. $this->wp_error->add( 'code', 'message' );
  142. $this->assertSameSets( array( 'code' ), $this->wp_error->get_error_codes() );
  143. }
  144. /**
  145. * @covers ::add
  146. * @covers ::get_error_codes
  147. */
  148. public function test_get_error_codes_with_multiple_errors_should_return_an_array_of_those_codes() {
  149. $this->wp_error->add( 'code', 'message' );
  150. $this->wp_error->add( 'code2', 'message2' );
  151. $expected = array( 'code', 'code2' );
  152. $this->assertSameSets( $expected, $this->wp_error->get_error_codes() );
  153. }
  154. /**
  155. * @covers ::__construct
  156. * @covers ::get_error_code
  157. */
  158. public function test_get_error_code_with_no_errors_should_return_an_empty_string() {
  159. $this->assertSame( '', $this->wp_error->get_error_code() );
  160. }
  161. /**
  162. * @covers ::add
  163. * @covers ::get_error_code
  164. */
  165. public function test_get_error_code_with_one_error_should_return_that_error_code() {
  166. $this->wp_error->add( 'code', 'message' );
  167. $this->assertSame( 'code', $this->wp_error->get_error_code() );
  168. }
  169. /**
  170. * @covers ::add
  171. * @covers ::get_error_code
  172. */
  173. public function test_get_error_code_with_multiple_errors_should_return_only_the_first_error_code() {
  174. $this->wp_error->add( 'code', 'message' );
  175. $this->wp_error->add( 'code2', 'message2' );
  176. $this->assertSame( 'code', $this->wp_error->get_error_code() );
  177. }
  178. /**
  179. * @covers ::__construct
  180. * @covers ::get_error_messages
  181. */
  182. public function test_get_error_messages_with_empty_code_and_no_errors_should_return_an_empty_array() {
  183. $this->assertEmpty( $this->wp_error->get_error_messages() );
  184. }
  185. /**
  186. * @covers ::add
  187. * @covers ::get_error_messages
  188. */
  189. public function test_get_error_messages_with_empty_code_one_error_should_return_an_array_with_that_message() {
  190. $this->wp_error->add( 'code', 'message' );
  191. $this->assertSameSets( array( 'message' ), $this->wp_error->get_error_messages() );
  192. }
  193. /**
  194. * @covers ::add
  195. * @covers ::get_error_messages
  196. */
  197. public function test_get_error_messages_with_empty_code_multiple_errors_should_return_an_array_of_messages() {
  198. $this->wp_error->add( 'code', 'message' );
  199. $this->wp_error->add( 'code2', 'message2' );
  200. $this->assertSameSets( array( 'message', 'message2' ), $this->wp_error->get_error_messages() );
  201. }
  202. /**
  203. * @covers ::__construct
  204. * @covers ::get_error_messages
  205. */
  206. public function test_get_error_messages_with_an_invalid_code_should_return_an_empty_array() {
  207. $this->assertEmpty( $this->wp_error->get_error_messages( 'code' ) );
  208. }
  209. /**
  210. * @covers ::add
  211. * @covers ::get_error_messages
  212. */
  213. public function test_get_error_messages_with_one_error_should_return_an_array_with_that_message() {
  214. $this->wp_error->add( 'code', 'message' );
  215. $this->assertSameSets( array( 'message' ), $this->wp_error->get_error_messages( 'code' ) );
  216. }
  217. /**
  218. * @covers ::add
  219. * @covers ::get_error_messages
  220. */
  221. public function test_get_error_messages_with_multiple_errors_same_code_should_return_an_array_with_all_messages() {
  222. $this->wp_error->add( 'code', 'message' );
  223. $this->wp_error->add( 'code', 'message2' );
  224. $this->assertSameSets( array( 'message', 'message2' ), $this->wp_error->get_error_messages( 'code' ) );
  225. }
  226. /**
  227. * @covers ::__construct
  228. * @covers ::get_error_message
  229. */
  230. public function test_get_error_message_with_empty_code_and_no_errors_should_return_an_empty_string() {
  231. $this->assertSame( '', $this->wp_error->get_error_message() );
  232. }
  233. /**
  234. * @covers ::add
  235. * @covers ::get_error_message
  236. */
  237. public function test_get_error_message_with_empty_code_and_one_error_should_return_that_message() {
  238. $this->wp_error->add( 'code', 'message' );
  239. $this->assertSame( 'message', $this->wp_error->get_error_message() );
  240. }
  241. /**
  242. * @covers ::add
  243. * @covers ::get_error_message
  244. */
  245. public function test_get_error_message_with_empty_code_and_multiple_errors_should_return_the_first_message() {
  246. $this->wp_error->add( 'code', 'message' );
  247. $this->wp_error->add( 'code2', 'message2' );
  248. $this->assertSame( 'message', $this->wp_error->get_error_message() );
  249. }
  250. /**
  251. * @covers ::add
  252. * @covers ::get_error_message
  253. */
  254. public function test_get_error_message_with_empty_code_and_multiple_errors_multiple_codes_should_return_the_first_message() {
  255. $this->wp_error->add( 'code', 'message' );
  256. $this->wp_error->add( 'code2', 'message2' );
  257. $this->wp_error->add( 'code', 'message2' );
  258. $this->assertSame( 'message', $this->wp_error->get_error_message() );
  259. }
  260. /**
  261. * @covers ::__construct
  262. * @covers ::get_error_message
  263. */
  264. public function test_get_error_message_with_invalid_code_and_no_errors_should_return_empty_string() {
  265. $this->assertSame( '', $this->wp_error->get_error_message( 'invalid' ) );
  266. }
  267. /**
  268. * @covers ::add
  269. * @covers ::get_error_message
  270. */
  271. public function test_get_error_message_with_invalid_code_and_one_error_should_return_an_empty_string() {
  272. $this->wp_error->add( 'code', 'message' );
  273. $this->assertSame( '', $this->wp_error->get_error_message( 'invalid' ) );
  274. }
  275. /**
  276. * @covers ::add
  277. * @covers ::get_error_message
  278. */
  279. public function test_get_error_message_with_invalid_code_and_multiple_errors_should_return_an_empty_string() {
  280. $this->wp_error->add( 'code', 'message' );
  281. $this->wp_error->add( 'code2', 'message2' );
  282. $this->assertSame( '', $this->wp_error->get_error_message( 'invalid' ) );
  283. }
  284. /**
  285. * @covers ::__construct
  286. * @covers ::get_error_data
  287. */
  288. public function test_get_error_data_with_empty_code_and_no_errors_should_evaluate_as_null() {
  289. $this->assertNull( $this->wp_error->get_error_data() );
  290. }
  291. /**
  292. * @covers ::add
  293. * @covers ::get_error_data
  294. */
  295. public function test_get_error_data_with_empty_code_one_error_no_data_should_evaluate_as_null() {
  296. $this->wp_error->add( 'code', 'message' );
  297. $this->assertNull( $this->wp_error->get_error_data() );
  298. }
  299. /**
  300. * @covers ::add
  301. * @covers ::get_error_data
  302. */
  303. public function test_get_error_data_with_empty_code_multiple_errors_no_data_should_evaluate_as_null() {
  304. $this->wp_error->add( 'code', 'message' );
  305. $this->wp_error->add( 'code2', 'message2' );
  306. $this->assertNull( $this->wp_error->get_error_data() );
  307. }
  308. /**
  309. * @covers ::add
  310. * @covers ::get_error_data
  311. */
  312. public function test_get_error_data_with_empty_code_and_one_error_with_data_should_return_that_data() {
  313. $expected = array( 'data-key' => 'data-value' );
  314. $this->wp_error->add( 'code', 'message', $expected );
  315. $this->assertSameSetsWithIndex( $expected, $this->wp_error->get_error_data() );
  316. }
  317. /**
  318. * @covers ::add
  319. * @covers ::get_error_data
  320. */
  321. public function test_get_error_data_with_empty_code_and_multiple_errors_different_codes_should_return_the_last_data_of_the_first_code() {
  322. $expected = array( 'data-key' => 'data-value' );
  323. $this->wp_error->add( 'code', 'message', $expected );
  324. $this->wp_error->add( 'code2', 'message2', 'data2' );
  325. $this->assertSameSetsWithIndex( $expected, $this->wp_error->get_error_data() );
  326. }
  327. /**
  328. * @covers ::add
  329. * @covers ::get_error_data
  330. */
  331. public function test_get_error_data_with_empty_code_and_multiple_errors_same_code_should_return_the_last_data_of_the_first_code() {
  332. $this->wp_error->add( 'code', 'message', 'data' );
  333. $this->wp_error->add( 'code', 'message2', 'data2' );
  334. $this->wp_error->add( 'code2', 'message2', 'data3' );
  335. $this->assertSame( 'data2', $this->wp_error->get_error_data() );
  336. }
  337. /**
  338. * @covers ::__construct
  339. * @covers ::get_error_data
  340. */
  341. public function test_get_error_data_with_code_and_no_errors_should_evaluate_as_null() {
  342. $this->assertNull( $this->wp_error->get_error_data( 'code' ) );
  343. }
  344. /**
  345. * @covers ::add
  346. * @covers ::get_error_data
  347. */
  348. public function test_get_error_data_with_code_and_one_error_with_no_data_should_evaluate_as_null() {
  349. $this->wp_error->add( 'code', 'message' );
  350. $this->assertNull( $this->wp_error->get_error_data( 'code' ) );
  351. }
  352. /**
  353. * @covers ::add
  354. * @covers ::get_error_data
  355. */
  356. public function test_get_error_data_with_code_and_one_error_with_data_should_return_that_data() {
  357. $expected = array( 'data-key' => 'data-value' );
  358. $this->wp_error->add( 'code', 'message', $expected );
  359. $this->assertSameSetsWithIndex( $expected, $this->wp_error->get_error_data( 'code' ) );
  360. }
  361. /**
  362. * @covers ::add
  363. * @covers ::get_error_data
  364. */
  365. public function test_get_error_data_with_code_and_multiple_errors_different_codes_should_return_the_last_stored_data_of_the_code() {
  366. $expected = array( 'data3' );
  367. $this->wp_error->add( 'code', 'message', 'data' );
  368. $this->wp_error->add( 'code2', 'message2', 'data2' );
  369. $this->wp_error->add( 'code', 'message3', $expected );
  370. $this->assertSameSetsWithIndex( $expected, $this->wp_error->get_error_data( 'code' ) );
  371. }
  372. /**
  373. * @covers ::add
  374. * @covers ::get_error_data
  375. */
  376. public function test_get_error_data_with_code_and_multiple_errors_same_code_should_return_the_last_stored_data() {
  377. $this->wp_error->add( 'code', 'message', 'data' );
  378. $this->wp_error->add( 'code', 'message2', 'data2' );
  379. $this->wp_error->add( 'code2', 'message3', 'data3' );
  380. $this->assertSame( 'data2', $this->wp_error->get_error_data( 'code' ) );
  381. }
  382. /**
  383. * @covers ::__construct
  384. * @covers ::get_all_error_data
  385. */
  386. public function test_get_all_error_data_with_code_and_no_errors_should_evaluate_as_empty_array() {
  387. $this->assertSame( array(), $this->wp_error->get_all_error_data( 'code' ) );
  388. }
  389. /**
  390. * @covers ::add
  391. * @covers ::get_all_error_data
  392. */
  393. public function test_get_all_error_data_with_code_and_one_error_with_no_data_should_evaluate_as_empty_array() {
  394. $this->wp_error->add( 'code', 'message' );
  395. $this->assertSame( array(), $this->wp_error->get_all_error_data( 'code' ) );
  396. }
  397. /**
  398. * @covers ::add
  399. * @covers ::get_all_error_data
  400. */
  401. public function test_get_all_error_data_with_code_and_one_error_with_data_should_return_that_data() {
  402. $expected = array( 'data-key' => 'data-value' );
  403. $this->wp_error->add( 'code', 'message', $expected );
  404. $actual = $this->wp_error->get_all_error_data( 'code' );
  405. $this->assertCount( 1, $actual );
  406. $this->assertSameSetsWithIndex( $expected, $actual[0] );
  407. }
  408. /**
  409. * @covers ::add
  410. * @covers ::get_all_error_data
  411. */
  412. public function test_get_all_error_data_with_code_and_multiple_errors_same_code_should_return_all_data() {
  413. $this->wp_error->add( 'code', 'message', 'data' );
  414. $this->wp_error->add( 'code', 'message2', 'data2' );
  415. $this->wp_error->add( 'code2', 'message3', 'data3' );
  416. $this->assertSame( array( 'data', 'data2' ), $this->wp_error->get_all_error_data( 'code' ) );
  417. }
  418. /**
  419. * @covers ::add
  420. * @covers ::get_all_error_data
  421. */
  422. public function test_get_all_error_data_should_handle_manipulation_of_error_data_property() {
  423. $this->wp_error->add_data( 'data1', 'code' );
  424. $this->wp_error->add_data( 'data2', 'code' );
  425. $this->wp_error->error_data['code'] = 'dataX';
  426. $this->assertSame( 'dataX', $this->wp_error->get_error_data( 'code' ) );
  427. $this->assertSame( array( 'data1', 'dataX' ), $this->wp_error->get_all_error_data( 'code' ) );
  428. }
  429. /**
  430. * @covers ::__construct
  431. * @covers ::has_errors
  432. */
  433. public function test_has_errors_with_no_errors_returns_false() {
  434. $this->assertFalse( $this->wp_error->has_errors() );
  435. }
  436. /**
  437. * @covers ::add
  438. * @covers ::has_errors
  439. */
  440. public function test_has_errors_with_errors_returns_true() {
  441. $this->wp_error->add( 'code', 'message', 'data' );
  442. $this->assertTrue( $this->wp_error->has_errors() );
  443. }
  444. /**
  445. * @covers ::add
  446. */
  447. public function test_add_with_empty_code_empty_message_empty_data_should_add_empty_key_to_errors_array() {
  448. $this->wp_error->add( '', '', 'data' );
  449. $this->assertArrayHasKey( '', $this->wp_error->errors );
  450. }
  451. /**
  452. * @covers ::add
  453. */
  454. public function test_add_with_empty_code_empty_message_empty_data_should_add_empty_message_to_errors_array_under_empty_key() {
  455. $this->wp_error->add( '', '', 'data' );
  456. $this->assertSameSetsWithIndex( array( '' => array( '' ) ), $this->wp_error->errors );
  457. }
  458. /**
  459. * @covers ::add
  460. */
  461. public function test_add_with_empty_code_empty_message_empty_data_should_not_alter_data() {
  462. $this->wp_error->add( '', '', '' );
  463. $this->assertEmpty( $this->wp_error->error_data );
  464. }
  465. /**
  466. * @covers ::add
  467. */
  468. public function test_add_with_empty_code_empty_message_non_empty_data_should_store_data_under_an_empty_code_key() {
  469. $this->wp_error->add( '', '', 'data' );
  470. $this->assertSameSetsWithIndex( array( '' => 'data' ), $this->wp_error->error_data );
  471. }
  472. /**
  473. * @covers ::add
  474. * @covers ::get_error_code
  475. */
  476. public function test_add_with_code_empty_message_empty_data_should_add_error_with_code() {
  477. $this->wp_error->add( 'code', '' );
  478. $this->assertSame( 'code', $this->wp_error->get_error_code() );
  479. }
  480. /**
  481. * @covers ::add
  482. * @covers ::get_error_message
  483. */
  484. public function test_add_with_code_empty_message_empty_data_should_add_error_with_empty_message() {
  485. $this->wp_error->add( 'code', '' );
  486. $this->assertSame( '', $this->wp_error->get_error_message( 'code' ) );
  487. }
  488. /**
  489. * @covers ::add
  490. * @covers ::get_error_data
  491. */
  492. public function test_add_with_code_empty_message_empty_data_should_not_add_error_data() {
  493. $this->wp_error->add( 'code', '' );
  494. $this->assertNull( $this->wp_error->get_error_data( 'code' ) );
  495. }
  496. /**
  497. * @covers ::add
  498. * @covers ::get_error_message
  499. */
  500. public function test_add_with_code_and_message_and_empty_data_should_should_add_error_with_that_message() {
  501. $this->wp_error->add( 'code', 'message' );
  502. $this->assertSame( 'message', $this->wp_error->get_error_message( 'code' ) );
  503. }
  504. /**
  505. * @covers ::add
  506. * @covers ::get_error_data
  507. */
  508. public function test_add_with_code_and_message_and_empty_data_should_not_alter_stored_data() {
  509. $this->wp_error->add( 'code', 'message' );
  510. $this->assertNull( $this->wp_error->get_error_data( 'code' ) );
  511. }
  512. /**
  513. * @covers ::add
  514. * @covers ::get_error_code
  515. */
  516. public function test_add_with_code_and_empty_message_and_data_should_add_error_with_that_code() {
  517. $this->wp_error->add( 'code', '', 'data' );
  518. $this->assertSame( 'code', $this->wp_error->get_error_code() );
  519. }
  520. /**
  521. * @covers ::add
  522. * @covers ::get_error_data
  523. */
  524. public function test_add_with_code_and_empty_message_and_data_should_store_that_data() {
  525. $this->wp_error->add( 'code', '', 'data' );
  526. $this->assertSame( 'data', $this->wp_error->get_error_data( 'code' ) );
  527. }
  528. /**
  529. * @covers ::add
  530. * @covers ::get_error_code
  531. */
  532. public function test_add_with_code_and_message_and_data_should_add_an_error_with_that_code() {
  533. $this->wp_error->add( 'code', 'message', 'data' );
  534. $this->assertSame( 'code', $this->wp_error->get_error_code() );
  535. }
  536. /**
  537. * @covers ::add
  538. * @covers ::get_error_message
  539. */
  540. public function test_add_with_code_and_message_and_data_should_add_an_error_with_that_message() {
  541. $this->wp_error->add( 'code', 'message', 'data' );
  542. $this->assertSame( 'message', $this->wp_error->get_error_message( 'code' ) );
  543. }
  544. /**
  545. * @covers ::add
  546. * @covers ::get_error_data
  547. */
  548. public function test_add_with_code_and_message_and_data_should_store_that_data() {
  549. $this->wp_error->add( 'code', 'message', 'data' );
  550. $this->assertSame( 'data', $this->wp_error->get_error_data( 'code' ) );
  551. }
  552. /**
  553. * @covers ::add
  554. * @covers ::get_error_messages
  555. */
  556. public function test_add_multiple_times_with_the_same_code_should_add_additional_messages_for_that_code() {
  557. $this->wp_error->add( 'code', 'message' );
  558. $this->wp_error->add( 'code', 'message2' );
  559. $expected = array( 'message', 'message2' );
  560. $this->assertSameSets( $expected, $this->wp_error->get_error_messages( 'code' ) );
  561. }
  562. /**
  563. * @covers ::add
  564. * @covers ::get_error_data
  565. */
  566. public function test_add_multiple_times_with_the_same_code_and_different_data_should_store_only_the_last_added_data() {
  567. $this->wp_error->add( 'code', 'message', 'data-bar' );
  568. $this->wp_error->add( 'code', 'message2', 'data-baz' );
  569. $this->assertSame( 'data-baz', $this->wp_error->get_error_data( 'code' ) );
  570. }
  571. /**
  572. * @covers ::add_data
  573. */
  574. public function test_add_data_with_empty_data_empty_code_should_create_orphaned_data_with_no_error() {
  575. $this->wp_error->add_data( '' );
  576. $this->assertEmpty( $this->wp_error->errors );
  577. }
  578. /**
  579. * @covers ::add_data
  580. */
  581. public function test_add_data_with_empty_data_empty_code_no_errors_should_create_data_under_an_empty_code_key() {
  582. $this->wp_error->add_data( '' );
  583. $this->assertSameSets( array( '' => '' ), $this->wp_error->error_data );
  584. }
  585. /**
  586. * @covers ::add_data
  587. * @covers ::get_error_data
  588. */
  589. public function test_add_data_with_data_empty_code_and_one_error_should_store_the_data_under_that_code() {
  590. $this->wp_error->add( 'code', 'message' );
  591. $this->wp_error->add_data( 'data' );
  592. $this->assertSame( 'data', $this->wp_error->get_error_data( 'code' ) );
  593. }
  594. /**
  595. * @covers ::add_data
  596. * @covers ::get_error_data
  597. */
  598. public function test_add_data_with_data_empty_code_and_multiple_errors_with_different_codes_should_store_it_under_the_first_code() {
  599. $this->wp_error->add( 'code', 'message' );
  600. $this->wp_error->add( 'code2', 'message2' );
  601. $this->wp_error->add_data( 'data' );
  602. $this->assertSame( 'data', $this->wp_error->get_error_data( 'code' ) );
  603. }
  604. /**
  605. * @covers ::add_data
  606. * @covers ::get_error_data
  607. */
  608. public function test_add_data_with_data_empty_code_and_multiple_errors_with_same_code_should_store_it_under_the_first_code() {
  609. $this->wp_error->add( 'code', 'message' );
  610. $this->wp_error->add( 'code2', 'message2' );
  611. $this->wp_error->add( 'code', 'message3' );
  612. $this->wp_error->add_data( 'data' );
  613. $this->assertSame( 'data', $this->wp_error->get_error_data( 'code' ) );
  614. }
  615. /**
  616. * @covers ::add_data
  617. */
  618. public function test_add_data_with_data_and_code_and_no_errors_should_create_orphaned_data_with_no_error() {
  619. $this->wp_error->add_data( 'data', 'code' );
  620. $this->assertEmpty( $this->wp_error->errors );
  621. }
  622. /**
  623. * @covers ::add_data
  624. */
  625. public function test_add_data_with_data_and_code_no_errors_should_create_data_under_that_code_key() {
  626. $this->wp_error->add_data( 'data', 'code' );
  627. $this->assertSameSets( array( 'code' => 'data' ), $this->wp_error->error_data );
  628. }
  629. /**
  630. * @covers ::add_data
  631. */
  632. public function test_add_data_with_data_and_code_one_error_different_code_should_create_orphaned_data_with_no_error() {
  633. $this->wp_error->add( 'code', 'message' );
  634. $this->wp_error->add_data( 'data', 'code2' );
  635. $this->assertSameSetsWithIndex( array( 'code' => array( 'message' ) ), $this->wp_error->errors );
  636. }
  637. /**
  638. * @covers ::add_data
  639. */
  640. public function test_add_data_with_data_and_code_one_error_different_code_should_create_data_under_that_code_key() {
  641. $this->wp_error->add( 'code', 'message' );
  642. $this->wp_error->add_data( 'data', 'code2' );
  643. $this->assertSameSetsWithIndex( array( 'code2' => 'data' ), $this->wp_error->error_data );
  644. }
  645. /**
  646. * @covers ::add_data
  647. * @covers ::get_error_data
  648. */
  649. public function test_add_data_with_data_and_code_should_add_data() {
  650. $this->wp_error->add( 'code', 'message' );
  651. $this->wp_error->add_data( 'data', 'code' );
  652. $this->assertSame( 'data', $this->wp_error->get_error_data( 'code' ) );
  653. }
  654. /**
  655. * @covers ::remove
  656. */
  657. public function test_remove_with_no_errors_should_affect_nothing() {
  658. $before = $this->wp_error->errors;
  659. $this->wp_error->remove( 'code' );
  660. $after = $this->wp_error->errors;
  661. $this->assertSameSetsWithIndex( $before, $after );
  662. }
  663. /**
  664. * @covers ::remove
  665. */
  666. public function test_remove_empty_code_no_errors_should_affect_nothing() {
  667. $before = $this->wp_error->errors;
  668. $this->wp_error->remove( '' );
  669. $after = $this->wp_error->errors;
  670. $this->assertSameSetsWithIndex( $before, $after );
  671. }
  672. /**
  673. * @covers ::remove
  674. */
  675. public function test_remove_empty_code_and_one_error_with_empty_string_code_should_remove_error() {
  676. $before = $this->wp_error->errors;
  677. $this->wp_error->add( '', 'message' );
  678. $this->wp_error->remove( '' );
  679. $after = $this->wp_error->errors;
  680. $this->assertSameSetsWithIndex( $before, $after );
  681. }
  682. /**
  683. * @covers ::remove
  684. */
  685. public function test_remove_empty_code_and_one_error_with_empty_string_code_should_remove_error_data() {
  686. $this->wp_error->add( '', 'message', 'data' );
  687. $this->wp_error->remove( '' );
  688. $after = $this->wp_error->error_data;
  689. $this->assertEmpty( $this->wp_error->error_data );
  690. }
  691. /**
  692. * @covers ::remove
  693. */
  694. public function test_remove_should_remove_the_error_with_the_given_code() {
  695. $this->wp_error->add( 'code', 'message' );
  696. $this->wp_error->remove( 'code' );
  697. $this->assertEmpty( $this->wp_error->errors );
  698. }
  699. /**
  700. * @covers ::remove
  701. * @covers ::get_error_data
  702. * @covers ::get_all_error_data
  703. */
  704. public function test_remove_should_remove_the_error_data_associated_with_the_given_code() {
  705. $this->wp_error->add( 'code', 'message', 'data' );
  706. $this->wp_error->add( 'code', 'message', 'data2' );
  707. $this->wp_error->remove( 'code' );
  708. $this->assertEmpty( $this->wp_error->error_data );
  709. $this->assertEmpty( $this->wp_error->get_error_data( 'code' ) );
  710. $this->assertEmpty( $this->wp_error->get_all_error_data( 'code' ) );
  711. }
  712. /**
  713. * @covers ::merge_from
  714. * @covers ::get_error_messages
  715. * @covers ::get_error_data
  716. * @covers ::get_all_error_data
  717. * @covers ::get_error_message
  718. */
  719. public function test_merge_from_should_copy_other_error_into_instance() {
  720. $this->wp_error->add( 'code1', 'message1', 'data1' );
  721. $other = new WP_Error( 'code1', 'message2', 'data2' );
  722. $other->add( 'code2', 'message3' );
  723. $this->wp_error->merge_from( $other );
  724. $this->assertSame( array( 'message1', 'message2' ), $this->wp_error->get_error_messages( 'code1' ) );
  725. $this->assertSame( 'data2', $this->wp_error->get_error_data( 'code1' ) );
  726. $this->assertSame( array( 'data1', 'data2' ), $this->wp_error->get_all_error_data( 'code1' ) );
  727. $this->assertSame( 'message3', $this->wp_error->get_error_message( 'code2' ) );
  728. }
  729. /**
  730. * @covers ::merge_from
  731. * @covers ::has_errors
  732. */
  733. public function test_merge_from_with_no_errors_should_not_add_to_instance() {
  734. $other = new WP_Error();
  735. $this->wp_error->merge_from( $other );
  736. $this->assertFalse( $this->wp_error->has_errors() );
  737. }
  738. /**
  739. * @covers ::export_to
  740. * @covers ::get_error_messages
  741. * @covers ::get_error_data
  742. * @covers ::get_all_error_data
  743. * @covers ::get_error_message
  744. */
  745. public function test_export_to_should_copy_instance_into_other_error() {
  746. $other = new WP_Error();
  747. $other->add( 'code1', 'message1', 'data1' );
  748. $this->wp_error->add( 'code1', 'message2', 'data2' );
  749. $this->wp_error->add( 'code2', 'message3' );
  750. $this->wp_error->export_to( $other );
  751. $this->assertSame( array( 'message1', 'message2' ), $other->get_error_messages( 'code1' ) );
  752. $this->assertSame( 'data2', $other->get_error_data( 'code1' ) );
  753. $this->assertSame( array( 'data1', 'data2' ), $other->get_all_error_data( 'code1' ) );
  754. $this->assertSame( 'message3', $other->get_error_message( 'code2' ) );
  755. }
  756. /**
  757. * @covers ::export_to
  758. * @covers ::has_errors
  759. */
  760. public function test_export_to_with_no_errors_should_not_add_to_other_error() {
  761. $other = new WP_Error();
  762. $this->wp_error->export_to( $other );
  763. $this->assertFalse( $other->has_errors() );
  764. }
  765. }