DebuggerTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 TestApp\Error\TestDebugger;
  23. use TestApp\Error\Thing\DebuggableThing;
  24. use TestApp\Error\Thing\SecurityThing;
  25. /**
  26. * DebuggerTest class
  27. *
  28. * !!! Be careful with changing code below as it may
  29. * !!! change line numbers which are used in the tests
  30. */
  31. class DebuggerTest extends TestCase
  32. {
  33. protected $_restoreError = false;
  34. /**
  35. * setUp method
  36. *
  37. * @return void
  38. */
  39. public function setUp(): void
  40. {
  41. parent::setUp();
  42. Configure::write('debug', true);
  43. Log::drop('stderr');
  44. Log::drop('stdout');
  45. }
  46. /**
  47. * tearDown method
  48. *
  49. * @return void
  50. */
  51. public function tearDown(): void
  52. {
  53. parent::tearDown();
  54. if ($this->_restoreError) {
  55. restore_error_handler();
  56. }
  57. }
  58. /**
  59. * testDocRef method
  60. *
  61. * @return void
  62. */
  63. public function testDocRef()
  64. {
  65. $this->skipIf(
  66. defined('HHVM_VERSION'),
  67. 'HHVM does not output doc references'
  68. );
  69. ini_set('docref_root', '');
  70. $this->assertEquals(ini_get('docref_root'), '');
  71. new Debugger();
  72. $this->assertEquals(ini_get('docref_root'), 'https://secure.php.net/');
  73. }
  74. /**
  75. * test Excerpt writing
  76. *
  77. * @return void
  78. */
  79. public function testExcerpt()
  80. {
  81. $result = Debugger::excerpt(__FILE__, __LINE__ - 1, 2);
  82. $this->assertIsArray($result);
  83. $this->assertCount(5, $result);
  84. $this->assertRegExp('/function(.+)testExcerpt/', $result[1]);
  85. $result = Debugger::excerpt(__FILE__, 2, 2);
  86. $this->assertIsArray($result);
  87. $this->assertCount(4, $result);
  88. $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not highlight php code');
  89. $pattern = '/<code>.*?<span style\="color\: \#\d+">.*?&lt;\?php/';
  90. $this->assertRegExp($pattern, $result[0]);
  91. $result = Debugger::excerpt(__FILE__, 11, 2);
  92. $this->assertCount(5, $result);
  93. $pattern = '/<span style\="color\: \#\d{6}">.*?<\/span>/';
  94. $this->assertRegExp($pattern, $result[0]);
  95. $return = Debugger::excerpt('[internal]', 2, 2);
  96. $this->assertEmpty($return);
  97. $result = Debugger::excerpt(__FILE__, __LINE__, 5);
  98. $this->assertCount(11, $result);
  99. $this->assertStringContainsString('Debugger', $result[5]);
  100. $this->assertStringContainsString('excerpt', $result[5]);
  101. $this->assertStringContainsString('__FILE__', $result[5]);
  102. $result = Debugger::excerpt(__FILE__, 1, 2);
  103. $this->assertCount(3, $result);
  104. $lastLine = count(explode("\n", file_get_contents(__FILE__)));
  105. $result = Debugger::excerpt(__FILE__, $lastLine, 2);
  106. $this->assertCount(3, $result);
  107. }
  108. /**
  109. * Test that setOutputFormat works.
  110. *
  111. * @return void
  112. */
  113. public function testSetOutputFormat()
  114. {
  115. Debugger::setOutputFormat('html');
  116. $this->assertSame('html', Debugger::getOutputFormat());
  117. }
  118. /**
  119. * Test that getOutputFormat/setOutputFormat works.
  120. *
  121. * @return void
  122. */
  123. public function testGetSetOutputFormat()
  124. {
  125. Debugger::setOutputFormat('html');
  126. $this->assertSame('html', Debugger::getOutputFormat());
  127. }
  128. /**
  129. * Test that choosing a non-existent format causes an exception
  130. *
  131. * @return void
  132. */
  133. public function testSetOutputAsException()
  134. {
  135. $this->expectException(\InvalidArgumentException::class);
  136. Debugger::setOutputFormat('Invalid junk');
  137. }
  138. /**
  139. * Test outputError with description encoding
  140. *
  141. * @return void
  142. */
  143. public function testOutputErrorDescriptionEncoding()
  144. {
  145. Debugger::setOutputFormat('html');
  146. ob_start();
  147. $debugger = Debugger::getInstance();
  148. $debugger->outputError([
  149. 'error' => 'Notice',
  150. 'code' => E_NOTICE,
  151. 'level' => E_NOTICE,
  152. 'description' => 'Undefined index <script>alert(1)</script>',
  153. 'file' => __FILE__,
  154. 'line' => __LINE__,
  155. ]);
  156. $result = ob_get_clean();
  157. $this->assertStringContainsString('&lt;script&gt;', $result);
  158. $this->assertStringNotContainsString('<script>', $result);
  159. }
  160. /**
  161. * Tests that the correct line is being highlighted.
  162. *
  163. * @return void
  164. */
  165. public function testOutputErrorLineHighlight()
  166. {
  167. Debugger::setOutputFormat('js');
  168. ob_start();
  169. $debugger = Debugger::getInstance();
  170. $data = [
  171. 'level' => E_NOTICE,
  172. 'code' => E_NOTICE,
  173. 'file' => __FILE__,
  174. 'line' => __LINE__,
  175. 'description' => 'Error description',
  176. 'start' => 1,
  177. ];
  178. $debugger->outputError($data);
  179. $result = ob_get_clean();
  180. $this->assertRegExp('#^\<span class\="code\-highlight"\>.*outputError.*\</span\>$#m', $result);
  181. }
  182. /**
  183. * Tests that changes in output formats using Debugger::output() change the templates used.
  184. *
  185. * @return void
  186. */
  187. public function testAddFormat()
  188. {
  189. Debugger::addFormat('js', [
  190. 'traceLine' => '{:reference} - <a href="txmt://open?url=file://{:file}' .
  191. '&line={:line}">{:path}</a>, line {:line}',
  192. ]);
  193. Debugger::setOutputFormat('js');
  194. $result = Debugger::trace();
  195. $this->assertRegExp('/' . preg_quote('txmt://open?url=file://', '/') . '(\/|[A-Z]:\\\\)' . '/', $result);
  196. Debugger::addFormat('xml', [
  197. 'error' => '<error><code>{:code}</code><file>{:file}</file><line>{:line}</line>' .
  198. '{:description}</error>',
  199. ]);
  200. Debugger::setOutputFormat('xml');
  201. ob_start();
  202. $debugger = Debugger::getInstance();
  203. $debugger->outputError([
  204. 'level' => E_NOTICE,
  205. 'code' => E_NOTICE,
  206. 'file' => __FILE__,
  207. 'line' => __LINE__,
  208. 'description' => 'Undefined variable: foo',
  209. ]);
  210. $result = ob_get_clean();
  211. $expected = [
  212. '<error',
  213. '<code', '8', '/code',
  214. '<file', 'preg:/[^<]+/', '/file',
  215. '<line', '' . ((int)__LINE__ - 9), '/line',
  216. 'preg:/Undefined variable:\s+foo/',
  217. '/error',
  218. ];
  219. $this->assertHtml($expected, $result, true);
  220. }
  221. /**
  222. * Test adding a format that is handled by a callback.
  223. *
  224. * @return void
  225. */
  226. public function testAddFormatCallback()
  227. {
  228. Debugger::addFormat('callback', ['callback' => [$this, 'customFormat']]);
  229. Debugger::setOutputFormat('callback');
  230. ob_start();
  231. $debugger = Debugger::getInstance();
  232. $debugger->outputError([
  233. 'error' => 'Notice',
  234. 'code' => E_NOTICE,
  235. 'level' => E_NOTICE,
  236. 'description' => 'Undefined variable $foo',
  237. 'file' => __FILE__,
  238. 'line' => __LINE__,
  239. ]);
  240. $result = ob_get_clean();
  241. $this->assertStringContainsString('Notice: I eated an error', $result);
  242. $this->assertStringContainsString('DebuggerTest.php', $result);
  243. Debugger::setOutputFormat('js');
  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) {
  281. Html => object(Cake\View\Helper\HtmlHelper) {}
  282. Form => object(Cake\View\Helper\FormHelper) {}
  283. int => (int) 2
  284. float => (float) 1.333
  285. string => ' '
  286. [protected] _helpers => object(Cake\View\HelperRegistry) {}
  287. [protected] Blocks => object(Cake\View\ViewBlock) {}
  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) {}
  304. [protected] response => object(Cake\Http\Response) {}
  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) {}
  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. * testLog method
  398. *
  399. * @return void
  400. */
  401. public function testLog()
  402. {
  403. Log::setConfig('test', [
  404. 'className' => 'Array',
  405. ]);
  406. Debugger::log('cool');
  407. Debugger::log(['whatever', 'here']);
  408. $messages = Log::engine('test')->read();
  409. $this->assertCount(2, $messages);
  410. $this->assertStringContainsString('DebuggerTest::testLog', $messages[0]);
  411. $this->assertStringContainsString('cool', $messages[0]);
  412. $this->assertStringContainsString('DebuggerTest::testLog', $messages[1]);
  413. $this->assertStringContainsString('[main]', $messages[1]);
  414. $this->assertStringContainsString("'whatever'", $messages[1]);
  415. $this->assertStringContainsString("'here'", $messages[1]);
  416. Log::drop('test');
  417. }
  418. /**
  419. * test log() depth
  420. *
  421. * @return void
  422. */
  423. public function testLogDepth()
  424. {
  425. Log::setConfig('test', [
  426. 'className' => 'Array',
  427. ]);
  428. $val = [
  429. 'test' => ['key' => 'val'],
  430. ];
  431. Debugger::log($val, 'debug', 0);
  432. $messages = Log::engine('test')->read();
  433. $this->assertStringContainsString('DebuggerTest::testLogDepth', $messages[0]);
  434. $this->assertStringContainsString('test', $messages[0]);
  435. $this->assertStringNotContainsString('val', $messages[0]);
  436. }
  437. /**
  438. * testDump method
  439. *
  440. * @return void
  441. */
  442. public function testDump()
  443. {
  444. $var = ['People' => [
  445. [
  446. 'name' => 'joeseph',
  447. 'coat' => 'technicolor',
  448. 'hair_color' => 'brown',
  449. ],
  450. [
  451. 'name' => 'Shaft',
  452. 'coat' => 'black',
  453. 'hair' => 'black',
  454. ],
  455. ]];
  456. ob_start();
  457. Debugger::dump($var);
  458. $result = ob_get_clean();
  459. $open = "\n";
  460. $close = "\n\n";
  461. $expected = <<<TEXT
  462. {$open}[
  463. 'People' => [
  464. (int) 0 => [
  465. 'name' => 'joeseph',
  466. 'coat' => 'technicolor',
  467. 'hair_color' => 'brown'
  468. ],
  469. (int) 1 => [
  470. 'name' => 'Shaft',
  471. 'coat' => 'black',
  472. 'hair' => 'black'
  473. ]
  474. ]
  475. ]{$close}
  476. TEXT;
  477. $this->assertTextEquals($expected, $result);
  478. ob_start();
  479. Debugger::dump($var, 1);
  480. $result = ob_get_clean();
  481. $expected = <<<TEXT
  482. {$open}[
  483. 'People' => [
  484. [maximum depth reached]
  485. ]
  486. ]{$close}
  487. TEXT;
  488. $this->assertTextEquals($expected, $result);
  489. }
  490. /**
  491. * test getInstance.
  492. *
  493. * @return void
  494. */
  495. public function testGetInstance()
  496. {
  497. $result = Debugger::getInstance();
  498. $this->assertInstanceOf(Debugger::class, $result);
  499. $result = Debugger::getInstance(TestDebugger::class);
  500. $this->assertInstanceOf(TestDebugger::class, $result);
  501. $result = Debugger::getInstance();
  502. $this->assertInstanceOf(TestDebugger::class, $result);
  503. $result = Debugger::getInstance(Debugger::class);
  504. $this->assertInstanceOf(Debugger::class, $result);
  505. }
  506. /**
  507. * Test that exportVar() doesn't loop through recursive structures.
  508. *
  509. * @return void
  510. */
  511. public function testExportVarRecursion()
  512. {
  513. $output = Debugger::exportVar($GLOBALS);
  514. $this->assertStringContainsString("'GLOBALS' => [recursion]", $output);
  515. }
  516. /**
  517. * test trace exclude
  518. *
  519. * @return void
  520. */
  521. public function testTraceExclude()
  522. {
  523. $result = Debugger::trace();
  524. $this->assertRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  525. $result = Debugger::trace([
  526. 'exclude' => ['Cake\Test\TestCase\Error\DebuggerTest::testTraceExclude'],
  527. ]);
  528. $this->assertNotRegExp('/^Cake\\\Test\\\TestCase\\\Error\\\DebuggerTest::testTraceExclude/', $result);
  529. }
  530. /**
  531. * Tests that __debugInfo is used when available
  532. *
  533. * @return void
  534. */
  535. public function testDebugInfo()
  536. {
  537. $object = new DebuggableThing();
  538. $result = Debugger::exportVar($object, 2);
  539. $expected = <<<eos
  540. object(TestApp\Error\Thing\DebuggableThing) {
  541. 'foo' => 'bar',
  542. 'inner' => object(TestApp\Error\Thing\DebuggableThing) {}
  543. }
  544. eos;
  545. $this->assertEquals($expected, $result);
  546. }
  547. /**
  548. * Tests reading the output mask settings.
  549. *
  550. * @return void
  551. */
  552. public function testSetOutputMask()
  553. {
  554. Debugger::setOutputMask(['password' => '[**********]']);
  555. $this->assertEquals(['password' => '[**********]'], Debugger::outputMask());
  556. Debugger::setOutputMask(['serial' => 'XXXXXX']);
  557. $this->assertEquals(['password' => '[**********]', 'serial' => 'XXXXXX'], Debugger::outputMask());
  558. Debugger::setOutputMask([], false);
  559. $this->assertEquals([], Debugger::outputMask());
  560. }
  561. /**
  562. * Tests the masking of an array key.
  563. *
  564. * @return void
  565. */
  566. public function testMaskArray()
  567. {
  568. Debugger::setOutputMask(['password' => '[**********]']);
  569. $result = Debugger::exportVar(['password' => 'pass1234']);
  570. $expected = "['password'=>[**********]]";
  571. $this->assertEquals($expected, preg_replace('/\s+/', '', $result));
  572. }
  573. /**
  574. * Tests the masking of an array key.
  575. *
  576. * @return void
  577. */
  578. public function testMaskObject()
  579. {
  580. Debugger::setOutputMask(['password' => '[**********]']);
  581. $object = new SecurityThing();
  582. $result = Debugger::exportVar($object);
  583. $expected = 'object(TestApp\\Error\\Thing\\SecurityThing){password=>[**********]}';
  584. $this->assertEquals($expected, preg_replace('/\s+/', '', $result));
  585. }
  586. /**
  587. * test testPrintVar()
  588. *
  589. * @return void
  590. */
  591. public function testPrintVar()
  592. {
  593. ob_start();
  594. Debugger::printVar('this-is-a-test', ['file' => __FILE__, 'line' => __LINE__], false);
  595. $result = ob_get_clean();
  596. $expectedText = <<<EXPECTED
  597. %s (line %d)
  598. ########## DEBUG ##########
  599. 'this-is-a-test'
  600. ###########################
  601. EXPECTED;
  602. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  603. $this->assertEquals($expected, $result);
  604. ob_start();
  605. $value = '<div>this-is-a-test</div>';
  606. Debugger::printVar($value, ['file' => __FILE__, 'line' => __LINE__], true);
  607. $result = ob_get_clean();
  608. $expectedHtml = <<<EXPECTED
  609. <div class="cake-debug-output" style="direction:ltr">
  610. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  611. <pre class="cake-debug">
  612. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  613. </pre>
  614. </div>
  615. EXPECTED;
  616. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  617. $this->assertEquals($expected, $result);
  618. ob_start();
  619. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], true);
  620. $result = ob_get_clean();
  621. $expected = <<<EXPECTED
  622. <div class="cake-debug-output" style="direction:ltr">
  623. <span><strong>%s</strong> (line <strong>%d</strong>)</span>
  624. <pre class="cake-debug">
  625. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  626. </pre>
  627. </div>
  628. EXPECTED;
  629. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
  630. $this->assertEquals($expected, $result);
  631. ob_start();
  632. Debugger::printVar('<div>this-is-a-test</div>', [], true);
  633. $result = ob_get_clean();
  634. $expected = <<<EXPECTED
  635. <div class="cake-debug-output" style="direction:ltr">
  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($expected, 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__]);
  645. $result = ob_get_clean();
  646. $expectedHtml = <<<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. $expectedText = <<<EXPECTED
  655. %s (line %d)
  656. ########## DEBUG ##########
  657. '<div>this-is-a-test</div>'
  658. ###########################
  659. EXPECTED;
  660. if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
  661. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  662. } else {
  663. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  664. }
  665. $this->assertEquals($expected, $result);
  666. ob_start();
  667. Debugger::printVar('<div>this-is-a-test</div>');
  668. $result = ob_get_clean();
  669. $expectedHtml = <<<EXPECTED
  670. <div class="cake-debug-output" style="direction:ltr">
  671. <pre class="cake-debug">
  672. &#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
  673. </pre>
  674. </div>
  675. EXPECTED;
  676. $expectedText = <<<EXPECTED
  677. ########## DEBUG ##########
  678. '<div>this-is-a-test</div>'
  679. ###########################
  680. EXPECTED;
  681. if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
  682. $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
  683. } else {
  684. $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
  685. }
  686. $this->assertEquals($expected, $result);
  687. ob_start();
  688. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
  689. $result = ob_get_clean();
  690. $expected = <<<EXPECTED
  691. %s (line %d)
  692. ########## DEBUG ##########
  693. '<div>this-is-a-test</div>'
  694. ###########################
  695. EXPECTED;
  696. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  697. $this->assertEquals($expected, $result);
  698. ob_start();
  699. Debugger::printVar('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
  700. $result = ob_get_clean();
  701. $expected = <<<EXPECTED
  702. %s (line %d)
  703. ########## DEBUG ##########
  704. '<div>this-is-a-test</div>'
  705. ###########################
  706. EXPECTED;
  707. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  708. $this->assertEquals($expected, $result);
  709. ob_start();
  710. Debugger::printVar('<div>this-is-a-test</div>', [], false);
  711. $result = ob_get_clean();
  712. $expected = <<<EXPECTED
  713. ########## DEBUG ##########
  714. '<div>this-is-a-test</div>'
  715. ###########################
  716. EXPECTED;
  717. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  718. $this->assertEquals($expected, $result);
  719. ob_start();
  720. Debugger::printVar(false, [], false);
  721. $result = ob_get_clean();
  722. $expected = <<<EXPECTED
  723. ########## DEBUG ##########
  724. false
  725. ###########################
  726. EXPECTED;
  727. $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
  728. $this->assertEquals($expected, $result);
  729. }
  730. /**
  731. * test formatHtmlMessage
  732. *
  733. * @return void
  734. */
  735. public function testFormatHtmlMessage()
  736. {
  737. $output = Debugger::formatHtmlMessage('Some `code` to `replace`');
  738. $this->assertSame('Some <code>code</code> to <code>replace</code>', $output);
  739. $output = Debugger::formatHtmlMessage("Some `co\nde` to `replace`\nmore");
  740. $this->assertSame("Some <code>co<br />\nde</code> to <code>replace</code><br />\nmore", $output);
  741. $output = Debugger::formatHtmlMessage("Some `code` to <script>alert(\"test\")</script>\nmore");
  742. $this->assertSame(
  743. "Some <code>code</code> to &lt;script&gt;alert(&quot;test&quot;)&lt;/script&gt;<br />\nmore",
  744. $output
  745. );
  746. }
  747. }