Skip to content

Commit

Permalink
Fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
MohmmedAshraf committed Jan 19, 2024
1 parent 45cb070 commit 8bdbb7e
Show file tree
Hide file tree
Showing 21 changed files with 481 additions and 385 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"nunomaduro/collision": "^7.0",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.18",
"pestphp/pest-plugin-faker": "^2.0",
"pestphp/pest-plugin-laravel": "^2.2",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion database/factories/LanguageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class LanguageFactory extends Factory
public function definition(): array
{
return [
'rtl' => $this->faker->boolean(),
'code' => $this->faker->randomElement(['en', 'nl', 'fr', 'de', 'es', 'it', 'pt', 'ru', 'ja', 'zh']),
'name' => $this->faker->randomElement(['English', 'Dutch', 'French', 'German', 'Spanish', 'Italian', 'Portuguese', 'Russian', 'Japanese', 'Chinese']),
'rtl' => $this->faker->boolean(),
];
}
}
9 changes: 8 additions & 1 deletion database/factories/TranslationFileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ public function definition(): array
{
return [
'name' => $this->faker->randomElement(['app', 'auth', 'pagination', 'passwords', 'validation']),
'extension' => $this->faker->randomElement(['json', 'php']),
'extension' => 'php',
'is_root' => false,
];
}

public function json(): self
{
return $this->state([
'extension' => 'json',
]);
}
}
14 changes: 2 additions & 12 deletions database/migrations/add_is_root_to_translation_files_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@

