Files
piratepoet/mailpoet/tests/integration/NewsletterTemplates/ThumbnailSaverTest.php
2024-03-21 16:57:59 +01:00

65 lines
2.8 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\NewsletterTemplates;
use MailPoet\Config\Env;
use MailPoet\Entities\NewsletterTemplateEntity;
class ThumbnailSaverTest extends \MailPoetTest {
/** @var ThumbnailSaver */
private $thumbnailSaver;
public function _before() {
$this->thumbnailSaver = $this->diContainer->get(ThumbnailSaver::class);
}
public function testItCanSaveThumbnailDataAsFile() {
$template = $this->createTemplate();
$template = $this->thumbnailSaver->ensureTemplateThumbnailFile($template);
$thumbnailUrl = $template->getThumbnail();
verify($thumbnailUrl)->notEmpty();
verify($thumbnailUrl)->isString();
verify($thumbnailUrl)->stringStartsWith(Env::$tempUrl);
verify($thumbnailUrl)->stringContainsString(ThumbnailSaver::THUMBNAIL_DIRECTORY);
[, $fileName] = explode(ThumbnailSaver::THUMBNAIL_DIRECTORY, (string)$thumbnailUrl);
$file = Env::$tempPath . '/' . ThumbnailSaver::THUMBNAIL_DIRECTORY . $fileName;
verify(file_exists($file))->true();
unlink($file); // remove the file after the test
}
public function testItUpdatesBaseUrlIfChanged() {
$template = $this->createTemplate();
$template = $this->thumbnailSaver->ensureTemplateThumbnailFile($template);
$thumbnailUrl = $template->getThumbnail();
$template->setThumbnail(str_replace(Env::$tempUrl, 'http://example.com', (string)$thumbnailUrl));
$template = $this->thumbnailSaver->ensureTemplateThumbnailFile($template);
// Base url was updated back to initial value
$thumbnailUrl = $template->getThumbnail();
verify($thumbnailUrl)->isString();
verify($thumbnailUrl)->stringStartsWith(Env::$tempUrl);
[, $fileName] = explode(ThumbnailSaver::THUMBNAIL_DIRECTORY, (string)$thumbnailUrl);
// File is still the same
verify($thumbnailUrl)->stringEndsWith($fileName);
$file = Env::$tempPath . '/' . ThumbnailSaver::THUMBNAIL_DIRECTORY . $fileName;
unlink($file); // remove the file after the test
}
public function testItSkipsNotBase64JpegData() {
$template = $this->createTemplate();
$template->setThumbnailData('Some data');
$template = $this->thumbnailSaver->ensureTemplateThumbnailFile($template);
$thumbnailUrl = $template->getThumbnail();
// Base url was updated back to initial value
verify($thumbnailUrl)->null();
}
private function createTemplate(): NewsletterTemplateEntity {
$template = new NewsletterTemplateEntity('Template');
$template->setBody([1]);
$template->setThumbnailData('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP//////////////////////////////////////////////////////////////////////////////////////wgALCAABAAEBAREA/8QAFBABAAAAAAAAAAAAAAAAAAAAAP/aAAgBAQABPxA=');
$this->entityManager->persist($template);
$this->entityManager->flush();
return $template;
}
}