Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/DomainTest.php
Show All 25 Lines | class DomainTest extends TestCase | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
foreach ($this->domains as $domain) { | foreach ($this->domains as $domain) { | ||||
$this->deleteTestDomain($domain); | $this->deleteTestDomain($domain); | ||||
} | } | ||||
$this->deleteTestUser('user@gmail.com'); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
foreach ($this->domains as $domain) { | foreach ($this->domains as $domain) { | ||||
$this->deleteTestDomain($domain); | $this->deleteTestDomain($domain); | ||||
} | } | ||||
$this->deleteTestUser('user@gmail.com'); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test domain create/creating observer | * Test domain create/creating observer | ||||
*/ | */ | ||||
public function testCreate(): void | public function testCreate(): void | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 158 Lines • ▼ Show 20 Lines | public function testDelete(): void | ||||
$job->handle(); | $job->handle(); | ||||
$this->assertTrue(Domain::withTrashed()->where('id', $domain->id)->first()->isDeleted()); | $this->assertTrue(Domain::withTrashed()->where('id', $domain->id)->first()->isDeleted()); | ||||
$domain->forceDelete(); | $domain->forceDelete(); | ||||
$this->assertCount(0, Domain::withTrashed()->where('id', $domain->id)->get()); | $this->assertCount(0, Domain::withTrashed()->where('id', $domain->id)->get()); | ||||
} | } | ||||
/** | |||||
* Test domain restoring | |||||
*/ | |||||
public function testRestore(): void | |||||
{ | |||||
Queue::fake(); | |||||
$domain = $this->getTestDomain('gmail.com', [ | |||||
'status' => Domain::STATUS_NEW | Domain::STATUS_SUSPENDED | |||||
| Domain::STATUS_LDAP_READY | Domain::STATUS_CONFIRMED, | |||||
'type' => Domain::TYPE_PUBLIC, | |||||
]); | |||||
$user = $this->getTestUser('user@gmail.com'); | |||||
$sku = \App\Sku::where('title', 'domain-hosting')->first(); | |||||
$now = \Carbon\Carbon::now(); | |||||
// Assign two entitlements to the domain, so we can assert that only the | |||||
// ones deleted last will be restored | |||||
$ent1 = \App\Entitlement::create([ | |||||
'wallet_id' => $user->wallets->first()->id, | |||||
'sku_id' => $sku->id, | |||||
'cost' => 0, | |||||
'entitleable_id' => $domain->id, | |||||
'entitleable_type' => Domain::class, | |||||
]); | |||||
$ent2 = \App\Entitlement::create([ | |||||
'wallet_id' => $user->wallets->first()->id, | |||||
'sku_id' => $sku->id, | |||||
'cost' => 0, | |||||
'entitleable_id' => $domain->id, | |||||
'entitleable_type' => Domain::class, | |||||
]); | |||||
$domain->delete(); | |||||
$this->assertTrue($domain->fresh()->trashed()); | |||||
$this->assertFalse($domain->fresh()->isDeleted()); | |||||
$this->assertTrue($ent1->fresh()->trashed()); | |||||
$this->assertTrue($ent2->fresh()->trashed()); | |||||
// Backdate some properties | |||||
\App\Entitlement::withTrashed()->where('id', $ent2->id)->update(['deleted_at' => $now->subMinutes(2)]); | |||||
\App\Entitlement::withTrashed()->where('id', $ent1->id)->update(['updated_at' => $now->subMinutes(10)]); | |||||
Queue::fake(); | |||||
$domain->restore(); | |||||
$domain->refresh(); | |||||
$this->assertFalse($domain->trashed()); | |||||
$this->assertFalse($domain->isDeleted()); | |||||
$this->assertFalse($domain->isSuspended()); | |||||
$this->assertFalse($domain->isLdapReady()); | |||||
$this->assertTrue($domain->isActive()); | |||||
$this->assertTrue($domain->isConfirmed()); | |||||
// Assert entitlements | |||||
$this->assertTrue($ent2->fresh()->trashed()); | |||||
$this->assertFalse($ent1->fresh()->trashed()); | |||||
$this->assertTrue($ent1->updated_at->greaterThan(\Carbon\Carbon::now()->subSeconds(5))); | |||||
// We expect only one CreateJob and one UpdateJob | |||||
// Because how Illuminate/Database/Eloquent/SoftDeletes::restore() method | |||||
// is implemented we cannot skip the UpdateJob in any way. | |||||
// I don't want to overwrite this method, the extra job shouldn't do any harm. | |||||
$this->assertCount(2, Queue::pushedJobs()); // @phpstan-ignore-line | |||||
Queue::assertPushed(\App\Jobs\Domain\UpdateJob::class, 1); | |||||
Queue::assertPushed(\App\Jobs\Domain\CreateJob::class, 1); | |||||
Queue::assertPushed( | |||||
\App\Jobs\Domain\CreateJob::class, | |||||
function ($job) use ($domain) { | |||||
return $domain->id === TestCase::getObjectProperty($job, 'domainId'); | |||||
} | |||||
); | |||||
} | |||||
} | } |