diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2015-11-04 05:20:45 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2015-11-04 07:28:41 +0300 |
commit | 68d3487a0f7807e58cc7efdacfb561a2a000fa2c (patch) | |
tree | 061f0c7be4b799efe250b37fa7412fbc15477275 | |
parent | da827372f3721461fab8cb8f524f8de84a268ddd (diff) |
refs #9163 use mysql instead of sqlite in tracker tests
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | tests/README.md | 2 | ||||
-rw-r--r-- | tests/javascript/README.md | 7 | ||||
-rw-r--r-- | tests/javascript/SQLite.php | 70 | ||||
-rw-r--r-- | tests/javascript/index.php | 22 | ||||
-rw-r--r-- | tests/javascript/piwik.php | 71 |
6 files changed, 54 insertions, 120 deletions
diff --git a/.travis.yml b/.travis.yml index c3be254c34..55509e6ea1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -37,7 +37,7 @@ env: - TEST_SUITE=IntegrationTests MYSQL_ADAPTER=PDO_MYSQL - TEST_SUITE=UnitTests MYSQL_ADAPTER=PDO_MYSQL # Javascript tests - - TEST_SUITE=JavascriptTests MYSQL_ADAPTER=PDO_MYSQL SKIP_COMPOSER_INSTALL=1 + - TEST_SUITE=JavascriptTests MYSQL_ADAPTER=PDO_MYSQL - TEST_SUITE=AngularJSTests MYSQL_ADAPTER=PDO_MYSQL SKIP_COMPOSER_INSTALL=1 # All tests after another - TEST_SUITE=AllTests MYSQL_ADAPTER=PDO_MYSQL diff --git a/tests/README.md b/tests/README.md index aaa18d6d34..49a4882f01 100644 --- a/tests/README.md +++ b/tests/README.md @@ -31,7 +31,7 @@ To execute the tests: 1. To install PHPUnit, run `php composer.phar install --dev` in the Piwik root directory. -2. Ensure the `[database_tests]` section in `piwik/config/config.php.ini` is set up correctly, +2. Ensure the `[database_tests]` section in `piwik/config/config.ini.php` is set up correctly, i.e. with the correct password to prevent the following error: `SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)` diff --git a/tests/javascript/README.md b/tests/javascript/README.md index 5852407301..9e34fdff07 100644 --- a/tests/javascript/README.md +++ b/tests/javascript/README.md @@ -1,10 +1,9 @@ # JavaScript Tests ## Setup -Javascript integration tests require sqlite: - * ensure this PHP extension is enabled to make sure you run all tests apt-get install php5-sqlite - * Then please create an empty file `enable_sqlite` in `tests/javascript/enable_sqlite` - * Re-execute the tests and make sure the "missing sqlite" error message does not display +Javascript integration tests require an installed Piwik and ensure the `[database_tests]` section in `piwik/config/config.ini.php` is set up correctly, i.e. with th correct password to prevent the following error: `SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)` + +The tests will create a database named `tracker_tests` and store several tracking requests in it. ## Execute diff --git a/tests/javascript/SQLite.php b/tests/javascript/SQLite.php deleted file mode 100644 index 5b4a3b3343..0000000000 --- a/tests/javascript/SQLite.php +++ /dev/null @@ -1,70 +0,0 @@ -<?php -/*! - * Piwik - free/libre analytics platform - * - * SQLite shim - * - * @link http://piwik.org - * @license http://www.opensource.org/licenses/bsd-license.php Simplified BSD - */ -if (class_exists('SQLite3')) -{ - class SQLite extends SQLite3 - { - public function __construct($filename) - { - parent::__construct($filename); - - // for backward compatibility - if (version_compare(PHP_VERSION, '5.3.3') > 0) - { - $this->busyTimeout(60000); - } - } - - public function query_array($sql) - { - $result = parent::query($sql); - - $rows = array(); - while ($res = $result->fetchArray(SQLITE3_ASSOC)) - { - $rows[] = $res; - } - return $rows; - } - } -} -else if (extension_loaded('sqlite')) -{ - class SQLite - { - private $handle; - - public function __construct($filename) - { - $this->handle = sqlite_open($filename); - } - - public function query_array($sql) - { - return sqlite_array_query($this->handle, $sql); - } - - public function exec($sql) - { - return sqlite_exec($this->handle, $sql); - } - - public function changes() - { - return sqlite_changes($this->handle); - } - - public function close() - { - sqlite_close($this->handle); - unset($this->handle); - } - } -} diff --git a/tests/javascript/index.php b/tests/javascript/index.php index 702233d622..0cfd028493 100644 --- a/tests/javascript/index.php +++ b/tests/javascript/index.php @@ -5,7 +5,14 @@ <meta charset="utf-8"> <title>piwik.js: Unit Tests</title> <?php -require_once(dirname(__FILE__).'/SQLite.php'); +$root = dirname(__FILE__) . '/../..'; + +try { + $mysql = include_once $root . "/tests/PHPUnit/bootstrap.php"; +} catch (Exception $e) { + echo 'alert("' . $e->getMessage() . '")'; + $mysql = false; +} if(file_exists("stub.tpl")) { echo file_get_contents("stub.tpl"); @@ -22,17 +29,8 @@ function getHeartbeatToken() { return "<?php $token = md5(uniqid(mt_rand(), true)); echo $token; ?>"; } <?php -$sqlite = false; -if (file_exists("enable_sqlite")) { - if (class_exists('SQLite')) { - $sqlite = true; - } -} -if(!$sqlite) { - echo 'alert("WARNING: Javascript integration tests require sqlite, \n1) ensure this PHP extension is enabled to make sure you run all tests \napt-get install php5-sqlite \n2) Then please create an empty file enable_sqlite in tests/javascript/enable_sqlite \n3) Re-execute this page and make sure this popup does not display ");'; -} -if ($sqlite) { +if ($mysql) { echo ' var _paq = _paq || []; @@ -2710,7 +2708,7 @@ function PiwikTest() { }); <?php -if ($sqlite) { +if ($mysql) { ?> module("request", { diff --git a/tests/javascript/piwik.php b/tests/javascript/piwik.php index 73fbedb232..77dbdec349 100644 --- a/tests/javascript/piwik.php +++ b/tests/javascript/piwik.php @@ -1,7 +1,31 @@ <?php // piwik.php test harness -require_once(dirname(__FILE__).'/SQLite.php'); +if (!defined('PIWIK_DOCUMENT_ROOT')) { + define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) . '/../..'); +} + +define('PIWIK_INCLUDE_PATH', PIWIK_DOCUMENT_ROOT); + + +require_once PIWIK_INCLUDE_PATH . '/core/bootstrap.php'; + +$environment = new \Piwik\Application\Environment(null); +$environment->init(); +$dbConfig = Piwik\Config::getInstance()->database_tests; +$dbConfig['dbname'] = 'tracker_tests'; + +try { + Piwik\Db::createDatabaseObject($dbConfig); +} catch (Exception $e) { + $dbInfosConnectOnly = $dbConfig; + $dbInfosConnectOnly['dbname'] = null; + Piwik\Db::createDatabaseObject($dbInfosConnectOnly); + Piwik\DbHelper::createDatabase($dbConfig['dbname']); + Piwik\Db::createDatabaseObject($dbConfig); +} + +$db = Piwik\Db::get(); function sendWebBug() { $trans_gif_64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="; @@ -14,25 +38,14 @@ function isPost() return $_SERVER['REQUEST_METHOD'] == 'POST'; } -if (!file_exists("enable_sqlite")) { +if (!Piwik\Db::hasDatabaseObject()) { sendWebBug(); exit; } -if (!class_exists('SQLite')) { - sendWebBug(); - exit; -} - -$sqlite = new SQLite( 'unittest2.dbf' ); -if (!$sqlite) { - header("HTTP/1.0 500 Internal Server Error"); - exit; -} - -function getNextRequestId($sqlite, $token) +function getNextRequestId($db, $token) { - $requests = $sqlite->query_array("SELECT uri FROM requests WHERE token = \"$token\""); + $requests = $db->fetchAll("SELECT uri FROM requests WHERE token = \"$token\""); if (empty($requests)) { return 1; @@ -41,17 +54,14 @@ function getNextRequestId($sqlite, $token) return count($requests) + 1; } -if (filesize(dirname(__FILE__).'/unittest2.dbf') == 0) -{ - try { - $query = @$sqlite->exec( 'CREATE TABLE requests (requestid TEXT, token TEXT, ip TEXT, ts TEXT, uri TEXT, referer TEXT, ua TEXT)' ); - } catch (Exception $e) { - header("HTTP/1.0 500 Internal Server Error"); - exit; - } +try { + $db->query( 'CREATE TABLE IF NOT EXISTS `requests` (requestid TEXT, token TEXT, ip TEXT, ts TEXT, uri TEXT, referer TEXT, ua TEXT) DEFAULT CHARSET=utf8' ); +} catch (Exception $e) { + header("HTTP/1.0 500 Internal Server Error"); + exit; } -function logRequest($sqlite, $uri, $data) { +function logRequest($db, $uri, $data) { $ip = $_SERVER['REMOTE_ADDR']; $ts = $_SERVER['REQUEST_TIME']; @@ -62,9 +72,9 @@ function logRequest($sqlite, $uri, $data) { $token = isset($data['token']) ? $data['token'] : ''; - $id = getNextRequestId($sqlite, $token); + $id = getNextRequestId($db, $token); - $query = $sqlite->exec("INSERT INTO requests (requestid, token, ip, ts, uri, referer, ua) VALUES (\"$id\", \"$token\", \"$ip\", \"$ts\", \"$uri\", \"$referrer\", \"$ua\")"); + $query = $db->query("INSERT INTO requests (requestid, token, ip, ts, uri, referer, ua) VALUES (\"$id\", \"$token\", \"$ip\", \"$ts\", \"$uri\", \"$referrer\", \"$ua\")"); return $query; } @@ -76,7 +86,7 @@ if (isset($_GET['requests'])) { echo "<html><head><title>$token</title></head><body>\n"; // $result = $sqlite->query_array("SELECT uri FROM requests"); - $result = @$sqlite->query_array("SELECT uri FROM requests WHERE token = \"$token\" AND ua = \"$ua\" ORDER BY ts ASC, requestid ASC"); + $result = @$db->fetchAll("SELECT uri FROM requests WHERE token = \"$token\" AND ua = \"$ua\" ORDER BY ts ASC, requestid ASC"); if ($result !== false) { $nofRows = count($result); echo "<span>$nofRows</span>\n"; @@ -107,7 +117,7 @@ if (isset($_GET['requests'])) { $data = array('token' => $matches[1]); } - $query = $query && logRequest($sqlite, $uri . $request, $data); + $query = $query && logRequest($db, $uri . $request, $data); } } else { @@ -115,16 +125,13 @@ if (isset($_GET['requests'])) { $uri .= '?' . file_get_contents('php://input'); } - $query = logRequest($sqlite, $uri, $data); + $query = logRequest($db, $uri, $data); } if (!$query) { header("HTTP/1.0 500 Internal Server Error"); } else { -// echo 'Number of rows modified: ', $sqlite->changes(); sendWebBug(); } } } - -$sqlite->close(); |