return new class() extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::table('ltu_translation_files', function (Blueprint $table) {
$table->boolean('is_root')->default(false)->after('extension');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::table('ltu_translation_files', function (Blueprint $table) {
$table->dropColumn('is_root');
Expand Down
15 changes: 13 additions & 2 deletions src/Actions/CopyPhrasesFromSourceAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Outhebox\TranslationsUI\Actions;

use Outhebox\TranslationsUI\Models\Translation;
use Outhebox\TranslationsUI\Models\TranslationFile;

class CopyPhrasesFromSourceAction
{
Expand All @@ -11,14 +12,24 @@ public static function execute(Translation $translation): void
$sourceTranslation = Translation::where('source', true)->first();

$sourceTranslation->phrases()->with('file')->get()->each(function ($sourcePhrase) use ($translation) {
$file = $sourcePhrase->file;

if ($file->is_root) {
$file = TranslationFile::firstOrCreate([
'is_root' => true,
'extension' => $file->extension,
'name' => $translation->language->code,
]);
}

$translation->phrases()->create([
'value' => null,
'uuid' => str()->uuid(),
'key' => $sourcePhrase->key,
'group' => $sourcePhrase->group,
'group' => $file->name,
'phrase_id' => $sourcePhrase->id,
'parameters' => $sourcePhrase->parameters,
'translation_file_id' => $sourcePhrase->file->id,
'translation_file_id' => $file->id,
]);
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/Actions/SyncPhrasesAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ public static function execute(Translation $source, $key, $value, $locale, $file
'source' => config('translations.source_language') === $locale,
]);

$isRoot = $file === $locale.'.json' || $file === $locale.'.php';

$translationFile = TranslationFile::firstOrCreate([
'name' => pathinfo($file, PATHINFO_FILENAME),
'extension' => pathinfo($file, PATHINFO_EXTENSION),
'is_root' => $isRoot,
]);

$key = config('translations.include_file_in_key') && ! $isRoot ? "{$translationFile->name}.{$key}" : $key;

$translation->phrases()->updateOrCreate([
'key' => $key,
'group' => $translationFile->name,
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Controllers/TranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public function store(Request $request): RedirectResponse
'languages' => 'required|array',
]);

$selectedLanguageIds = $request->input('languages');
$languages = Language::whereIn('id', $selectedLanguageIds)->get();
$languages = Language::whereIn('id', $request->input('languages'))->get();

foreach ($languages as $language) {
CreateTranslationForLanguageAction::execute($language);
Expand Down
6 changes: 3 additions & 3 deletions src/TranslationsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class TranslationsManager
{
public function __construct(
protected Filesystem $filesystem,
protected Filesystem $filesystem
) {
}

Expand Down Expand Up @@ -106,10 +106,10 @@ public function export(): void

foreach ($phrasesTree as $locale => $groups) {
foreach ($groups as $file => $phrases) {
$path = lang_path($file);
$path = lang_path("$locale/$file");

if (! $this->filesystem->isDirectory(dirname($path))) {
$this->filesystem->makeDirectory(dirname($path), 0o755, true);
$this->filesystem->makeDirectory(dirname($path), 0755, true);
}

if (! $this->filesystem->exists($path)) {
Expand Down
1 change: 1 addition & 0 deletions src/TranslationsUIServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function configurePackage(Package $package): void
'create_contributors_table',
'create_contributor_languages_table',
'create_invites_table',
'add_is_root_to_translation_files_table',
])
->hasCommands([
PublishCommand::class,
Expand Down
12 changes: 6 additions & 6 deletions tests/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;
use Brick\VarExporter\VarExporter;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

function createDirectoryIfNotExits($path)
function createDirectoryIfNotExits($path): void
{
// check if $path is a filename or a directory
if (Str::contains($path, '.')) {
Expand All @@ -16,16 +16,16 @@ function createDirectoryIfNotExits($path)
}
}

function createPhpLanguageFile($path, array $content)
function createPhpLanguageFile($path, array $content): void
{
$path = lang_path($path);

createDirectoryIfNotExits($path);

File::put($path, "<?php\n\nreturn " . VarExporter::export($content, VarExporter::TRAILING_COMMA_IN_ARRAY) . ';' . PHP_EOL);
File::put($path, "<?php\n\nreturn ".VarExporter::export($content, VarExporter::TRAILING_COMMA_IN_ARRAY).';'.PHP_EOL);
}

function createJsonLangaueFile($path, array $content)
function createJsonLanguageFile($path, array $content): void
{
$path = lang_path($path);

Expand Down
115 changes: 48 additions & 67 deletions tests/Http/Controllers/Auth/AuthenticatedSessionControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,55 @@
<?php

namespace Outhebox\TranslationsUI\Tests\Http\Controllers\Auth;

use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Outhebox\TranslationsUI\Enums\RoleEnum;
use Outhebox\TranslationsUI\Models\Contributor;
use Outhebox\TranslationsUI\Tests\TestCase;

class AuthenticatedSessionControllerTest extends TestCase
{
/** @test */
public function login_page_can_be_rendered()
{
$this->get(route('ltu.login'))
->assertStatus(200);

}

/** @test */
public function login_request_will_validate_email(): void
{
$response = $this->post(route('ltu.login.attempt'), [
'email' => 'not-an-email',
'password' => 'password',
])->assertRedirect(route('ltu.login'));

$this->assertInstanceOf(ValidationException::class, $response->exception);
}

/** @test */
public function login_request_will_validate_password(): void
{
$response = $this->post(route('ltu.login.attempt'), [
'email' => $this->owner->email,
'password' => 'what-is-my-password',
])->assertSessionHasErrors();

$this->assertInstanceOf(ValidationException::class, $response->exception);
}

/** @test */
public function login_request_will_authenticate_user(): void
{
$this->withoutExceptionHandling();

$user = Contributor::factory([
'role' => RoleEnum::owner,
'password' => Hash::make('password'),
])->create();

$this->post(route('ltu.login.attempt'), [
'email' => $user->email,
'password' => 'password',
])->assertRedirect(route('ltu.translation.index'));
}

/** @test */
public function authenticated_users_can_access_dashboard(): void
{
$this->actingAs($this->owner, 'translations')
->get(route('ltu.login'))
->assertRedirect(route('ltu.translation.index'));
}

/** @test */
public function authenticated_users_can_logout(): void
{
$this->actingAs($this->owner, 'translations')
->get(route('ltu.logout'))
->assertRedirect(route('ltu.login'));
}
}
it('login page can be rendered', function () {
$this->get(route('ltu.login'))
->assertStatus(200);
});

it('login request will validate email', function () {
$response = $this->post(route('ltu.login.attempt'), [
'email' => 'not-an-email',
'password' => 'password',
])->assertRedirect(route('ltu.login'));

expect($response->exception)->toBeInstanceOf(ValidationException::class);
});

it('login request will validate password', function () {
$response = $this->post(route('ltu.login.attempt'), [
'email' => $this->owner->email,
'password' => 'what-is-my-password',
])->assertSessionHasErrors();

expect($response->exception)->toBeInstanceOf(ValidationException::class);
});

it('login request will authenticate user', function () {
$this->withoutExceptionHandling();

$user = Contributor::factory([
'role' => RoleEnum::owner,
'password' => Hash::make('password'),
])->create();

$this->post(route('ltu.login.attempt'), [
'email' => $user->email,
'password' => 'password',
])->assertRedirect(route('ltu.translation.index'));
});

it('authenticated users can access dashboard', function () {
$this->actingAs($this->owner, 'translations')
->get(route('ltu.login'))
->assertRedirect(route('ltu.translation.index'));
});

it('authenticated users can logout', function () {
$this->actingAs($this->owner, 'translations')
->get(route('ltu.logout'))
->assertRedirect(route('ltu.login'));
});
Loading

0 comments on commit 8bdbb7e

Please sign in to comment.