store = Store::getInstance("\n; some comment\n" . TYPO3_FORM . "=testformnameDoNotChange\n", true, 'init'); } /** * @throws CodeException * @throws UserFormException * @throws UserReportException */ public function testGetInstance() { $a = Store::getInstance('', true); $b = Store::getInstance('', true); $this->assertFalse($a === null, "There should be a class"); $this->assertEquals($a, $b, "Both classes should be the same"); } /** * @throws CodeException * @throws UserFormException */ public function testGetVarStoreTypo3() { // T3 Bodytext $this->store->setVar(TYPO3_FORM, "testformnameDoNotChange", STORE_TYPO3); $this->assertEquals('testformnameDoNotChange', $this->store->getVar(TYPO3_FORM, STORE_TYPO3), "System: " . TYPO3_FORM); } /** * @throws CodeException * @throws UserFormException */ public function testGetVarStoreSystem() { // DBUSER in qfq.ini $val = $this->store->getVar(SYSTEM_DB_INDEX_DATA, STORE_SYSTEM); $this->assertTrue($val !== false && $val !== '', "SYSTEM_DB_INDEX_DATA found in qfq.ini"); } /** * @throws CodeException * @throws UserFormException */ public function testSetVarStoreSystem() { // set new Sessionname $this->store->setVar(SYSTEM_SQL_LOG_MODE, "all", STORE_SYSTEM); // Sessionname: default value $this->assertEquals('all', $this->store->getVar(SYSTEM_SQL_LOG_MODE, STORE_SYSTEM), "System: SQL_LOG"); // set new Sessionname $this->store->setVar(SYSTEM_SQL_LOG_MODE, "modify", STORE_SYSTEM); $this->assertEquals('modify', $this->store->getVar(SYSTEM_SQL_LOG_MODE, STORE_SYSTEM), "System: SQL_LOG"); } /** * @throws CodeException * @throws UserFormException */ public function testGetVarStoreClient() { # Violates SANITIZE class: sanitized string is always an empty string. # Access are cached: # Test: Retrieve a variable, default sanitize class $this->assertEquals('1234', $this->store->getVar(CLIENT_RECORD_ID, STORE_CLIENT), "FormName: default sanitize class"); # violates default SANITIZE digit: sanitized string is always an empty string. $this->assertEquals('', $this->store->getVar(CLIENT_SIP, STORE_CLIENT), "sanitize class digit fails"); // Test GET $this->assertEquals('1234', $this->store->getVar('key01', STORE_CLIENT), "Param: GET"); // Test POST $this->assertEquals('2345', $this->store->getVar('key02', STORE_CLIENT), "Param: POST"); // Test not found $this->assertFalse($this->store->getVar('keyUnknown', STORE_CLIENT), "Param: unknown"); // Test Cache value $_POST['key03'] = 'lost'; $this->store->getVar('key03', STORE_CLIENT); $this->assertEquals('3456', $this->store->getVar('key03', STORE_CLIENT), "Param: read from cache"); // Test Cache not found $this->store->getVar('keyUnknwon2'); $this->assertFalse($this->store->getVar('keyUnknown2', STORE_CLIENT), "Param: unknown from cache"); // Test overwrite default sanitize class $this->assertEquals('!!digit!!', $this->store->getVar(CLIENT_FORM, STORE_CLIENT, SANITIZE_ALLOW_DIGIT), "Param: overwrite default sanitize class"); // Test: POST higher priority than GET $this->assertEquals('5678', $this->store->getVar('key04', STORE_CLIENT), "Param: POST higher priority than GET"); } /** * @throws CodeException * @throws DbException * @throws UserFormException * @throws UserReportException */ public function testStorePriority() { //default prio FSRVD $this->store->unsetStore(STORE_RECORD); $db = new Database(); $tableDefinition = $db->getTableDefinition('Person'); $this->store->fillStoreTableDefaultColumnType($tableDefinition); // $this->assertEquals('male', $this->store->getVar('gender','','alnumx'), "Get default definition from table person.gender"); $this->store->setVar('gender', 'female', STORE_RECORD); $this->assertEquals('female', $this->store->getVar('gender'), "Retrieve 'gender' from STORE_RECORD"); $this->store->setVar('gender', 'male2', STORE_SIP); $this->assertEquals('male2', $this->store->getVar('gender'), "Retrieve 'gender' from STORE_SIP"); $this->store->setVar('gender', 'female2', STORE_FORM); $this->assertEquals('female2', $this->store->getVar('gender', '', SANITIZE_ALLOW_ALNUMX), "Retrieve 'gender' from STORE_SIP"); } /** * @throws CodeException * @throws UserFormException */ public function testStoreDifferentSanitizeClass() { //default prio FSRD $this->store->setVar('fruit', 'apple', STORE_RECORD); $this->assertEquals('apple', $this->store->getVar('fruit'), "Retrieve 'fruit' from STORE_RECORD"); $this->store->setVar('color', 'green', STORE_FORM); $this->assertEquals('!!digit!!', $this->store->getVar('color'), "Retrieve 'color' from STORE_FORM"); $this->assertEquals('green', $this->store->getVar('color', '', SANITIZE_ALLOW_ALNUMX), "Retrieve 'color' from STORE_FORM"); } /** * @throws CodeException * @throws UserFormException */ public function testGetVarStore0() { //default prio FSRD $this->assertEquals(0, $this->store->getVar('fakename', STORE_ZERO), "Retrieve anything from STORE_ZERO"); } /** * @throws CodeException * @throws UserFormException */ public function testUnsetStore() { $this->store->unsetStore(STORE_RECORD); $this->store->setStore(array(), STORE_RECORD); $this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store."); } /** * @throws CodeException * @throws UserFormException */ public function testSetVarArray() { $this->store->unsetStore(STORE_RECORD); $arr = ['a' => '1', 'apple' => 'green']; $this->store->setStore($arr, STORE_RECORD); $this->assertEquals('1', $this->store->getVar('a', STORE_RECORD), "Retrieve a value from store 'fake'"); $this->assertEquals('green', $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store."); } /** * @throws CodeException * @throws UserFormException */ public function testSetVarArrayEmpty() { $this->store->unsetStore(STORE_RECORD); $arr = array(); $this->store->setStore($arr, STORE_RECORD); $this->assertEquals(false, $this->store->getVar('apple', STORE_RECORD), "Retrieve a value from store."); } /** * @throws CodeException * @throws UserFormException * @throws UserReportException */ public function testSetVar() { $this->store = Store::getInstance('', true); foreach ([STORE_SIP, STORE_CLIENT, STORE_EXTRA] AS $store) { // initial set $this->store->setVar('unitTest', '123', $store); $this->assertEquals('123', $this->store->getVar('unitTest', $store), "Retrieve var."); // redefine, using default 'overwrite=true' $this->store->setVar('unitTest', '1234', $store); $this->assertEquals('1234', $this->store->getVar('unitTest', $store), "Retrieve var."); // redefine, using explicit 'overwrite=true' $this->store->setVar('unitTest', '12345', $store, true); $this->assertEquals('12345', $this->store->getVar('unitTest', $store), "Retrieve var."); // redefine, using explicit 'overwrite=false' but with the same value (that is ok) $this->store->setVar('unitTest', '12345', $store, false); $this->assertEquals('12345', $this->store->getVar('unitTest', $store), "Retrieve var."); } } // /** // * @expectedException \qfq\UserFormException // * // * @throws CodeException // * @throws UserFormException // * @throws UserReportException // */ // public function testConfigMandatoryValues() { // // $fileName = $this->createFile(''); // // the following produces Undefined index exception since you give an empty config.ini which does not contain the index. // // What do you want to test here? // $this->store = Store::getInstance('', true, $fileName); // unlink($fileName); // // $this->assertEquals(false, $this->store->getStore(STORE_SYSTEM), "Retrieve system store."); // } // /** * @throws CodeException * @throws UserFormException * @throws UserReportException */ public function testAppendToStore() { $this->store->unsetStore(STORE_RECORD); $this->store->appendToStore(array(), STORE_RECORD); $this->assertEquals(array(), $this->store->getStore(STORE_RECORD)); $param = ['a' => 'hello', 'b' => 'world']; $this->store->appendToStore($param, STORE_RECORD); $this->assertEquals($param, $this->store->getStore(STORE_RECORD)); $this->store->appendToStore(null, STORE_RECORD); $this->assertEquals($param, $this->store->getStore(STORE_RECORD)); $this->store->appendToStore(array(), STORE_RECORD); $this->assertEquals($param, $this->store->getStore(STORE_RECORD)); $param2 = ['c' => 'nice', 'd' => 'job']; $this->store->appendToStore($param2, STORE_RECORD); $this->assertEquals(array_merge($param, $param2), $this->store->getStore(STORE_RECORD)); $param3 = ['a' => 'well', 'b' => 'done']; $this->store->appendToStore($param3, STORE_RECORD); $this->assertEquals(array_merge($param3, $param2), $this->store->getStore(STORE_RECORD)); $this->store->setStore($param, STORE_RECORD, true); $this->store->appendToStore([$param2, $param3, $param3], STORE_RECORD); $this->assertEquals(array_merge($param, $param2), $this->store->getStore(STORE_RECORD)); } /** * @param $body * @return bool|string */ private function createFile($body) { $tmpFileName = tempnam('/tmp', 'config.qfq.ini.'); $handle = fopen($tmpFileName, "w"); fwrite($handle, $body); fclose($handle); return $tmpFileName; } /** * @throws CodeException * @throws UserFormException * @throws UserReportException */ public function testConfigIniDefaultValues() { $expect = [ SYSTEM_DB_1_USER => '', SYSTEM_DB_1_SERVER => '', SYSTEM_DB_1_PASSWORD => '', SYSTEM_DB_1_NAME => '', SYSTEM_DOCUMENTATION_QFQ => SYSTEM_DOCUMENTATION_QFQ_URL, SYSTEM_FLAG_PRODUCTION => 'yes', SYSTEM_THUMBNAIL_DIR_SECURE => SYSTEM_THUMBNAIL_DIR_SECURE_DEFAULT, SYSTEM_THUMBNAIL_DIR_PUBLIC => SYSTEM_THUMBNAIL_DIR_PUBLIC_DEFAULT, SYSTEM_CMD_INKSCAPE => 'inkscape', SYSTEM_CMD_CONVERT => 'convert', SYSTEM_CMD_WKHTMLTOPDF => '/opt/wkhtmltox/bin/wkhtmltopdf', SYSTEM_DATE_FORMAT => 'yyyy-mm-dd', SYSTEM_THROW_GENERAL_ERROR => 'no', SYSTEM_QFQ_LOG => SYSTEM_QFQ_LOG_FILE, SYSTEM_SQL_LOG_MODE => SQL_LOG_MODE_MODIFY, SYSTEM_SHOW_DEBUG_INFO => SYSTEM_SHOW_DEBUG_INFO_AUTO, SYSTEM_DB_INIT => 'set names utf8', SYSTEM_DB_UPDATE => SYSTEM_DB_UPDATE_AUTO, SYSTEM_DB_INDEX_DATA => '1', SYSTEM_DB_INDEX_QFQ => '1', SYSTEM_ESCAPE_TYPE_DEFAULT => 'm', SYSTEM_SECURITY_VARS_HONEYPOT => 'email,username,password', SYSTEM_SECURITY_ATTACK_DELAY => 5, SYSTEM_SECURITY_SHOW_MESSAGE => '0', SYSTEM_SECURITY_GET_MAX_LENGTH => 50, SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS => SYSTEM_RECORD_LOCK_TIMEOUT_SECONDS_DEFAULT, SYSTEM_ENTER_AS_SUBMIT => 1, SYSTEM_EDIT_FORM_PAGE => 'form', SYSTEM_EXTRA_BUTTON_INFO_INLINE => '', SYSTEM_EXTRA_BUTTON_INFO_BELOW => '', SYSTEM_EXTRA_BUTTON_INFO_CLASS => '', SYSTEM_SAVE_BUTTON_TEXT => '', SYSTEM_SAVE_BUTTON_TOOLTIP => '', SYSTEM_SAVE_BUTTON_CLASS => 'btn btn-default navbar-btn', SYSTEM_SAVE_BUTTON_GLYPH_ICON => 'glyphicon-ok', SYSTEM_CLOSE_BUTTON_TEXT => '', SYSTEM_CLOSE_BUTTON_TOOLTIP => 'Close', SYSTEM_CLOSE_BUTTON_CLASS => 'btn btn-default navbar-btn', SYSTEM_CLOSE_BUTTON_GLYPH_ICON => 'glyphicon-remove', SYSTEM_DELETE_BUTTON_TEXT => '', SYSTEM_DELETE_BUTTON_TOOLTIP => 'Delete', SYSTEM_DELETE_BUTTON_CLASS => 'btn btn-default navbar-btn', SYSTEM_DELETE_BUTTON_GLYPH_ICON => 'glyphicon-trash', SYSTEM_NEW_BUTTON_TEXT => '', SYSTEM_NEW_BUTTON_TOOLTIP => 'New', SYSTEM_NEW_BUTTON_CLASS => 'btn btn-default navbar-btn', SYSTEM_NEW_BUTTON_GLYPH_ICON => 'glyphicon-plus', F_BS_COLUMNS => 'col-md-12 col-lg-10', F_BS_LABEL_COLUMNS => 'col-md-3 col-lg-3', F_BS_INPUT_COLUMNS => 'col-md-6 col-lg-6', F_BS_NOTE_COLUMNS => 'col-md-3 col-lg-3', F_FE_DATA_REQUIRED_ERROR => F_FE_DATA_REQUIRED_ERROR_DEFAULT, F_FE_DATA_MATCH_ERROR => F_FE_DATA_MATCH_ERROR_DEFAULT, F_FE_DATA_ERROR => F_FE_DATA_ERROR_DEFAULT, F_CLASS_PILL => 'qfq-color-grey-1', F_CLASS_BODY => 'qfq-color-grey-2', F_BUTTON_ON_CHANGE_CLASS => 'btn-info alert-info', SYSTEM_DB_NAME_DATA => '', SYSTEM_DB_NAME_QFQ => '', SYSTEM_SQL_LOG => SYSTEM_SQL_LOG_FILE, SYSTEM_MAIL_LOG => SYSTEM_MAIL_LOG_FILE, 'DB_2_USER' => '', 'DB_2_SERVER' => '', 'DB_2_PASSWORD' => '', 'DB_2_NAME' => '', 'DB_3_USER' => '', 'DB_3_SERVER' => '', 'DB_3_PASSWORD' => '', 'DB_3_NAME' => '', 'LDAP_1_RDN' => 'LDAP_1_RDN', 'LDAP_1_PASSWORD' => 'LDAP_1_PASSWORD', SYSTEM_LABEL_ALIGN => SYSTEM_LABEL_ALIGN_LEFT, F_FE_DATA_PATTERN_ERROR=> F_FE_DATA_PATTERN_ERROR_DEFAULT, F_FE_DATA_PATTERN_ERROR_SYSTEM=> F_FE_DATA_PATTERN_ERROR_DEFAULT, ]; $body = <<< EOT '', 'DB_1_SERVER' => '', 'DB_1_PASSWORD' => '', 'DB_1_NAME' => '', 'DB_2_USER' => '', 'DB_2_SERVER' => '', 'DB_2_PASSWORD' => '', 'DB_2_NAME' => '', 'DB_3_USER' => '', 'DB_3_SERVER' => '', 'DB_3_PASSWORD' => '', 'DB_3_NAME' => '', 'LDAP_1_RDN' => 'LDAP_1_RDN', 'LDAP_1_PASSWORD' => 'LDAP_1_PASSWORD', ]; EOT; $fileName = $this->createFile($body); $this->store = Store::getInstance('', true, $fileName); unlink($fileName); $config = $this->store->getStore(STORE_SYSTEM); # The following won't be checked by content, cause they will change on different installations. // foreach ([ SYSTEM_SQL_LOG, SYSTEM_MAIL_LOG, SYSTEM_SITE_PATH, SYSTEM_EXT_PATH, SYSTEM_SEND_E_MAIL] as $key) { foreach ([SYSTEM_SITE_PATH, SYSTEM_EXT_PATH, SYSTEM_SEND_E_MAIL, SYSTEM_SESSION_TIMEOUT_SECONDS] as $key) { $this->assertTrue(isset($config[$key]), "Missing default value for '$key' " ); unset ($config[$key]); } // check default values $this->assertEquals($expect, $config, "Retrieve system store."); } /** * @expectedException \qfq\UserFormException * * @throws CodeException * @throws UserFormException */ public function testGetStore() { $this->assertEquals(array(), $this->store->getStore('unknownstore')); } }