Update test

and some cleanup
This commit is contained in:
Jeremy Benoist 2016-08-24 22:29:36 +02:00
parent b1afef30dc
commit eddda878a0
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
3 changed files with 41 additions and 18 deletions

View file

@ -465,8 +465,12 @@ class EntryController extends Controller
{
$this->checkUserAction($entry);
if ('' === $entry->getUuid() || null === $entry->getUuid()) {
$this->generateEntryUuid($entry);
if (null === $entry->getUuid()) {
$entry->generateUuid();
$em = $this->getDoctrine()->getManager();
$em->persist($entry);
$em->flush();
}
return $this->redirect($this->generateUrl('share_entry', [
@ -488,6 +492,7 @@ class EntryController extends Controller
$this->checkUserAction($entry);
$entry->cleanUuid();
$em = $this->getDoctrine()->getManager();
$em->persist($entry);
$em->flush();
@ -498,31 +503,24 @@ class EntryController extends Controller
}
/**
* Share entry content.
* Ability to view a content publicly.
*
* @param Entry $entry
*
* @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
* @Cache(maxage="25200", public=true)
* @Cache(maxage="25200", smaxage="25200", public=true)
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function shareEntryAction(Entry $entry)
{
if (!$this->get('craue_config')->get('share_public')) {
throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
}
return $this->render(
'@WallabagCore/themes/share.html.twig',
array('entry' => $entry)
['entry' => $entry]
);
}
/**
* @param Entry $entry
*/
private function generateEntryUuid(Entry $entry)
{
$entry->generateUuid();
$em = $this->getDoctrine()->getManager();
$em->persist($entry);
$em->flush();
}
}

View file

@ -627,7 +627,7 @@ class Entry
public function generateUuid()
{
if (empty($this->uuid) || is_null($this->uuid)) {
if (null === $this->uuid) {
// @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
$this->uuid = uniqid('', true);
}

View file

@ -709,11 +709,36 @@ class EntryControllerTest extends WallabagCoreTestCase
->getRepository('WallabagCoreBundle:Entry')
->findOneByUser($this->getLoggedInUserId());
// no uuid
$client->request('GET', '/share/'.$content->getUuid());
$this->assertContains('max-age=25200, public', $client->getResponse()->headers->get('cache-control'));
$this->assertEquals(404, $client->getResponse()->getStatusCode());
// generating the uuid
$client->request('GET', '/share/'.$content->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode());
// follow link with uuid
$crawler = $client->followRedirect();
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertContains('max-age=25200', $client->getResponse()->headers->get('cache-control'));
$this->assertContains('public', $client->getResponse()->headers->get('cache-control'));
$this->assertContains('s-maxage=25200', $client->getResponse()->headers->get('cache-control'));
$this->assertNotContains('no-cache', $client->getResponse()->headers->get('cache-control'));
// sharing is now disabled
$client->getContainer()->get('craue_config')->set('share_public', 0);
$client->request('GET', '/share/'.$content->getUuid());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
$client->request('GET', '/view/'.$content->getId());
$this->assertContains('no-cache', $client->getResponse()->headers->get('cache-control'));
// removing the share
$client->request('GET', '/share/delete/'.$content->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode());
// share is now disable
$client->request('GET', '/share/'.$content->getUuid());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
}