Skip to content
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

Add a validator on URL entity #6906

Open
wants to merge 1 commit into
base: 2.6
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/Wallabag/ApiBundle/Controller/EntryRestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Event\EntryDeletedEvent;
Expand Down Expand Up @@ -688,8 +689,15 @@
*
* @return JsonResponse
*/
public function postEntriesAction(Request $request, EntryRepository $entryRepository, ContentProxy $contentProxy, LoggerInterface $logger, TagsAssigner $tagsAssigner, EventDispatcherInterface $eventDispatcher)
{
public function postEntriesAction(
Request $request,
EntryRepository $entryRepository,
ContentProxy $contentProxy,
LoggerInterface $logger,
TagsAssigner $tagsAssigner,
EventDispatcherInterface $eventDispatcher,
ValidatorInterface $validator
) {
$this->validateAuthentication();

$url = $request->request->get('url');
Expand Down Expand Up @@ -760,6 +768,13 @@
$contentProxy->setDefaultEntryTitle($entry);
}

$errors = $validator->validate($entry);
if (\count($errors) > 0) {
$errorsString = (string) $errors;

Check failure on line 773 in src/Wallabag/ApiBundle/Controller/EntryRestController.php

View workflow job for this annotation

GitHub Actions / CS Fixer, PHPStan & TwigCS

Cannot cast Symfony\Component\Validator\ConstraintViolationListInterface to string.

return $this->sendResponse($errorsString);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if I have to return the error like that.

}

$this->entityManager->persist($entry);
$this->entityManager->flush();

Expand Down
2 changes: 2 additions & 0 deletions src/Wallabag/CoreBundle/Controller/EntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public function addEntryFormAction(Request $request, TranslatorInterface $transl
// entry saved, dispatch event about it!
$this->eventDispatcher->dispatch(new EntrySavedEvent($entry), EntrySavedEvent::NAME);

return $this->redirect($this->generateUrl('homepage'));
} elseif ($form->isSubmitted() && !$form->isValid()) {
return $this->redirect($this->generateUrl('homepage'));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to have an error message here.
To be in this case, we have to change the type field via the web console (to have a text field, no more an URL one).

}

Expand Down
3 changes: 3 additions & 0 deletions src/Wallabag/CoreBundle/Entity/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class Entry
* @var string
*
* @Assert\NotBlank()
* @Assert\Url(
* message = "The url '{{ value }}' is not a valid url",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to translate this message.

* )
* @ORM\Column(name="url", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
Expand Down
19 changes: 19 additions & 0 deletions tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,25 @@ public function testDeleteEntryExpectBadRequest()
$this->assertSame(400, $this->client->getResponse()->getStatusCode());
}

public function testBadFormatURL()
{
$this->client->request('POST', '/api/entries.json', [
'url' => 'wallabagIsAwesome',
'tags' => 'google',
'title' => 'New title for my article',
'content' => 'my content',
'language' => 'de',
'published_at' => '2016-09-08T11:55:58+0200',
'authors' => 'bob,helen',
'public' => 1,
]);

$this->assertSame(200, $this->client->getResponse()->getStatusCode());

$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertStringContainsString('The url \'"wallabagIsAwesome"\' is not a valid url', $content);
}

public function testPostEntry()
{
$this->client->request('POST', '/api/entries.json', [
Expand Down
37 changes: 34 additions & 3 deletions tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class EntryControllerTest extends WallabagCoreTestCase
public const AN_URL_CONTAINING_AN_ARTICLE_WITH_IMAGE = 'https://www.lemonde.fr/judo/article/2017/11/11/judo-la-decima-de-teddy-riner_5213605_1556020.html';
public $downloadImagesEnabled = false;
public $url = 'https://www.lemonde.fr/pixels/article/2019/06/18/ce-qu-il-faut-savoir-sur-le-libra-la-cryptomonnaie-de-facebook_5477887_4408996.html';
public $wrongUrl = 'wallabagIsAwesome';
private $entryDataTestAttribute = '[data-test="entry"]';

/**
Expand Down Expand Up @@ -137,9 +138,7 @@ public function testPostNewEmpty()

$crawler = $client->submit($form);

$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertCount(1, $alert = $crawler->filter('form ul li')->extract(['_text']));
$this->assertSame('This value should not be blank.', $alert[0]);
$this->assertSame(302, $client->getResponse()->getStatusCode());
}

/**
Expand Down Expand Up @@ -423,6 +422,38 @@ public function testPostNewThatWillBeTagged()
$em->flush();
}

/**
* @group NetworkCalls
*/
public function testBadFormatURL()
{
$this->logInAs('admin');
$client = $this->getTestClient();

$client->getContainer()->get(Config::class)->set('store_article_headers', 1);

$crawler = $client->request('GET', '/new');

$this->assertSame(200, $client->getResponse()->getStatusCode());

$form = $crawler->filter('form[name=entry]')->form();

$data = [
'entry[url]' => $this->wrongUrl,
];

$client->submit($form, $data);

$this->assertSame(302, $client->getResponse()->getStatusCode());

$content = $client->getContainer()
->get(EntityManagerInterface::class)
->getRepository(Entry::class)
->findByUrlAndUserId($this->wrongUrl, $this->getLoggedInUserId());

$this->assertFalse($content);
}

public function testArchive()
{
$this->logInAs('admin');
Expand Down