-
Notifications
You must be signed in to change notification settings - Fork 0
Add Special:PersistentPageIdentifierResolver page #64
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
650570e
Add Resolver page
alistair3149 26f4f49
Code review
alistair3149 d0d79a6
Code review
alistair3149 61481e2
Add integration test for SpecialPersistentPageIdentifierResolver
alistair3149 dfc6776
Redirect immediately if the subpage is valid
alistair3149 892d3e5
Match both ID and custom format
alistair3149 74d0210
Clean up pages after test
alistair3149 fb3647f
Fix failing test
alistair3149 97b39bf
Ensure persistent IDs are generated for existing pages in GenerateMis…
alistair3149 b504e07
Merge branch 'master' into feat-resolver
alistair3149 b805080
Remove unnecessary tearDown method from SpecialPersistentPageIdentifi…
alistair3149 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,7 @@ | ||
| <?php | ||
| $specialPageAliases = []; | ||
|
|
||
| /** English (English) */ | ||
| $specialPageAliases['en'] = [ | ||
| 'PersistentPageIdentifierResolver' => [ 'PersistentPageIdentifierResolver' ], | ||
| ]; |
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
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
77 changes: 77 additions & 0 deletions
77
src/EntryPoints/SpecialPersistentPageIdentifierResolver.php
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,77 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace ProfessionalWiki\PersistentPageIdentifiers\EntryPoints; | ||
|
|
||
| use FormSpecialPage; | ||
| use ProfessionalWiki\PersistentPageIdentifiers\PersistentPageIdentifiersExtension; | ||
| use Status; | ||
| use Title; | ||
|
|
||
| class SpecialPersistentPageIdentifierResolver extends FormSpecialPage { | ||
|
|
||
| public function __construct() { | ||
| parent::__construct( 'PersistentPageIdentifierResolver' ); | ||
| } | ||
|
|
||
| public function execute( $subPage ): void { | ||
| if ( $subPage === null || $subPage === '' ) { | ||
| return; | ||
| } | ||
|
|
||
| $title = $this->getTitleFromPersistentId( $subPage ); | ||
|
|
||
| if ( $title === null ) { | ||
| return; | ||
| } | ||
|
|
||
| $this->getOutput()->redirect( $title->getFullURL() ); | ||
| } | ||
|
|
||
| protected function getFormFields(): array { | ||
| return [ | ||
| 'persistentpageidentifier' => [ | ||
| 'type' => 'text', | ||
| 'label-message' => 'persistentpageidentifiers-info-label', | ||
| 'required' => true, | ||
| ] | ||
| ]; | ||
| } | ||
|
|
||
| public function onSubmit( array $data ): Status|bool { | ||
| $title = $this->getTitleFromPersistentId( $data['persistentpageidentifier'] ); | ||
|
|
||
| if ( $title === null ) { | ||
| // Message: persistentpageidentifierresolver-not-exists | ||
| return Status::newFatal( $this->getMessagePrefix() . '-not-exists' ); | ||
| } | ||
|
|
||
| $this->getOutput()->redirect( $title->getFullURL() ); | ||
|
|
||
| return true; | ||
| } | ||
alistair3149 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| protected function getDisplayFormat(): string { | ||
| return 'ooui'; | ||
| } | ||
|
|
||
| public function getGroupName(): string { | ||
| return 'redirects'; | ||
| } | ||
|
|
||
| private function getTitleFromPersistentId( string $persistentId ): ?Title { | ||
| $pageId = $this->getPageIdFromPersistentId( $persistentId ); | ||
|
|
||
| if ( $pageId !== null ) { | ||
| return Title::newFromID( $pageId ); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
alistair3149 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private function getPageIdFromPersistentId( string $persistentId ): ?int { | ||
| return PersistentPageIdentifiersExtension::getInstance()->getPersistentPageIdentifiersRepo()->getPageIdFromPersistentId( $persistentId ); | ||
| } | ||
|
|
||
| } | ||
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
117 changes: 117 additions & 0 deletions
117
tests/Integration/SpecialPersistentPageIdentifierResolverIntegrationTest.php
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,117 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace ProfessionalWiki\PersistentPageIdentifiers\Tests\Integration; | ||
|
|
||
| use ProfessionalWiki\PersistentPageIdentifiers\Adapters\DatabasePersistentPageIdentifiersRepo; | ||
| use ProfessionalWiki\PersistentPageIdentifiers\Application\PersistentPageIdentifiersRepo; | ||
| use ProfessionalWiki\PersistentPageIdentifiers\EntryPoints\SpecialPersistentPageIdentifierResolver; | ||
| use ProfessionalWiki\PersistentPageIdentifiers\Tests\PersistentPageIdentifiersIntegrationTest; | ||
| use Status; | ||
|
|
||
| /** | ||
| * @covers \ProfessionalWiki\PersistentPageIdentifiers\EntryPoints\SpecialPersistentPageIdentifierResolver | ||
| * @group Database | ||
| */ | ||
| class SpecialPersistentPageIdentifierResolverIntegrationTest extends PersistentPageIdentifiersIntegrationTest { | ||
|
|
||
| private PersistentPageIdentifiersRepo $repo; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
| $this->tablesUsed[] = 'persistent_page_ids'; | ||
| $this->repo = new DatabasePersistentPageIdentifiersRepo( $this->db ); | ||
| } | ||
|
|
||
| protected function newSpecialPage(): SpecialPersistentPageIdentifierResolver { | ||
| return new SpecialPersistentPageIdentifierResolver(); | ||
| } | ||
|
|
||
| public function testExecuteWithValidIdRedirects(): void { | ||
| $page = $this->createPageWithText(); | ||
alistair3149 marked this conversation as resolved.
Show resolved
Hide resolved
alistair3149 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| $resolver = $this->newSpecialPage(); | ||
| $resolver->execute( $this->repo->getPersistentId( $page->getId() ) ); | ||
|
|
||
| $this->assertSame( | ||
| $page->getTitle()->getFullURL(), | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should redirect to the page associated with the persistent ID.' | ||
| ); | ||
| } | ||
|
|
||
| public function testExecuteWithNonExistentIdReturns(): void { | ||
| $resolver = $this->newSpecialPage(); | ||
| $resolver->execute( 'non-existent-id' ); | ||
|
|
||
| $this->assertSame( | ||
| '', | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should not redirect for a non-existent persistent ID.' | ||
| ); | ||
| } | ||
alistair3149 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public function testExecuteWithEmptySubPageReturns(): void { | ||
| $resolver = $this->newSpecialPage(); | ||
| $resolver->execute( '' ); | ||
|
|
||
| $this->assertSame( | ||
| '', | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should not redirect for an empty subpage.' | ||
| ); | ||
| } | ||
alistair3149 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public function testExecuteWithNullSubPageReturns(): void { | ||
| $resolver = $this->newSpecialPage(); | ||
| $resolver->execute( null ); | ||
|
|
||
| $this->assertSame( | ||
| '', | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should not redirect for a null subpage.' | ||
| ); | ||
| } | ||
alistair3149 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public function testOnSubmitWithValidIdRedirects(): void { | ||
| $page = $this->createPageWithText(); | ||
|
|
||
| $resolver = $this->newSpecialPage(); | ||
| $status = $resolver->onSubmit( [ 'persistentpageidentifier' => $this->repo->getPersistentId( $page->getId() ) ] ); | ||
|
|
||
| $this->assertTrue( $status, 'onSubmit should return true on success.' ); | ||
| $this->assertSame( | ||
| $page->getTitle()->getFullURL(), | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should redirect to the page associated with the persistent ID on form submission.' | ||
| ); | ||
| } | ||
|
|
||
| public function testOnSubmitWithNonExistentIdReturnsFatalStatus(): void { | ||
| $resolver = $this->newSpecialPage(); | ||
| $status = $resolver->onSubmit( [ 'persistentpageidentifier' => 'non-existent-id' ] ); | ||
|
|
||
| $this->assertInstanceOf( | ||
| Status::class, | ||
| $status, | ||
| 'onSubmit should return a Status object for a non-existent ID.' | ||
| ); | ||
| $this->assertFalse( $status->isGood(), 'Status should be fatal.' ); | ||
|
|
||
| $errors = $status->getErrors(); | ||
| $this->assertNotEmpty( $errors, 'Status should contain errors.' ); | ||
| $this->assertEquals( | ||
| 'persistentpageidentifierresolver-not-exists', | ||
| $errors[0]['message'], | ||
| 'Status message should indicate the ID does not exist.' | ||
| ); | ||
|
|
||
| $this->assertSame( | ||
| '', | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should not redirect when ID does not exist on form submission.' | ||
| ); | ||
| } | ||
|
|
||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.