/** * write() should return string if $format is valid */ public function test_write_returnsString_ifFormatIsValid() { $string = '\\cxds ing'; $snippet = new Snippet(); $snippet->read($string); return $this->assertEquals($string, $snippet->write()); }
/** * Saves a new snippet * @param {stdClass} $data Data passed from ActionScript * @return {array} Standard response base */ public function newSnippet($data) { $response = CodeBank_ClientAPI::responseBase(); //Ensure logged in if (!Permission::check('CODE_BANK_ACCESS')) { $response['status'] = 'EROR'; $response['message'] = _t('CodeBankAPI.PERMISSION_DENINED', '_Permission Denied'); return $response; } try { $snippet = new Snippet(); $snippet->Title = $data->title; $snippet->Description = $data->description; $snippet->Text = $data->code; $snippet->Tags = $data->tags; $snippet->LanguageID = $data->language; $snippet->CreatorID = Member::currentUserID(); $snippet->PackageID = $data->packageID; if ($data->folderID > 0) { $folder = SnippetFolder::get()->byID(intval($data->folderID)); if (empty($folder) || $folder === false || $folder->ID == 0) { $response['status'] = "EROR"; $response['message'] = _t('CodeBankAPI.FOLDER_DOES_NOT_EXIST', '_Folder does not exist'); return $response; } if ($folder->LanguageID != $snippet->LanguageID) { $response['status'] = "EROR"; $response['message'] = _t('CodeBankAPI.FOLDER_NOT_LANGUAGE', '_Folder is not in the same language as the snippet'); return $response; } } $snippet->write(); $response['status'] = "HELO"; } catch (Exception $e) { $response['status'] = "EROR"; $response['message'] = "Internal Server error occured"; } return $response; }
/** * Tests to see if the language deletion is working correctly */ public function testDeleteLanguage() { $this->objFromFixture('Member', 'admin')->login(); //Login admin //Try deleting a default language $response = $this->getAMFResponse('Administration.deleteLanguage', array('id' => SnippetLanguage::get()->filter('Name', 'PHP')->first()->ID)); //Validate the response $this->assertEquals('EROR', $response['status'], 'Response status should have been EROR'); $this->assertEquals(_t('CodeBankAPI.LANGUAGE_DELETE_ERROR', '_Language cannot be deleted, it is either not a user language or has snippets attached to it'), $response['message'], 'Response message should have been that the language is not a user language or has children'); //Create a test language $lang = new SnippetLanguage(); $lang->Name = 'API Language'; $lang->FileExtension = 'api'; $lang->UserLanguage = true; $lang->write(); //Create a snippet for the language $snippet = new Snippet(); $snippet->Title = 'test snippet'; $snippet->Text = 'Hello World'; $snippet->LanguageID = $lang->ID; $snippet->write(); //Try deleting a the language $response = $this->getAMFResponse('Administration.deleteLanguage', array('id' => $lang->ID)); //Validate the response $this->assertEquals('EROR', $response['status'], 'Response status should have been EROR'); $this->assertEquals(_t('CodeBankAPI.LANGUAGE_DELETE_ERROR', '_Language cannot be deleted, it is either not a user language or has snippets attached to it'), $response['message'], 'Response message should have been that the language is not a user language or has children'); //Delete the snippet and try again $snippet->delete(); $response = $this->getAMFResponse('Administration.deleteLanguage', array('id' => $lang->ID)); //Validate the response $this->assertEquals('HELO', $response['status'], 'Response status should have been HELO'); }
/** * Processes the upload request * @param {array} $data Submitted data * @param {Form} $form Submitting form * @return {SS_HTTPResponse} Response */ public function doImportData($data, Form $form) { if (!Permission::check('ADMIN')) { Security::permissionFailure($this); return; } $fileData = $form->Fields()->dataFieldByName('ImportFile')->Value(); //Check that the file uploaded if (!array_key_exists('tmp_name', $fileData) || !file_exists($fileData['tmp_name'])) { $form->sessionMessage(_t('CodeBank.IMPORT_READ_ERROR', '_Could not read the file to be imported'), 'bad'); return $this->redirectBack(); } //Load the file into memory $fileData = file_get_contents($fileData['tmp_name']); if ($fileData === false || empty($fileData)) { $form->sessionMessage(_t('CodeBank.IMPORT_READ_ERROR', '_Could not read the file to be imported'), 'bad'); return $this->redirectBack(); } //Decode the json $fileData = json_decode($fileData); if ($fileData === false || !is_object($fileData)) { $form->sessionMessage(_t('CodeBank.IMPORT_READ_ERROR', '_Could not read the file to be imported'), 'bad'); return $this->redirectBack(); } //Verify the format is ToServer if ($fileData->format != 'ToServer') { $form->sessionMessage(_t('CodeBank.IMPORT_FILE_FORMAT_INCORRECT', '_Import file format is incorrect'), 'bad'); return $this->redirectBack(); } //Bump Up the time limit this may take time set_time_limit(480); //Start transaction if supported if (DB::getConn()->supportsTransactions()) { DB::getConn()->transactionStart(); } //If not appending empty the tables if (!isset($data['AppendData'])) { DB::query('DELETE FROM Snippet'); DB::query('DELETE FROM SnippetVersion'); DB::query('DELETE FROM SnippetLanguage'); DB::query('DELETE FROM SnippetPackage'); DB::query('DELETE FROM SnippetFolder'); } else { $langMap = array(); $pkgMap = array(); $folderMap = array(); $snipMap = array(); } //Import Languages foreach ($fileData->data->languages as $lang) { if (isset($data['AppendData'])) { $dbLang = SnippetLanguage::get()->filter('Name:ExactMatch:nocase', Convert::raw2sql($lang->language))->first(); if (!empty($dbLang) && $dbLang !== false && $dbLang->ID > 0) { $langMap['lang-' . $lang->id] = $dbLang->ID; } else { $newLang = new SnippetLanguage(); $newLang->Name = $lang->language; $newLang->FileExtension = $lang->file_extension; $newLang->HighlightCode = $lang->shjs_code; $newLang->UserLanguage = $lang->user_language; $newLang->write(); $langMap['lang-' . $lang->id] = $newLang->ID; unset($newLang); } } else { DB::query('INSERT INTO "SnippetLanguage" ("ID", "ClassName", "Created", "LastEdited", "Name", "FileExtension", "HighlightCode", "UserLanguage") ' . "VALUES(" . intval($lang->id) . ",'SnippetLanguage', '" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "','" . Convert::raw2sql($lang->language) . "','" . Convert::raw2sql($lang->file_extension) . "','" . Convert::raw2sql($lang->shjs_code) . "'," . intval($lang->user_language) . ")"); } } //Import Packages foreach ($fileData->data->packages as $pkg) { if (isset($data['AppendData'])) { $newPkg = new SnippetPackage(); $newPkg->Title = $pkg->title; $newPkg->write(); $pkgMap['pkg-' . $pkg->id] = $newPkg->ID; unset($newPkg); } else { DB::query('INSERT INTO "SnippetPackage" ("ID", "ClassName", "Created", "LastEdited", "Title") ' . "VALUES(" . intval($pkg->id) . ",'SnippetPackage', '" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "','" . Convert::raw2sql($pkg->title) . "')"); } } //Import Folders foreach ($fileData->data->folders as $folder) { if (isset($data['AppendData'])) { if (!isset($langMap['lang-' . $folder->fkLanguageId])) { if (DB::getConn()->supportsTransactions()) { DB::getConn()->transactionRollback(); } $form->sessionMessage(_t('CodeBank.IMPORT_LANG_NOT_FOUND', '_Import failed language not found'), 'bad'); return $this->redirectBack(); } $newFld = new SnippetFolder(); $newFld->Name = $folder->name; $newFld->ParentID = $folder->fkParentId > 0 && isset($folderMap['fld-' . $folder->fkParentId]) ? $folderMap['fld-' . $folder->fkParentId] : 0; $newFld->LanguageID = $langMap['lang-' . $folder->fkLanguageId]; $newFld->write(); $folderMap['fld-' . $folder->id] = $newFld->ID; unset($newFld); } else { DB::query('INSERT INTO "SnippetFolder" ("ID", "ClassName", "Created", "LastEdited", "Name", "ParentID", "LanguageID") ' . "VALUES(" . intval($folder->id) . ",'SnippetFolder', '" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "','" . Convert::raw2sql($folder->name) . "', " . intval($folder->fkParentId) . ", " . intval($folder->fkLanguageId) . ")"); } } //Import Snippets foreach ($fileData->data->snippets as $snip) { if (isset($data['AppendData'])) { if (!isset($langMap['lang-' . $snip->fkLanguage])) { if (DB::getConn()->supportsTransactions()) { DB::getConn()->transactionRollback(); } $form->sessionMessage(_t('CodeBank.IMPORT_LANG_NOT_FOUND', '_Import failed language not found'), 'bad'); return $this->redirectBack(); } $newSnip = new Snippet(); $newSnip->Title = $snip->title; $newSnip->Description = $snip->description; $newSnip->Tags = $snip->tags; $newSnip->LanguageID = $langMap['lang-' . $snip->fkLanguage]; $newSnip->CreatorID = Member::currentUserID(); $newSnip->LastEditorID = Member::currentUserID(); $newSnip->PackageID = $snip->fkPackageID > 0 && isset($pkgMap['pkg-' . $snip->fkPackageID]) ? $pkgMap['pkg-' . $snip->fkPackageID] : 0; $newSnip->FolderID = $snip->fkFolderID > 0 && isset($folderMap['fld-' . $snip->fkFolderID]) ? $folderMap['fld-' . $snip->fkFolderID] : 0; $newSnip->write(); $snipMap['snip-' . $snip->id] = $newSnip->ID; unset($newSnip); } else { DB::query('INSERT INTO "Snippet" ("ID", "ClassName", "Created", "LastEdited", "Title", "Description", "Tags", "LanguageID", "CreatorID", "LastEditorID", "PackageID", "FolderID") ' . "VALUES(" . intval($snip->id) . ",'Snippet', '" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "','" . Convert::raw2sql($snip->title) . "', '" . Convert::raw2sql($snip->description) . "', '" . Convert::raw2sql($snip->tags) . "', " . intval($snip->fkLanguage) . ", " . Member::currentUserID() . ", " . Member::currentUserID() . ", " . intval($snip->fkPackageID) . ", " . intval($snip->fkFolderID) . ")"); } } //Import Snippet Versions foreach ($fileData->data->versions as $ver) { if (isset($data['AppendData'])) { if (!isset($snipMap['snip-' . $ver->fkSnippit])) { if (DB::getConn()->supportsTransactions()) { DB::getConn()->transactionRollback(); } $form->sessionMessage(_t('CodeBank.IMPORT_SNIP_NOT_FOUND', '_Import failed snippet not found'), 'bad'); return $this->redirectBack(); } DB::query('INSERT INTO "SnippetVersion" ("ClassName", "Created", "LastEdited", "Text", "ParentID") ' . "VALUES('SnippetVersion', '" . Convert::raw2sql($ver->date) . "','" . Convert::raw2sql($ver->date) . "','" . Convert::raw2sql($ver->text) . "', " . intval($snipMap['snip-' . $ver->fkSnippit]) . ")"); } else { DB::query('INSERT INTO "SnippetVersion" ("ID", "ClassName", "Created", "LastEdited", "Text", "ParentID") ' . "VALUES(" . intval($ver->id) . ",'SnippetVersion', '" . Convert::raw2sql($ver->date) . "','" . Convert::raw2sql($ver->date) . "','" . Convert::raw2sql($ver->text) . "', " . intval($ver->fkSnippit) . ")"); } } //End transaction if supported if (DB::getConn()->supportsTransactions()) { DB::getConn()->transactionEnd(); } //Display success after redirecting back Session::set('reloadOnImportDialogClose', true); $form->sessionMessage(_t('CodeBank.IMPORT_COMPLETE', '_Import Completed'), 'good'); return $this->redirectBack(); }