From 33f4871c9c943c7743247c083b93689651825463 Mon Sep 17 00:00:00 2001 From: ayushchd Date: Wed, 21 Aug 2013 02:21:28 +0530 Subject: [PATCH 1/2] Undefined variable and testuite fixes --- libraries/insert_edit.lib.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/insert_edit.lib.php b/libraries/insert_edit.lib.php index a2226ea4c4..d56d9e52c0 100644 --- a/libraries/insert_edit.lib.php +++ b/libraries/insert_edit.lib.php @@ -1552,9 +1552,9 @@ function PMA_getSumbitAndResetButtonForActionsPanel($tabindex, $tabindex_for_val . '' . '' . '' + . ' tabindex="' . ($tabindex + $tabindex_for_value + 6) . '" id="buttonYes" />' . '' + . ' tabindex="' . ($tabindex + $tabindex_for_value + 7) . '" />' . ''; } @@ -1613,6 +1613,7 @@ function PMA_getSpecialCharsAndBackupFieldForExistingRow( $real_null_value, $gis_data_types, $column_name_appendix ) { $special_chars_encoded = ''; + $data = null; // (we are editing) if (is_null($current_row[$column['Field']])) { $real_null_value = true; @@ -1775,8 +1776,10 @@ function PMA_isInsertRow() $header = $response->getHeader(); $scripts = $header->getScripts(); $scripts->addFile('tbl_change.js'); - include 'tbl_change.php'; - exit; + if (!defined('TESTSUITE')) { + include 'tbl_change.php'; + exit; + } } } From fd97b69c22e0507210ed8e99be52685560967097 Mon Sep 17 00:00:00 2001 From: ayushchd Date: Wed, 21 Aug 2013 02:21:59 +0530 Subject: [PATCH 2/2] Tests for libraries/insert_edit.lib.php --- test/libraries/PMA_insert_edit_test.php | 2731 +++++++++++++++++++++++ 1 file changed, 2731 insertions(+) create mode 100644 test/libraries/PMA_insert_edit_test.php diff --git a/test/libraries/PMA_insert_edit_test.php b/test/libraries/PMA_insert_edit_test.php new file mode 100644 index 0000000000..97c20e7991 --- /dev/null +++ b/test/libraries/PMA_insert_edit_test.php @@ -0,0 +1,2731 @@ + 'bar ', '1' => ' test'); + $_REQUEST['clause_is_unique'] = false; + $_REQUEST['sql_query'] = 'SELECT a'; + $GLOBALS['goto'] = 'index.php'; + + $result = PMA_getFormParametersForInsertForm( + 'dbname', 'tablename', false, $where_clause, 'localhost' + ); + + $this->assertEquals( + array( + 'db' => 'dbname', + 'table' => 'tablename', + 'goto' => 'index.php', + 'err_url' => 'localhost', + 'sql_query' => 'SELECT a', + 'where_clause[foo]' => 'bar', + 'where_clause[1]' => 'test', + 'clause_is_unique' => false + ), + $result + ); + } + + /** + * Test for PMA_getWhereClauseArray + * + * @return void + */ + public function testGetWhereClauseArray() + { + $this->assertNull( + PMA_getWhereClauseArray(null) + ); + + $this->assertEquals( + array(1, 2, 3), + PMA_getWhereClauseArray(array(1, 2, 3)) + ); + + $this->assertEquals( + array('clause'), + PMA_getWhereClauseArray('clause') + ); + } + + /** + * Test for PMA_analyzeWhereClauses + * + * @return void + */ + public function testAnalyzeWhereClause() + { + $clauses = array('a=1', 'b="fo\o"'); + + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + + $dbi->expects($this->at(0)) + ->method('query') + ->with( + 'SELECT * FROM `db`.`table` WHERE a=1;', + null, + PMA_DatabaseInterface::QUERY_STORE + ) + ->will($this->returnValue('result1')); + + $dbi->expects($this->at(3)) + ->method('query') + ->with( + 'SELECT * FROM `db`.`table` WHERE b="fo\o";', + null, + PMA_DatabaseInterface::QUERY_STORE + ) + ->will($this->returnValue('result2')); + + $dbi->expects($this->at(1)) + ->method('fetchAssoc') + ->with('result1') + ->will($this->returnValue(array('assoc1'))); + + $dbi->expects($this->at(4)) + ->method('fetchAssoc') + ->with('result2') + ->will($this->returnValue(array('assoc2'))); + + $GLOBALS['dbi'] = $dbi; + $result = PMA_analyzeWhereClauses($clauses, 'table', 'db'); + + $this->assertEquals( + array( + array('a=1', 'b="fo\\\\o"'), + array('result1', 'result2'), + array( + array('assoc1'), + array('assoc2') + ), + '' + ), + $result + ); + } + + /** + * Test for PMA_showEmptyResultMessageOrSetUniqueCondition + * + * @return void + */ + public function testShowEmptyResultMessageOrSetUniqueCondition() + { + $temp = new stdClass; + $temp->orgname = 'orgname'; + $temp->table = 'table'; + $temp->type = 'real'; + $temp->primary_key = 1; + $meta_arr = array($temp); + + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + + $dbi->expects($this->at(0)) + ->method('getFieldsMeta') + ->with('result1') + ->will($this->returnValue($meta_arr)); + + $GLOBALS['dbi'] = $dbi; + + $result = PMA_showEmptyResultMessageOrSetUniqueCondition( + array('1' => array('1' => 1)), 1, array(), + 'SELECT', array('1' => 'result1') + ); + + $this->assertTrue($result); + + // case 2 + $GLOBALS['cfg']['ShowSQL'] = false; + + $responseMock = $this->getMockBuilder('PMA_Response') + ->disableOriginalConstructor() + ->setMethods(array('addHtml')) + ->getMock(); + + $response = new ReflectionProperty('PMA_Response', '_instance'); + $response->setAccessible(true); + $response->setValue(null, $responseMock); + + $result = PMA_showEmptyResultMessageOrSetUniqueCondition( + array(false), 0, array('1'), 'SELECT', array('1' => 'result1') + ); + + $this->assertFalse($result); + } + + /** + * Test for PMA_loadFirstRow + * + * @return void + */ + public function testLoadFirstRow() + { + $GLOBALS['cfg']['InsertRows'] = 2; + + $dbi = $this->getMockBuilder('PMA_DatabaseInterface') + ->disableOriginalConstructor() + ->getMock(); + + $dbi->expects($this->at(0)) + ->method('query') + ->with( + 'SELECT * FROM `db`.`table` LIMIT 1;', + null, + PMA_DatabaseInterface::QUERY_STORE + ) + ->will($this->returnValue('result1')); + + $GLOBALS['dbi'] = $dbi; + + $result = PMA_loadFirstRow('table', 'db'); + + $this->assertEquals( + array('result1', array(false, false)), + $result + ); + } + + /** + * Test for PMA_urlParamsInEditMode + * + * @return void + */ + public function testUrlParamsInEditMode() + { + $where_clause_array = array('foo=1', 'bar=2'); + $_REQUEST['sql_query'] = 'SELECT 1'; + + $result = PMA_urlParamsInEditMode(array(1), $where_clause_array, true); + + $this->assertEquals( + array( + '0' => 1, + 'where_clause' => 'bar=2', + 'sql_query' => 'SELECT 1' + ), + $result + ); + } + + /** + * Test for PMA_showFunctionFieldsInEditMode + * + * @return void + */ + public function testShowFunctionFieldsInEditMode() + { + $GLOBALS['cfg']['ShowFieldTypesInDataEditView'] = true; + $GLOBALS['cfg']['ServerDefault'] = 1; + $url_params = array('ShowFunctionFields' => 2); + + $result = PMA_showFunctionFieldsInEditMode($url_params, false); + + $this->assertEquals( + ' : ' + . 'Function' . "\n", + $result + ); + + // case 2 + $result = PMA_showFunctionFieldsInEditMode($url_params, true); + + $this->assertEquals( + 'Function' . "\n", + $result + ); + } + + /** + * Test for PMA_showColumnTypesInDataEditView + * + * @return void + */ + public function testShowColumnTypesInDataEditView() + { + $GLOBALS['cfg']['ShowFieldTypesInDataEditView'] = true; + $GLOBALS['cfg']['ShowFunctionFields'] = true; + $GLOBALS['cfg']['ServerDefault'] = 1; + $url_params = array('ShowFunctionFields' => 2); + + $result = PMA_showColumnTypesInDataEditView($url_params, false); + + $this->assertEquals( + ' : ' + . 'Type' . "\n", + $result + ); + + // case 2 + $result = PMA_showColumnTypesInDataEditView($url_params, true); + + $this->assertEquals( + 'Type' . "\n", + $result + ); + } + + /** + * Test for PMA_getDefaultForDatetime + * + * @return void + */ + public function testGetDefaultForDatetime() + { + $column['Type'] = 'datetime'; + $column['Null'] = 'YES'; + + $this->assertNull( + PMA_getDefaultForDatetime($column) //should be passed as reference? + ); + } + + /** + * Test for PMA_analyzeTableColumnsArray + * + * @return void + */ + public function testAnalyzeTableColumnsArray() + { + $column = array( + 'Field' => '1<2', + 'Field_md5' => 'pswd', + 'Type' => 'float(10, 1)' + ); + + $result = PMA_analyzeTableColumnsArray( + $column, array(), false + ); + + $this->assertEquals( + $result['Field_html'], + '1<2' + ); + + $this->assertEquals( + $result['Field_md5'], + '4342210df36bf2ff2c4e2a997a6d4089' + ); + + $this->assertEquals( + $result['True_Type'], + 'float' + ); + + $this->assertEquals( + $result['len'], + 100 + ); + + $this->assertEquals( + $result['Field_title'], + '1<2' + ); + + $this->assertEquals( + $result['is_binary'], + false + ); + + $this->assertEquals( + $result['is_blob'], + false + ); + + $this->assertEquals( + $result['is_char'], + false + ); + + $this->assertEquals( + $result['pma_type'], + 'float(10, 1)' + ); + + $this->assertEquals( + $result['wrap'], + ' nowrap' + ); + + $this->assertEquals( + $result['Field'], + '1<2' + ); + } + + /** + * Test for PMA_getColumnTitle + * + * @return void + */ + public function testGetColumnTitle() + { + $column['Field'] = 'f1<'; + $column['Field_html'] = 'f1<'; + + $this->assertEquals( + PMA_getColumnTitle($column, array()), + 'f1<' + ); + + $comments['f1<'] = 'comment>'; + + $result = PMA_getColumnTitle($column, $comments); + + $this->assertContains( + 'title="comment>"', + $result + ); + + $this->assertContains( + 'f1<', + $result + ); + } + + /** + * Test for PMA_isColumnBinary + * + * @return void + */ + public function testIsColumnBinary() + { + $column['Type'] = 'binaryfoo'; + $this->assertEquals('binaryfoo', PMA_isColumnBinary($column)); + + $column['Type'] = 'Binaryfoo'; + $this->assertEquals('Binaryfoo', PMA_isColumnBinary($column)); + + $column['Type'] = 'varbinaryfoo'; + $this->assertEquals('binaryfoo', PMA_isColumnBinary($column)); + + $column['Type'] = 'barbinaryfoo'; + $this->assertFalse(PMA_isColumnBinary($column)); + } + + /** + * Test for PMA_isColumnBlob + * + * @return void + */ + public function testIsColumnBlog() + { + $column['Type'] = 'blob'; + $this->assertEquals('blob', PMA_isColumnBlob($column)); + + $column['Type'] = 'bloB'; + $this->assertEquals('bloB', PMA_isColumnBlob($column)); + + $column['Type'] = 'mediumBloB'; + $this->assertEquals('BloB', PMA_isColumnBlob($column)); + + $column['Type'] = 'tinyblobabc'; + $this->assertEquals('blobabc', PMA_isColumnBlob($column)); + + $column['Type'] = 'longblob'; + $this->assertEquals('blob', PMA_isColumnBlob($column)); + + $column['Type'] = 'foolongblobbar'; + $this->assertFalse(PMA_isColumnBlob($column)); + } + + /** + * Test for PMA_iscolumnchar + * + * @return void + */ + public function testIsColumnChar() + { + $column['Type'] = 'char(10)'; + $this->assertEquals('char(10)', PMA_iscolumnchar($column)); + + $column['Type'] = 'VarChar(20)'; + $this->assertEquals('Char(20)', PMA_iscolumnchar($column)); + + $column['Type'] = 'foochar'; + $this->assertFalse(PMA_iscolumnchar($column)); + } + + /** + * Test for PMA_getEnumSetAndTimestampColumns + * + * @return void + */ + public function testGetEnumAndTimestampColumns() + { + $column['True_Type'] = 'set'; + $this->assertEquals( + array('set', '', false), + PMA_getEnumSetAndTimestampColumns($column, false) + ); + + $column['True_Type'] = 'enum'; + $this->assertEquals( + array('enum', '', false), + PMA_getEnumSetAndTimestampColumns($column, false) + ); + + $column['True_Type'] = 'timestamp'; + $column['Type'] = 'date'; + $this->assertEquals( + array('date', ' nowrap', true), + PMA_getEnumSetAndTimestampColumns($column, false) + ); + + $column['True_Type'] = 'timestamp'; + $column['Type'] = 'date'; + $this->assertEquals( + array('date', ' nowrap', false), + PMA_getEnumSetAndTimestampColumns($column, true) + ); + + $column['True_Type'] = 'SET'; + $column['Type'] = 'num'; + $this->assertEquals( + array('num', ' nowrap', false), + PMA_getEnumSetAndTimestampColumns($column, false) + ); + + $column['True_Type'] = ''; + $column['Type'] = 'num'; + $this->assertEquals( + array('num', ' nowrap', false), + PMA_getEnumSetAndTimestampColumns($column, false) + ); + } + + /** + * Test for PMA_getFunctionColumn + * + * @return void + */ + public function testGetFunctionColumn() + { + $GLOBALS['cfg']['ProtectBinary'] = true; + $column['is_blob'] = true; + $this->assertTag( + PMA_getTagArray('', array('content' => 'Binary')), + PMA_getFunctionColumn($column, false, '', '', '', '', '', '', '') + ); + + $GLOBALS['cfg']['ProtectBinary'] = 'all'; + $column['is_binary'] = true; + $this->assertTag( + PMA_getTagArray('', array('content' => 'Binary')), + PMA_getFunctionColumn($column, true, '', '', '', '', '', '', '') + ); + + $GLOBALS['cfg']['ProtectBinary'] = 'noblob'; + $column['is_blob'] = false; + $this->assertTag( + PMA_getTagArray('', array('content' => 'Binary')), + PMA_getFunctionColumn($column, true, '', '', '', '', '', '', '') + ); + + $GLOBALS['cfg']['ProtectBinary'] = false; + $column['True_Type'] = 'enum'; + $this->assertTag( + PMA_getTagArray('', array('content' => '--')), + PMA_getFunctionColumn($column, true, '', '', '', '', '', '', '') + ); + + $column['True_Type'] = 'set'; + $this->assertTag( + PMA_getTagArray('', array('content' => '--')), + PMA_getFunctionColumn($column, true, '', '', '', '', '', '', '') + ); + + $column['True_Type'] = ''; + $column['pma_type'] = 'int'; + $this->assertTag( + PMA_getTagArray('', array('content' => '--')), + PMA_getFunctionColumn( + $column, true, '', '', array('int'), '', '', '', '' + ) + ); + + $GLOBALS['PMA_Types'] = new PMA_Types; + $column['Field'] = 'num'; + $this->assertContains( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + 'assertTag( + PMA_getTagArray( + 'assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '", '', '' + ); + + $this->assertEquals( + "\n", + $result + ); + } + + /** + * Test for PMA_getNullifyCodeForNullColumn + * + * @return void + */ + public function testGetNullifyCodeForNullColumn() + { + $column['True_Type'] = 'enum'; + $column['Type'] = 'ababababababababababa'; + $this->assertEquals( + '1', + PMA_getNullifyCodeForNullColumn($column, null, null) + ); + + $column['True_Type'] = 'enum'; + $column['Type'] = 'abababababababababab'; + $this->assertEquals( + '2', + PMA_getNullifyCodeForNullColumn($column, null, null) + ); + + $column['True_Type'] = 'set'; + $this->assertEquals( + '3', + PMA_getNullifyCodeForNullColumn($column, null, null) + ); + + $column['True_Type'] = ''; + $column['Field'] = 'f'; + $foreigners['f'] = true; + $foreignData['foreign_link'] = ''; + $this->assertEquals( + '4', + PMA_getNullifyCodeForNullColumn($column, $foreigners, $foreignData) + ); + } + + /** + * Test for PMA_getForeignLink + * + * @return void + */ + public function testGetForeignLink() + { + $column['Field'] = 'f'; + $titles['Browse'] = "'"; + $GLOBALS['cfg']['ServerDefault'] = 2; + $result = PMA_getForeignLink( + $column, 'a', 'b', 'd', 2, 0, 1, "", array('tbl', 'db'), 1, $titles + ); + + $this->assertTag( + PMA_getTagArray( + 'assertTag( + PMA_getTagArray( + '', + array('content' => "\\'") + ), + $result + ); + + $this->assertContains( + '", $foreignData + ); + + $this->assertContains( + "a\n", + $result + ); + + $this->assertContains( + 'assertTag( + PMA_getTagArray( + '
 (Max: 64KiB)' . "\n", + $result + ); + + // case 5 + $column['is_blob'] = false; + $column['len'] = 10; + $GLOBALS['cfg']['LimitChars'] = 40; + + /** + * This condition should be tested, however, it gives an undefined function + * PMA_getFileSelectOptions error: + * $GLOBALS['cfg']['UploadDir'] = true; + * + */ + + $result = PMA_getBinaryAndBlobColumn( + $column, '1223', null, 20, 'a', 'b', 'c', 2, 1, 1, '/', null, + 'foo', true + ); + + $this->assertEquals( + "\na\n" + . '', + $result + ); + } + + /** + * Test for PMA_getHTMLinput + * + * @return void + */ + public function testGetHTMLinput() + { + $column['pma_type'] = 'date'; + $result = PMA_getHTMLinput($column, 'a', 'b', 30, 'c', 23, 2, 0); + + $this->assertEquals( + '', + $result + ); + + // case 2 datetime + $column['pma_type'] = 'datetime'; + $result = PMA_getHTMLinput($column, 'a', 'b', 30, 'c', 23, 2, 0); + $this->assertEquals( + '', + $result + ); + + // case 3 timestamp + $column['pma_type'] = 'timestamp'; + $result = PMA_getHTMLinput($column, 'a', 'b', 30, 'c', 23, 2, 0); + $this->assertEquals( + '', + $result + ); + } + + /** + * Test for PMA_getMaxUploadSize + * + * @return void + */ + public function testGetMaxUploadSize() + { + $GLOBALS['max_upload_size'] = 257; + $column['pma_type'] = 'tinyblob'; + $result = PMA_getMaxUploadSize($column, 256); + + $this->assertEquals( + array("(Max: 256B)\n", 256), + $result + ); + + // case 2 + $GLOBALS['max_upload_size'] = 250; + $column['pma_type'] = 'tinyblob'; + $result = PMA_getMaxUploadSize($column, 20); + + $this->assertEquals( + array("(Max: 250B)\n", 250), + $result + ); + } + + /** + * Test for PMA_getNoSupportTypes + * + * @return void + */ + public function testGetNoSupportTypes() + { + $column['len'] = 20; + $column['is_char'] = true; + $GLOBALS['cfg']['CharEditing'] = ''; + $GLOBALS['cfg']['MaxSizeForInputField'] = 30; + $GLOBALS['cfg']['MinSizeForInputField'] = 10; + $GLOBALS['cfg']['TextareaRows'] = 20; + $GLOBALS['cfg']['TextareaCols'] = 10; + $GLOBALS['cfg']['CharTextareaRows'] = 5; + $GLOBALS['cfg']['CharTextareaCols'] = 1; + + $extracted_columnspec['spec_in_brackets'] = 25; + $result = PMA_getNoSupportTypes( + $column, 'defchar', 'a', 'b', 'c', 22, '<', 12, 1, "/", "<", + "foo\nbar", $extracted_columnspec + ); + + $this->assertEquals( + "a\n\na\n" + . '', + $result + ); + + // case 2: (else) + $column['is_char'] = false; + $column['Extra'] = 'auto_increment'; + $column['pma_type'] = 'timestamp'; + $column['True_Type'] = 'bit'; + $result = PMA_getNoSupportTypes( + $column, 'defchar', 'a', 'b', 'c', 22, '<', 12, 1, "/", "<", + "foo\nbar", $extracted_columnspec + ); + + $this->assertEquals( + "a\n" + . '', + $result + ); + + // case 3: (else -> datetime) + $column['pma_type'] = 'datetime'; + $result = PMA_getNoSupportTypes( + $column, 'defchar', 'a', 'b', 'c', 22, '<', 12, 1, "/", "<", + "foo\nbar", $extracted_columnspec + ); + + $this->assertContains( + '', + $result + ); + } + + /** + * Test for PMA_getColumnSize + * + * @return void + */ + public function testGetColumnSize() + { + $column['is_char'] = true; + $extracted_columnspec['spec_in_brackets'] = 45; + $GLOBALS['cfg']['MinSizeForInputField'] = 30; + $GLOBALS['cfg']['MaxSizeForInputField'] = 40; + + $this->assertEquals( + 40, + PMA_getColumnSize($column, $extracted_columnspec) + ); + + $this->assertEquals( + 'textarea', + $GLOBALS['cfg']['CharEditing'] + ); + + // case 2 + $column['is_char'] = false; + $column['len'] = 20; + $this->assertEquals( + 30, + PMA_getColumnSize($column, $extracted_columnspec) + ); + } + + /** + * Test for PMA_getHTMLforGisDataTypes + * + * @return void + */ + public function testGetHTMLforGisDataTypes() + { + $GLOBALS['cfg']['ActionLinksMode'] = 'icons'; + $GLOBALS['cfg']['LinkLengthLimit'] = 2; + $this->assertContains( + '
Edit/Insert' + . '', + PMA_getHTMLforGisDataTypes() + ); + } + + /** + * Test for PMA_getContinueInsertionForm + * + * @return void + */ + public function testGetContinueInsertionForm() + { + $where_clause_array = array("aassertTag( + PMA_getTagArray( + '
' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '' + ), + $result + ); + + $this->assertTag( + PMA_getTagArray( + '