-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add endpoint for setting MW DB version #1010
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
27b858a
introduce Helper
deer-wmde cec031c
add route config
deer-wmde 0904495
formatting
deer-wmde e36c7e7
code
deer-wmde 93bf48a
clarify error message
deer-wmde e862d91
patch -> post
deer-wmde a81961b
Apply suggestion from @outdooracorn
deer-wmde e85ca41
formatting
deer-wmde 3b7d1a0
remove getDbVersion
deer-wmde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| /* The purpose of this class to have one only place where | ||
| * this map of mediawikiVersion -> dbVersion lives. | ||
| */ | ||
|
|
||
| namespace App\Helper; | ||
|
|
||
| /** | ||
| * Exception thrown when a database version is not mapped in WikiDbVersionHelper. | ||
| */ | ||
| class UnknownDbVersionException extends \Exception {} | ||
|
|
||
| class WikiDbVersionHelper { | ||
| /** @var array<string, string> Map of DB version strings to MediaWiki version strings */ | ||
| private const DB_VERSION_TO_MW_VERSION = [ | ||
| 'mw1.39-wbs1' => '139', | ||
| 'mw1.43-wbs1' => '143', | ||
| ]; | ||
|
|
||
| public static function isValidDbVersion(string $dbVersionString): bool { | ||
| return array_key_exists( | ||
| $dbVersionString, | ||
| self::DB_VERSION_TO_MW_VERSION | ||
| ); | ||
| } | ||
|
|
||
| public static function isValidMwVersion(string $mwVersionString): bool { | ||
| return array_key_exists( | ||
| $mwVersionString, | ||
| array_flip(self::DB_VERSION_TO_MW_VERSION) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @throws UnknownDbVersionException | ||
| */ | ||
| public static function getMwVersion(string $dbVersionString): string { | ||
| if (self::isValidDbVersion($dbVersionString)) { | ||
| return self::DB_VERSION_TO_MW_VERSION[$dbVersionString]; | ||
| } | ||
|
|
||
| throw new UnknownDbVersionException("Unknown database version string: '{$dbVersionString}'"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Jobs; | ||
|
|
||
| use App\Helper\UnknownDbVersionException; | ||
| use App\Helper\WikiDbVersionHelper; | ||
| use Tests\TestCase; | ||
|
|
||
| class WikiDbVersionHelperTest extends TestCase { | ||
| public function testUnknownDbVersion() { | ||
| $this->expectException(UnknownDbVersionException::class); | ||
|
|
||
| WikiDbVersionHelper::getMwVersion('invalidDbVersion'); | ||
| } | ||
|
|
||
| public function testKnownMwVersion() { | ||
| $this->assertSame( | ||
| WikiDbVersionHelper::getMwVersion('mw1.39-wbs1'), | ||
| '139' | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Routes\Backend; | ||
|
|
||
| use App\Wiki; | ||
| use App\WikiDb; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Tests\TestCase; | ||
|
|
||
| class WikiControllerTest extends TestCase { | ||
| use RefreshDatabase; | ||
|
|
||
| const VALID_WIKI_DB_VERSION_STRING_139 = 'mw1.39-wbs1'; | ||
|
|
||
| const VALID_WIKI_DB_VERSION_STRING_143 = 'mw1.43-wbs1'; | ||
|
|
||
| protected $routeSetDbVersion = '/backend/wiki/setDbVersion'; | ||
|
|
||
| protected $routeGetWikiForDomain = '/backend/wiki/getWikiForDomain'; | ||
|
|
||
| private function createWiki(string $domain, string $version) { | ||
| $wiki = Wiki::factory()->create(['domain' => $domain]); | ||
| WikiDb::create([ | ||
| 'name' => $domain, | ||
| 'user' => 'someUser', | ||
| 'password' => 'somePassword', | ||
| 'version' => $version, | ||
| 'prefix' => 'somePrefix', | ||
| 'wiki_id' => $wiki->id, | ||
| ]); | ||
| } | ||
|
|
||
| public function testSetWikiDbVersionForDomainSuccess() { | ||
| $targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143; | ||
| $wikiDomain = 'coffeebase.wikibase.cloud'; | ||
|
|
||
| $this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139); | ||
|
|
||
| $this->postJson("{$this->routeSetDbVersion}?domain={$wikiDomain}&dbVersion={$targetDbVersion}") | ||
| ->assertStatus(200) | ||
| ->assertJson([ | ||
| 'result' => 'success', | ||
| ]); | ||
| } | ||
|
|
||
| public function testSetWikiDbVersionForDomainWikiNotfound() { | ||
| $targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143; | ||
| $wikiDomain = 'notFound.wikibase.cloud'; | ||
|
|
||
| $this->postJson("{$this->routeSetDbVersion}?domain={$wikiDomain}&dbVersion={$targetDbVersion}") | ||
| ->assertStatus(404); | ||
| } | ||
|
|
||
| public function testSetWikiDbVersionForDomainUnknownDbVersion() { | ||
| $targetDbVersion = 'unknownVersion'; | ||
| $wikiDomain = 'coffeebase.wikibase.cloud'; | ||
|
|
||
| $this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139); | ||
|
|
||
| $this->postJson("{$this->routeSetDbVersion}?domain={$wikiDomain}&dbVersion={$targetDbVersion}") | ||
| ->assertStatus(400); | ||
| } | ||
|
|
||
| public function testSetWikiDbVersionForDomainMissingDbVersion() { | ||
| $wikiDomain = 'coffeebase.wikibase.cloud'; | ||
|
|
||
| $this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139); | ||
|
|
||
| $this->postJson("{$this->routeSetDbVersion}?domain={$wikiDomain}") | ||
| ->assertStatus(422); | ||
| } | ||
|
|
||
| public function testSetWikiDbVersionForDomainMissingWikiDomain() { | ||
| $targetDbVersion = self::VALID_WIKI_DB_VERSION_STRING_143; | ||
| $wikiDomain = 'coffeebase.wikibase.cloud'; | ||
|
|
||
| $this->createWiki($wikiDomain, self::VALID_WIKI_DB_VERSION_STRING_139); | ||
|
|
||
| $this->postJson("{$this->routeSetDbVersion}?dbVersion={$targetDbVersion}") | ||
| ->assertStatus(422); | ||
| } | ||
|
|
||
| public function testGetWikiForDomainMissingWikiDomain() { | ||
| $this->getJson("{$this->routeGetWikiForDomain}") | ||
| ->assertStatus(422); | ||
| } | ||
|
|
||
| public function testGetWikiForDomainWikiNotFound() { | ||
| $this->getJson("{$this->routeGetWikiForDomain}?domain=somewiki") | ||
| ->assertStatus(404); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[non-blocking] I'm now wondering if
MediaWikiHostResolvershould be a helper as well, rather than a service?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know - I think it was something I suggested back then, but I wouldn't change it again now since we tested the implementation already in all the Jobs etc.
Also as a side note I thought it was quite nice to see this Service pattern getting implemented and how its use can play out with Dependency Injection, so it could be a good example for possible future
Serviceclasses