diff --git a/app/Jobs/PlatformStatsSummaryJob.php b/app/Jobs/PlatformStatsSummaryJob.php index f201e0c2..f8c66dc4 100644 --- a/app/Jobs/PlatformStatsSummaryJob.php +++ b/app/Jobs/PlatformStatsSummaryJob.php @@ -80,14 +80,14 @@ public function prepareStats(array $allStats, $wikis): array { $currentTime = CarbonImmutable::now(); foreach ($wikis as $wiki) { - $this->apiUrl = $this->mwHostResolver->getBackendUrlForDomain($wiki->domain) . '/w/api.php'; // used in PageFetcher::fetchPagesInNamespace - if (!is_null($wiki->deleted_at)) { $deletedWikis[] = $wiki; continue; } + $this->apiUrl = $this->mwHostResolver->getBackendUrlForDomain($wiki->domain) . '/w/api.php'; // used in PageFetcher::fetchPagesInNamespace + // add items and properties counts of the wiki to the corresponded arrays try { $nextItemCount = count($this->fetchPagesInNamespace($wiki->domain, MediawikiNamespace::item)); diff --git a/tests/Jobs/PlatformStatsSummaryJobTest.php b/tests/Jobs/PlatformStatsSummaryJobTest.php index f7284eb5..be63dd9d 100644 --- a/tests/Jobs/PlatformStatsSummaryJobTest.php +++ b/tests/Jobs/PlatformStatsSummaryJobTest.php @@ -234,6 +234,74 @@ public function testGroupings() { ); } + public function testSkipDeletedWikisBeforeResolvingBackendUrl() { + $deletedWiki = Wiki::factory()->create(['deleted_at' => CarbonImmutable::now()->subDay(), 'domain' => 'deleted.cloud']); + WikiDb::create([ + 'name' => 'deleted_db', + 'user' => 'asdasd', + 'password' => 'asdasfasfasf', + 'version' => 'version', + 'prefix' => 'asdasd', + 'wiki_id' => $deletedWiki->id, + ]); + + $activeWiki = Wiki::factory()->create(['deleted_at' => null, 'domain' => 'active.cloud']); + WikiDb::create([ + 'name' => 'active_db', + 'user' => 'asdasd', + 'password' => 'asdasfasfasf', + 'version' => 'version', + 'prefix' => 'asdasd', + 'wiki_id' => $activeWiki->id, + ]); + + Http::fake([ + $this->mwBackendHost . '/w/api.php?action=query&list=allpages&apnamespace=122&apcontinue=&aplimit=max&format=json' => Http::response([ + 'query' => ['allpages' => []], + ], 200), + $this->mwBackendHost . '/w/api.php?action=query&list=allpages&apnamespace=120&apcontinue=&aplimit=max&format=json' => Http::response([ + 'query' => ['allpages' => []], + ], 200), + ]); + + $this->mockMwHostResolver + ->expects($this->once()) + ->method('getBackendUrlForDomain') + ->with('active.cloud') + ->willReturn($this->mwBackendHost); + + $job = new PlatformStatsSummaryJob(); + (function ($resolver): void { + $this->mwHostResolver = $resolver; + })->call($job, $this->mockMwHostResolver); + + $groups = $job->prepareStats([ + [ + 'wiki' => 'active.cloud', + 'edits' => 1, + 'pages' => 1, + 'users' => 1, + 'active_users' => 1, + 'lastEdit' => MWTimestampHelper::getMWTimestampFromCarbon(CarbonImmutable::now()), + 'first100UsingOauth' => '0', + 'platform_summary_version' => 'v1', + ], + [ + 'wiki' => 'deleted.cloud', + 'edits' => 1, + 'pages' => 1, + 'users' => 1, + 'active_users' => 1, + 'lastEdit' => MWTimestampHelper::getMWTimestampFromCarbon(CarbonImmutable::now()), + 'first100UsingOauth' => '0', + 'platform_summary_version' => 'v1', + ], + ], [$deletedWiki, $activeWiki]); + + $this->assertSame(1, $groups['deleted']); + $this->assertSame(1, $groups['edited_last_90_days']); + } + public function testCreationStats() { $this->markTestSkipped('Pollutes the deleted wiki list'); $mockJob = $this->createMock(Job::class);