From ce0824035524b9159c8ff1b31c9304121379e20d Mon Sep 17 00:00:00 2001 From: Peter Papp Date: Fri, 5 Mar 2021 11:57:48 +0100 Subject: [PATCH] added it_update_settings, it_get_page, it_update_settings_image test --- app/Console/Commands/SetupDevEnvironment.php | 7 +- .../Controllers/Admin/PagesController.php | 11 +- .../General/SetupWizardController.php | 15 +- app/Http/Controllers/SettingController.php | 21 +- app/Http/Helpers/helpers.php | 10 +- app/Models/Setting.php | 8 +- app/Services/SetupService.php | 18 +- config/content.php | 296 +++++++++--------- ...020_06_25_142635_create_settings_table.php | 2 +- tests/Feature/AdminTest.php | 56 +++- 10 files changed, 250 insertions(+), 194 deletions(-) diff --git a/app/Console/Commands/SetupDevEnvironment.php b/app/Console/Commands/SetupDevEnvironment.php index 358325d1..ca4a57db 100644 --- a/app/Console/Commands/SetupDevEnvironment.php +++ b/app/Console/Commands/SetupDevEnvironment.php @@ -97,11 +97,6 @@ class SetupDevEnvironment extends Command ->each(function ($content) { Setting::updateOrCreate($content); }); - - collect(config('content.pages')) - ->each(function ($page) { - Page::updateOrCreate($page); - }); } /** @@ -189,6 +184,8 @@ class SetupDevEnvironment extends Command $this->call('migrate:fresh', [ '--force' => true ]); + + $this->setup->seed_default_pages(); } /** diff --git a/app/Http/Controllers/Admin/PagesController.php b/app/Http/Controllers/Admin/PagesController.php index c762349e..fa8b0161 100644 --- a/app/Http/Controllers/Admin/PagesController.php +++ b/app/Http/Controllers/Admin/PagesController.php @@ -21,8 +21,7 @@ class PagesController extends Controller public function index() { return new PageCollection( - Page::sortable() - ->paginate(10) + Page::sortable()->paginate(10) ); } @@ -34,9 +33,7 @@ class PagesController extends Controller */ public function show(Page $page) { - return new PageResource( - $page - ); + return new PageResource($page); } /** @@ -52,7 +49,9 @@ class PagesController extends Controller return Demo::response_204(); } - $page->update(make_single_input($request)); + $page->update( + make_single_input($request) + ); return response(new PageResource($page), 204); } diff --git a/app/Http/Controllers/General/SetupWizardController.php b/app/Http/Controllers/General/SetupWizardController.php index 1d498d52..85766274 100644 --- a/app/Http/Controllers/General/SetupWizardController.php +++ b/app/Http/Controllers/General/SetupWizardController.php @@ -336,17 +336,17 @@ class SetupWizardController extends Controller // Store Logo if ($request->hasFile('logo')) { - $logo = store_system_image($request->file('logo'), 'system'); + $logo = store_system_image($request->file('logo')); } // Store Logo horizontal if ($request->hasFile('logo_horizontal')) { - $logo_horizontal = store_system_image($request->file('logo_horizontal'), 'system'); + $logo_horizontal = store_system_image($request->file('logo_horizontal')); } // Store favicon if ($request->hasFile('favicon')) { - $favicon = store_system_image($request->file('favicon'), 'system'); + $favicon = store_system_image($request->file('favicon')); } // Get options @@ -467,13 +467,6 @@ class SetupWizardController extends Controller 'value' => $request->purchase_code, ]); - // Create legal pages and index content - $content = $request->license === 'Extended' ? collect(config('content.content_extended')) : collect(config('content.content_regular')); - - $content->each(function ($content) { - Setting::updateOrCreate($content); - }); - // Retrieve access token $response = Route::dispatch(self::make_login_request($request)); @@ -503,7 +496,7 @@ class SetupWizardController extends Controller '--force' => true ]); - $this->setup->seed_pages(); + $this->setup->seed_default_pages(); } /** diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php index 1109fead..8e63cc54 100644 --- a/app/Http/Controllers/SettingController.php +++ b/app/Http/Controllers/SettingController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers; use App\Http\Tools\Demo; -use App\Setting; +use App\Models\Setting; use Artisan; use Stripe; use Cartalyst\Stripe\Exception\UnauthorizedException; @@ -40,7 +40,6 @@ class SettingController extends Controller */ public function update(Request $request) { - // Check if is demo if (env('APP_DEMO')) { return Demo::response_204(); } @@ -48,21 +47,23 @@ class SettingController extends Controller // Store image if exist if ($request->hasFile($request->name)) { - // Store image - $image_path = store_system_image($request->file($request->name), 'system'); - // Find and update image path - Setting::updateOrCreate(['name' => $request->name], [ - 'value' => $image_path + Setting::updateOrCreate([ + 'name' => $request->name + ], [ + 'value' => store_system_image( + $request->file($request->name) + ) ]); return response('Done', 204); } // Find and update variable - Setting::updateOrCreate(['name' => $request->name], [ - 'value' => $request->value - ]); + Setting::updateOrCreate( + ['name' => $request->name], + ['value' => $request->value] + ); return response('Done', 204); } diff --git a/app/Http/Helpers/helpers.php b/app/Http/Helpers/helpers.php index 7472fbcd..599e3d96 100644 --- a/app/Http/Helpers/helpers.php +++ b/app/Http/Helpers/helpers.php @@ -35,9 +35,7 @@ function obfuscate_email($email) */ function get_setting($setting) { - $row = Setting::where('name', $setting)->first(); - - return $row ? $row->value : null; + return Setting::find($setting)->value ?? null; } /** @@ -256,13 +254,13 @@ function store_avatar($image) function store_system_image($image) { // Store avatar - $image_path = Str::random(8) . '-' . str_replace(' ', '', $image->getClientOriginalName()); + $filename = Str::random(8) . '-' . str_replace(' ', '', $image->getClientOriginalName()); // Store image to disk - Storage::putFileAs('system', $image, $image_path); + Storage::putFileAs('system', $image, $filename); // Return path to image - return `system/$image_path`; + return "system/$filename"; } /** diff --git a/app/Models/Setting.php b/app/Models/Setting.php index 7de5e5d3..67d51e0b 100644 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -9,7 +9,13 @@ class Setting extends Model { use HasFactory; + protected $fillable = [ + 'value', + ]; + public $timestamps = false; - protected $guarded = ['id']; + protected $primaryKey = 'name'; + + protected $keyType = 'string'; } diff --git a/app/Services/SetupService.php b/app/Services/SetupService.php index d318bafc..21f4346a 100644 --- a/app/Services/SetupService.php +++ b/app/Services/SetupService.php @@ -1,10 +1,9 @@ each(function ($page) { Page::updateOrCreate($page); }); } + + /** + * Store default VueFileManager settings into database + * + * @param $license + */ + public function seed_default_settings($license) + { + collect(config('content.content.' . strtolower($license))) + ->each(function ($content) { + Setting::forceCreate($content); + }); + } } \ No newline at end of file diff --git a/config/content.php b/config/content.php index ed44c14c..9486bc4d 100644 --- a/config/content.php +++ b/config/content.php @@ -1,7 +1,7 @@ [ + 'pages' => [ [ 'visibility' => 0, 'title' => 'Terms of Service', @@ -21,152 +21,154 @@ return [ 'content' => 'Metus penatibus ligula dolor natoque non habitasse laoreet facilisis, libero vivamus eget semper vulputate interdum integer, phasellus lorem enim blandit consectetur nullam sollicitudin. Hendrerit interdum luctus ut in molestie himenaeos eros cum laoreet parturient est, eu lectus hac et netus viverra dictumst congue elit sem senectus litora, fames scelerisque adipiscing inceptos fringilla montes sociosqu suscipit auctor potenti. Elementum lacus vulputate viverra ac morbi ligula ipsum facilisi, sit eu imperdiet lacinia congue dis vitae.', ], ], - 'content_regular' => [ - [ - 'name' => 'section_features', - 'value' => 0, - ], - [ - 'name' => 'section_feature_boxes', - 'value' => 0, - ], - [ - 'name' => 'section_get_started', - 'value' => 0, - ], - [ - 'name' => 'header_title', - 'value' => 'Simple & Powerful Personal Cloud Storage', - ], - [ - 'name' => 'header_description', - 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', - ], - [ - 'name' => 'features_title', - 'value' => 'The Fastest Growing File Manager on the CodeCanyon Market', - ], - [ - 'name' => 'features_description', - 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', - ], - [ - 'name' => 'feature_title_1', - 'value' => 'Truly Freedom', - ], - [ - 'name' => 'feature_description_1', - 'value' => 'You have full control over VueFileManager, no third authorities will control your service or usage, only you.', - ], - [ - 'name' => 'feature_title_2', - 'value' => 'The Sky is the Limit', - ], - [ - 'name' => 'feature_description_2', - 'value' => 'VueFileManager is cloud storage software. You have to install and running application on your own server hosting.', - ], - [ - 'name' => 'feature_title_3', - 'value' => 'No Monthly Fees', - ], - [ - 'name' => 'feature_description_3', - 'value' => 'When you running VueFileManager on your own server hosting, anybody can\'t control your content or resell your user data. Your data is safe.', - ], - [ - 'name' => 'get_started_title', - 'value' => 'Ready to Get Started
With Us?', - ], - [ - 'name' => 'get_started_description', - 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', - ], - [ - 'name' => 'footer_content', - 'value' => '© 2021 Simple & Powerful Personal Cloud Storage. Developed by Hi5Ve.Digital', - ], - [ - 'name' => 'allow_homepage', - 'value' => 1, - ], - ], - 'content_extended' => [ - [ - 'name' => 'section_features', - 'value' => '1', - ], - [ - 'name' => 'section_feature_boxes', - 'value' => '1', - ], - [ - 'name' => 'section_pricing_content', - 'value' => '1', - ], - [ - 'name' => 'section_get_started', - 'value' => '1', - ], - [ - 'name' => 'header_title', - 'value' => 'Simple & Powerful Personal Cloud Storage', - ], - [ - 'name' => 'header_description', - 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', - ], - [ - 'name' => 'features_title', - 'value' => 'The Fastest Growing File Manager on the CodeCanyon Market', - ], - [ - 'name' => 'features_description', - 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', - ], - [ - 'name' => 'feature_title_1', - 'value' => 'Truly Freedom', - ], - [ - 'name' => 'feature_description_1', - 'value' => 'You have full control over VueFileManager, no third authorities will control your service or usage, only you.', - ], - [ - 'name' => 'feature_title_2', - 'value' => 'The Sky is the Limit', - ], - [ - 'name' => 'feature_description_2', - 'value' => 'VueFileManager is cloud storage software. You have to install and running application on your own server hosting.', - ], - [ - 'name' => 'feature_title_3', - 'value' => 'No Monthly Fees', - ], - [ - 'name' => 'feature_description_3', - 'value' => 'When you running VueFileManager on your own server hosting, anybody can\'t control your content or resell your user data. Your data is safe.', - ], - [ - 'name' => 'pricing_title', - 'value' => 'Pick the Best Plan For Your Needs', - ], - [ - 'name' => 'pricing_description', - 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', - ], - [ - 'name' => 'get_started_title', - 'value' => 'Ready to Get Started
With Us?', - ], - [ - 'name' => 'get_started_description', - 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', - ], - [ - 'name' => 'footer_content', - 'value' => '© 2021 Simple & Powerful Personal Cloud Storage. Developed by Hi5Ve.Digital', + 'content' => [ + 'regular' => [ + [ + 'name' => 'section_features', + 'value' => 0, + ], + [ + 'name' => 'section_feature_boxes', + 'value' => 0, + ], + [ + 'name' => 'section_get_started', + 'value' => 0, + ], + [ + 'name' => 'header_title', + 'value' => 'Simple & Powerful Personal Cloud Storage', + ], + [ + 'name' => 'header_description', + 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', + ], + [ + 'name' => 'features_title', + 'value' => 'The Fastest Growing File Manager on the CodeCanyon Market', + ], + [ + 'name' => 'features_description', + 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', + ], + [ + 'name' => 'feature_title_1', + 'value' => 'Truly Freedom', + ], + [ + 'name' => 'feature_description_1', + 'value' => 'You have full control over VueFileManager, no third authorities will control your service or usage, only you.', + ], + [ + 'name' => 'feature_title_2', + 'value' => 'The Sky is the Limit', + ], + [ + 'name' => 'feature_description_2', + 'value' => 'VueFileManager is cloud storage software. You have to install and running application on your own server hosting.', + ], + [ + 'name' => 'feature_title_3', + 'value' => 'No Monthly Fees', + ], + [ + 'name' => 'feature_description_3', + 'value' => 'When you running VueFileManager on your own server hosting, anybody can\'t control your content or resell your user data. Your data is safe.', + ], + [ + 'name' => 'get_started_title', + 'value' => 'Ready to Get Started
With Us?', + ], + [ + 'name' => 'get_started_description', + 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', + ], + [ + 'name' => 'footer_content', + 'value' => '© 2021 Simple & Powerful Personal Cloud Storage. Developed by Hi5Ve.Digital', + ], + [ + 'name' => 'allow_homepage', + 'value' => 1, + ], ], + 'extended' => [ + [ + 'name' => 'section_features', + 'value' => '1', + ], + [ + 'name' => 'section_feature_boxes', + 'value' => '1', + ], + [ + 'name' => 'section_pricing_content', + 'value' => '1', + ], + [ + 'name' => 'section_get_started', + 'value' => '1', + ], + [ + 'name' => 'header_title', + 'value' => 'Simple & Powerful Personal Cloud Storage', + ], + [ + 'name' => 'header_description', + 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', + ], + [ + 'name' => 'features_title', + 'value' => 'The Fastest Growing File Manager on the CodeCanyon Market', + ], + [ + 'name' => 'features_description', + 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', + ], + [ + 'name' => 'feature_title_1', + 'value' => 'Truly Freedom', + ], + [ + 'name' => 'feature_description_1', + 'value' => 'You have full control over VueFileManager, no third authorities will control your service or usage, only you.', + ], + [ + 'name' => 'feature_title_2', + 'value' => 'The Sky is the Limit', + ], + [ + 'name' => 'feature_description_2', + 'value' => 'VueFileManager is cloud storage software. You have to install and running application on your own server hosting.', + ], + [ + 'name' => 'feature_title_3', + 'value' => 'No Monthly Fees', + ], + [ + 'name' => 'feature_description_3', + 'value' => 'When you running VueFileManager on your own server hosting, anybody can\'t control your content or resell your user data. Your data is safe.', + ], + [ + 'name' => 'pricing_title', + 'value' => 'Pick the Best Plan For Your Needs', + ], + [ + 'name' => 'pricing_description', + 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', + ], + [ + 'name' => 'get_started_title', + 'value' => 'Ready to Get Started
With Us?', + ], + [ + 'name' => 'get_started_description', + 'value' => 'Your private cloud storage software build on Laravel & Vue.js. No limits & no monthly fees. Truly freedom.', + ], + [ + 'name' => 'footer_content', + 'value' => '© 2021 Simple & Powerful Personal Cloud Storage. Developed by Hi5Ve.Digital', + ], + ] ], ]; \ No newline at end of file diff --git a/database/migrations/2020_06_25_142635_create_settings_table.php b/database/migrations/2020_06_25_142635_create_settings_table.php index 44fb4ba8..0361f03f 100644 --- a/database/migrations/2020_06_25_142635_create_settings_table.php +++ b/database/migrations/2020_06_25_142635_create_settings_table.php @@ -14,7 +14,7 @@ class CreateSettingsTable extends Migration public function up() { Schema::create('settings', function (Blueprint $table) { - $table->string('name')->unique(); + $table->string('name')->unique()->primary(); $table->longText('value')->nullable(); }); } diff --git a/tests/Feature/AdminTest.php b/tests/Feature/AdminTest.php index aef30a29..167b829a 100644 --- a/tests/Feature/AdminTest.php +++ b/tests/Feature/AdminTest.php @@ -42,7 +42,7 @@ class AdminTest extends TestCase ->count(2) ->create(['filesize' => 1000000]); - Setting::create([ + Setting::forceCreate([ 'name' => 'license', 'value' => 'Regular' ]); @@ -442,7 +442,7 @@ class AdminTest extends TestCase */ public function it_get_all_pages() { - $this->setup->seed_pages(); + $this->setup->seed_default_pages(); collect(['terms-of-service', 'privacy-policy', 'cookie-policy']) ->each(function ($slug) { @@ -459,7 +459,7 @@ class AdminTest extends TestCase */ public function it_get_page() { - $this->setup->seed_pages(); + $this->setup->seed_default_pages(); $this->getJson('/api/admin/pages/terms-of-service') ->assertStatus(200) @@ -473,7 +473,7 @@ class AdminTest extends TestCase */ public function it_update_page() { - $this->setup->seed_pages(); + $this->setup->seed_default_pages(); $this->patchJson('/api/admin/pages/terms-of-service', [ 'name' => 'title', @@ -484,4 +484,52 @@ class AdminTest extends TestCase 'title' => 'New Title' ]); } + + /** + * @test + */ + public function it_update_settings() + { + $this->setup->seed_default_settings('Extended'); + + $this->patchJson('/api/admin/settings', [ + 'name' => 'header_title', + 'value' => 'New Header Title' + ])->assertStatus(204); + + $this->assertDatabaseHas('settings', [ + 'value' => 'New Header Title' + ]); + } + + /** + * @test + */ + public function it_update_settings_image() + { + Storage::fake('local'); + + $this->setup->create_directories(); + + Setting::forceCreate([ + 'name' => 'app_logo', + 'value' => null, + ]); + + $logo = UploadedFile::fake() + ->image('fake-image.jpg'); + + $this->patchJson('/api/admin/settings', [ + 'name' => 'app_logo', + 'app_logo' => $logo + ])->assertStatus(204); + + $this->assertDatabaseMissing('settings', [ + 'app_logo' => null + ]); + + Storage::assertExists( + get_setting('app_logo') + ); + } }