wallabag/tests/Controller/ExportControllerTest.php

370 lines
14 KiB
PHP
Raw Normal View History

<?php
2024-02-19 00:30:12 +00:00
namespace Tests\Wallabag\Controller;
2022-08-28 00:01:46 +00:00
use Doctrine\ORM\EntityManagerInterface;
use Tests\Wallabag\WallabagTestCase;
2024-02-19 00:30:12 +00:00
use Wallabag\Entity\Entry;
use Wallabag\Repository\UserRepository;
class ExportControllerTest extends WallabagTestCase
{
private $adminEntry;
private $bobEntry;
2023-06-29 15:10:23 +00:00
private $sameDomainEntry;
private $sameDomainEntry2;
public function testLogin()
{
$client = $this->getTestClient();
$client->request('GET', '/export/unread.csv');
2017-07-01 07:52:38 +00:00
$this->assertSame(302, $client->getResponse()->getStatusCode());
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString('login', $client->getResponse()->headers->get('location'));
}
public function testUnknownCategoryExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
$client->request('GET', '/export/awesomeness.epub');
2017-07-01 07:52:38 +00:00
$this->assertSame(404, $client->getResponse()->getStatusCode());
}
public function testUnknownFormatExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
$client->request('GET', '/export/unread.xslx');
2017-07-01 07:52:38 +00:00
$this->assertSame(404, $client->getResponse()->getStatusCode());
}
public function testUnsupportedFormatExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
2016-01-25 21:25:55 +00:00
$client->request('GET', '/export/unread.doc');
2017-07-01 07:52:38 +00:00
$this->assertSame(404, $client->getResponse()->getStatusCode());
$content = $client->getContainer()
2022-08-28 00:01:46 +00:00
->get(EntityManagerInterface::class)
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->findOneByUsernameAndNotArchived('admin');
2017-07-01 07:52:38 +00:00
$client->request('GET', '/export/' . $content->getId() . '.doc');
$this->assertSame(404, $client->getResponse()->getStatusCode());
}
public function testNonExistingEntryId()
{
$this->logInAs('admin');
$client = $this->getTestClient();
2023-08-23 06:44:40 +00:00
$client->request('GET', '/export/0.pdf');
2017-07-01 07:52:38 +00:00
$this->assertSame(404, $client->getResponse()->getStatusCode());
}
public function testForbiddenEntryId()
{
$this->logInAs('admin');
$client = $this->getTestClient();
2023-02-01 08:51:02 +00:00
$content = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository(Entry::class)
2023-02-01 08:51:02 +00:00
->findOneByUsernameAndNotArchived('bob');
2023-08-23 06:44:40 +00:00
$client->request('GET', '/export/' . $content->getId() . '.pdf');
$this->assertSame(404, $client->getResponse()->getStatusCode());
}
public function testEpubExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
ob_start();
$crawler = $client->request('GET', '/export/archive.epub');
ob_end_clean();
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
2017-07-01 07:52:38 +00: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'));
}
public function testPdfExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
ob_start();
$crawler = $client->request('GET', '/export/all.pdf');
ob_end_clean();
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
2017-07-01 07:52:38 +00: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'));
ob_start();
$crawler = $client->request('GET', '/export/tag_entries.pdf?tag=t:foo-bar');
ob_end_clean();
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
2017-07-01 07:52:38 +00:00
$this->assertSame('application/pdf', $headers->get('content-type'));
$this->assertSame('attachment; filename="Tag foo bar articles.pdf"', $headers->get('content-disposition'));
2017-07-01 07:52:38 +00:00
$this->assertSame('binary', $headers->get('content-transfer-encoding'));
}
2016-01-25 21:25:55 +00:00
public function testTxtExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
2016-01-25 21:25:55 +00:00
ob_start();
$crawler = $client->request('GET', '/export/all.txt');
ob_end_clean();
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
2016-01-25 21:25:55 +00:00
$headers = $client->getResponse()->headers;
2017-07-01 07:52:38 +00: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 21:25:55 +00:00
}
public function testCsvExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
// to be sure results are the same
$contentInDB = $client->getContainer()
2022-08-28 00:01:46 +00:00
->get(EntityManagerInterface::class)
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->createQueryBuilder('e')
->select('e, t')
->leftJoin('e.user', 'u')
->leftJoin('e.tags', 't')
->where('u.username = :username')->setParameter('username', 'admin')
->andWhere('e.isArchived = true')
->getQuery()
->getArrayResult();
ob_start();
$crawler = $client->request('GET', '/export/archive.csv');
ob_end_clean();
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
2017-07-01 07:52:38 +00: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'));
$csv = str_getcsv($client->getResponse()->getContent(), "\n");
$this->assertGreaterThan(1, $csv);
// +1 for title line
$this->assertCount(\count($contentInDB) + 1, $csv);
2017-07-01 07:52:38 +00:00
$this->assertSame('Title;URL;Content;Tags;"MIME Type";Language;"Creation date"', $csv[0]);
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString($contentInDB[0]['title'], $csv[1]);
$this->assertStringContainsString($contentInDB[0]['url'], $csv[1]);
$this->assertStringContainsString($contentInDB[0]['content'], $csv[1]);
$this->assertStringContainsString($contentInDB[0]['mimetype'], $csv[1]);
$this->assertStringContainsString($contentInDB[0]['language'], $csv[1]);
$this->assertStringContainsString($contentInDB[0]['createdAt']->format('d/m/Y h:i:s'), $csv[1]);
foreach ($contentInDB[0]['tags'] as $tag) {
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString($tag['label'], $csv[1]);
}
}
2015-10-18 13:49:00 +00:00
public function testJsonExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
2015-10-18 13:49:00 +00:00
$contentInDB = $client->getContainer()
2022-08-28 00:01:46 +00:00
->get(EntityManagerInterface::class)
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
2016-10-02 14:53:52 +00:00
->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
2015-10-18 13:49:00 +00:00
ob_start();
2017-07-01 07:52:38 +00:00
$crawler = $client->request('GET', '/export/' . $contentInDB->getId() . '.json');
2015-10-18 13:49:00 +00:00
ob_end_clean();
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
2015-10-18 13:49:00 +00:00
$headers = $client->getResponse()->headers;
2017-07-01 07:52:38 +00:00
$this->assertSame('application/json', $headers->get('content-type'));
$this->assertSame('attachment; filename="' . $this->getSanitizedFilename($contentInDB->getTitle()) . '.json"', $headers->get('content-disposition'));
2017-07-01 07:52:38 +00:00
$this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
$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 14:38:08 +00:00
$this->assertArrayHasKey('created_at', $content[0]);
$this->assertArrayHasKey('updated_at', $content[0]);
2017-07-03 05:30:54 +00:00
$this->assertSame((int) $contentInDB->isArchived(), $content[0]['is_archived']);
$this->assertSame((int) $contentInDB->isStarred(), $content[0]['is_starred']);
2017-07-01 07:52:38 +00: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 14:20:49 +00:00
$this->assertContains('baz', $content[0]['tags']);
$this->assertContains('foo', $content[0]['tags']);
2015-10-18 13:49:00 +00:00
}
public function testJsonExportFromSearch()
{
$this->setUpForJsonExportFromSearch();
$this->logInAs('admin');
$client = $this->getTestClient();
ob_start();
$crawler = $client->request('GET', '/export/search.json?search_entry[term]=entry+search&currentRoute=homepage');
ob_end_clean();
$this->assertSame(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
$this->assertSame('application/json', $headers->get('content-type'));
$this->assertSame('attachment; filename="Search entry search articles.json"', $headers->get('content-disposition'));
$this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertCount(1, $content);
$this->tearDownForJsonExportFromSearch();
}
2015-10-18 13:49:00 +00:00
public function testXmlExport()
{
$this->logInAs('admin');
$client = $this->getTestClient();
2015-10-18 13:49:00 +00:00
// to be sure results are the same
$contentInDB = $client->getContainer()
2022-08-28 00:01:46 +00:00
->get(EntityManagerInterface::class)
2022-08-25 19:37:10 +00:00
->getRepository(Entry::class)
->createQueryBuilder('e')
->leftJoin('e.user', 'u')
->where('u.username = :username')->setParameter('username', 'admin')
->andWhere('e.isArchived = false')
->getQuery()
->getArrayResult();
2015-10-18 13:49:00 +00:00
ob_start();
$crawler = $client->request('GET', '/export/unread.xml');
ob_end_clean();
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
2015-10-18 13:49:00 +00:00
$headers = $client->getResponse()->headers;
2017-07-01 07:52:38 +00: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'));
$content = new \SimpleXMLElement($client->getResponse()->getContent());
$this->assertGreaterThan(0, $content->count());
2020-06-15 11:37:50 +00:00
$this->assertCount(\count($contentInDB), $content);
$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 14:38:08 +00:00
$this->assertNotEmpty('created_at', (string) $content->entry[0]->created_at);
$this->assertNotEmpty('updated_at', (string) $content->entry[0]->updated_at);
2015-10-18 13:49:00 +00:00
}
2023-06-29 15:10:23 +00:00
public function testJsonExportFromSameDomain()
{
$this->logInAs('admin');
$client = $this->getTestClient();
ob_start();
$crawler = $client->request('GET', '/export/same_domain.json?entry=1');
ob_end_clean();
$this->assertSame(200, $client->getResponse()->getStatusCode());
$headers = $client->getResponse()->headers;
$this->assertSame('application/json', $headers->get('content-type'));
$this->assertSame('attachment; filename="Same domain articles.json"', $headers->get('content-disposition'));
$this->assertSame('UTF-8', $headers->get('content-transfer-encoding'));
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertCount(4, $content);
}
private function setUpForJsonExportFromSearch()
{
$em = $this->getEntityManager();
$userRepository = static::getContainer()->get(UserRepository::class);
$user = $userRepository->findOneByUserName('admin');
$this->adminEntry = new Entry($user);
$this->adminEntry->setUrl('http://0.0.0.0/entry-search-admin');
$this->adminEntry->setTitle('test title entry search admin');
$this->adminEntry->setContent('this is my content /o/');
$em->persist($this->adminEntry);
$user = $userRepository->findOneByUserName('bob');
$this->bobEntry = new Entry($user);
$this->bobEntry->setUrl('http://0.0.0.0/entry-search-bob');
$this->bobEntry->setTitle('test title entry search bob');
$this->bobEntry->setContent('this is my content /o/');
$em->persist($this->bobEntry);
$em->flush();
}
private function tearDownForJsonExportFromSearch()
{
$em = $this->getEntityManager();
$em->remove($this->adminEntry);
$em->remove($this->bobEntry);
$em->flush();
}
private function getSanitizedFilename($title)
{
$transliterator = \Transliterator::createFromRules(':: Any-Latin; :: Latin-ASCII; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC;', \Transliterator::FORWARD);
return preg_replace('/[^A-Za-z0-9\- \']/', '', $transliterator->transliterate($title));
}
}