Skip to content

Commit

Permalink
Fix corrupted data in 'allowedIncludes' (#118)
Browse files Browse the repository at this point in the history
* Fix corrupted data when including many-to-many relationship

* Adjust tests: prepend table names on query log assertions

* Add test for many-to-many relationship in "allowedIncludes"

* Fix CI issues
  • Loading branch information
rainerkent authored and AlexVanderbist committed Oct 29, 2018
1 parent f19d18b commit 1a4a900
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ protected function addIncludesToQuery(Collection $includes)
}

return [$fullRelationName => function ($query) use ($fields) {
$query->select($fields);
$query->select($this->prependFieldsWithTableName($fields, $query->getModel()->getTable()));
}];
});
})
Expand Down
8 changes: 4 additions & 4 deletions tests/FieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,27 @@ public function it_can_fetch_only_requested_columns_from_an_included_model()
$queryBuilder->first()->relatedModels;

$this->assertQueryLogContains('select "test_models"."id" from "test_models"');
$this->assertQueryLogContains('select "name" from "related_models"');
$this->assertQueryLogContains('select "related_models"."name" from "related_models"');
}

/** @test */
public function it_can_allow_specific_fields_on_an_included_model()
{
$request = new Request([
'fields' => ['related_models' => 'id,first_name'],
'fields' => ['related_models' => 'id,name'],
'include' => ['related-models'],
]);

$queryBuilder = QueryBuilder::for(TestModel::class, $request)
->allowedIncludes('related-models')
->allowedFields(['related_models.id', 'related_models.first_name']);
->allowedFields(['related_models.id', 'related_models.name']);

DB::enableQueryLog();

$queryBuilder->first()->relatedModels;

$this->assertQueryLogContains('select "test_models".* from "test_models"');
$this->assertQueryLogContains('select "id", "first_name" from "related_models"');
$this->assertQueryLogContains('select "related_models"."id", "related_models"."name" from "related_models"');
}

protected function createQueryFromFieldRequest(array $fields): QueryBuilder
Expand Down
18 changes: 18 additions & 0 deletions tests/IncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function setUp()
$model
->relatedModels()->create(['name' => 'Test'])
->nestedRelatedModels()->create(['name' => 'Test']);

$model->relatedThroughPivotModels()->create([
'id' => $model->id + 1,
'name' => 'Test',
]);
});
}

Expand Down Expand Up @@ -140,6 +145,19 @@ public function it_can_include_multiple_model_relations()
$this->assertRelationLoaded($models, 'otherRelatedModels');
}

/** @test */
public function it_returns_correct_id_when_including_many_to_many_relationship()
{
$models = $this
->createQueryFromIncludeRequest('related-through-pivot-models')
->allowedIncludes('related-through-pivot-models')
->get();

$relatedModel = $models->first()->relatedThroughPivotModels->first();

$this->assertEquals($relatedModel->id, $relatedModel->pivot->related_through_pivot_model_id);
}

/** @test */
public function an_invalid_include_query_exception_contains_the_unknown_and_allowed_includes()
{
Expand Down
18 changes: 18 additions & 0 deletions tests/Models/RelatedThroughPivotModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Spatie\QueryBuilder\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class RelatedThroughPivotModel extends Model
{
protected $guarded = [];

public $timestamps = false;

public function testModels(): BelongsToMany
{
return $this->belongsToMany(TestModel::class, 'pivot_models');
}
}
6 changes: 6 additions & 0 deletions tests/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class TestModel extends Model
{
Expand All @@ -21,6 +22,11 @@ public function otherRelatedModels(): HasMany
return $this->hasMany(RelatedModel::class);
}

public function relatedThroughPivotModels(): BelongsToMany
{
return $this->belongsToMany(RelatedThroughPivotModel::class, 'pivot_models');
}

public function scopeNamed(Builder $query, string $name) : Builder
{
return $query->where('name', $name);
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ protected function setUpDatabase(Application $app)
$table->integer('related_model_id');
$table->string('name');
});

$app['db']->connection()->getSchemaBuilder()->create('pivot_models', function (Blueprint $table) {
$table->increments('id');
$table->string('test_model_id');
$table->integer('related_through_pivot_model_id');
});

$app['db']->connection()->getSchemaBuilder()->create('related_through_pivot_models', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
}

protected function getPackageProviders($app)
Expand Down

0 comments on commit 1a4a900

Please sign in to comment.