2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Test for PMA_formatSql from common.lib.php
6 * @package phpMyAdmin-test
7 * @version $Id: PMA_formatSql_test.php
8 * @group common.lib-tests
14 require_once 'libraries/common.lib.php';
15 require_once 'libraries/sqlparser.lib.php';
17 class PMA_formatSql_test
extends PHPUnit_Framework_TestCase
21 * temporary variable for globals array
26 * temporary variable for session array
28 protected $tmpSession;
31 * storing globals and session
33 public function setUp() {
39 * recovering globals and session
41 public function tearDown() {
46 function testFormatSQLNotArray(){
48 $cfg['SQP']['fmtType'] = 'html';
49 $sql = "SELECT * FROM tTable;";
50 $this->assertEquals("<pre>\n$sql\n</pre>",PMA_formatSql($sql));
53 function testFormatSQLfmTypeHtml_1(){
55 $cfg['SQP']['fmtType'] = 'html';
56 $cfg['MySQLManualType'] = 'viewable';
62 'type' => 'alpha_reservedWord',
69 'type' => 'digit_integer',
75 'type' => 'punct_queryend',
81 $unparsed = "SELECT 1;";
82 $expected = '<span class="syntax"><span class="inner_sql"><span class="syntax_alpha syntax_alpha_reservedWord">SELECT</span></a> <span class="syntax_digit syntax_digit_integer">1</span> <span class="syntax_punct syntax_punct_queryend">;</span><br /><br /></span></span>';
84 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
87 function testFormatSQLfmTypeHtml_2(){
89 $cfg['SQP']['fmtType'] = 'html';
90 $cfg['MySQLManualType'] = 'viewable';
92 $unparsed = "SELECT * from `tTable`;";
97 'type' => 'alpha_reservedWord',
110 'type' => 'alpha_reservedWord',
117 'type' => 'quote_backtick',
118 'data' => '`tTable`',
123 'type' => 'punct_queryend',
129 $expected = '<span class="syntax"><span class="inner_sql"><span class="syntax_alpha syntax_alpha_reservedWord">SELECT</span></a> <span class="syntax_punct">*</span> <br /><span class="syntax_alpha syntax_alpha_reservedWord">FROM</span> <span class="syntax_quote syntax_quote_backtick">`tTable`</span> <span class="syntax_punct syntax_punct_queryend">;</span><br /><br /></span></span>';
131 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
134 function testFormatSQLfmTypeHtml_3(){
136 $cfg['SQP']['fmtType'] = 'html';
137 $cfg['MySQLManualType'] = 'viewable';
139 $unparsed = 'SELECT * FROM `tTable_A` A INNER JOIN `tTable_B` B ON B.ID = A.ID;';
144 'type' => 'alpha_reservedWord',
157 'type' => 'alpha_reservedWord',
164 'type' => 'quote_backtick',
165 'data' => '`tTable_A`',
170 'type' => 'alpha_identifier',
173 'forbidden' => false,
177 'type' => 'alpha_reservedWord',
184 'type' => 'alpha_reservedWord',
191 'type' => 'quote_backtick',
192 'data' => '`tTable_B`',
197 'type' => 'alpha_identifier',
200 'forbidden' => false,
204 'type' => 'alpha_reservedWord',
211 'type' => 'alpha_identifier',
214 'forbidden' => false,
218 'type' => 'punct_qualifier',
224 'type' => 'alpha_identifier',
227 'forbidden' => false,
237 'type' => 'alpha_identifier',
240 'forbidden' => false,
244 'type' => 'punct_qualifier',
250 'type' => 'alpha_identifier',
253 'forbidden' => false,
257 'type' => 'punct_queryend',
264 $expected = '<span class="syntax"><span class="inner_sql"><span class="syntax_alpha syntax_alpha_reservedWord">SELECT</span></a> <span class="syntax_punct">*</span> <br /><span class="syntax_alpha syntax_alpha_reservedWord">FROM</span> <span class="syntax_quote syntax_quote_backtick">`tTable_A`</span> <span class="syntax_alpha syntax_alpha_identifier">A</span><br /><span class="syntax_alpha syntax_alpha_reservedWord">INNER</span> <span class="syntax_alpha syntax_alpha_reservedWord">JOIN</span> <span class="syntax_quote syntax_quote_backtick">`tTable_B`</span> <span class="syntax_alpha syntax_alpha_identifier">B</span> <span class="syntax_alpha syntax_alpha_reservedWord">ON</span> <span class="syntax_alpha syntax_alpha_identifier">B</span><span class="syntax_punct syntax_punct_qualifier">.</span><span class="syntax_alpha syntax_alpha_identifier">ID</span> <span class="syntax_punct">=</span></a> <span class="syntax_alpha syntax_alpha_identifier">A</span><span class="syntax_punct syntax_punct_qualifier">.</span><span class="syntax_alpha syntax_alpha_identifier">ID</span><span class="syntax_punct syntax_punct_queryend">;</span><br /><br /></span></span>';
266 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
269 function testFormatSQLfmTypeText_1(){
271 $cfg['SQP']['fmtType'] = 'text';
272 $cfg['MySQLManualType'] = 'viewable';
275 'raw' => 'SELECT 1;',
278 'type' => 'alpha_reservedWord',
285 'type' => 'digit_integer',
291 'type' => 'punct_queryend',
297 $unparsed = "SELECT 1;";
298 $expected = '<span class="inner_sql">SELECT</a> 1 ;<br /><br /></span>';
300 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
303 function testFormatSQLfmTypeText_2(){
305 $cfg['SQP']['fmtType'] = 'text';
306 $cfg['MySQLManualType'] = 'viewable';
308 $unparsed = "SELECT * from `tTable`;";
313 'type' => 'alpha_reservedWord',
326 'type' => 'alpha_reservedWord',
333 'type' => 'quote_backtick',
334 'data' => '`tTable`',
339 'type' => 'punct_queryend',
345 $expected = '<span class="inner_sql">SELECT</a> * <br />FROM `tTable` ;<br /><br /></span>';
347 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
350 function testFormatSQLfmTypeText_3(){
352 $cfg['SQP']['fmtType'] = 'text';
353 $cfg['MySQLManualType'] = 'viewable';
355 $unparsed = 'SELECT * FROM `tTable_A` A INNER JOIN `tTable_B` B ON B.ID = A.ID;';
360 'type' => 'alpha_reservedWord',
373 'type' => 'alpha_reservedWord',
380 'type' => 'quote_backtick',
381 'data' => '`tTable_A`',
386 'type' => 'alpha_identifier',
389 'forbidden' => false,
393 'type' => 'alpha_reservedWord',
400 'type' => 'alpha_reservedWord',
407 'type' => 'quote_backtick',
408 'data' => '`tTable_B`',
413 'type' => 'alpha_identifier',
416 'forbidden' => false,
420 'type' => 'alpha_reservedWord',
427 'type' => 'alpha_identifier',
430 'forbidden' => false,
434 'type' => 'punct_qualifier',
440 'type' => 'alpha_identifier',
443 'forbidden' => false,
453 'type' => 'alpha_identifier',
456 'forbidden' => false,
460 'type' => 'punct_qualifier',
466 'type' => 'alpha_identifier',
469 'forbidden' => false,
473 'type' => 'punct_queryend',
479 $expected = '<span class="inner_sql">SELECT</a> * <br />FROM `tTable_A` A<br />INNER JOIN `tTable_B` B ON B.ID =</a> A.ID;<br /><br /></span>';
481 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
484 function testFormatSQLfmTypeNone_1(){
486 $cfg['SQP']['fmtType'] = 'none';
487 $cfg['MySQLManualType'] = 'viewable';
490 'raw' => 'SELECT 1;',
493 'type' => 'alpha_reservedWord',
500 'type' => 'digit_integer',
506 'type' => 'punct_queryend',
512 $unparsed = "SELECT 1;";
514 $expected = "<span class=\"inner_sql\"><pre>\nSELECT 1;\n</pre></span>";
515 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
517 $expected = "SELECT 1;";
518 $this->assertEquals($expected,PMA_formatSql($sql));
521 function testFormatSQLfmTypeNone_2(){
523 $cfg['SQP']['fmtType'] = 'none';
524 $cfg['MySQLManualType'] = 'viewable';
526 $unparsed = "SELECT * from `tTable`;";
531 'type' => 'alpha_reservedWord',
544 'type' => 'alpha_reservedWord',
551 'type' => 'quote_backtick',
552 'data' => '`tTable`',
557 'type' => 'punct_queryend',
564 $expected = "<span class=\"inner_sql\"><pre>\nSELECT * from `tTable`;\n</pre></span>";
565 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
567 $expected = "SELECT * from `tTable`;";
568 $this->assertEquals($expected,PMA_formatSql($sql));
571 function testFormatSQLfmTypeNone_3(){
573 $cfg['SQP']['fmtType'] = 'none';
574 $cfg['MySQLManualType'] = 'viewable';
576 $unparsed = 'SELECT * FROM `tTable_A` A INNER JOIN `tTable_B` B ON B.ID = A.ID;';
581 'type' => 'alpha_reservedWord',
594 'type' => 'alpha_reservedWord',
601 'type' => 'quote_backtick',
602 'data' => '`tTable_A`',
607 'type' => 'alpha_identifier',
610 'forbidden' => false,
614 'type' => 'alpha_reservedWord',
621 'type' => 'alpha_reservedWord',
628 'type' => 'quote_backtick',
629 'data' => '`tTable_B`',
634 'type' => 'alpha_identifier',
637 'forbidden' => false,
641 'type' => 'alpha_reservedWord',
648 'type' => 'alpha_identifier',
651 'forbidden' => false,
655 'type' => 'punct_qualifier',
661 'type' => 'alpha_identifier',
664 'forbidden' => false,
674 'type' => 'alpha_identifier',
677 'forbidden' => false,
681 'type' => 'punct_qualifier',
687 'type' => 'alpha_identifier',
690 'forbidden' => false,
694 'type' => 'punct_queryend',
701 $expected = "<span class=\"inner_sql\"><pre>\nSELECT * FROM `tTable_A` A INNER JOIN `tTable_B` B ON B.ID = A.ID;\n</pre></span>";
702 $this->assertEquals($expected,PMA_formatSql($sql, $unparsed));
704 $expected = 'SELECT * FROM `tTable_A` A INNER JOIN `tTable_B` B ON B.ID = A.ID;';
705 $this->assertEquals($expected,PMA_formatSql($sql));
708 function testFormatSQLWithoutType(){
710 $cfg['SQP']['fmtType'] = '';
711 $cfg['MySQLManualType'] = 'viewable';
713 'raw' => 'SELECT 1;',
716 'type' => 'alpha_reservedWord',
723 'type' => 'digit_integer',
729 'type' => 'punct_queryend',
735 $this->assertEmpty(PMA_formatSql($sql));
738 function testFormatSQLError(){
739 global $SQP_errorString;
740 $SQP_errorString = true;
741 $sql = array("raw" => "& \" < >");
742 $this->assertEquals("& " < >",PMA_formatSql($sql));
743 $SQP_errorString = false;