StatusTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. <?php
  2. /**
  3. * @author Addshore
  4. */
  5. class StatusTest extends MediaWikiLangTestCase {
  6. /**
  7. * @dataProvider provideValues
  8. * @covers Status::newGood
  9. */
  10. public function testNewGood( $value = null ) {
  11. $status = Status::newGood( $value );
  12. $this->assertTrue( $status->isGood() );
  13. $this->assertTrue( $status->isOK() );
  14. $this->assertEquals( $value, $status->getValue() );
  15. }
  16. public static function provideValues() {
  17. return [
  18. [],
  19. [ 'foo' ],
  20. [ [ 'foo' => 'bar' ] ],
  21. [ new Exception() ],
  22. [ 1234 ],
  23. ];
  24. }
  25. /**
  26. * @covers Status::newFatal
  27. */
  28. public function testNewFatalWithMessage() {
  29. $message = $this->getMockBuilder( Message::class )
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. $status = Status::newFatal( $message );
  33. $this->assertFalse( $status->isGood() );
  34. $this->assertFalse( $status->isOK() );
  35. $this->assertEquals( $message, $status->getMessage() );
  36. }
  37. /**
  38. * @covers Status::newFatal
  39. */
  40. public function testNewFatalWithString() {
  41. $message = 'foo';
  42. $status = Status::newFatal( $message );
  43. $this->assertFalse( $status->isGood() );
  44. $this->assertFalse( $status->isOK() );
  45. $this->assertEquals( $message, $status->getMessage()->getKey() );
  46. }
  47. /**
  48. * Test 'ok' and 'errors' getters.
  49. *
  50. * @covers Status::__get
  51. */
  52. public function testOkAndErrorsGetters() {
  53. $status = Status::newGood( 'foo' );
  54. $this->assertTrue( $status->ok );
  55. $status = Status::newFatal( 'foo', 1, 2 );
  56. $this->assertFalse( $status->ok );
  57. $this->assertArrayEquals(
  58. [
  59. [
  60. 'type' => 'error',
  61. 'message' => 'foo',
  62. 'params' => [ 1, 2 ]
  63. ]
  64. ],
  65. $status->errors
  66. );
  67. }
  68. /**
  69. * Test 'ok' setter.
  70. *
  71. * @covers Status::__set
  72. */
  73. public function testOkSetter() {
  74. $status = new Status();
  75. $status->ok = false;
  76. $this->assertFalse( $status->isOK() );
  77. $status->ok = true;
  78. $this->assertTrue( $status->isOK() );
  79. }
  80. /**
  81. * @dataProvider provideSetResult
  82. * @covers Status::setResult
  83. */
  84. public function testSetResult( $ok, $value = null ) {
  85. $status = new Status();
  86. $status->setResult( $ok, $value );
  87. $this->assertEquals( $ok, $status->isOK() );
  88. $this->assertEquals( $value, $status->getValue() );
  89. }
  90. public static function provideSetResult() {
  91. return [
  92. [ true ],
  93. [ false ],
  94. [ true, 'value' ],
  95. [ false, 'value' ],
  96. ];
  97. }
  98. /**
  99. * @dataProvider provideIsOk
  100. * @covers Status::setOK
  101. * @covers Status::isOK
  102. */
  103. public function testIsOk( $ok ) {
  104. $status = new Status();
  105. $status->setOK( $ok );
  106. $this->assertEquals( $ok, $status->isOK() );
  107. }
  108. public static function provideIsOk() {
  109. return [
  110. [ true ],
  111. [ false ],
  112. ];
  113. }
  114. /**
  115. * @covers Status::getValue
  116. */
  117. public function testGetValue() {
  118. $status = new Status();
  119. $status->value = 'foobar';
  120. $this->assertEquals( 'foobar', $status->getValue() );
  121. }
  122. /**
  123. * @dataProvider provideIsGood
  124. * @covers Status::isGood
  125. */
  126. public function testIsGood( $ok, $errors, $expected ) {
  127. $status = new Status();
  128. $status->setOK( $ok );
  129. foreach ( $errors as $error ) {
  130. $status->warning( $error );
  131. }
  132. $this->assertEquals( $expected, $status->isGood() );
  133. }
  134. public static function provideIsGood() {
  135. return [
  136. [ true, [], true ],
  137. [ true, [ 'foo' ], false ],
  138. [ false, [], false ],
  139. [ false, [ 'foo' ], false ],
  140. ];
  141. }
  142. /**
  143. * @dataProvider provideMockMessageDetails
  144. * @covers Status::warning
  145. * @covers Status::getWarningsArray
  146. * @covers Status::getStatusArray
  147. */
  148. public function testWarningWithMessage( $mockDetails ) {
  149. $status = new Status();
  150. $messages = $this->getMockMessages( $mockDetails );
  151. foreach ( $messages as $message ) {
  152. $status->warning( $message );
  153. }
  154. $warnings = $status->getWarningsArray();
  155. $this->assertEquals( count( $messages ), count( $warnings ) );
  156. foreach ( $messages as $key => $message ) {
  157. $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
  158. $this->assertEquals( $warnings[$key], $expectedArray );
  159. }
  160. }
  161. /**
  162. * @dataProvider provideMockMessageDetails
  163. * @covers Status::error
  164. * @covers Status::getErrorsArray
  165. * @covers Status::getStatusArray
  166. * @covers Status::getErrors
  167. */
  168. public function testErrorWithMessage( $mockDetails ) {
  169. $status = new Status();
  170. $messages = $this->getMockMessages( $mockDetails );
  171. foreach ( $messages as $message ) {
  172. $status->error( $message );
  173. }
  174. $errors = $status->getErrorsArray();
  175. $this->assertEquals( count( $messages ), count( $errors ) );
  176. foreach ( $messages as $key => $message ) {
  177. $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
  178. $this->assertEquals( $errors[$key], $expectedArray );
  179. }
  180. }
  181. /**
  182. * @dataProvider provideMockMessageDetails
  183. * @covers Status::fatal
  184. * @covers Status::getErrorsArray
  185. * @covers Status::getStatusArray
  186. */
  187. public function testFatalWithMessage( $mockDetails ) {
  188. $status = new Status();
  189. $messages = $this->getMockMessages( $mockDetails );
  190. foreach ( $messages as $message ) {
  191. $status->fatal( $message );
  192. }
  193. $errors = $status->getErrorsArray();
  194. $this->assertEquals( count( $messages ), count( $errors ) );
  195. foreach ( $messages as $key => $message ) {
  196. $expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
  197. $this->assertEquals( $errors[$key], $expectedArray );
  198. }
  199. $this->assertFalse( $status->isOK() );
  200. }
  201. protected function getMockMessage( $key = 'key', $params = [] ) {
  202. $message = $this->getMockBuilder( Message::class )
  203. ->disableOriginalConstructor()
  204. ->getMock();
  205. $message->expects( $this->atLeastOnce() )
  206. ->method( 'getKey' )
  207. ->will( $this->returnValue( $key ) );
  208. $message->expects( $this->atLeastOnce() )
  209. ->method( 'getParams' )
  210. ->will( $this->returnValue( $params ) );
  211. return $message;
  212. }
  213. /**
  214. * @param array $messageDetails E.g. array( 'KEY' => array(/PARAMS/) )
  215. * @return Message[]
  216. */
  217. protected function getMockMessages( $messageDetails ) {
  218. $messages = [];
  219. foreach ( $messageDetails as $key => $paramsArray ) {
  220. $messages[] = $this->getMockMessage( $key, $paramsArray );
  221. }
  222. return $messages;
  223. }
  224. public static function provideMockMessageDetails() {
  225. return [
  226. [ [ 'key1' => [ 'foo' => 'bar' ] ] ],
  227. [ [ 'key1' => [ 'foo' => 'bar' ], 'key2' => [ 'foo2' => 'bar2' ] ] ],
  228. ];
  229. }
  230. /**
  231. * @covers Status::merge
  232. */
  233. public function testMerge() {
  234. $status1 = new Status();
  235. $status2 = new Status();
  236. $message1 = $this->getMockMessage( 'warn1' );
  237. $message2 = $this->getMockMessage( 'error2' );
  238. $status1->warning( $message1 );
  239. $status2->error( $message2 );
  240. $status1->merge( $status2 );
  241. $this->assertEquals(
  242. 2,
  243. count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() )
  244. );
  245. }
  246. /**
  247. * @covers Status::merge
  248. */
  249. public function testMergeWithOverwriteValue() {
  250. $status1 = new Status();
  251. $status2 = new Status();
  252. $message1 = $this->getMockMessage( 'warn1' );
  253. $message2 = $this->getMockMessage( 'error2' );
  254. $status1->warning( $message1 );
  255. $status2->error( $message2 );
  256. $status2->value = 'FooValue';
  257. $status1->merge( $status2, true );
  258. $this->assertEquals(
  259. 2,
  260. count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() )
  261. );
  262. $this->assertEquals( 'FooValue', $status1->getValue() );
  263. }
  264. /**
  265. * @covers Status::hasMessage
  266. */
  267. public function testHasMessage() {
  268. $status = new Status();
  269. $status->fatal( 'bad' );
  270. $status->fatal( wfMessage( 'bad-msg' ) );
  271. $this->assertTrue( $status->hasMessage( 'bad' ) );
  272. $this->assertTrue( $status->hasMessage( 'bad-msg' ) );
  273. $this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg' ) ) );
  274. $this->assertFalse( $status->hasMessage( 'good' ) );
  275. }
  276. /**
  277. * @dataProvider provideCleanParams
  278. * @covers Status::cleanParams
  279. */
  280. public function testCleanParams( $cleanCallback, $params, $expected ) {
  281. $method = new ReflectionMethod( Status::class, 'cleanParams' );
  282. $method->setAccessible( true );
  283. $status = new Status();
  284. $status->cleanCallback = $cleanCallback;
  285. $this->assertEquals( $expected, $method->invoke( $status, $params ) );
  286. }
  287. public static function provideCleanParams() {
  288. $cleanCallback = function ( $value ) {
  289. return '-' . $value . '-';
  290. };
  291. return [
  292. [ false, [ 'foo' => 'bar' ], [ 'foo' => 'bar' ] ],
  293. [ $cleanCallback, [ 'foo' => 'bar' ], [ 'foo' => '-bar-' ] ],
  294. ];
  295. }
  296. /**
  297. * @dataProvider provideGetWikiTextAndHtml
  298. * @covers Status::getWikiText
  299. */
  300. public function testGetWikiText(
  301. Status $status, $wikitext, $wrappedWikitext, $html, $wrappedHtml
  302. ) {
  303. $this->assertEquals( $wikitext, $status->getWikiText() );
  304. $this->assertEquals( $wrappedWikitext, $status->getWikiText( 'wrap-short', 'wrap-long', 'qqx' ) );
  305. }
  306. /**
  307. * @dataProvider provideGetWikiTextAndHtml
  308. * @covers Status::getHtml
  309. */
  310. public function testGetHtml(
  311. Status $status, $wikitext, $wrappedWikitext, $html, $wrappedHtml
  312. ) {
  313. $this->assertEquals( $html, $status->getHTML() );
  314. $this->assertEquals( $wrappedHtml, $status->getHTML( 'wrap-short', 'wrap-long', 'qqx' ) );
  315. }
  316. /**
  317. * @return array Array of arrays with values;
  318. * 0 => status object
  319. * 1 => expected string (with no context)
  320. */
  321. public static function provideGetWikiTextAndHtml() {
  322. $testCases = [];
  323. $testCases['GoodStatus'] = [
  324. new Status(),
  325. "Internal error: Status::getWikiText called for a good result, this is incorrect\n",
  326. "(wrap-short: (internalerror_info: Status::getWikiText called for a good result, " .
  327. "this is incorrect\n))",
  328. "<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
  329. "<p>(wrap-short: (internalerror_info: Status::getWikiText called for a good result, " .
  330. "this is incorrect\n))\n</p>",
  331. ];
  332. $status = new Status();
  333. $status->setOK( false );
  334. $testCases['GoodButNoError'] = [
  335. $status,
  336. "Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
  337. "(wrap-short: (internalerror_info: Status::getWikiText: Invalid result object: " .
  338. "no error text but not OK\n))",
  339. "<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
  340. "<p>(wrap-short: (internalerror_info: Status::getWikiText: Invalid result object: " .
  341. "no error text but not OK\n))\n</p>",
  342. ];
  343. $status = new Status();
  344. $status->warning( 'fooBar!' );
  345. $testCases['1StringWarning'] = [
  346. $status,
  347. "⧼fooBar!⧽",
  348. "(wrap-short: (fooBar!))",
  349. "<p>⧼fooBar!⧽\n</p>",
  350. "<p>(wrap-short: (fooBar!))\n</p>",
  351. ];
  352. $status = new Status();
  353. $status->warning( 'fooBar!' );
  354. $status->warning( 'fooBar2!' );
  355. $testCases['2StringWarnings'] = [
  356. $status,
  357. "* ⧼fooBar!⧽\n* ⧼fooBar2!⧽\n",
  358. "(wrap-long: * (fooBar!)\n* (fooBar2!)\n)",
  359. "<ul><li>⧼fooBar!⧽</li>\n<li>⧼fooBar2!⧽</li></ul>\n",
  360. "<p>(wrap-long: * (fooBar!)\n</p>\n<ul><li>(fooBar2!)</li></ul>\n<p>)\n</p>",
  361. ];
  362. $status = new Status();
  363. $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
  364. $testCases['1MessageWarning'] = [
  365. $status,
  366. "⧼fooBar!⧽",
  367. "(wrap-short: (fooBar!: foo, bar))",
  368. "<p>⧼fooBar!⧽\n</p>",
  369. "<p>(wrap-short: (fooBar!: foo, bar))\n</p>",
  370. ];
  371. $status = new Status();
  372. $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
  373. $status->warning( new Message( 'fooBar2!' ) );
  374. $testCases['2MessageWarnings'] = [
  375. $status,
  376. "* ⧼fooBar!⧽\n* ⧼fooBar2!⧽\n",
  377. "(wrap-long: * (fooBar!: foo, bar)\n* (fooBar2!)\n)",
  378. "<ul><li>⧼fooBar!⧽</li>\n<li>⧼fooBar2!⧽</li></ul>\n",
  379. "<p>(wrap-long: * (fooBar!: foo, bar)\n</p>\n<ul><li>(fooBar2!)</li></ul>\n<p>)\n</p>",
  380. ];
  381. return $testCases;
  382. }
  383. private static function sanitizedMessageParams( Message $message ) {
  384. return array_map( function ( $p ) {
  385. return $p instanceof Message
  386. ? [
  387. 'key' => $p->getKey(),
  388. 'params' => self::sanitizedMessageParams( $p ),
  389. 'lang' => $p->getLanguage()->getCode(),
  390. ]
  391. : $p;
  392. }, $message->getParams() );
  393. }
  394. /**
  395. * @dataProvider provideGetMessage
  396. * @covers Status::getMessage
  397. */
  398. public function testGetMessage(
  399. Status $status, $expectedParams = [], $expectedKey, $expectedWrapper
  400. ) {
  401. $message = $status->getMessage( null, null, 'qqx' );
  402. $this->assertInstanceOf( Message::class, $message );
  403. $this->assertEquals( $expectedParams, self::sanitizedMessageParams( $message ),
  404. 'Message::getParams' );
  405. $this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
  406. $message = $status->getMessage( 'wrapper-short', 'wrapper-long' );
  407. $this->assertInstanceOf( Message::class, $message );
  408. $this->assertEquals( $expectedWrapper, $message->getKey(), 'Message::getKey with wrappers' );
  409. $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
  410. $message = $status->getMessage( 'wrapper' );
  411. $this->assertInstanceOf( Message::class, $message );
  412. $this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' );
  413. $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
  414. $message = $status->getMessage( false, 'wrapper' );
  415. $this->assertInstanceOf( Message::class, $message );
  416. $this->assertEquals( 'wrapper', $message->getKey(), 'Message::getKey with wrappers' );
  417. $this->assertCount( 1, $message->getParams(), 'Message::getParams with wrappers' );
  418. }
  419. /**
  420. * @return array Array of arrays with values;
  421. * 0 => status object
  422. * 1 => expected Message parameters (with no context)
  423. * 2 => expected Message key
  424. */
  425. public static function provideGetMessage() {
  426. $testCases = [];
  427. $testCases['GoodStatus'] = [
  428. new Status(),
  429. [ "Status::getMessage called for a good result, this is incorrect\n" ],
  430. 'internalerror_info',
  431. 'wrapper-short'
  432. ];
  433. $status = new Status();
  434. $status->setOK( false );
  435. $testCases['GoodButNoError'] = [
  436. $status,
  437. [ "Status::getMessage: Invalid result object: no error text but not OK\n" ],
  438. 'internalerror_info',
  439. 'wrapper-short'
  440. ];
  441. $status = new Status();
  442. $status->warning( 'fooBar!' );
  443. $testCases['1StringWarning'] = [
  444. $status,
  445. [],
  446. 'fooBar!',
  447. 'wrapper-short'
  448. ];
  449. $status = new Status();
  450. $status->warning( 'fooBar!' );
  451. $status->warning( 'fooBar2!' );
  452. $testCases[ '2StringWarnings' ] = [
  453. $status,
  454. [
  455. [ 'key' => 'fooBar!', 'params' => [], 'lang' => 'qqx' ],
  456. [ 'key' => 'fooBar2!', 'params' => [], 'lang' => 'qqx' ]
  457. ],
  458. "* \$1\n* \$2",
  459. 'wrapper-long'
  460. ];
  461. $status = new Status();
  462. $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
  463. $testCases['1MessageWarning'] = [
  464. $status,
  465. [ 'foo', 'bar' ],
  466. 'fooBar!',
  467. 'wrapper-short'
  468. ];
  469. $status = new Status();
  470. $status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
  471. $status->warning( new Message( 'fooBar2!' ) );
  472. $testCases['2MessageWarnings'] = [
  473. $status,
  474. [
  475. [ 'key' => 'fooBar!', 'params' => [ 'foo', 'bar' ], 'lang' => 'qqx' ],
  476. [ 'key' => 'fooBar2!', 'params' => [], 'lang' => 'qqx' ]
  477. ],
  478. "* \$1\n* \$2",
  479. 'wrapper-long'
  480. ];
  481. return $testCases;
  482. }
  483. /**
  484. * @covers Status::replaceMessage
  485. */
  486. public function testReplaceMessage() {
  487. $status = new Status();
  488. $message = new Message( 'key1', [ 'foo1', 'bar1' ] );
  489. $status->error( $message );
  490. $newMessage = new Message( 'key2', [ 'foo2', 'bar2' ] );
  491. $status->replaceMessage( $message, $newMessage );
  492. $this->assertEquals( $newMessage, $status->errors[0]['message'] );
  493. }
  494. /**
  495. * @covers Status::getErrorMessage
  496. */
  497. public function testGetErrorMessage() {
  498. $method = new ReflectionMethod( Status::class, 'getErrorMessage' );
  499. $method->setAccessible( true );
  500. $status = new Status();
  501. $key = 'foo';
  502. $params = [ 'bar' ];
  503. /** @var Message $message */
  504. $message = $method->invoke( $status, array_merge( [ $key ], $params ) );
  505. $this->assertInstanceOf( Message::class, $message );
  506. $this->assertEquals( $key, $message->getKey() );
  507. $this->assertEquals( $params, $message->getParams() );
  508. }
  509. /**
  510. * @covers Status::getErrorMessageArray
  511. */
  512. public function testGetErrorMessageArray() {
  513. $method = new ReflectionMethod( Status::class, 'getErrorMessageArray' );
  514. $method->setAccessible( true );
  515. $status = new Status();
  516. $key = 'foo';
  517. $params = [ 'bar' ];
  518. /** @var Message[] $messageArray */
  519. $messageArray = $method->invoke(
  520. $status,
  521. [
  522. array_merge( [ $key ], $params ),
  523. array_merge( [ $key ], $params )
  524. ]
  525. );
  526. $this->assertInternalType( 'array', $messageArray );
  527. $this->assertCount( 2, $messageArray );
  528. foreach ( $messageArray as $message ) {
  529. $this->assertInstanceOf( Message::class, $message );
  530. $this->assertEquals( $key, $message->getKey() );
  531. $this->assertEquals( $params, $message->getParams() );
  532. }
  533. }
  534. /**
  535. * @covers Status::getErrorsByType
  536. */
  537. public function testGetErrorsByType() {
  538. $status = new Status();
  539. $warning = new Message( 'warning111' );
  540. $error = new Message( 'error111' );
  541. $status->warning( $warning );
  542. $status->error( $error );
  543. $warnings = $status->getErrorsByType( 'warning' );
  544. $errors = $status->getErrorsByType( 'error' );
  545. $this->assertCount( 1, $warnings );
  546. $this->assertCount( 1, $errors );
  547. $this->assertEquals( $warning, $warnings[0]['message'] );
  548. $this->assertEquals( $error, $errors[0]['message'] );
  549. }
  550. /**
  551. * @covers Status::__wakeup
  552. */
  553. public function testWakeUpSanitizesCallback() {
  554. $status = new Status();
  555. $status->cleanCallback = function ( $value ) {
  556. return '-' . $value . '-';
  557. };
  558. $status->__wakeup();
  559. $this->assertEquals( false, $status->cleanCallback );
  560. }
  561. /**
  562. * @dataProvider provideNonObjectMessages
  563. * @covers Status::getStatusArray
  564. */
  565. public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
  566. $status = new Status();
  567. if ( !array_key_exists( 1, $nonObjMsg ) ) {
  568. $status->warning( $nonObjMsg[0] );
  569. } else {
  570. $status->warning( $nonObjMsg[0], $nonObjMsg[1] );
  571. }
  572. $array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
  573. $this->assertEquals( 1, count( $array ) );
  574. $this->assertEquals( $nonObjMsg, $array[0] );
  575. }
  576. public static function provideNonObjectMessages() {
  577. return [
  578. [ [ 'ImaString', [ 'param1' => 'value1' ] ] ],
  579. [ [ 'ImaString' ] ],
  580. ];
  581. }
  582. /**
  583. * @dataProvider provideErrorsWarningsOnly
  584. * @covers Status::splitByErrorType
  585. * @covers StatusValue::splitByErrorType
  586. */
  587. public function testGetErrorsWarningsOnlyStatus( $errorText, $warningText, $type, $errorResult,
  588. $warningResult
  589. ) {
  590. $status = Status::newGood();
  591. if ( $errorText ) {
  592. $status->fatal( $errorText );
  593. }
  594. if ( $warningText ) {
  595. $status->warning( $warningText );
  596. }
  597. $testStatus = $status->splitByErrorType()[$type];
  598. $this->assertEquals( $errorResult, $testStatus->getErrorsByType( 'error' ) );
  599. $this->assertEquals( $warningResult, $testStatus->getErrorsByType( 'warning' ) );
  600. }
  601. public static function provideErrorsWarningsOnly() {
  602. return [
  603. [
  604. 'Just an error',
  605. 'Just a warning',
  606. 0,
  607. [
  608. 0 => [
  609. 'type' => 'error',
  610. 'message' => 'Just an error',
  611. 'params' => []
  612. ],
  613. ],
  614. [],
  615. ], [
  616. 'Just an error',
  617. 'Just a warning',
  618. 1,
  619. [],
  620. [
  621. 0 => [
  622. 'type' => 'warning',
  623. 'message' => 'Just a warning',
  624. 'params' => []
  625. ],
  626. ],
  627. ], [
  628. null,
  629. null,
  630. 1,
  631. [],
  632. [],
  633. ], [
  634. null,
  635. null,
  636. 0,
  637. [],
  638. [],
  639. ]
  640. ];
  641. }
  642. }