BookmarkExportTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. require_once 'application/NetscapeBookmarkUtils.php';
  3. /**
  4. * Netscape bookmark export
  5. */
  6. class BookmarkExportTest extends PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * @var string datastore to test write operations
  10. */
  11. protected static $testDatastore = 'sandbox/datastore.php';
  12. /**
  13. * @var ReferenceLinkDB instance.
  14. */
  15. protected static $refDb = null;
  16. /**
  17. * @var LinkDB private LinkDB instance.
  18. */
  19. protected static $linkDb = null;
  20. /**
  21. * Instantiate reference data
  22. */
  23. public static function setUpBeforeClass()
  24. {
  25. self::$refDb = new ReferenceLinkDB();
  26. self::$refDb->write(self::$testDatastore);
  27. self::$linkDb = new LinkDB(self::$testDatastore, true, false);
  28. }
  29. /**
  30. * Attempt to export an invalid link selection
  31. * @expectedException Exception
  32. * @expectedExceptionMessageRegExp /Invalid export selection/
  33. */
  34. public function testFilterAndFormatInvalid()
  35. {
  36. NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, '');
  37. }
  38. /**
  39. * Prepare all links for export
  40. */
  41. public function testFilterAndFormatAll()
  42. {
  43. $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, '');
  44. $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
  45. foreach ($links as $link) {
  46. $date = $link['created'];
  47. $this->assertEquals(
  48. $date->getTimestamp(),
  49. $link['timestamp']
  50. );
  51. $this->assertEquals(
  52. str_replace(' ', ',', $link['tags']),
  53. $link['taglist']
  54. );
  55. }
  56. }
  57. /**
  58. * Prepare private links for export
  59. */
  60. public function testFilterAndFormatPrivate()
  61. {
  62. $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, '');
  63. $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
  64. foreach ($links as $link) {
  65. $date = $link['created'];
  66. $this->assertEquals(
  67. $date->getTimestamp(),
  68. $link['timestamp']
  69. );
  70. $this->assertEquals(
  71. str_replace(' ', ',', $link['tags']),
  72. $link['taglist']
  73. );
  74. }
  75. }
  76. /**
  77. * Prepare public links for export
  78. */
  79. public function testFilterAndFormatPublic()
  80. {
  81. $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, '');
  82. $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
  83. foreach ($links as $link) {
  84. $date = $link['created'];
  85. $this->assertEquals(
  86. $date->getTimestamp(),
  87. $link['timestamp']
  88. );
  89. $this->assertEquals(
  90. str_replace(' ', ',', $link['tags']),
  91. $link['taglist']
  92. );
  93. }
  94. }
  95. /**
  96. * Do not prepend notes with the Shaarli index's URL
  97. */
  98. public function testFilterAndFormatDoNotPrependNoteUrl()
  99. {
  100. $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, '');
  101. $this->assertEquals(
  102. '?WDWyig',
  103. $links[0]['url']
  104. );
  105. }
  106. /**
  107. * Prepend notes with the Shaarli index's URL
  108. */
  109. public function testFilterAndFormatPrependNoteUrl()
  110. {
  111. $indexUrl = 'http://localhost:7469/shaarli/';
  112. $links = NetscapeBookmarkUtils::filterAndFormat(
  113. self::$linkDb,
  114. 'public',
  115. true,
  116. $indexUrl
  117. );
  118. $this->assertEquals(
  119. $indexUrl . '?WDWyig',
  120. $links[0]['url']
  121. );
  122. }
  123. }