Files
esun3d/app/code/Esun/MultiLocaleTranslation/Test/Unit/Model/Entity/BlogPostEntityTest.php
T
2026-09-15 10:35:56 +08:00

83 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Test\Unit\Model\Entity;
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
use Esun\MultiLocaleTranslation\Model\Entity\BlogPostEntity;
use Mageplaza\Blog\Model\Post;
use Mageplaza\Blog\Model\PostFactory;
use PHPUnit\Framework\TestCase;
class BlogPostEntityTest extends TestCase
{
public function testLoadsSourceTranslationAndTargetFallbackFromSamePost(): void
{
$post = $this->createMock(Post::class);
$post->method('load')->with(1101)->willReturnSelf();
$post->method('getId')->willReturn(1101);
$post->method('getData')->willReturnMap([
['name', null, 'English title'],
['short_description', null, 'English summary'],
['post_content', null, '<p>English content</p>'],
['meta_title', null, 'English meta title'],
['meta_keywords', null, 'English keywords'],
['meta_description', null, 'English meta description'],
]);
$postFactory = $this->createMock(PostFactory::class);
$postFactory->method('create')->willReturn($post);
$repository = $this->createMock(CmsTranslationRepository::class);
$repository->expects(self::exactly(2))->method('getTranslations')->willReturnOnConsecutiveCalls(
['name' => 'Titre français'],
[]
);
$skippedFields = $this->createMock(SkippedFields::class);
$fields = (new BlogPostEntity($postFactory, $repository, $skippedFields))->getFields(1101, 2, 3);
self::assertSame('Titre français', $fields[0]['source_value']);
self::assertSame('English title', $fields[0]['target_value']);
self::assertTrue($fields[0]['use_default']);
self::assertSame('post_content', $fields[2]['code']);
self::assertTrue($fields[2]['is_html']);
}
public function testSavesTranslationAndDeletesUseDefaultFields(): void
{
$post = $this->createMock(Post::class);
$post->method('load')->with(1101)->willReturnSelf();
$post->method('getId')->willReturn(1101);
$postFactory = $this->createMock(PostFactory::class);
$postFactory->method('create')->willReturn($post);
$repository = $this->createMock(CmsTranslationRepository::class);
$repository->expects(self::once())->method('deleteTranslations')->with(
1101,
3,
CmsTranslationRepository::ENTITY_BLOG_POST,
['meta_keywords']
);
$repository->expects(self::once())->method('saveTranslations')->with(
1101,
3,
CmsTranslationRepository::ENTITY_BLOG_POST,
['name' => 'Deutscher Titel']
);
$entity = new BlogPostEntity(
$postFactory,
$repository,
$this->createMock(SkippedFields::class)
);
$entity->apply(
1101,
3,
['name' => 'Deutscher Titel', 'url_key' => 'must-not-be-saved'],
['meta_keywords' => true]
);
}
}