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.

255 lines
8.1KB

  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__) . "/../OPDS_renderer.php");
  11. define ("OPDS_RELAX_NG", dirname(__FILE__) . "/opds-relax-ng/opds_catalog_1_1.rng");
  12. define ("OPENSEARCHDESCRIPTION_RELAX_NG", dirname(__FILE__) . "/opds-relax-ng/opensearchdescription.rng");
  13. define ("JING_JAR", dirname(__FILE__) . "/jing.jar");
  14. define ("OPDSVALIDATOR_JAR", dirname(__FILE__) . "/OPDSValidator.jar");
  15. define ("TEST_FEED", dirname(__FILE__) . "/text.atom");
  16. class OpdsTest extends PHPUnit_Framework_TestCase
  17. {
  18. public static function tearDownAfterClass()
  19. {
  20. if (!file_exists (TEST_FEED)) {
  21. return;
  22. }
  23. unlink (TEST_FEED);
  24. }
  25. function jingValidateSchema($feed, $relax = OPDS_RELAX_NG) {
  26. $path = "";
  27. $res = system($path . 'java -jar ' . JING_JAR . ' ' . $relax . ' ' . $feed);
  28. if ($res != '') {
  29. echo 'RelaxNG validation error: '.$res;
  30. return false;
  31. } else
  32. return true;
  33. }
  34. function opdsValidator($feed) {
  35. $oldcwd = getcwd(); // Save the old working directory
  36. chdir("test");
  37. $path = "";
  38. $res = system($path . 'java -jar ' . OPDSVALIDATOR_JAR . ' ' . $feed);
  39. chdir($oldcwd);
  40. if ($res != '') {
  41. echo 'OPDS validation error: '.$res;
  42. return false;
  43. } else
  44. return true;
  45. }
  46. function opdsCompleteValidation ($feed) {
  47. return $this->jingValidateSchema($feed) && $this->opdsValidator($feed);
  48. }
  49. public function testPageIndex ()
  50. {
  51. global $config;
  52. $page = Base::PAGE_INDEX;
  53. $query = NULL;
  54. $qid = NULL;
  55. $n = "1";
  56. $_SERVER['QUERY_STRING'] = "";
  57. $config['cops_subtitle_default'] = "My subtitle";
  58. $currentPage = Page::getPage ($page, $qid, $query, $n);
  59. $currentPage->InitializeContent ();
  60. $OPDSRender = new OPDSRenderer ();
  61. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  62. $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
  63. $_SERVER ["HTTP_USER_AGENT"] = "XXX";
  64. $config['cops_generate_invalid_opds_stream'] = "1";
  65. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  66. $this->AssertFalse ($this->jingValidateSchema (TEST_FEED));
  67. $this->AssertFalse ($this->opdsValidator (TEST_FEED));
  68. $_SERVER['QUERY_STRING'] = NULL;
  69. }
  70. /**
  71. * @dataProvider providerPage
  72. */
  73. public function testMostPages ($page, $query)
  74. {
  75. $qid = NULL;
  76. $n = "1";
  77. $_SERVER['QUERY_STRING'] = "?page={$page}";
  78. if (!empty ($query)) {
  79. $_SERVER['QUERY_STRING'] .= "&query={$query}";
  80. }
  81. $_SERVER['REQUEST_URI'] = "feed.php" . $_SERVER['QUERY_STRING'];
  82. $currentPage = Page::getPage ($page, $qid, $query, $n);
  83. $currentPage->InitializeContent ();
  84. $OPDSRender = new OPDSRenderer ();
  85. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  86. $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
  87. }
  88. public function providerPage ()
  89. {
  90. return array (
  91. array (Base::PAGE_OPENSEARCH, "car"),
  92. array (Base::PAGE_ALL_AUTHORS, NULL),
  93. array (Base::PAGE_ALL_SERIES, NULL),
  94. array (Base::PAGE_ALL_TAGS, NULL),
  95. array (Base::PAGE_ALL_PUBLISHERS, NULL),
  96. array (Base::PAGE_ALL_LANGUAGES, NULL),
  97. array (Base::PAGE_ALL_RECENT_BOOKS, NULL),
  98. array (Base::PAGE_ALL_BOOKS, NULL)
  99. );
  100. }
  101. public function testPageIndexMultipleDatabase ()
  102. {
  103. global $config;
  104. $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
  105. "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
  106. $page = Base::PAGE_INDEX;
  107. $query = NULL;
  108. $qid = "1";
  109. $n = "1";
  110. $_SERVER['QUERY_STRING'] = "";
  111. $currentPage = Page::getPage ($page, $qid, $query, $n);
  112. $currentPage->InitializeContent ();
  113. $OPDSRender = new OPDSRenderer ();
  114. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  115. $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
  116. }
  117. public function testOpenSearchDescription ()
  118. {
  119. $_SERVER['QUERY_STRING'] = "";
  120. $OPDSRender = new OPDSRenderer ();
  121. file_put_contents (TEST_FEED, $OPDSRender->getOpenSearch ());
  122. $this->AssertTrue ($this->jingValidateSchema (TEST_FEED, OPENSEARCHDESCRIPTION_RELAX_NG));
  123. $_SERVER['QUERY_STRING'] = NULL;
  124. }
  125. public function testPageAuthorMultipleDatabase ()
  126. {
  127. global $config;
  128. $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
  129. "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
  130. $page = Base::PAGE_AUTHOR_DETAIL;
  131. $query = NULL;
  132. $qid = "1";
  133. $n = "1";
  134. $_SERVER['QUERY_STRING'] = "page=" . Base::PAGE_AUTHOR_DETAIL . "&id=1";
  135. $_GET ["db"] = "0";
  136. $currentPage = Page::getPage ($page, $qid, $query, $n);
  137. $currentPage->InitializeContent ();
  138. $OPDSRender = new OPDSRenderer ();
  139. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  140. $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
  141. }
  142. public function testPageAuthorsDetail ()
  143. {
  144. global $config;
  145. $page = Base::PAGE_AUTHOR_DETAIL;
  146. $query = NULL;
  147. $qid = "1";
  148. $n = "1";
  149. $_SERVER['QUERY_STRING'] = "page=" . Base::PAGE_AUTHOR_DETAIL . "&id=1&n=1";
  150. $config['cops_max_item_per_page'] = 2;
  151. // First page
  152. $currentPage = Page::getPage ($page, $qid, $query, $n);
  153. $currentPage->InitializeContent ();
  154. $OPDSRender = new OPDSRenderer ();
  155. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  156. $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
  157. // Second page
  158. $n = 2;
  159. $currentPage = Page::getPage ($page, $qid, $query, $n);
  160. $currentPage->InitializeContent ();
  161. $OPDSRender = new OPDSRenderer ();
  162. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  163. $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
  164. // No pagination
  165. $config['cops_max_item_per_page'] = -1;
  166. }
  167. public function testPageAuthorsDetail_WithFacets ()
  168. {
  169. global $config;
  170. $page = Base::PAGE_AUTHOR_DETAIL;
  171. $query = NULL;
  172. $qid = "1";
  173. $n = "1";
  174. $_SERVER['QUERY_STRING'] = "page=" . Base::PAGE_AUTHOR_DETAIL . "&id=1&n=1";
  175. $_GET["tag"] = "Short Stories";
  176. $config['cops_books_filter'] = array ("Only Short Stories" => "Short Stories", "No Short Stories" => "!Short Stories");
  177. $currentPage = Page::getPage ($page, $qid, $query, $n);
  178. $currentPage->InitializeContent ();
  179. $OPDSRender = new OPDSRenderer ();
  180. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  181. $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
  182. $config['cops_books_filter'] = array ();
  183. }
  184. public function testPageAuthorsDetail_WithoutAnyId ()
  185. {
  186. global $config;
  187. $page = Base::PAGE_AUTHOR_DETAIL;
  188. $query = NULL;
  189. $qid = "1";
  190. $n = "1";
  191. $_SERVER['QUERY_STRING'] = "page=" . Base::PAGE_AUTHOR_DETAIL . "&id=1&n=1";
  192. $_SERVER['REQUEST_URI'] = "index.php?XXXX";
  193. $currentPage = Page::getPage ($page, $qid, $query, $n);
  194. $currentPage->InitializeContent ();
  195. $currentPage->idPage = NULL;
  196. $OPDSRender = new OPDSRenderer ();
  197. file_put_contents (TEST_FEED, $OPDSRender->render ($currentPage));
  198. $this->AssertTrue ($this->opdsCompleteValidation (TEST_FEED));
  199. }
  200. }