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.

206 lines
7.7KB

  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__) . "/../resources/doT-php/doT.php");
  10. require_once (dirname(__FILE__) . "/../base.php");
  11. class BaseTest extends PHPUnit_Framework_TestCase
  12. {
  13. public function testAddURLParameter ()
  14. {
  15. $this->assertEquals ("?db=0", addURLParameter ("?", "db", "0"));
  16. $this->assertEquals ("?key=value&db=0", addURLParameter ("?key=value", "db", "0"));
  17. $this->assertEquals ("?key=value&otherKey=&db=0", addURLParameter ("?key=value&otherKey", "db", "0"));
  18. }
  19. /**
  20. * FALSE is returned if the create_function failed (meaning there was a syntax error)
  21. * @dataProvider providerTemplate
  22. */
  23. public function testServerSideRender ($template)
  24. {
  25. $_COOKIE["template"] = $template;
  26. $this->assertNull (serverSideRender (NULL));
  27. }
  28. /**
  29. * The function for the head of the HTML catalog
  30. * @dataProvider providerTemplate
  31. */
  32. public function testGenerateHeader ($templateName)
  33. {
  34. $_SERVER["HTTP_USER_AGENT"] = "Firefox";
  35. global $config;
  36. $headcontent = file_get_contents(dirname(__FILE__) . '/../templates/' . $templateName . '/file.html');
  37. $template = new doT ();
  38. $tpl = $template->template ($headcontent, NULL);
  39. $data = array("title" => $config['cops_title_default'],
  40. "version" => VERSION,
  41. "opds_url" => $config['cops_full_url'] . "feed.php",
  42. "customHeader" => "",
  43. "template" => $templateName,
  44. "server_side_rendering" => useServerSideRendering (),
  45. "current_css" => getCurrentCss (),
  46. "favico" => $config['cops_icon'],
  47. "getjson_url" => "getJSON.php?" . addURLParameter (getQueryString (), "complete", 1));
  48. $head = $tpl ($data);
  49. $this->assertContains ("<head>", $head);
  50. $this->assertContains ("</head>", $head);
  51. }
  52. public function providerTemplate ()
  53. {
  54. return array (
  55. array ("bootstrap"),
  56. array ("default")
  57. );
  58. }
  59. public function testLocalize ()
  60. {
  61. $this->assertEquals ("Authors", localize ("authors.title"));
  62. $this->assertEquals ("unknow.key", localize ("unknow.key"));
  63. }
  64. public function testLocalizeFr ()
  65. {
  66. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3";
  67. $this->assertEquals ("Auteurs", localize ("authors.title", -1, true));
  68. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
  69. localize ("authors.title", -1, true);
  70. }
  71. public function testLocalizeUnknown ()
  72. {
  73. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "aa";
  74. $this->assertEquals ("Authors", localize ("authors.title", -1, true));
  75. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
  76. localize ("authors.title", -1, true);
  77. }
  78. /**
  79. * @dataProvider providerGetLangAndTranslationFile
  80. */
  81. public function testGetLangAndTranslationFile ($acceptLanguage, $result)
  82. {
  83. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $acceptLanguage;
  84. list ($lang, $lang_file) = GetLangAndTranslationFile ();
  85. $this->assertEquals ($result, $lang);
  86. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
  87. localize ("authors.title", -1, true);
  88. }
  89. public function providerGetLangAndTranslationFile ()
  90. {
  91. return array (
  92. array ("en", "en"),
  93. array ("fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3", "fr"),
  94. array ("fr-FR", "fr"),
  95. array ("pt,en-us;q=0.7,en;q=0.3", "en"),
  96. array ("pt-br,pt;q=0.8,en-us;q=0.5,en;q=0.3", "pt_BR"),
  97. array ("pt-pt,pt;q=0.8,en;q=0.5,en-us;q=0.3", "pt_PT"),
  98. array ("zl", "en"),
  99. );
  100. }
  101. /**
  102. * @dataProvider providerGetAcceptLanguages
  103. */
  104. public function testGetAcceptLanguages ($acceptLanguage, $result)
  105. {
  106. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $acceptLanguage;
  107. $langs = array_keys(GetAcceptLanguages ());
  108. $this->assertEquals ($result, $langs[0]);
  109. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
  110. localize ("authors.title", -1, true);
  111. }
  112. public function providerGetAcceptLanguages ()
  113. {
  114. return array (
  115. array ("en", "en"),
  116. array ("en-US", "en_US"),
  117. array ("fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3", "fr"), // French locale with Firefox
  118. array ("fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4", "fr_FR"), // French locale with Chrome
  119. array ("fr-FR", "fr_FR"), // French locale with IE11
  120. array ("pt-br,pt;q=0.8,en-us;q=0.5,en;q=0.3", "pt_BR"),
  121. array ("zl", "zl"),
  122. );
  123. }
  124. public function testBaseFunction () {
  125. global $config;
  126. $this->assertFalse (Base::isMultipleDatabaseEnabled ());
  127. $this->assertEquals (array ("" => dirname(__FILE__) . "/BaseWithSomeBooks/"), Base::getDbList ());
  128. $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
  129. "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
  130. $this->assertTrue (Base::isMultipleDatabaseEnabled ());
  131. $this->assertEquals ("Some books", Base::getDbName (0));
  132. $this->assertEquals ("One book", Base::getDbName (1));
  133. $this->assertEquals ($config['calibre_directory'], Base::getDbList ());
  134. }
  135. public function testCheckDatabaseAvailability_1 () {
  136. $this->assertTrue (Base::checkDatabaseAvailability ());
  137. }
  138. public function testCheckDatabaseAvailability_2 () {
  139. global $config;
  140. $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
  141. "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
  142. $this->assertTrue (Base::checkDatabaseAvailability ());
  143. }
  144. /**
  145. * @expectedException Exception
  146. * @expectedExceptionMessage not found
  147. */
  148. public function testCheckDatabaseAvailability_Exception1 () {
  149. global $config;
  150. $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
  151. "One book" => dirname(__FILE__) . "/OneBook/");
  152. $this->assertTrue (Base::checkDatabaseAvailability ());
  153. }
  154. /**
  155. * @expectedException Exception
  156. * @expectedExceptionMessage not found
  157. */
  158. public function testCheckDatabaseAvailability_Exception2 () {
  159. global $config;
  160. $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/SomeBooks/",
  161. "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
  162. $this->assertTrue (Base::checkDatabaseAvailability ());
  163. }
  164. /*
  165. Test normalized utf8 string according to unicode.org output
  166. more here :
  167. http://unicode.org/cldr/utility/transform.jsp?a=Latin-ASCII&b=%C3%80%C3%81%C3%82%C3%83%C3%84%C3%85%C3%87%C3%88%C3%89%C3%8A%C3%8B%C3%8C%C3%8D%C3%8E%C3%8F%C5%92%C3%92%C3%93%C3%94%C3%95%C3%96%C3%99%C3%9A%C3%9B%C3%9C%C3%9D%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5%C3%A7%C3%A8%C3%A9%C3%AA%C3%AB%C3%AC%C3%AD%C3%AE%C3%AF%C5%93%C3%B0%C3%B2%C3%B3%C3%B4%C3%B5%C3%B6%C3%B9%C3%BA%C3%BB%C3%BC%C3%BD%C3%BF%C3%B1
  168. */
  169. public function testNormalizeUtf8String () {
  170. $this->assertEquals ("AAAAAACEEEEIIIIOEOOOOOUUUUYaaaaaaceeeeiiiioedooooouuuuyyn",
  171. normalizeUtf8String ("ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏŒÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïœðòóôõöùúûüýÿñ"));
  172. }
  173. }