-
Notifications
You must be signed in to change notification settings - Fork 2
IBX-10978: Added tel/mailto options to urlType #106
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
Open
reithor
wants to merge
10
commits into
4.6
Choose a base branch
from
ibx-10978-add-tel-mailto-to-urltype
base: 4.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
434784a
IBX-10978: Added tel/mailto options to urlType
reithor 8322c56
code fix
reithor bdeef52
Use Symfony FixUrlProtocolListener when possible
reithor 6ce6946
Added test for FixUrlProtocolListener
reithor 096ba1d
Code Fix
reithor ebc75bd
Fix mailto handling
reithor b9c8718
Fix mailto handling
reithor 32d43b3
Improved SonarQube result
reithor 12c8017
Minor fix
reithor 1080bbe
Changes from code review
reithor 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,81 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\ContentForms\Form\EventSubscriber; | ||
|
|
||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
| use Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener as BaseFixUrlProtocolListener; | ||
| use Symfony\Component\Form\FormEvent; | ||
| use Symfony\Component\Form\FormEvents; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class FixUrlProtocolListener implements EventSubscriberInterface | ||
| { | ||
| /** @var string|null */ | ||
| private $defaultProtocol; | ||
|
|
||
| /** @var \Symfony\Component\Form\Extension\Core\EventListener\FixUrlProtocolListener */ | ||
| private $fixUrlProtocolListener; | ||
|
|
||
| /** | ||
| * @param string|null $defaultProtocol The URL scheme to add when there is none or null to not modify the data | ||
| */ | ||
| public function __construct(?string $defaultProtocol = 'http') | ||
| { | ||
| $this->defaultProtocol = $defaultProtocol; | ||
| $this->fixUrlProtocolListener = new BaseFixUrlProtocolListener($defaultProtocol); | ||
| } | ||
|
|
||
| public function onSubmit(FormEvent $event): void | ||
| { | ||
| $data = $event->getData(); | ||
| $dataLink = $data['link'] ?? null; | ||
| if (null === $this->defaultProtocol || empty($data) || empty($dataLink) || !\is_string($dataLink)) { | ||
| return; | ||
| } | ||
|
|
||
| $protocol = explode(':', $dataLink)[0]; | ||
| if ($this->hasAuthority($protocol) && $this->hasAuthority($this->defaultProtocol)) { | ||
| $event->setData($dataLink); | ||
| $this->fixUrlProtocolListener->onSubmit($event); | ||
| $data['link'] = $event->getData(); | ||
| $event->setData($data); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (!$this->hasAuthority($protocol) && preg_match('~^(?:[/.]|[\w+.-]+:|[^:/?@#]++@)~', $dataLink)) { | ||
| return; | ||
| } | ||
|
|
||
| if ($this->hasAuthority($this->defaultProtocol)) { | ||
| $schemaSeparator = '://'; | ||
| $regExp = '~^(?:[/.]|[\w+.-]+//|[^:/?@#]++@)~'; | ||
| } else { | ||
| $schemaSeparator = ':'; | ||
| $regExp = '~^[\w+.-]+:~'; // allowing emails for non-http/https/file | ||
| } | ||
|
|
||
| if (!preg_match($regExp, $dataLink)) { | ||
| $data['link'] = $this->defaultProtocol . $schemaSeparator . $dataLink; | ||
| $event->setData($data); | ||
| } | ||
| } | ||
|
|
||
| private function hasAuthority(string $protocol): bool | ||
| { | ||
| return !in_array($protocol, ['mailto', 'tel'], true); | ||
| } | ||
|
|
||
| public static function getSubscribedEvents(): array | ||
| { | ||
| return [FormEvents::SUBMIT => 'onSubmit']; | ||
| } | ||
| } |
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
108 changes: 108 additions & 0 deletions
108
tests/lib/Form/EventSubscriber/FixUrlProtocolListenerTest.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,108 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\ContentForms\Form\EventSubscriber; | ||
|
|
||
| use Ibexa\ContentForms\Form\EventSubscriber\FixUrlProtocolListener; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\Form\FormEvent; | ||
| use Symfony\Component\Form\FormInterface; | ||
|
|
||
| final class FixUrlProtocolListenerTest extends TestCase | ||
| { | ||
| private const DOMAIN = 'example.com'; | ||
| private const MAIL = 'foo@' . self::DOMAIN; | ||
| private const TEL = '+123456'; | ||
| private const URL_HTTP = 'http://' . self::DOMAIN; | ||
| private const URL_HTTPS = 'https://' . self::DOMAIN; | ||
| private const URL_MAILTO = 'mailto:' . self::MAIL; | ||
| private const URL_RELATIVE = '/foo/bar/'; | ||
| private const URL_SFTP = 'sftp://' . self::DOMAIN; | ||
| private const URL_TEL = 'tel:' . self::TEL; | ||
|
|
||
| /** | ||
| * @dataProvider provideUrlCases | ||
| * | ||
| * @param array<string, string>|null $inputData | ||
| * @param array<string, string>|null $expectedData | ||
| * @param string $defaultProtocol | ||
| */ | ||
| public function testUrlProtocolHandling(?array $inputData, ?array $expectedData, ?string $defaultProtocol = 'http'): void | ||
| { | ||
| $form = $this->createMock(FormInterface::class); | ||
| $listener = new FixUrlProtocolListener($defaultProtocol); | ||
|
|
||
| $event = new FormEvent($form, $inputData); | ||
|
|
||
| $listener->onSubmit($event); | ||
|
|
||
| self::assertSame($expectedData, $event->getData()); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{ | ||
| * 0: array<string, string>|null, | ||
| * 1: array<string, string>|null | ||
| * }> | ||
| */ | ||
| public static function provideUrlCases(): iterable | ||
| { | ||
| return [ | ||
| 'adds http when protocol missing' => [ | ||
| ['link' => self::DOMAIN], | ||
| ['link' => self::URL_HTTP], | ||
| ], | ||
| 'does not modify https url' => [ | ||
| ['link' => self::URL_HTTPS], | ||
| ['link' => self::URL_HTTPS], | ||
| ], | ||
| 'does not modify http url' => [ | ||
| ['link' => self::URL_HTTP], | ||
| ['link' => self::URL_HTTP], | ||
| ], | ||
| 'keep relative url with leading / intact' => [ | ||
| ['link' => self::URL_RELATIVE], | ||
| ['link' => self::URL_RELATIVE], | ||
| ], | ||
| 'keeps ftp intact' => [ | ||
| ['link' => self::URL_SFTP], | ||
| ['link' => self::URL_SFTP], | ||
| ], | ||
| 'keeps tel intact' => [ | ||
| ['link' => self::URL_TEL], | ||
| ['link' => self::URL_TEL], | ||
| ], | ||
| 'adds default tel' => [ | ||
| ['link' => self::TEL], | ||
| ['link' => self::URL_TEL], | ||
| 'tel', | ||
| ], | ||
| 'keeps mailto intact' => [ | ||
| ['link' => self::URL_MAILTO], | ||
| ['link' => self::URL_MAILTO], | ||
| ], | ||
| 'adds default mailto' => [ | ||
| ['link' => self::MAIL], | ||
| ['link' => self::URL_MAILTO], | ||
| 'mailto', | ||
| ], | ||
| 'does nothing when link is empty string' => [ | ||
| ['link' => ''], | ||
| ['link' => ''], | ||
| ], | ||
| 'does nothing when link key is missing' => [ | ||
| [], | ||
| [], | ||
| ], | ||
| 'does nothing when data is null' => [ | ||
| null, | ||
| null, | ||
| ], | ||
| ]; | ||
| } | ||
| } |
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.