From dfc650aa239d3a2c028d0ba13132ce75b4f4c0b4 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Mon, 3 Dec 2018 00:08:04 +0100 Subject: namespacing: \Shaarli\Feed\{Cache,CachedPage,FeedBuilder} Signed-off-by: VirtualTam --- application/Cache.php | 38 ----- application/CachedPage.php | 59 ------- application/FeedBuilder.php | 296 --------------------------------- application/feed/Cache.php | 38 +++++ application/feed/CachedPage.php | 61 +++++++ application/feed/FeedBuilder.php | 299 ++++++++++++++++++++++++++++++++++ composer.json | 1 + index.php | 8 +- plugins/pubsubhubbub/pubsubhubbub.php | 1 + tests/CacheTest.php | 91 ----------- tests/CachedPageTest.php | 121 -------------- tests/FeedBuilderTest.php | 245 ---------------------------- tests/LinkDBTest.php | 4 +- tests/feed/CacheTest.php | 92 +++++++++++ tests/feed/CachedPageTest.php | 120 ++++++++++++++ tests/feed/FeedBuilderTest.php | 250 ++++++++++++++++++++++++++++ 16 files changed, 867 insertions(+), 857 deletions(-) delete mode 100644 application/Cache.php delete mode 100644 application/CachedPage.php delete mode 100644 application/FeedBuilder.php create mode 100644 application/feed/Cache.php create mode 100644 application/feed/CachedPage.php create mode 100644 application/feed/FeedBuilder.php delete mode 100644 tests/CacheTest.php delete mode 100644 tests/CachedPageTest.php delete mode 100644 tests/FeedBuilderTest.php create mode 100644 tests/feed/CacheTest.php create mode 100644 tests/feed/CachedPageTest.php create mode 100644 tests/feed/FeedBuilderTest.php diff --git a/application/Cache.php b/application/Cache.php deleted file mode 100644 index e5d43e61..00000000 --- a/application/Cache.php +++ /dev/null @@ -1,38 +0,0 @@ -cacheDir = $cacheDir; - $this->filename = $this->cacheDir.'/'.sha1($url).'.cache'; - $this->shouldBeCached = $shouldBeCached; - } - - /** - * Returns the cached version of a page, if it exists and should be cached - * - * @return string a cached version of the page if it exists, null otherwise - */ - public function cachedVersion() - { - if (!$this->shouldBeCached) { - return null; - } - if (is_file($this->filename)) { - return file_get_contents($this->filename); - } - return null; - } - - /** - * Puts a page in the cache - * - * @param string $pageContent XML content to cache - */ - public function cache($pageContent) - { - if (!$this->shouldBeCached) { - return; - } - file_put_contents($this->filename, $pageContent); - } -} diff --git a/application/FeedBuilder.php b/application/FeedBuilder.php deleted file mode 100644 index 73fafcbe..00000000 --- a/application/FeedBuilder.php +++ /dev/null @@ -1,296 +0,0 @@ -linkDB = $linkDB; - $this->feedType = $feedType; - $this->serverInfo = $serverInfo; - $this->userInput = $userInput; - $this->isLoggedIn = $isLoggedIn; - } - - /** - * Build data for feed templates. - * - * @return array Formatted data for feeds templates. - */ - public function buildData() - { - // Search for untagged links - if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) { - $this->userInput['searchtags'] = false; - } - - // Optionally filter the results: - $linksToDisplay = $this->linkDB->filterSearch($this->userInput); - - $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); - - // Can't use array_keys() because $link is a LinkDB instance and not a real array. - $keys = array(); - foreach ($linksToDisplay as $key => $value) { - $keys[] = $key; - } - - $pageaddr = escape(index_url($this->serverInfo)); - $linkDisplayed = array(); - for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { - $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr); - } - - $data['language'] = $this->getTypeLanguage(); - $data['last_update'] = $this->getLatestDateFormatted(); - $data['show_dates'] = !$this->hideDates || $this->isLoggedIn; - // Remove leading slash from REQUEST_URI. - $data['self_link'] = escape(server_url($this->serverInfo)) - . escape($this->serverInfo['REQUEST_URI']); - $data['index_url'] = $pageaddr; - $data['usepermalinks'] = $this->usePermalinks === true; - $data['links'] = $linkDisplayed; - - return $data; - } - - /** - * Build a feed item (one per shaare). - * - * @param array $link Single link array extracted from LinkDB. - * @param string $pageaddr Index URL. - * - * @return array Link array with feed attributes. - */ - protected function buildItem($link, $pageaddr) - { - $link['guid'] = $pageaddr .'?'. $link['shorturl']; - // Check for both signs of a note: starting with ? and 7 chars long. - if ($link['url'][0] === '?' && strlen($link['url']) === 7) { - $link['url'] = $pageaddr . $link['url']; - } - if ($this->usePermalinks === true) { - $permalink = ''. t('Direct link') .''; - } else { - $permalink = ''. t('Permalink') .''; - } - $link['description'] = format_description($link['description'], '', false, $pageaddr); - $link['description'] .= PHP_EOL .'
— '. $permalink; - - $pubDate = $link['created']; - $link['pub_iso_date'] = $this->getIsoDate($pubDate); - - // atom:entry elements MUST contain exactly one atom:updated element. - if (!empty($link['updated'])) { - $upDate = $link['updated']; - $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM); - } else { - $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM); - ; - } - - // Save the more recent item. - if (empty($this->latestDate) || $this->latestDate < $pubDate) { - $this->latestDate = $pubDate; - } - if (!empty($upDate) && $this->latestDate < $upDate) { - $this->latestDate = $upDate; - } - - $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); - uasort($taglist, 'strcasecmp'); - $link['taglist'] = $taglist; - - return $link; - } - - /** - * Set this to true to use permalinks instead of direct links. - * - * @param boolean $usePermalinks true to force permalinks. - */ - public function setUsePermalinks($usePermalinks) - { - $this->usePermalinks = $usePermalinks; - } - - /** - * Set this to true to hide timestamps in feeds. - * - * @param boolean $hideDates true to enable. - */ - public function setHideDates($hideDates) - { - $this->hideDates = $hideDates; - } - - /** - * Set the locale. Used to show feed language. - * - * @param string $locale The locale (eg. 'fr_FR.UTF8'). - */ - public function setLocale($locale) - { - $this->locale = strtolower($locale); - } - - /** - * Get the language according to the feed type, based on the locale: - * - * - RSS format: en-us (default: 'en-en'). - * - ATOM format: fr (default: 'en'). - * - * @return string The language. - */ - public function getTypeLanguage() - { - // Use the locale do define the language, if available. - if (! empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) { - $length = ($this->feedType == self::$FEED_RSS) ? 5 : 2; - return str_replace('_', '-', substr($this->locale, 0, $length)); - } - return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en'; - } - - /** - * Format the latest item date found according to the feed type. - * - * Return an empty string if invalid DateTime is passed. - * - * @return string Formatted date. - */ - protected function getLatestDateFormatted() - { - if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) { - return ''; - } - - $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; - return $this->latestDate->format($type); - } - - /** - * Get ISO date from DateTime according to feed type. - * - * @param DateTime $date Date to format. - * @param string|bool $format Force format. - * - * @return string Formatted date. - */ - protected function getIsoDate(DateTime $date, $format = false) - { - if ($format !== false) { - return $date->format($format); - } - if ($this->feedType == self::$FEED_RSS) { - return $date->format(DateTime::RSS); - } - return $date->format(DateTime::ATOM); - } - - /** - * Returns the number of link to display according to 'nb' user input parameter. - * - * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. - * If 'nb' is set to 'all', display all filtered links (max parameter). - * - * @param int $max maximum number of links to display. - * - * @return int number of links to display. - */ - public function getNbLinks($max) - { - if (empty($this->userInput['nb'])) { - return self::$DEFAULT_NB_LINKS; - } - - if ($this->userInput['nb'] == 'all') { - return $max; - } - - $intNb = intval($this->userInput['nb']); - if (! is_int($intNb) || $intNb == 0) { - return self::$DEFAULT_NB_LINKS; - } - - return $intNb; - } -} diff --git a/application/feed/Cache.php b/application/feed/Cache.php new file mode 100644 index 00000000..e5d43e61 --- /dev/null +++ b/application/feed/Cache.php @@ -0,0 +1,38 @@ +cacheDir = $cacheDir; + $this->filename = $this->cacheDir . '/' . sha1($url) . '.cache'; + $this->shouldBeCached = $shouldBeCached; + } + + /** + * Returns the cached version of a page, if it exists and should be cached + * + * @return string a cached version of the page if it exists, null otherwise + */ + public function cachedVersion() + { + if (!$this->shouldBeCached) { + return null; + } + if (is_file($this->filename)) { + return file_get_contents($this->filename); + } + return null; + } + + /** + * Puts a page in the cache + * + * @param string $pageContent XML content to cache + */ + public function cache($pageContent) + { + if (!$this->shouldBeCached) { + return; + } + file_put_contents($this->filename, $pageContent); + } +} diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php new file mode 100644 index 00000000..dcfd2c89 --- /dev/null +++ b/application/feed/FeedBuilder.php @@ -0,0 +1,299 @@ +linkDB = $linkDB; + $this->feedType = $feedType; + $this->serverInfo = $serverInfo; + $this->userInput = $userInput; + $this->isLoggedIn = $isLoggedIn; + } + + /** + * Build data for feed templates. + * + * @return array Formatted data for feeds templates. + */ + public function buildData() + { + // Search for untagged links + if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) { + $this->userInput['searchtags'] = false; + } + + // Optionally filter the results: + $linksToDisplay = $this->linkDB->filterSearch($this->userInput); + + $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay)); + + // Can't use array_keys() because $link is a LinkDB instance and not a real array. + $keys = array(); + foreach ($linksToDisplay as $key => $value) { + $keys[] = $key; + } + + $pageaddr = escape(index_url($this->serverInfo)); + $linkDisplayed = array(); + for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) { + $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr); + } + + $data['language'] = $this->getTypeLanguage(); + $data['last_update'] = $this->getLatestDateFormatted(); + $data['show_dates'] = !$this->hideDates || $this->isLoggedIn; + // Remove leading slash from REQUEST_URI. + $data['self_link'] = escape(server_url($this->serverInfo)) + . escape($this->serverInfo['REQUEST_URI']); + $data['index_url'] = $pageaddr; + $data['usepermalinks'] = $this->usePermalinks === true; + $data['links'] = $linkDisplayed; + + return $data; + } + + /** + * Build a feed item (one per shaare). + * + * @param array $link Single link array extracted from LinkDB. + * @param string $pageaddr Index URL. + * + * @return array Link array with feed attributes. + */ + protected function buildItem($link, $pageaddr) + { + $link['guid'] = $pageaddr . '?' . $link['shorturl']; + // Check for both signs of a note: starting with ? and 7 chars long. + if ($link['url'][0] === '?' && strlen($link['url']) === 7) { + $link['url'] = $pageaddr . $link['url']; + } + if ($this->usePermalinks === true) { + $permalink = '' . t('Direct link') . ''; + } else { + $permalink = '' . t('Permalink') . ''; + } + $link['description'] = format_description($link['description'], '', false, $pageaddr); + $link['description'] .= PHP_EOL . '
— ' . $permalink; + + $pubDate = $link['created']; + $link['pub_iso_date'] = $this->getIsoDate($pubDate); + + // atom:entry elements MUST contain exactly one atom:updated element. + if (!empty($link['updated'])) { + $upDate = $link['updated']; + $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM); + } else { + $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM); + } + + // Save the more recent item. + if (empty($this->latestDate) || $this->latestDate < $pubDate) { + $this->latestDate = $pubDate; + } + if (!empty($upDate) && $this->latestDate < $upDate) { + $this->latestDate = $upDate; + } + + $taglist = array_filter(explode(' ', $link['tags']), 'strlen'); + uasort($taglist, 'strcasecmp'); + $link['taglist'] = $taglist; + + return $link; + } + + /** + * Set this to true to use permalinks instead of direct links. + * + * @param boolean $usePermalinks true to force permalinks. + */ + public function setUsePermalinks($usePermalinks) + { + $this->usePermalinks = $usePermalinks; + } + + /** + * Set this to true to hide timestamps in feeds. + * + * @param boolean $hideDates true to enable. + */ + public function setHideDates($hideDates) + { + $this->hideDates = $hideDates; + } + + /** + * Set the locale. Used to show feed language. + * + * @param string $locale The locale (eg. 'fr_FR.UTF8'). + */ + public function setLocale($locale) + { + $this->locale = strtolower($locale); + } + + /** + * Get the language according to the feed type, based on the locale: + * + * - RSS format: en-us (default: 'en-en'). + * - ATOM format: fr (default: 'en'). + * + * @return string The language. + */ + public function getTypeLanguage() + { + // Use the locale do define the language, if available. + if (!empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) { + $length = ($this->feedType === self::$FEED_RSS) ? 5 : 2; + return str_replace('_', '-', substr($this->locale, 0, $length)); + } + return ($this->feedType === self::$FEED_RSS) ? 'en-en' : 'en'; + } + + /** + * Format the latest item date found according to the feed type. + * + * Return an empty string if invalid DateTime is passed. + * + * @return string Formatted date. + */ + protected function getLatestDateFormatted() + { + if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) { + return ''; + } + + $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM; + return $this->latestDate->format($type); + } + + /** + * Get ISO date from DateTime according to feed type. + * + * @param DateTime $date Date to format. + * @param string|bool $format Force format. + * + * @return string Formatted date. + */ + protected function getIsoDate(DateTime $date, $format = false) + { + if ($format !== false) { + return $date->format($format); + } + if ($this->feedType == self::$FEED_RSS) { + return $date->format(DateTime::RSS); + } + return $date->format(DateTime::ATOM); + } + + /** + * Returns the number of link to display according to 'nb' user input parameter. + * + * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS. + * If 'nb' is set to 'all', display all filtered links (max parameter). + * + * @param int $max maximum number of links to display. + * + * @return int number of links to display. + */ + public function getNbLinks($max) + { + if (empty($this->userInput['nb'])) { + return self::$DEFAULT_NB_LINKS; + } + + if ($this->userInput['nb'] == 'all') { + return $max; + } + + $intNb = intval($this->userInput['nb']); + if (!is_int($intNb) || $intNb == 0) { + return self::$DEFAULT_NB_LINKS; + } + + return $intNb; + } +} diff --git a/composer.json b/composer.json index 027203f4..7c9cbf3d 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "Shaarli\\Config\\": "application/config/", "Shaarli\\Config\\Exception\\": "application/config/exception", "Shaarli\\Exceptions\\": "application/exceptions", + "Shaarli\\Feed\\": "application/feed", "Shaarli\\Security\\": "application/security" } } diff --git a/index.php b/index.php index cc41d80c..6d1ae3fc 100644 --- a/index.php +++ b/index.php @@ -57,10 +57,8 @@ require_once __DIR__ . '/vendor/autoload.php'; // Shaarli library require_once 'application/ApplicationUtils.php'; -require_once 'application/Cache.php'; -require_once 'application/CachedPage.php'; require_once 'application/config/ConfigPlugin.php'; -require_once 'application/FeedBuilder.php'; +require_once 'application/feed/Cache.php'; require_once 'application/FileUtils.php'; require_once 'application/History.php'; require_once 'application/HttpUtils.php'; @@ -76,7 +74,9 @@ require_once 'application/PluginManager.php'; require_once 'application/Router.php'; require_once 'application/Updater.php'; use \Shaarli\Config\ConfigManager; -use Shaarli\History; +use \Shaarli\Feed\CachedPage; +use \Shaarli\Feed\FeedBuilder; +use \Shaarli\History; use \Shaarli\Languages; use \Shaarli\Security\LoginManager; use \Shaarli\Security\SessionManager; diff --git a/plugins/pubsubhubbub/pubsubhubbub.php b/plugins/pubsubhubbub/pubsubhubbub.php index 9f0342a3..1872af8a 100644 --- a/plugins/pubsubhubbub/pubsubhubbub.php +++ b/plugins/pubsubhubbub/pubsubhubbub.php @@ -11,6 +11,7 @@ use pubsubhubbub\publisher\Publisher; use Shaarli\Config\ConfigManager; +use Shaarli\Feed\FeedBuilder; /** * Plugin init function - set the hub to the default appspot one. diff --git a/tests/CacheTest.php b/tests/CacheTest.php deleted file mode 100644 index f60fad91..00000000 --- a/tests/CacheTest.php +++ /dev/null @@ -1,91 +0,0 @@ -assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); - } - - $this->assertFileExists(self::$testCacheDir.'/intru.der'); - } - - /** - * Purge cached pages - missing directory - */ - public function testPurgeCachedPagesMissingDir() - { - $oldlog = ini_get('error_log'); - ini_set('error_log', '/dev/null'); - $this->assertEquals( - 'Cannot purge sandbox/dummycache_missing: no directory', - purgeCachedPages(self::$testCacheDir.'_missing') - ); - ini_set('error_log', $oldlog); - } - - /** - * Purge cached pages and session cache - */ - public function testInvalidateCaches() - { - $this->assertArrayNotHasKey('tags', $_SESSION); - $_SESSION['tags'] = array('goodbye', 'cruel', 'world'); - - invalidateCaches(self::$testCacheDir); - foreach (self::$pages as $page) { - $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache'); - } - - $this->assertArrayNotHasKey('tags', $_SESSION); - } -} diff --git a/tests/CachedPageTest.php b/tests/CachedPageTest.php deleted file mode 100644 index 51565cd6..00000000 --- a/tests/CachedPageTest.php +++ /dev/null @@ -1,121 +0,0 @@ -assertFileNotExists(self::$filename); - $page->cache('

Some content

'); - $this->assertFileExists(self::$filename); - $this->assertEquals( - '

Some content

', - file_get_contents(self::$filename) - ); - } - - /** - * "Cache" a page's content - the page is not to be cached - */ - public function testShouldNotCache() - { - $page = new CachedPage(self::$testCacheDir, self::$url, false); - - $this->assertFileNotExists(self::$filename); - $page->cache('

Some content

'); - $this->assertFileNotExists(self::$filename); - } - - /** - * Return a page's cached content - */ - public function testCachedVersion() - { - $page = new CachedPage(self::$testCacheDir, self::$url, true); - - $this->assertFileNotExists(self::$filename); - $page->cache('

Some content

'); - $this->assertFileExists(self::$filename); - $this->assertEquals( - '

Some content

', - $page->cachedVersion() - ); - } - - /** - * Return a page's cached content - the file does not exist - */ - public function testCachedVersionNoFile() - { - $page = new CachedPage(self::$testCacheDir, self::$url, true); - - $this->assertFileNotExists(self::$filename); - $this->assertEquals( - null, - $page->cachedVersion() - ); - } - - /** - * Return a page's cached content - the page is not to be cached - */ - public function testNoCachedVersion() - { - $page = new CachedPage(self::$testCacheDir, self::$url, false); - - $this->assertFileNotExists(self::$filename); - $this->assertEquals( - null, - $page->cachedVersion() - ); - } -} diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php deleted file mode 100644 index 4ca58e5a..00000000 --- a/tests/FeedBuilderTest.php +++ /dev/null @@ -1,245 +0,0 @@ -write(self::$testDatastore); - self::$linkDB = new LinkDB(self::$testDatastore, true, false); - self::$serverInfo = array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '80', - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/index.php?do=feed', - ); - } - - /** - * Test GetTypeLanguage(). - */ - public function testGetTypeLanguage() - { - $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false); - $feedBuilder->setLocale(self::$LOCALE); - $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage()); - $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false); - $feedBuilder->setLocale(self::$LOCALE); - $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage()); - $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false); - $this->assertEquals('en', $feedBuilder->getTypeLanguage()); - $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false); - $this->assertEquals('en-en', $feedBuilder->getTypeLanguage()); - } - - /** - * Test buildData with RSS feed. - */ - public function testRSSBuildData() - { - $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false); - $feedBuilder->setLocale(self::$LOCALE); - $data = $feedBuilder->buildData(); - // Test headers (RSS) - $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); - $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); - $this->assertEquals(true, $data['show_dates']); - $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); - $this->assertEquals('http://host.tld/', $data['index_url']); - $this->assertFalse($data['usepermalinks']); - $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); - - // Test first not pinned link (note link) - $link = $data['links'][array_keys($data['links'])[2]]; - $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); - $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); - $this->assertEquals('http://host.tld/?WDWyig', $link['url']); - $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); - $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']); - $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']); - $this->assertEquals($pub, $up); - $this->assertContains('Stallman has a beard', $link['description']); - $this->assertContains('Permalink', $link['description']); - $this->assertContains('http://host.tld/?WDWyig', $link['description']); - $this->assertEquals(1, count($link['taglist'])); - $this->assertEquals('sTuff', $link['taglist'][0]); - - // Test URL with external link. - $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']); - - // Test multitags. - $this->assertEquals(5, count($data['links'][6]['taglist'])); - $this->assertEquals('css', $data['links'][6]['taglist'][0]); - - // Test update date - $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); - } - - /** - * Test buildData with ATOM feed (test only specific to ATOM). - */ - public function testAtomBuildData() - { - $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); - $feedBuilder->setLocale(self::$LOCALE); - $data = $feedBuilder->buildData(); - $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); - $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); - $link = $data['links'][array_keys($data['links'])[2]]; - $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); - $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); - } - - /** - * Test buildData with search criteria. - */ - public function testBuildDataFiltered() - { - $criteria = array( - 'searchtags' => 'stuff', - 'searchterm' => 'beard', - ); - $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); - $feedBuilder->setLocale(self::$LOCALE); - $data = $feedBuilder->buildData(); - $this->assertEquals(1, count($data['links'])); - $link = array_shift($data['links']); - $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); - } - - /** - * Test buildData with nb limit. - */ - public function testBuildDataCount() - { - $criteria = array( - 'nb' => '3', - ); - $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); - $feedBuilder->setLocale(self::$LOCALE); - $data = $feedBuilder->buildData(); - $this->assertEquals(3, count($data['links'])); - $link = $data['links'][array_keys($data['links'])[2]]; - $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); - } - - /** - * Test buildData with permalinks on. - */ - public function testBuildDataPermalinks() - { - $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); - $feedBuilder->setLocale(self::$LOCALE); - $feedBuilder->setUsePermalinks(true); - $data = $feedBuilder->buildData(); - $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); - $this->assertTrue($data['usepermalinks']); - // First link is a permalink - $link = $data['links'][array_keys($data['links'])[2]]; - $this->assertEquals(41, $link['id']); - $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); - $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); - $this->assertEquals('http://host.tld/?WDWyig', $link['url']); - $this->assertContains('Direct link', $link['description']); - $this->assertContains('http://host.tld/?WDWyig', $link['description']); - // Second link is a direct link - $link = $data['links'][array_keys($data['links'])[3]]; - $this->assertEquals(8, $link['id']); - $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); - $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); - $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); - $this->assertContains('Direct link', $link['description']); - $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); - } - - /** - * Test buildData with hide dates settings. - */ - public function testBuildDataHideDates() - { - $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); - $feedBuilder->setLocale(self::$LOCALE); - $feedBuilder->setHideDates(true); - $data = $feedBuilder->buildData(); - $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); - $this->assertFalse($data['show_dates']); - - // Show dates while logged in - $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true); - $feedBuilder->setLocale(self::$LOCALE); - $feedBuilder->setHideDates(true); - $data = $feedBuilder->buildData(); - $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); - $this->assertTrue($data['show_dates']); - } - - /** - * Test buildData when Shaarli is served from a subdirectory - */ - public function testBuildDataServerSubdir() - { - $serverInfo = array( - 'HTTPS' => 'Off', - 'SERVER_NAME' => 'host.tld', - 'SERVER_PORT' => '8080', - 'SCRIPT_NAME' => '/~user/shaarli/index.php', - 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed', - ); - $feedBuilder = new FeedBuilder( - self::$linkDB, - FeedBuilder::$FEED_ATOM, - $serverInfo, - null, - false - ); - $feedBuilder->setLocale(self::$LOCALE); - $data = $feedBuilder->buildData(); - - $this->assertEquals( - 'http://host.tld:8080/~user/shaarli/index.php?do=feed', - $data['self_link'] - ); - - // Test first link (note link) - $link = $data['links'][array_keys($data['links'])[2]]; - $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); - $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); - $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']); - } -} diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 9b2f35e6..737a2247 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php @@ -3,9 +3,7 @@ * Link datastore tests */ -use Shaarli\Exceptions\IOException; - -require_once 'application/Cache.php'; +require_once 'application/feed/Cache.php'; require_once 'application/FileUtils.php'; require_once 'application/LinkDB.php'; require_once 'application/Utils.php'; diff --git a/tests/feed/CacheTest.php b/tests/feed/CacheTest.php new file mode 100644 index 00000000..c0a9f26f --- /dev/null +++ b/tests/feed/CacheTest.php @@ -0,0 +1,92 @@ +assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); + } + + $this->assertFileExists(self::$testCacheDir . '/intru.der'); + } + + /** + * Purge cached pages - missing directory + */ + public function testPurgeCachedPagesMissingDir() + { + $oldlog = ini_get('error_log'); + ini_set('error_log', '/dev/null'); + $this->assertEquals( + 'Cannot purge sandbox/dummycache_missing: no directory', + purgeCachedPages(self::$testCacheDir . '_missing') + ); + ini_set('error_log', $oldlog); + } + + /** + * Purge cached pages and session cache + */ + public function testInvalidateCaches() + { + $this->assertArrayNotHasKey('tags', $_SESSION); + $_SESSION['tags'] = array('goodbye', 'cruel', 'world'); + + invalidateCaches(self::$testCacheDir); + foreach (self::$pages as $page) { + $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache'); + } + + $this->assertArrayNotHasKey('tags', $_SESSION); + } +} diff --git a/tests/feed/CachedPageTest.php b/tests/feed/CachedPageTest.php new file mode 100644 index 00000000..0bcc1442 --- /dev/null +++ b/tests/feed/CachedPageTest.php @@ -0,0 +1,120 @@ +assertFileNotExists(self::$filename); + $page->cache('

Some content

'); + $this->assertFileExists(self::$filename); + $this->assertEquals( + '

Some content

', + file_get_contents(self::$filename) + ); + } + + /** + * "Cache" a page's content - the page is not to be cached + */ + public function testShouldNotCache() + { + $page = new CachedPage(self::$testCacheDir, self::$url, false); + + $this->assertFileNotExists(self::$filename); + $page->cache('

Some content

'); + $this->assertFileNotExists(self::$filename); + } + + /** + * Return a page's cached content + */ + public function testCachedVersion() + { + $page = new CachedPage(self::$testCacheDir, self::$url, true); + + $this->assertFileNotExists(self::$filename); + $page->cache('

Some content

'); + $this->assertFileExists(self::$filename); + $this->assertEquals( + '

Some content

', + $page->cachedVersion() + ); + } + + /** + * Return a page's cached content - the file does not exist + */ + public function testCachedVersionNoFile() + { + $page = new CachedPage(self::$testCacheDir, self::$url, true); + + $this->assertFileNotExists(self::$filename); + $this->assertEquals( + null, + $page->cachedVersion() + ); + } + + /** + * Return a page's cached content - the page is not to be cached + */ + public function testNoCachedVersion() + { + $page = new CachedPage(self::$testCacheDir, self::$url, false); + + $this->assertFileNotExists(self::$filename); + $this->assertEquals( + null, + $page->cachedVersion() + ); + } +} diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php new file mode 100644 index 00000000..1fdbc60e --- /dev/null +++ b/tests/feed/FeedBuilderTest.php @@ -0,0 +1,250 @@ +write(self::$testDatastore); + self::$linkDB = new LinkDB(self::$testDatastore, true, false); + self::$serverInfo = array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '80', + 'SCRIPT_NAME' => '/index.php', + 'REQUEST_URI' => '/index.php?do=feed', + ); + } + + /** + * Test GetTypeLanguage(). + */ + public function testGetTypeLanguage() + { + $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage()); + $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage()); + $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false); + $this->assertEquals('en', $feedBuilder->getTypeLanguage()); + $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false); + $this->assertEquals('en-en', $feedBuilder->getTypeLanguage()); + } + + /** + * Test buildData with RSS feed. + */ + public function testRSSBuildData() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + // Test headers (RSS) + $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); + $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); + $this->assertEquals(true, $data['show_dates']); + $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); + $this->assertEquals('http://host.tld/', $data['index_url']); + $this->assertFalse($data['usepermalinks']); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + + // Test first not pinned link (note link) + $link = $data['links'][array_keys($data['links'])[2]]; + $this->assertEquals(41, $link['id']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); + $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); + $this->assertEquals('http://host.tld/?WDWyig', $link['url']); + $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); + $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']); + $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']); + $this->assertEquals($pub, $up); + $this->assertContains('Stallman has a beard', $link['description']); + $this->assertContains('Permalink', $link['description']); + $this->assertContains('http://host.tld/?WDWyig', $link['description']); + $this->assertEquals(1, count($link['taglist'])); + $this->assertEquals('sTuff', $link['taglist'][0]); + + // Test URL with external link. + $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']); + + // Test multitags. + $this->assertEquals(5, count($data['links'][6]['taglist'])); + $this->assertEquals('css', $data['links'][6]['taglist'][0]); + + // Test update date + $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); + } + + /** + * Test buildData with ATOM feed (test only specific to ATOM). + */ + public function testAtomBuildData() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); + $link = $data['links'][array_keys($data['links'])[2]]; + $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); + $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); + } + + /** + * Test buildData with search criteria. + */ + public function testBuildDataFiltered() + { + $criteria = array( + 'searchtags' => 'stuff', + 'searchterm' => 'beard', + ); + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + $this->assertEquals(1, count($data['links'])); + $link = array_shift($data['links']); + $this->assertEquals(41, $link['id']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); + } + + /** + * Test buildData with nb limit. + */ + public function testBuildDataCount() + { + $criteria = array( + 'nb' => '3', + ); + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + $this->assertEquals(3, count($data['links'])); + $link = $data['links'][array_keys($data['links'])[2]]; + $this->assertEquals(41, $link['id']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); + } + + /** + * Test buildData with permalinks on. + */ + public function testBuildDataPermalinks() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $feedBuilder->setUsePermalinks(true); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertTrue($data['usepermalinks']); + // First link is a permalink + $link = $data['links'][array_keys($data['links'])[2]]; + $this->assertEquals(41, $link['id']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); + $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); + $this->assertEquals('http://host.tld/?WDWyig', $link['url']); + $this->assertContains('Direct link', $link['description']); + $this->assertContains('http://host.tld/?WDWyig', $link['description']); + // Second link is a direct link + $link = $data['links'][array_keys($data['links'])[3]]; + $this->assertEquals(8, $link['id']); + $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); + $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); + $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); + $this->assertContains('Direct link', $link['description']); + $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); + } + + /** + * Test buildData with hide dates settings. + */ + public function testBuildDataHideDates() + { + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); + $feedBuilder->setLocale(self::$LOCALE); + $feedBuilder->setHideDates(true); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertFalse($data['show_dates']); + + // Show dates while logged in + $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true); + $feedBuilder->setLocale(self::$LOCALE); + $feedBuilder->setHideDates(true); + $data = $feedBuilder->buildData(); + $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); + $this->assertTrue($data['show_dates']); + } + + /** + * Test buildData when Shaarli is served from a subdirectory + */ + public function testBuildDataServerSubdir() + { + $serverInfo = array( + 'HTTPS' => 'Off', + 'SERVER_NAME' => 'host.tld', + 'SERVER_PORT' => '8080', + 'SCRIPT_NAME' => '/~user/shaarli/index.php', + 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed', + ); + $feedBuilder = new FeedBuilder( + self::$linkDB, + FeedBuilder::$FEED_ATOM, + $serverInfo, + null, + false + ); + $feedBuilder->setLocale(self::$LOCALE); + $data = $feedBuilder->buildData(); + + $this->assertEquals( + 'http://host.tld:8080/~user/shaarli/index.php?do=feed', + $data['self_link'] + ); + + // Test first link (note link) + $link = $data['links'][array_keys($data['links'])[2]]; + $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); + $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); + $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']); + } +} -- cgit v1.2.3