Skip to content

Commit

Permalink
Fix PHP Error when Term Taxonomy points to non-existing term_id
Browse files Browse the repository at this point in the history
  • Loading branch information
kasus committed May 12, 2021
1 parent d1d6846 commit 1c953d6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,11 @@ public function getTermsAttribute()
return $taxonomy->taxonomy == 'post_tag' ?
'tag' : $taxonomy->taxonomy;
})->map(function ($group) {
return $group->mapWithKeys(function ($item) {
return [$item->term->slug => $item->term->name];
});
return $group
->filter(fn($item) => $item->term !== NULL)
->mapWithKeys(function ($item) {
return [$item->term->slug => $item->term->name];
});
})->toArray();
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Model/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,26 @@ public function test_it_can_have_term()
$this->assertEquals('Bar', $post->keywords_str);
}

/** @doesNotPerformAssertions */
public function test_it_can_handle_corrupted_term()
{
$post = factory(Post::class)->create();

$taxonomy = factory(Taxonomy::class)->create([
'term_id' => 0,
'taxonomy' => 'category'
]);

$post->taxonomies()->attach(
$taxonomy->getKey(),
[
'term_order' => 0,
]
);

$post->terms;
}

public function test_it_can_have_author_relation()
{
$post = $this->createPostWithAuthor();
Expand Down

0 comments on commit 1c953d6

Please sign in to comment.