Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/PublicWikiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function index(Request $request) {
]);

$params = array_merge(self::$defaultParams, $request->input());
$query = Wiki::query();
$query = Wiki::query()->with('wikiLatestProfile');

if (array_key_exists('is_featured', $params)) {
$query = $query->where([
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Resources/PublicWikiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public function toArray($request): array {
'sitename' => $this->sitename,
'wiki_site_stats' => $this->wikiSiteStats,
'logo_url' => $logoSetting ? $logoSetting->value : null,
'reuse_prototype' => $this->wikiLatestProfile
? $this->wikiLatestProfile->purpose === 'data_hub'
&& $this->wikiLatestProfile->temporality === 'permanent'
&& $this->wikiLatestProfile->audience === 'wide'
: false,
];
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@
});

$router->apiResource('wiki', 'PublicWikiController')->only(['index', 'show']);
$router->apiResource('reusePrototype', 'PublicWikiController')->only(['index']);
$router->apiResource('wikiConversionData', 'ConversionMetricController')->only(['index']);
});
104 changes: 104 additions & 0 deletions tests/Http/Controllers/PublicWikiControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Tests\Http\Controllers;

use App\Http\Controllers\PublicWikiController;
use App\Http\Resources\PublicWikiResource;
use App\Wiki;
use App\WikiProfile;
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class PublicWikiControllerTest extends TestCase {
use DatabaseTransactions;

public function testShowLoadsWikiLatestProfileForResource(): void {
$wiki = Wiki::factory()->create([
'domain' => 'controller-test.wikibase.cloud',
'sitename' => 'controller-test',
]);

WikiProfile::create([
'wiki_id' => $wiki->id,
'purpose' => 'data_hub',
'temporality' => 'permanent',
'audience' => 'wide',
]);

$controller = new PublicWikiController;
$resource = $controller->show($wiki->id);

$this->assertInstanceOf(PublicWikiResource::class, $resource);
$this->assertSame(true, $resource->toArray(new Request)['reuse_prototype']);
}

public function testIndexEagerLoadsWikiLatestProfileOnceForCollection(): void {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test that checks false is returned when there is no WikiProfile information in the database.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 tests added:

  • return false when a wiki doesn't match reuse prototype
  • return false when a wiki doesn't have a WikiProfile

for ($i = 1; $i <= 3; $i++) {
$wiki = Wiki::factory()->create([
'domain' => "index-eager-load-test-{$i}.wikibase.cloud",
'sitename' => "Index Eager Load Test Site {$i}",
]);

WikiProfile::create([
'wiki_id' => $wiki->id,
'purpose' => 'data_hub',
'temporality' => 'permanent',
'audience' => 'wide',
]);
}

$wikiProfileQueryCount = 0;
DB::listen(function (QueryExecuted $query) use (&$wikiProfileQueryCount): void {
if (str_contains($query->sql, 'wiki_profiles')) {
$wikiProfileQueryCount++;
}
});

$controller = new PublicWikiController;
$resourceCollection = $controller->index(new Request);

$this->assertSame(1, $wikiProfileQueryCount);
$this->assertTrue($resourceCollection->first()->relationLoaded('wikiLatestProfile'));
}

public function testIndexReturnsFalseWhenWikiHasNoLatestProfile(): void {
$wikiWithoutProfile = Wiki::factory()->create([
'domain' => 'no-profile.wikibase.cloud',
'sitename' => 'No Profile Test Site',
]);

$controller = new PublicWikiController;
$request = new Request;
$resourceCollection = $controller->index($request);

$resource = $resourceCollection->firstWhere('id', $wikiWithoutProfile->id);

$this->assertNotNull($resource);
$this->assertFalse($resource->toArray($request)['reuse_prototype']);
}

public function testIndexReturnsFalseWhenWikiLatestProfileDoesNotMatchReusePrototype(): void {
$incompleteProfileWiki = Wiki::factory()->create([
'domain' => 'incomplete-profile.wikibase.cloud',
'sitename' => 'Incomplete Profile Test Site',
]);
WikiProfile::create([
'wiki_id' => $incompleteProfileWiki->id,
'purpose' => 'other',
'temporality' => 'temporary',
'audience' => 'other',
]);

$controller = new PublicWikiController;
$request = new Request;
$resourceCollection = $controller->index($request);

$resource = $resourceCollection->firstWhere('id', $incompleteProfileWiki->id);

$this->assertNotNull($resource);
$this->assertFalse($resource->toArray($request)['reuse_prototype']);
}
}
51 changes: 51 additions & 0 deletions tests/Routes/Wiki/PublicWikiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Routes\Wiki;

use App\Wiki;
use App\WikiProfile;
use App\WikiSetting;
use App\WikiSiteStats;
use Illuminate\Foundation\Testing\DatabaseTransactions;
Expand All @@ -18,12 +19,14 @@ class PublicWikiTest extends TestCase {
protected function setUp(): void {
parent::setUp();
Wiki::query()->delete();
WikiProfile::query()->delete();
WikiSiteStats::query()->delete();
WikiSetting::query()->delete();
}

protected function tearDown(): void {
Wiki::query()->delete();
WikiProfile::query()->delete();
WikiSiteStats::query()->delete();
WikiSetting::query()->delete();
parent::tearDown();
Expand Down Expand Up @@ -279,4 +282,52 @@ public function testLogoUrl() {
)
->assertJsonPath('data.1.logo_url', null);
}

public function testReusePrototype() {
$reusableWiki = Wiki::factory()->create([
'domain' => 'reusable.wikibase.cloud', 'sitename' => 'asite',
]);
WikiSiteStats::factory()->create([
'wiki_id' => $reusableWiki->id,
]);
WikiProfile::create([
'wiki_id' => $reusableWiki->id,
'purpose' => 'data_hub',
'temporality' => 'permanent',
'audience' => 'wide',
]);

$nonReusableWiki = Wiki::factory()->create([
'domain' => 'non-reusable.wikibase.cloud', 'sitename' => 'bsite',
]);
WikiSiteStats::factory()->create([
'wiki_id' => $nonReusableWiki->id,
]);
WikiProfile::create([
'wiki_id' => $nonReusableWiki->id,
'purpose' => 'other',
'temporality' => 'other',
'audience' => 'other',
]);

$noProfileWiki = Wiki::factory()->create([
'domain' => 'no-profile.wikibase.cloud', 'sitename' => 'csite',
]);
WikiSiteStats::factory()->create([
'wiki_id' => $noProfileWiki->id,
]);

$this->json('GET', 'reusePrototype')
->assertStatus(200)
->assertJsonPath('data.0.domain', 'reusable.wikibase.cloud')
->assertJsonPath('data.0.reuse_prototype', true)
->assertJsonPath('data.1.domain', 'non-reusable.wikibase.cloud')
->assertJsonPath('data.1.reuse_prototype', false)
->assertJsonPath('data.2.domain', 'no-profile.wikibase.cloud')
->assertJsonPath('data.2.reuse_prototype', false);

$this->json('GET', $this->route . '/' . $reusableWiki->id)
->assertStatus(200)
->assertJsonPath('data.reuse_prototype', true);
}
}
Loading