2024-07-04 17:46:09 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\Categories\Api;
|
|
|
|
|
|
|
|
use App\Models\Asset;
|
|
|
|
use App\Models\AssetModel;
|
|
|
|
use App\Models\Category;
|
|
|
|
use App\Models\User;
|
|
|
|
use Illuminate\Testing\Fluent\AssertableJson;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class CreateCategoriesTest extends TestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
public function testRequiresPermissionToCreateCategory()
|
|
|
|
{
|
|
|
|
$this->actingAsForApi(User::factory()->create())
|
|
|
|
->postJson(route('api.categories.store'))
|
|
|
|
->assertForbidden();
|
|
|
|
}
|
|
|
|
|
2024-07-04 19:22:56 -07:00
|
|
|
public function testCanCreateCategoryWithValidCategoryType()
|
2024-07-04 17:46:09 -07:00
|
|
|
{
|
|
|
|
$response = $this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.categories.store'), [
|
|
|
|
'name' => 'Test Category',
|
|
|
|
'eula_text' => 'Test EULA',
|
|
|
|
'category_type' => 'accessory',
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertStatusMessageIs('success')
|
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
|
|
|
|
|
|
|
$this->assertTrue(Category::where('name', 'Test Category')->exists());
|
|
|
|
|
|
|
|
$category = Category::find($response['payload']['id']);
|
|
|
|
$this->assertEquals('Test Category', $category->name);
|
|
|
|
$this->assertEquals('Test EULA', $category->eula_text);
|
|
|
|
$this->assertEquals('accessory', $category->category_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCannotCreateCategoryWithoutCategoryType()
|
2024-07-04 19:22:56 -07:00
|
|
|
{
|
|
|
|
$response = $this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.categories.store'), [
|
|
|
|
'name' => 'Test Category',
|
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertStatus(200)
|
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->assertJson([
|
|
|
|
'messages' => [
|
|
|
|
'category_type' => ['The category type field is required.'],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
$this->assertFalse(Category::where('name', 'Test Category')->exists());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCannotCreateCategoryWithInvalidCategoryType()
|
2024-07-04 17:46:09 -07:00
|
|
|
{
|
|
|
|
$this->actingAsForApi(User::factory()->superuser()->create())
|
|
|
|
->postJson(route('api.categories.store'), [
|
|
|
|
'name' => 'Test Category',
|
|
|
|
'eula_text' => 'Test EULA',
|
2024-07-04 19:22:56 -07:00
|
|
|
'category_type' => 'invalid',
|
2024-07-04 17:46:09 -07:00
|
|
|
])
|
|
|
|
->assertOk()
|
|
|
|
->assertStatus(200)
|
2024-07-04 19:22:56 -07:00
|
|
|
->assertStatusMessageIs('error')
|
|
|
|
->assertJson([
|
|
|
|
'messages' => [
|
|
|
|
'category_type' => ['The selected category type is invalid.'],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2024-07-04 17:46:09 -07:00
|
|
|
$this->assertFalse(Category::where('name', 'Test Category')->exists());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|