2015-10-16 10:51:53 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-01 21:27:35 +02:00
|
|
|
namespace Tests\Wallabag\CoreBundle\Controller;
|
2015-10-16 10:51:53 +02:00
|
|
|
|
2016-06-01 21:27:35 +02:00
|
|
|
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
|
2015-10-16 10:51:53 +02:00
|
|
|
|
|
|
|
class ExportControllerTest extends WallabagCoreTestCase
|
|
|
|
{
|
|
|
|
public function testLogin()
|
|
|
|
{
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
|
|
|
$client->request('GET', '/export/unread.csv');
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(302, $client->getResponse()->getStatusCode());
|
2015-10-16 10:51:53 +02:00
|
|
|
$this->assertContains('login', $client->getResponse()->headers->get('location'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnknownCategoryExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
2015-10-30 20:57:10 +01:00
|
|
|
$client->request('GET', '/export/awesomeness.epub');
|
2015-10-16 10:51:53 +02:00
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(404, $client->getResponse()->getStatusCode());
|
2015-10-16 10:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnknownFormatExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
2015-10-30 20:57:10 +01:00
|
|
|
$client->request('GET', '/export/unread.xslx');
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(404, $client->getResponse()->getStatusCode());
|
2015-10-30 20:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsupportedFormatExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
2016-01-25 22:25:55 +01:00
|
|
|
$client->request('GET', '/export/unread.doc');
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(404, $client->getResponse()->getStatusCode());
|
2015-10-30 20:57:10 +01:00
|
|
|
|
|
|
|
$content = $client->getContainer()
|
|
|
|
->get('doctrine.orm.entity_manager')
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
|
|
|
->findOneByUsernameAndNotArchived('admin');
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$client->request('GET', '/export/' . $content->getId() . '.doc');
|
|
|
|
$this->assertSame(404, $client->getResponse()->getStatusCode());
|
2015-10-30 20:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBadEntryId()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
|
|
|
$client->request('GET', '/export/0.mobi');
|
2015-10-16 10:51:53 +02:00
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(404, $client->getResponse()->getStatusCode());
|
2015-10-16 10:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEpubExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$crawler = $client->request('GET', '/export/archive.epub');
|
|
|
|
ob_end_clean();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2015-10-16 10:51:53 +02:00
|
|
|
|
|
|
|
$headers = $client->getResponse()->headers;
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('application/epub+zip', $headers->get('content-type'));
|
|
|
|
$this->assertSame('attachment; filename="Archive articles.epub"', $headers->get('content-disposition'));
|
|
|
|
$this->assertSame('binary', $headers->get('content-transfer-encoding'));
|
2015-10-16 10:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMobiExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
|
|
|
$content = $client->getContainer()
|
|
|
|
->get('doctrine.orm.entity_manager')
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
|
|
|
->findOneByUsernameAndNotArchived('admin');
|
|
|
|
|
|
|
|
ob_start();
|
2017-07-01 09:52:38 +02:00
|
|
|
$crawler = $client->request('GET', '/export/' . $content->getId() . '.mobi');
|
2015-10-16 10:51:53 +02:00
|
|
|
ob_end_clean();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2015-10-16 10:51:53 +02:00
|
|
|
|
|
|
|
$headers = $client->getResponse()->headers;
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('application/x-mobipocket-ebook', $headers->get('content-type'));
|
2019-01-07 23:50:08 +01:00
|
|
|
$this->assertSame('attachment; filename="' . $this->getSanitizedFilename($content->getTitle()) . '.mobi"', $headers->get('content-disposition'));
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('binary', $headers->get('content-transfer-encoding'));
|
2015-10-16 10:51:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPdfExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$crawler = $client->request('GET', '/export/all.pdf');
|
|
|
|
ob_end_clean();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2015-10-16 10:51:53 +02:00
|
|
|
|
|
|
|
$headers = $client->getResponse()->headers;
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('application/pdf', $headers->get('content-type'));
|
|
|
|
$this->assertSame('attachment; filename="All articles.pdf"', $headers->get('content-disposition'));
|
|
|
|
$this->assertSame('binary', $headers->get('content-transfer-encoding'));
|
2016-10-29 14:03:55 +02:00
|
|
|
|
|
|
|
ob_start();
|
2016-12-27 21:26:53 +01:00
|
|
|
$crawler = $client->request('GET', '/export/tag_entries.pdf?tag=foo-bar');
|
2016-10-29 14:03:55 +02:00
|
|
|
ob_end_clean();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2016-10-29 14:03:55 +02:00
|
|
|
|
|
|
|
$headers = $client->getResponse()->headers;
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('application/pdf', $headers->get('content-type'));
|
2019-01-07 23:50:08 +01:00
|
|
|
$this->assertSame('attachment; filename="Tag foo bar articles.pdf"', $headers->get('content-disposition'));
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('binary', $headers->get('content-transfer-encoding'));
|
2015-10-16 10:51:53 +02:00
|
|
|
}
|
|
|
|
|
2016-01-25 22:25:55 +01:00
|
|
|
public function testTxtExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$crawler = $client->request('GET', '/export/all.txt');
|
|
|
|
ob_end_clean();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2016-01-25 22:25:55 +01:00
|
|
|
|
|
|
|
$headers = $client->getResponse()->headers;
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('text/plain; charset=UTF-8', $headers->get('content-type'));
|
|
|
|
$this->assertSame('attachment; filename="All articles.txt"', $headers->get('content-disposition'));
|
|
|
|
$this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
|
2016-01-25 22:25:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-16 10:51:53 +02:00
|
|
|
public function testCsvExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
2015-10-30 20:57:10 +01:00
|
|
|
// to be sure results are the same
|
|
|
|
$contentInDB = $client->getContainer()
|
|
|
|
->get('doctrine.orm.entity_manager')
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
|
|
|
->createQueryBuilder('e')
|
2016-10-02 16:06:42 +02:00
|
|
|
->select('e, t')
|
2015-10-30 20:57:10 +01:00
|
|
|
->leftJoin('e.user', 'u')
|
2016-10-02 16:06:42 +02:00
|
|
|
->leftJoin('e.tags', 't')
|
2015-10-30 20:57:10 +01:00
|
|
|
->where('u.username = :username')->setParameter('username', 'admin')
|
|
|
|
->andWhere('e.isArchived = true')
|
|
|
|
->getQuery()
|
|
|
|
->getArrayResult();
|
|
|
|
|
2015-10-16 10:51:53 +02:00
|
|
|
ob_start();
|
2015-10-30 20:57:10 +01:00
|
|
|
$crawler = $client->request('GET', '/export/archive.csv');
|
2015-10-16 10:51:53 +02:00
|
|
|
ob_end_clean();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2015-10-16 10:51:53 +02:00
|
|
|
|
|
|
|
$headers = $client->getResponse()->headers;
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('application/csv', $headers->get('content-type'));
|
|
|
|
$this->assertSame('attachment; filename="Archive articles.csv"', $headers->get('content-disposition'));
|
|
|
|
$this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
|
2015-10-16 10:51:53 +02:00
|
|
|
|
|
|
|
$csv = str_getcsv($client->getResponse()->getContent(), "\n");
|
|
|
|
|
|
|
|
$this->assertGreaterThan(1, $csv);
|
2015-10-30 20:57:10 +01:00
|
|
|
// +1 for title line
|
2019-01-17 14:28:05 +01:00
|
|
|
$this->assertCount(\count($contentInDB) + 1, $csv);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('Title;URL;Content;Tags;"MIME Type";Language;"Creation date"', $csv[0]);
|
2016-10-02 16:06:42 +02:00
|
|
|
$this->assertContains($contentInDB[0]['title'], $csv[1]);
|
|
|
|
$this->assertContains($contentInDB[0]['url'], $csv[1]);
|
|
|
|
$this->assertContains($contentInDB[0]['content'], $csv[1]);
|
|
|
|
$this->assertContains($contentInDB[0]['mimetype'], $csv[1]);
|
|
|
|
$this->assertContains($contentInDB[0]['language'], $csv[1]);
|
|
|
|
$this->assertContains($contentInDB[0]['createdAt']->format('d/m/Y h:i:s'), $csv[1]);
|
|
|
|
|
|
|
|
foreach ($contentInDB[0]['tags'] as $tag) {
|
2017-05-03 10:53:10 +02:00
|
|
|
$this->assertContains($tag['label'], $csv[1]);
|
2016-10-02 16:06:42 +02:00
|
|
|
}
|
2015-10-16 10:51:53 +02:00
|
|
|
}
|
2015-10-18 15:49:00 +02:00
|
|
|
|
|
|
|
public function testJsonExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
2015-10-30 20:57:10 +01:00
|
|
|
$contentInDB = $client->getContainer()
|
|
|
|
->get('doctrine.orm.entity_manager')
|
|
|
|
->getRepository('WallabagCoreBundle:Entry')
|
2016-10-02 16:53:52 +02:00
|
|
|
->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
|
2015-10-30 20:57:10 +01:00
|
|
|
|
2015-10-18 15:49:00 +02:00
|
|
|
ob_start();
|
2017-07-01 09:52:38 +02:00
|
|
|
$crawler = $client->request('GET', '/export/' . $contentInDB->getId() . '.json');
|
2015-10-18 15:49:00 +02:00
|
|
|
ob_end_clean();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2015-10-18 15:49:00 +02:00
|
|
|
|
|
|
|
$headers = $client->getResponse()->headers;
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('application/json', $headers->get('content-type'));
|
2019-01-07 23:50:08 +01:00
|
|
|
$this->assertSame('attachment; filename="' . $this->getSanitizedFilename($contentInDB->getTitle()) . '.json"', $headers->get('content-disposition'));
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
|
2015-10-30 20:57:10 +01:00
|
|
|
|
|
|
|
$content = json_decode($client->getResponse()->getContent(), true);
|
|
|
|
$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('tags', $content[0]);
|
2016-09-08 16:38:08 +02:00
|
|
|
$this->assertArrayHasKey('created_at', $content[0]);
|
|
|
|
$this->assertArrayHasKey('updated_at', $content[0]);
|
2016-10-02 16:06:42 +02:00
|
|
|
|
2017-07-03 07:30:54 +02:00
|
|
|
$this->assertSame((int) $contentInDB->isArchived(), $content[0]['is_archived']);
|
|
|
|
$this->assertSame((int) $contentInDB->isStarred(), $content[0]['is_starred']);
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame($contentInDB->getTitle(), $content[0]['title']);
|
|
|
|
$this->assertSame($contentInDB->getUrl(), $content[0]['url']);
|
|
|
|
$this->assertSame([['text' => 'This is my annotation /o/', 'quote' => 'content']], $content[0]['annotations']);
|
|
|
|
$this->assertSame($contentInDB->getMimetype(), $content[0]['mimetype']);
|
|
|
|
$this->assertSame($contentInDB->getLanguage(), $content[0]['language']);
|
|
|
|
$this->assertSame($contentInDB->getReadingtime(), $content[0]['reading_time']);
|
|
|
|
$this->assertSame($contentInDB->getDomainname(), $content[0]['domain_name']);
|
2017-08-03 16:20:49 +02:00
|
|
|
$this->assertContains('baz', $content[0]['tags']);
|
|
|
|
$this->assertContains('foo', $content[0]['tags']);
|
2015-10-18 15:49:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testXmlExport()
|
|
|
|
{
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$client = $this->getClient();
|
|
|
|
|
2015-10-30 20:57:10 +01:00
|
|
|
// 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();
|
|
|
|
|
2015-10-18 15:49:00 +02:00
|
|
|
ob_start();
|
|
|
|
$crawler = $client->request('GET', '/export/unread.xml');
|
|
|
|
ob_end_clean();
|
|
|
|
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2015-10-18 15:49:00 +02:00
|
|
|
|
|
|
|
$headers = $client->getResponse()->headers;
|
2017-07-01 09:52:38 +02:00
|
|
|
$this->assertSame('application/xml', $headers->get('content-type'));
|
|
|
|
$this->assertSame('attachment; filename="Unread articles.xml"', $headers->get('content-disposition'));
|
|
|
|
$this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
|
2015-10-30 20:57:10 +01:00
|
|
|
|
|
|
|
$content = new \SimpleXMLElement($client->getResponse()->getContent());
|
|
|
|
$this->assertGreaterThan(0, $content->count());
|
2018-09-05 14:25:32 +02:00
|
|
|
$this->assertSame(\count($contentInDB), $content->count());
|
2015-10-30 20:57:10 +01:00
|
|
|
$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);
|
2016-09-08 16:38:08 +02:00
|
|
|
$this->assertNotEmpty('created_at', (string) $content->entry[0]->created_at);
|
|
|
|
$this->assertNotEmpty('updated_at', (string) $content->entry[0]->updated_at);
|
2015-10-18 15:49:00 +02:00
|
|
|
}
|
2019-01-07 23:50:08 +01:00
|
|
|
|
|
|
|
private function getSanitizedFilename($title)
|
|
|
|
{
|
|
|
|
return preg_replace('/[^A-Za-z0-9\- \']/', '', iconv('utf-8', 'us-ascii//TRANSLIT', $title));
|
|
|
|
}
|
2015-10-16 10:51:53 +02:00
|
|
|
}
|