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,49 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Test\Unit\Plugin\Adminhtml;
use Esun\MultiLocaleTranslation\Plugin\Adminhtml\BlogPostEditButton;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\Registry;
use Mageplaza\Blog\Block\Adminhtml\Post\Edit;
use Mageplaza\Blog\Model\Post;
use PHPUnit\Framework\TestCase;
class BlogPostEditButtonTest extends TestCase
{
public function testAddsTranslateButtonThroughContainerApi(): void
{
$post = $this->createMock(Post::class);
$post->method('getId')->willReturn(1);
$registry = $this->createMock(Registry::class);
$registry->method('registry')->with('mageplaza_blog_post')->willReturn($post);
$authorization = $this->createMock(AuthorizationInterface::class);
$authorization->method('isAllowed')->with(
'Esun_MultiLocaleTranslation::translate_blog'
)->willReturn(true);
$request = $this->createMock(RequestInterface::class);
$request->method('getParam')->with('store')->willReturn(16);
$subject = $this->createMock(Edit::class);
$subject->method('getRequest')->willReturn($request);
$subject->method('getUrl')->willReturnCallback(
static fn (string $route): string => '/' . $route
);
$subject->expects(self::once())->method('addButton')->with(
'localetranslate_translate',
self::callback(
static fn (array $data): bool => (string)$data['label'] === 'Translate'
&& str_contains($data['onclick'], '"entityType":"blog_post"')
&& str_contains($data['onclick'], '"sourceStoreId":16')
),
0,
30
);
$plugin = new BlogPostEditButton($registry, $authorization);
self::assertSame([], $plugin->beforeToHtml($subject));
}
}
@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Test\Unit\Plugin\Adminhtml;
use Esun\MultiLocaleTranslation\Model\Entity\BlogPostEntity;
use Esun\MultiLocaleTranslation\Plugin\Adminhtml\BlogPostSave;
use Magento\Framework\App\Cache\Manager as CacheManager;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\Message\ManagerInterface;
use Mageplaza\Blog\Controller\Adminhtml\Post\Save;
use PHPUnit\Framework\TestCase;
class BlogPostSaveTest extends TestCase
{
public function testDefaultScopeUsesMageplazaSaveController(): void
{
$request = $this->createMock(RequestInterface::class);
$request->method('getParam')->with('post', [])->willReturn([]);
$plugin = $this->createPlugin($request);
$expected = new \stdClass();
self::assertSame(
$expected,
$plugin->aroundExecute($this->createMock(Save::class), static fn () => $expected)
);
}
public function testStoreScopeSavesOnlyTranslatedFieldWhitelist(): void
{
$request = $this->createMock(RequestInterface::class);
$request->method('getParam')->willReturnCallback(
static fn (string $name, mixed $default = null): mixed => match ($name) {
'post' => [
'translation_store_id' => 16,
'name' => 'Translated post',
'url_key' => 'must-not-be-saved',
'image' => 'must-not-be-saved.jpg',
'use_default' => ['meta_keywords' => '1'],
],
'id' => 1,
default => $default,
}
);
$entity = $this->createMock(BlogPostEntity::class);
$entity->expects(self::once())->method('apply')->with(
1,
16,
['name' => 'Translated post'],
['meta_keywords' => '1']
);
$redirect = $this->createMock(Redirect::class);
$redirect->expects(self::once())->method('setPath')->with(
'mageplaza_blog/post/edit',
['id' => 1, 'store' => 16]
)->willReturnSelf();
$redirectFactory = $this->createMock(RedirectFactory::class);
$redirectFactory->method('create')->willReturn($redirect);
$cacheManager = $this->createMock(CacheManager::class);
$cacheManager->expects(self::once())->method('clean')->with(['full_page', 'block_html']);
$authorization = $this->createMock(AuthorizationInterface::class);
$authorization->method('isAllowed')->willReturn(true);
$plugin = new BlogPostSave(
$request,
$entity,
$redirectFactory,
$this->createMock(ManagerInterface::class),
$cacheManager,
$authorization
);
self::assertSame(
$redirect,
$plugin->aroundExecute(
$this->createMock(Save::class),
static fn () => self::fail('The Mageplaza save controller must not run for a translated scope.')
)
);
}
private function createPlugin(RequestInterface $request): BlogPostSave
{
return new BlogPostSave(
$request,
$this->createMock(BlogPostEntity::class),
$this->createMock(RedirectFactory::class),
$this->createMock(ManagerInterface::class),
$this->createMock(CacheManager::class),
$this->createMock(AuthorizationInterface::class)
);
}
}