-
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 all 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
90 changes: 90 additions & 0 deletions
90
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,90 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace ProfessionalWiki\PersistentPageIdentifiers\EntryPoints; | ||
|
|
||
| use FormSpecialPage; | ||
| use ProfessionalWiki\PersistentPageIdentifiers\PersistentPageIdentifiersExtension; | ||
| use ProfessionalWiki\PersistentPageIdentifiers\Presentation\PersistentPageIdFormatter; | ||
| use Status; | ||
| use Title; | ||
|
|
||
| class SpecialPersistentPageIdentifierResolver extends FormSpecialPage { | ||
|
|
||
| public function __construct() { | ||
| parent::__construct( 'PersistentPageIdentifierResolver' ); | ||
| } | ||
|
|
||
| public function execute( $subPage ): void { | ||
| // Redirect to the page immediately if it is valid | ||
| $url = $this->getUrlFromPersistentId( $subPage ); | ||
| if ( $url !== null ) { | ||
| $this->getOutput()->redirect( $url ); | ||
| return; | ||
| } | ||
|
|
||
| parent::execute( $subPage ); | ||
| } | ||
|
|
||
| protected function getFormFields(): array { | ||
| return [ | ||
| 'persistentpageidentifier' => [ | ||
| 'type' => 'text', | ||
| 'label-message' => 'persistentpageidentifiers-info-label', | ||
| 'required' => true, | ||
| ] | ||
| ]; | ||
| } | ||
|
|
||
| public function onSubmit( array $data ): Status|bool { | ||
| $url = $this->getUrlFromPersistentId( $data['persistentpageidentifier'] ); | ||
| if ( $url === null ) { | ||
| return Status::newFatal( $this->getMessagePrefix() . '-not-exists' ); | ||
| } | ||
|
|
||
| $this->getOutput()->redirect( $url ); | ||
| return true; | ||
| } | ||
|
|
||
| protected function getDisplayFormat(): string { | ||
| return 'ooui'; | ||
| } | ||
|
|
||
| public function getGroupName(): string { | ||
| return 'redirects'; | ||
| } | ||
|
|
||
| private function getUrlFromPersistentId( ?string $persistentId ): ?string { | ||
| if ( $persistentId === null || $persistentId === '' ) { | ||
| return null; | ||
| } | ||
|
|
||
| $title = $this->getTitleFromPersistentId( $this->extractId( $persistentId ) ); | ||
|
|
||
| if ( $title === null || !$title->exists() ) { | ||
| return null; | ||
| } | ||
|
|
||
| return $title->getFullURL(); | ||
| } | ||
|
|
||
| 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 ); | ||
| } | ||
|
|
||
| private function extractId( string $input ): string { | ||
| return PersistentPageIdentifiersExtension::getInstance()->newPersistentPageIdFormatter()->extractId( $input ); | ||
| } | ||
|
|
||
| } | ||
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
127 changes: 127 additions & 0 deletions
127
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,127 @@ | ||
| <?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; | ||
| use WikiPage; | ||
|
|
||
| /** | ||
| * @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(); | ||
| // Call parent::execute first before attempting to execute with an invalid subpage | ||
| // This is only needed for the test | ||
| $resolver->getContext()->setTitle( $resolver->getPageTitle() ); | ||
| $resolver->execute( 'non-existent-id' ); | ||
|
|
||
| $this->assertSame( | ||
| '', | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should not redirect for a non-existent persistent ID.' | ||
| ); | ||
| } | ||
|
|
||
| public function testExecuteWithEmptySubPageReturns(): void { | ||
| $resolver = $this->newSpecialPage(); | ||
| // Call parent::execute first before attempting to execute with an empty subpage | ||
| // This is only needed for the test | ||
| $resolver->getContext()->setTitle( $resolver->getPageTitle() ); | ||
| $resolver->execute( '' ); | ||
|
|
||
| $this->assertSame( | ||
| '', | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should not redirect for an empty subpage.' | ||
| ); | ||
| } | ||
|
|
||
| public function testExecuteWithNullSubPageReturns(): void { | ||
| $resolver = $this->newSpecialPage(); | ||
| // Call parent::execute first before attempting to execute with a null subpage | ||
| // This is only needed for the test | ||
| $resolver->getContext()->setTitle( $resolver->getPageTitle() ); | ||
| $resolver->execute( null ); | ||
|
|
||
| $this->assertSame( | ||
| '', | ||
| $resolver->getOutput()->getRedirect(), | ||
| 'Should not redirect for a null subpage.' | ||
| ); | ||
| } | ||
|
|
||
| public function testOnSubmitWithValidIdRedirects(): void { | ||
| $this->page = $this->createPageWithText(); | ||
|
|
||
| $resolver = $this->newSpecialPage(); | ||
| $status = $resolver->onSubmit( [ 'persistentpageidentifier' => $this->repo->getPersistentId( $this->page->getId() ) ] ); | ||
|
|
||
| $this->assertTrue( $status, 'onSubmit should return true on success.' ); | ||
| $this->assertSame( | ||
| $this->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
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
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.