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,39 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Test\Unit\Model\Queue;
use Esun\MultiLocaleTranslation\Model\Queue\CronConsumer;
use Esun\MultiLocaleTranslation\Model\Queue\QueueProcessor;
use Magento\Framework\App\Config\ScopeConfigInterface;
use PHPUnit\Framework\TestCase;
class CronConsumerTest extends TestCase
{
public function testUsesConfiguredQueueLimits(): void
{
$processor = $this->createMock(QueueProcessor::class);
$processor->expects(self::once())->method('process')->with(10, 50);
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
$scopeConfig->method('getValue')->willReturnCallback(
static fn(string $path): string => str_ends_with($path, 'tasks_per_run') ? '10' : '50'
);
(new CronConsumer($processor, $scopeConfig))->execute();
}
public function testClampsQueueLimitsToAllowedRange(): void
{
$processor = $this->createMock(QueueProcessor::class);
$processor->expects(self::once())->method('process')->with(50, 10);
$scopeConfig = $this->createMock(ScopeConfigInterface::class);
$scopeConfig->method('getValue')->willReturnCallback(
static fn(string $path): string => str_ends_with($path, 'tasks_per_run') ? '999' : '1'
);
(new CronConsumer($processor, $scopeConfig))->execute();
}
}
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Test\Unit\Model\Queue;
use Esun\MultiLocaleTranslation\Model\EntityPool;
use Esun\MultiLocaleTranslation\Model\ProviderConfig;
use Esun\MultiLocaleTranslation\Model\Queue\QueueManager;
use Esun\MultiLocaleTranslation\Model\StoreViewProvider;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
use PHPUnit\Framework\TestCase;
class QueueManagerTest extends TestCase
{
public function testClearsEveryQueueRecord(): void
{
$select = $this->createMock(Select::class);
$select->method('from')->willReturnSelf();
$select->method('where')->willReturnSelf();
$connection = $this->createMock(AdapterInterface::class);
$connection->method('select')->willReturn($select);
$connection->method('fetchOne')->willReturn('2');
$connection->expects(self::once())
->method('delete')
->with('esun_localetranslation_queue')
->willReturn(18);
$resourceConnection = $this->createMock(ResourceConnection::class);
$resourceConnection->method('getConnection')->willReturn($connection);
$resourceConnection->method('getTableName')->willReturn('esun_localetranslation_queue');
$manager = new QueueManager(
$resourceConnection,
$this->createMock(EntityPool::class),
$this->createMock(ProviderConfig::class),
$this->createMock(StoreViewProvider::class)
);
self::assertSame(['deleted' => 18, 'processing' => 2], $manager->clearAll());
}
}
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Test\Unit\Model\Queue;
use Esun\MultiLocaleTranslation\Api\TranslationProviderInterface;
use Esun\MultiLocaleTranslation\Model\EntityPool;
use Esun\MultiLocaleTranslation\Model\LocaleResolver;
use Esun\MultiLocaleTranslation\Model\ProviderPool;
use Esun\MultiLocaleTranslation\Model\Queue\QueueProcessor;
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
use Esun\MultiLocaleTranslation\Model\TranslationLogger;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;
use PHPUnit\Framework\TestCase;
class QueueProcessorTest extends TestCase
{
public function testProcessesAndSavesOnePendingTask(): void
{
$select = $this->createMock(Select::class);
$select->method('from')->willReturnSelf();
$select->method('where')->willReturnSelf();
$select->method('order')->willReturnSelf();
$select->method('limit')->willReturnSelf();
$connection = $this->createMock(AdapterInterface::class);
$connection->method('select')->willReturn($select);
$connection->method('fetchOne')->willReturn(10);
$connection->method('fetchRow')->willReturn([
'queue_id' => 10,
'provider' => 'deepl',
'entity_type' => 'cms_page',
'entity_id' => 5,
'source_store_id' => 0,
'target_store_id' => 2,
]);
$connection->expects(self::exactly(2))->method('update')->willReturn(1);
$resourceConnection = $this->createMock(ResourceConnection::class);
$resourceConnection->method('getConnection')->willReturn($connection);
$resourceConnection->method('getTableName')->willReturn('esun_localetranslation_queue');
$entity = $this->createMock(TranslatableEntityInterface::class);
$entity->method('getFields')->with(5, 0, 2)->willReturn([
[
'code' => 'title',
'source_value' => 'About us',
'target_value' => '',
'is_html' => false,
'use_default' => true,
],
]);
$entity->expects(self::once())->method('apply')->with(5, 2, ['title' => 'À propos']);
$entityPool = new EntityPool(['cms_page' => $entity]);
$provider = $this->createMock(TranslationProviderInterface::class);
$provider->expects(self::once())->method('translate')
->with(['title' => ['value' => 'About us', 'is_html' => false]], 'EN', 'FR')
->willReturn(['title' => 'À propos']);
$providerPool = new ProviderPool(['deepl' => $provider]);
$localeResolver = $this->createMock(LocaleResolver::class);
$localeResolver->method('getLanguageCode')->willReturnMap([
[0, 'deepl', false, 'EN'],
[2, 'deepl', true, 'FR'],
]);
$logger = $this->createMock(TranslationLogger::class);
$logger->expects(self::once())->method('logSuccess');
$processor = new QueueProcessor(
$resourceConnection,
$entityPool,
$providerPool,
$localeResolver,
$logger
);
self::assertSame(1, $processor->process(1));
}
}