Skip to content

Commit

Permalink
Merge pull request #7339 from ProcessMaker/task/FOUR-18756
Browse files Browse the repository at this point in the history
FOUR-18756: Create tests for the current screen translations
  • Loading branch information
agustinbusso authored Sep 16, 2024
2 parents b03e6bd + 5f71c25 commit 89c0054
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
172 changes: 172 additions & 0 deletions tests/Feature/Api/TranslationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

namespace Tests\Feature\Api;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use ProcessMaker\Facades\WorkflowManager;
use ProcessMaker\Jobs\ImportV2;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;


class TranslationTest extends TestCase
{
use RequestHelper;

public function testTranslationWithDefaultLanguage()
{
$defaultAdminUser = User::factory()->create(['is_administrator' => true]);
$portugueseUser = User::factory()->create(['is_administrator' => true, 'language' => 'pt']);
$spanishUser = User::factory()->create(['is_administrator' => true, 'language' => 'es']);

$this->user = $defaultAdminUser;

$fileName = __DIR__ . '/../../Fixtures/translation_test.json';
$file = new UploadedFile($fileName, 'translation_test.json', null, null, true);
Storage::putFileAs('import', $file, 'payload.json');
Storage::put('import/options.json', '{}');
$hash = md5_file(Storage::path(ImportV2::FILE_PATH));
ImportV2::dispatchSync($this->user->id, null, $hash, false);

$process = Process::orderBy('id', 'desc')->first();

WorkflowManager::triggerStartEvent(
$process,
$process->getDefinitions()->getEvent('node_1'),
[]
);

$task = ProcessRequestToken::orderBy('id', 'desc')->first();

// The first task should be created
$this->assertNotEmpty($task);

$url = route('api.tasks.show', [$task->id]);
$response = $this->apiCall('GET', "$url?include=screen");
$response->assertStatus(200);

$responseData = $response->json();
$items = collect($responseData['screen']['config'][0]['items']);
$firstNameItem = $items->firstWhere('config.name', 'first_name');
$lastNameItem = $items->firstWhere('config.name', 'last_name');
$ageItem = $items->firstWhere('config.name', 'age');

$this->assertEquals('First Name', $firstNameItem['config']['label']);
$this->assertEquals('Last Name', $lastNameItem['config']['label']);
$this->assertEquals('Age', $ageItem['config']['label']);


// As portuguese is not translated, english must be used
$this->user = $portugueseUser;
$response = $this->apiCall('GET', "$url?include=screen");
$responseData = $response->json();
$items = collect($responseData['screen']['config'][0]['items']);
$firstNameItem = $items->firstWhere('config.name', 'first_name');
$lastNameItem = $items->firstWhere('config.name', 'last_name');
$ageItem = $items->firstWhere('config.name', 'age');

$this->assertEquals('First Name', $firstNameItem['config']['label']);
$this->assertEquals('Last Name', $lastNameItem['config']['label']);
$this->assertEquals('Age', $ageItem['config']['label']);


// Spanish has translation, so it should be used for the screen:
$this->user = $spanishUser;
$response = $this->apiCall('GET', "$url?include=screen");
$responseData = $response->json();
$items = collect($responseData['screen']['config'][0]['items']);
$firstNameItem = $items->firstWhere('config.name', 'first_name');
$lastNameItem = $items->firstWhere('config.name', 'last_name');
$ageItem = $items->firstWhere('config.name', 'age');

$this->assertEquals('Nombre', $firstNameItem['config']['label']);
$this->assertEquals('Apellido', $lastNameItem['config']['label']);
$this->assertEquals('Edad', $ageItem['config']['label']);
}


public function testTranslationWithLanguageThatDoesNotHaveTranslation()
{
$defaultAdminUser = User::factory()->create(['is_administrator' => true]);
$portugueseUser = User::factory()->create(['is_administrator' => true, 'language' => 'pt']);

$this->user = $defaultAdminUser;

$fileName = __DIR__ . '/../../Fixtures/translation_test.json';
$file = new UploadedFile($fileName, 'translation_test.json', null, null, true);
Storage::putFileAs('import', $file, 'payload.json');
Storage::put('import/options.json', '{}');
$hash = md5_file(Storage::path(ImportV2::FILE_PATH));
ImportV2::dispatchSync($this->user->id, null, $hash, false);

$process = Process::orderBy('id', 'desc')->first();

WorkflowManager::triggerStartEvent(
$process,
$process->getDefinitions()->getEvent('node_1'),
[]
);

$task = ProcessRequestToken::orderBy('id', 'desc')->first();

$this->user = $portugueseUser;
$url = route('api.tasks.show', [$task->id]);
$response = $this->apiCall('GET', "$url?include=screen");
$response->assertStatus(200);

// As portuguese is not translated, english must be used
$response = $this->apiCall('GET', "$url?include=screen");
$responseData = $response->json();
$items = collect($responseData['screen']['config'][0]['items']);
$firstNameItem = $items->firstWhere('config.name', 'first_name');
$lastNameItem = $items->firstWhere('config.name', 'last_name');
$ageItem = $items->firstWhere('config.name', 'age');

$this->assertEquals('First Name', $firstNameItem['config']['label']);
$this->assertEquals('Last Name', $lastNameItem['config']['label']);
$this->assertEquals('Age', $ageItem['config']['label']);
}

public function testTranslationWithLanguageThatHasTranslation()
{

$spanishUser = User::factory()->create(['is_administrator' => true, 'language' => 'es']);
$this->user = User::factory()->create(['is_administrator' => true]);

$fileName = __DIR__ . '/../../Fixtures/translation_test.json';
$file = new UploadedFile($fileName, 'translation_test.json', null, null, true);
Storage::putFileAs('import', $file, 'payload.json');
Storage::put('import/options.json', '{}');
$hash = md5_file(Storage::path(ImportV2::FILE_PATH));
ImportV2::dispatchSync($this->user->id, null, $hash, false);

$process = Process::orderBy('id', 'desc')->first();

WorkflowManager::triggerStartEvent(
$process,
$process->getDefinitions()->getEvent('node_1'),
[]
);

$newTask = ProcessRequestToken::orderBy('id', 'desc')->first();
$url = route('api.tasks.show', [$newTask->id]);
$response = $this->apiCall('GET', "$url?include=screen");

// Spanish has translation, so it should be used for the screen:
$this->user = $spanishUser;
$response = $this->apiCall('GET', "$url?include=screen");
$responseData = $response->json();
$items = collect($responseData['screen']['config'][0]['items']);
$firstNameItem = $items->firstWhere('config.name', 'first_name');
$lastNameItem = $items->firstWhere('config.name', 'last_name');
$ageItem = $items->firstWhere('config.name', 'age');

$this->assertEquals('Nombre', $firstNameItem['config']['label']);
$this->assertEquals('Apellido', $lastNameItem['config']['label']);
$this->assertEquals('Edad', $ageItem['config']['label']);
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/translation_test.json

Large diffs are not rendered by default.

0 comments on commit 89c0054

Please sign in to comment.