FeedBuilderTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. require_once 'application/FeedBuilder.php';
  3. require_once 'application/LinkDB.php';
  4. /**
  5. * FeedBuilderTest class.
  6. *
  7. * Unit tests for FeedBuilder.
  8. */
  9. class FeedBuilderTest extends PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @var string locale Basque (Spain).
  13. */
  14. public static $LOCALE = 'eu_ES';
  15. /**
  16. * @var string language in RSS format.
  17. */
  18. public static $RSS_LANGUAGE = 'eu-es';
  19. /**
  20. * @var string language in ATOM format.
  21. */
  22. public static $ATOM_LANGUAGUE = 'eu';
  23. protected static $testDatastore = 'sandbox/datastore.php';
  24. public static $linkDB;
  25. public static $serverInfo;
  26. /**
  27. * Called before every test method.
  28. */
  29. public static function setUpBeforeClass()
  30. {
  31. $refLinkDB = new ReferenceLinkDB();
  32. $refLinkDB->write(self::$testDatastore);
  33. self::$linkDB = new LinkDB(self::$testDatastore, true, false);
  34. self::$serverInfo = array(
  35. 'HTTPS' => 'Off',
  36. 'SERVER_NAME' => 'host.tld',
  37. 'SERVER_PORT' => '80',
  38. 'SCRIPT_NAME' => '/index.php',
  39. 'REQUEST_URI' => '/index.php?do=feed',
  40. );
  41. }
  42. /**
  43. * Test GetTypeLanguage().
  44. */
  45. public function testGetTypeLanguage()
  46. {
  47. $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
  48. $feedBuilder->setLocale(self::$LOCALE);
  49. $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage());
  50. $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
  51. $feedBuilder->setLocale(self::$LOCALE);
  52. $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage());
  53. $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
  54. $this->assertEquals('en', $feedBuilder->getTypeLanguage());
  55. $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
  56. $this->assertEquals('en-en', $feedBuilder->getTypeLanguage());
  57. }
  58. /**
  59. * Test buildData with RSS feed.
  60. */
  61. public function testRSSBuildData()
  62. {
  63. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false);
  64. $feedBuilder->setLocale(self::$LOCALE);
  65. $data = $feedBuilder->buildData();
  66. // Test headers (RSS)
  67. $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
  68. $this->assertEmpty($data['pubsubhub_url']);
  69. $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
  70. $this->assertEquals(true, $data['show_dates']);
  71. $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
  72. $this->assertEquals('http://host.tld/', $data['index_url']);
  73. $this->assertFalse($data['usepermalinks']);
  74. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  75. // Test first link (note link)
  76. $link = array_shift($data['links']);
  77. $this->assertEquals('20150310_114651', $link['linkdate']);
  78. $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
  79. $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
  80. $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
  81. $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']);
  82. $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
  83. $this->assertEquals($pub, $up);
  84. $this->assertContains('Stallman has a beard', $link['description']);
  85. $this->assertContains('Permalink', $link['description']);
  86. $this->assertContains('http://host.tld/?WDWyig', $link['description']);
  87. $this->assertEquals(1, count($link['taglist']));
  88. $this->assertEquals('sTuff', $link['taglist'][0]);
  89. // Test URL with external link.
  90. $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links']['20150310_114633']['url']);
  91. // Test multitags.
  92. $this->assertEquals(5, count($data['links']['20141125_084734']['taglist']));
  93. $this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]);
  94. // Test update date
  95. $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']);
  96. }
  97. /**
  98. * Test buildData with ATOM feed (test only specific to ATOM).
  99. */
  100. public function testAtomBuildData()
  101. {
  102. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  103. $feedBuilder->setLocale(self::$LOCALE);
  104. $data = $feedBuilder->buildData();
  105. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  106. $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
  107. $link = array_shift($data['links']);
  108. $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
  109. $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']);
  110. }
  111. /**
  112. * Test buildData with search criteria.
  113. */
  114. public function testBuildDataFiltered()
  115. {
  116. $criteria = array(
  117. 'searchtags' => 'stuff',
  118. 'searchterm' => 'beard',
  119. );
  120. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
  121. $feedBuilder->setLocale(self::$LOCALE);
  122. $data = $feedBuilder->buildData();
  123. $this->assertEquals(1, count($data['links']));
  124. $link = array_shift($data['links']);
  125. $this->assertEquals('20150310_114651', $link['linkdate']);
  126. }
  127. /**
  128. * Test buildData with nb limit.
  129. */
  130. public function testBuildDataCount()
  131. {
  132. $criteria = array(
  133. 'nb' => '1',
  134. );
  135. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
  136. $feedBuilder->setLocale(self::$LOCALE);
  137. $data = $feedBuilder->buildData();
  138. $this->assertEquals(1, count($data['links']));
  139. $link = array_shift($data['links']);
  140. $this->assertEquals('20150310_114651', $link['linkdate']);
  141. }
  142. /**
  143. * Test buildData with permalinks on.
  144. */
  145. public function testBuildDataPermalinks()
  146. {
  147. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  148. $feedBuilder->setLocale(self::$LOCALE);
  149. $feedBuilder->setUsePermalinks(true);
  150. $data = $feedBuilder->buildData();
  151. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  152. $this->assertTrue($data['usepermalinks']);
  153. // First link is a permalink
  154. $link = array_shift($data['links']);
  155. $this->assertEquals('20150310_114651', $link['linkdate']);
  156. $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
  157. $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
  158. $this->assertContains('Direct link', $link['description']);
  159. $this->assertContains('http://host.tld/?WDWyig', $link['description']);
  160. // Second link is a direct link
  161. $link = array_shift($data['links']);
  162. $this->assertEquals('20150310_114633', $link['linkdate']);
  163. $this->assertEquals('http://host.tld/?kLHmZg', $link['guid']);
  164. $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
  165. $this->assertContains('Direct link', $link['description']);
  166. $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
  167. }
  168. /**
  169. * Test buildData with hide dates settings.
  170. */
  171. public function testBuildDataHideDates()
  172. {
  173. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  174. $feedBuilder->setLocale(self::$LOCALE);
  175. $feedBuilder->setHideDates(true);
  176. $data = $feedBuilder->buildData();
  177. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  178. $this->assertFalse($data['show_dates']);
  179. // Show dates while logged in
  180. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
  181. $feedBuilder->setLocale(self::$LOCALE);
  182. $feedBuilder->setHideDates(true);
  183. $data = $feedBuilder->buildData();
  184. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  185. $this->assertTrue($data['show_dates']);
  186. }
  187. /**
  188. * Test buildData with hide dates settings.
  189. */
  190. public function testBuildDataPubsubhub()
  191. {
  192. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  193. $feedBuilder->setLocale(self::$LOCALE);
  194. $feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
  195. $data = $feedBuilder->buildData();
  196. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  197. $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
  198. }
  199. /**
  200. * Test buildData when Shaarli is served from a subdirectory
  201. */
  202. public function testBuildDataServerSubdir()
  203. {
  204. $serverInfo = array(
  205. 'HTTPS' => 'Off',
  206. 'SERVER_NAME' => 'host.tld',
  207. 'SERVER_PORT' => '8080',
  208. 'SCRIPT_NAME' => '/~user/shaarli/index.php',
  209. 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
  210. );
  211. $feedBuilder = new FeedBuilder(
  212. self::$linkDB,
  213. FeedBuilder::$FEED_ATOM,
  214. $serverInfo,
  215. null,
  216. false
  217. );
  218. $feedBuilder->setLocale(self::$LOCALE);
  219. $data = $feedBuilder->buildData();
  220. $this->assertEquals(
  221. 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
  222. $data['self_link']
  223. );
  224. // Test first link (note link)
  225. $link = array_shift($data['links']);
  226. $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
  227. $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
  228. }
  229. }