-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDeleteWikiFinalizeJobTest.php
More file actions
136 lines (108 loc) · 5.33 KB
/
Copy pathDeleteWikiFinalizeJobTest.php
File metadata and controls
136 lines (108 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Tests\Routes\Auth;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\WikiManager;
use App\Wiki;
use App\WikiSetting;
use Carbon\Carbon;
use App\Jobs\DeleteWikiFinalizeJob;
use App\WikiDb;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\WikiLogoController;
use App\Http\Curl\HttpRequest;
use Illuminate\Contracts\Queue\Job;
class DeleteWikiFinalizeJobTest extends TestCase
{
use RefreshDatabase;
public function setUp(): void {
parent::setUp();
Storage::fake('gcs-public-static');
}
public function testDeleteWiki()
{
$user = User::factory()->create(['verified' => true]);
$wiki = Wiki::factory()->create( [ 'deleted_at' => Carbon::now()->timestamp ] );
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$setting = WikiSetting::create(['wiki_id' => $wiki->id, 'name' => 'asdf', 'value' => false]);
$request = $this->createMock(HttpRequest::class);
$request->expects($this->never())->method('execute');
$job = new DeleteWikiFinalizeJob( $wiki->id );
$job->handle($request);
$this->assertNull( Wiki::withTrashed()->where('id', $wiki->id)->first() );
$this->assertNull( WikiManager::whereId($manager->id)->first() );
$this->assertNull( WikiSetting::whereId($setting->id)->first() );
}
public function testDoesNotDeleteWhenResourcesStillExist()
{
$user = User::factory()->create(['verified' => true]);
$wiki = Wiki::factory()->create( [ 'deleted_at' => Carbon::now()->timestamp ] );
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$setting = WikiSetting::create(['wiki_id' => $wiki->id, 'name' => 'asdf', 'value' => false]);
$wikiDbName = 'wikiDbName';
$wikiDB = WikiDb::create([
'name' => $wikiDbName,
'user' => 'asdasd',
'password' => 'asdasfasfasf',
'version' => 'asdasdasdas',
'prefix' => 'asdasd',
'wiki_id' => $wiki->id
]);
$mockResponse = "index\n" . "{$wikiDbName}_content_blabla\n" . "{$wikiDbName}_general_bla\n";
$request = $this->createMock(HttpRequest::class);
$request->expects($this->once())
->method('execute')
->willReturn($mockResponse);
$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->once())->method('fail')->with(
new \RuntimeException("Elasticsearch indices with basename {$wikiDbName} still exists")
);
$job = new DeleteWikiFinalizeJob( $wiki->id );
$job->setJob($mockJob);
$job->handle($request);
// TODO should this dispatch the deletion job if some resources still existed?
// should not delete because WikiDB job probably failed and couldn't delete
$this->assertNotNull( Wiki::withTrashed()->where('id', $wiki->id)->first() );
$this->assertNotNull( WikiManager::whereId($manager->id)->first() );
$this->assertNotNull( WikiSetting::whereId($setting->id)->first() );
$this->assertNotNull( WikiDb::whereId($wikiDB->id)->first() );
}
public function testDoesNotDeleteNonDeletedWikis()
{
$user = User::factory()->create(['verified' => true]);
$wiki = Wiki::factory()->create( [ 'deleted_at' => null ] );
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$setting = WikiSetting::create(['wiki_id' => $wiki->id, 'name' => 'asdf', 'value' => false]);
$request = $this->createMock(HttpRequest::class);
$request->expects($this->never())->method('execute');
$job = new DeleteWikiFinalizeJob( $wiki->id );
$job->handle($request);
// should not get deleted when deleted_at is not set to something.
$this->assertNotNull( Wiki::withTrashed()->where('id', $wiki->id)->first() );
$this->assertNotNull( WikiManager::whereId($manager->id)->first() );
$this->assertNotNull( WikiSetting::whereId($setting->id)->first() );
}
public function testDeletesFiles()
{
$user = User::factory()->create(['verified' => true]);
$wiki = Wiki::factory()->create( [ 'deleted_at' => Carbon::now()->timestamp ] );
$manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);
$setting = WikiSetting::create(['wiki_id' => $wiki->id, 'name' => WikiSetting::wgFavicon, 'value' => false]);
$siteDir = Wiki::getSiteDirectory($wiki->id);
Storage::disk('gcs-public-static')
->makeDirectory($siteDir);
Storage::disk('gcs-public-static')->assertExists($siteDir);
$request = $this->createMock(HttpRequest::class);
$request->expects($this->never())->method('execute');
$job = new DeleteWikiFinalizeJob( $wiki->id );
$job->handle($request);
// deletion happened
$this->assertNull( Wiki::withTrashed()->where('id', $wiki->id)->first() );
$this->assertNull( WikiManager::whereId($manager->id)->first() );
$this->assertNull( WikiSetting::whereId($setting->id)->first() );
// site dir gone
Storage::disk('gcs-public-static')->assertMissing($siteDir);
}
}