Merge branch 'master' of gitea:museum-digital/MDNodaHelpers

This commit is contained in:
Joshua Ramon Enslin 2024-05-04 01:19:23 +02:00
commit 7f342ed3c4
2 changed files with 122 additions and 11 deletions

View File

@ -11,7 +11,59 @@ declare(strict_types = 1);
*/ */
final class NodaValidationHelper { final class NodaValidationHelper {
const ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS = 2; const ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS = 3;
/**
* Validates an actor description for completeness. Of course, only an informed
* guess based on the length and character composition of the description can be
* made.
*
* @param string $description Input descrition.
* @param string $name Names of the actor. Optional. Setting this enables
* checks e.g. to prevent duplicating the actor name
* as a description.
*
* @return void
*/
public static function validateTagDescription(string $description, string $name = ""):void {
// For nodac, empty tag descriptions are to be allowed. Thus, a
// dedicated check for fully empty ones with a dedicated exception
// is needed.
if (empty($description)) {
throw new MDInvalidEmptyInputException("No tag name is provided");
}
// Throw error on descriptions that are too short
if (\mb_strlen($description) < 10) {
throw new MDgenericInvalidInputsException("Tag description is too short");
}
// Validate tag description based on character composition.
// Ensure more than 3 distinct characters are used.
$chars = \str_split($description);
$uniqueChars = array_unique($chars);
if (count($uniqueChars) <= self::ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS) {
throw new MDgenericInvalidInputsException("There need to be more than " . self::ACTOR_DESCRIPTION_REQUIRED_DISTINCT_CHARS . " distinct characters.");
}
// Ensure more than the actor name is used.
$clearedChars = [' ' => ' ', ',' => ' ', ';' => ' ', '.' => ' '];
$uniqueNames = array_unique(array_diff(explode(' ', strtr($name, $clearedChars)), ['']));
sort($uniqueNames);
$descCleared = strtr($description, $clearedChars);
$descWords = array_unique(array_diff(explode(' ', $descCleared), ['']));
sort($descWords);
if ($uniqueNames === $descWords) {
throw new MDgenericInvalidInputsException("The tag name was simply repeated in the description. This is not enough.");
}
}
/** /**
* Validates an actor description for completeness. Of course, only an informed * Validates an actor description for completeness. Of course, only an informed

View File

@ -5,19 +5,22 @@
* @author Joshua Ramon Enslin <joshua@museum-digital.de> * @author Joshua Ramon Enslin <joshua@museum-digital.de>
*/ */
declare(strict_types = 1); declare(strict_types = 1);
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
/** /**
* This script contains tests for the validation of single field contents. * This script contains tests for the validation of single field contents.
* *
* @covers \NodaValidationHelper * @covers \NodaValidationHelper
*/ */
#[small]
#[CoversClass(\NodaValidationHelper::class)]
final class NodaValidationHelperTest extends TestCase { final class NodaValidationHelperTest extends TestCase {
/** /**
* Test that a specific error is thrown if no description for actors is provided. * Test that a specific error is thrown if no description for actors is provided.
* *
* @small
*
* @return void * @return void
*/ */
public function testActorDescriptionValidationFailsOnEmptyInput():void { public function testActorDescriptionValidationFailsOnEmptyInput():void {
@ -30,8 +33,6 @@ final class NodaValidationHelperTest extends TestCase {
/** /**
* Test successfully refusing too short actor descriptions. * Test successfully refusing too short actor descriptions.
* *
* @small
*
* @return void * @return void
*/ */
public function testActorDescriptionValidationFailsOnTooShortInput():void { public function testActorDescriptionValidationFailsOnTooShortInput():void {
@ -44,8 +45,6 @@ final class NodaValidationHelperTest extends TestCase {
/** /**
* Test successfully refusing actor descriptions that have too few distinct characters. * Test successfully refusing actor descriptions that have too few distinct characters.
* *
* @small
*
* @return void * @return void
*/ */
public function testActorDescriptionValidationFailsOnTooFewDistinctCharacters():void { public function testActorDescriptionValidationFailsOnTooFewDistinctCharacters():void {
@ -58,8 +57,6 @@ final class NodaValidationHelperTest extends TestCase {
/** /**
* Test successfully refusing actor descriptions that simply duplicate the actor name. * Test successfully refusing actor descriptions that simply duplicate the actor name.
* *
* @small
*
* @return void * @return void
*/ */
public function testActorDescriptionValidationFailsOnDuplicatedActorNames():void { public function testActorDescriptionValidationFailsOnDuplicatedActorNames():void {
@ -80,8 +77,6 @@ final class NodaValidationHelperTest extends TestCase {
/** /**
* Test that a valid description is accepted. * Test that a valid description is accepted.
* *
* @small
*
* @return void * @return void
*/ */
public function testActorDescriptionValidationAcceptsValidDescription():void { public function testActorDescriptionValidationAcceptsValidDescription():void {
@ -90,4 +85,68 @@ final class NodaValidationHelperTest extends TestCase {
self::assertTrue(true); self::assertTrue(true);
} }
/**
* Test that a specific error is thrown if no description for tags is provided.
*
* @return void
*/
public function testTagDescriptionValidationFailsOnEmptyInput():void {
$this->expectException(MDInvalidEmptyInputException::class);
NodaValidationHelper::validateTagDescription("");
}
/**
* Test successfully refusing too short tag descriptions.
*
* @return void
*/
public function testTagDescriptionValidationFailsOnTooShortInput():void {
$this->expectException(MDgenericInvalidInputsException::class);
NodaValidationHelper::validateTagDescription("abc");
}
/**
* Test successfully refusing tag descriptions that have too few distinct characters.
*
* @return void
*/
public function testTagDescriptionValidationFailsOnTooFewDistinctCharacters():void {
$this->expectException(MDgenericInvalidInputsException::class);
NodaValidationHelper::validateTagDescription("aaaaaaaaaaaa");
}
/**
* Test successfully refusing tag descriptions that simply duplicate the tag name.
*
* @return void
*/
public function testTagDescriptionValidationFailsOnDuplicatedTagNames():void {
$this->expectException(MDgenericInvalidInputsException::class);
NodaValidationHelper::validateTagDescription("Richard Lepsius", "Richard Lepsius");
$this->expectException(MDgenericInvalidInputsException::class);
NodaValidationHelper::validateTagDescription("Richard Lepsius", "Lepsius Richard");
$this->expectException(MDgenericInvalidInputsException::class);
NodaValidationHelper::validateTagDescription("Lepsius, Richard", "Lepsius, Richard");
}
/**
* Test that a valid description is accepted.
*
* @return void
*/
public function testTagDescriptionValidationAcceptsValidDescription():void {
NodaValidationHelper::validateTagDescription("Richard Lepsius war ein Forscher", "Richard Lepsius");
self::assertTrue(true);
}
} }