From 676744de3a01cf9b87b8b22d7f050781936c164e Mon Sep 17 00:00:00 2001 From: Ollie Date: Sun, 20 Sep 2026 23:35:56 +0100 Subject: [PATCH 1/9] Improve UserFactory and its references * Make the `verified` attribute defualt to `true`, as that is what is used everywhere * Remove `'verified' => true` from all references of `User::factory()` * Add an `unverified()` method for easily setting `verified` to `false * Add an `admin` method for easily setting `is_admin` to `1` * Add type declarations to method parameters and return values --- database/factories/UserFactory.php | 27 +++++++++++++++---- database/seeds/WikisSeeder.php | 1 - .../CirrusSearch/ForceSearchIndexTest.php | 2 +- .../QueueSearchIndexBatchesTest.php | 2 +- .../DeleteQueryserviceNamespaceJobTest.php | 4 +-- tests/Jobs/DeleteWikiDispatcherJobTest.php | 2 +- tests/Jobs/DeleteWikiFinalizeJobTest.php | 8 +++--- tests/Jobs/DeleteWikiJobTest.php | 2 +- .../ElasticSearchIndexDeleteTest.php | 2 +- .../ElasticSearchIndexDeleteTest.php | 2 +- .../QueryserviceNamespaceJobTest.php | 2 +- tests/Jobs/KubernetesIngressCreateTest.php | 2 +- tests/Jobs/KubernetesIngressDeleteJobTest.php | 2 +- tests/Jobs/PlatformStatsSummaryJobTest.php | 2 +- .../SendEmptyWikiNotificationsJobTest.php | 10 +++---- tests/Jobs/SetWikiLogoTest.php | 2 +- tests/Jobs/SiteStatsUpdateJobTest.php | 2 +- tests/Middleware/LimitWikiAccessTest.php | 4 +-- tests/Routes/Wiki/CreateTest.php | 18 ++++++------- tests/Routes/Wiki/DeleteWikiTest.php | 4 +-- tests/Routes/Wiki/DetailsTest.php | 8 +++--- tests/Routes/Wiki/EntityImportTest.php | 16 +++++------ tests/Routes/Wiki/LogoUpdateTest.php | 4 +-- tests/Routes/Wiki/MineTest.php | 4 +-- tests/Routes/Wiki/ProfileControllerTest.php | 10 +++---- tests/Routes/Wiki/SettingUpdateTest.php | 8 +++--- 26 files changed, 83 insertions(+), 67 deletions(-) diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index b535d4b46..9bcc2e90f 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -15,14 +15,31 @@ class UserFactory extends Factory { /** * Define the model's default state. - * - * @return array */ - public function definition() { + public function definition(): array { return [ - 'password' => $this->faker->password(), 'email' => $this->faker->unique()->safeEmail(), - 'verified' => $this->faker->boolean(), + 'password' => $this->faker->password(), + 'verified' => true, + 'is_admin' => 0, ]; } + + /** + * Indicate that the user's email address should be unverified. + */ + public function unverified(): static { + return $this->state(fn (array $attributes) => [ + 'verified' => false, + ]); + } + + /** + * Indicate that the user is an admin. + */ + public function admin(int $value = 1): static { + return $this->state(fn (array $attributes) => [ + 'is_admin' => $value, + ]); + } } diff --git a/database/seeds/WikisSeeder.php b/database/seeds/WikisSeeder.php index af9fd972b..f9d0f6105 100644 --- a/database/seeds/WikisSeeder.php +++ b/database/seeds/WikisSeeder.php @@ -76,7 +76,6 @@ public function run() { $user = User::create([ 'email' => $email, 'password' => Hash::make($email), - 'verified' => true, ]); for ($id = 0; $id < 50; $id++) { diff --git a/tests/Jobs/CirrusSearch/ForceSearchIndexTest.php b/tests/Jobs/CirrusSearch/ForceSearchIndexTest.php index c20b36cda..4d9bb74e1 100644 --- a/tests/Jobs/CirrusSearch/ForceSearchIndexTest.php +++ b/tests/Jobs/CirrusSearch/ForceSearchIndexTest.php @@ -33,7 +33,7 @@ class ForceSearchIndexTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->user = User::factory()->create(['verified' => true]); + $this->user = User::factory()->create(); $this->wiki = Wiki::factory()->create(); WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]); WikiSetting::factory()->create( diff --git a/tests/Jobs/CirrusSearch/QueueSearchIndexBatchesTest.php b/tests/Jobs/CirrusSearch/QueueSearchIndexBatchesTest.php index 1e7845c7d..e4de37e51 100644 --- a/tests/Jobs/CirrusSearch/QueueSearchIndexBatchesTest.php +++ b/tests/Jobs/CirrusSearch/QueueSearchIndexBatchesTest.php @@ -32,7 +32,7 @@ class QueueSearchIndexBatchesTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->user = User::factory()->create(['verified' => true]); + $this->user = User::factory()->create(); $this->wiki = Wiki::factory()->create(); WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]); WikiSetting::factory()->create( diff --git a/tests/Jobs/DeleteQueryserviceNamespaceJobTest.php b/tests/Jobs/DeleteQueryserviceNamespaceJobTest.php index 8eacfff98..f3f4966ac 100644 --- a/tests/Jobs/DeleteQueryserviceNamespaceJobTest.php +++ b/tests/Jobs/DeleteQueryserviceNamespaceJobTest.php @@ -18,7 +18,7 @@ class DeleteQueryserviceNamespaceJobTest extends TestCase { use DatabaseTransactions; public function testDeleteNamespace() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->subDays(30)->timestamp]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); @@ -83,7 +83,7 @@ public function testNoWiki() { public function testNoNamespace() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->subDays(30)->timestamp]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); diff --git a/tests/Jobs/DeleteWikiDispatcherJobTest.php b/tests/Jobs/DeleteWikiDispatcherJobTest.php index 8a299dc1a..911869ffc 100644 --- a/tests/Jobs/DeleteWikiDispatcherJobTest.php +++ b/tests/Jobs/DeleteWikiDispatcherJobTest.php @@ -39,7 +39,7 @@ protected function setUp(): void { } private function getWiki($daysSinceDelete = 30): Wiki { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create( [ 'deleted_at' => $daysSinceDelete > 0 ? Carbon::now()->subDays($daysSinceDelete)->timestamp : null, diff --git a/tests/Jobs/DeleteWikiFinalizeJobTest.php b/tests/Jobs/DeleteWikiFinalizeJobTest.php index 8d9f25f49..c34d15af2 100644 --- a/tests/Jobs/DeleteWikiFinalizeJobTest.php +++ b/tests/Jobs/DeleteWikiFinalizeJobTest.php @@ -24,7 +24,7 @@ protected function setUp(): void { } public function testDeleteWiki() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $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]); @@ -41,7 +41,7 @@ public function testDeleteWiki() { } public function testDoesNotDeleteWhenResourcesStillExist() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $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]); @@ -81,7 +81,7 @@ public function testDoesNotDeleteWhenResourcesStillExist() { } public function testDoesNotDeleteNonDeletedWikis() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $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]); @@ -100,7 +100,7 @@ public function testDoesNotDeleteNonDeletedWikis() { public function testDeletesFiles() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $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]); diff --git a/tests/Jobs/DeleteWikiJobTest.php b/tests/Jobs/DeleteWikiJobTest.php index d9943bd1d..e675669b8 100644 --- a/tests/Jobs/DeleteWikiJobTest.php +++ b/tests/Jobs/DeleteWikiJobTest.php @@ -61,7 +61,7 @@ public function testDeletesWiki() { // transaction that refresh database has started ue to the DROP statement Carbon::setTestNow(Carbon::create(2021, 9, 13, 12)); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $this->wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->timestamp]); WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $user->id]); diff --git a/tests/Jobs/ElasticSearch/ElasticSearchIndexDeleteTest.php b/tests/Jobs/ElasticSearch/ElasticSearchIndexDeleteTest.php index 2aa2d536b..3ccfe7c11 100644 --- a/tests/Jobs/ElasticSearch/ElasticSearchIndexDeleteTest.php +++ b/tests/Jobs/ElasticSearch/ElasticSearchIndexDeleteTest.php @@ -26,7 +26,7 @@ class ElasticSearchIndexDeleteTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->user = User::factory()->create(['verified' => true]); + $this->user = User::factory()->create(); $this->wiki = Wiki::factory()->create(); WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]); WikiSetting::factory()->create( diff --git a/tests/Jobs/Integration/ElasticSearchIndexDeleteTest.php b/tests/Jobs/Integration/ElasticSearchIndexDeleteTest.php index 625e2ec35..86ef79314 100644 --- a/tests/Jobs/Integration/ElasticSearchIndexDeleteTest.php +++ b/tests/Jobs/Integration/ElasticSearchIndexDeleteTest.php @@ -77,7 +77,7 @@ public function testDeletion() { $response = $this->makeRequest("http://$ELASTICSEARCH_HOST/test_db_name_content_first?pretty", 'PUT'); $this->assertTrue($response['acknowledged']); - $this->user = User::factory()->create(['verified' => true]); + $this->user = User::factory()->create(); $this->wiki = Wiki::factory()->create(); WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]); WikiSetting::factory()->create( diff --git a/tests/Jobs/Integration/QueryserviceNamespaceJobTest.php b/tests/Jobs/Integration/QueryserviceNamespaceJobTest.php index cdda8b8cd..38a7e9093 100644 --- a/tests/Jobs/Integration/QueryserviceNamespaceJobTest.php +++ b/tests/Jobs/Integration/QueryserviceNamespaceJobTest.php @@ -37,7 +37,7 @@ public function testIntegrationCreate() { $mockJob = $this->createMock(Job::class); $mockJob->expects($this->never())->method('fail'); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['deleted_at' => Carbon::now()->subDays(30)->timestamp]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); diff --git a/tests/Jobs/KubernetesIngressCreateTest.php b/tests/Jobs/KubernetesIngressCreateTest.php index 5e484c51d..22468b753 100644 --- a/tests/Jobs/KubernetesIngressCreateTest.php +++ b/tests/Jobs/KubernetesIngressCreateTest.php @@ -20,7 +20,7 @@ class KubernetesIngressCreateTest extends TestCase { public function testCreateIngressJobDoesNotFail() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['deleted_at' => null]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); diff --git a/tests/Jobs/KubernetesIngressDeleteJobTest.php b/tests/Jobs/KubernetesIngressDeleteJobTest.php index a3d8e86f3..4a46b4874 100644 --- a/tests/Jobs/KubernetesIngressDeleteJobTest.php +++ b/tests/Jobs/KubernetesIngressDeleteJobTest.php @@ -17,7 +17,7 @@ class KubernetesIngressDeleteJobTest extends TestCase { public function testDoesNotDeleteNonDeletedWikis() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['deleted_at' => null]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); diff --git a/tests/Jobs/PlatformStatsSummaryJobTest.php b/tests/Jobs/PlatformStatsSummaryJobTest.php index c0b310c1c..dc60d059a 100644 --- a/tests/Jobs/PlatformStatsSummaryJobTest.php +++ b/tests/Jobs/PlatformStatsSummaryJobTest.php @@ -64,7 +64,7 @@ private function seedWikis() { $manager = $this->app->make('db'); for ($n = 0; $n < $this->numWikis; $n++) { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['deleted_at' => null]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); diff --git a/tests/Jobs/SendEmptyWikiNotificationsJobTest.php b/tests/Jobs/SendEmptyWikiNotificationsJobTest.php index ca588356f..a90b13545 100644 --- a/tests/Jobs/SendEmptyWikiNotificationsJobTest.php +++ b/tests/Jobs/SendEmptyWikiNotificationsJobTest.php @@ -36,7 +36,7 @@ public function testEmptyWikiNotificationsSendNotification() { )->toDateTimeString(); Notification::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['created_at' => $thresholdDaysAgo]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $wiki->wikiLifecycleEvents()->updateOrCreate(['first_edited' => null]); @@ -58,7 +58,7 @@ public function testEmptyWikiNotificationsNotificationNotSent() { ->toDateTimeString(); Notification::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['created_at' => $thresholdDaysAgo]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $wiki->wikiLifecycleEvents()->updateOrCreate(['first_edited' => null]); @@ -74,7 +74,7 @@ public function testEmptyWikiNotificationsFreshWiki() { $now = Carbon::now()->toDateTimeString(); Notification::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['created_at' => $now]); $manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); @@ -100,7 +100,7 @@ public function testEmptyWikiNotificationsActiveWiki() { $now = Carbon::now()->toDateTimeString(); Notification::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['created_at' => $thresholdDaysAgo]); $manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); @@ -122,7 +122,7 @@ public function testEmptyWikiNotificationsEmptyNotificationReceived() { )->toDateTimeString(); Notification::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['created_at' => $thresholdDaysAgo]); $manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); diff --git a/tests/Jobs/SetWikiLogoTest.php b/tests/Jobs/SetWikiLogoTest.php index 520052a1f..cd4d19b75 100644 --- a/tests/Jobs/SetWikiLogoTest.php +++ b/tests/Jobs/SetWikiLogoTest.php @@ -55,7 +55,7 @@ public function testSetLogoFails($wikiKey, $wikiValue, $logoPath) { */ public function testSetLogoSucceeds($wikiKey, $wikiValue, $logoPath) { // create user and wiki for this test - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory('nodb')->create([$wikiKey => $wikiValue]); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); diff --git a/tests/Jobs/SiteStatsUpdateJobTest.php b/tests/Jobs/SiteStatsUpdateJobTest.php index 2851b4d19..a3b7de8ad 100644 --- a/tests/Jobs/SiteStatsUpdateJobTest.php +++ b/tests/Jobs/SiteStatsUpdateJobTest.php @@ -27,7 +27,7 @@ class SiteStatsUpdateJobTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->user = User::factory()->create(['verified' => true]); + $this->user = User::factory()->create(); $this->wiki = Wiki::factory()->create(); $this->manager = WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]); $this->mwBackendHost = 'mediawiki.localhost'; diff --git a/tests/Middleware/LimitWikiAccessTest.php b/tests/Middleware/LimitWikiAccessTest.php index 4277b39ef..5a0f16cf8 100644 --- a/tests/Middleware/LimitWikiAccessTest.php +++ b/tests/Middleware/LimitWikiAccessTest.php @@ -30,7 +30,7 @@ protected function tearDown(): void { private function createWikiAndUser(): array { $wiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); return [$wiki, $user]; @@ -60,7 +60,7 @@ public function testSuccess(): void { public function testFailOnWrongWikiManager(): void { $userWiki = Wiki::factory()->create(); $otherWiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]); foreach ($this->generateRequestUriAndBody($otherWiki) as $name => [$uri, $body]) { diff --git a/tests/Routes/Wiki/CreateTest.php b/tests/Routes/Wiki/CreateTest.php index 4711b1547..6173f1491 100644 --- a/tests/Routes/Wiki/CreateTest.php +++ b/tests/Routes/Wiki/CreateTest.php @@ -52,7 +52,7 @@ public function testWikiCreateDispatchesSomeJobs($elasticSearchConfig) { Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); Queue::assertNothingPushed(); $response = $this->actingAs($user, 'api') @@ -167,7 +167,7 @@ public function testCreateWikiLimitsNumWikisPerUser() { Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); Queue::assertNothingPushed(); // This shouldn't stop first create since it's deleted @@ -238,7 +238,7 @@ private function createSQLandQSDBs(): void { public function testCreateWikiHandlesRangeOfPostValues($data, $expectedStatus): void { $this->createSQLandQSDBs(); Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $response = $this->actingAs($user, 'api') ->json( 'POST', @@ -299,7 +299,7 @@ public static function createWikiHandlesRangeOfPostValuesProvider(): array { public function testCreateWithProfileCreatesProfiles(): void { $this->createSQLandQSDBs(); Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $response = $this->actingAs($user, 'api') ->json( 'POST', @@ -314,7 +314,7 @@ public function testCreateWithProfileCreatesProfiles(): void { public function testCreateWithKERCreatesProfiles(): void { $this->createSQLandQSDBs(); Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $response = $this->actingAs($user, 'api') ->json( 'POST', @@ -332,7 +332,7 @@ public function testCreateWithKERCreatesProfiles(): void { public function testCreateWithKERRejectsIfSelectedOptionIsInvalid(): void { $this->createSQLandQSDBs(); Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $response = $this->actingAs($user, 'api') ->json( 'POST', @@ -351,7 +351,7 @@ public function testCreateWithKERRejectsIfSelectedOptionIsInvalid(): void { public function testCreateWithKERCreatesIf3000FreeTextResponse(): void { $this->createSQLandQSDBs(); Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $response = $this->actingAs($user, 'api') ->json( 'POST', @@ -369,7 +369,7 @@ public function testCreateWithKERCreatesIf3000FreeTextResponse(): void { public function testCreateWithKERErrorsIf3001FreeTextResponse(): void { $this->createSQLandQSDBs(); Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $response = $this->actingAs($user, 'api') ->json( 'POST', @@ -389,7 +389,7 @@ public function testCreateWithKERErrorsIf3001FreeTextResponse(): void { public function testCreateWikiErrorsIfKerIsInvalid(): void { $this->createSQLandQSDBs(); Queue::fake(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $response = $this->actingAs($user, 'api')->json( 'POST', diff --git a/tests/Routes/Wiki/DeleteWikiTest.php b/tests/Routes/Wiki/DeleteWikiTest.php index abb972ae5..3381256e7 100644 --- a/tests/Routes/Wiki/DeleteWikiTest.php +++ b/tests/Routes/Wiki/DeleteWikiTest.php @@ -14,7 +14,7 @@ class DeleteWikiTest extends TestCase { use RefreshDatabase; public function testDelete() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory('nodb')->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); @@ -36,7 +36,7 @@ public function testDelete() { public function testFailOnWrongWikiManager(): void { $userWiki = Wiki::factory()->create(); $otherWiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') ->post('wiki/delete', ['wiki' => $otherWiki->id]) diff --git a/tests/Routes/Wiki/DetailsTest.php b/tests/Routes/Wiki/DetailsTest.php index d4f98b977..d8162bdb0 100644 --- a/tests/Routes/Wiki/DetailsTest.php +++ b/tests/Routes/Wiki/DetailsTest.php @@ -39,7 +39,7 @@ public function testNoCredentials() { } public function testSkipsNonPublicSettings() { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(['domain' => 'other.wikibase.cloud']); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); @@ -58,7 +58,7 @@ public function testSkipsNonPublicSettings() { public function testReturnsCorrectWikiNotFirstWiki(): void { $firstWiki = Wiki::factory()->create(); $userWiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]); $this->assertEquals($firstWiki->id, Wiki::first()->id); $this->actingAs($user, 'api') @@ -70,7 +70,7 @@ public function testReturnsCorrectWikiNotFirstWiki(): void { public function testFailOnWrongWikiManager(): void { $userWiki = Wiki::factory()->create(); $otherWiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') ->postJson($this->route, ['wiki' => $otherWiki->id]) @@ -79,7 +79,7 @@ public function testFailOnWrongWikiManager(): void { public function testWikiProfile() { $wiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $versionA = WikiProfile::create([ diff --git a/tests/Routes/Wiki/EntityImportTest.php b/tests/Routes/Wiki/EntityImportTest.php index 13d4605f0..0503bcaac 100644 --- a/tests/Routes/Wiki/EntityImportTest.php +++ b/tests/Routes/Wiki/EntityImportTest.php @@ -35,7 +35,7 @@ protected function tearDown(): void { public function testNoCredentials() { $wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $this->actingAs($user, 'api') ->json('GET', $this->route . '?wiki=' . $wiki->id) ->assertStatus(403); @@ -43,7 +43,7 @@ public function testNoCredentials() { public function testEmpty() { $wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') ->json('GET', $this->route . '?wiki=' . $wiki->id) @@ -54,7 +54,7 @@ public function testEmpty() { public function testResults() { $wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']); $otherWiki = Wiki::factory()->create(['domain' => 'other.wikibase.cloud']); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); WikiEntityImport::factory()->create([ @@ -81,7 +81,7 @@ public function testResults() { public function testCreateWhilePending() { Bus::fake(); $wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); WikiEntityImport::factory()->create([ @@ -101,7 +101,7 @@ public function testCreateWhilePending() { public function testCreateWhenSucceeded() { Bus::fake(); $wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); WikiEntityImport::factory()->create([ @@ -121,7 +121,7 @@ public function testCreateWhenSucceeded() { public function testCreateWhenEmpty() { Bus::fake(); $wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') @@ -135,7 +135,7 @@ public function testCreateWhenEmpty() { public function testCreateValidation() { Bus::fake(); $wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') @@ -149,7 +149,7 @@ public function testCreateValidation() { public function testCreateWhenFailed() { Bus::fake(); $wiki = Wiki::factory()->create(['domain' => 'test.wikibase.cloud']); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); WikiEntityImport::factory()->create([ diff --git a/tests/Routes/Wiki/LogoUpdateTest.php b/tests/Routes/Wiki/LogoUpdateTest.php index d23c2ef4e..73687ca46 100644 --- a/tests/Routes/Wiki/LogoUpdateTest.php +++ b/tests/Routes/Wiki/LogoUpdateTest.php @@ -19,7 +19,7 @@ public function testUpdate() { $storage = Storage::fake('static-assets'); $file = UploadedFile::fake()->createWithContent('logo_200x200.png', file_get_contents(__DIR__ . '/../../data/logo_200x200.png')); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory('nodb')->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); @@ -56,7 +56,7 @@ public function testUpdate() { public function testFailOnWrongWikiManager(): void { $userWiki = Wiki::factory()->create(); $otherWiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]); $file = UploadedFile::fake() ->createWithContent('logo_200x200.png', file_get_contents(__DIR__ . '/../../data/logo_200x200.png')); diff --git a/tests/Routes/Wiki/MineTest.php b/tests/Routes/Wiki/MineTest.php index 21496919d..278c87a4e 100644 --- a/tests/Routes/Wiki/MineTest.php +++ b/tests/Routes/Wiki/MineTest.php @@ -19,7 +19,7 @@ class MineTest extends TestCase { public function testMineDefault() { Config::set('wbstack.wiki_max_per_user', false); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $this->actingAs($user, 'api') ->json('POST', $this->route, []) ->assertStatus(200) @@ -29,7 +29,7 @@ public function testMineDefault() { public function testMineWithWikis() { Config::set('wbstack.wiki_max_per_user', 1); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); diff --git a/tests/Routes/Wiki/ProfileControllerTest.php b/tests/Routes/Wiki/ProfileControllerTest.php index f13b71642..4e17070e7 100644 --- a/tests/Routes/Wiki/ProfileControllerTest.php +++ b/tests/Routes/Wiki/ProfileControllerTest.php @@ -27,7 +27,7 @@ protected function tearDown(): void { public function testFailOnMissingWiki(): void { $wiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $wiki->delete(); @@ -50,7 +50,7 @@ public function testFailOnMissingWiki(): void { public function testFailOnWrongWikiManager(): void { $userWiki = Wiki::factory()->create(); $otherWiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') @@ -71,7 +71,7 @@ public function testFailOnWrongWikiManager(): void { public function testFailOnEmptyProfile(): void { $userWiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') @@ -88,7 +88,7 @@ public function testFailOnEmptyProfile(): void { public function testFailOnInvalidProfile(): void { $wiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') @@ -117,7 +117,7 @@ public function testFailOnInvalidProfile(): void { public function testKeepAllVersions(): void { $wiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $versionA = $this->actingAs($user, 'api') diff --git a/tests/Routes/Wiki/SettingUpdateTest.php b/tests/Routes/Wiki/SettingUpdateTest.php index 5f9f1e4b3..066345e80 100644 --- a/tests/Routes/Wiki/SettingUpdateTest.php +++ b/tests/Routes/Wiki/SettingUpdateTest.php @@ -24,7 +24,7 @@ class SettingUpdateTest extends TestCase { public function testSetInvalidSetting() { $settingName = 'iDoNotExistAsASetting'; $wiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') ->json('POST', str_replace('foo', $settingName, $this->route), [ @@ -40,7 +40,7 @@ public function testFailOnWrongWikiManager(): void { $settingName = 'wwExtEnableConfirmAccount'; $userWiki = Wiki::factory()->create(); $otherWiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $userWiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') ->postJson(str_replace('foo', $settingName, $this->route), [ @@ -84,7 +84,7 @@ public static function provideValidSettings() { * @dataProvider provideValidSettings */ public function testValidSetting($settingName, $settingValue, $expectedStored) { - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); $wiki = Wiki::factory('nodb')->create(); $manager = WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); @@ -150,7 +150,7 @@ public static function provideValidSettingsBadValues() { */ public function testValidSettingBadValues($settingName, $settingValue) { $wiki = Wiki::factory()->create(); - $user = User::factory()->create(['verified' => true]); + $user = User::factory()->create(); WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]); $this->actingAs($user, 'api') From 75a4013a1afde9c75af392395fca4d3bedbf804a Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 21 Sep 2026 01:22:46 +0100 Subject: [PATCH 2/9] Use `DatabaseTransactions` trait in `ElasticSearchAliasInitTest` Use the `DatabaseTransactions` trait to keep the tests from poluting the database rather than manually removing *all* `Wiki`s and `WikiDb`s from the database in the `tearDown()` method. `Wiki::query()->delete();` doesn't even clean the database properly as `Wiki`s are soft-deleted. The `->forceDelete()` method should have been used. --- tests/Jobs/CirrusSearch/ElasticSearchAliasInitTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Jobs/CirrusSearch/ElasticSearchAliasInitTest.php b/tests/Jobs/CirrusSearch/ElasticSearchAliasInitTest.php index fe6c8b52d..387226808 100644 --- a/tests/Jobs/CirrusSearch/ElasticSearchAliasInitTest.php +++ b/tests/Jobs/CirrusSearch/ElasticSearchAliasInitTest.php @@ -7,9 +7,12 @@ use App\Wiki; use App\WikiDb; use Illuminate\Contracts\Queue\Job; +use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\TestCase; class ElasticSearchAliasInitTest extends TestCase { + use DatabaseTransactions; + private $wikiId; private $esHost; @@ -29,8 +32,6 @@ protected function setUp(): void { protected function tearDown(): void { putenv('ELASTICSEARCH_SHARED_INDEX_PREFIX'); - Wiki::query()->delete(); - WikiDb::query()->delete(); parent::tearDown(); } From ec2304f51ae7e5130e3c04940cdd9e33f5f9e9fa Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 21 Sep 2026 01:43:39 +0100 Subject: [PATCH 3/9] fix: delete all `Wiki`s in `PlatformStatsSummaryJobTest::tearDown()` Quick fix to prevent the test from polluting the database with a soft-deleted `Wiki`. This test, and the `PlatformStatsSummaryJob` it is testing, should be improved so that wiping the whole database after each test isn't required. --- tests/Jobs/PlatformStatsSummaryJobTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Jobs/PlatformStatsSummaryJobTest.php b/tests/Jobs/PlatformStatsSummaryJobTest.php index dc60d059a..407a63919 100644 --- a/tests/Jobs/PlatformStatsSummaryJobTest.php +++ b/tests/Jobs/PlatformStatsSummaryJobTest.php @@ -52,8 +52,10 @@ protected function setUp(): void { ); } + // TODO: Improve PlatformStatsSummaryJob and this test so that it isn't required to wipe whole database tables protected function tearDown(): void { - Wiki::query()->delete(); + // Wikis are soft-deleted, leaving the database dirty, unless `->forceDelete()` is used + Wiki::query()->forceDelete(); User::query()->delete(); WikiManager::query()->delete(); WikiDb::query()->delete(); From d94fbefe960cf48fd48c78d3ed60e2d84ad48faf Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 21 Sep 2026 02:10:24 +0100 Subject: [PATCH 4/9] Prevent `LogoUpdateTest` from polluting the database Add `DatabaseTransactions` trait to `LogoUpdateTest` to prevent it from polluting the database --- tests/Routes/Wiki/LogoUpdateTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Routes/Wiki/LogoUpdateTest.php b/tests/Routes/Wiki/LogoUpdateTest.php index 73687ca46..605a59dfb 100644 --- a/tests/Routes/Wiki/LogoUpdateTest.php +++ b/tests/Routes/Wiki/LogoUpdateTest.php @@ -7,6 +7,7 @@ use App\WikiManager; use App\WikiSetting; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Intervention\Image\Facades\Image; @@ -14,6 +15,7 @@ class LogoUpdateTest extends TestCase { use HasFactory; + use DatabaseTransactions; public function testUpdate() { $storage = Storage::fake('static-assets'); From b6bf621f67e803fa9619acf553adcc980fa8ec08 Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 21 Sep 2026 02:12:20 +0100 Subject: [PATCH 5/9] Remove unused `HasFactory` trait from `LogoUpdateTest` --- tests/Routes/Wiki/LogoUpdateTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Routes/Wiki/LogoUpdateTest.php b/tests/Routes/Wiki/LogoUpdateTest.php index 605a59dfb..a7a4a85d6 100644 --- a/tests/Routes/Wiki/LogoUpdateTest.php +++ b/tests/Routes/Wiki/LogoUpdateTest.php @@ -6,7 +6,6 @@ use App\Wiki; use App\WikiManager; use App\WikiSetting; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; @@ -14,7 +13,6 @@ use Tests\TestCase; class LogoUpdateTest extends TestCase { - use HasFactory; use DatabaseTransactions; public function testUpdate() { From 80170282d919df7c300eff502602bc969cfb29c2 Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 21 Sep 2026 02:18:36 +0100 Subject: [PATCH 6/9] Prevent `ProfileControllerTest` from polluting the database * Add the `DatabaseTransactions` trait to prevent it from polluting the database * Remove the `setUp()` method as this test shouldn't be responsible for cleaning up after other tests * Remove the `tearDown()` method as the database isn't polluted when the `DatabaseTransactions` trait is used --- tests/Routes/Wiki/ProfileControllerTest.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/tests/Routes/Wiki/ProfileControllerTest.php b/tests/Routes/Wiki/ProfileControllerTest.php index 4e17070e7..c2c503bb3 100644 --- a/tests/Routes/Wiki/ProfileControllerTest.php +++ b/tests/Routes/Wiki/ProfileControllerTest.php @@ -6,24 +6,13 @@ use App\Wiki; use App\WikiManager; use App\WikiProfile; +use Illuminate\Foundation\Testing\DatabaseTransactions; use Tests\TestCase; class ProfileControllerTest extends TestCase { - protected $route = 'wiki/profile'; - - protected function setUp(): void { - parent::setUp(); - Wiki::query()->delete(); - WikiManager::query()->delete(); - WikiProfile::query()->delete(); - } + use DatabaseTransactions; - protected function tearDown(): void { - Wiki::query()->delete(); - WikiManager::query()->delete(); - WikiProfile::query()->delete(); - parent::tearDown(); - } + protected $route = 'wiki/profile'; public function testFailOnMissingWiki(): void { $wiki = Wiki::factory()->create(); From 071a35e890f9da61aba0ced5e5b97d45af7b0d7e Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 21 Sep 2026 03:00:39 +0100 Subject: [PATCH 7/9] Fix fragile tests and improve `DeletedWikiMetricsControllerTest` * Fix tests creating `Wiki`s with invalid `user_id` of `0` * Remove the `setUp()` method as `DeletedWikiMetricsControllerTest` shouldn't be responsible for cleaning up after other tests * Remove the `tearDown()` method as `DeletedWikiMetricsControllerTest` doesn't pollute the database * Simplify signature and implementation of `createAndDeleteTestWiki()` to remove functionality not used by the test * Make use of `User::factory()` rather than reimplementing logic in the TestCase * Replace `private function createUserWithPrivileges()` with the `UserFactory::admin()` method * Refactor out the requests to `auth/login` by using `->actingAs()` on the requests under test * Rely on the `Wiki::factory()` to set the `sitename` attribute * Add type declarations to method parameters and return values * Inline variables that are only used once * Rename test methods for clarity --- .../Wiki/DeletedWikiMetricsControllerTest.php | 94 ++++++------------- 1 file changed, 29 insertions(+), 65 deletions(-) diff --git a/tests/Routes/Wiki/DeletedWikiMetricsControllerTest.php b/tests/Routes/Wiki/DeletedWikiMetricsControllerTest.php index 285518584..773310b57 100644 --- a/tests/Routes/Wiki/DeletedWikiMetricsControllerTest.php +++ b/tests/Routes/Wiki/DeletedWikiMetricsControllerTest.php @@ -5,7 +5,6 @@ use App\User; use App\Wiki; use App\WikiManager; -use App\WikiSetting; use App\WikiSiteStats; use Carbon\CarbonImmutable; use Illuminate\Foundation\Testing\DatabaseTransactions; @@ -13,94 +12,59 @@ use Tests\TestCase; class DeletedWikiMetricsControllerTest extends TestCase { - protected string $route = 'deletedWikiMetrics'; - use DatabaseTransactions; use OptionsRequestAllowed; - protected function setUp(): void { - parent::setUp(); - Wiki::query()->delete(); - WikiSiteStats::query()->delete(); - WikiSetting::query()->delete(); - } + protected string $route = 'deletedWikiMetrics'; - protected function tearDown(): void { - Wiki::query()->delete(); - WikiSiteStats::query()->delete(); - WikiSetting::query()->delete(); - parent::tearDown(); - } + public function testUnauthorizedStatusIfUserNotLoggedIn(): void { + $this->createAndDeleteTestWiki('one.wikibase.cloud', User::factory()->create()); + $this->createAndDeleteTestWiki('two.wikibase.cloud', User::factory()->create()); - public function testUnauthorisedStatusIfUserNotLoggedIn() { - $this->createAndDeleteTestWiki('one.wikibase.cloud', 0, '', 1, 2); - $this->createAndDeleteTestWiki('two.wikibase.cloud', 10, 'Some Reason', 0, 3); $response = $this->get($this->route); $response->assertStatus(401); } - public function testRedirectIfUserIsLoggedInAsNotAdmin() { - $user = $this->createUserWithPrivileges(0); - $this->actingAs($user, 'api')->get('auth/login'); - $this->createAndDeleteTestWiki('one.wikibase.cloud', 0, '', 1, 2); - $this->createAndDeleteTestWiki('two.wikibase.cloud', 10, 'Some Reason', 0, 3); - $response = $this->get($this->route); + public function testRedirectsIfRegularUserLoggedIn(): void { + $this->createAndDeleteTestWiki('one.wikibase.cloud', User::factory()->create()); + $this->createAndDeleteTestWiki('two.wikibase.cloud', User::factory()->create()); + + $response = $this->actingAs(User::factory()->create(), 'api')->get($this->route); $response->assertStatus(302); } - public function testDownloadCsvIfUserIsLoggedInAsAdmin() { - $user = $this->createUserWithPrivileges(1); - $this->actingAs($user, 'api')->get('auth/login'); - $this->createAndDeleteTestWiki('one.wikibase.cloud', 0, '', 1, 2); - $this->createAndDeleteTestWiki('two.wikibase.cloud', 10, 'Some Reason', 0, 3); - $response = $this->get($this->route); + public function testDownloadsCsvIfAdminUser(): void { + $this->createAndDeleteTestWiki('one.wikibase.cloud', User::factory()->create()); + $this->createAndDeleteTestWiki('two.wikibase.cloud', User::factory()->create()); + + $response = $this->actingAs(User::factory()->admin()->create(), 'api')->get($this->route); $response->assertStatus(200) ->assertDownload(CarbonImmutable::now()->toIso8601String() . '-deleted_wiki_metric.csv'); } - public function testOutputHasCorrectContent() { - $user1 = User::factory()->create(['verified' => true]); - $user2 = User::factory()->create(['verified' => true]); - $deletedWikis = [ - $this->createAndDeleteTestWiki('one.wikibase.cloud', $user1->id, '', 1, 2), - $this->createAndDeleteTestWiki('two.wikibase.cloud', $user2->id, 'Some Reason', 0, 3), - $this->createAndDeleteTestWiki('sameuser.wikibase.cloud', $user1->id, 'Some Reason', 0, 3), - ]; - $user = $this->createUserWithPrivileges(1); - $this->actingAs($user, 'api')->get('auth/login'); - - $response = $this->get($this->route); - - $rawCsv = $response->getContent(); - $output = array_map('str_getcsv', explode("\n", $rawCsv)); - $this->assertSame('one.wikibase.cloud', $output[1][0]); - $this->assertSame('two.wikibase.cloud', $output[2][0]); - $this->assertSame('Some Reason', $output[2][1]); - } + public function testOutputHasCorrectContent(): void { + $user = User::factory()->create(); + $this->createAndDeleteTestWiki('one.wikibase.cloud', $user); + $this->createAndDeleteTestWiki('two.wikibase.cloud', User::factory()->create(), 'Some Reason'); + $this->createAndDeleteTestWiki('sameuser.wikibase.cloud', $user); - private function createUserWithPrivileges($userPrivilege) { - $password = 'apassword'; + $response = $this->actingAs(User::factory()->admin()->create(), 'api')->get($this->route); - return User::factory()->create([ - 'verified' => true, - 'email' => 'atestmail@gmail.com', - 'password' => password_hash($password, PASSWORD_DEFAULT), - 'is_admin' => $userPrivilege, - ]); + $csvArray = array_map('str_getcsv', explode("\n", $response->getContent())); + $this->assertSame('one.wikibase.cloud', $csvArray[1][0]); + $this->assertSame('two.wikibase.cloud', $csvArray[2][0]); + $this->assertSame('Some Reason', $csvArray[2][1]); } - private function createAndDeleteTestWiki($domain, $user_id, $wikiDeletionReason, $createdWeeksAgo = 1, $wiki_users = 1) { + private function createAndDeleteTestWiki(string $domain, User $user, string $wikiDeletionReason = ''): Wiki { $current_date = CarbonImmutable::now(); $wiki = Wiki::factory()->create([ - 'domain' => $domain, 'sitename' => 'bsite', 'created_at' => $current_date->subWeeks($createdWeeksAgo), - ]); - WikiManager::factory()->create([ - 'wiki_id' => $wiki->id, 'user_id' => $user_id, - ]); - WikiSiteStats::factory()->create([ - 'wiki_id' => $wiki->id, 'pages' => 77, 'users' => $wiki_users, + 'domain' => $domain, + 'created_at' => $current_date->subWeeks(1), ]); + WikiManager::factory()->for($user)->for($wiki)->create(); + WikiSiteStats::factory()->create(['wiki_id' => $wiki->id, 'pages' => 77, 'users' => 5]); $wiki->save(); $wiki->update(['wiki_deletion_reason' => $wikiDeletionReason]); From f12396b4a574c57b944bc596196b134cd1c1a72d Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 21 Sep 2026 03:54:40 +0100 Subject: [PATCH 8/9] fix test: Delete `WikiDb`s before `Wiki`s in PlatformStatsSummaryJobTest --- tests/Jobs/PlatformStatsSummaryJobTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Jobs/PlatformStatsSummaryJobTest.php b/tests/Jobs/PlatformStatsSummaryJobTest.php index 407a63919..96ddbbaa2 100644 --- a/tests/Jobs/PlatformStatsSummaryJobTest.php +++ b/tests/Jobs/PlatformStatsSummaryJobTest.php @@ -54,11 +54,11 @@ protected function setUp(): void { // TODO: Improve PlatformStatsSummaryJob and this test so that it isn't required to wipe whole database tables protected function tearDown(): void { + WikiDb::query()->delete(); // Wikis are soft-deleted, leaving the database dirty, unless `->forceDelete()` is used Wiki::query()->forceDelete(); User::query()->delete(); WikiManager::query()->delete(); - WikiDb::query()->delete(); parent::tearDown(); } From 0ede004dcea2cbbedf50b0113dbf3b140a639c2f Mon Sep 17 00:00:00 2001 From: Ollie Date: Mon, 21 Sep 2026 08:40:31 +0100 Subject: [PATCH 9/9] fix test: Delete `WikiDb` before deleting `Wiki` in DeleteWikiTest --- tests/Metrics/WikiMetricsTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Metrics/WikiMetricsTest.php b/tests/Metrics/WikiMetricsTest.php index 82796349d..1a553b024 100644 --- a/tests/Metrics/WikiMetricsTest.php +++ b/tests/Metrics/WikiMetricsTest.php @@ -479,7 +479,9 @@ public function testSavesEntityCountsCorrectly($expectedItemCount, $expectedProp (new WikiMetrics())->saveMetrics($wiki); - // clean up after the test + // clean up test data after metrics have been saved + // TODO: Why does the `RefreshDatabase` trait not handle this? + $wikiDb = $wikiDb->delete(); $wiki->forceDelete(); $this->assertDatabaseHas('wiki_daily_metrics', [