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

Add tests for and refactor publication field type validation rules #765

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
23e28b8
Create PublicationFieldTypeValidationRulesTest.php
caendesilva Dec 19, 2022
d90dcc9
Link to the unit test
caendesilva Dec 19, 2022
158d1f7
Add testing helper to make a publication type
caendesilva Dec 19, 2022
7967aa6
Revert "Add testing helper to make a publication type"
caendesilva Dec 19, 2022
230cf4f
Implement the unit test
caendesilva Dec 19, 2022
790485b
Test with image files
caendesilva Dec 19, 2022
bd3a62b
Null coalesce to empty collection
caendesilva Dec 19, 2022
537bd98
Add code review comments
caendesilva Dec 19, 2022
0161c3a
Remove default switch case that's impossible to reach due to enum
caendesilva Dec 19, 2022
dbc9230
Update skipped test
caendesilva Dec 19, 2022
85d2745
Rename local variable doBetween to useRange
caendesilva Dec 19, 2022
e9d0448
Apply fixes from StyleCI
StyleCIBot Dec 19, 2022
584a3a4
Resolve fixme
caendesilva Dec 20, 2022
2fb82a7
Merge branch 'publications-feature' into test-and-refactor-publicatio…
caendesilva Dec 20, 2022
a49c40a
Merge branch 'master' into test-and-refactor-publication-validation-r…
caendesilva Dec 20, 2022
64ead71
Update PublicationFieldValidationRulesTest for renamed class
caendesilva Dec 20, 2022
7e6b1e2
Merge branch 'publications-feature' into test-and-refactor-publicatio…
caendesilva Dec 21, 2022
a829ce1
Merge branch 'publications-feature' into test-and-refactor-publicatio…
caendesilva Dec 21, 2022
b0d75d1
Resolve todos
caendesilva Dec 21, 2022
4308643
Add the URL rule to URLs
caendesilva Dec 21, 2022
4eed23e
Link to the rules documentation
caendesilva Dec 21, 2022
a601376
Rename test methods
caendesilva Dec 21, 2022
f038165
Add testing helper to expect a validation exception
caendesilva Dec 21, 2022
e2f1f26
Parse the datetimes to ensure they are valid and normalized
caendesilva Dec 21, 2022
7894869
Support only using min date values
caendesilva Dec 21, 2022
1f4420f
Remove code comment
caendesilva Dec 21, 2022
851f667
Evaluate min/max values for dates independently
caendesilva Dec 21, 2022
4db1fe1
Test array validation
caendesilva Dec 21, 2022
bc32f14
Add the date rule to datetime fields
caendesilva Dec 21, 2022
4492aa7
Test datetime validation
caendesilva Dec 21, 2022
380ae76
Split out test methods
caendesilva Dec 21, 2022
a50908c
Apply fixes from StyleCI
StyleCIBot Dec 21, 2022
374bb41
Allow only min value to be set
caendesilva Dec 21, 2022
76431ff
Use class constant instead of FQCN
caendesilva Dec 21, 2022
22f57d5
Rename test methods to be more descriptive
caendesilva Dec 21, 2022
7aeeb2d
Floats must be numeric to be properly range validated
caendesilva Dec 21, 2022
353e66d
Remove range validation tests
caendesilva Dec 21, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Features\Publications\Models;

use function collect;
use Hyde\Framework\Features\Publications\Concerns\PublicationFieldTypes;
use Hyde\Framework\Features\Publications\PublicationService;
use Hyde\Support\Concerns\Serializable;
Expand Down Expand Up @@ -64,25 +65,26 @@ public function toArray(): array
];
}

/** @see \Hyde\Framework\Testing\Unit\PublicationFieldTypeValidationRulesTest */
public function getValidationRules(bool $reload = true): Collection
{
$defaultRules = Collection::create(PublicationFieldTypes::values());
$fieldRules = Collection::create($defaultRules->get($this->type->value));

$doBetween = true;
$useRange = true;
// The trim command used to process the min/max input results in a string, so
// we need to test both int and string values to determine required status.
if (($this->min && ! $this->max) || ($this->min == '0' && $this->max == '0')) {
$fieldRules->forget($fieldRules->search('required'));
$doBetween = false;
$useRange = false;
}

switch ($this->type->value) {
case 'array':
$fieldRules->add('array');
$fieldRules->add('array'); // FIXME do we do range validation too?
caendesilva marked this conversation as resolved.
Show resolved Hide resolved
break;
case 'datetime':
if ($doBetween) {
if ($useRange) {
$fieldRules->add("after:$this->min");
$fieldRules->add("before:$this->max");
}
Expand All @@ -91,26 +93,26 @@ public function getValidationRules(bool $reload = true): Collection
case 'integer':
case 'string':
case 'text':
if ($doBetween) {
if ($useRange) {
$fieldRules->add("between:$this->min,$this->max");
}
break;
case 'image':
$mediaFiles = PublicationService::getMediaForPubType($this->publicationType, $reload);
$valueList = $mediaFiles->implode(',');
$fieldRules->add("in:$valueList");
$fieldRules->add("in:$valueList"); // FIXME What if the list is empty?
caendesilva marked this conversation as resolved.
Show resolved Hide resolved
// FIXME: Now the items look like 'in:_media/foo/bar.jpg', but do we really need the directory information?
// Wouldn't it suffice with just 'in:bar.jpg' since we already know what directory it is in?
// We could then easily qualify it within the template and/or via a helper method.
break;
case 'tag':
$tagValues = PublicationService::getValuesForTagName($this->tagGroup, $reload);
$tagValues = PublicationService::getValuesForTagName($this->tagGroup, $reload) ?? collect([]);
$valueList = $tagValues->implode(',');
$fieldRules->add("in:$valueList");
break;
case 'url':
// FIXME Shouldn't we add a 'url' rule here?
caendesilva marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
caendesilva marked this conversation as resolved.
Show resolved Hide resolved
throw new \InvalidArgumentException(
"Unhandled field type [{$this->type->value}]. Possible field types are: ".implode(', ', PublicationFieldTypes::values())
);
}

return $fieldRules;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit;

use Hyde\Framework\Features\Publications\Models\PublicationFieldType;
use Hyde\Framework\Features\Publications\Models\PublicationType;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Framework\Features\Publications\Models\PublicationFieldType
*/
class PublicationFieldTypeValidationRulesTest extends TestCase
{
public function testWithArray()
{
$rules = (new PublicationFieldType('array', 'myArray', '4', '8'))->getValidationRules();
$this->assertSame(['array'], $rules->toArray());
}

public function testWithDatetime()
{
$rules = (new PublicationFieldType('datetime', 'myDatetime', '4', '8'))->getValidationRules();
$this->assertSame(['after:4', 'before:8'], $rules->toArray());
}

public function testWithFloat()
{
$rules = (new PublicationFieldType('float', 'myFloat', '4', '8'))->getValidationRules();
$this->assertSame(['between:4,8'], $rules->toArray());
}

public function testWithInteger()
{
$rules = (new PublicationFieldType('integer', 'myInteger', '4', '8'))->getValidationRules();
$this->assertSame(['between:4,8'], $rules->toArray());
}

public function testWithString()
{
$rules = (new PublicationFieldType('string', 'myString', '4', '8'))->getValidationRules();
$this->assertSame(['between:4,8'], $rules->toArray());
}

public function testWithText()
{
$rules = (new PublicationFieldType('text', 'myText', '4', '8'))->getValidationRules();
$this->assertSame(['between:4,8'], $rules->toArray());
}

public function testWithImage()
{
$this->directory('_media/foo');
$this->file('_media/foo/bar.jpg');
$this->file('_media/foo/baz.png');
$rules = (new PublicationFieldType('image', 'myImage', '4', '8', publicationType: new PublicationType('foo')))->getValidationRules();
$this->assertSame(['in:_media/foo/bar.jpg,_media/foo/baz.png'], $rules->toArray());
}

public function testWithTag()
{
$rules = (new PublicationFieldType('tag', 'myTag', '4', '8', 'foo'))->getValidationRules();
$this->assertSame(['in:'], $rules->toArray());
}

public function testWithUrl()
{
$rules = (new PublicationFieldType('url', 'myUrl', '4', '8'))->getValidationRules();
$this->assertSame([], $rules->toArray());
}
}