Commit 9fed1f49 authored by bvipul's avatar bvipul

Settings and Page Tests

parent 21af3cb7
......@@ -45,27 +45,9 @@ class SettingsController extends Controller
public function update(Setting $setting, UpdateSettingsRequest $request)
{
$this->settings->update($setting, $request->except(['_token', '_method']));
return redirect()
->route('admin.settings.edit', $setting->id)
->with('flash_success', trans('alerts.backend.settings.updated'));
}
/**
* @param Setting $setting
* @param Request $request
* Remove logo or favicon icon
*
* @return mixed
*/
public function removeIcon(Request $request)
{
$this->settings->removeicon($request->data);
return json_encode(
[
'status' => true,
]
);
}
}
<?php
namespace App\Http\Controllers\Backend\Settings;
use App\Http\Controllers\Controller;
use App\Models\Settings\Setting;
use App\Repositories\Backend\Settings\SettingsRepository;
use Illuminate\Http\Request;
/**
* Class SettingsLogoController.
*/
class SettingsLogoController extends Controller
{
protected $settings;
/**
* @param \App\Repositories\Backend\Settings\SettingsRepository $settings
*/
public function __construct(SettingsRepository $settings)
{
$this->settings = $settings;
}
/**
* Remove logo or favicon icon
*
* @param \App\Models\Settings\Setting $setting
* @param \Illuminate\Http\Request $request
*
* @return mixed
*/
public function destroy(Setting $setting, Request $request)
{
$this->settings->removeLogo($setting, $request->data);
return json_encode([
'status' => true
]);
}
}
......@@ -132,7 +132,7 @@ class BlogsRepository extends BaseRepository
/**
* Creating Tags.
*
* @param Array($tags)
* @param Array $tags
*
* @return array
*/
......
......@@ -2,9 +2,10 @@
namespace App\Repositories\Backend\Settings;
use App\Exceptions\GeneralException;
use App\Models\Settings\Setting;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;
use Illuminate\Support\Facades\Storage;
/**
* Class SettingsRepository.
......@@ -16,29 +17,61 @@ class SettingsRepository extends BaseRepository
*/
const MODEL = Setting::class;
/**
* Site Logo Path
*
* @var string
*/
protected $site_logo_path;
/**
* Favicon path
*
* @var string
*/
protected $favicon_path;
/**
* Storage Class Object
*
* @var \Illuminate\Support\Facades\Storage
*/
protected $storage;
/**
* Constructor
*/
public function __construct()
{
$this->site_logo_path = 'img'. DIRECTORY_SEPARATOR . 'logo' . DIRECTORY_SEPARATOR;
$this->favicon_path = 'img' . DIRECTORY_SEPARATOR . 'favicon' . DIRECTORY_SEPARATOR;
$this->storage = Storage::disk('public');
}
/**
* @param \App\Models\Settings\Setting $setting
* @param $input
* @param Array $input
*
* @throws \App\Exceptions\GeneralException
*
* return bool
* @return bool
*/
public function update(Setting $setting, array $input)
{
if (isset($input['logo'])) {
$image_upload = $this->uploadlogoimage($setting, $input['logo']);
$input['logo'] = $image_upload;
if (! empty($input['logo'])) {
$this->removeLogo($setting, 'logo');
$input['logo'] = $this->uploadLogo($setting, $input['logo'], 'logo');
}
if (isset($input['favicon'])) {
$image_upload = $this->uploadfaviconimage($setting, $input['favicon']);
$input['favicon'] = $image_upload;
if (! empty($input['favicon'])) {
$this->removeLogo($setting, 'favicon');
$input['favicon'] = $this->uploadLogo($setting, $input['favicon'], 'favicon');
}
if ($setting->update($input)) {
if ($setting->update($input))
return true;
}
throw new GeneralException(trans('exceptions.backend.settings.update_error'));
}
......@@ -46,63 +79,32 @@ class SettingsRepository extends BaseRepository
/*
* Upload logo image
*/
public function uploadlogoimage($setting, $logo)
public function uploadLogo($setting, $logo, $type)
{
$image_name_ex = $logo->getClientOriginalExtension();
$path = $type == "logo" ? $this->site_logo_path : $this->favicon_path;
if ($setting->logo) {
if (file_exists(public_path().'/img/site_logo/'.$setting->logo)) {
unlink('img/site_logo/'.$setting->logo);
}
}
$image_name = time() . $logo->getClientOriginalName();
$image_name = time().$logo->getClientOriginalName();
$destinationPath = public_path('img/site_logo');
$logo->move($destinationPath, $image_name);
$this->storage->put($path . $image_name, file_get_contents($logo->getRealPath()));
return $image_name;
}
/*
* Upload favicon icon image
* remove logo or favicon icon
*/
public function uploadfaviconimage($setting, $logo)
public function removeLogo(Setting $setting, $type)
{
$image_name_ex = $logo->getClientOriginalExtension();
$path = $type == "logo" ? $this->site_logo_path : $this->favicon_path;
if ($setting->favicon) {
if (file_exists(public_path().'/img/favicon_icon/'.$setting->favicon)) {
unlink('img/favicon_icon/'.$setting->favicon);
}
if ($setting->$type && $this->storage->exists($path . $setting->$type)) {
$this->storage->delete($path . $setting->$type);
}
$image_name = time().$logo->getClientOriginalName();
$destinationPath = public_path('/img/favicon_icon');
$logo->move($destinationPath, $image_name);
$result = $setting->update([ $type => null ]);
return $image_name;
}
if($result) return true;
/*
* remove logo or favicon icon
*/
public function removeicon($input)
{
$setting = $this->query()->get();
if ($input == 'logo') {
if ($setting[0]->logo) {
if (file_exists(public_path().'/img/site_logo/'.$setting[0]->logo)) {
unlink('img/site_logo/'.$setting[0]->logo);
}
$this->query()->update(['logo' => null]);
}
} else {
if ($setting[0]->favicon) {
if (file_exists(public_path().'/img/favicon_icon/'.$setting[0]->favicon)) {
unlink('img/favicon_icon/'.$setting[0]->favicon);
}
}
$this->query()->update(['favicon' => null]);
}
throw new GeneralException(trans('exceptions.backend.settings.update_error'));
}
}
......@@ -201,7 +201,7 @@ return [
'companydetails' => 'Company Contact Details',
'mail' => 'Mail Settings',
'footer' => 'Footer Settings',
'terms' => 'Terms & Condition Settings',
'terms' => 'Terms and Condition Settings',
'google' => 'Google Analytics Track Code',
],
......
......@@ -6,5 +6,5 @@
Route::group(['namespace' => 'Settings'], function () {
Route::resource('settings', 'SettingsController', ['except' => ['show', 'create', 'save', 'index', 'destroy']]);
Route::post('removeicon', 'SettingsController@removeIcon')->name('removeicon');
Route::post('removeicon/{setting}', 'SettingsLogoController@destroy')->name('removeIcon');
});
<?php
namespace Tests\Feature\Backend;
use Tests\TestCase;
use App\Models\Settings\Setting;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ManageSettingsTest extends TestCase
{
protected $setting;
public function setUp()
{
parent::setUp();
$this->setting = Setting::find(1);
$this->actingAs($this->admin);
}
/** @test */
public function setting_page_shows_different_tabs()
{
$this->get(route('admin.settings.edit', $this->setting))
->assertSee(__('labels.backend.settings.seo'))
->assertSee(__('labels.backend.settings.companydetails'))
->assertSee(__('labels.backend.settings.mail'))
->assertSee(__('labels.backend.settings.footer'))
->assertSee(__('labels.backend.settings.terms'))
->assertSee(__('labels.backend.settings.google'));
}
/** @test */
public function it_can_update_a_valid_site_logo()
{
$this->patch(route('admin.settings.update', $this->setting), [
'logo' => UploadedFile::fake()->image('logo.jpg', 226, 48)
]);
Storage::disk('public')->assertExists('img/logo/' . $this->setting->logo);
}
/** @test */
public function it_throws_error_for_valid_site_logo()
{
$this->withExceptionHandling();
$this->patch(route('admin.settings.update', $this->setting), [
'logo' => UploadedFile::fake()->image('logo.jpg', 200, 500)
])
->assertSessionHasErrors('logo');
}
/** @test */
public function it_can_update_site_favicon()
{
$this->patch(route('admin.settings.update', $this->setting), [
'favicon' => UploadedFile::fake()->image('favicon.jpg', 16, 16)
]);
Storage::disk('public')->assertExists('img/favicon/' . $this->setting->favicon);
}
/** @test */
public function it_throws_error_for_valid_site_favicon()
{
$this->withExceptionHandling();
$this->patch(route('admin.settings.update', $this->setting), [
'favicon' => UploadedFile::fake()->image('favicon.jpg', 200, 500)
])
->assertSessionHasErrors('favicon');
}
/** @test */
public function a_user_can_update_settings()
{
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment