public function testThemeConstruct() { $path = realpath(dirname(__FILE__) . '/../../../'); $path_r = explode(DIRECTORY_SEPARATOR, $path); $app = $this->getApp(); $this->assertEquals(FileHelper::replaceSeparators('vfs://root/public/'), FileHelper::replaceSeparators($app->getTheme()->getBasePath())); unsetRealpath(); }
/** * @param \Skully\App\Models\Setting $oldMe */ public function afterDestroy($oldMe) { if ($this->bean->old('input_type') == "image") { //delete images if (file_exists($oldMe->imagePath())) { FileHelper::removeFolder($oldMe->imagePath()); } } }
public function testResizedWithoutMaxOnly() { unlink(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'stretched.jpg'); $originalPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'original.jpg'; $path = ImageProcessor::resize($originalPath, array('maxOnly' => false, 'w' => 1500, 'resultDir' => dirname(__FILE__) . DIRECTORY_SEPARATOR, 'outputFilename' => 'stretched.jpg')); $imagesize = getimagesize($path); $this->assertEquals(1500, $imagesize[0]); $this->assertEquals(FileHelper::replaceSeparators(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'stretched.jpg'), $path); }
public function testPackCommand() { $app = __setupApp(); $console = new Console($app, true); $packedPath = FileHelper::replaceSeparators(realpath(__DIR__ . '/packerTest/packed.js')); if (file_exists($packedPath)) { unlink($packedPath); } $this->assertFalse(file_exists($packedPath)); ob_get_clean(); ob_start(); $output = $console->run("skully:pack Commands/packerTest/packerTest.txt"); // Cannot test this on Windows somehow, so just check if pack command is running. $this->assertNotEmpty($output); }
function S3ProcessTempImage($tmp, $options, $oldFile = '') { $path = $this->parentProcessTempImage($tmp, $options, $oldFile); $key = S3Helpers::key($this->app->config('publicDir'), $path); $amazonS3Config = $this->app->config('amazonS3'); $client = S3Client::factory($amazonS3Config['settings']); $abspath = FileHelper::replaceSeparators($this->app->getTheme()->getBasePath() . $path); $client->putObject(array('Bucket' => $amazonS3Config['bucket'], 'Key' => $key, 'SourceFile' => $abspath, 'ACL' => 'public-read')); $client->waitUntil('ObjectExists', array('Bucket' => $amazonS3Config['bucket'], 'Key' => $key)); unlink($abspath); // Delete Old File if (!empty($oldFile)) { $relativeOldFile = str_replace($this->app->getTheme()->getPublicBasePath(), '', $oldFile); $oldKey = S3Helpers::key($this->app->config('publicDir'), $relativeOldFile); $client->deleteObject(array('Bucket' => $amazonS3Config['bucket'], 'Key' => $oldKey)); } return $path; }
public function testLoadDefaultTheme() { $app = $this->getApp(); $this->assertTrue(file_exists('vfs://root/public/test/App/')); $this->assertEquals(FileHelper::replaceSeparators('vfs://root/public/test/App/'), $app->getTheme()->getAppPath('')); $this->assertEquals(FileHelper::replaceSeparators('vfs://root/public/test/'), $app->getTheme()->getPath('')); $this->assertEquals(FileHelper::replaceSeparators('vfs://root/public/default/App/views/home/index.tpl'), $app->getTheme()->getAppPath(FileHelper::replaceSeparators('views/home/index.tpl'))); $this->assertEquals(FileHelper::replaceSeparators('vfs://root/public/test/App/views/admin/home/index.tpl'), $app->getTheme()->getAppPath(FileHelper::replaceSeparators('views/admin/home/index.tpl'))); /**@var Controller $controller **/ $controller = new \App\Controllers\HomeController($app, 'index'); $this->assertEquals('This is app default home', $controller->fetch('/home/index')); ob_start(); $controller->render(); $output = ob_get_clean(); $this->assertEquals('This is app default home', $output); $controller = new \App\Controllers\Admin\HomeController($app, 'index'); $this->assertEquals('This is app test admin home', $controller->fetch()); unsetRealpath(); }
public function testSetupGit() { $app = __setupApp(); $console = new Console($app, true); $console->addCommands($app->config('additionalCommands')); $gitpath = realpath(__DIR__ . '/../app/.git-s3'); echo $gitpath; shell_exec('rm -rf ' . $gitpath); $this->assertFalse(file_exists($gitpath)); $commands = $console->getCommands(); function className($item) { return get_class($item); } print_r(array_map('className', $commands)); $output = $console->run("skully:s3 setup:git -f"); echo $output->fetch(); FileHelper::removeFolder($gitpath); $this->assertFalse(file_exists($gitpath)); }
public function testUploadFile() { $this->migrate(); $this->login(); // Preparation: Create an image entry. $image = $this->app->createModel('image', array('position' => 0)); R::store($image); $this->assertNotEmpty($image->getID()); // upload to aws s3 $filename = 'original.jpg'; $filepath = realpath(__DIR__ . '/' . $filename); $_SERVER['REQUEST_METHOD'] = "POST"; $params = array('image_id' => $image->getID(), 'data' => '{ "id": ' . $image->getID() . ', "type": "uploadOnce", "settingName": "multiple_many_types" }', 'settingName' => 'multiple_many_types', 'uploadOnce' => 1); $size = filesize($filepath); $_FILES = array('file-0' => array('name' => $filename, 'type' => 'image/jpeg', 'tmp_name' => $filepath, 'error' => 0, 'size' => $size)); ob_start(); $this->app->runControllerFromRawUrl('admin/cRUDImages/uploadImage', $params); $output = ob_get_clean(); echo "\nOutput of uploadImage:\n"; echo $output; $output_r = json_decode($output); print_r($output_r); $this->assertFalse(property_exists($output_r[0], 'error')); // See if file uploaded successfully. echo "\nFind image with id " . $image->getID(); $imageBean = R::findOne('image', 'id = ?', array($image->getID())); $this->assertNotEmpty($imageBean); echo "\n...Found!"; $data = json_decode($imageBean['multiple_many_types']); echo "\nUploaded images:\n"; print_r($data); $this->assertEquals(1, count($data)); // Uploaded data must contain 'images/Image/1/original-smartphone' $this->assertNotEquals(-1, strpos(SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone), 'public/images/Image/' . $image->getID() . '/original-smartphone')); // Now see if file does exist in Amazon S3 repository. $amazonS3Config = $this->app->config('amazonS3'); $client = \Aws\S3\S3Client::factory($amazonS3Config['settings']); $result = $client->getObject(array('Bucket' => $amazonS3Config['bucket'], 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone))); $this->assertNotEmpty($result['Body']); // $this->app->getLogger()->log("result get object : " . print_r($result, true)); // TEST ACL $resultAcl = $client->getObjectAcl(array('Bucket' => $amazonS3Config['bucket'], 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone))); // $this->app->getLogger()->log("result get object ACL : " . print_r($resultAcl, true)); $this->assertNotEmpty($resultAcl["Grants"]); $dataAcl = array(); foreach (\Aws\S3\Model\Acp::fromArray($resultAcl->toArray()) as $grant) { $grantee = $grant->getGrantee(); $dataAcl[$grantee->getGroupUri()] = array($grantee->getType(), $grant->getPermission()); } $this->assertEquals(2, count($dataAcl)); $this->assertArrayHasKey('http://acs.amazonaws.com/groups/global/AllUsers', $dataAcl); $this->assertEquals(array('Group', 'READ'), $dataAcl['http://acs.amazonaws.com/groups/global/AllUsers']); // ---- end of test ACL ---- // Local file must have been deleted. $filepath = \Skully\App\Helpers\FileHelper::replaceSeparators($this->app->getTheme()->getBasePath() . $data[0]->smartphone); echo "\nChecking if file {$filepath} is deleted..."; $this->assertFalse(file_exists($filepath)); echo "\nYep"; // Delete Models try { R::trash($imageBean); } catch (\Exception $e) { echo 'failed to delete image bean. reason: ' . $e->getMessage(); } // // Cleanup by removing files in bucket. // $client->deleteObject(array( // 'Bucket' => $amazonS3Config['bucket'], // 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone)) // ); // // $client->deleteObject(array( // 'Bucket' => $amazonS3Config['bucket'], // 'Key' => SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->desktop)) // ); // Check existence of deleted files on s3 server $result = $client->doesObjectExist($amazonS3Config['bucket'], SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->smartphone)); $this->assertFalse($result); $result = $client->doesObjectExist($amazonS3Config['bucket'], SkullyAwsS3\Helpers\S3Helpers::key($this->app->config('publicDir'), $data[0]->desktop)); $this->assertFalse($result); }
/** * @param $instance * @param string $imageFieldName Name of image field to lookup for. In * setting this is 'value', but in CRUD this should be name of * image field. * @throws \Exception * @return null|string */ protected function processUploadedImage($instance, $imageFieldName = 'value') { $uploadOnce = UtilitiesHelper::toBoolean($this->getParam('uploadOnce')); $uploadedImages = array(); $error = null; $type = $this->getParam('type'); $settingName = $this->getParam('settingName'); $validFormats = array("jpg", "png", "gif", "bmp", "jpeg"); $imageSettings = $this->getImageSettings(); $imageSetting = $imageSettings[$settingName]; $this->app->getLogger()->log("files uploaded: " . print_r($_FILES, true)); if (empty($error) && !empty($instance) && !empty($imageSetting)) { $nFiles = count($_FILES); for ($i = 0; $i < $nFiles; $i++) { $name = $_FILES['file-' . $i]['name']; $size = $_FILES['file-' . $i]['size']; if (strlen($name)) { $filename_r = explode(".", $name); $ext = strtolower($filename_r[count($filename_r) - 1]); unset($filename_r[count($filename_r) - 1]); $imageName = implode($filename_r); if (in_array($ext, $validFormats)) { $maxFilesize = (int) $this->app->iniGet('upload_max_filesize') * 1024 * 1024; if ($size < $maxFilesize) { $tmp = $_FILES['file-' . $i]['tmp_name']; $filePath = $instance->imageBaseUrl(); $fullPath = $this->app->getTheme()->getPublicBasePath() . $filePath; if (!file_exists($fullPath)) { mkdir($fullPath, 0775, true); } // Create the file $options = array('resultDir' => $fullPath, 'noImagick' => $this->app->config('noImagick'), 'outputFilename' => str_replace(" ", "-", $name)); $instanceImages = $instance->get($imageFieldName); if (is_string($instanceImages) && $imageSetting["types"]) { $instanceImages = json_decode($instanceImages, true); } if ($uploadOnce) { // Upload once can only be used by multiple images. if ($imageSetting['types']) { // Many types $newInstanceImage = array(); foreach ($imageSetting['types'] as $key => $typeOptions) { $options = array_merge($options, $imageSetting['types'][$key]); $options['outputFilename'] = str_replace(" ", "-", $imageName) . '-' . $key . '.' . $ext; try { $path = $this->processTempImage($tmp, $options); $newInstanceImage[$key] = $path; $uploadedImages[] = array('data' => $this->getParam('data'), 'path' => $newInstanceImage[$key], 'message' => $this->app->getTranslator()->translate("imageUploaded")); } catch (\Exception $e) { $uploadedImages[] = array('data' => $this->getParam('data'), 'path' => $filePath . $imageName . '.' . $ext, 'message' => $e->getMessage(), 'error' => true); } } $instanceImages[] = $newInstanceImage; $instance->set($imageFieldName, json_encode($instanceImages)); } else { // Single type } } else { // Upload one image from "change" button. if ($imageSetting['types']) { // Many types if ($imageSetting['_config']['multiple']) { // multiple images $position = $this->getParam("position"); $options = array_merge($options, $imageSetting['types'][$type]); $oldFile = $this->app->getTheme()->getPublicBasePath() . $instanceImages[$position][$type]; try { $path = $this->processTempImage($tmp, $options, $oldFile); $instanceImages[$position][$type] = $path; $instance->set($imageFieldName, json_encode($instanceImages)); $uploadedImages[] = array('data' => $this->getParam('data'), 'path' => $instanceImages[$position][$type], 'message' => $this->app->getTranslator()->translate("imageUploaded")); } catch (\Exception $e) { $uploadedImages[] = array('data' => $this->getParam('data'), 'path' => $filePath . $imageName . '.' . $ext, 'message' => $e->getMessage(), 'error' => true); } } else { // single image if (empty($imageSetting['types'][$type])) { throw new \Exception("Type {$type} not found"); } if (empty($instanceImages)) { $instanceImages = array(); } $options = array_merge($options, $imageSetting['types'][$type]); $oldFile = $this->app->getTheme()->getPublicBasePath() . $instanceImages[$type]; try { $path = $this->processTempImage($tmp, $options, $oldFile); $instanceImages[$type] = $path; $instance->set($imageFieldName, json_encode($instanceImages)); $uploadedImages[] = array('data' => $this->getParam('data'), 'path' => $instanceImages[$type], 'message' => $this->app->getTranslator()->translate("imageUploaded")); } catch (\Exception $e) { throw new \Exception($e->getMessage()); } } } else { // One type $options = array_merge($options, $imageSetting['options']); if ($imageSetting['_config']['multiple']) { //MULTIPLE $position = $this->getParam("position"); $oldFile = $this->app->getTheme()->getPublicBasePath() . $instanceImages[$position]; try { $path = $this->processTempImage($tmp, $options, $oldFile); $instanceImages[$position] = $path; $instance->set($imageFieldName, $instanceImages); $uploadedImages[] = array('data' => $this->getParam('data'), 'path' => $instanceImages[$position], 'message' => $this->app->getTranslator()->translate("imageUploaded")); } catch (\Exception $e) { throw new \Exception($e->getMessage()); } } else { //SINGLE $oldFile = $this->app->getTheme()->getPublicBasePath() . $instanceImages; try { $path = $this->processTempImage($tmp, $options, $oldFile); $instanceImages = $path; $instance->set($imageFieldName, $instanceImages); $uploadedImages[] = array('data' => $this->getParam('data'), 'path' => $instanceImages, 'message' => $this->app->getTranslator()->translate("imageUploaded")); } catch (\Exception $e) { throw new \Exception($e->getMessage()); } } } } R::store($instance); } else { throw new \Exception($this->app->getTranslator()->translate('errorFilesizeTooBig', array("fileSize" => FileHelper::parseBytes($maxFilesize)))); } } else { throw new \Exception($this->app->getTranslator()->translate("errorInvalidFormat", array("ext" => implode(", ", $validFormats)))); } } else { throw new \Exception($this->app->getTranslator()->translate("errorFileEmpty")); } } } else { if (empty($instance)) { $uploadedImages = array('data' => $this->getParam('data'), 'path' => '', 'message' => $this->app->getTranslator()->translate("errorInstanceEmpty")); } elseif (empty($imageSetting)) { $uploadedImages = array('data' => $this->getParam('data'), 'path' => '', 'message' => $this->app->getTranslator()->translate("errorSettingEmpty")); } } return $uploadedImages; }
public function destroy() { $helper = $this->setupHelper(); $helper->setMode('item'); if (!empty($_POST)) { try { $this->assignItems($helper); $helper->deleteItem($this->getParam('p'), $this->getParam('id')); FileHelper::removeFolder($this->app->config('basePath') . 'App/smarty/cache'); FileHelper::removeFolder($this->app->config('basePath') . 'App/smarty/templates_c'); $this->setMessage("Item " . $this->getParam('id') . " deleted", 'message'); } catch (\Exception $e) { if (file_exists($helper->getCompletePath($this->getParam('p')))) { $this->setMessage("Item not found", 'error'); } else { $this->setMessage("File not found", 'error'); } } } else { $this->setMessage("Cannot access this page directly", 'error'); } echo json_encode(array('redirect' => $this->app->getRouter()->getUrl($this->indexPath, array('p' => $this->getParam('p'), 'id' => $this->getParam('id'), 'l' => $this->getParam('l'))))); }
public function afterDestroy($oldMe) { $oldId = $this->bean->old("id"); $classname = $this->classname(); /** @var \Skully\App\Models\BaseModel $this */ $imagePath = $this->getApp()->getTheme()->getPublicBasePath() . 'images/' . $classname . '/' . $oldId . '/'; FileHelper::removeFolder($imagePath); }
public function testSSL() { $app = $this->getApp(); $this->assertEquals(realpath(dirname(__FILE__)), $app->getRealpath(dirname(__FILE__))); $appHomeDir = FileHelper::replaceSeparators(__DIR__ . '/App/public/default/App/views/home'); file_put_contents(FileHelper::replaceSeparators($appHomeDir . '/ssl.tpl'), '{public_url value="test"}'); ob_start(); $_SERVER['HTTPS'] = true; $app->runControllerFromRawUrl('/home/ssl'); $_SERVER['HTTPS'] = false; $output = ob_get_clean(); $this->assertEquals('https://localhost/skully/public/?value=test', $output); file_put_contents(FileHelper::replaceSeparators($appHomeDir . '/ssl1.tpl'), '{public_url value="test" __ssl=true}'); ob_start(); $app->runControllerFromRawUrl('/home/ssl1'); $output = ob_get_clean(); $this->assertEquals('https://localhost/skully/public/?value=test', $output); file_put_contents(FileHelper::replaceSeparators($appHomeDir . '/ssl2.tpl'), '{theme_url value="test"}'); ob_start(); $_SERVER['HTTPS'] = true; $app->runControllerFromRawUrl('/home/ssl2'); $_SERVER['HTTPS'] = false; $output = ob_get_clean(); $this->assertEquals('https://localhost/skully/public/default/?value=test', $output); file_put_contents(FileHelper::replaceSeparators($appHomeDir . '/ssl3.tpl'), '{theme_url value="test" __ssl=true}'); ob_start(); $app->runControllerFromRawUrl('/home/ssl3'); $output = ob_get_clean(); $this->assertEquals('https://localhost/skully/public/default/?value=test', $output); /** * Testing SSL with url() function. */ file_put_contents(FileHelper::replaceSeparators($appHomeDir . '/ssl4.tpl'), '{url path="home/ssltest"}'); ob_start(); $_SERVER['HTTPS'] = true; $app->runControllerFromRawUrl('/home/ssl4'); $_SERVER['HTTPS'] = false; $output = ob_get_clean(); $this->assertEquals('https://localhost/skully/home/ssltest', $output); // If pointing to other site, do not attempt to change http. file_put_contents(FileHelper::replaceSeparators($appHomeDir . '/ssl5.tpl'), '{url path="http://teguhwijaya.com"}'); ob_start(); $_SERVER['HTTPS'] = true; $app->runControllerFromRawUrl('/home/ssl5'); $_SERVER['HTTPS'] = false; $output = ob_get_clean(); $this->assertEquals('http://teguhwijaya.com', $output); }
<?php namespace Tests; use RedBeanPHP\Facade as R; use Skully\App\Helpers\FileHelper; require_once FileHelper::replaceSeparators(dirname(__FILE__) . '/DatabaseTestCase.php'); class ModelTest extends \Tests\DatabaseTestCase { /** * Cant have camelcased beans * @expectedException \RedBeanPHP\RedException */ public function testTwoWordsCamelcasedBean() { R::freeze(false); $testName = R::dispense('testName'); $id = R::store($testName); R::load('testName', $id); R::freeze($this->frozen); } /** * Cant have underscored beans * @expectedException \RedBeanPHP\RedException */ public function testTwoWordsUnderscoredBean() { R::freeze(false); $testName = R::dispense('test_name'); $id = R::store($testName); R::load('test_name', $id);
/** * @param string $path * @param array $params * @param boolean $hideErrors True to hide errors from file not found. * @param boolean $ssl When true or false force to change security mode (http or https). * @throws \Skully\Exceptions\ThemeFileNotFoundException Given a path, must find that path within the themes/ directory * @return string */ public function getUrl($path = '', $params = array(), $hideErrors = false, $ssl = null) { $fullUrl = $path; $fullPath = $this->getBasePath() . $this->themeName . DIRECTORY_SEPARATOR . $path; if (!file_exists(FileHelper::replaceSeparators($fullPath))) { $fullPath = $this->getBasePath() . 'default' . DIRECTORY_SEPARATOR . $path; if (!file_exists(FileHelper::replaceSeparators($fullPath))) { } else { $fullUrl = $this->getPublicBaseUrl($ssl) . 'default/' . $path; } } else { $fullUrl = $this->getPublicBaseUrl($ssl) . $this->themeName . '/' . $path; } if (!file_exists(FileHelper::replaceSeparators($fullPath)) && !$hideErrors) { throw new ThemeFileNotFoundException("Theme file not found after searching at these locations: \n" . $this->getBasePath() . $this->themeName . DIRECTORY_SEPARATOR . FileHelper::replaceSeparators($path) . "\n" . $this->getBasePath() . 'default' . DIRECTORY_SEPARATOR . FileHelper::replaceSeparators($path) . "\n"); } if (empty($params)) { return $fullUrl; } else { return $fullUrl . '?' . http_build_query($params); } }
/** * @return array * Return additional pluginsDir used in template engine. * Override this as needed in the app. */ protected function additionalTemplateEnginePluginsDir() { return array(FileHelper::replaceSeparators($this->config('basePath') . $this->getAppName() . '/smarty/plugins/')); }