FeedBuilderTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
  69. $this->assertEquals(true, $data['show_dates']);
  70. $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
  71. $this->assertEquals('http://host.tld/', $data['index_url']);
  72. $this->assertFalse($data['usepermalinks']);
  73. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  74. // Test first not pinned link (note link)
  75. $link = $data['links'][array_keys($data['links'])[2]];
  76. $this->assertEquals(41, $link['id']);
  77. $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
  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'][8]['url']);
  91. // Test multitags.
  92. $this->assertEquals(5, count($data['links'][6]['taglist']));
  93. $this->assertEquals('css', $data['links'][6]['taglist'][0]);
  94. // Test update date
  95. $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['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 = $data['links'][array_keys($data['links'])[2]];
  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'][8]['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(41, $link['id']);
  126. $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
  127. }
  128. /**
  129. * Test buildData with nb limit.
  130. */
  131. public function testBuildDataCount()
  132. {
  133. $criteria = array(
  134. 'nb' => '3',
  135. );
  136. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
  137. $feedBuilder->setLocale(self::$LOCALE);
  138. $data = $feedBuilder->buildData();
  139. $this->assertEquals(3, count($data['links']));
  140. $link = $data['links'][array_keys($data['links'])[2]];
  141. $this->assertEquals(41, $link['id']);
  142. $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
  143. }
  144. /**
  145. * Test buildData with permalinks on.
  146. */
  147. public function testBuildDataPermalinks()
  148. {
  149. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  150. $feedBuilder->setLocale(self::$LOCALE);
  151. $feedBuilder->setUsePermalinks(true);
  152. $data = $feedBuilder->buildData();
  153. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  154. $this->assertTrue($data['usepermalinks']);
  155. // First link is a permalink
  156. $link = $data['links'][array_keys($data['links'])[2]];
  157. $this->assertEquals(41, $link['id']);
  158. $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
  159. $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
  160. $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
  161. $this->assertContains('Direct link', $link['description']);
  162. $this->assertContains('http://host.tld/?WDWyig', $link['description']);
  163. // Second link is a direct link
  164. $link = $data['links'][array_keys($data['links'])[3]];
  165. $this->assertEquals(8, $link['id']);
  166. $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
  167. $this->assertEquals('http://host.tld/?RttfEw', $link['guid']);
  168. $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
  169. $this->assertContains('Direct link', $link['description']);
  170. $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
  171. }
  172. /**
  173. * Test buildData with hide dates settings.
  174. */
  175. public function testBuildDataHideDates()
  176. {
  177. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  178. $feedBuilder->setLocale(self::$LOCALE);
  179. $feedBuilder->setHideDates(true);
  180. $data = $feedBuilder->buildData();
  181. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  182. $this->assertFalse($data['show_dates']);
  183. // Show dates while logged in
  184. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
  185. $feedBuilder->setLocale(self::$LOCALE);
  186. $feedBuilder->setHideDates(true);
  187. $data = $feedBuilder->buildData();
  188. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  189. $this->assertTrue($data['show_dates']);
  190. }
  191. /**
  192. * Test buildData when Shaarli is served from a subdirectory
  193. */
  194. public function testBuildDataServerSubdir()
  195. {
  196. $serverInfo = array(
  197. 'HTTPS' => 'Off',
  198. 'SERVER_NAME' => 'host.tld',
  199. 'SERVER_PORT' => '8080',
  200. 'SCRIPT_NAME' => '/~user/shaarli/index.php',
  201. 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
  202. );
  203. $feedBuilder = new FeedBuilder(
  204. self::$linkDB,
  205. FeedBuilder::$FEED_ATOM,
  206. $serverInfo,
  207. null,
  208. false
  209. );
  210. $feedBuilder->setLocale(self::$LOCALE);
  211. $data = $feedBuilder->buildData();
  212. $this->assertEquals(
  213. 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
  214. $data['self_link']
  215. );
  216. // Test first link (note link)
  217. $link = $data['links'][array_keys($data['links'])[2]];
  218. $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
  219. $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
  220. $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']);
  221. }
  222. }