92 lines
3.1 KiB
PHP
92 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Cms;
|
|
|
|
use App\Models\Article;
|
|
use App\Models\ArticleCategory;
|
|
use App\Models\ArticleTag;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class ArticleApiTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_articles_index_only_returns_active_and_public_for_guests(): void
|
|
{
|
|
Article::factory()->create(['title' => 'Visible']);
|
|
Article::factory()->draft()->create(['title' => 'Draft']);
|
|
Article::factory()->expired()->create(['title' => 'Expired']);
|
|
Article::factory()->scheduled()->create(['title' => 'Scheduled']);
|
|
|
|
$res = $this->getJson('/api/v1/articles?per_page=50');
|
|
|
|
$res->assertOk();
|
|
$res->assertJsonCount(1, 'data');
|
|
$res->assertJsonFragment(['title' => 'Visible']);
|
|
$res->assertJsonMissing(['title' => 'Draft']);
|
|
$res->assertJsonMissing(['title' => 'Expired']);
|
|
$res->assertJsonMissing(['title' => 'Scheduled']);
|
|
}
|
|
|
|
public function test_articles_index_filters_by_type_category_tag_and_search(): void
|
|
{
|
|
$cat = ArticleCategory::factory()->create(['slug' => 'news']);
|
|
$tag = ArticleTag::factory()->create(['slug' => 'health']);
|
|
|
|
$a1 = Article::factory()->create([
|
|
'content_type' => Article::CONTENT_TYPE_NOTICE,
|
|
'title' => 'Needle Drop',
|
|
'content' => 'alpha beta',
|
|
]);
|
|
$a1->categories()->attach($cat);
|
|
$a1->tags()->attach($tag);
|
|
|
|
Article::factory()->create([
|
|
'content_type' => Article::CONTENT_TYPE_BLOG,
|
|
'title' => 'Other',
|
|
'content' => 'gamma delta',
|
|
]);
|
|
|
|
$this->getJson('/api/v1/articles?type=notice&per_page=50')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonFragment(['slug' => $a1->slug]);
|
|
|
|
$this->getJson('/api/v1/articles?category=news&per_page=50')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonFragment(['slug' => $a1->slug]);
|
|
|
|
$this->getJson('/api/v1/articles?tag=health&per_page=50')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonFragment(['slug' => $a1->slug]);
|
|
|
|
$this->getJson('/api/v1/articles?search=needle&per_page=50')
|
|
->assertOk()
|
|
->assertJsonCount(1, 'data')
|
|
->assertJsonFragment(['slug' => $a1->slug]);
|
|
}
|
|
|
|
public function test_articles_show_returns_wrapped_data_and_increments_view_count(): void
|
|
{
|
|
$article = Article::factory()->create([
|
|
'view_count' => 0,
|
|
'title' => 'My Article',
|
|
]);
|
|
|
|
$this->getJson('/api/v1/articles/'.$article->slug)
|
|
->assertOk()
|
|
->assertJsonPath('data.slug', $article->slug)
|
|
->assertJsonStructure([
|
|
'data' => ['id', 'title', 'slug', 'content'],
|
|
'related' => [
|
|
'*' => ['id', 'title', 'slug', 'content_type'],
|
|
],
|
|
]);
|
|
|
|
$this->assertSame(1, $article->refresh()->view_count);
|
|
}
|
|
}
|