2013-10-03 12:48:02 +03:00
|
|
|
<?php
|
2013-10-24 15:23:32 +03:00
|
|
|
/**
|
|
|
|
* COPS (Calibre OPDS PHP Server) test file
|
|
|
|
*
|
|
|
|
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
|
|
|
|
* @author Sébastien Lucas <sebastien@slucas.fr>
|
|
|
|
*/
|
2013-10-03 12:48:02 +03:00
|
|
|
|
|
|
|
require_once (dirname(__FILE__) . "/config_test.php");
|
2014-01-06 22:24:11 +02:00
|
|
|
require_once (dirname(__FILE__) . "/../resources/doT-php/doT.php");
|
2013-10-03 12:48:02 +03:00
|
|
|
require_once (dirname(__FILE__) . "/../base.php");
|
|
|
|
|
2013-10-03 16:22:21 +03:00
|
|
|
class BaseTest extends PHPUnit_Framework_TestCase
|
2013-12-05 11:52:51 +02:00
|
|
|
{
|
2013-10-03 12:48:02 +03:00
|
|
|
public function testAddURLParameter ()
|
|
|
|
{
|
|
|
|
$this->assertEquals ("?db=0", addURLParameter ("?", "db", "0"));
|
|
|
|
$this->assertEquals ("?key=value&db=0", addURLParameter ("?key=value", "db", "0"));
|
|
|
|
$this->assertEquals ("?key=value&otherKey=&db=0", addURLParameter ("?key=value&otherKey", "db", "0"));
|
|
|
|
}
|
2014-02-06 21:48:46 +02:00
|
|
|
|
2014-08-27 11:02:15 +03:00
|
|
|
/**
|
|
|
|
* FALSE is returned if the create_function failed (meaning there was a syntax error)
|
|
|
|
* @dataProvider providerTemplate
|
|
|
|
*/
|
|
|
|
public function testServerSideRender ($template)
|
2014-01-06 22:24:11 +02:00
|
|
|
{
|
2014-08-27 11:02:15 +03:00
|
|
|
$_COOKIE["template"] = $template;
|
2014-01-06 22:24:11 +02:00
|
|
|
$this->assertNull (serverSideRender (NULL));
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
2014-08-27 11:02:15 +03:00
|
|
|
/**
|
|
|
|
* The function for the head of the HTML catalog
|
|
|
|
* @dataProvider providerTemplate
|
|
|
|
*/
|
|
|
|
public function testGenerateHeader ($templateName)
|
2014-02-14 18:10:14 +02:00
|
|
|
{
|
|
|
|
$_SERVER["HTTP_USER_AGENT"] = "Firefox";
|
|
|
|
global $config;
|
2014-08-27 11:02:15 +03:00
|
|
|
$headcontent = file_get_contents(dirname(__FILE__) . '/../templates/' . $templateName . '/file.html');
|
2014-02-14 18:10:14 +02:00
|
|
|
$template = new doT ();
|
2014-08-27 11:02:15 +03:00
|
|
|
$tpl = $template->template ($headcontent, NULL);
|
2014-02-14 18:10:14 +02:00
|
|
|
$data = array("title" => $config['cops_title_default'],
|
|
|
|
"version" => VERSION,
|
|
|
|
"opds_url" => $config['cops_full_url'] . "feed.php",
|
2014-07-29 23:36:32 +03:00
|
|
|
"customHeader" => "",
|
2014-08-27 11:02:15 +03:00
|
|
|
"template" => $templateName,
|
2014-02-14 18:10:14 +02:00
|
|
|
"server_side_rendering" => useServerSideRendering (),
|
|
|
|
"current_css" => getCurrentCss (),
|
|
|
|
"favico" => $config['cops_icon'],
|
|
|
|
"getjson_url" => "getJSON.php?" . addURLParameter (getQueryString (), "complete", 1));
|
|
|
|
|
2014-08-27 11:02:15 +03:00
|
|
|
$head = $tpl ($data);
|
2014-02-14 18:10:14 +02:00
|
|
|
$this->assertContains ("<head>", $head);
|
|
|
|
$this->assertContains ("</head>", $head);
|
|
|
|
}
|
|
|
|
|
2014-08-27 11:02:15 +03:00
|
|
|
public function providerTemplate ()
|
|
|
|
{
|
|
|
|
return array (
|
|
|
|
array ("bootstrap"),
|
|
|
|
array ("default")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-04 13:20:34 +03:00
|
|
|
public function testLocalize ()
|
|
|
|
{
|
|
|
|
$this->assertEquals ("Authors", localize ("authors.title"));
|
2013-12-05 11:52:51 +02:00
|
|
|
|
2013-10-15 09:31:12 +03:00
|
|
|
$this->assertEquals ("unknow.key", localize ("unknow.key"));
|
2013-10-04 13:20:34 +03:00
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
2013-10-04 13:20:34 +03:00
|
|
|
public function testLocalizeFr ()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3";
|
|
|
|
$this->assertEquals ("Auteurs", localize ("authors.title", -1, true));
|
2013-12-05 11:52:51 +02:00
|
|
|
|
2013-10-04 13:20:34 +03:00
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
|
|
|
|
localize ("authors.title", -1, true);
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
2013-10-04 13:20:34 +03:00
|
|
|
public function testLocalizeUnknown ()
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "aa";
|
|
|
|
$this->assertEquals ("Authors", localize ("authors.title", -1, true));
|
2013-12-05 11:52:51 +02:00
|
|
|
|
2013-10-04 13:20:34 +03:00
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
|
|
|
|
localize ("authors.title", -1, true);
|
|
|
|
}
|
2013-12-05 11:52:51 +02:00
|
|
|
|
2015-02-05 22:44:00 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider providerGetLangAndTranslationFile
|
|
|
|
*/
|
|
|
|
public function testGetLangAndTranslationFile ($acceptLanguage, $result)
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = $acceptLanguage;
|
|
|
|
list ($lang, $lang_file) = GetLangAndTranslationFile ();
|
|
|
|
$this->assertEquals ($result, $lang);
|
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
|
|
|
|
localize ("authors.title", -1, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerGetLangAndTranslationFile ()
|
|
|
|
{
|
|
|
|
return array (
|
|
|
|
array ("en", "en"),
|
|
|
|
array ("fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3", "fr"),
|
2015-02-06 21:55:02 +02:00
|
|
|
array ("fr-FR", "fr"),
|
2015-02-06 04:40:04 +02:00
|
|
|
array ("pt,en-us;q=0.7,en;q=0.3", "en"),
|
2015-02-06 21:55:02 +02:00
|
|
|
array ("pt-br,pt;q=0.8,en-us;q=0.5,en;q=0.3", "pt_BR"),
|
|
|
|
array ("pt-pt,pt;q=0.8,en;q=0.5,en-us;q=0.3", "pt_PT"),
|
2015-02-05 22:44:00 +02:00
|
|
|
array ("zl", "en"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerGetAcceptLanguages
|
|
|
|
*/
|
|
|
|
public function testGetAcceptLanguages ($acceptLanguage, $result)
|
|
|
|
{
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = $acceptLanguage;
|
2015-02-06 07:55:27 +02:00
|
|
|
$langs = array_keys(GetAcceptLanguages ());
|
|
|
|
$this->assertEquals ($result, $langs[0]);
|
2015-02-05 22:44:00 +02:00
|
|
|
|
|
|
|
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = "en";
|
|
|
|
localize ("authors.title", -1, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerGetAcceptLanguages ()
|
|
|
|
{
|
|
|
|
return array (
|
|
|
|
array ("en", "en"),
|
|
|
|
array ("en-US", "en_US"),
|
2015-02-06 21:55:02 +02:00
|
|
|
array ("fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3", "fr"), // French locale with Firefox
|
|
|
|
array ("fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4", "fr_FR"), // French locale with Chrome
|
|
|
|
array ("fr-FR", "fr_FR"), // French locale with IE11
|
2015-02-06 04:40:04 +02:00
|
|
|
array ("pt-br,pt;q=0.8,en-us;q=0.5,en;q=0.3", "pt_BR"),
|
2015-02-05 22:44:00 +02:00
|
|
|
array ("zl", "zl"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-11-26 18:17:22 +02:00
|
|
|
public function testBaseFunction () {
|
|
|
|
global $config;
|
2013-12-05 11:52:51 +02:00
|
|
|
|
2013-11-26 18:17:22 +02:00
|
|
|
$this->assertFalse (Base::isMultipleDatabaseEnabled ());
|
|
|
|
$this->assertEquals (array ("" => dirname(__FILE__) . "/BaseWithSomeBooks/"), Base::getDbList ());
|
|
|
|
|
|
|
|
$config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
|
|
|
|
"One book" => dirname(__FILE__) . "/BaseWithOneBook/");
|
|
|
|
|
|
|
|
$this->assertTrue (Base::isMultipleDatabaseEnabled ());
|
|
|
|
$this->assertEquals ("Some books", Base::getDbName (0));
|
|
|
|
$this->assertEquals ("One book", Base::getDbName (1));
|
|
|
|
$this->assertEquals ($config['calibre_directory'], Base::getDbList ());
|
|
|
|
}
|
2013-12-14 19:48:34 +02:00
|
|
|
|
|
|
|
public function testCheckDatabaseAvailability_1 () {
|
|
|
|
$this->assertTrue (Base::checkDatabaseAvailability ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckDatabaseAvailability_2 () {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
|
|
|
|
"One book" => dirname(__FILE__) . "/BaseWithOneBook/");
|
|
|
|
|
|
|
|
$this->assertTrue (Base::checkDatabaseAvailability ());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionMessage not found
|
|
|
|
*/
|
|
|
|
public function testCheckDatabaseAvailability_Exception1 () {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
|
|
|
|
"One book" => dirname(__FILE__) . "/OneBook/");
|
|
|
|
|
|
|
|
$this->assertTrue (Base::checkDatabaseAvailability ());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Exception
|
|
|
|
* @expectedExceptionMessage not found
|
|
|
|
*/
|
|
|
|
public function testCheckDatabaseAvailability_Exception2 () {
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/SomeBooks/",
|
|
|
|
"One book" => dirname(__FILE__) . "/BaseWithOneBook/");
|
|
|
|
|
|
|
|
$this->assertTrue (Base::checkDatabaseAvailability ());
|
|
|
|
}
|
2014-05-03 18:57:06 +03:00
|
|
|
|
2015-10-29 22:10:55 +02:00
|
|
|
/*
|
|
|
|
Test normalized utf8 string according to unicode.org output
|
|
|
|
more here :
|
|
|
|
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
|
|
|
|
*/
|
2014-05-03 18:57:06 +03:00
|
|
|
public function testNormalizeUtf8String () {
|
2015-10-29 22:10:55 +02:00
|
|
|
$this->assertEquals ("AAAAAACEEEEIIIIOEOOOOOUUUUYaaaaaaceeeeiiiioedooooouuuuyyn",
|
|
|
|
normalizeUtf8String ("ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏŒÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïœðòóôõöùúûüýÿñ"));
|
2014-05-03 18:57:06 +03:00
|
|
|
}
|
2013-10-03 09:38:58 +03:00
|
|
|
}
|