This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,82 @@
<?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]
);
}
}
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Test\Unit\Model\Entity;
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
use Esun\MultiLocaleTranslation\Model\Entity\CategoryEntity;
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
use PHPUnit\Framework\TestCase;
class CategoryEntityTest extends TestCase
{
public function testSavesTranslationUsingTargetStoreScope(): void
{
$category = $this->createMock(Category::class);
$category->expects(self::once())->method('setStoreId')->with(24)->willReturnSelf();
$category->expects(self::once())->method('load')->with(21)->willReturnSelf();
$category->method('getId')->willReturn(21);
$category->expects(self::once())->method('setData')->with('name', 'Résine très vendue')->willReturnSelf();
$categoryFactory = $this->createMock(CategoryFactory::class);
$categoryFactory->method('create')->willReturn($category);
$categoryResource = $this->createMock(CategoryResource::class);
$categoryResource->expects(self::once())->method('save')->with($category);
$entity = new CategoryEntity(
$categoryFactory,
$categoryResource,
$this->createMock(ScopeOverriddenValue::class),
$this->createMock(SkippedFields::class)
);
$entity->apply(21, 24, ['name' => 'Résine très vendue']);
}
public function testDoesNotSaveAConfiguredSkippedField(): void
{
$category = $this->createMock(Category::class);
$category->method('setStoreId')->willReturnSelf();
$category->method('load')->willReturnSelf();
$category->method('getId')->willReturn(21);
$category->expects(self::never())->method('setData');
$categoryFactory = $this->createMock(CategoryFactory::class);
$categoryFactory->method('create')->willReturn($category);
$categoryResource = $this->createMock(CategoryResource::class);
$categoryResource->expects(self::once())->method('save')->with($category);
$skippedFields = $this->createMock(SkippedFields::class);
$skippedFields->method('isSkipped')->willReturnCallback(
static fn(string $entityType, string $field): bool => $entityType === 'category' && $field === 'name'
);
$entity = new CategoryEntity(
$categoryFactory,
$categoryResource,
$this->createMock(ScopeOverriddenValue::class),
$skippedFields
);
$entity->apply(21, 24, ['name' => 'Should not be saved']);
}
}
@@ -0,0 +1,76 @@
<?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\WikiCategoryEntity;
use Esun\Wiki\Model\Category;
use Esun\Wiki\Model\CategoryFactory;
use PHPUnit\Framework\TestCase;
class WikiCategoryEntityTest extends TestCase
{
public function testLoadsOnlyWikiContentFieldsAndNeverUrlFields(): void
{
$category = $this->createMock(Category::class);
$category->method('load')->with(12)->willReturnSelf();
$category->method('getId')->willReturn(12);
$category->method('getData')->willReturnMap([
['title', null, 'English title'],
['summary', null, 'English summary'],
['content', null, '<p>English content</p>'],
['meta_title', null, 'English meta'],
['meta_description', null, 'English description'],
['meta_keywords', null, 'English keywords'],
]);
$factory = $this->createMock(CategoryFactory::class);
$factory->method('create')->willReturn($category);
$repository = $this->createMock(CmsTranslationRepository::class);
$repository->expects(self::exactly(2))->method('getTranslations')->willReturnOnConsecutiveCalls(
['title' => 'Deutscher Titel'],
[]
);
$fields = (new WikiCategoryEntity(
$factory,
$repository,
$this->createMock(SkippedFields::class)
))->getFields(12, 16, 17);
self::assertSame('Deutscher Titel', $fields[0]['source_value']);
self::assertSame('English title', $fields[0]['target_value']);
self::assertSame('content', $fields[2]['code']);
self::assertTrue($fields[2]['is_html']);
self::assertNotContains('slug', array_column($fields, 'code'));
self::assertNotContains('custom_url_path', array_column($fields, 'code'));
}
public function testApplyFiltersUnknownAndUrlFields(): void
{
$category = $this->createMock(Category::class);
$category->method('load')->with(12)->willReturnSelf();
$category->method('getId')->willReturn(12);
$factory = $this->createMock(CategoryFactory::class);
$factory->method('create')->willReturn($category);
$repository = $this->createMock(CmsTranslationRepository::class);
$repository->expects(self::once())->method('saveTranslations')->with(
12,
17,
CmsTranslationRepository::ENTITY_WIKI_CATEGORY,
['title' => 'Translated title']
);
(new WikiCategoryEntity(
$factory,
$repository,
$this->createMock(SkippedFields::class)
))->apply(12, 17, [
'title' => 'Translated title',
'slug' => 'must-not-save',
'custom_url_path' => 'must-not-save',
]);
}
}
@@ -0,0 +1,47 @@
<?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\WikiHomepageEntity;
use Esun\Wiki\Model\Homepage;
use Esun\Wiki\Model\ResourceModel\Homepage\Collection;
use Esun\Wiki\Model\ResourceModel\Homepage\CollectionFactory;
use PHPUnit\Framework\TestCase;
class WikiHomepageEntityTest extends TestCase
{
public function testLoadsHomepageTranslationFields(): void
{
$homepage = $this->createMock(Homepage::class);
$homepage->method('getId')->willReturn(1);
$homepage->method('getData')->willReturnMap([
['title', null, 'Wiki'],
['content', null, '<p>Wiki content</p>'],
['meta_title', null, 'Wiki meta'],
['meta_description', null, 'Wiki description'],
['meta_keywords', null, 'Wiki keywords'],
]);
$collection = $this->createMock(Collection::class);
$collection->method('addFieldToFilter')->willReturnSelf();
$collection->method('setPageSize')->willReturnSelf();
$collection->method('getFirstItem')->willReturn($homepage);
$factory = $this->createMock(CollectionFactory::class);
$factory->method('create')->willReturn($collection);
$repository = $this->createMock(CmsTranslationRepository::class);
$repository->method('getTranslations')->willReturn([]);
$fields = (new WikiHomepageEntity(
$factory,
$repository,
$this->createMock(SkippedFields::class)
))->getFields(1, 0, 16);
self::assertSame(['title', 'content', 'meta_title', 'meta_description', 'meta_keywords'],
array_column($fields, 'code'));
self::assertTrue($fields[1]['is_html']);
}
}