-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDeleteWikiJobTest.php
More file actions
188 lines (149 loc) · 6.11 KB
/
Copy pathDeleteWikiJobTest.php
File metadata and controls
188 lines (149 loc) · 6.11 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace Tests\Jobs;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Jobs\DeleteWikiDbJob;
use App\User;
use App\Wiki;
use App\WikiManager;
use App\WikiDb;
use App\Jobs\ProvisionWikiDbJob;
use Illuminate\Support\Facades\DB;
use Illuminate\Contracts\Queue\Job;
use Carbon\Carbon;
use PDOException;
use PHPUnit\TextUI\RuntimeException;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Database\DatabaseManager;
class DeleteWikiJobTest extends TestCase
{
use RefreshDatabase;
use DispatchesJobs;
private $wiki;
private function getExpectedDeletedDatabaseName( $wiki ): string {
return "mwdb_deleted_1631534400_" . $wiki->id;
}
private function getResultValues( $resultRows ) {
$results = [];
foreach($resultRows as $row) {
$results[] = array_unique(array_values($row))[0];
}
return $results;
}
public function testDispatching() {
$mockJob = $this->createMock(Job::class);
$job = new DeleteWikiDbJob(-1);
$job->setJob($mockJob);
$mockJob->expects($this->once())
->method('fail');
$this->dispatchNow($job);
}
public function testDeletesWiki()
{
Carbon::setTestNow(Carbon::create(2021, 9, 13, 12));
$user = User::factory()->create(['verified' => true]);
$this->wiki = Wiki::factory()->create( [ 'deleted_at' => Carbon::now()->timestamp ] );
WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $user->id]);
$databaseName = 'the_test_database';
$expectedDeletedName = $this->getExpectedDeletedDatabaseName( $this->wiki );
$databases = [
[
"prefix" => "prefix",
"name" => "the_test_database"
],
[
"prefix" => "prefix2",
"name" => "the_test_database_not_to_be_deleted"
]
];
// Would be injected by the app
$manager = $this->app->make('db');
$job = new ProvisionWikiDbJob($databases[0]['prefix'], $databases[0]['name'], null);
$job->handle($manager);
// Would be injected by the app
$manager = $this->app->make('db');
$job = new ProvisionWikiDbJob($databases[1]['prefix'], $databases[1]['name'], null);
$job->handle($manager);
$databaseName = 'the_test_database';
// set the first database to be this wikis database
WikiDb::where([
'name' => $databaseName,
])->first()->update(['wiki_id' => $this->wiki->id]);
// make sure it stuck
$wikiDB = WikiDb::where([ 'wiki_id' => $this->wiki->id ])->first();
$this->assertNotNull( $wikiDB );
// get a new connection and look at the tables for later assertions
$conn = $this->app->make('db')->connection('mw');
$pdo = $conn->getPdo();
$pdo->exec("USE {$wikiDB->name}");
$initialTables = $pdo->query("SHOW TABLES")->fetchAll();
$conn->disconnect();
// we now have some mediawiki tables here
$this->assertCount(85, $initialTables);
$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->never())->method('fail');
// would get injected by the app
$manager = $this->app->make('db');
// this job will kill the underlying connection
$job = new DeleteWikiDbJob( $this->wiki->id );
// $job->setJob($mockJob);
$job->handle($manager);
// get a new connection and take a look at the database tables and newly created databases
$conn = $this->app->make('db')->connection('mw');
$pdo = $conn->getPdo();
$pdo->exec("USE {$wikiDB->name}");
$databases = $this->getResultValues($pdo->query("SHOW DATABASES")->fetchAll());
$this->assertNull( WikiDb::where([ 'wiki_id' => $this->wiki->id ])->first() );
// Both databases now exist, nothing has been dropped
$this->assertContains( $expectedDeletedName, $databases);
// after delete job we don't have any tables here any more
$this->assertCount(0, $this->getResultValues( $pdo->query("SHOW TABLES")->fetchAll()));
// all tables are now in the new deleted database
$pdo->exec("USE {$expectedDeletedName}");
$this->assertCount(85, $this->getResultValues( $pdo->query("SHOW TABLES")->fetchAll()));
// Content now live in the deleted database
$result = $pdo->query(sprintf('SELECT * FROM %s.interwiki', $expectedDeletedName))->fetchAll();
$this->assertCount(66, $result);
// cleanup test deleted database
$pdo->exec("DROP DATABASE {$this->getExpectedDeletedDatabaseName( $this->wiki )}");
// Tables no longer exist in the old one
$this->expectException(PDOException::class);
$this->expectExceptionMessage("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'the_test_database.prefix_interwiki' doesn't exist");
$pdo->exec(sprintf('SELECT * FROM %s.%s_interwiki', $databaseName, $wikiDB->prefix ));
}
/**
* @dataProvider failureProvider
*/
public function testFailure( $wiki_id, $deleted_at, string $expectedFailure)
{
if ($wiki_id !== -1) {
$wiki = Wiki::factory()->create( [ 'deleted_at' => $deleted_at ] );
$wiki_id = $wiki->id;
}
$mockMananger = $this->createMock(DatabaseManager::class);
$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->once())
->method('fail')
->with(new \RuntimeException(str_replace('<WIKI_ID>', $wiki_id, $expectedFailure)));
$job = new DeleteWikiDbJob($wiki_id);
$job->setJob($mockJob);
$job->handle($mockMananger);
}
public function failureProvider() {
yield [
-1,
null,
'Wiki not found for <WIKI_ID>',
];
yield [
1,
null,
'Wiki <WIKI_ID> is not marked for deletion.',
];
yield [
1,
Carbon::now()->subDays(30)->timestamp,
'WikiDb not found for <WIKI_ID>',
];
}
}