Calibre OPDS (and HTML) PHP Server : web-based light alternative to Calibre content server / Calibre2OPDS to serve ebooks (epub, mobi, pdf, ...) http://blog.slucas.fr/en/oss/calibre-opds-php-server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

425 lines
16KB

  1. <?php
  2. /**
  3. * COPS (Calibre OPDS PHP Server) test file
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Sébastien Lucas <sebastien@slucas.fr>
  7. */
  8. require_once (dirname(__FILE__) . "/config_test.php");
  9. require_once (dirname(__FILE__) . "/../book.php");
  10. require_once (dirname(__FILE__) . "/../JSON_renderer.php");
  11. /*
  12. Publishers:
  13. id:2 (2 books) Macmillan and Co. London: Lewis Caroll
  14. id:3 (2 books) D. Appleton and Company Alexander Dumas
  15. id:4 (1 book) Macmillan Publishers USA: Jack London
  16. id:5 (1 book) Pierson's Magazine: H. G. Wells
  17. id:6 (8 books) Strand Magazine: Arthur Conan Doyle
  18. */
  19. define ("TEST_THUMBNAIL", dirname(__FILE__) . "/thumbnail.jpg");
  20. define ("COVER_WIDTH", 400);
  21. define ("COVER_HEIGHT", 600);
  22. class BookTest extends PHPUnit_Framework_TestCase
  23. {
  24. public static function setUpBeforeClass()
  25. {
  26. $book = Book::getBookById(2);
  27. if (!is_dir ($book->path)) {
  28. mkdir ($book->path, 0777, true);
  29. }
  30. $im = imagecreatetruecolor(COVER_WIDTH, COVER_HEIGHT);
  31. $text_color = imagecolorallocate($im, 255, 0, 0);
  32. imagestring($im, 1, 5, 5, 'Book cover', $text_color);
  33. imagejpeg ($im, $book->path . "/cover.jpg", 80);
  34. }
  35. public static function tearDownAfterClass()
  36. {
  37. $book = Book::getBookById(2);
  38. if (!file_exists ($book->path . "/cover.jpg")) {
  39. return;
  40. }
  41. unlink ($book->path . "/cover.jpg");
  42. rmdir ($book->path);
  43. rmdir (dirname ($book->path));
  44. }
  45. public function testGetBookCount ()
  46. {
  47. $this->assertEquals (14, Book::getBookCount ());
  48. }
  49. public function testGetCount ()
  50. {
  51. $entryArray = Book::getCount ();
  52. $this->assertEquals (2, count($entryArray));
  53. $entryAllBooks = $entryArray [0];
  54. $this->assertEquals ("Alphabetical index of the 14 books", $entryAllBooks->content);
  55. $entryRecentBooks = $entryArray [1];
  56. $this->assertEquals ("50 most recent books", $entryRecentBooks->content);
  57. }
  58. public function testGetCountRecent ()
  59. {
  60. global $config;
  61. $config['cops_recentbooks_limit'] = 0;
  62. $entryArray = Book::getCount ();
  63. $this->assertEquals (1, count($entryArray));
  64. $config['cops_recentbooks_limit'] = 2;
  65. $entryArray = Book::getCount ();
  66. $entryRecentBooks = $entryArray [1];
  67. $this->assertEquals ("2 most recent books", $entryRecentBooks->content);
  68. $config['cops_recentbooks_limit'] = 50;
  69. }
  70. public function testGetBooksByAuthor ()
  71. {
  72. // All book by Arthur Conan Doyle
  73. global $config;
  74. $config['cops_max_item_per_page'] = 5;
  75. list ($entryArray, $totalNumber) = Book::getBooksByAuthor (1, 1);
  76. $this->assertEquals (5, count($entryArray));
  77. $this->assertEquals (8, $totalNumber);
  78. list ($entryArray, $totalNumber) = Book::getBooksByAuthor (1, 2);
  79. $this->assertEquals (3, count($entryArray));
  80. $this->assertEquals (8, $totalNumber);
  81. $config['cops_max_item_per_page'] = -1;
  82. list ($entryArray, $totalNumber) = Book::getBooksByAuthor (1, -1);
  83. $this->assertEquals (8, count($entryArray));
  84. $this->assertEquals (-1, $totalNumber);
  85. }
  86. public function testGetBooksBySeries ()
  87. {
  88. // All book from the Sherlock Holmes series
  89. list ($entryArray, $totalNumber) = Book::getBooksBySeries (1, -1);
  90. $this->assertEquals (7, count($entryArray));
  91. $this->assertEquals (-1, $totalNumber);
  92. }
  93. public function testGetBooksByPublisher ()
  94. {
  95. // All books from Strand Magazine
  96. list ($entryArray, $totalNumber) = Book::getBooksByPublisher (6, -1);
  97. $this->assertEquals (8, count($entryArray));
  98. $this->assertEquals (-1, $totalNumber);
  99. }
  100. public function testGetBooksByTag ()
  101. {
  102. // All book with the Fiction tag
  103. list ($entryArray, $totalNumber) = Book::getBooksByTag (1, -1);
  104. $this->assertEquals (14, count($entryArray));
  105. $this->assertEquals (-1, $totalNumber);
  106. }
  107. public function testGetBooksByLanguage ()
  108. {
  109. // All english book (= all books)
  110. list ($entryArray, $totalNumber) = Book::getBooksByLanguage (1, -1);
  111. $this->assertEquals (14, count($entryArray));
  112. $this->assertEquals (-1, $totalNumber);
  113. }
  114. public function testGetAllBooks ()
  115. {
  116. // All books by first letter
  117. $entryArray = Book::getAllBooks ();
  118. $this->assertCount (9, $entryArray);
  119. }
  120. public function testGetBooksByStartingLetter ()
  121. {
  122. // All books by first letter
  123. list ($entryArray, $totalNumber) = Book::getBooksByStartingLetter ("T", -1);
  124. $this->assertEquals (-1, $totalNumber);
  125. $this->assertCount (3, $entryArray);
  126. }
  127. public function testGetBookByDataId ()
  128. {
  129. $book = Book::getBookByDataId (17);
  130. $this->assertEquals ("Alice's Adventures in Wonderland", $book->getTitle ());
  131. }
  132. public function testGetAllRecentBooks ()
  133. {
  134. // All recent books
  135. global $config;
  136. $config['cops_recentbooks_limit'] = 2;
  137. $entryArray = Book::getAllRecentBooks ();
  138. $this->assertCount (2, $entryArray);
  139. $config['cops_recentbooks_limit'] = 50;
  140. $entryArray = Book::getAllRecentBooks ();
  141. $this->assertCount (14, $entryArray);
  142. }
  143. public function testGetBookById ()
  144. {
  145. // also check most of book's class methods
  146. $book = Book::getBookById(2);
  147. $linkArray = $book->getLinkArray ();
  148. $this->assertCount (5, $linkArray);
  149. $this->assertEquals ("The Return of Sherlock Holmes", $book->getTitle ());
  150. $this->assertEquals ("urn:uuid:87ddbdeb-1e27-4d06-b79b-4b2a3bfc6a5f", $book->getEntryId ());
  151. $this->assertEquals ("index.php?page=13&id=2", $book->getDetailUrl ());
  152. $this->assertEquals ("Doyle, Arthur Conan", $book->getAuthorsName ());
  153. $this->assertEquals ("Fiction, Mystery & Detective, Short Stories", $book->getTagsName ());
  154. $this->assertEquals ('<p class="description">The Return of Sherlock Holmes is a collection of 13 Sherlock Holmes stories, originally published in 1903-1904, by Arthur Conan Doyle.<br />The book was first published on March 7, 1905 by Georges Newnes, Ltd and in a Colonial edition by Longmans. 30,000 copies were made of the initial print run. The US edition by McClure, Phillips &amp; Co. added another 28,000 to the run.<br />This was the first Holmes collection since 1893, when Holmes had "died" in "The Adventure of the Final Problem". Having published The Hound of the Baskervilles in 1901–1902 (although setting it before Holmes\' death) Doyle came under intense pressure to revive his famous character.</p>', $book->getComment (false));
  155. $this->assertEquals ("English", $book->getLanguages ());
  156. $this->assertEquals ("", $book->getRating ());
  157. $book->rating = 8;
  158. // 4 filled stars and one empty
  159. $this->assertEquals ("&#9733;&#9733;&#9733;&#9733;&#9734;", $book->getRating ());
  160. $this->assertEquals ("Strand Magazine", $book->getPublisher()->name);
  161. }
  162. public function testGetThumbnailNotNeeded ()
  163. {
  164. $book = Book::getBookById(2);
  165. $this->assertFalse ($book->getThumbnail (NULL, NULL, NULL));
  166. // Current cover is 400*600
  167. $this->assertFalse ($book->getThumbnail (COVER_WIDTH, NULL, NULL));
  168. $this->assertFalse ($book->getThumbnail (COVER_WIDTH + 1, NULL, NULL));
  169. $this->assertFalse ($book->getThumbnail (NULL, COVER_HEIGHT, NULL));
  170. $this->assertFalse ($book->getThumbnail (NULL, COVER_HEIGHT + 1, NULL));
  171. }
  172. /**
  173. * @dataProvider providerThumbnail
  174. */
  175. public function testGetThumbnailByWidth ($width, $height, $expectedWidth, $expectedHeight)
  176. {
  177. $book = Book::getBookById(2);
  178. $this->assertTrue ($book->getThumbnail ($width, $height, TEST_THUMBNAIL));
  179. $size = GetImageSize(TEST_THUMBNAIL);
  180. $this->assertEquals ($expectedWidth, $size [0]);
  181. $this->assertEquals ($expectedHeight, $size [1]);
  182. unlink (TEST_THUMBNAIL);
  183. }
  184. public function providerThumbnail ()
  185. {
  186. return array (
  187. array (164, NULL, 164, 246),
  188. array (NULL, 164, 109, 164)
  189. );
  190. }
  191. public function testGetMostInterestingDataToSendToKindle ()
  192. {
  193. // Get Alice (available as MOBI, PDF, EPUB in that order)
  194. $book = Book::getBookById(17);
  195. $data = $book->GetMostInterestingDataToSendToKindle ();
  196. $this->assertEquals ("MOBI", $data->format);
  197. array_shift ($book->datas);
  198. $data = $book->GetMostInterestingDataToSendToKindle ();
  199. $this->assertEquals ("PDF", $data->format);
  200. array_shift ($book->datas);
  201. $data = $book->GetMostInterestingDataToSendToKindle ();
  202. $this->assertEquals ("EPUB", $data->format);
  203. }
  204. public function testGetDataById ()
  205. {
  206. global $config;
  207. // Get Alice MOBI=>17, PDF=>19, EPUB=>20
  208. $book = Book::getBookById(17);
  209. $data = $book->getDataById (17);
  210. $this->assertEquals ("MOBI", $data->format);
  211. $data = $book->getDataById (20);
  212. $this->assertEquals ("EPUB", $data->format);
  213. $this->assertEquals ("Carroll, Lewis - Alice's Adventures in Wonderland.epub", $data->getUpdatedFilenameEpub ());
  214. $this->assertEquals ("Carroll, Lewis - Alice's Adventures in Wonderland.kepub.epub", $data->getUpdatedFilenameKepub ());
  215. $this->assertEquals (dirname(__FILE__) . "/BaseWithSomeBooks/Lewis Carroll/Alice's Adventures in Wonderland (17)/Alice's Adventures in Wonderland - Lewis Carroll.epub", $data->getLocalPath ());
  216. $config['cops_use_url_rewriting'] = "1";
  217. $config['cops_provide_kepub'] = "1";
  218. $_SERVER["HTTP_USER_AGENT"] = "Kobo";
  219. $this->assertEquals ("download/20/Carroll%2C+Lewis+-+Alice%27s+Adventures+in+Wonderland.kepub.epub", $data->getHtmlLink ());
  220. $_SERVER["HTTP_USER_AGENT"] = "Firefox";
  221. $this->assertEquals ("download/20/Alice%27s+Adventures+in+Wonderland+-+Lewis+Carroll.epub", $data->getHtmlLink ());
  222. $config['cops_use_url_rewriting'] = "0";
  223. $this->assertEquals ("fetch.php?data=20&type=epub&id=17", $data->getHtmlLink ());
  224. }
  225. public function testGetFilePath () {
  226. $book = Book::getBookById(17);
  227. $this->assertEquals ("Lewis Carroll/Alice's Adventures in Wonderland (17)/cover.jpg", $book->getFilePath ("jpg", NULL, true));
  228. $this->assertEquals ("Lewis Carroll/Alice's Adventures in Wonderland (17)/Alice's Adventures in Wonderland - Lewis Carroll.epub", $book->getFilePath ("epub", 20, true));
  229. $this->assertEquals ("Lewis Carroll/Alice's Adventures in Wonderland (17)/Alice's Adventures in Wonderland - Lewis Carroll.mobi", $book->getFilePath ("mobi", 17, true));
  230. }
  231. public function testGetDataFormat () {
  232. $book = Book::getBookById(17);
  233. // Get Alice MOBI=>17, PDF=>19, EPUB=>20
  234. $data = $book->getDataFormat ("EPUB");
  235. $this->assertEquals (20, $data->id);
  236. $data = $book->getDataFormat ("MOBI");
  237. $this->assertEquals (17, $data->id);
  238. $data = $book->getDataFormat ("PDF");
  239. $this->assertEquals (19, $data->id);
  240. $this->assertNull ($book->getDataFormat ("FB2"));
  241. }
  242. public function testGetMimeType () {
  243. $book = Book::getBookById(17);
  244. // Get Alice MOBI=>17, PDF=>19, EPUB=>20
  245. $data = $book->getDataFormat ("EPUB");
  246. $this->assertEquals ("application/epub+zip", $data->getMimeType ());
  247. $data = $book->getDataFormat ("MOBI");
  248. $this->assertEquals ("application/x-mobipocket-ebook", $data->getMimeType ());
  249. $data = $book->getDataFormat ("PDF");
  250. $this->assertEquals ("application/pdf", $data->getMimeType ());
  251. // Alter a data to make a test for finfo_file if enabled
  252. $data->extension = "ico";
  253. $data->format = "ICO";
  254. $data->name = "favicon";
  255. $data->book->path = realpath (dirname(__FILE__) . "/../");
  256. if (function_exists('finfo_open') === true) {
  257. $this->assertEquals ("image/x-icon", $data->getMimeType ());
  258. } else {
  259. $this->assertEquals ("application/octet-stream", $data->getMimeType ());
  260. }
  261. }
  262. public function testTypeaheadSearch ()
  263. {
  264. $_GET["page"] = Base::PAGE_OPENSEARCH_QUERY;
  265. $_GET["query"] = "fic";
  266. $_GET["search"] = "1";
  267. $array = JSONRenderer::getJson ();
  268. $this->assertCount (3, $array);
  269. $this->assertEquals ("2 tags", $array[0]["title"]);
  270. $this->assertEquals ("Fiction", $array[1]["title"]);
  271. $this->assertEquals ("Science Fiction", $array[2]["title"]);
  272. $_GET["query"] = "car";
  273. $_GET["search"] = "1";
  274. $array = JSONRenderer::getJson ();
  275. $this->assertCount (4, $array);
  276. $this->assertEquals ("1 book", $array[0]["title"]);
  277. $this->assertEquals ("A Study in Scarlet", $array[1]["title"]);
  278. $this->assertEquals ("1 author", $array[2]["title"]);
  279. $this->assertEquals ("Carroll, Lewis", $array[3]["title"]);
  280. $_GET["query"] = "art";
  281. $_GET["search"] = "1";
  282. $array = JSONRenderer::getJson ();
  283. $this->assertCount (4, $array);
  284. $this->assertEquals ("1 author", $array[0]["title"]);
  285. $this->assertEquals ("Doyle, Arthur Conan", $array[1]["title"]);
  286. $this->assertEquals ("1 series", $array[2]["title"]);
  287. $this->assertEquals ("D'Artagnan Romances", $array[3]["title"]);
  288. $_GET["query"] = "Macmillan";
  289. $_GET["search"] = "1";
  290. $array = JSONRenderer::getJson ();
  291. $this->assertCount (3, $array);
  292. $this->assertEquals ("2 publishers", $array[0]["title"]);
  293. $this->assertEquals ("Macmillan and Co. London", $array[1]["title"]);
  294. $this->assertEquals ("Macmillan Publishers USA", $array[2]["title"]);
  295. $_GET["query"] = NULL;
  296. $_GET["search"] = NULL;
  297. }
  298. public function testTypeaheadSearchWithIgnored ()
  299. {
  300. global $config;
  301. $_GET["page"] = Base::PAGE_OPENSEARCH_QUERY;
  302. $_GET["query"] = "car";
  303. $_GET["search"] = "1";
  304. $config ['cops_ignored_categories'] = array ("author");
  305. $array = JSONRenderer::getJson ();
  306. $this->assertCount (2, $array);
  307. $this->assertEquals ("1 book", $array[0]["title"]);
  308. $this->assertEquals ("A Study in Scarlet", $array[1]["title"]);
  309. $_GET["query"] = "art";
  310. $_GET["search"] = "1";
  311. $config ['cops_ignored_categories'] = array ("series");
  312. $array = JSONRenderer::getJson ();
  313. $this->assertCount (2, $array);
  314. $this->assertEquals ("1 author", $array[0]["title"]);
  315. $this->assertEquals ("Doyle, Arthur Conan", $array[1]["title"]);
  316. $_GET["query"] = NULL;
  317. $_GET["search"] = NULL;
  318. }
  319. public function testTypeaheadSearchMultiDatabase ()
  320. {
  321. global $config;
  322. $_GET["page"] = Base::PAGE_OPENSEARCH_QUERY;
  323. $_GET["query"] = "art";
  324. $_GET["search"] = "1";
  325. $_GET["multi"] = "1";
  326. $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
  327. "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
  328. $array = JSONRenderer::getJson ();
  329. $this->assertCount (5, $array);
  330. $this->assertEquals ("Some books", $array[0]["title"]);
  331. $this->assertEquals ("1 author", $array[1]["title"]);
  332. $this->assertEquals ("1 series", $array[2]["title"]);
  333. $this->assertEquals ("One book", $array[3]["title"]);
  334. $this->assertEquals ("1 book", $array[4]["title"]);
  335. $_GET["query"] = NULL;
  336. $_GET["search"] = NULL;
  337. }
  338. public function tearDown () {
  339. Base::clearDb ();
  340. }
  341. }