new: [Org] more tests and validations

pull/9579/head
Christophe Vandeplas 2024-04-16 07:07:50 +00:00
parent fc17f5f683
commit e2a4984329
3 changed files with 102 additions and 11 deletions

View File

@ -1,18 +1,62 @@
<?php
declare(strict_types=1);
namespace App\Model\Behavior;
use Cake\ORM\Behavior;
use Cake\Event\EventInterface;
use Cake\Datasource\EntityInterface;
use ArrayObject;
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\Utility\Text;
use Cake\Validation\Validator;
class UUIDBehavior extends Behavior
{
/**
* beforeSave
*
* @param \Cake\Event\EventInterface $event the efent
* @param \Cake\Datasource\EntityInterface; $entity the entity
* @param array $options extra options
* @return void
*/
public function beforeSave(EventInterface $event, EntityInterface $entity, ArrayObject $options)
{
if ($entity->isNew() && empty($entity['uuid'])) {
$entity['uuid'] = Text::uuid();
}
}
/**
* buildValidator
*
* @param \Cake\Event\EventInterface $event the event
* @param \Cake\Validation\Validator $validator the validator
* @param string $name the string to validate
* @return \Cake\Validation\Validator
*/
public function buildValidator(EventInterface $event, Validator $validator, string $name)
{
$validator
->notEmptyString('uuid')
->add(
'uuid',
'valid',
[
'rule' => 'uuid',
'message' => 'The UUID is not valid',
]
)
->add(
'uuid',
'unique',
[
'rule' => 'validateUnique',
'provider' => 'table',
'message' => 'The UUID name must be unique.',
]
);
return $validator;
}
}

View File

@ -56,7 +56,6 @@ class OrganisationsTable extends AppTable
{
$validator
->notEmptyString('name')
->notEmptyString('uuid')
->requirePresence(['name', 'uuid'], 'create')
->add(
'name',
@ -68,18 +67,33 @@ class OrganisationsTable extends AppTable
],
'maxLength' => [
'rule' => ['maxLength', 255],
'message' => 'Names cannot be too long.',
'message' => 'Name cannot be more than 255 chars.',
],
]
)
->add(
'uuid',
'unique',
'type',
'maxLength',
[
'rule' => 'validateUnique',
'provider' => 'table',
'message' => 'The organisation name must be unique.',
]
'rule' => ['maxLength', 255],
'message' => 'Type cannot be more than 255 chars.',
],
)
->add(
'nationality',
'maxLength',
[
'rule' => ['maxLength', 255],
'message' => 'Nationality cannot be more than 255 chars.',
],
)
->add(
'sector',
'maxLength',
[
'rule' => ['maxLength', 255],
'message' => 'Sector cannot be more than 255 chars.',
],
);
return $validator;

View File

@ -185,6 +185,39 @@ class AddOrganisationsApiTest extends TestCase
);
}
public function testBadUuid(): void
{
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);
$faker = \Faker\Factory::create();
$org_data = [
'uuid' => '11111111-1111-1111-1111-111111111111',
'name' => $faker->text(10),
'description' => $faker->text(10),
'nationality' => $faker->countryCode,
'sector' => 'DUPLICATE ENTRY',
'type' => '',
'contacts' => '',
'local' => 1,
'restricted_to_domain' => '',
'landingpage' => '',
//'date_created' => $faker->dateTime()->getTimestamp(),
// 'date_modified' => $faker->dateTime()->getTimestamp(),
// 'created_by' => 0,
];
$url = sprintf('%s', self::ENDPOINT);
$this->post(
$url,
$org_data
);
$this->assertResponseCode(200);
$this->assertDbRecordNotExists(
'Organisations',
[
'name' => $org_data['name'],
]
);
}
public function testAddLongName(): void
{
$this->setAuthToken(AuthKeysFixture::ADMIN_API_KEY);