Files
michaelschiemer/tests/Unit/Domain/Cms/Services/ContentLocalizationServiceTest.php
Michael Schiemer 2d53270056 feat(cms,asset): add comprehensive test suite and finalize modules
- Add comprehensive test suite for CMS and Asset modules using Pest Framework
- Implement ContentTypeService::delete() protection against deletion of in-use content types
- Add CannotDeleteContentTypeInUseException for better error handling
- Fix DerivatPipelineRegistry::getAllPipelines() to handle object uniqueness correctly
- Fix VariantName::getScale() to correctly parse scales with file extensions
- Update CMS module documentation with new features, exceptions, and test coverage
- Add CmsTestHelpers and AssetTestHelpers for test data factories
- Fix BlockTypeRegistry to be immutable after construction
- Update ContentTypeService to check for associated content before deletion
- Improve BlockRendererRegistry initialization

Test coverage:
- Value Objects: All CMS and Asset value objects
- Services: ContentService, ContentTypeService, SlugGenerator, BlockValidator, ContentLocalizationService, AssetService, DeduplicationService, MetadataExtractor
- Repositories: All database repositories with mocked connections
- Rendering: Block renderers and ContentRenderer
- Controllers: API endpoints for both modules

254 tests passing, 38 remaining (mostly image processing pipeline tests)
2025-11-10 02:12:28 +01:00

174 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Domain\Cms\Entities\Content;
use App\Domain\Cms\Entities\ContentTranslation;
use App\Domain\Cms\Enums\ContentStatus;
use App\Domain\Cms\Repositories\ContentTranslationRepository;
use App\Domain\Cms\Services\ContentLocalizationService;
use App\Domain\Cms\ValueObjects\ContentBlocks;
use App\Domain\Cms\ValueObjects\ContentId;
use App\Domain\Cms\ValueObjects\ContentSlug;
use App\Domain\Cms\ValueObjects\ContentTypeId;
use App\Domain\Cms\ValueObjects\Locale;
use App\Framework\Core\ValueObjects\Timestamp;
use App\Framework\DateTime\SystemClock;
use Tests\Support\CmsTestHelpers;
describe('ContentLocalizationService', function () {
beforeEach(function () {
$this->clock = new SystemClock();
$this->repository = Mockery::mock(ContentTranslationRepository::class);
$this->service = new ContentLocalizationService($this->repository, $this->clock);
});
it('creates translation', function () {
$contentId = ContentId::generate($this->clock);
$locale = Locale::german();
$blocks = ContentBlocks::fromArray([
[
'id' => 'text-1',
'type' => 'text',
'data' => ['content' => 'Deutscher Text'],
],
]);
$this->repository->shouldReceive('save')
->once()
->andReturnNull();
$translation = $this->service->createTranslation(
contentId: $contentId,
locale: $locale,
title: 'Deutscher Titel',
blocks: $blocks
);
expect($translation)->toBeInstanceOf(ContentTranslation::class);
expect($translation->locale->equals($locale))->toBeTrue();
expect($translation->title)->toBe('Deutscher Titel');
});
it('updates translation', function () {
$contentId = ContentId::generate($this->clock);
$locale = Locale::german();
$translation = CmsTestHelpers::createContentTranslation($contentId, $locale);
$this->repository->shouldReceive('findByContentAndLocale')
->once()
->with($contentId, $locale)
->andReturn($translation);
$this->repository->shouldReceive('save')
->once()
->andReturnNull();
$updated = $this->service->updateTranslation(
contentId: $contentId,
locale: $locale,
title: 'Aktualisierter Titel'
);
expect($updated->title)->toBe('Aktualisierter Titel');
});
it('throws exception when updating non-existent translation', function () {
$contentId = ContentId::generate($this->clock);
$locale = Locale::german();
$this->repository->shouldReceive('findByContentAndLocale')
->once()
->with($contentId, $locale)
->andReturn(null);
expect(fn () => $this->service->updateTranslation(
contentId: $contentId,
locale: $locale,
title: 'New Title'
))->toThrow(DomainException::class);
});
it('gets translation', function () {
$contentId = ContentId::generate($this->clock);
$locale = Locale::german();
$translation = CmsTestHelpers::createContentTranslation($contentId, $locale);
$this->repository->shouldReceive('findByContentAndLocale')
->once()
->with($contentId, $locale)
->andReturn($translation);
$found = $this->service->getTranslation($contentId, $locale);
expect($found)->toBe($translation);
});
it('returns content for default locale', function () {
$content = CmsTestHelpers::createContent($this->clock);
$locale = $content->defaultLocale;
$localized = $this->service->getContentForLocale($content, $locale);
expect($localized)->toBe($content);
});
it('returns translated content for non-default locale', function () {
$content = CmsTestHelpers::createContent($this->clock);
$locale = Locale::german();
$translation = CmsTestHelpers::createContentTranslation($content->id, $locale);
$this->repository->shouldReceive('findByContentAndLocale')
->once()
->with($content->id, $locale)
->andReturn($translation);
$localized = $this->service->getContentForLocale($content, $locale);
expect($localized->title)->toBe($translation->title);
});
it('falls back to default locale when translation not found', function () {
$content = CmsTestHelpers::createContent($this->clock);
$locale = Locale::german();
$this->repository->shouldReceive('findByContentAndLocale')
->once()
->with($content->id, $locale)
->andReturn(null);
$localized = $this->service->getContentForLocale($content, $locale);
expect($localized->title)->toBe($content->title);
});
it('gets all translations for content', function () {
$contentId = ContentId::generate($this->clock);
$translations = [
CmsTestHelpers::createContentTranslation($contentId, Locale::german()),
];
$this->repository->shouldReceive('findByContent')
->once()
->with($contentId)
->andReturn($translations);
$found = $this->service->getAllTranslations($contentId);
expect($found)->toBe($translations);
});
it('deletes translation', function () {
$contentId = ContentId::generate($this->clock);
$locale = Locale::german();
$this->repository->shouldReceive('delete')
->once()
->with($contentId, $locale)
->andReturnNull();
$this->service->deleteTranslation($contentId, $locale);
});
});