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,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)
);
}
}