Fix route parameters

Improve export tests
Improve CSV export
This commit is contained in:
Jeremy Benoist 2015-10-30 20:57:10 +01:00 committed by Nicolas Lœuillet
parent 268e9e7277
commit cceca9ea1d
3 changed files with 108 additions and 8 deletions

View file

@ -7,6 +7,10 @@ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Wallabag\CoreBundle\Entity\Entry;
/**
* The try/catch can be removed once all formats will be implemented.
* Still need implementation: txt.
*/
class ExportController extends Controller
{
/**
@ -14,7 +18,10 @@ class ExportController extends Controller
*
* @param Entry $entry
*
* @Route("/export/{id}.{format}", requirements={"id" = "\d+"}, name="export_entry")
* @Route("/export/{id}.{format}", name="export_entry", requirements={
* "format": "epub|mobi|pdf|json|xml|txt|csv",
* "id": "\d+"
* })
*/
public function downloadEntryAction(Entry $entry, $format)
{
@ -32,7 +39,7 @@ class ExportController extends Controller
* Export all entries for current user.
*
* @Route("/export/{category}.{format}", name="export_entries", requirements={
* "_format": "epub|mobi|pdf|json|xml|txt|csv",
* "format": "epub|mobi|pdf|json|xml|txt|csv",
* "category": "all|unread|starred|archive"
* })
*/

View file

@ -9,6 +9,9 @@ use JMS\Serializer;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializationContext;
/**
* This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest.
*/
class EntriesExport
{
private $wallabagUrl;
@ -303,7 +306,8 @@ class EntriesExport
array(
$entry->getTitle(),
$entry->getURL(),
$entry->getContent(),
// remove new line to avoid crazy results
str_replace(array("\r\n", "\r", "\n"), '', $entry->getContent()),
implode(', ', $entry->getTags()->toArray()),
$entry->getMimetype(),
$entry->getLanguage(),
@ -363,7 +367,11 @@ class EntriesExport
{
$serializer = SerializerBuilder::create()->build();
return $serializer->serialize($this->entries, $format, SerializationContext::create()->setGroups(array('entries_for_user')));
return $serializer->serialize(
$this->entries,
$format,
SerializationContext::create()->setGroups(array('entries_for_user'))
);
}
/**

View file

@ -21,7 +21,7 @@ class ExportControllerTest extends WallabagCoreTestCase
$this->logInAs('admin');
$client = $this->getClient();
$crawler = $client->request('GET', '/export/awesomeness.epub');
$client->request('GET', '/export/awesomeness.epub');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
@ -31,7 +31,34 @@ class ExportControllerTest extends WallabagCoreTestCase
$this->logInAs('admin');
$client = $this->getClient();
$crawler = $client->request('GET', '/export/unread.xslx');
$client->request('GET', '/export/unread.xslx');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
public function testUnsupportedFormatExport()
{
$this->logInAs('admin');
$client = $this->getClient();
$client->request('GET', '/export/unread.txt');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
$content = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findOneByUsernameAndNotArchived('admin');
$client->request('GET', '/export/'.$content->getId().'.txt');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
public function testBadEntryId()
{
$this->logInAs('admin');
$client = $this->getClient();
$client->request('GET', '/export/0.mobi');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
@ -97,20 +124,33 @@ class ExportControllerTest extends WallabagCoreTestCase
$this->logInAs('admin');
$client = $this->getClient();
// to be sure results are the same
$contentInDB = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->createQueryBuilder('e')
->leftJoin('e.user', 'u')
->where('u.username = :username')->setParameter('username', 'admin')
->andWhere('e.isArchived = true')
->getQuery()
->getArrayResult();
ob_start();
$crawler = $client->request('GET', '/export/unread.csv');
$crawler = $client->request('GET', '/export/archive.csv');
ob_end_clean();
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
$this->assertEquals('application/csv', $headers->get('content-type'));
$this->assertEquals('attachment; filename="Unread articles.csv"', $headers->get('content-disposition'));
$this->assertEquals('attachment; filename="Archive articles.csv"', $headers->get('content-disposition'));
$this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
$csv = str_getcsv($client->getResponse()->getContent(), "\n");
$this->assertGreaterThan(1, $csv);
// +1 for title line
$this->assertEquals(count($contentInDB)+1, count($csv));
$this->assertEquals('Title;URL;Content;Tags;"MIME Type";Language', $csv[0]);
}
@ -119,6 +159,16 @@ class ExportControllerTest extends WallabagCoreTestCase
$this->logInAs('admin');
$client = $this->getClient();
// to be sure results are the same
$contentInDB = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->createQueryBuilder('e')
->leftJoin('e.user', 'u')
->where('u.username = :username')->setParameter('username', 'admin')
->getQuery()
->getArrayResult();
ob_start();
$crawler = $client->request('GET', '/export/all.json');
ob_end_clean();
@ -129,6 +179,21 @@ class ExportControllerTest extends WallabagCoreTestCase
$this->assertEquals('application/json', $headers->get('content-type'));
$this->assertEquals('attachment; filename="All articles.json"', $headers->get('content-disposition'));
$this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals(count($contentInDB), count($content));
$this->assertArrayHasKey('id', $content[0]);
$this->assertArrayHasKey('title', $content[0]);
$this->assertArrayHasKey('url', $content[0]);
$this->assertArrayHasKey('is_archived', $content[0]);
$this->assertArrayHasKey('is_starred', $content[0]);
$this->assertArrayHasKey('content', $content[0]);
$this->assertArrayHasKey('mimetype', $content[0]);
$this->assertArrayHasKey('language', $content[0]);
$this->assertArrayHasKey('reading_time', $content[0]);
$this->assertArrayHasKey('domain_name', $content[0]);
$this->assertArrayHasKey('preview_picture', $content[0]);
$this->assertArrayHasKey('tags', $content[0]);
}
public function testXmlExport()
@ -136,6 +201,17 @@ class ExportControllerTest extends WallabagCoreTestCase
$this->logInAs('admin');
$client = $this->getClient();
// to be sure results are the same
$contentInDB = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->createQueryBuilder('e')
->leftJoin('e.user', 'u')
->where('u.username = :username')->setParameter('username', 'admin')
->andWhere('e.isArchived = false')
->getQuery()
->getArrayResult();
ob_start();
$crawler = $client->request('GET', '/export/unread.xml');
ob_end_clean();
@ -146,5 +222,14 @@ class ExportControllerTest extends WallabagCoreTestCase
$this->assertEquals('application/xml', $headers->get('content-type'));
$this->assertEquals('attachment; filename="Unread articles.xml"', $headers->get('content-disposition'));
$this->assertEquals('UTF-8', $headers->get('content-transfer-encoding'));
$content = new \SimpleXMLElement($client->getResponse()->getContent());
$this->assertGreaterThan(0, $content->count());
$this->assertEquals(count($contentInDB), $content->count());
$this->assertNotEmpty('id', (string) $content->entry[0]->id);
$this->assertNotEmpty('title', (string) $content->entry[0]->title);
$this->assertNotEmpty('url', (string) $content->entry[0]->url);
$this->assertNotEmpty('content', (string) $content->entry[0]->content);
$this->assertNotEmpty('domain_name', (string) $content->entry[0]->domain_name);
}
}