Skip to content

Commit

Permalink
fix: prevent query builder from overwriting scopes upon applying filters
Browse files Browse the repository at this point in the history
Merge pull request #257 from JonErickson/feature/prevent-overwriting-scopes
  • Loading branch information
alexzarbn authored Jun 2, 2024
2 parents b69499a + 65dcff6 commit f2b93f7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Drivers/Standard/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function buildQuery($query, Request $request)
$actionMethod = $request->route()->getActionMethod();

if (!$this->intermediateMode) {
$this->applyIncludesToQuery($query, $request);
$this->applyAggregatesToQuery($query, $request);

if (in_array($actionMethod, ['index', 'search', 'show'])) {
if ($actionMethod === 'search') {
$this->applyScopesToQuery($query, $request);
Expand All @@ -81,9 +84,6 @@ public function buildQuery($query, Request $request)
}
$this->applySoftDeletesToQuery($query, $request);
}

$this->applyIncludesToQuery($query, $request);
$this->applyAggregatesToQuery($query, $request);
}

return $query;
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,28 @@ public function scopePublished(Builder $query)
return $query->where('publish_at', '<', Carbon::now());
}

/**
* @param Builder $query
* @param string $dateTime
* @return Builder|\Illuminate\Database\Query\Builder
*/
public function scopePublishedAt(Builder $query, string $dateTime)
{
return $query->where('publish_at', $dateTime);
}

/**
* @param Builder $query
* @param string $direction
* @return Builder|\Illuminate\Database\Query\Builder
*/
public function scopeOrderComments(Builder $query, string $direction = 'asc')
{
return $query->with(['comments' => function (MorphMany $query) use ($direction) {
$query->orderBy('created_at', $direction);
}]);
}

/**
* @param Builder $query
* @return Builder|\Illuminate\Database\Query\Builder
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Drivers/Standard/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Orion\Tests\Unit\Drivers\Standard;

use Carbon\Carbon;
use Illuminate\Routing\Route;
use Mockery;
use Orion\Drivers\Standard\ParamsValidator;
use Orion\Drivers\Standard\QueryBuilder;
use Orion\Drivers\Standard\RelationsResolver;
use Orion\Drivers\Standard\SearchBuilder;
use Orion\Http\Requests\Request;
use Orion\Tests\Fixtures\App\Models\Comment;
use Orion\Tests\Fixtures\App\Models\Post;
use Orion\Tests\Fixtures\App\Models\Team;
use Orion\Tests\Fixtures\App\Models\User;
Expand Down Expand Up @@ -127,6 +129,51 @@ function () {
$this->assertSame($postA->id, $posts->first()->id);
}

/** @test */
public function include_relations_do_not_overwrite_scopes_with_same_relations(): void
{
$request = new Request(['include' => 'comments']);
$request->setRouteResolver(
function () {
return new Route('POST', '/api/posts/search', [ControllerStub::class, 'search']);
}
);
$request->query->set(
'scopes',
[
['name' => 'orderComments', 'parameters' => ['asc']],
]
);

$post = factory(Post::class)->create();

$commentA = factory(Comment::class)->make(['created_at' => Carbon::parse('2019-01-01 09:35:14')]);
$commentA->commentable()->associate($post);
$commentA->save();

$commentB = factory(Comment::class)->make(['created_at' => Carbon::parse('2018-01-01 09:35:14')]);
$commentB->commentable()->associate($post);
$commentB->save();

$query = Post::query();

$queryBuilder = new QueryBuilder(
Post::class,
new ParamsValidator(['orderComments'], [], [], [], ['comments']),
new RelationsResolver(['comments'], []),
new SearchBuilder([])
);
$queryBuilder->buildQuery($query, $request);

$posts = $query->get();

$dates = $posts->first()->comments->map(function(Comment $comment) {
return $comment->created_at;
});

$this->assertTrue(Carbon::parse($dates->first())->isBefore(Carbon::parse($dates->last())));
}

/** @test */
public function applying_model_level_fields_filters_with_singular_values(): void
{
Expand Down

0 comments on commit f2b93f7

Please sign in to comment.