trypost/tests/Unit/Support/PostMediaRulesTest.php
Paulo Castellano 1242cc7384 refactor(posts): centralize inline media validation in PostMediaRules
The media.* rules were duplicated across the web update request and both API
requests (and diverged: web requires hosted id+path and tracks source; the API
accepts a bare external url it downloads). Pull them into one
App\Support\PostMediaRules::rules(hosted:) — same pattern as PostPlatformMetaRules
— parameterized by contract, so there's a single place to add a media key and the
validated()-strips-unlisted-keys footgun can't drift between entry points.

Behavior is unchanged (each ruleset is reproduced exactly). Web store keeps its
loose 'media' => array (no item rules) and is left out on purpose — adding strict
rules there would change the web create contract.
2026-06-28 20:43:09 -03:00

38 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
use App\Support\PostMediaRules;
test('hosted media rules require id and path and allow source', function () {
$rules = PostMediaRules::rules(hosted: true);
expect($rules['media.*.id'])->toContain('required')
->and($rules['media.*.path'])->toContain('required')
->and($rules)->toHaveKey('media.*.source')
->and($rules)->toHaveKey('media.*.source_meta');
});
test('api media rules accept a bare external url and omit source', function () {
$rules = PostMediaRules::rules(hosted: false);
expect($rules['media.*.id'])->toContain('nullable')
->and($rules['media.*.url'])->toContain('url:http,https')
->and($rules)->not->toHaveKey('media.*.source')
->and($rules)->not->toHaveKey('media.*.source_meta');
});
test('both variants keep the shared item keys so validated() preserves them', function () {
foreach ([true, false] as $hosted) {
expect(PostMediaRules::rules(hosted: $hosted))->toHaveKeys([
'media.*.id',
'media.*.path',
'media.*.url',
'media.*.type',
'media.*.mime_type',
'media.*.original_filename',
'media.*.size',
'media.*.meta',
]);
}
});