5 changed files with 167 additions and 41 deletions
-
55app/Helpers/functions.php
-
27resources/views/users/edit.blade.php
-
6resources/views/users/partials/profile.blade.php
-
5resources/views/users/search.blade.php
-
115tests/Unit/Helpers/UserPhotoHelperTest.php
@ -1,31 +1,40 @@ |
|||
<?php |
|||
|
|||
use App\User; |
|||
|
|||
function formatSizeUnits($bytes) |
|||
{ |
|||
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 |
|||
{ |
|||
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; |
|||
} |
|||
} |
|||
|
|||
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'); |
|||
} |
|||
@ -0,0 +1,115 @@ |
|||
<?php |
|||
|
|||
namespace Tests\Unit\Helpers; |
|||
|
|||
use App\User; |
|||
use Tests\TestCase; |
|||
use Illuminate\Foundation\Testing\DatabaseMigrations; |
|||
|
|||
class UserPhotoHelperTest extends TestCase |
|||
{ |
|||
use DatabaseMigrations; |
|||
|
|||
/** @test */ |
|||
public function user_photo_path_function_exists() |
|||
{ |
|||
$this->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 = '<img'; |
|||
$imageString .= ' src="'.asset($photoFile).'"'; |
|||
$imageString .= '>'; |
|||
|
|||
$this->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 = '<img'; |
|||
$imageString .= ' src="'.asset('storage/'.$photoPath).'"'; |
|||
$imageString .= '>'; |
|||
|
|||
$this->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 = '<img'; |
|||
$imageString .= ' src="'.asset($photoFile).'"'; |
|||
$imageString .= ' class="123"'; |
|||
$imageString .= ' style="display: inline"'; |
|||
$imageString .= '>'; |
|||
|
|||
$overrides = [ |
|||
'class' => '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 = '<img'; |
|||
$imageString .= ' src="'.asset($photoFile).'"'; |
|||
$imageString .= '>'; |
|||
|
|||
$this->assertEquals($imageString, userPhoto($user)); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue