FeedBuilderTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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('/Tue, 10 Mar 2015 11:46:51 \+\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['iso_date']);
  81. $this->assertContains('Stallman has a beard', $link['description']);
  82. $this->assertContains('Permalink', $link['description']);
  83. $this->assertContains('http://host.tld/?WDWyig', $link['description']);
  84. $this->assertEquals(1, count($link['taglist']));
  85. $this->assertEquals('sTuff', $link['taglist'][0]);
  86. // Test URL with external link.
  87. $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links']['20150310_114633']['url']);
  88. // Test multitags.
  89. $this->assertEquals(5, count($data['links']['20141125_084734']['taglist']));
  90. $this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]);
  91. }
  92. /**
  93. * Test buildData with ATOM feed (test only specific to ATOM).
  94. */
  95. public function testAtomBuildData()
  96. {
  97. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  98. $feedBuilder->setLocale(self::$LOCALE);
  99. $data = $feedBuilder->buildData();
  100. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  101. $link = array_shift($data['links']);
  102. $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:+\d{2}/', $link['iso_date']);
  103. }
  104. /**
  105. * Test buildData with search criteria.
  106. */
  107. public function testBuildDataFiltered()
  108. {
  109. $criteria = array(
  110. 'searchtags' => 'stuff',
  111. 'searchterm' => 'beard',
  112. );
  113. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
  114. $feedBuilder->setLocale(self::$LOCALE);
  115. $data = $feedBuilder->buildData();
  116. $this->assertEquals(1, count($data['links']));
  117. $link = array_shift($data['links']);
  118. $this->assertEquals('20150310_114651', $link['linkdate']);
  119. }
  120. /**
  121. * Test buildData with nb limit.
  122. */
  123. public function testBuildDataCount()
  124. {
  125. $criteria = array(
  126. 'nb' => '1',
  127. );
  128. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
  129. $feedBuilder->setLocale(self::$LOCALE);
  130. $data = $feedBuilder->buildData();
  131. $this->assertEquals(1, count($data['links']));
  132. $link = array_shift($data['links']);
  133. $this->assertEquals('20150310_114651', $link['linkdate']);
  134. }
  135. /**
  136. * Test buildData with permalinks on.
  137. */
  138. public function testBuildDataPermalinks()
  139. {
  140. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  141. $feedBuilder->setLocale(self::$LOCALE);
  142. $feedBuilder->setUsePermalinks(true);
  143. $data = $feedBuilder->buildData();
  144. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  145. $this->assertTrue($data['usepermalinks']);
  146. // First link is a permalink
  147. $link = array_shift($data['links']);
  148. $this->assertEquals('20150310_114651', $link['linkdate']);
  149. $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
  150. $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
  151. $this->assertContains('Direct link', $link['description']);
  152. $this->assertContains('http://host.tld/?WDWyig', $link['description']);
  153. // Second link is a direct link
  154. $link = array_shift($data['links']);
  155. $this->assertEquals('20150310_114633', $link['linkdate']);
  156. $this->assertEquals('http://host.tld/?kLHmZg', $link['guid']);
  157. $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
  158. $this->assertContains('Direct link', $link['description']);
  159. $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
  160. }
  161. /**
  162. * Test buildData with hide dates settings.
  163. */
  164. public function testBuildDataHideDates()
  165. {
  166. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  167. $feedBuilder->setLocale(self::$LOCALE);
  168. $feedBuilder->setHideDates(true);
  169. $data = $feedBuilder->buildData();
  170. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  171. $this->assertFalse($data['show_dates']);
  172. // Show dates while logged in
  173. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
  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->assertTrue($data['show_dates']);
  179. }
  180. /**
  181. * Test buildData with hide dates settings.
  182. */
  183. public function testBuildDataPubsubhub()
  184. {
  185. $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
  186. $feedBuilder->setLocale(self::$LOCALE);
  187. $feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
  188. $data = $feedBuilder->buildData();
  189. $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
  190. $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
  191. }
  192. }