Implement package limit notification system
This commit is contained in:
134
tests/Feature/Api/EventGuestUploadLimitTest.php
Normal file
134
tests/Feature/Api/EventGuestUploadLimitTest.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api;
|
||||
|
||||
use App\Jobs\Packages\SendEventPackagePhotoLimitNotification;
|
||||
use App\Jobs\Packages\SendEventPackagePhotoThresholdWarning;
|
||||
use App\Models\Emotion;
|
||||
use App\Models\Event;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\MediaStorageTarget;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Services\EventJoinTokenService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EventGuestUploadLimitTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Config::set('filesystems.default', 'local');
|
||||
Storage::fake('local');
|
||||
|
||||
MediaStorageTarget::query()->create([
|
||||
'key' => 'local',
|
||||
'name' => 'Local',
|
||||
'driver' => 'local',
|
||||
'config' => [],
|
||||
'is_hot' => true,
|
||||
'is_default' => true,
|
||||
'is_active' => true,
|
||||
'priority' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_guest_upload_blocked_when_photo_limit_reached(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
$event = Event::factory()->for($tenant)->create([
|
||||
'status' => 'published',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 1,
|
||||
'max_guests' => null,
|
||||
]);
|
||||
|
||||
EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 1,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(7),
|
||||
]);
|
||||
|
||||
$emotion = Emotion::factory()->create();
|
||||
$emotion->eventTypes()->attach($event->event_type_id);
|
||||
|
||||
/** @var EventJoinTokenService $tokenService */
|
||||
$tokenService = $this->app->make(EventJoinTokenService::class);
|
||||
$joinToken = $tokenService->createToken($event, ['label' => 'Test']);
|
||||
$token = $joinToken->plain_token;
|
||||
|
||||
$response = $this->post("/api/v1/events/{$token}/upload", [
|
||||
'photo' => UploadedFile::fake()->image('limit.jpg', 800, 600),
|
||||
], [
|
||||
'X-Device-Id' => 'device-123',
|
||||
]);
|
||||
|
||||
$response->assertStatus(402);
|
||||
$response->assertJsonPath('error.code', 'photo_limit_exceeded');
|
||||
|
||||
Bus::assertNothingDispatched();
|
||||
}
|
||||
|
||||
public function test_guest_upload_increments_usage_and_succeeds(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
$event = Event::factory()->for($tenant)->create([
|
||||
'status' => 'published',
|
||||
]);
|
||||
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 2,
|
||||
'max_guests' => null,
|
||||
]);
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now(),
|
||||
'used_photos' => 1,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->addDays(7),
|
||||
]);
|
||||
|
||||
$emotion = Emotion::factory()->create();
|
||||
$emotion->eventTypes()->attach($event->event_type_id);
|
||||
|
||||
/** @var EventJoinTokenService $tokenService */
|
||||
$tokenService = $this->app->make(EventJoinTokenService::class);
|
||||
$token = $tokenService->createToken($event, ['label' => 'Test'])->plain_token;
|
||||
|
||||
$response = $this->post("/api/v1/events/{$token}/upload", [
|
||||
'photo' => UploadedFile::fake()->image('success.jpg', 1024, 768),
|
||||
], [
|
||||
'X-Device-Id' => 'device-456',
|
||||
]);
|
||||
|
||||
$response->assertCreated();
|
||||
$this->assertEquals(
|
||||
2,
|
||||
$eventPackage->refresh()->used_photos
|
||||
);
|
||||
|
||||
$thresholdJobs = Bus::dispatched(SendEventPackagePhotoThresholdWarning::class);
|
||||
$this->assertGreaterThanOrEqual(2, $thresholdJobs->count());
|
||||
Bus::assertDispatched(SendEventPackagePhotoLimitNotification::class);
|
||||
}
|
||||
}
|
||||
216
tests/Feature/Console/CheckEventPackagesCommandTest.php
Normal file
216
tests/Feature/Console/CheckEventPackagesCommandTest.php
Normal file
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Console;
|
||||
|
||||
use App\Console\Commands\CheckEventPackages;
|
||||
use App\Events\Packages\EventPackageGalleryExpired;
|
||||
use App\Events\Packages\EventPackageGalleryExpiring;
|
||||
use App\Events\Packages\TenantCreditsLow;
|
||||
use App\Events\Packages\TenantPackageExpired;
|
||||
use App\Events\Packages\TenantPackageExpiring;
|
||||
use App\Models\Event;
|
||||
use App\Models\EventPackage;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\TenantPackage;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Event as EventFacade;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CheckEventPackagesCommandTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_dispatches_gallery_warning_and_updates_timestamp(): void
|
||||
{
|
||||
EventFacade::fake();
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 100,
|
||||
]);
|
||||
$event = Event::factory()->for($tenant)->create();
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now()->subMonth(),
|
||||
'used_photos' => 10,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->copy()->addDays(7),
|
||||
]);
|
||||
|
||||
Artisan::call(CheckEventPackages::class);
|
||||
|
||||
EventFacade::assertDispatched(EventPackageGalleryExpiring::class, function ($event) use ($eventPackage) {
|
||||
return $event->eventPackage->is($eventPackage) && $event->daysRemaining === 7;
|
||||
});
|
||||
|
||||
$this->assertNotNull($eventPackage->fresh()->gallery_warning_sent_at);
|
||||
}
|
||||
|
||||
public function test_dispatches_gallery_expired_and_updates_timestamp(): void
|
||||
{
|
||||
EventFacade::fake();
|
||||
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->endcustomer()->create([
|
||||
'max_photos' => 100,
|
||||
]);
|
||||
$event = Event::factory()->for($tenant)->create();
|
||||
|
||||
$eventPackage = EventPackage::create([
|
||||
'event_id' => $event->id,
|
||||
'package_id' => $package->id,
|
||||
'purchased_price' => $package->price,
|
||||
'purchased_at' => now()->subMonth(),
|
||||
'used_photos' => 10,
|
||||
'used_guests' => 0,
|
||||
'gallery_expires_at' => now()->copy()->subDay(),
|
||||
]);
|
||||
|
||||
Artisan::call(CheckEventPackages::class);
|
||||
|
||||
EventFacade::assertDispatched(EventPackageGalleryExpired::class, function ($event) use ($eventPackage) {
|
||||
return $event->eventPackage->is($eventPackage);
|
||||
});
|
||||
|
||||
$this->assertNotNull($eventPackage->fresh()->gallery_expired_notified_at);
|
||||
}
|
||||
|
||||
public function test_dispatches_tenant_package_expiry_warning_and_updates_timestamp(): void
|
||||
{
|
||||
EventFacade::fake();
|
||||
|
||||
Config::set('package-limits.package_expiry_days', [6, 1]);
|
||||
|
||||
$now = now()->startOfMinute();
|
||||
Carbon::setTestNow($now);
|
||||
|
||||
try {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->reseller()->create([
|
||||
'max_events_per_year' => 5,
|
||||
]);
|
||||
|
||||
$tenantPackage = TenantPackage::factory()->for($tenant)->for($package)->create([
|
||||
'expires_at' => $now->copy()->addDays(6),
|
||||
'expiry_warning_sent_at' => null,
|
||||
]);
|
||||
|
||||
Artisan::call(CheckEventPackages::class);
|
||||
|
||||
EventFacade::assertDispatched(TenantPackageExpiring::class, function ($event) use ($tenantPackage) {
|
||||
return $event->tenantPackage->is($tenantPackage) && $event->daysRemaining === 6;
|
||||
});
|
||||
|
||||
$this->assertNotNull($tenantPackage->fresh()->expiry_warning_sent_at);
|
||||
} finally {
|
||||
Carbon::setTestNow();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_dispatches_tenant_package_expired_and_updates_timestamp(): void
|
||||
{
|
||||
EventFacade::fake();
|
||||
|
||||
$now = now()->startOfMinute();
|
||||
Carbon::setTestNow($now);
|
||||
|
||||
try {
|
||||
$tenant = Tenant::factory()->create();
|
||||
$package = Package::factory()->reseller()->create([
|
||||
'max_events_per_year' => 5,
|
||||
]);
|
||||
|
||||
$tenantPackage = TenantPackage::factory()->for($tenant)->for($package)->create([
|
||||
'expires_at' => $now->copy()->subDay(),
|
||||
'expired_notified_at' => null,
|
||||
]);
|
||||
|
||||
Artisan::call(CheckEventPackages::class);
|
||||
|
||||
EventFacade::assertDispatched(TenantPackageExpired::class, function ($event) use ($tenantPackage) {
|
||||
return $event->tenantPackage->is($tenantPackage);
|
||||
});
|
||||
|
||||
$this->assertNotNull($tenantPackage->fresh()->expired_notified_at);
|
||||
} finally {
|
||||
Carbon::setTestNow();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_dispatches_credit_warning_and_sets_threshold(): void
|
||||
{
|
||||
EventFacade::fake();
|
||||
|
||||
Config::set('package-limits.credit_thresholds', [5, 1]);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'event_credits_balance' => 5,
|
||||
'credit_warning_sent_at' => null,
|
||||
'credit_warning_threshold' => null,
|
||||
]);
|
||||
|
||||
Artisan::call(CheckEventPackages::class);
|
||||
|
||||
EventFacade::assertDispatched(TenantCreditsLow::class, function ($event) use ($tenant) {
|
||||
return $event->tenant->is($tenant) && $event->threshold === 5 && $event->balance === 5;
|
||||
});
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertNotNull($tenant->credit_warning_sent_at);
|
||||
$this->assertSame(5, $tenant->credit_warning_threshold);
|
||||
}
|
||||
|
||||
public function test_resets_credit_warning_when_balance_recovers(): void
|
||||
{
|
||||
EventFacade::fake();
|
||||
|
||||
Config::set('package-limits.credit_thresholds', [5, 1]);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'event_credits_balance' => 10,
|
||||
'credit_warning_sent_at' => now()->subDay(),
|
||||
'credit_warning_threshold' => 1,
|
||||
]);
|
||||
|
||||
Artisan::call(CheckEventPackages::class);
|
||||
|
||||
EventFacade::assertNotDispatched(TenantCreditsLow::class);
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertNull($tenant->credit_warning_sent_at);
|
||||
$this->assertNull($tenant->credit_warning_threshold);
|
||||
}
|
||||
|
||||
public function test_dispatches_lower_credit_threshold_after_higher_warning(): void
|
||||
{
|
||||
EventFacade::fake();
|
||||
|
||||
Config::set('package-limits.credit_thresholds', [5, 1]);
|
||||
|
||||
$tenant = Tenant::factory()->create([
|
||||
'event_credits_balance' => 1,
|
||||
'credit_warning_sent_at' => now()->subDay(),
|
||||
'credit_warning_threshold' => 5,
|
||||
]);
|
||||
|
||||
Artisan::call(CheckEventPackages::class);
|
||||
|
||||
EventFacade::assertDispatched(TenantCreditsLow::class, function ($event) use ($tenant) {
|
||||
return $event->tenant->is($tenant) && $event->threshold === 1;
|
||||
});
|
||||
|
||||
$tenant->refresh();
|
||||
|
||||
$this->assertSame(1, $tenant->credit_warning_threshold);
|
||||
$this->assertNotNull($tenant->credit_warning_sent_at);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user