trypost/tests/Feature/PostSearchTest.php
Paulo Castellano 74c6442728 refactor: code review fixes — policies, enums, data_get, tests
- Refactor WorkspacePolicy to use pivot role instead of workspace.user_id
- Add manageBilling policy (owner only) to BillingController
- Fix ApiKeyController authorization (view → manageTeam for store/destroy)
- Fix WorkspaceInviteController using workspace.user_id for owner checks
- Fix WorkspaceController settings is_owner using workspace.user_id
- Create PostAction enum for UpdatePost/PostController action strings
- Create ApiToken\Status enum
- Add User::SUBSCRIPTION_NAME constant, replace all hardcoded 'default'
- Convert wantsEmailFor to accept NotificationType enum
- Convert all $data[] to data_get() across publishers, controllers, jobs
- Fix SocialLoginController callback missing try/catch
- Fix SocialController::toggleActive missing workspace null check
- Fix UpdatePost NPE on meta merge when postPlatform not found
- Remove HTML5 required attributes from form inputs
- Convert function declarations to arrow functions in Vue components
- Replace hardcoded URLs with Wayfinder route helpers
- Replace new Date() with dayjs
- Add 16 new test files covering policies, authorization, publishing
2026-03-31 00:40:18 -03:00

105 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\User\Setup;
use App\Enums\UserWorkspace\Role;
use App\Models\Post;
use App\Models\PostPlatform;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
beforeEach(function () {
$this->user = User::factory()->create(['setup' => Setup::Completed]);
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->user->workspaces()->attach($this->workspace->id, ['role' => Role::Owner->value]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
});
test('search returns matching posts by platform content', function () {
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
$matchingPost = Post::factory()->create(['workspace_id' => $this->workspace->id, 'user_id' => $this->user->id]);
PostPlatform::factory()->create([
'post_id' => $matchingPost->id,
'social_account_id' => $account->id,
'content' => 'Hello marketing world',
'enabled' => true,
]);
$nonMatchingPost = Post::factory()->create(['workspace_id' => $this->workspace->id, 'user_id' => $this->user->id]);
PostPlatform::factory()->create([
'post_id' => $nonMatchingPost->id,
'social_account_id' => $account->id,
'content' => 'Something else entirely',
'enabled' => true,
]);
$response = $this->actingAs($this->user)->get(route('app.posts.index', ['search' => 'marketing']));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->has('posts.data', 1)
->where('filters.search', 'marketing')
);
});
test('search with no matches returns empty', function () {
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
$post = Post::factory()->create(['workspace_id' => $this->workspace->id, 'user_id' => $this->user->id]);
PostPlatform::factory()->create([
'post_id' => $post->id,
'social_account_id' => $account->id,
'content' => 'Hello world',
'enabled' => true,
]);
$response = $this->actingAs($this->user)->get(route('app.posts.index', ['search' => 'nonexistent']));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->has('posts.data', 0)
->where('filters.search', 'nonexistent')
);
});
test('empty search returns all posts', function () {
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
Post::factory()->count(3)->create(['workspace_id' => $this->workspace->id, 'user_id' => $this->user->id])->each(function ($post) use ($account) {
PostPlatform::factory()->create([
'post_id' => $post->id,
'social_account_id' => $account->id,
'enabled' => true,
]);
});
$response = $this->actingAs($this->user)->get(route('app.posts.index'));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->has('posts.data', 3)
->where('filters.search', '')
);
});
test('search is case insensitive', function () {
$account = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]);
$post = Post::factory()->create(['workspace_id' => $this->workspace->id, 'user_id' => $this->user->id]);
PostPlatform::factory()->create([
'post_id' => $post->id,
'social_account_id' => $account->id,
'content' => 'MARKETING CAMPAIGN',
'enabled' => true,
]);
$response = $this->actingAs($this->user)->get(route('app.posts.index', ['search' => 'marketing']));
$response->assertOk();
$response->assertInertia(fn ($page) => $page
->has('posts.data', 1)
);
});