Skip to content

Commit c950b22

Browse files
committed
Ensure we don't only select taxonomy when we actually want all term columns
1 parent eaa33ec commit c950b22

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/Taxonomies/TermQueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function get($columns = ['*'])
175175
$this->applyCollectionAndTaxonomyWheres();
176176

177177
// Ensure 'taxonomy' is always selected as it's required for Term::fromModel().
178-
if (! in_array('*', $columns) && ! in_array('taxonomy', $columns)) {
178+
if (! empty($columns) && ! in_array('*', $columns) && ! in_array('taxonomy', $columns)) {
179179
$columns[] = 'taxonomy';
180180
}
181181

@@ -218,7 +218,7 @@ public function paginate($perPage = null, $columns = [], $pageName = 'page', $pa
218218
$this->applyCollectionAndTaxonomyWheres();
219219

220220
// Ensure 'taxonomy' is always selected as it's required for Term::fromModel().
221-
if (! in_array('*', $columns) && ! in_array('taxonomy', $columns)) {
221+
if (! empty($columns) && ! in_array('*', $columns) && ! in_array('taxonomy', $columns)) {
222222
$columns[] = 'taxonomy';
223223
}
224224

tests/Data/Taxonomies/TermQueryBuilderTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,4 +675,61 @@ public function terms_are_found_using_where_relation()
675675
$this->assertCount(1, $terms);
676676
$this->assertEquals(['c'], $terms->map->slug->all());
677677
}
678+
679+
#[Test]
680+
public function it_ensures_taxonomy_column_is_selected_when_columns_are_specified()
681+
{
682+
Taxonomy::make('tags')->save();
683+
Term::make('a')->taxonomy('tags')->data(['title' => 'Term A', 'description' => 'Description A'])->save();
684+
Term::make('b')->taxonomy('tags')->data(['title' => 'Term B', 'description' => 'Description B'])->save();
685+
686+
// When specific columns are requested (not including taxonomy), taxonomy should be auto-added
687+
$terms = Term::query()->get(['slug', 'data']);
688+
$this->assertCount(2, $terms);
689+
$this->assertEquals(['a', 'b'], $terms->map->slug()->all());
690+
691+
// When empty array is passed, should not add taxonomy column
692+
$terms = Term::query()->get([]);
693+
$this->assertCount(2, $terms);
694+
$this->assertEquals(['a', 'b'], $terms->map->slug()->all());
695+
696+
// When * is requested, taxonomy is included automatically
697+
$terms = Term::query()->get(['*']);
698+
$this->assertCount(2, $terms);
699+
$this->assertEquals(['a', 'b'], $terms->map->slug()->all());
700+
701+
// When taxonomy is already included, should not be duplicated
702+
$terms = Term::query()->get(['slug', 'taxonomy', 'data']);
703+
$this->assertCount(2, $terms);
704+
$this->assertEquals(['a', 'b'], $terms->map->slug()->all());
705+
}
706+
707+
#[Test]
708+
public function it_ensures_taxonomy_column_is_selected_when_paginating_with_columns()
709+
{
710+
Taxonomy::make('tags')->save();
711+
Term::make('a')->taxonomy('tags')->data(['title' => 'Term A'])->save();
712+
Term::make('b')->taxonomy('tags')->data(['title' => 'Term B'])->save();
713+
Term::make('c')->taxonomy('tags')->data(['title' => 'Term C'])->save();
714+
715+
// When specific columns are requested in paginate (not including taxonomy), taxonomy should be auto-added
716+
$paginated = Term::query()->paginate(2, ['slug', 'data']);
717+
$this->assertCount(2, $paginated);
718+
$this->assertEquals(['a', 'b'], $paginated->getCollection()->map->slug()->all());
719+
720+
// When empty array is passed to paginate, should not add taxonomy column
721+
$paginated = Term::query()->paginate(2, []);
722+
$this->assertCount(2, $paginated);
723+
$this->assertEquals(['a', 'b'], $paginated->getCollection()->map->slug()->all());
724+
725+
// When * is requested in paginate, taxonomy is included automatically
726+
$paginated = Term::query()->paginate(2, ['*']);
727+
$this->assertCount(2, $paginated);
728+
$this->assertEquals(['a', 'b'], $paginated->getCollection()->map->slug()->all());
729+
730+
// When taxonomy is already included in paginate, should not be duplicated
731+
$paginated = Term::query()->paginate(2, ['slug', 'taxonomy', 'data']);
732+
$this->assertCount(2, $paginated);
733+
$this->assertEquals(['a', 'b'], $paginated->getCollection()->map->slug()->all());
734+
}
678735
}

0 commit comments

Comments
 (0)