2016-03-12 17:54:56 +01:00
|
|
|
<?php
|
|
|
|
|
2018-12-03 00:08:04 +01:00
|
|
|
namespace Shaarli\Feed;
|
|
|
|
|
|
|
|
use DateTime;
|
2020-09-26 14:18:01 +02:00
|
|
|
use malkusch\lock\mutex\NoMutex;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\Bookmark\Bookmark;
|
|
|
|
use Shaarli\Bookmark\BookmarkFileService;
|
2019-01-12 23:55:38 +01:00
|
|
|
use Shaarli\Bookmark\LinkDB;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
|
|
|
use Shaarli\Formatter\FormatterFactory;
|
|
|
|
use Shaarli\History;
|
2021-01-20 15:59:00 +01:00
|
|
|
use Shaarli\Plugin\PluginManager;
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\TestCase;
|
2021-04-05 11:00:28 +02:00
|
|
|
use Shaarli\Tests\Utils\ReferenceLinkDB;
|
2018-12-03 00:08:04 +01:00
|
|
|
|
2016-03-12 17:54:56 +01:00
|
|
|
/**
|
|
|
|
* FeedBuilderTest class.
|
|
|
|
*
|
|
|
|
* Unit tests for FeedBuilder.
|
|
|
|
*/
|
2020-09-03 14:52:34 +02:00
|
|
|
class FeedBuilderTest extends TestCase
|
2016-03-12 17:54:56 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string locale Basque (Spain).
|
|
|
|
*/
|
|
|
|
public static $LOCALE = 'eu_ES';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string language in RSS format.
|
|
|
|
*/
|
|
|
|
public static $RSS_LANGUAGE = 'eu-es';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string language in ATOM format.
|
|
|
|
*/
|
|
|
|
public static $ATOM_LANGUAGUE = 'eu';
|
|
|
|
|
|
|
|
protected static $testDatastore = 'sandbox/datastore.php';
|
|
|
|
|
2020-01-17 21:34:12 +01:00
|
|
|
public static $bookmarkService;
|
|
|
|
|
|
|
|
public static $formatter;
|
2016-03-12 17:54:56 +01:00
|
|
|
|
|
|
|
public static $serverInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called before every test method.
|
|
|
|
*/
|
2020-09-03 14:52:34 +02:00
|
|
|
public static function setUpBeforeClass(): void
|
2016-03-12 17:54:56 +01:00
|
|
|
{
|
2020-09-26 14:18:01 +02:00
|
|
|
$mutex = new NoMutex();
|
2020-01-17 21:34:12 +01:00
|
|
|
$conf = new ConfigManager('tests/utils/config/configJson');
|
|
|
|
$conf->set('resource.datastore', self::$testDatastore);
|
2021-04-05 11:00:28 +02:00
|
|
|
$refLinkDB = new ReferenceLinkDB();
|
2016-03-12 17:54:56 +01:00
|
|
|
$refLinkDB->write(self::$testDatastore);
|
2020-01-17 21:34:12 +01:00
|
|
|
$history = new History('sandbox/history.php');
|
2020-01-18 11:33:23 +01:00
|
|
|
$factory = new FormatterFactory($conf, true);
|
2021-01-20 15:59:00 +01:00
|
|
|
$pluginManager = new PluginManager($conf);
|
2020-01-17 21:34:12 +01:00
|
|
|
self::$formatter = $factory->getFormatter();
|
2021-01-20 15:59:00 +01:00
|
|
|
self::$bookmarkService = new BookmarkFileService(
|
|
|
|
$conf,
|
|
|
|
$pluginManager,
|
|
|
|
$history,
|
|
|
|
$mutex,
|
|
|
|
true
|
|
|
|
);
|
2020-01-17 21:34:12 +01:00
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
self::$serverInfo = [
|
2016-03-12 17:54:56 +01:00
|
|
|
'HTTPS' => 'Off',
|
|
|
|
'SERVER_NAME' => 'host.tld',
|
|
|
|
'SERVER_PORT' => '80',
|
|
|
|
'SCRIPT_NAME' => '/index.php',
|
2020-09-03 14:52:34 +02:00
|
|
|
'REQUEST_URI' => '/feed/atom',
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2016-03-12 17:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test buildData with RSS feed.
|
|
|
|
*/
|
|
|
|
public function testRSSBuildData()
|
|
|
|
{
|
2020-01-17 21:34:12 +01:00
|
|
|
$feedBuilder = new FeedBuilder(
|
|
|
|
self::$bookmarkService,
|
|
|
|
self::$formatter,
|
2020-05-18 13:03:13 +02:00
|
|
|
static::$serverInfo,
|
2020-01-17 21:34:12 +01:00
|
|
|
false
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
$feedBuilder->setLocale(self::$LOCALE);
|
2020-05-18 13:03:13 +02:00
|
|
|
$data = $feedBuilder->buildData(FeedBuilder::$FEED_RSS, null);
|
2016-03-12 17:54:56 +01:00
|
|
|
// Test headers (RSS)
|
|
|
|
$this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
|
2016-08-03 09:45:28 +02:00
|
|
|
$this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals(true, $data['show_dates']);
|
2020-09-03 14:52:34 +02:00
|
|
|
$this->assertEquals('http://host.tld/feed/atom', $data['self_link']);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals('http://host.tld/', $data['index_url']);
|
|
|
|
$this->assertFalse($data['usepermalinks']);
|
|
|
|
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
|
|
|
|
|
2018-05-22 22:44:38 +02:00
|
|
|
// Test first not pinned link (note link)
|
2020-08-29 10:06:40 +02:00
|
|
|
$link = $data['links'][array_keys($data['links'])[0]];
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertEquals(41, $link['id']);
|
2021-04-05 11:00:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'),
|
|
|
|
$link['created']
|
|
|
|
);
|
2020-07-28 22:24:41 +02:00
|
|
|
$this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
|
|
|
|
$this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
|
2016-08-03 09:45:28 +02:00
|
|
|
$this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
|
|
|
|
$pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']);
|
2018-12-03 00:08:04 +01:00
|
|
|
$up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
|
2016-08-03 09:45:28 +02:00
|
|
|
$this->assertEquals($pub, $up);
|
2020-09-29 14:41:40 +02:00
|
|
|
$this->assertContainsPolyfill('Stallman has a beard', $link['description']);
|
|
|
|
$this->assertContainsPolyfill('Permalink', $link['description']);
|
|
|
|
$this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals(1, count($link['taglist']));
|
2016-04-14 17:59:37 +02:00
|
|
|
$this->assertEquals('sTuff', $link['taglist'][0]);
|
2016-03-12 17:54:56 +01:00
|
|
|
|
|
|
|
// Test URL with external link.
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
|
2016-03-12 17:54:56 +01:00
|
|
|
|
|
|
|
// Test multitags.
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertEquals(5, count($data['links'][6]['taglist']));
|
|
|
|
$this->assertEquals('css', $data['links'][6]['taglist'][0]);
|
2016-08-03 09:45:28 +02:00
|
|
|
|
|
|
|
// Test update date
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
|
2016-03-12 17:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test buildData with ATOM feed (test only specific to ATOM).
|
|
|
|
*/
|
|
|
|
public function testAtomBuildData()
|
|
|
|
{
|
2020-01-17 21:34:12 +01:00
|
|
|
$feedBuilder = new FeedBuilder(
|
|
|
|
self::$bookmarkService,
|
|
|
|
self::$formatter,
|
2020-05-18 13:03:13 +02:00
|
|
|
static::$serverInfo,
|
2020-01-17 21:34:12 +01:00
|
|
|
false
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
$feedBuilder->setLocale(self::$LOCALE);
|
2020-05-18 13:03:13 +02:00
|
|
|
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
|
2016-08-03 09:45:28 +02:00
|
|
|
$this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
|
2020-08-29 10:06:40 +02:00
|
|
|
$link = $data['links'][array_keys($data['links'])[0]];
|
2016-08-03 09:45:28 +02:00
|
|
|
$this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
|
2016-03-12 17:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test buildData with search criteria.
|
|
|
|
*/
|
|
|
|
public function testBuildDataFiltered()
|
|
|
|
{
|
2021-04-05 09:39:34 +02:00
|
|
|
$criteria = [
|
2016-03-12 17:54:56 +01:00
|
|
|
'searchtags' => 'stuff',
|
|
|
|
'searchterm' => 'beard',
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2020-01-17 21:34:12 +01:00
|
|
|
$feedBuilder = new FeedBuilder(
|
|
|
|
self::$bookmarkService,
|
|
|
|
self::$formatter,
|
2020-05-18 13:03:13 +02:00
|
|
|
static::$serverInfo,
|
2020-01-17 21:34:12 +01:00
|
|
|
false
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
$feedBuilder->setLocale(self::$LOCALE);
|
2020-05-18 13:03:13 +02:00
|
|
|
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals(1, count($data['links']));
|
|
|
|
$link = array_shift($data['links']);
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertEquals(41, $link['id']);
|
2021-04-05 11:00:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'),
|
|
|
|
$link['created']
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test buildData with nb limit.
|
|
|
|
*/
|
|
|
|
public function testBuildDataCount()
|
|
|
|
{
|
2021-04-05 09:39:34 +02:00
|
|
|
$criteria = [
|
2018-05-22 22:44:38 +02:00
|
|
|
'nb' => '3',
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2020-01-17 21:34:12 +01:00
|
|
|
$feedBuilder = new FeedBuilder(
|
|
|
|
self::$bookmarkService,
|
|
|
|
self::$formatter,
|
2020-05-18 13:03:13 +02:00
|
|
|
static::$serverInfo,
|
2020-01-17 21:34:12 +01:00
|
|
|
false
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
$feedBuilder->setLocale(self::$LOCALE);
|
2020-05-18 13:03:13 +02:00
|
|
|
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
|
2018-05-22 22:44:38 +02:00
|
|
|
$this->assertEquals(3, count($data['links']));
|
2020-08-29 10:06:40 +02:00
|
|
|
$link = $data['links'][array_keys($data['links'])[0]];
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertEquals(41, $link['id']);
|
2021-04-05 11:00:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'),
|
|
|
|
$link['created']
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test buildData with permalinks on.
|
|
|
|
*/
|
|
|
|
public function testBuildDataPermalinks()
|
|
|
|
{
|
2020-01-17 21:34:12 +01:00
|
|
|
$feedBuilder = new FeedBuilder(
|
|
|
|
self::$bookmarkService,
|
|
|
|
self::$formatter,
|
2020-05-18 13:03:13 +02:00
|
|
|
static::$serverInfo,
|
2020-01-17 21:34:12 +01:00
|
|
|
false
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
$feedBuilder->setLocale(self::$LOCALE);
|
|
|
|
$feedBuilder->setUsePermalinks(true);
|
2020-05-18 13:03:13 +02:00
|
|
|
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
|
|
|
|
$this->assertTrue($data['usepermalinks']);
|
|
|
|
// First link is a permalink
|
2020-08-29 10:06:40 +02:00
|
|
|
$link = $data['links'][array_keys($data['links'])[0]];
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertEquals(41, $link['id']);
|
2021-04-05 11:00:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'),
|
|
|
|
$link['created']
|
|
|
|
);
|
2020-07-28 22:24:41 +02:00
|
|
|
$this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
|
|
|
|
$this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
|
2020-09-29 14:41:40 +02:00
|
|
|
$this->assertContainsPolyfill('Direct link', $link['description']);
|
|
|
|
$this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
|
2016-03-12 17:54:56 +01:00
|
|
|
// Second link is a direct link
|
2020-08-29 10:06:40 +02:00
|
|
|
$link = $data['links'][array_keys($data['links'])[1]];
|
2016-11-28 16:17:25 +01:00
|
|
|
$this->assertEquals(8, $link['id']);
|
2021-04-05 11:00:28 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'),
|
|
|
|
$link['created']
|
|
|
|
);
|
2020-07-28 22:24:41 +02:00
|
|
|
$this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
|
2020-09-29 14:41:40 +02:00
|
|
|
$this->assertContainsPolyfill('Direct link', $link['description']);
|
|
|
|
$this->assertContainsPolyfill('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
|
2016-03-12 17:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test buildData with hide dates settings.
|
|
|
|
*/
|
|
|
|
public function testBuildDataHideDates()
|
|
|
|
{
|
2020-01-17 21:34:12 +01:00
|
|
|
$feedBuilder = new FeedBuilder(
|
|
|
|
self::$bookmarkService,
|
|
|
|
self::$formatter,
|
2020-05-18 13:03:13 +02:00
|
|
|
static::$serverInfo,
|
2020-01-17 21:34:12 +01:00
|
|
|
false
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
$feedBuilder->setLocale(self::$LOCALE);
|
|
|
|
$feedBuilder->setHideDates(true);
|
2020-05-18 13:03:13 +02:00
|
|
|
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
|
|
|
|
$this->assertFalse($data['show_dates']);
|
|
|
|
|
|
|
|
// Show dates while logged in
|
2020-01-17 21:34:12 +01:00
|
|
|
$feedBuilder = new FeedBuilder(
|
|
|
|
self::$bookmarkService,
|
|
|
|
self::$formatter,
|
2020-05-18 13:03:13 +02:00
|
|
|
static::$serverInfo,
|
2020-01-17 21:34:12 +01:00
|
|
|
true
|
|
|
|
);
|
2016-03-12 17:54:56 +01:00
|
|
|
$feedBuilder->setLocale(self::$LOCALE);
|
|
|
|
$feedBuilder->setHideDates(true);
|
2020-05-18 13:03:13 +02:00
|
|
|
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
|
2016-03-12 17:54:56 +01:00
|
|
|
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
|
|
|
|
$this->assertTrue($data['show_dates']);
|
|
|
|
}
|
|
|
|
|
2016-10-16 20:52:35 +02:00
|
|
|
/**
|
|
|
|
* Test buildData when Shaarli is served from a subdirectory
|
|
|
|
*/
|
|
|
|
public function testBuildDataServerSubdir()
|
|
|
|
{
|
2021-04-05 09:39:34 +02:00
|
|
|
$serverInfo = [
|
2016-10-16 20:52:35 +02:00
|
|
|
'HTTPS' => 'Off',
|
|
|
|
'SERVER_NAME' => 'host.tld',
|
|
|
|
'SERVER_PORT' => '8080',
|
|
|
|
'SCRIPT_NAME' => '/~user/shaarli/index.php',
|
2020-09-03 14:52:34 +02:00
|
|
|
'REQUEST_URI' => '/~user/shaarli/feed/atom',
|
2021-04-05 09:39:34 +02:00
|
|
|
];
|
2016-10-16 20:52:35 +02:00
|
|
|
$feedBuilder = new FeedBuilder(
|
2020-01-17 21:34:12 +01:00
|
|
|
self::$bookmarkService,
|
|
|
|
self::$formatter,
|
2016-10-16 20:52:35 +02:00
|
|
|
$serverInfo,
|
|
|
|
false
|
|
|
|
);
|
|
|
|
$feedBuilder->setLocale(self::$LOCALE);
|
2020-05-18 13:03:13 +02:00
|
|
|
$data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
|
2016-10-16 20:52:35 +02:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-09-03 14:52:34 +02:00
|
|
|
'http://host.tld:8080/~user/shaarli/feed/atom',
|
2016-10-16 20:52:35 +02:00
|
|
|
$data['self_link']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test first link (note link)
|
2020-08-29 10:06:40 +02:00
|
|
|
$link = $data['links'][array_keys($data['links'])[0]];
|
2020-07-28 22:24:41 +02:00
|
|
|
$this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']);
|
|
|
|
$this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']);
|
2020-09-29 14:41:40 +02:00
|
|
|
$this->assertContainsPolyfill('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);
|
2016-10-16 20:52:35 +02:00
|
|
|
}
|
2016-03-12 17:54:56 +01:00
|
|
|
}
|