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
81 changes: 81 additions & 0 deletions src/lib/Form/EventSubscriber/FixUrlProtocolListener.php
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'];
}
}
3 changes: 3 additions & 0 deletions src/lib/Form/Type/FieldType/UrlFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Ibexa\ContentForms\Form\Type\FieldType;

use Ibexa\ContentForms\FieldType\DataTransformer\FieldValueTransformer;
use Ibexa\ContentForms\Form\EventSubscriber\FixUrlProtocolListener;
use Ibexa\Contracts\Core\Repository\FieldTypeService;
use JMS\TranslationBundle\Annotation\Desc;
use Symfony\Component\Form\AbstractType;
Expand Down Expand Up @@ -49,8 +50,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
[
'label' => /** @Desc("URL") */ 'content.field_type.ezurl.link',
'required' => $options['required'],
'default_protocol' => null,
]
)
->addEventSubscriber(new FixUrlProtocolListener())
->add(
'text',
TextType::class,
Expand Down
108 changes: 108 additions & 0 deletions tests/lib/Form/EventSubscriber/FixUrlProtocolListenerTest.php
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,
],
];
}
}
Loading