From 639a8c92af49cacaa0887f6f071bad804e6ffbde Mon Sep 17 00:00:00 2001 From: aslan Date: Wed, 14 Feb 2024 16:16:44 +0300 Subject: [PATCH] add Education Level test --- .../admin/Catalog/DirectionController.php | 12 +- .../admin/Catalog/StoreDirectionRequest.php | 2 + .../admin/Catalog/UpdateDirectionRequest.php | 2 + app/Models/Direction.php | 7 + database/factories/DirectionFactory.php | 2 + ...4_02_09_114155_create_directions_table.php | 2 + database/seeders/DirectionSeeder.php | 31 ++++- .../admin/catalog/direction/create.blade.php | 21 ++- .../admin/catalog/direction/edit.blade.php | 21 ++- .../admin/catalog/direction/index.blade.php | 2 + .../admin/catalog/direction/show.blade.php | 2 + .../Feature/admin/catalog/DepartmentTest.php | 2 + tests/Feature/admin/catalog/DirectionTest.php | 4 + .../admin/catalog/EducationLevelTest.php | 121 ++++++++++++++++++ 14 files changed, 220 insertions(+), 11 deletions(-) create mode 100644 tests/Feature/admin/catalog/EducationLevelTest.php diff --git a/app/Http/Controllers/admin/Catalog/DirectionController.php b/app/Http/Controllers/admin/Catalog/DirectionController.php index c50c793..9d39d43 100644 --- a/app/Http/Controllers/admin/Catalog/DirectionController.php +++ b/app/Http/Controllers/admin/Catalog/DirectionController.php @@ -7,6 +7,7 @@ use App\Http\Requests\admin\Catalog\StoreDirectionRequest; use App\Http\Requests\admin\Catalog\UpdateDirectionRequest; use App\Models\Department; use App\Models\Direction; +use App\Models\EducationLevel; use App\Models\Faculty; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; @@ -23,8 +24,9 @@ class DirectionController extends Controller public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application { + $levels = EducationLevel::pluck('name', 'id'); $departments = Department::pluck('name', 'id'); - return view('admin.catalog.direction.create', compact('departments')); + return view('admin.catalog.direction.create', compact('departments', 'levels')); } public function store(StoreDirectionRequest $request): RedirectResponse @@ -36,6 +38,8 @@ class DirectionController extends Controller $direction->description = $validated['description']; $direction->position = $validated['position']; $direction->slug = $validated['slug']; + $direction->code = $validated['code']; + $direction->education_level_id = $validated['education_level_id']; $direction->department_id = $validated['department_id']; $direction->save(); @@ -60,18 +64,22 @@ class DirectionController extends Controller public function edit(Direction $direction): View|Application|Factory|\Illuminate\Contracts\Foundation\Application { + $levels = EducationLevel::pluck('name', 'id'); $departments = Department::pluck('name', 'id'); - return view('admin.catalog.direction.edit', compact('direction', 'departments')); + return view('admin.catalog.direction.edit', compact('direction', 'departments', 'levels')); } public function update(UpdateDirectionRequest $request, Direction $direction): RedirectResponse { $validated = $request->validated(); + $direction = new Direction(); $direction->name = $validated['name']; $direction->description = $validated['description']; $direction->position = $validated['position']; $direction->slug = $validated['slug']; + $direction->code = $validated['code']; + $direction->education_level_id = $validated['education_level_id']; $direction->department_id = $validated['department_id']; $direction->save(); diff --git a/app/Http/Requests/admin/Catalog/StoreDirectionRequest.php b/app/Http/Requests/admin/Catalog/StoreDirectionRequest.php index 88abe17..c4e7c82 100644 --- a/app/Http/Requests/admin/Catalog/StoreDirectionRequest.php +++ b/app/Http/Requests/admin/Catalog/StoreDirectionRequest.php @@ -18,6 +18,8 @@ class StoreDirectionRequest extends FormRequest 'name' => 'required|string|max:255|unique:directions,name', 'description' => 'string', 'slug' => 'required|string', + 'code' => 'required|string', + 'education_level_id' => 'required|int', 'department_id' => 'required|int' ]; } diff --git a/app/Http/Requests/admin/Catalog/UpdateDirectionRequest.php b/app/Http/Requests/admin/Catalog/UpdateDirectionRequest.php index 97b190d..4b9e072 100644 --- a/app/Http/Requests/admin/Catalog/UpdateDirectionRequest.php +++ b/app/Http/Requests/admin/Catalog/UpdateDirectionRequest.php @@ -19,6 +19,8 @@ class UpdateDirectionRequest extends FormRequest 'description' => 'string', 'department_id' => 'int|required', 'slug' => 'required|string', + 'code' => 'required|string', + 'education_level_id' => 'required|int', 'name' => [ 'required', 'string', diff --git a/app/Models/Direction.php b/app/Models/Direction.php index 98f8ce7..db3ebc3 100644 --- a/app/Models/Direction.php +++ b/app/Models/Direction.php @@ -15,10 +15,17 @@ class Direction extends Model 'name', 'description', 'position', + 'slug', + 'code', ]; public function department(): BelongsTo { return $this->belongsTo(Department::class); } + + public function educationLevel(): BelongsTo + { + return $this->belongsTo(EducationLevel::class); + } } diff --git a/database/factories/DirectionFactory.php b/database/factories/DirectionFactory.php index 18e5461..c1143c8 100644 --- a/database/factories/DirectionFactory.php +++ b/database/factories/DirectionFactory.php @@ -12,8 +12,10 @@ class DirectionFactory extends Factory 'name' => fake()->name(), 'description' => fake()->text(), 'slug' => fake()->slug(), + 'code' => fake()->text(50), 'position' => fake()->randomDigit(), 'department_id' => 1, + 'education_level_id' => 1 ]; } } diff --git a/database/migrations/2024_02_09_114155_create_directions_table.php b/database/migrations/2024_02_09_114155_create_directions_table.php index 7ba432a..3989716 100644 --- a/database/migrations/2024_02_09_114155_create_directions_table.php +++ b/database/migrations/2024_02_09_114155_create_directions_table.php @@ -15,9 +15,11 @@ return new class extends Migration $table->id(); $table->string('name'); $table->text('description'); + $table->string('code'); $table->integer('position'); $table->string('slug'); $table->foreignId('department_id')->constrained('departments'); + $table->foreignId('education_level_id')->constrained('education_levels'); $table->timestamps(); }); } diff --git a/database/seeders/DirectionSeeder.php b/database/seeders/DirectionSeeder.php index 1b26f73..adf9119 100644 --- a/database/seeders/DirectionSeeder.php +++ b/database/seeders/DirectionSeeder.php @@ -4,11 +4,40 @@ namespace Database\Seeders; use App\Models\Direction; use Illuminate\Database\Seeder; +use Illuminate\Support\Facades\DB; class DirectionSeeder extends Seeder { public function run(): void { - Direction::factory(3)->create(); + DB::table('directions')->insert([ + [ + 'name' => 'Юриспруденция', + 'description' => 'Юриспруденция', + 'slug' => 'jurisprudence', + 'code' => '40.03.01', + 'position' => 1, + 'department_id' => 1, + 'education_level_id' => 1, + ], + [ + 'name' => 'фармация', + 'description' => 'фармация', + 'slug' => 'pharmacy', + 'code' => '33.05.01', + 'position' => 2, + 'department_id' => 1, + 'education_level_id' => 2, + ], + [ + 'name' => 'строительство', + 'description' => 'строительство', + 'slug' => 'construction', + 'code' => '08.04.01', + 'position' => 3, + 'department_id' => 1, + 'education_level_id' => 3, + ], + ]); } } diff --git a/resources/views/admin/catalog/direction/create.blade.php b/resources/views/admin/catalog/direction/create.blade.php index 8d8e121..0f41774 100644 --- a/resources/views/admin/catalog/direction/create.blade.php +++ b/resources/views/admin/catalog/direction/create.blade.php @@ -19,14 +19,14 @@
- {{ Form::label('name', 'Название') }} + {{ Form::label('code', 'Название') }}
- {{ Form::text('name', '', ['class' => 'form-control']) }} + {{ Form::text('code', '', ['class' => 'form-control']) }}
@if ($errors->any()) - {{ $errors->first('name') }} + {{ $errors->first('code') }} @endif
@@ -42,7 +42,7 @@ @endif
- {{ Form::label('department_id', 'Кафедры') }} + {{ Form::label('department_id', 'Кафедра') }}
{{ Form::select('department_id', $departments, null, ['class' => 'form-select']) }} @@ -52,6 +52,19 @@ {{ $errors->first('department_id') }} @endif
+ +
+ {{ Form::label('education_level_id', 'Увовень образования') }} +
+
+ {{ Form::select('education_level_id', $levels, null, ['class' => 'form-select']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('education_level_id') }} + @endif +
+
{{ Form::label('slug', 'URL') }}
diff --git a/resources/views/admin/catalog/direction/edit.blade.php b/resources/views/admin/catalog/direction/edit.blade.php index ea231cd..ceb27e8 100644 --- a/resources/views/admin/catalog/direction/edit.blade.php +++ b/resources/views/admin/catalog/direction/edit.blade.php @@ -4,7 +4,7 @@ @auth()
-

Создать кафедру

+

Изменить направление

{{ Form::open(['url' => route('departments.update', $direction), 'method' => 'PATCH', 'class' => '']) }}
@@ -43,16 +43,29 @@ @endif
- {{ Form::label('educational_institution_id', 'Факультет') }} + {{ Form::label('department_id', 'Факультет') }}
- {{ Form::select('faculty_id', $departments, $direction->department->id, ['class' => 'form-select']) }} + {{ Form::select('department_id', $departments, $direction->department->id, ['class' => 'form-select']) }}
@if ($errors->any()) - {{ $errors->first('educational_institution_id') }} + {{ $errors->first('department_id') }} @endif
+ +
+ {{ Form::label('education_level_id', 'Увовень образования') }} +
+
+ {{ Form::select('education_level_id', $levels, $direction->educationLevel->id, ['class' => 'form-select']) }} +
+
+ @if ($errors->any()) + {{ $errors->first('education_level_id') }} + @endif +
+
{{ Form::label('slug', 'URL') }}
diff --git a/resources/views/admin/catalog/direction/index.blade.php b/resources/views/admin/catalog/direction/index.blade.php index 544eff4..588ce04 100644 --- a/resources/views/admin/catalog/direction/index.blade.php +++ b/resources/views/admin/catalog/direction/index.blade.php @@ -15,6 +15,7 @@ Описание URL Кафедра + Уровень образования действия @@ -27,6 +28,7 @@ {{ Str::words($direction->description, 10, '...') }} {{ $direction->slug }} {{ $direction->department->name }} + {{ $direction->educationLevel->name }} редактировать diff --git a/resources/views/admin/catalog/direction/show.blade.php b/resources/views/admin/catalog/direction/show.blade.php index 89d7b05..d79fa1b 100644 --- a/resources/views/admin/catalog/direction/show.blade.php +++ b/resources/views/admin/catalog/direction/show.blade.php @@ -23,6 +23,8 @@

{{ $direction->slug }}

Кафедра

{{ $department->name }}

+

Уровень Образования

+

{{ $direction->educationLevel->name }}

@endauth @endsection diff --git a/tests/Feature/admin/catalog/DepartmentTest.php b/tests/Feature/admin/catalog/DepartmentTest.php index c194a04..7a9fbd5 100644 --- a/tests/Feature/admin/catalog/DepartmentTest.php +++ b/tests/Feature/admin/catalog/DepartmentTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature\admin\catalog; use App\Models\Department; use App\Models\Direction; use App\Models\EducationalInstitution; +use App\Models\EducationLevel; use App\Models\Faculty; use App\Models\User; use Tests\TestCase; @@ -19,6 +20,7 @@ class DepartmentTest extends TestCase parent::setUp(); EducationalInstitution::factory()->create(); Faculty::factory()->create(); + EducationLevel::factory()->create(); $this->department = Department::factory()->create(); $this->data = Department::factory()->make()->only([ diff --git a/tests/Feature/admin/catalog/DirectionTest.php b/tests/Feature/admin/catalog/DirectionTest.php index 363e255..791d329 100644 --- a/tests/Feature/admin/catalog/DirectionTest.php +++ b/tests/Feature/admin/catalog/DirectionTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature\admin\catalog; use App\Models\Department; use App\Models\Direction; use App\Models\EducationalInstitution; +use App\Models\EducationLevel; use App\Models\Faculty; use App\Models\User; use Tests\TestCase; @@ -20,13 +21,16 @@ class DirectionTest extends TestCase EducationalInstitution::factory()->create(); Faculty::factory()->create(); Department::factory()->create(); + EducationLevel::factory()->create(); $this->direction = Direction::factory()->create(); $this->data = Direction::factory()->make()->only([ 'position', 'name', 'description', + 'code', 'slug', + 'education_level_id', 'department_id', ]); diff --git a/tests/Feature/admin/catalog/EducationLevelTest.php b/tests/Feature/admin/catalog/EducationLevelTest.php new file mode 100644 index 0000000..4d5ecb5 --- /dev/null +++ b/tests/Feature/admin/catalog/EducationLevelTest.php @@ -0,0 +1,121 @@ +create(); + Faculty::factory()->create(); + Department::factory()->create(); + $this->level = EducationLevel::factory()->create(); + + $this->direction = Direction::factory()->create(); + $this->data = EducationLevel::factory()->make()->only([ + 'name', + 'description', + 'slug', + ]); + + $this->user = User::factory()->create([ + 'name' => 'admin', + 'email' => 'test@example.com', + 'password' => 123456 + ]); + } + + public function testIndexEducationLevelsPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('education_levels.index')); + + $response->assertOk(); + } + + public function testCreateEducationLevelPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('education_levels.create')); + + $response->assertOk(); + } + + public function testStoreEducationLevel(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->post(route('education_levels.store', $this->data)); + + $response->assertRedirect(route('education_levels.index')); + + $this->assertDatabaseHas('education_levels', $this->data); + } + + public function testShowEducationLevelPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('education_levels.show', $this->level)); + + $response->assertOk(); + } + + public function testEditEducationLevelPage(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->get(route('education_levels.edit', $this->level)); + + $response->assertOk(); + } + + public function testUpdateEducationLevel(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->patch(route('education_levels.update', $this->level), $this->data); + + $response->assertRedirect(route('education_levels.index')); + + $this->assertDatabaseHas('education_levels', $this->data); + } + + public function testDestroyEducationLevel(): void + { + $this->direction->delete(); + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->delete(route('education_levels.destroy', $this->level)); + + $response->assertRedirect(route('education_levels.index')); + + $this->assertDatabaseMissing('education_levels', $this->level->toArray()); + } + + public function testNotDestroyEducationLevel(): void + { + $response = $this->actingAs($this->user) + ->withSession(['banned' => false]) + ->delete(route('education_levels.destroy', $this->level)); + + $response->assertStatus(302); + + $this->assertDatabaseHas('education_levels', $this->level->only(['name', 'slug'])); + } +}