uploadSession = new UploadSessionDto(); $this->uploadHandler = $this->createMock(UploadHandler::class); $this->uploadHandler->method('getUploadSession')->willReturnCallback(fn () => $this->uploadSession); $this->uploadHandler->method('findOrphanageFilepath')->willReturnCallback( fn (string $type, string $filename) => in_array($filename, $this->goneFromOrphanage, true) ? null : '/orphanage/'.$filename ); $this->mailingRepository = $this->createMock(MailingRepository::class); $this->entityManager = $this->createMock(EntityManagerInterface::class); $this->handler = new MailingAttachmentHandler( $this->uploadHandler, $this->mailingRepository, $this->entityManager, $this->createStub(FeatureManagerInterface::class), ); } public function testAttachmentsWithinTheLimitPass(): void { $this->drop('a.pdf', 4 * self::MB, Upload::TYPE_MAILING_ATTACHMENT); $this->assertNull($this->handler->validate(Upload::TYPE_MAILING_ATTACHMENT, 6 * self::MB)); } public function testStoredAndPendingFilesTogetherMayNotPassTheLimit(): void { $this->drop('a.pdf', 4 * self::MB, Upload::TYPE_MAILING_ATTACHMENT); $error = $this->handler->validate(Upload::TYPE_MAILING_ATTACHMENT, 6 * self::MB + 1); $this->assertSame( 'Die Anhänge sind zusammen 10,1 MB groß, erlaubt sind 10,0 MB - sonst kommt die Mail nicht an', $error ); } /** * The template form and the compose page drop into the same session, told apart by type * only. One page's files must not count against - or be saved by - the other. */ public function testOnlyThePendingFilesOfTheGivenTypeCount(): void { $this->drop('template.pdf', 5 * self::MB, Upload::TYPE_MAILING_TEMPLATE); $this->drop('mailing.pdf', 1 * self::MB, Upload::TYPE_MAILING_ATTACHMENT); $this->assertSame(1 * self::MB, $this->handler->getPendingSize(Upload::TYPE_MAILING_ATTACHMENT)); $this->assertNull($this->handler->validate(Upload::TYPE_MAILING_ATTACHMENT, 9 * self::MB)); } public function testAPendingFileGoneFromTheOrphanageIsNamedAndDropped(): void { $this->drop('a.pdf', self::MB, Upload::TYPE_MAILING_ATTACHMENT, 'Packliste.pdf'); $this->goneFromOrphanage = ['a.pdf']; $this->uploadHandler ->expects($this->once()) ->method('removeUploadFromSession') ->with('uuid-a.pdf') ; $this->assertSame( 'Bitte lade Packliste.pdf noch einmal hoch, sie liegt nicht mehr bereit', $this->handler->validate(Upload::TYPE_MAILING_ATTACHMENT, 0) ); } public function testFilesTickedForRemovalNoLongerCountTowardsTheTemplateLimit(): void { $kept = $this->stored(6 * self::MB); $removed = $this->stored(3 * self::MB); $template = (new MailingTemplate())->addAttachment($kept)->addAttachment($removed); $this->drop('a.pdf', 3 * self::MB, Upload::TYPE_MAILING_TEMPLATE); $this->assertNotNull($this->handler->validateTemplateAttachments($template, [])); $this->assertNull($this->handler->validateTemplateAttachments($template, [$removed])); } /** * A sent mailing lists the files it went out with; taking one off the template must not take * it out of that record. */ public function testAFileASentMailingListsIsDetachedButKept(): void { $upload = $this->stored(self::MB); $template = (new MailingTemplate())->addAttachment($upload); $this->mailingRepository->method('hasAttachment')->willReturn(true); $this->entityManager->expects($this->never())->method('remove'); $this->uploadHandler->expects($this->never())->method('removeUploadedFile'); $this->handler->saveTemplateAttachments($template, [$upload], new User()); $this->assertTrue($template->getAttachments()->isEmpty()); $this->assertNull($upload->getMailingTemplate()); } public function testAFileNoMailingListsIsDeleted(): void { $upload = $this->stored(self::MB); $template = (new MailingTemplate())->addAttachment($upload); $this->mailingRepository->method('hasAttachment')->willReturn(false); $this->entityManager->expects($this->once())->method('remove')->with($upload); $this->uploadHandler->expects($this->once())->method('removeUploadedFile')->with($upload); $this->handler->saveTemplateAttachments($template, [$upload], new User()); } private function drop(string $filename, int $size, string $type, ?string $originalFilename = null): void { $this->uploadSession->addUpload(new UploadDto( 'uuid-'.$filename, $filename, $originalFilename ?? $filename, 'application/pdf', $size, $type )); } private function stored(int $size): Upload { return (new Upload()) ->setType(Upload::TYPE_MAILING_TEMPLATE) ->setFilename(uniqid().'.pdf') ->setOriginalFilename('Datei.pdf') ->setSize($size) ; } }