Added the option to only show attachment types for certain element classes

This commit is contained in:
Jan Böhmer 2026-02-08 19:30:06 +01:00
parent 2c56ec746c
commit a4c2b8f885
8 changed files with 231 additions and 5 deletions

View file

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace App\Tests\Entity\Attachments;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Attachments\PartAttachment;
use App\Entity\Attachments\UserAttachment;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
@ -34,4 +36,51 @@ class AttachmentTypeTest extends TestCase
$this->assertInstanceOf(Collection::class, $attachment_type->getAttachmentsForType());
$this->assertEmpty($attachment_type->getFiletypeFilter());
}
public function testSetAllowedTargets(): void
{
$attachmentType = new AttachmentType();
$this->expectException(\InvalidArgumentException::class);
$attachmentType->setAllowedTargets(['target1', 'target2']);
}
public function testGetSetAllowedTargets(): void
{
$attachmentType = new AttachmentType();
$attachmentType->setAllowedTargets([PartAttachment::class, UserAttachment::class]);
$this->assertSame([PartAttachment::class, UserAttachment::class], $attachmentType->getAllowedTargets());
//Caching should also work
$this->assertSame([PartAttachment::class, UserAttachment::class], $attachmentType->getAllowedTargets());
//Setting null should reset the allowed targets
$attachmentType->setAllowedTargets(null);
$this->assertNull($attachmentType->getAllowedTargets());
}
public function testIsAllowedForTarget(): void
{
$attachmentType = new AttachmentType();
//By default, all targets should be allowed
$this->assertTrue($attachmentType->isAllowedForTarget(PartAttachment::class));
$this->assertTrue($attachmentType->isAllowedForTarget(UserAttachment::class));
//Set specific allowed targets
$attachmentType->setAllowedTargets([PartAttachment::class]);
$this->assertTrue($attachmentType->isAllowedForTarget(PartAttachment::class));
$this->assertFalse($attachmentType->isAllowedForTarget(UserAttachment::class));
//Set both targets
$attachmentType->setAllowedTargets([PartAttachment::class, UserAttachment::class]);
$this->assertTrue($attachmentType->isAllowedForTarget(PartAttachment::class));
$this->assertTrue($attachmentType->isAllowedForTarget(UserAttachment::class));
//Reset allowed targets
$attachmentType->setAllowedTargets(null);
$this->assertTrue($attachmentType->isAllowedForTarget(PartAttachment::class));
$this->assertTrue($attachmentType->isAllowedForTarget(UserAttachment::class));
}
}