client = static::createClient(server: ['HTTPS' => true]); $this->client->disableReboot(); $this->entityManager = self::getContainer()->get(EntityManagerInterface::class); $this->entityManager->getConnection()->beginTransaction(); $this->user = (new User()) ->setFirstName('Mia') ->setLastName('Manager') ->setEmail(uniqid('template.', true).'@example.com') ->setRoles(['ROLE_MANAGER']) ->setBusProAddressId(1) ->setBusProPersonId(1) ; $this->entityManager->persist($this->user); $this->entityManager->flush(); $this->client->loginUser($this->user); } protected function tearDown(): void { $this->entityManager->getConnection()->rollBack(); parent::tearDown(); } public function testAManagerCreatesATemplate(): void { $this->client->request('GET', self::LIST_URL.'/create'); $this->assertResponseIsSuccessful(); $this->client->submitForm('Speichern', [ 'mailing_template[name]' => 'Packliste', 'mailing_template[subject]' => 'Deine Packliste', 'mailing_template[message]' => 'Hallo {{vorname}}', ]); $this->assertResponseRedirects(self::LIST_URL); $template = $this->entityManager->getRepository(MailingTemplate::class)->findOneBy(['name' => 'Packliste']); $this->assertNotNull($template); $this->assertSame('Deine Packliste', $template->getSubject()); } public function testATemplateWithoutSubjectIsNotSaved(): void { $this->client->request('GET', self::LIST_URL.'/create'); $this->client->submitForm('Speichern', [ 'mailing_template[name]' => 'Packliste', 'mailing_template[subject]' => '', 'mailing_template[message]' => 'Hallo', ]); // re-rendered rather than redirected, like every form in this app $this->assertResponseIsSuccessful(); $this->assertSelectorTextContains('body', 'Bitte gib einen Betreff an'); $this->assertNull($this->entityManager->getRepository(MailingTemplate::class)->findOneBy(['name' => 'Packliste'])); } /** * Deleting is soft: the mailings sent from the template keep pointing at it. It is gone from * the list and can no longer be edited. */ public function testDeletingATemplateHidesItButKeepsTheRow(): void { $template = $this->template('Packliste'); $this->client->request('POST', sprintf('%s/delete/%s', self::LIST_URL, $template->getUuid())); $this->assertSame(self::LIST_URL, $this->client->getResponse()->headers->get('HX-Redirect')); $this->entityManager->clear(); $this->assertTrue($this->entityManager->find(MailingTemplate::class, $template->getId())->isDeleted()); $crawler = $this->client->request('GET', self::LIST_URL); $this->assertStringNotContainsString('Packliste', $crawler->filter('table')->text()); $this->client->request('GET', sprintf('%s/edit/%s', self::LIST_URL, $template->getUuid())); $this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND); } /** * A file a sent mailing went out with stays in that record when it comes off the template; * one nobody sent is deleted with it. */ public function testTickedAttachmentsComeOffTheTemplateOnSave(): void { $template = $this->template('Packliste'); $sent = $this->attachment($template, 'Anreise.pdf'); $unsent = $this->attachment($template, 'Entwurf.pdf'); $this->entityManager->persist((new Mailing())->setSubject('S')->setMessage('M')->addAttachment($sent)); $this->entityManager->flush(); $sentId = $sent->getId(); $unsentId = $unsent->getId(); $crawler = $this->client->request('GET', sprintf('%s/edit/%s', self::LIST_URL, $template->getUuid())); $form = $crawler->selectButton('Speichern')->form(); foreach ($form['mailing_template[removeAttachments]'] as $checkbox) { $checkbox->tick(); } $this->client->submit($form); $this->assertResponseRedirects(self::LIST_URL); $this->entityManager->clear(); $this->assertCount(0, $this->entityManager->find(MailingTemplate::class, $template->getId())->getAttachments()); $this->assertNotNull($this->entityManager->find(Upload::class, $sentId)); $this->assertNull($this->entityManager->find(Upload::class, $unsentId)); } private function template(string $name): MailingTemplate { $template = (new MailingTemplate())->setName($name)->setSubject('Betreff')->setMessage('Nachricht'); $this->entityManager->persist($template); $this->entityManager->flush(); return $template; } private function attachment(MailingTemplate $template, string $filename): Upload { // No file on disk: removing a missing file is a no-op for UploadHandler::removeUploadedFile(). $upload = (new Upload()) ->setType(Upload::TYPE_MAILING_TEMPLATE) ->setFilename(sprintf('test-%s-%s', bin2hex(random_bytes(4)), $filename)) ->setOriginalFilename($filename) ->setMimeType('application/pdf') ->setSize(1024) ->setOwner($this->user) ; $template->addAttachment($upload); $this->entityManager->persist($upload); $this->entityManager->flush(); return $upload; } }