write test for another items and change some part of the code
This commit is contained in:
@@ -8,7 +8,6 @@ use App\Enums\CallEvents;
|
||||
use App\Services\Notification\NotificationService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Mockery\MockInterface;
|
||||
use Tests\TestCase;
|
||||
|
||||
|
||||
41
tests/Feature/Category/ListTest.php
Normal file
41
tests/Feature/Category/ListTest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Category;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ListTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
|
||||
public function test_guest_user_cannot_access_list(): void
|
||||
{
|
||||
$response = $this->getJson(route('categories.list'));
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
public function test_user_can_see_list(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$response = $this->actingAs($user)->getJson(route('categories.list'));
|
||||
|
||||
Category::factory()->count(3)->create();
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonStructure([
|
||||
'data' => [
|
||||
'*' => [
|
||||
'id',
|
||||
'category_id',
|
||||
'category_name',
|
||||
'subcategory_id',
|
||||
'subcategory_name'
|
||||
],
|
||||
]
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,20 @@ class DeleteTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
|
||||
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$permission = Permission::factory()->create();
|
||||
$response = $this->getJson(route('permissions.destroy',[$permission->id]));
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_authenticated_user_can_delete_permission(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->deleteJson(route('permissions.destroy', $permission));
|
||||
$response = $this->actingAs($user)->deleteJson(route('permissions.destroy', [$permission->id]));
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
@@ -33,7 +41,7 @@ class DeleteTest extends TestCase
|
||||
{
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$response = $this->deleteJson(route('permissions.destroy', $permission));
|
||||
$response = $this->deleteJson(route('permissions.destroy', [$permission->id]));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
@@ -12,21 +12,16 @@ class IndexTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
|
||||
public function test_unauthenticated_user_cannot_access_permission_index(): void
|
||||
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$response = $this->getJson(route('permissions.index'));
|
||||
|
||||
$response->assertStatus(401);
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_all_permissions_returned_successfully(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory()->state([
|
||||
'name' => 'permissions',
|
||||
'name_fa' => 'مدیریت دسترسی',
|
||||
'guard_name' => 'web',
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('permissions.index'));
|
||||
|
||||
@@ -13,11 +13,10 @@ class ListTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
|
||||
public function test_unauthenticated_user_cannot_access_permission_list(): void
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$response = $this->getJson(route('permissions.list'));
|
||||
|
||||
$response->assertStatus(401);
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,11 +30,6 @@ class ListTest extends TestCase
|
||||
public function test_permissions_list_returns_correct_structure(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory()->state([
|
||||
'name' => 'manage-permissions',
|
||||
'name_fa' => 'مدیریت دسترسیها',
|
||||
'guard_name' => 'web',
|
||||
]))
|
||||
->create();
|
||||
|
||||
Permission::factory()->count(3)->create();
|
||||
|
||||
@@ -16,18 +16,13 @@ class SHowTest extends TestCase
|
||||
{
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$response = $this->getJson(route('permissions.show', $permission));
|
||||
$response = $this->getJson(route('permissions.show', [$permission->id]));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
public function test_permission_show_returns_correct_structure_and_data(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory()->state([
|
||||
'name' => 'manage-permissions',
|
||||
'name_fa' => 'مدیریت دسترسیها',
|
||||
'guard_name' => 'web',
|
||||
]))
|
||||
->create();
|
||||
|
||||
$permission = Permission::factory()->create([
|
||||
@@ -36,7 +31,7 @@ class SHowTest extends TestCase
|
||||
'guard_name' => 'web',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('permissions.show', $permission));
|
||||
$response = $this->actingAs($user)->getJson(route('permissions.show', [$permission->id]));
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
|
||||
@@ -45,7 +45,7 @@ class StoreTest extends TestCase
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.store'), [
|
||||
'name' => $duplicateName, // حالا واقعا تکراری هستند
|
||||
'name' => $duplicateName,
|
||||
'name_fa' => $duplicateNameFa,
|
||||
]);
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class UpdateTest extends TestCase
|
||||
{
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$response = $this->postJson(route('permissions.update', $permission));
|
||||
$response = $this->postJson(route('permissions.update', [$permission->id]));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class UpdateTest extends TestCase
|
||||
$user = User::factory()->create();
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', $permission));
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', [$permission->id]));
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
@@ -42,7 +42,7 @@ class UpdateTest extends TestCase
|
||||
$user = User::factory()->create();
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', $permission), [
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', [$permission->id]), [
|
||||
'name' =>$this->faker->randomDigit(),
|
||||
'name_fa' => $this->faker->randomDigit(),
|
||||
]);
|
||||
@@ -61,7 +61,7 @@ class UpdateTest extends TestCase
|
||||
$permission1 = Permission::factory()->create();
|
||||
$permission2 = Permission::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', $permission1), [
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', [$permission1->id]), [
|
||||
'name' => $permission2->name,
|
||||
'name_fa' => $permission2->name_fa,
|
||||
]);
|
||||
@@ -80,7 +80,7 @@ class UpdateTest extends TestCase
|
||||
|
||||
$longString = $this->faker->regexify('[a-zA-Z0-9]{256}');
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', $permission), [
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', [$permission->id]), [
|
||||
'name' => $longString,
|
||||
'name_fa' => $longString,
|
||||
]);
|
||||
@@ -102,7 +102,7 @@ class UpdateTest extends TestCase
|
||||
'name_fa' => $this->faker->unique()->words(2, true),
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', $permission), $payload);
|
||||
$response = $this->actingAs($user)->postJson(route('permissions.update', [$permission->id]), $payload);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
|
||||
32
tests/Feature/Province/ListTest.php
Normal file
32
tests/Feature/Province/ListTest.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Province;
|
||||
|
||||
use App\Models\Province;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ListTest extends TestCase
|
||||
{
|
||||
use refreshDatabase, WithFaker;
|
||||
|
||||
public function test_user_can_view_list():void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$response = $this->actingAs($user)->getJson(route('provinces.list'));
|
||||
|
||||
Province::factory()->count(3)->create();
|
||||
$response->assertOk();
|
||||
$response->assertJsonStructure([
|
||||
'data' => [
|
||||
'*' => [
|
||||
'id',
|
||||
'name'
|
||||
],
|
||||
]
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -12,11 +12,11 @@ class IndexTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
|
||||
public function test_the_user_is_not_authenticated(): void
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$response = $this->getJson(route('roles.index'));
|
||||
|
||||
$response->assertStatus(401);
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_authenticated_user_can_see_roles_index(): void
|
||||
|
||||
@@ -12,7 +12,7 @@ use Tests\TestCase;
|
||||
class ShowTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
public function test_guest_cannot_view_role(): void
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$role = Role::factory()->create();
|
||||
$response = $this->postJson(route('roles.show',[$role->id]));
|
||||
|
||||
@@ -13,7 +13,7 @@ class StoreTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
|
||||
public function test_the_user_is_not_authenticated()
|
||||
public function test_unauthenticated_user_cannot_access()
|
||||
{
|
||||
$response = $this->postJson(route('roles.store'));
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ class UpdateTest extends TestCase
|
||||
public function test_the_user_is_not_authenticated()
|
||||
{
|
||||
$role = Role::factory()->create();
|
||||
$response = $this->postJson(route('roles.update',$role));
|
||||
$response = $this->postJson(route('roles.update',[$role->id]));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
@@ -23,8 +23,10 @@ class UpdateTest extends TestCase
|
||||
public function test_fields_are_required(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$role = Role::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('roles.store'));
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('roles.update',[$role->id]));
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
@@ -37,6 +39,8 @@ class UpdateTest extends TestCase
|
||||
public function test_fields_must_be_unique(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$role = Role::factory()->create();
|
||||
|
||||
|
||||
$duplicateName = $this->faker->unique()->slug;
|
||||
$duplicateNameFa = $this->faker->unique()->word;
|
||||
@@ -47,7 +51,7 @@ class UpdateTest extends TestCase
|
||||
'guard_name' => 'web',
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('roles.store'), [
|
||||
$response = $this->actingAs($user)->postJson(route('roles.update',[$role->id]), [
|
||||
'name' => $duplicateName,
|
||||
'name_fa' => $duplicateNameFa,
|
||||
]);
|
||||
@@ -61,10 +65,12 @@ class UpdateTest extends TestCase
|
||||
|
||||
public function test_name_and_name_fa_must_be_strings(): void
|
||||
{
|
||||
$role = Role::factory()->create();
|
||||
|
||||
$user = User::factory()->create();
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('roles.store'), [
|
||||
$response = $this->actingAs($user)->postJson(route('roles.update',[$role->id]), [
|
||||
'name' => 123,
|
||||
'name_fa' => 456,
|
||||
'permissions' => [$permission->id],
|
||||
@@ -79,11 +85,13 @@ class UpdateTest extends TestCase
|
||||
|
||||
public function test_name_and_name_fa_must_not_exceed_max_length(): void
|
||||
{
|
||||
$role = Role::factory()->create();
|
||||
|
||||
$user = User::factory()->create();
|
||||
$longString = str_repeat('a', 256);
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('roles.store'), [
|
||||
$response = $this->actingAs($user)->postJson(route('roles.update',[$role->id]), [
|
||||
'name' => $longString,
|
||||
'name_fa' => $longString,
|
||||
'permissions' => [$permission->id],
|
||||
@@ -99,8 +107,10 @@ class UpdateTest extends TestCase
|
||||
public function test_permissions_must_be_array(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$role = Role::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('roles.store'), [
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('roles.update',[$role->id]), [
|
||||
'name' => $this->faker->slug,
|
||||
'name_fa' => $this->faker->word,
|
||||
'permissions' => 1,
|
||||
@@ -130,6 +140,8 @@ class UpdateTest extends TestCase
|
||||
public function test_valid_role_is_update_successfully(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$role = Role::factory()->create();
|
||||
|
||||
$permission = Permission::factory()->create();
|
||||
|
||||
$payload = [
|
||||
@@ -138,7 +150,7 @@ class UpdateTest extends TestCase
|
||||
'permissions' => [$permission->id],
|
||||
];
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('roles.store'), $payload);
|
||||
$response = $this->actingAs($user)->postJson(route('roles.update',[$role->id]), $payload);
|
||||
|
||||
$response->assertOk()
|
||||
->assertExactJson([
|
||||
|
||||
61
tests/Feature/User/DeleteTest.php
Normal file
61
tests/Feature/User/DeleteTest.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\User;
|
||||
|
||||
use App\Models\Permission;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class DeleteTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$response = $this->getJson(route('users.destroy',[$user->id]));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
public function test_user_without_permission_cannot_destroy_users(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('users.destroy',[$user->id]));
|
||||
$response->assertForbidden();
|
||||
}
|
||||
public function test_superAdmin_cant_updated()
|
||||
{
|
||||
|
||||
$user = User::factory()->create([
|
||||
'username' => "witel",
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('users.destroy',[$user->id]));
|
||||
|
||||
$response->assertForbidden();
|
||||
|
||||
}
|
||||
public function test_can_delete_a_user()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(permission::factory([
|
||||
'name'=> 'manage_users',
|
||||
"name_fa" => 'مدیریت کاربران',
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->deleteJson(route('users.destroy', [$user->id]));
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertExactJson([
|
||||
'message' => __('messages.successful')
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('users', [
|
||||
'username' => $user->username
|
||||
]);
|
||||
}
|
||||
}
|
||||
59
tests/Feature/User/IndexTest.php
Normal file
59
tests/Feature/User/IndexTest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\User;
|
||||
|
||||
use App\Models\Permission;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class IndexTest extends TestCase
|
||||
{
|
||||
use refreshDatabase, WithFaker;
|
||||
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$response = $this->getJson(route('users.index'));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
public function test_user_without_permission_cannot_see_index(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$response = $this->actingAs($user)->getJson(route('users.index'));
|
||||
$response->assertForbidden();
|
||||
}
|
||||
public function test_all_users_returned_successfully_for_index_user(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(permission::factory([
|
||||
'name'=> 'manage_users',
|
||||
"name_fa" => 'مدیریت کاربران',
|
||||
]))
|
||||
->create();
|
||||
User::factory()->count(2)->create();
|
||||
$response = $this->actingAs($user)->getJson(route('users.index',[
|
||||
'filters'=> json_encode([]),
|
||||
'sortings'=> json_encode([]),
|
||||
'relations'=> ['roles:id,name_fa']
|
||||
]));
|
||||
$response->assertOk();
|
||||
$response->assertJsonStructure([
|
||||
'data' => [
|
||||
'*' => ['id',
|
||||
'full_name',
|
||||
'username',
|
||||
'province_id',
|
||||
'phone_number',
|
||||
'national_id',
|
||||
'telephone_id',
|
||||
],
|
||||
],
|
||||
'meta' => [
|
||||
'totalRowCount'
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
76
tests/Feature/User/ShowTest.php
Normal file
76
tests/Feature/User/ShowTest.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\User;
|
||||
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ShowTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase, WithFaker;
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$response = $this->getJson(route('users.show',[$user->id]));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
public function test_user_without_permission_cannot_show_users(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('users.show',[$user->id]));
|
||||
$response->assertForbidden();
|
||||
}
|
||||
public function test_superAdmin_cant_updated()
|
||||
{
|
||||
|
||||
$user = User::factory()->create([
|
||||
'username' => "witel",
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('users.show',[$user->id]));
|
||||
|
||||
$response->assertForbidden();
|
||||
|
||||
}
|
||||
public function test_user_with_permission_can_show_users(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$role = Role::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'test'
|
||||
]))->create();
|
||||
|
||||
$user->assignRole($role->id);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('users.show',[$user->id]));
|
||||
$response->assertJsonStructure([
|
||||
'data' => [
|
||||
'username',
|
||||
'full_name',
|
||||
'id',
|
||||
'gender',
|
||||
'email',
|
||||
'phone_number',
|
||||
'avatar',
|
||||
'national_id',
|
||||
'position',
|
||||
'province_id',
|
||||
'province_name',
|
||||
'role',
|
||||
'telephone_id'
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
508
tests/Feature/User/StoreTest.php
Normal file
508
tests/Feature/User/StoreTest.php
Normal file
@@ -0,0 +1,508 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\User;
|
||||
|
||||
use App\Models\Permission;
|
||||
use App\Models\Province;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Tests\TestCase;
|
||||
|
||||
class StoreTest extends TestCase
|
||||
{
|
||||
use refreshDatabase, WithFaker;
|
||||
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$response = $this->getJson(route('users.store'));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
public function test_user_without_permission_cannot_see_index(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$response = $this->actingAs($user)->getJson(route('users.store'));
|
||||
$response->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_fields_are_required()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'),[]);
|
||||
$response->assertUnprocessable()
|
||||
->assertInValid([
|
||||
'full_name' => __('validation.required', ['attribute' => 'نام کامل']),
|
||||
'username' => __('validation.required', ['attribute' => 'نام کاربری']),
|
||||
'position' => __('validation.required', ['attribute' => 'سمت']),
|
||||
'gender' => __('validation.required', ['attribute' => 'جنسیت']),
|
||||
'national_id' => __('validation.required', ['attribute' => 'کدملی']),
|
||||
'phone_number' => __('validation.required', ['attribute' => 'شماره تلفن']),
|
||||
'province_id' => __('validation.required', ['attribute' => 'استان']),
|
||||
'password' => __('validation.required', ['attribute' => 'رمز عبور']),
|
||||
'role_id' => __('validation.required', ['attribute' => 'شناسه نقش ']),
|
||||
]);
|
||||
|
||||
}
|
||||
public function test_gender_should_be_male_or_female()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $this->faker->unique()->userName,
|
||||
'position' => $this->faker->jobTitle,
|
||||
'gender' => 'other',
|
||||
'national_id' => $this->faker->numerify('##########'),
|
||||
'phone_number' => $this->faker->numerify('09#########'),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'password' => $this->faker->regexify('[A-Za-z]{4}[0-9]{4}')
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'gender' => __('validation.in', ['attribute' => 'جنسیت'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_phone_number_should_match_regex()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '01' . $this->faker->numberBetween(100000000, 999999999),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'phone_number' => __('validation.regex', ['attribute' => 'شماره تلفن'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_password_should_match_the_regex()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->lexify('?????'),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'password' => __('validation.regex', ['attribute' => 'رمز عبور'])
|
||||
]);
|
||||
}
|
||||
public function test_fields_should_be_string()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->numberBetween(10, 20),
|
||||
'username' => $this->faker->numberBetween(10, 20),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'full_name' => __('validation.string', ['attribute' => 'نام کامل']),
|
||||
'username' => __('validation.string', ['attribute' => 'نام کاربری']),
|
||||
]);
|
||||
}
|
||||
public function test_avatar_should_be_less_than_2048()
|
||||
{
|
||||
$avatar = UploadedFile::fake()->image('file.png')->size('3000');
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'roles' => Role::factory()->create()->id,
|
||||
'avatar' => $avatar
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'avatar' => __('validation.max.file', ['attribute' => 'آواتار', 'max' => 2048])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_full_name_must_not_exceed_max_length():void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$longString = $this->faker->regexify('[a-zA-Z0-9]{256}');
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $longString,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
|
||||
]);
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'full_name' => __('validation.max.string', ['attribute' => 'نام کامل', 'max' => 255]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_fields_id_should_be_unique()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$existingPhone = '09' .$this->faker->numberBetween(100000000, 999999999);
|
||||
$existingEmail = $this->faker->email;
|
||||
$existingUser = $this->faker->name;
|
||||
$existingNational = $this->faker->numberBetween(1000000000, 9999999999);
|
||||
|
||||
User::factory()->create([
|
||||
'phone_number' => $existingPhone,
|
||||
'national_id' => $existingNational,
|
||||
'username' => $existingUser,
|
||||
'email' => $existingEmail,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' =>$existingUser,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' =>$existingNational,
|
||||
'phone_number' => $existingPhone,
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'email' =>$existingEmail,
|
||||
]);
|
||||
|
||||
$response->assertJsonValidationErrors([
|
||||
'phone_number' => __('validation.unique', ['attribute' => 'شماره تلفن']),
|
||||
'email' => __('validation.unique', ['attribute' => 'ایمیل']),
|
||||
'username' => __('validation.unique', ['attribute' => 'نام کاربری']),
|
||||
'national_id' => __('validation.unique', ['attribute' => 'کدملی']),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_password_should_have_minimum_8_characters(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $this->faker->unique()->userName,
|
||||
'position' => $this->faker->jobTitle,
|
||||
'gender' => $this->faker->randomElement(['male', 'female']),
|
||||
'national_id' => $this->faker->numerify('##########'),
|
||||
'phone_number' => $this->faker->numerify('09#########'),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => 'abc123',
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'email' => $this->faker->unique()->safeEmail,
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'password' => __('validation.min.string', ['attribute' => 'رمز عبور', 'min' => 8]),
|
||||
]);
|
||||
}
|
||||
public function test_national_id_should_be_10_digits()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(10000, 99999),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'national_id' => __('validation.digits', [
|
||||
'attribute' => 'کدملی',
|
||||
'digits' => 10,
|
||||
])
|
||||
]);
|
||||
}
|
||||
public function test_phone_number_should_be_11_digits()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(1000000, 9999999),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'phone_number' => __('validation.digits', [
|
||||
'attribute' => 'شماره تلفن',
|
||||
'digits' => 11
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_role_should_exist_in_roles_table()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$invalidRoleId = 9999;
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'role_id' => $invalidRoleId ,
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'role_id' => __('validation.exists', ['attribute' => 'شناسه نقش '])
|
||||
]);
|
||||
}
|
||||
public function test_province_id_should_be_exist_in_province_table()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$invalidProvince = $this->faker->numberBetween(100000000, 999999999);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [ 'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => $invalidProvince,
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'province_id' => __('validation.exists', ['attribute' => 'استان'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_email_should_have_valid_format(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $this->faker->unique()->userName,
|
||||
'position' => $this->faker->jobTitle,
|
||||
'gender' => $this->faker->randomElement(['male', 'female']),
|
||||
'national_id' => $this->faker->numerify('##########'),
|
||||
'phone_number' => $this->faker->numerify('09#########'),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => 'abc12345',
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'email' => 'not-an-email',
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'email' => __('validation.email', ['attribute' => 'ایمیل']),
|
||||
]);
|
||||
}
|
||||
public function test_avatar_should_be_valid_mime_type(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$invalidFile = UploadedFile::fake()->create('avatar.txt', 100, 'text/plain');
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $this->faker->unique()->userName,
|
||||
'position' => $this->faker->jobTitle,
|
||||
'gender' => $this->faker->randomElement(['male', 'female']),
|
||||
'national_id' => $this->faker->numerify('##########'),
|
||||
'phone_number' => $this->faker->numerify('09#########'),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->regexify('[A-Za-z]{4}[0-9]{4}'),
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'email' => $this->faker->unique()->safeEmail,
|
||||
'avatar' => $invalidFile,
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'avatar' => __('validation.mimes', ['attribute' => 'آواتار', 'values' => 'png, jpg, pdf']),
|
||||
]);
|
||||
}
|
||||
public function test_phone_number_must_be_numeric(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$invalidPhone = $this->faker->word();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $this->faker->unique()->userName,
|
||||
'position' => $this->faker->jobTitle,
|
||||
'gender' => $this->faker->randomElement(['male', 'female']),
|
||||
'national_id' => $this->faker->numerify('##########'),
|
||||
'phone_number' =>$invalidPhone ,
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => 'abc12345',
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'email' => 'not-an-email',
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'phone_number' => __('validation.numeric', ['attribute' => 'شماره تلفن']),
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function test_can_store_an_user()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$username = $this->faker->userName;
|
||||
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $username,
|
||||
'position' => $this->faker->lexify('?????'),
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'telephone_id' => '01-1'
|
||||
]);
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertExactJson([
|
||||
'message' => __('messages.successful')
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'username' => $username
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
430
tests/Feature/User/UpdateTest.php
Normal file
430
tests/Feature/User/UpdateTest.php
Normal file
@@ -0,0 +1,430 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\User;
|
||||
|
||||
use App\Models\Permission;
|
||||
use App\Models\Province;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UpdateTest extends TestCase
|
||||
{
|
||||
use refreshDatabase, WithFaker;
|
||||
|
||||
public function test_unauthenticated_user_cannot_access(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->create();
|
||||
$response = $this->getJson(route('users.update',[$user->id]));
|
||||
|
||||
$response->assertUnauthorized();
|
||||
}
|
||||
public function test_user_without_permission_cannot_see_index(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$response = $this->actingAs($user)->getJson(route('users.update',[$user->id]));
|
||||
$response->assertForbidden();
|
||||
}
|
||||
public function test_superAdmin_cant_updated()
|
||||
{
|
||||
|
||||
$user = User::factory()->create([
|
||||
'username' => "witel",
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->getJson(route('users.update',[$user->id]));
|
||||
|
||||
$response->assertForbidden();
|
||||
|
||||
}
|
||||
public function test_fields_are_required()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]),[]);
|
||||
$response->assertUnprocessable()
|
||||
->assertInValid([
|
||||
'full_name' => __('validation.required', ['attribute' => 'نام کامل']),
|
||||
'username' => __('validation.required', ['attribute' => 'نام کاربری']),
|
||||
'position' => __('validation.required', ['attribute' => 'سمت']),
|
||||
'gender' => __('validation.required', ['attribute' => 'جنسیت']),
|
||||
'national_id' => __('validation.required', ['attribute' => 'کدملی']),
|
||||
'phone_number' => __('validation.required', ['attribute' => 'شماره تلفن']),
|
||||
'province_id' => __('validation.required', ['attribute' => 'استان']),
|
||||
'role_id' => __('validation.required', ['attribute' => 'شناسه نقش ']),
|
||||
]);
|
||||
|
||||
}
|
||||
public function test_gender_should_be_male_or_female()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $this->faker->unique()->userName,
|
||||
'position' => $this->faker->jobTitle,
|
||||
'gender' => 'other',
|
||||
'national_id' => $this->faker->numerify('##########'),
|
||||
'phone_number' => $this->faker->numerify('09#########'),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'password' => $this->faker->regexify('[A-Za-z]{4}[0-9]{4}')
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'gender' => __('validation.in', ['attribute' => 'جنسیت'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_phone_number_should_match_regex()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '01' . $this->faker->numberBetween(100000000, 999999999),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'phone_number' => __('validation.regex', ['attribute' => 'شماره تلفن'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_fields_should_be_string()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->numberBetween(10, 20),
|
||||
'username' => $this->faker->numberBetween(10, 20),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'full_name' => __('validation.string', ['attribute' => 'نام کامل']),
|
||||
'username' => __('validation.string', ['attribute' => 'نام کاربری']),
|
||||
]);
|
||||
}
|
||||
public function test_avatar_should_be_less_than_2048()
|
||||
{
|
||||
$avatar = UploadedFile::fake()->image('file.png')->size('3000');
|
||||
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'roles' => Role::factory()->create()->id,
|
||||
'avatar' => $avatar
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'avatar' => __('validation.max.file', ['attribute' => 'آواتار', 'max' => 2048])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_full_name_must_not_exceed_max_length():void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$longString = $this->faker->regexify('[a-zA-Z0-9]{256}');
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $longString,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
|
||||
]);
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'full_name' => __('validation.max.string', ['attribute' => 'نام کامل', 'max' => 255]),
|
||||
]);
|
||||
}
|
||||
public function test_national_id_should_be_10_digits()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(10000, 99999),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'national_id' => __('validation.digits', [
|
||||
'attribute' => 'کدملی',
|
||||
'digits' => 10,
|
||||
])
|
||||
]);
|
||||
}
|
||||
public function test_phone_number_should_be_11_digits()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(1000000, 9999999),
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'phone_number' => __('validation.digits', [
|
||||
'attribute' => 'شماره تلفن',
|
||||
'digits' => 11
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_role_should_exist_in_roles_table()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$invalidRoleId = 9999;
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'role_id' => $invalidRoleId ,
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'role_id' => __('validation.exists', ['attribute' => 'شناسه نقش '])
|
||||
]);
|
||||
}
|
||||
public function test_province_id_should_be_exist_in_province_table()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$invalidProvince = $this->faker->numberBetween(100000000, 999999999);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [ 'full_name' => $this->faker->text,
|
||||
'username' => $this->faker->text,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => $invalidProvince,
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'province_id' => __('validation.exists', ['attribute' => 'استان'])
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_email_should_have_valid_format(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $this->faker->unique()->userName,
|
||||
'position' => $this->faker->jobTitle,
|
||||
'gender' => $this->faker->randomElement(['male', 'female']),
|
||||
'national_id' => $this->faker->numerify('##########'),
|
||||
'phone_number' => $this->faker->numerify('09#########'),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => 'abc12345',
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'email' => 'not-an-email',
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'email' => __('validation.email', ['attribute' => 'ایمیل']),
|
||||
]);
|
||||
}
|
||||
public function test_avatar_should_be_valid_mime_type(): void
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$invalidFile = UploadedFile::fake()->create('avatar.txt', 100, 'text/plain');
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $this->faker->unique()->userName,
|
||||
'position' => $this->faker->jobTitle,
|
||||
'gender' => $this->faker->randomElement(['male', 'female']),
|
||||
'national_id' => $this->faker->numerify('##########'),
|
||||
'phone_number' => $this->faker->numerify('09#########'),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->regexify('[A-Za-z]{4}[0-9]{4}'),
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'email' => $this->faker->unique()->safeEmail,
|
||||
'avatar' => $invalidFile,
|
||||
]);
|
||||
|
||||
$response->assertUnprocessable()
|
||||
->assertInvalid([
|
||||
'avatar' => __('validation.mimes', ['attribute' => 'آواتار', 'values' => 'png, jpg, pdf']),
|
||||
]);
|
||||
}
|
||||
public function test_fields_id_should_be_unique()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
$existingPhone = '09' .$this->faker->numberBetween(100000000, 999999999);
|
||||
$existingEmail = $this->faker->email;
|
||||
$existingUser = $this->faker->name;
|
||||
$existingNational = $this->faker->numberBetween(1000000000, 9999999999);
|
||||
|
||||
User::factory()->create([
|
||||
'phone_number' => $existingPhone,
|
||||
'national_id' => $existingNational,
|
||||
'username' => $existingUser,
|
||||
'email' => $existingEmail,
|
||||
]);
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.store'), [
|
||||
'full_name' => $this->faker->text,
|
||||
'username' =>$existingUser,
|
||||
'position' => $this->faker->text,
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' =>$existingNational,
|
||||
'phone_number' => $existingPhone,
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'password' => $this->faker->numberBetween(1000, 9999) . $this->faker->lexify('?????'),
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'email' =>$existingEmail,
|
||||
]);
|
||||
|
||||
$response->assertJsonValidationErrors([
|
||||
'phone_number' => __('validation.unique', ['attribute' => 'شماره تلفن']),
|
||||
'email' => __('validation.unique', ['attribute' => 'ایمیل']),
|
||||
'username' => __('validation.unique', ['attribute' => 'نام کاربری']),
|
||||
'national_id' => __('validation.unique', ['attribute' => 'کدملی']),
|
||||
]);
|
||||
}
|
||||
public function test_can_update_an_user()
|
||||
{
|
||||
$user = User::factory()
|
||||
->has(Permission::factory([
|
||||
'name' => 'manage_users',
|
||||
'name_fa' => 'مدیریت کاربران'
|
||||
]))
|
||||
->create();
|
||||
|
||||
$username = $this->faker->userName;
|
||||
|
||||
$response = $this->actingAs($user)->postJson(route('users.update',[$user->id]), [
|
||||
'full_name' => $this->faker->name,
|
||||
'username' => $username,
|
||||
'position' => $this->faker->lexify('?????'),
|
||||
'gender' => $this->faker->randomElement(['female', 'male']),
|
||||
'national_id' => $this->faker->numberBetween(1000000000, 9999999999),
|
||||
'phone_number' => '09' . $this->faker->numberBetween(100000000, 999999999),
|
||||
'province_id' => Province::factory()->create()->id,
|
||||
'role_id' => Role::factory()->create()->id,
|
||||
'telephone_id' => '01-1'
|
||||
]);
|
||||
|
||||
$response->assertStatus(200)
|
||||
->assertExactJson([
|
||||
'message' => __('messages.successful')
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'username' => $username
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user