diff --git a/app/Helpers/functions.php b/app/Helpers/functions.php index 5986646..5af7003 100644 --- a/app/Helpers/functions.php +++ b/app/Helpers/functions.php @@ -1,31 +1,40 @@ = 1073741824) - { - $bytes = number_format($bytes / 1073741824, 2) . ' GB'; - } - elseif ($bytes >= 1048576) - { - $bytes = number_format($bytes / 1048576, 2) . ' MB'; - } - elseif ($bytes >= 1024) - { - $bytes = number_format($bytes / 1024, 2) . ' KB'; - } - elseif ($bytes > 1) - { - $bytes = $bytes . ' bytes'; - } - elseif ($bytes == 1) - { - $bytes = $bytes . ' byte'; - } - else - { + if ($bytes >= 1073741824) { + $bytes = number_format($bytes / 1073741824, 2).' GB'; + } elseif ($bytes >= 1048576) { + $bytes = number_format($bytes / 1048576, 2).' MB'; + } elseif ($bytes >= 1024) { + $bytes = number_format($bytes / 1024, 2).' KB'; + } elseif ($bytes > 1) { + $bytes = $bytes.' bytes'; + } elseif ($bytes == 1) { + $bytes = $bytes.' byte'; + } else { $bytes = '0 bytes'; } return $bytes; -} \ No newline at end of file +} + +function userPhoto(User $user, $attributes = []) +{ + return Html::image( + userPhotoPath($user->photo_path, $user->gender_id), + null, + $attributes + ); +} + +function userPhotoPath($photoPath, $genderId) +{ + if (is_file(public_path('storage/'.$photoPath))) { + return asset('storage/'.$photoPath); + } + + return asset('images/icon_user_'.$genderId.'.png'); +} diff --git a/resources/views/users/edit.blade.php b/resources/views/users/edit.blade.php index 19c1af2..22f39c9 100644 --- a/resources/views/users/edit.blade.php +++ b/resources/views/users/edit.blade.php @@ -13,13 +13,6 @@

{{ trans('user.edit') }}

-
- @if ($user->photo_path && is_file(public_path('storage/'.$user->photo_path))) - {{ Html::image('storage/'.$user->photo_path, $user->name, ['style' => 'max-width:100%']) }} - @else - {{ Html::image('images/icon_user_'.$user->gender_id.'.png', $user->name, ['style' => 'max-width:100%']) }} - @endif -
{!! FormField::text('name', ['label' => trans('user.name')]) !!} {!! FormField::text('nickname', ['label' => trans('user.nickname')]) !!}
@@ -56,11 +49,21 @@
{{ Form::close() }}
- {{ Form::open(['route' => ['users.photo-upload', $user], 'method' => 'patch', 'files' => true]) }} - {!! FormField::file('photo', ['required' => true, 'label' => __('user.reupload_photo'), 'info' => ['text' => 'Format jpg, maks: 200 Kb.', 'class' => 'warning']]) !!} - {!! Form::submit(__('user.update_photo'), ['class' => 'btn btn-success']) !!} - {{ link_to_route('users.show', trans('app.cancel'), [$user], ['class' => 'btn btn-default']) }} - {{ Form::close() }} +
+

{{ __('user.update_photo') }}

+ {{ Form::open(['route' => ['users.photo-upload', $user], 'method' => 'patch', 'files' => true]) }} +
+ {{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }} +
+
+ {!! FormField::file('photo', ['required' => true, 'label' => __('user.reupload_photo'), 'info' => ['text' => 'Format jpg, maks: 200 Kb.', 'class' => 'warning']]) !!} +
+ + {{ Form::close() }} +
@endsection diff --git a/resources/views/users/partials/profile.blade.php b/resources/views/users/partials/profile.blade.php index 462fa20..c39e9b8 100644 --- a/resources/views/users/partials/profile.blade.php +++ b/resources/views/users/partials/profile.blade.php @@ -1,11 +1,7 @@

{{ trans('user.profile') }}

- @if ($user->photo_path && is_file(public_path('storage/'.$user->photo_path))) - {{ Html::image('storage/'.$user->photo_path, $user->name, ['style' => 'max-width:100%']) }} - @else - {{ Html::image('images/icon_user_'.$user->gender_id.'.png', $user->name, ['style' => 'max-width:100%']) }} - @endif + {{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }}
diff --git a/resources/views/users/search.blade.php b/resources/views/users/search.blade.php index 2f167be..eb139e7 100644 --- a/resources/views/users/search.blade.php +++ b/resources/views/users/search.blade.php @@ -27,8 +27,11 @@ @foreach ($chunkedUser as $user)
-

{{ $user->profileLink() }} ({{ $user->gender }})

+
+ {{ userPhoto($user, ['style' => 'width:100%;max-width:300px']) }} +
+

{{ $user->profileLink() }} ({{ $user->gender }})

{{ trans('user.nickname') }} : {{ $user->nickname }}

{{ trans('user.father') }} : {{ $user->father_id ? $user->father->name : '' }}
diff --git a/tests/Unit/Helpers/UserPhotoHelperTest.php b/tests/Unit/Helpers/UserPhotoHelperTest.php new file mode 100644 index 0000000..c370ac6 --- /dev/null +++ b/tests/Unit/Helpers/UserPhotoHelperTest.php @@ -0,0 +1,115 @@ +assertTrue(function_exists('userPhotoPath')); + } + + /** @test */ + public function user_photo_path_function_returns_default_photo_path_based_on_gender_if_photo_path_is_null() + { + $genderId = 1; // Male + $this->assertEquals( + asset('images/icon_user_1.png'), userPhotoPath(null, $genderId) + ); + + $genderId = 2; // Female + $this->assertEquals( + asset('images/icon_user_2.png'), userPhotoPath(null, $genderId) + ); + } + /** @test */ + public function user_photo_function_exists() + { + $this->assertTrue(function_exists('userPhoto')); + } + + /** @test */ + public function user_photo_function_returns_default_image_photo_element_if_no_agency_image_path_setting() + { + $user = factory(User::class)->create(['gender_id' => 1]); + + $photoFile = 'images/icon_user_1.png'; + + $imageString = 'assertEquals($imageString, userPhoto($user)); + } + + /** @test */ + public function user_photo_function_returns_correct_photo_element_based_on_user_photo_path() + { + $photoPath = 'images/user_photo_path.jpg'; + + copy(public_path('images/icon_user_1.png'), storage_path('app/public/images/user_photo_path.jpg')); + + $this->assertFileExists(storage_path('app/public/images/user_photo_path.jpg')); + + $user = factory(User::class)->create([ + 'gender_id' => 2, + 'photo_path' => $photoPath, + ]); + + $imageString = 'assertEquals($imageString, userPhoto($user)); + + $this->assertFileExists(storage_path('app/public/images/user_photo_path.jpg')); + unlink(storage_path('app/public/images/user_photo_path.jpg')); + $this->assertFileNotExists(storage_path('app/public/images/user_photo_path.jpg')); + } + + /** @test */ + public function user_photo_function_has_overrideable_attributes() + { + $user = factory(User::class)->create([ + 'gender_id' => 1, + ]); + + $photoFile = 'images/icon_user_1.png'; + + $imageString = ' '123', + 'style' => 'display: inline', + ]; + $this->assertEquals($imageString, userPhoto($user, $overrides)); + } + + /** @test */ + public function user_photo_function_returns_default_gender_logo_image_if_user_photo_file_doesnt_exists() + { + $user = factory(User::class)->create([ + 'gender_id' => 2, + 'photo_path' => 'images/non_exists_photo_path.jpg', + ]); + + $photoFile = 'images/icon_user_2.png'; + + $imageString = 'assertEquals($imageString, userPhoto($user)); + } +}