Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/Admin/DiscountsTest.php
Show All 19 Lines | class DiscountsTest extends TestCase | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test listing discounts (/api/v4/discounts) | * Test listing discounts (GET /api/v4/users/{user}/discounts) | ||||
*/ | */ | ||||
public function testIndex(): void | public function testuserDiscounts(): void | ||||
{ | { | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$admin = $this->getTestUser('jeroen@jeroen.jeroen'); | $admin = $this->getTestUser('jeroen@jeroen.jeroen'); | ||||
// Non-admin user | // Non-admin user | ||||
$response = $this->actingAs($user)->get("api/v4/discounts"); | $response = $this->actingAs($user)->get("api/v4/users/{$user->id}/discounts"); | ||||
$response->assertStatus(403); | $response->assertStatus(403); | ||||
// Admin user | // Admin user | ||||
$response = $this->actingAs($admin)->get("api/v4/discounts"); | $response = $this->actingAs($admin)->get("api/v4/users/{$user->id}/discounts"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$discount_test = Discount::where('code', 'TEST')->first(); | $discount_test = Discount::where('code', 'TEST')->first(); | ||||
$discount_free = Discount::where('discount', 100)->first(); | $discount_free = Discount::where('discount', 100)->first(); | ||||
$this->assertSame(3, $json['count']); | $this->assertSame(3, $json['count']); | ||||
$this->assertSame($discount_test->id, $json['list'][0]['id']); | $this->assertSame($discount_test->id, $json['list'][0]['id']); | ||||
$this->assertSame($discount_test->discount, $json['list'][0]['discount']); | $this->assertSame($discount_test->discount, $json['list'][0]['discount']); | ||||
$this->assertSame($discount_test->code, $json['list'][0]['code']); | $this->assertSame($discount_test->code, $json['list'][0]['code']); | ||||
$this->assertSame($discount_test->description, $json['list'][0]['description']); | $this->assertSame($discount_test->description, $json['list'][0]['description']); | ||||
$this->assertSame('10% - Test voucher [TEST]', $json['list'][0]['label']); | $this->assertSame('10% - Test voucher [TEST]', $json['list'][0]['label']); | ||||
$this->assertSame($discount_free->id, $json['list'][2]['id']); | $this->assertSame($discount_free->id, $json['list'][2]['id']); | ||||
$this->assertSame($discount_free->discount, $json['list'][2]['discount']); | $this->assertSame($discount_free->discount, $json['list'][2]['discount']); | ||||
$this->assertSame($discount_free->code, $json['list'][2]['code']); | $this->assertSame($discount_free->code, $json['list'][2]['code']); | ||||
$this->assertSame($discount_free->description, $json['list'][2]['description']); | $this->assertSame($discount_free->description, $json['list'][2]['description']); | ||||
$this->assertSame('100% - Free Account', $json['list'][2]['label']); | $this->assertSame('100% - Free Account', $json['list'][2]['label']); | ||||
// A user in another tenant | |||||
$user = $this->getTestUser('user@sample-tenant.dev-local'); | |||||
$response = $this->actingAs($admin)->get("api/v4/users/{$user->id}/discounts"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$discount = Discount::withObjectTenantContext($user)->where('discount', 10)->first(); | |||||
$this->assertSame(1, $json['count']); | |||||
$this->assertSame($discount->id, $json['list'][0]['id']); | |||||
$this->assertSame($discount->discount, $json['list'][0]['discount']); | |||||
$this->assertSame($discount->code, $json['list'][0]['code']); | |||||
$this->assertSame($discount->description, $json['list'][0]['description']); | |||||
$this->assertSame('10% - ' . $discount->description, $json['list'][0]['label']); | |||||
} | } | ||||
} | } |