Skip to content

Commit

Permalink
Merge pull request #9 from tectonic/bugfix/ignore-not-loaded-relation…
Browse files Browse the repository at this point in the history
…ship

Bugfix - Add support for translating deep model relationships with one missing relation
  • Loading branch information
fabriciozeferino authored Feb 28, 2023
2 parents 7bb9957 + fa35d86 commit 5796c16
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/Translator/Transformers/CollectionTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected function modelTranslations(Collection $translations, Model $model, boo

protected function parseRelationsTranslations(array $relations, Collection $translations): Collection
{

$relationTranslations = collect();
foreach ($relations as $relation) {
if ($relation instanceof Collection) {
Expand All @@ -130,11 +131,13 @@ protected function parseRelationsTranslations(array $relations, Collection $tran
->groupBy(fn ($item) => $this->groupByKey($item))
);
}
} else {
} elseif(!is_null($relation)) {
$relationTranslations->push($translations->get($this->groupByKey($relation)));
if (array_filter($relation->getRelations())) {

if ($deepRelations = array_filter($relation->getRelations())) {

$relationTranslations = $relationTranslations->merge(
$this->parseRelationsTranslations($relation->getRelations(), $translations)
$this->parseRelationsTranslations($deepRelations, $translations)
);
}
}
Expand Down
27 changes: 22 additions & 5 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Tests\Fixtures\Models\Content;
use Tests\Fixtures\Models\Link;
use Tests\Fixtures\Models\Post;
use Tests\Fixtures\Models\Reviewer;

class EndToEndTest extends AcceptanceTestCase
{
Expand Down Expand Up @@ -92,7 +93,6 @@ public function testTranslationsForCollectionRelationships()
public function testTranslationsForCollectionDeepRelationships()
{
$category = Category::with(['content', 'content.links', 'content.author', 'content.author.posts'])->get();

$translated = Translator::translate($category)->first();

$this->assertCount(2, $translated->content);
Expand All @@ -112,14 +112,29 @@ public function testTranslationsForModelDeepRelationships()

$translated = Translator::translate($category);

$this->assertCount(3, $translated->content);
$this->assertCount(4, $translated->content);
$this->assertCount(2, $translated->content[0]->links);
$this->assertEquals('This is what we shall do', $translated->content[0]->trans('en_GB', 'title'));
$this->assertEquals('This is a link', $translated->content[0]->links[0]->trans('en_GB', 'title'));
$this->assertEquals('Author 2 summary', $translated->content[0]->author->trans('en_GB', 'summary'));
$this->assertEquals('This is a title 3', $translated->content[0]->author->posts[0]->trans('en_GB', 'title'));
}

public function testTranslationsForModelDeepRelationshipsWithMissingRelation()
{
$links = Link::with(['content', 'content.links.reviewer', 'content.author'])->get();
$translated = Translator::translate($links);

$this->assertCount(11, $translated);
$link = $translated->first();
$this->assertCount(2, $link->content->links);
$this->assertEquals('This is a link', $link->trans('en_GB', 'title'));
$this->assertEquals('This is what we shall do', $link->content->trans('en_GB', 'title'));
$this->assertEquals('This is a link',$link->content->links[0]->trans('en_GB', 'title'));
$this->assertEquals('Author 1 summary', $link->content->author->trans('en_GB', 'summary'));
$this->assertNull($link->content->author->posts[0]->trans('en_GB', 'title'));
}

/**
* Tests translations for nested relationship collections.
*/
Expand Down Expand Up @@ -161,6 +176,7 @@ private function createContent()
$this->author2->content()->save($this->content3 = $this->category2->content()->save(new Content));
$this->author2->content()->save($this->content4 = $this->category2->content()->save(new Content));
$this->author2->content()->save($this->content5 = $this->category2->content()->save(new Content));
$this->content6 = $this->category2->content()->save(new Content);
}

/**
Expand All @@ -178,6 +194,7 @@ private function createLinks()
$this->content4->links()->save(new Link);
$this->content5->links()->save(new Link);
$this->content5->links()->save(new Link);
$this->content6->links()->save(new Link);
}

/**
Expand Down Expand Up @@ -292,7 +309,7 @@ private function createTranslations()
'field' => 'title',
'value' => 'This is a link'
]);

Translation::create([
'language' => 'en_GB',
'resource' => 'Link',
Expand Down Expand Up @@ -332,14 +349,14 @@ private function createTranslations()
'field' => 'title',
'value' => 'This is a title 3'
]);

Translation::create([
'language' => 'en_GB',
'resource' => 'Post',
'foreign_id' => $this->post4->id,
'field' => 'title',
'value' => 'This is a title 4'
]);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
Schema::create('content', function(Blueprint $table)
{
$table->increments('id');
$table->integer('category_id')->index();
$table->integer('category_id')->index()->nullable();
$table->integer('author_id')->nullable()->index();
$table->timestamps();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateReviewersTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('reviewers', function(Blueprint $table)
{
$table->increments('id');
$table->integer('link_id')->index();
$table->timestamps();

$table->foreign('link_id')->references('id')->on('links');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('reviewers');
}

}
7 changes: 6 additions & 1 deletion tests/Fixtures/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public function content()
{
return $this->belongsTo(Content::class);
}


public function reviewer()
{
return $this->hasOne(Reviewer::class);
}


/**
* Returns an array of the field names that can be used for translations.
Expand Down
29 changes: 29 additions & 0 deletions tests/Fixtures/Models/Reviewer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Tests\Fixtures\Models;

use Tectonic\LaravelLocalisation\Database\TranslationRetriever;
use Tectonic\Localisation\Contracts\Translatable;
use Tectonic\Localisation\Translator\Translations;

class Reviewer extends \Eloquent implements Translatable
{
use Translations;
use TranslationRetriever;

public $table = 'reviewers';

public function link()
{
return $this->belongsTo(Link::class);
}

/**
* Returns an array of the field names that can be used for translations.
*
* @return array
*/
public function getTranslatableFields()
{
return ['title', 'description'];
}
}

0 comments on commit 5796c16

Please sign in to comment.