Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: assert that color is not blank in Category #4033

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/src/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class Category extends BaseEntity implements BelongsToCampInterface, CopyFromPro
/**
* The color of the activities in this category, as a hex color string.
*/
#[InputFilter\Trim]
#[Assert\NotBlank]
#[Assert\Regex(pattern: '/^#[0-9a-zA-Z]{6}$/')]
#[ApiProperty(example: '#4DBB52')]
#[Groups(['read', 'write'])]
Expand Down
64 changes: 63 additions & 1 deletion api/tests/Api/Categories/CreateCategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,49 @@ public function testCreateCategoryCleansForbiddenCharactersFromName() {
));
}

public function testCreateCategoryValidatesNullColor() {
static::createClientWithCredentials()->request(
'POST',
'/categories',
[
'json' => $this->getExampleWritePayload(
[
'color' => null,
]
),
]
);

$this->assertResponseStatusCodeSame(400);
$this->assertJsonContains([
'detail' => 'The type of the "color" attribute must be "string", "NULL" given.',
]);
}

public function testCreateCategoryValidatesEmptyColor() {
static::createClientWithCredentials()->request(
'POST',
'/categories',
[
'json' => $this->getExampleWritePayload(
[
'color' => '',
]
),
]
);

$this->assertResponseStatusCodeSame(422);
$this->assertJsonContains([
'violations' => [
[
'propertyPath' => 'color',
'message' => 'This value should not be blank.',
],
],
]);
}

public function testCreateCategoryValidatesMissingColor() {
static::createClientWithCredentials()->request('POST', '/categories', ['json' => $this->getExampleWritePayload([], ['color'])]);

Expand All @@ -349,7 +392,7 @@ public function testCreateCategoryValidatesMissingColor() {
'violations' => [
[
'propertyPath' => 'color',
'message' => 'This value should not be null.',
'message' => 'This value should not be blank.',
],
],
]);
Expand All @@ -371,6 +414,25 @@ public function testCreateCategoryValidatesInvalidColor() {
]);
}

public function testCreateCategoryTrimsColor() {
static::createClientWithCredentials()->request(
'POST',
'/categories',
[
'json' => $this->getExampleWritePayload(
[
'color' => " \t #4DBB52 \t ",
]
),
]
);

$this->assertResponseStatusCodeSame(201);
$this->assertJsonContains($this->getExampleReadPayload([
'color' => '#4DBB52',
]));
}

public function testCreateCategoryUsesDefaultForMissingNumberingStyle() {
static::createClientWithCredentials()->request('POST', '/categories', ['json' => $this->getExampleWritePayload([], ['numberingStyle'])]);

Expand Down
62 changes: 62 additions & 0 deletions api/tests/Api/Categories/UpdateCategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,49 @@ public function testPatchCategoryCleansForbiddenCharactersFromName() {
);
}

public function testPatchCategoryValidatesNullColor() {
$category = static::$fixtures['category1'];
static::createClientWithCredentials()->request(
'PATCH',
'/categories/'.$category->getId(),
[
'json' => [
'color' => null,
],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]
);

$this->assertResponseStatusCodeSame(400);
$this->assertJsonContains([
'detail' => 'The type of the "color" attribute must be "string", "NULL" given.',
]);
}

public function testPatchCategoryValidatesEmptyColor() {
$category = static::$fixtures['category1'];
static::createClientWithCredentials()->request(
'PATCH',
'/categories/'.$category->getId(),
[
'json' => [
'color' => '',
],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]
);

$this->assertResponseStatusCodeSame(422);
$this->assertJsonContains([
'violations' => [
[
'propertyPath' => 'color',
'message' => 'This value should not be blank.',
],
],
]);
}

public function testPatchCategoryValidatesInvalidColor() {
$category = static::$fixtures['category1'];
static::createClientWithCredentials()->request('PATCH', '/categories/'.$category->getId(), ['json' => [
Expand All @@ -433,6 +476,25 @@ public function testPatchCategoryValidatesInvalidColor() {
]);
}

public function testPatchCategoryTrimsColor() {
$category = static::$fixtures['category1'];
static::createClientWithCredentials()->request(
'PATCH',
'/categories/'.$category->getId(),
[
'json' => [
'color' => " \t #4DBB52 \t ",
],
'headers' => ['Content-Type' => 'application/merge-patch+json'],
]
);

$this->assertResponseStatusCodeSame(200);
$this->assertJsonContains([
'color' => '#4DBB52',
]);
}

public function testPatchCategoryValidatesInvalidNumberingStyle() {
$category = static::$fixtures['category1'];
static::createClientWithCredentials()->request('PATCH', '/categories/'.$category->getId(), ['json' => [
Expand Down