2017-06-13 16:48:10 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Tests\Wallabag\Controller;
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2024-02-24 19:24:51 +00:00
|
|
|
use Tests\Wallabag\WallabagTestCase;
|
2024-02-19 00:30:12 +00:00
|
|
|
use Wallabag\Entity\Entry;
|
|
|
|
use Wallabag\Entity\User;
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2024-02-24 19:24:51 +00:00
|
|
|
class FeedControllerTest extends WallabagTestCase
|
2017-06-13 16:48:10 +00:00
|
|
|
{
|
|
|
|
public function validateDom($xml, $type, $nb = null, $tagValue = null)
|
|
|
|
{
|
|
|
|
$doc = new \DOMDocument();
|
|
|
|
$doc->loadXML($xml);
|
|
|
|
|
2019-04-25 12:12:56 +00:00
|
|
|
$xpath = new \DOMXPath($doc);
|
2017-06-13 16:48:10 +00:00
|
|
|
$xpath->registerNamespace('a', 'http://www.w3.org/2005/Atom');
|
|
|
|
|
|
|
|
if (null === $nb) {
|
|
|
|
$this->assertGreaterThan(0, $xpath->query('//a:entry')->length);
|
|
|
|
} else {
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame($nb, $xpath->query('//a:entry')->length);
|
2017-06-13 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(1, $xpath->query('/a:feed')->length);
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(1, $xpath->query('/a:feed/a:title')->length);
|
2020-06-15 11:37:50 +00:00
|
|
|
$this->assertStringContainsString('favicon.ico', $xpath->query('/a:feed/a:icon')->item(0)->nodeValue);
|
2024-02-24 23:10:23 +00:00
|
|
|
$this->assertStringContainsString('logo-square.svg', $xpath->query('/a:feed/a:logo')->item(0)->nodeValue);
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(1, $xpath->query('/a:feed/a:updated')->length);
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(1, $xpath->query('/a:feed/a:generator')->length);
|
|
|
|
$this->assertSame('wallabag', $xpath->query('/a:feed/a:generator')->item(0)->nodeValue);
|
|
|
|
$this->assertSame('admin', $xpath->query('/a:feed/a:author/a:name')->item(0)->nodeValue);
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(1, $xpath->query('/a:feed/a:subtitle')->length);
|
2024-01-01 18:11:01 +00:00
|
|
|
if (null !== $tagValue && str_starts_with($type, 'tag')) {
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame('wallabag — ' . $type . ' ' . $tagValue . ' feed', $xpath->query('/a:feed/a:title')->item(0)->nodeValue);
|
|
|
|
$this->assertSame('Atom feed for entries tagged with ' . $tagValue, $xpath->query('/a:feed/a:subtitle')->item(0)->nodeValue);
|
2017-06-13 16:48:10 +00:00
|
|
|
} else {
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame('wallabag — ' . $type . ' feed', $xpath->query('/a:feed/a:title')->item(0)->nodeValue);
|
|
|
|
$this->assertSame('Atom feed for ' . $type . ' entries', $xpath->query('/a:feed/a:subtitle')->item(0)->nodeValue);
|
2017-06-13 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(1, $xpath->query('/a:feed/a:link[@rel="self"]')->length);
|
2020-06-15 11:37:50 +00:00
|
|
|
$this->assertStringContainsString($type, $xpath->query('/a:feed/a:link[@rel="self"]')->item(0)->getAttribute('href'));
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(1, $xpath->query('/a:feed/a:link[@rel="last"]')->length);
|
2017-06-13 16:48:10 +00:00
|
|
|
|
|
|
|
foreach ($xpath->query('//a:entry') as $item) {
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(1, $xpath->query('a:title', $item)->length);
|
|
|
|
$this->assertSame(1, $xpath->query('a:link[@rel="via"]', $item)->length);
|
|
|
|
$this->assertSame(1, $xpath->query('a:link[@rel="alternate"]', $item)->length);
|
|
|
|
$this->assertSame(1, $xpath->query('a:id', $item)->length);
|
|
|
|
$this->assertSame(1, $xpath->query('a:published', $item)->length);
|
|
|
|
$this->assertSame(1, $xpath->query('a:content', $item)->length);
|
2017-06-13 16:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataForBadUrl()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'/feed/admin/YZIOAUZIAO/unread',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/feed/wallace/YZIOAUZIAO/starred',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/feed/wallace/YZIOAUZIAO/archives',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/feed/wallace/YZIOAUZIAO/all',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataForBadUrl
|
|
|
|
*/
|
|
|
|
public function testBadUrl($url)
|
|
|
|
{
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2017-06-13 16:48:10 +00:00
|
|
|
|
|
|
|
$client->request('GET', $url);
|
|
|
|
|
|
|
|
$this->assertSame(404, $client->getResponse()->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnread()
|
|
|
|
{
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$em = $client->getContainer()->get(EntityManagerInterface::class);
|
2017-06-13 16:48:10 +00:00
|
|
|
$user = $em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(User::class)
|
2017-06-13 16:48:10 +00:00
|
|
|
->findOneByUsername('admin');
|
|
|
|
|
|
|
|
$config = $user->getConfig();
|
|
|
|
$config->setFeedToken('SUPERTOKEN');
|
|
|
|
$config->setFeedLimit(2);
|
|
|
|
$em->persist($config);
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/unread');
|
|
|
|
|
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
|
|
|
|
|
|
|
$this->validateDom($client->getResponse()->getContent(), 'unread', 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStarred()
|
|
|
|
{
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$em = $client->getContainer()->get(EntityManagerInterface::class);
|
2017-06-13 16:48:10 +00:00
|
|
|
$user = $em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(User::class)
|
2017-06-13 16:48:10 +00:00
|
|
|
->findOneByUsername('admin');
|
|
|
|
|
|
|
|
$config = $user->getConfig();
|
|
|
|
$config->setFeedToken('SUPERTOKEN');
|
|
|
|
$config->setFeedLimit(1);
|
|
|
|
$em->persist($config);
|
|
|
|
$em->flush();
|
|
|
|
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2017-06-13 16:48:10 +00:00
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/starred');
|
|
|
|
|
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode(), 1);
|
|
|
|
|
|
|
|
$this->validateDom($client->getResponse()->getContent(), 'starred');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArchives()
|
|
|
|
{
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$em = $client->getContainer()->get(EntityManagerInterface::class);
|
2017-06-13 16:48:10 +00:00
|
|
|
$user = $em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(User::class)
|
2017-06-13 16:48:10 +00:00
|
|
|
->findOneByUsername('admin');
|
|
|
|
|
|
|
|
$config = $user->getConfig();
|
|
|
|
$config->setFeedToken('SUPERTOKEN');
|
|
|
|
$config->setFeedLimit(null);
|
|
|
|
$em->persist($config);
|
|
|
|
$em->flush();
|
|
|
|
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2017-06-13 16:48:10 +00:00
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/archive');
|
|
|
|
|
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
|
|
|
|
|
|
|
$this->validateDom($client->getResponse()->getContent(), 'archive');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAll()
|
|
|
|
{
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$em = $client->getContainer()->get(EntityManagerInterface::class);
|
2017-06-13 16:48:10 +00:00
|
|
|
$user = $em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(User::class)
|
2017-06-13 16:48:10 +00:00
|
|
|
->findOneByUsername('admin');
|
|
|
|
|
|
|
|
$config = $user->getConfig();
|
|
|
|
$config->setFeedToken('SUPERTOKEN');
|
|
|
|
$config->setFeedLimit(null);
|
|
|
|
$em->persist($config);
|
|
|
|
$em->flush();
|
|
|
|
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2017-06-13 16:48:10 +00:00
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/all');
|
|
|
|
|
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
|
|
|
|
|
|
|
$this->validateDom($client->getResponse()->getContent(), 'all');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPagination()
|
|
|
|
{
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$em = $client->getContainer()->get(EntityManagerInterface::class);
|
2017-06-13 16:48:10 +00:00
|
|
|
$user = $em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(User::class)
|
2017-06-13 16:48:10 +00:00
|
|
|
->findOneByUsername('admin');
|
|
|
|
|
|
|
|
$config = $user->getConfig();
|
|
|
|
$config->setFeedToken('SUPERTOKEN');
|
|
|
|
$config->setFeedLimit(1);
|
|
|
|
$em->persist($config);
|
|
|
|
$em->flush();
|
|
|
|
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2017-06-13 16:48:10 +00:00
|
|
|
|
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/unread');
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2017-06-13 16:48:10 +00:00
|
|
|
$this->validateDom($client->getResponse()->getContent(), 'unread');
|
|
|
|
|
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/unread/2');
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2017-06-13 16:48:10 +00:00
|
|
|
$this->validateDom($client->getResponse()->getContent(), 'unread');
|
|
|
|
|
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/unread/3000');
|
2019-04-25 12:12:56 +00:00
|
|
|
$this->assertSame(302, $client->getResponse()->getStatusCode());
|
2017-06-13 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTags()
|
|
|
|
{
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$em = $client->getContainer()->get(EntityManagerInterface::class);
|
2017-06-13 16:48:10 +00:00
|
|
|
$user = $em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(User::class)
|
2017-06-13 16:48:10 +00:00
|
|
|
->findOneByUsername('admin');
|
|
|
|
|
|
|
|
$config = $user->getConfig();
|
|
|
|
$config->setFeedToken('SUPERTOKEN');
|
|
|
|
$config->setFeedLimit(null);
|
|
|
|
$em->persist($config);
|
2022-03-10 07:06:55 +00:00
|
|
|
|
|
|
|
$entry1 = $em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(Entry::class)
|
2022-03-10 07:06:55 +00:00
|
|
|
->find(1)
|
|
|
|
;
|
|
|
|
|
|
|
|
$entry4 = $em
|
2022-08-25 19:37:10 +00:00
|
|
|
->getRepository(Entry::class)
|
2022-03-10 07:06:55 +00:00
|
|
|
->find(4)
|
|
|
|
;
|
|
|
|
|
|
|
|
$now = new \DateTimeImmutable('now');
|
|
|
|
|
|
|
|
$day1 = $now->modify('-8 days');
|
|
|
|
$day2 = $now->modify('-6 days');
|
|
|
|
$day3 = $now->modify('-4 days');
|
|
|
|
$day4 = $now->modify('-2 days');
|
|
|
|
|
|
|
|
$entry1->setCreatedAt($day1);
|
|
|
|
$entry4->setCreatedAt($day2);
|
|
|
|
|
|
|
|
$property = (new \ReflectionObject($entry4))->getProperty('updatedAt');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($entry4, $day3);
|
|
|
|
|
2023-07-26 14:01:22 +00:00
|
|
|
// We have to flush and sleep here to be sure that $entry1 and $entry4 have different updatedAt values
|
|
|
|
$em->flush();
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
$property = (new \ReflectionObject($entry1))->getProperty('updatedAt');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($entry1, $day4);
|
|
|
|
|
2017-06-13 16:48:10 +00:00
|
|
|
$em->flush();
|
|
|
|
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2022-03-10 07:06:55 +00:00
|
|
|
// tag foo - without sort
|
2023-01-09 21:18:23 +00:00
|
|
|
$crawler = $client->request('GET', '/feed/admin/SUPERTOKEN/tags/t:foo');
|
2022-03-10 07:06:55 +00:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
|
|
|
$this->assertSame('test title entry4', $crawler->filterXPath('//feed/entry[1]/title')->text());
|
|
|
|
$this->assertSame('test title entry1', $crawler->filterXPath('//feed/entry[2]/title')->text());
|
|
|
|
|
|
|
|
// tag foo - with sort created
|
2023-01-09 21:18:23 +00:00
|
|
|
$crawler = $client->request('GET', '/feed/admin/SUPERTOKEN/tags/t:foo?sort=created');
|
2022-03-10 07:06:55 +00:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
|
|
|
$this->assertSame('test title entry4', $crawler->filterXPath('//feed/entry[1]/title')->text());
|
|
|
|
$this->assertSame('test title entry1', $crawler->filterXPath('//feed/entry[2]/title')->text());
|
|
|
|
|
|
|
|
// tag foo - with sort updated
|
2023-01-09 21:18:23 +00:00
|
|
|
$crawler = $client->request('GET', '/feed/admin/SUPERTOKEN/tags/t:foo?sort=updated');
|
2017-06-13 16:48:10 +00:00
|
|
|
$this->assertSame(200, $client->getResponse()->getStatusCode());
|
2022-03-10 07:06:55 +00:00
|
|
|
$this->assertSame('test title entry1', $crawler->filterXPath('//feed/entry[1]/title')->text());
|
|
|
|
$this->assertSame('test title entry4', $crawler->filterXPath('//feed/entry[2]/title')->text());
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2022-03-10 07:06:55 +00:00
|
|
|
// tag foo - with invalid sort
|
2023-01-09 21:18:23 +00:00
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/tags/t:foo?sort=invalid');
|
2022-03-10 07:06:55 +00:00
|
|
|
$this->assertSame(400, $client->getResponse()->getStatusCode());
|
2017-06-13 16:48:10 +00:00
|
|
|
|
2022-03-10 07:06:55 +00:00
|
|
|
// tag foo/3000
|
2023-01-09 21:18:23 +00:00
|
|
|
$client->request('GET', '/feed/admin/SUPERTOKEN/tags/t:foo/3000');
|
2017-06-13 16:48:10 +00:00
|
|
|
$this->assertSame(302, $client->getResponse()->getStatusCode());
|
|
|
|
}
|
2019-04-26 11:40:58 +00:00
|
|
|
|
|
|
|
public function dataForRedirect()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'/admin/YZIOAUZIAO/unread.xml',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/admin/YZIOAUZIAO/starred.xml',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/admin/YZIOAUZIAO/archive.xml',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/admin/YZIOAUZIAO/all.xml',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'/admin/YZIOAUZIAO/tags/foo.xml',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataForRedirect
|
|
|
|
*/
|
|
|
|
public function testRedirectFromRssToAtom($url)
|
|
|
|
{
|
2022-11-23 16:09:32 +00:00
|
|
|
$client = $this->getTestClient();
|
2019-04-26 11:40:58 +00:00
|
|
|
|
|
|
|
$client->request('GET', $url);
|
|
|
|
|
|
|
|
$this->assertSame(301, $client->getResponse()->getStatusCode());
|
|
|
|
}
|
2017-06-13 16:48:10 +00:00
|
|
|
}
|