DebuggerTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Error;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\Configure;
  19. use Cake\Error\Debugger;
  20. use Cake\Log\Log;
  21. use Cake\TestSuite\TestCase;
  22. use stdClass;
  23. use TestApp\Error\TestDebugger;
  24. use TestApp\Error\Thing\DebuggableThing;
  25. use TestApp\Error\Thing\SecurityThing;
  26. /**
  27. * DebuggerTest class
  28. *
  29. * !!! Be careful with changing code below as it may
  30. * !!! change line numbers which are used in the tests
  31. */
  32. class DebuggerTest extends TestCase
  33. {
  34. protected $_restoreError = false;
  35. /**
  36. * setUp method
  37. *
  38. * @return void
  39. */
  40. public function setUp(): void
  41. {
  42. parent::setUp();
  43. Configure::write('debug', true);
  44. Log::drop('stderr');
  45. Log::drop('stdout');
  46. }
  47. /**
  48. * tearDown method
  49. *
  50. * @return void
  51. */
  52. public function tearDown(): void
  53. {
  54. parent::tearDown();
  55. if ($this->_restoreError) {
  56. restore_error_handler();
  57. }
  58. }
  59. /**
  60. * testDocRef method
  61. *
  62. * @return void
  63. */
  64. public function testDocRef()
  65. {
  66. $this->skipIf(
  67. defined('HHVM_VERSION'),
  68. 'HHVM does not output doc references'
  69. );
  70. ini_set('docref_root', '');
  71. $this->assertEquals(ini_get('docref_root'), '');
  72. new Debugger();
  73. $this->assertEquals(ini_get('docref_root'), 'https://secure.php.net/');
  74. }
  75. /**
  76. * test Excerpt writing
  77. *
  78. * @return void
  79. */
  80. public function testExcerpt()
  81. {
  82. $result = Debugger::excerpt(__FILE__, __LINE__ - 1, 2);
  83. $this->assertIsArray($result);
  84. $this->assertCount(5, $result);
  85. $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
  86. $result = Debugger::excerpt(__FILE__, 2, 2);
  87. $this->assertIsArray($result);
  88. $this->assertCount(4, $result);
  89. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not highlight php code');
  90. $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
  91. $this->assertRegExp($pattern, $result[0]);
  92. $result = Debugger::excerpt(__FILE__, 11, 2);
  93. $this->assertCount(5, $result);
  94. $pattern = '/<span style\="color\: \#\d{6}">.*?<\/span>/';
  95. $this->assertRegExp($pattern, $result[0]);
  96. $return = Debugger::excerpt('[internal]', 2, 2);
  97. $this->assertEmpty($return);
  98. $result = Debugger::excerpt(__FILE__, __LINE__, 5);
  99. $this->assertCount(11, $result);
  100. $this->assertStringContainsString('Debugger', $result[5]);
  101. $this->assertStringContainsString('excerpt', $result[5]);
  102. $this->assertStringContainsString('__FILE__', $result[5]);
  103. $result = Debugger::excerpt(__FILE__, 1, 2);
  104. $this->assertCount(3, $result);
  105. $lastLine = count(explode("\n", file_get_contents(__FILE__)));
  106. $result = Debugger::excerpt(__FILE__, $lastLine, 2);
  107. $this->assertCount(3, $result);
  108. }
  109. /**
  110. * Test that setOutputFormat works.
  111. *
  112. * @return void
  113. */
  114. public function testSetOutputFormat()
  115. {
  116. Debugger::setOutputFormat('html');
  117. $this->assertSame('html', Debugger::getOutputFormat());
  118. }
  119. /**
  120. * Test that getOutputFormat/setOutputFormat works.
  121. *
  122. * @return void
  123. */
  124. public function testGetSetOutputFormat()
  125. {
  126. Debugger::setOutputFormat('html');
  127. $this->assertSame('html', Debugger::getOutputFormat());
  128. }
  129. /**
  130. * Test that choosing a non-existent format causes an exception
  131. *
  132. * @return void
  133. */
  134. public function testSetOutputAsException()
  135. {
  136. $this->expectException(\InvalidArgumentException::class);
  137. Debugger::setOutputFormat('Invalid junk');
  138. }
  139. /**
  140. * Test outputError with description encoding
  141. *
  142. * @return void
  143. */
  144. public function testOutputErrorDescriptionEncoding()
  145. {
  146. Debugger::setOutputFormat('html');
  147. ob_start();
  148. $debugger = Debugger::getInstance();
  149. $debugger->outputError([
  150. 'error' => 'Notice',
  151. 'code' => E_NOTICE,
  152. 'level' => E_NOTICE,
  153. 'description' => 'Undefined index <script>alert(1)</script>',
  154. 'file' => __FILE__,
  155. 'line' => __LINE__,
  156. ]);
  157. $result = ob_get_clean();
  158. $this->assertStringContainsString('&lt;script&gt;', $result);
  159. $this->assertStringNotContainsString('<script>', $result);
  160. }
  161. /**
  162. * Tests that the correct line is being highlighted.
  163. *
  164. * @return void
  165. */
  166. public function testOutputErrorLineHighlight()
  167. {
  168. Debugger::setOutputFormat('js');
  169. ob_start();
  170. $debugger = Debugger::getInstance();
  171. $data = [
  172. 'level' => E_NOTICE,
  173. 'code' => E_NOTICE,
  174. 'file' => __FILE__,
  175. 'line' => __LINE__,
  176. 'description' => 'Error description',
  177. 'start' => 1,
  178. ];
  179. $debugger->outputError($data);
  180. $result = ob_get_clean();
  181. $this->assertRegExp('#^\<span class\="code\-highlight"\>.*outputError.*\</span\>$#m', $result);
  182. }
  183. /**
  184. * Tests that changes in output formats using Debugger::output() change the templates used.
  185. *
  186. * @return void
  187. */
  188. public function testAddFormat()
  189. {
  190. Debugger::addFormat('js', [
  191. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  192. '&line={:line}">{:path}</a>, line {:line}',
  193. ]);
  194. Debugger::setOutputFormat('js');
  195. $result = Debugger::trace();
  196. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  197. Debugger::addFormat('xml', [
  198. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  199. '{:description}</error>',
  200. ]);
  201. Debugger::setOutputFormat('xml');
  202. ob_start();
  203. $debugger = Debugger::getInstance();
  204. $debugger->outputError([
  205. 'level' => E_NOTICE,
  206. 'code' => E_NOTICE,
  207. 'file' => __FILE__,
  208. 'line' => __LINE__,
  209. 'description' => 'Undefined variable: foo',
  210. ]);
  211. $result = ob_get_clean();
  212. $expected = [
  213. '<error',
  214. '<code', '8', '/code',
  215. '<file', 'preg:/[^<]+/', '/file',
  216. '<line', '' . ((int)__LINE__ - 9), '/line',
  217. 'preg:/Undefined variable:\s+foo/',
  218. '/error',
  219. ];
  220. $this->assertHtml($expected, $result, true);
  221. }
  222. /**
  223. * Test adding a format that is handled by a callback.
  224. *
  225. * @return void
  226. */
  227. public function testAddFormatCallback()
  228. {
  229. Debugger::addFormat('callback', ['callback' => [$this, 'customFormat']]);
  230. Debugger::setOutputFormat('callback');
  231. ob_start();
  232. $debugger = Debugger::getInstance();
  233. $debugger->outputError([
  234. 'error' => 'Notice',
  235. 'code' => E_NOTICE,
  236. 'level' => E_NOTICE,
  237. 'description' => 'Undefined variable $foo',
  238. 'file' => __FILE__,
  239. 'line' => __LINE__,
  240. ]);
  241. $result = ob_get_clean();
  242. $this->assertStringContainsString('Notice: I eated an error', $result);
  243. $this->assertStringContainsString('DebuggerTest.php', $result);
  244. }
  245. /**
  246. * Test method for testing addFormat with callbacks.
  247. *
  248. * @return void
  249. */
  250. public function customFormat($error, $strings)
  251. {
  252. echo $error['error'] . ': I eated an error ' . $error['file'];
  253. }
  254. /**
  255. * testTrimPath method
  256. *
  257. * @return void
  258. */
  259. public function testTrimPath()
  260. {
  261. $this->assertSame('APP/', Debugger::trimPath(APP));
  262. $this->assertSame('CORE' . DS . 'src' . DS, Debugger::trimPath(CAKE));
  263. $this->assertSame('Some/Other/Path', Debugger::trimPath('Some/Other/Path'));
  264. }
  265. /**
  266. * testExportVar method
  267. *
  268. * @return void
  269. */
  270. public function testExportVar()
  271. {
  272. $Controller = new Controller();
  273. $Controller->viewBuilder()->setHelpers(['Html', 'Form']);
  274. $View = $Controller->createView();
  275. $View->int = 2;
  276. $View->float = 1.333;
  277. $View->string = ' ';
  278. $result = Debugger::exportVar($View);
  279. $expected = <<<TEXT
  280. object(Cake\View\View) id:0 {
  281. Html => object(Cake\View\Helper\HtmlHelper) id:1 {}
  282. Form => object(Cake\View\Helper\FormHelper) id:2 {}
  283. int => (int) 2
  284. float => (float) 1.333
  285. string => ' '
  286. [protected] _helpers => object(Cake\View\HelperRegistry) id:3 {}
  287. [protected] Blocks => object(Cake\View\ViewBlock) id:4 {}
  288. [protected] plugin => null
  289. [protected] name => ''
  290. [protected] helpers => [
  291. (int) 0 => 'Html',
  292. (int) 1 => 'Form'
  293. ]
  294. [protected] templatePath => null
  295. [protected] template => null
  296. [protected] layout => 'default'
  297. [protected] layoutPath => ''
  298. [protected] autoLayout => true
  299. [protected] viewVars => []
  300. [protected] _ext => '.php'
  301. [protected] subDir => ''
  302. [protected] theme => null
  303. [protected] request => object(Cake\Http\ServerRequest) id:5 {}
  304. [protected] response => object(Cake\Http\Response) id:6 {}
  305. [protected] elementCache => 'default'
  306. [protected] _passedVars => [
  307. (int) 0 => 'viewVars',
  308. (int) 1 => 'autoLayout',
  309. (int) 2 => 'helpers',
  310. (int) 3 => 'template',
  311. (int) 4 => 'layout',
  312. (int) 5 => 'name',
  313. (int) 6 => 'theme',
  314. (int) 7 => 'layoutPath',
  315. (int) 8 => 'templatePath',
  316. (int) 9 => 'plugin'
  317. ]
  318. [protected] _defaultConfig => []
  319. [protected] _paths => []
  320. [protected] _pathsForPlugin => []
  321. [protected] _parents => []
  322. [protected] _current => null
  323. [protected] _currentType => ''
  324. [protected] _stack => []
  325. [protected] _viewBlockClass => 'Cake\View\ViewBlock'
  326. [protected] _eventManager => object(Cake\Event\EventManager) id:7 {}
  327. [protected] _eventClass => 'Cake\Event\Event'
  328. [protected] _config => []
  329. [protected] _configInitialized => true
  330. }
  331. TEXT;
  332. $this->assertTextEquals($expected, $result);
  333. $data = [
  334. 1 => 'Index one',
  335. 5 => 'Index five',
  336. ];
  337. $result = Debugger::exportVar($data);
  338. $expected = <<<TEXT
  339. [
  340. (int) 1 => 'Index one',
  341. (int) 5 => 'Index five'
  342. ]
  343. TEXT;
  344. $this->assertTextEquals($expected, $result);
  345. $data = [
  346. 'key' => [
  347. 'value',
  348. ],
  349. ];
  350. $result = Debugger::exportVar($data, 1);
  351. $expected = <<<TEXT
  352. [
  353. 'key' => [
  354. '' => [maximum depth reached]
  355. ]
  356. ]
  357. TEXT;
  358. $this->assertTextEquals($expected, $result);
  359. $data = false;
  360. $result = Debugger::exportVar($data);
  361. $expected = <<<TEXT
  362. false
  363. TEXT;
  364. $this->assertTextEquals($expected, $result);
  365. $file = fopen('php://output', 'w');
  366. fclose($file);
  367. $result = Debugger::exportVar($file);
  368. $this->assertTextEquals('(unknown)', $result);
  369. }
  370. /**
  371. * Test exporting various kinds of false.
  372. *
  373. * @return void
  374. */
  375. public function testExportVarZero()
  376. {
  377. $data = [
  378. 'nothing' => '',
  379. 'null' => null,
  380. 'false' => false,
  381. 'szero' => '0',
  382. 'zero' => 0,
  383. ];
  384. $result = Debugger::exportVar($data);
  385. $expected = <<<TEXT
  386. [
  387. 'nothing' => '',
  388. 'null' => null,
  389. 'false' => false,
  390. 'szero' => '0',
  391. 'zero' => (int) 0
  392. ]
  393. TEXT;
  394. $this->assertTextEquals($expected, $result);
  395. }
  396. /**
  397. * test exportVar with cyclic objects.
  398. *
  399. * @return void
  400. */
  401. public function testExportVarCyclicRef()
  402. {
  403. $parent = new stdClass();
  404. $parent->name = 'cake';
  405. $middle = new stdClass();
  406. $parent->child = $middle;
  407. $middle->name = 'php';
  408. $middle->child = $parent;
  409. $result = Debugger::exportVar($parent, 6);
  410. $expected = <<<TEXT
  411. object(stdClass) id:0 {
  412. name => 'cake'
  413. child => object(stdClass) id:1 {
  414. name => 'php'
  415. child => object(stdClass) id:0 {}
  416. }
  417. }
  418. TEXT;
  419. $this->assertTextEquals($expected, $result);
  420. }
  421. /**
  422. * testLog method
  423. *
  424. * @return void
  425. */
  426. public function testLog()
  427. {
  428. Log::setConfig('test', [
  429. 'className' => 'Array',
  430. ]);
  431. Debugger::log('cool');
  432. Debugger::log(['whatever', 'here']);
  433. $messages = Log::engine('test')->read();
  434. $this->assertCount(2, $messages);
  435. $this->assertStringContainsString('DebuggerTest::testLog', $messages[0]);
  436. $this->assertStringContainsString('cool', $messages[0]);
  437. $this->assertStringContainsString('DebuggerTest::testLog', $messages[1]);
  438. $this->assertStringContainsString('[main]', $messages[1]);
  439. $this->assertStringContainsString("'whatever'", $messages[1]);
  440. $this->assertStringContainsString("'here'", $messages[1]);
  441. Log::drop('test');
  442. }
  443. /**
  444. * test log() depth
  445. *
  446. * @return void
  447. */
  448. public function testLogDepth()
  449. {
  450. Log::setConfig('test', [
  451. 'className' => 'Array',
  452. ]);
  453. $val = [
  454. 'test' => ['key' => 'val'],
  455. ];
  456. Debugger::log($val, 'debug', 0);
  457. $messages = Log::engine('test')->read();
  458. $this->assertStringContainsString('DebuggerTest::testLogDepth', $messages[0]);
  459. $this->assertStringContainsString('test', $messages[0]);
  460. $this->assertStringNotContainsString('val', $messages[0]);
  461. }
  462. /**
  463. * testDump method
  464. *
  465. * @return void
  466. */
  467. public function testDump()
  468. {
  469. $var = ['People' => [
  470. [
  471. 'name' => 'joeseph',
  472. 'coat' => 'technicolor',
  473. 'hair_color' => 'brown',
  474. ],
  475. [
  476. 'name' => 'Shaft',
  477. 'coat' => 'black',
  478. 'hair' => 'black',
  479. ],
  480. ]];
  481. ob_start();
  482. Debugger::dump($var);
  483. $result = ob_get_clean();
  484. $open = "\n";
  485. $close = "\n\n";
  486. $expected = <<<TEXT
  487. {$open}[
  488. 'People' => [
  489. (int) 0 => [
  490. 'name' => 'joeseph',
  491. 'coat' => 'technicolor',
  492. 'hair_color' => 'brown'
  493. ],
  494. (int) 1 => [
  495. 'name' => 'Shaft',
  496. 'coat' => 'black',
  497. 'hair' => 'black'
  498. ]
  499. ]
  500. ]{$close}
  501. TEXT;
  502. $this->assertTextEquals($expected, $result);
  503. ob_start();
  504. Debugger::dump($var, 1);
  505. $result = ob_get_clean();
  506. $expected = <<<TEXT
  507. {$open}[
  508. 'People' => [
  509. '' => [maximum depth reached]
  510. ]
  511. ]{$close}
  512. TEXT;
  513. $this->assertTextEquals($expected, $result);
  514. }
  515. /**
  516. * test getInstance.
  517. *
  518. * @return void
  519. */
  520. public function testGetInstance()
  521. {
  522. $result = Debugger::getInstance();
  523. $this->assertInstanceOf(Debugger::class, $result);
  524. $result = Debugger::getInstance(TestDebugger::class);
  525. $this->assertInstanceOf(TestDebugger::class, $result);
  526. $result = Debugger::getInstance();
  527. $this->assertInstanceOf(TestDebugger::class, $result);
  528. $result = Debugger::getInstance(Debugger::class);
  529. $this->assertInstanceOf(Debugger::class, $result);
  530. }
  531. /**
  532. * Test that exportVar() will stop traversing recursive arrays like GLOBALS.
  533. *
  534. * @return void
  535. */
  536. public function testExportVarRecursion()
  537. {
  538. $output = Debugger::exportVar($GLOBALS);
  539. $this->assertRegExp("/'GLOBALS' => \[\s+'' \=\> \[maximum depth reached\]/", $output);
  540. }
  541. /**
  542. * test trace exclude
  543. *
  544. * @return void
  545. */
  546. public function testTraceExclude()
  547. {
  548. $result = Debugger::trace();
  549. $this->assertRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  550. $result = Debugger::trace([
  551. 'exclude' => ['Cake\Test\TestCase\Error\DebuggerTest::testTraceExclude'],
  552. ]);
  553. $this->assertNotRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  554. }
  555. /**
  556. * Tests that __debugInfo is used when available
  557. *
  558. * @return void
  559. */
  560. public function testDebugInfo()
  561. {
  562. $object = new DebuggableThing();
  563. $result = Debugger::exportVar($object, 2);
  564. $expected = <<<eos
  565. object(TestApp\Error\Thing\DebuggableThing) id:0 {
  566. 'foo' => 'bar'
  567. 'inner' => object(TestApp\Error\Thing\DebuggableThing) id:1 {}
  568. }
  569. eos;
  570. $this->assertEquals($expected, $result);
  571. }
  572. /**
  573. * Tests reading the output mask settings.
  574. *
  575. * @return void
  576. */
  577. public function testSetOutputMask()
  578. {
  579. Debugger::setOutputMask(['password' => '[**********]']);
  580. $this->assertEquals(['password' => '[**********]'], Debugger::outputMask());
  581. Debugger::setOutputMask(['serial' => 'XXXXXX']);
  582. $this->assertEquals(['password' => '[**********]', 'serial' => 'XXXXXX'], Debugger::outputMask());
  583. Debugger::setOutputMask([], false);
  584. $this->assertEquals([], Debugger::outputMask());
  585. }
  586. /**
  587. * Tests the masking of an array key.
  588. *
  589. * @return void
  590. */
  591. public function testMaskArray()
  592. {
  593. Debugger::setOutputMask(['password' => '[**********]']);
  594. $result = Debugger::exportVar(['password' => 'pass1234']);
  595. $expected = "['password'=>'[**********]']";
  596. $this->assertEquals($expected, preg_replace('/\s+/', '', $result));
  597. }
  598. /**
  599. * Tests the masking of an array key.
  600. *
  601. * @return void
  602. */
  603. public function testMaskObject()
  604. {
  605. Debugger::setOutputMask(['password' => '[**********]']);
  606. $object = new SecurityThing();
  607. $result = Debugger::exportVar($object);
  608. $expected = "object(TestApp\\Error\\Thing\\SecurityThing)id:0{password=>'[**********]'}";
  609. $this->assertEquals($expected, preg_replace('/\s+/', '', $result));
  610. }
  611. /**
  612. * test testPrintVar()
  613. *
  614. * @return void
  615. */
  616. public function testPrintVar()
  617. {
  618. ob_start();
  619. Debugger::printVar('this-is-a-test', ['file' => __FILE__, 'line' => __LINE__], false);
  620. $result = ob_get_clean();
  621. $expectedText = <<<EXPECTED
  622. %s (line %d)
  623. ########## DEBUG ##########
  624. 'this-is-a-test'
  625. ###########################
  626. EXPECTED;
  627. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  628. $this->assertEquals($expected, $result);
  629. ob_start();
  630. $value = '<div>this-is-a-test</div>';
  631. Debugger::printVar($value, ['file' => __FILE__, 'line' => __LINE__], true);
  632. $result = ob_get_clean();
  633. $expectedHtml = <<<EXPECTED
  634. <div class="cake-debug-output" style="direction:ltr">
  635. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  636. <pre class="cake-debug">
  637. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  638. </pre>
  639. </div>
  640. EXPECTED;
  641. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  642. $this->assertEquals($expected, $result);
  643. ob_start();
  644. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], true);
  645. $result = ob_get_clean();
  646. $expected = <<<EXPECTED
  647. <div class="cake-debug-output" style="direction:ltr">
  648. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  649. <pre class="cake-debug">
  650. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  651. </pre>
  652. </div>
  653. EXPECTED;
  654. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  655. $this->assertEquals($expected, $result);
  656. ob_start();
  657. Debugger::printVar('<div>this-is-a-test</div>', [], true);
  658. $result = ob_get_clean();
  659. $expected = <<<EXPECTED
  660. <div class="cake-debug-output" style="direction:ltr">
  661. <pre class="cake-debug">
  662. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  663. </pre>
  664. </div>
  665. EXPECTED;
  666. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  667. $this->assertEquals($expected, $result);
  668. ob_start();
  669. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__]);
  670. $result = ob_get_clean();
  671. $expectedHtml = <<<EXPECTED
  672. <div class="cake-debug-output" style="direction:ltr">
  673. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  674. <pre class="cake-debug">
  675. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  676. </pre>
  677. </div>
  678. EXPECTED;
  679. $expectedText = <<<EXPECTED
  680. %s (line %d)
  681. ########## DEBUG ##########
  682. '<div>this-is-a-test</div>'
  683. ###########################
  684. EXPECTED;
  685. if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
  686. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  687. } else {
  688. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  689. }
  690. $this->assertEquals($expected, $result);
  691. ob_start();
  692. Debugger::printVar('<div>this-is-a-test</div>');
  693. $result = ob_get_clean();
  694. $expectedHtml = <<<EXPECTED
  695. <div class="cake-debug-output" style="direction:ltr">
  696. <pre class="cake-debug">
  697. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  698. </pre>
  699. </div>
  700. EXPECTED;
  701. $expectedText = <<<EXPECTED
  702. ########## DEBUG ##########
  703. '<div>this-is-a-test</div>'
  704. ###########################
  705. EXPECTED;
  706. if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
  707. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  708. } else {
  709. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  710. }
  711. $this->assertEquals($expected, $result);
  712. ob_start();
  713. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
  714. $result = ob_get_clean();
  715. $expected = <<<EXPECTED
  716. %s (line %d)
  717. ########## DEBUG ##########
  718. '<div>this-is-a-test</div>'
  719. ###########################
  720. EXPECTED;
  721. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  722. $this->assertEquals($expected, $result);
  723. ob_start();
  724. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
  725. $result = ob_get_clean();
  726. $expected = <<<EXPECTED
  727. %s (line %d)
  728. ########## DEBUG ##########
  729. '<div>this-is-a-test</div>'
  730. ###########################
  731. EXPECTED;
  732. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  733. $this->assertEquals($expected, $result);
  734. ob_start();
  735. Debugger::printVar('<div>this-is-a-test</div>', [], false);
  736. $result = ob_get_clean();
  737. $expected = <<<EXPECTED
  738. ########## DEBUG ##########
  739. '<div>this-is-a-test</div>'
  740. ###########################
  741. EXPECTED;
  742. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  743. $this->assertEquals($expected, $result);
  744. ob_start();
  745. Debugger::printVar(false, [], false);
  746. $result = ob_get_clean();
  747. $expected = <<<EXPECTED
  748. ########## DEBUG ##########
  749. false
  750. ###########################
  751. EXPECTED;
  752. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  753. $this->assertEquals($expected, $result);
  754. }
  755. /**
  756. * test formatHtmlMessage
  757. *
  758. * @return void
  759. */
  760. public function testFormatHtmlMessage()
  761. {
  762. $output = Debugger::formatHtmlMessage('Some `code` to `replace`');
  763. $this->assertSame('Some <code>code</code> to <code>replace</code>', $output);
  764. $output = Debugger::formatHtmlMessage("Some `co\nde` to `replace`\nmore");
  765. $this->assertSame("Some <code>co<br />\nde</code> to <code>replace</code><br />\nmore", $output);
  766. $output = Debugger::formatHtmlMessage("Some `code` to <script>alert(\"test\")</script>\nmore");
  767. $this->assertSame(
  768. "Some <code>code</code> to &lt;script&gt;alert(&quot;test&quot;)&lt;/script&gt;<br />\nmore",
  769. $output
  770. );
  771. }
  772. }