aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/bootstrap.php5
-rw-r--r--tests/dbal/select_test.php2
-rw-r--r--tests/dbal/write_test.php2
-rw-r--r--tests/mock/cache.php11
-rw-r--r--tests/mock/session_testable.php11
-rw-r--r--tests/network/checkdnsrr_test.php2
-rw-r--r--tests/random/gen_rand_string_test.php2
-rw-r--r--tests/regex/censor_test.php2
-rw-r--r--tests/regex/email_test.php2
-rw-r--r--tests/regex/ipv4_test.php2
-rw-r--r--tests/regex/ipv6_test.php2
-rw-r--r--tests/regex/url_test.php2
-rw-r--r--tests/request/request_var_test.php4
-rw-r--r--tests/security/extract_current_page_test.php6
-rw-r--r--tests/security/redirect_test.php6
-rw-r--r--tests/session/continue_test.php117
-rw-r--r--tests/session/fixtures/sessions_empty.xml6
-rw-r--r--tests/session/init_test.php56
-rw-r--r--tests/session/session_continue.php117
-rw-r--r--tests/session/session_init.php76
-rw-r--r--tests/session/testable_factory.php171
-rw-r--r--tests/template/template_test.php251
-rw-r--r--tests/template/templates/includephp.html2
-rw-r--r--tests/template/templates/loop_nested.html4
-rw-r--r--tests/test_framework/phpbb_database_test_case.php293
-rw-r--r--tests/test_framework/phpbb_database_test_connection_manager.php346
-rw-r--r--tests/text_processing/make_clickable_test.php4
-rw-r--r--tests/user/lang_test.php58
-rw-r--r--tests/utf/normalizer_test.php10
-rw-r--r--tests/utf/utf8_clean_string_test.php2
-rw-r--r--tests/utf/utf8_wordwrap_test.php2
31 files changed, 868 insertions, 708 deletions
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 1fba323277..6f3c93a374 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -12,6 +12,10 @@ $phpbb_root_path = 'phpBB/';
$phpEx = 'php';
$table_prefix = 'phpbb_';
+if (!defined('E_DEPRECATED'))
+{
+ define('E_DEPRECATED', 8192);
+}
error_reporting(E_ALL & ~E_DEPRECATED);
// If we are on PHP >= 6.0.0 we do not need some code
@@ -30,3 +34,4 @@ require_once $phpbb_root_path . 'includes/constants.php';
require_once 'test_framework/phpbb_test_case_helpers.php';
require_once 'test_framework/phpbb_test_case.php';
require_once 'test_framework/phpbb_database_test_case.php';
+require_once 'test_framework/phpbb_database_test_connection_manager.php';
diff --git a/tests/dbal/select_test.php b/tests/dbal/select_test.php
index 987de5cbff..533416f14b 100644
--- a/tests/dbal/select_test.php
+++ b/tests/dbal/select_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_dbal_select_test extends phpbb_database_test_case
{
diff --git a/tests/dbal/write_test.php b/tests/dbal/write_test.php
index a24b6efcc4..4709d45fa5 100644
--- a/tests/dbal/write_test.php
+++ b/tests/dbal/write_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_dbal_write_test extends phpbb_database_test_case
{
diff --git a/tests/mock/cache.php b/tests/mock/cache.php
index 2ac46f7090..738d1eaaba 100644
--- a/tests/mock/cache.php
+++ b/tests/mock/cache.php
@@ -53,9 +53,16 @@ class phpbb_mock_cache
$test->assertEquals($data, $this->data[$var_name]);
}
- public function check(PHPUnit_Framework_Assert $test, $data)
+ public function check(PHPUnit_Framework_Assert $test, $data, $ignore_db_info = true)
{
- $test->assertEquals($data, $this->data);
+ $cache_data = $this->data;
+
+ if ($ignore_db_info)
+ {
+ unset($cache_data['mysqli_version']);
+ }
+
+ $test->assertEquals($data, $cache_data);
}
}
diff --git a/tests/mock/session_testable.php b/tests/mock/session_testable.php
index 2d7d42f82a..47089cb94b 100644
--- a/tests/mock/session_testable.php
+++ b/tests/mock/session_testable.php
@@ -7,9 +7,16 @@
*
*/
-require_once '../phpBB/includes/functions.php';
-require_once '../phpBB/includes/session.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/session.php';
+/**
+* Extends the session class to overwrite the setting of cookies.
+*
+* The session class directly writes cookie headers making it impossible to
+* test it without warnings about sent headers. This class only stores cookie
+* data for later verification.
+*/
class phpbb_mock_session_testable extends session
{
private $_cookies = array();
diff --git a/tests/network/checkdnsrr_test.php b/tests/network/checkdnsrr_test.php
index 9410deaf64..5a756dcef8 100644
--- a/tests/network/checkdnsrr_test.php
+++ b/tests/network/checkdnsrr_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
/**
* @group slow
diff --git a/tests/random/gen_rand_string_test.php b/tests/random/gen_rand_string_test.php
index fa519f134c..115c55e4e2 100644
--- a/tests/random/gen_rand_string_test.php
+++ b/tests/random/gen_rand_string_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_random_gen_rand_string_test extends phpbb_test_case
{
diff --git a/tests/regex/censor_test.php b/tests/regex/censor_test.php
index 93c761c8d0..fa9104e71d 100644
--- a/tests/regex/censor_test.php
+++ b/tests/regex/censor_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_regex_censor_test extends phpbb_test_case
{
diff --git a/tests/regex/email_test.php b/tests/regex/email_test.php
index 5d6e207cbb..0695b801d5 100644
--- a/tests/regex/email_test.php
+++ b/tests/regex/email_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_regex_email_test extends phpbb_test_case
{
diff --git a/tests/regex/ipv4_test.php b/tests/regex/ipv4_test.php
index 735a2c4384..9829547508 100644
--- a/tests/regex/ipv4_test.php
+++ b/tests/regex/ipv4_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_regex_ipv4_test extends phpbb_test_case
{
diff --git a/tests/regex/ipv6_test.php b/tests/regex/ipv6_test.php
index 187588f861..1b2018403c 100644
--- a/tests/regex/ipv6_test.php
+++ b/tests/regex/ipv6_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_regex_ipv6_test extends phpbb_test_case
{
diff --git a/tests/regex/url_test.php b/tests/regex/url_test.php
index 246cbf549c..c3a336063a 100644
--- a/tests/regex/url_test.php
+++ b/tests/regex/url_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
class phpbb_regex_url_test extends phpbb_test_case
{
diff --git a/tests/request/request_var_test.php b/tests/request/request_var_test.php
index 0901b43920..fa17b1909f 100644
--- a/tests/request/request_var_test.php
+++ b/tests/request/request_var_test.php
@@ -7,8 +7,8 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
-require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php';
class phpbb_request_request_var_test extends phpbb_test_case
{
diff --git a/tests/security/extract_current_page_test.php b/tests/security/extract_current_page_test.php
index ff0ab4d1bb..71c7a3a397 100644
--- a/tests/security/extract_current_page_test.php
+++ b/tests/security/extract_current_page_test.php
@@ -7,10 +7,10 @@
*
*/
-require_once __DIR__ . '/base.php';
+require_once dirname(__FILE__) . '/base.php';
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
-require_once __DIR__ . '/../../phpBB/includes/session.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/session.php';
class phpbb_security_extract_current_page_test extends phpbb_security_test_base
{
diff --git a/tests/security/redirect_test.php b/tests/security/redirect_test.php
index c53414e7df..70ba8527b1 100644
--- a/tests/security/redirect_test.php
+++ b/tests/security/redirect_test.php
@@ -7,10 +7,10 @@
*
*/
-require_once __DIR__ . '/base.php';
+require_once dirname(__FILE__) . '/base.php';
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
-require_once __DIR__ . '/../../phpBB/includes/session.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/session.php';
class phpbb_security_redirect_test extends phpbb_security_test_base
{
diff --git a/tests/session/continue_test.php b/tests/session/continue_test.php
new file mode 100644
index 0000000000..3080121978
--- /dev/null
+++ b/tests/session/continue_test.php
@@ -0,0 +1,117 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2011 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+require_once dirname(__FILE__) . '/../mock/cache.php';
+require_once dirname(__FILE__) . '/testable_factory.php';
+
+class phpbb_session_continue_test extends phpbb_database_test_case
+{
+ public function getDataSet()
+ {
+ return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/sessions_full.xml');
+ }
+
+ static public function session_begin_attempts()
+ {
+ return array(
+ array(
+ 'bar_session', '4', 'user agent', '127.0.0.1',
+ array(
+ array('session_id' => 'anon_session', 'session_user_id' => 1),
+ array('session_id' => 'bar_session', 'session_user_id' => 4),
+ ),
+ array(),
+ 'If a request comes with a valid session id with matching user agent and IP, no new session should be created.',
+ ),
+ array(
+ 'anon_session', '4', 'user agent', '127.0.0.1',
+ array(
+ array('session_id' => '__new_session_id__', 'session_user_id' => 1), // use generated SID
+ array('session_id' => 'bar_session', 'session_user_id' => 4),
+ ),
+ array(
+ 'u' => array('1', null),
+ 'k' => array(null, null),
+ 'sid' => array('__new_session_id__', null),
+ ),
+ 'If a request comes with a valid session id and IP but different user id and user agent, a new anonymous session is created and the session matching the supplied session id is deleted.',
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider session_begin_attempts
+ */
+ public function test_session_begin_valid_session($session_id, $user_id, $user_agent, $ip, $expected_sessions, $expected_cookies, $message)
+ {
+ $db = $this->new_dbal();
+ $session_factory = new phpbb_session_testable_factory;
+ $session_factory->set_cookies(array(
+ '_sid' => $session_id,
+ '_u' => $user_id,
+ ));
+ $session_factory->merge_config_data(array(
+ 'session_length' => time(), // need to do this to allow sessions started at time 0
+ ));
+ $session_factory->merge_server_data(array(
+ 'HTTP_USER_AGENT' => $user_agent,
+ 'REMOTE_ADDR' => $ip,
+ ));
+
+ $session = $session_factory->get_session($db);
+ $session->page = array('page' => 'page', 'forum' => 0);
+
+ $session->session_begin();
+
+ $sql = 'SELECT session_id, session_user_id
+ FROM phpbb_sessions
+ ORDER BY session_user_id';
+
+ $expected_sessions = $this->replace_session($expected_sessions, $session->session_id);
+ $expected_cookies = $this->replace_session($expected_cookies, $session->session_id);
+
+ $this->assertSqlResultEquals(
+ $expected_sessions,
+ $sql,
+ $message
+ );
+
+ $session->check_cookies($this, $expected_cookies);
+
+ $session_factory->check($this);
+ }
+
+ /**
+ * Replaces recursively the value __new_session_id__ with the given session
+ * id.
+ *
+ * @param array $array An array of data
+ * @param string $session_id The new session id to use instead of the
+ * placeholder.
+ * @return array The input array with all occurances of __new_session_id__
+ * replaced.
+ */
+ public function replace_session($array, $session_id)
+ {
+ foreach ($array as $key => &$value)
+ {
+ if ($value === '__new_session_id__')
+ {
+ $value = $session_id;
+ }
+
+ if (is_array($value))
+ {
+ $value = $this->replace_session($value, $session_id);
+ }
+ }
+
+ return $array;
+ }
+}
diff --git a/tests/session/fixtures/sessions_empty.xml b/tests/session/fixtures/sessions_empty.xml
index 66fa585b18..f94337314e 100644
--- a/tests/session/fixtures/sessions_empty.xml
+++ b/tests/session/fixtures/sessions_empty.xml
@@ -16,4 +16,10 @@
<value>bar</value>
</row>
</table>
+ <table name="phpbb_sessions">
+ <column>session_id</column>
+ <column>session_user_id</column>
+ <column>session_ip</column>
+ <column>session_browser</column>
+ </table>
</dataset>
diff --git a/tests/session/init_test.php b/tests/session/init_test.php
new file mode 100644
index 0000000000..1181fab636
--- /dev/null
+++ b/tests/session/init_test.php
@@ -0,0 +1,56 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2011 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+require_once dirname(__FILE__) . '/../mock/cache.php';
+require_once dirname(__FILE__) . '/testable_factory.php';
+
+class phpbb_session_init_test extends phpbb_database_test_case
+{
+ public function getDataSet()
+ {
+ return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/sessions_empty.xml');
+ }
+
+ // also see security/extract_current_page.php
+
+ public function test_login_session_create()
+ {
+ $db = $this->new_dbal();
+ $session_factory = new phpbb_session_testable_factory;
+
+ $session = $session_factory->get_session($db);
+ $session->page = array('page' => 'page', 'forum' => 0);
+
+ $session->session_create(3);
+
+ $sql = 'SELECT session_user_id
+ FROM phpbb_sessions';
+
+ $this->assertSqlResultEquals(
+ array(array('session_user_id' => 3)),
+ $sql,
+ 'Check if exacly one session for user id 3 was created'
+ );
+
+ $cookie_expire = $session->time_now + 31536000; // default is one year
+
+ $session->check_cookies($this, array(
+ 'u' => array(null, $cookie_expire),
+ 'k' => array(null, $cookie_expire),
+ 'sid' => array($session->session_id, $cookie_expire),
+ ));
+
+ global $SID, $_SID;
+ $this->assertEquals($session->session_id, $_SID);
+ $this->assertEquals('?sid=' . $session->session_id, $SID);
+
+ $session_factory->check($this);
+ }
+}
+
diff --git a/tests/session/session_continue.php b/tests/session/session_continue.php
deleted file mode 100644
index 58956c18a9..0000000000
--- a/tests/session/session_continue.php
+++ /dev/null
@@ -1,117 +0,0 @@
-<?php
-/**
-*
-* @package testing
-* @copyright (c) 2008 phpBB Group
-* @license http://opensource.org/licenses/gpl-license.php GNU Public License
-*
-*/
-
-require_once 'test_framework/framework.php';
-require_once 'mock/cache.php';
-require_once 'mock/session_testable.php';
-
-class phpbb_session_continue_test extends phpbb_database_test_case
-{
- public function getDataSet()
- {
- return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/sessions_full.xml');
- }
-
- static public function session_begin_attempts()
- {
- return array(
- array(
- 'bar_session', '4', 'user agent',
- array(
- array('session_id' => 'anon_session', 'session_user_id' => 1),
- array('session_id' => 'bar_session', 'session_user_id' => 4)
- ),
- array(),
- 'Check if no new session was created',
- ),
- array(
- 'anon_session', '4', 'user agent',
- array(
- array('session_id' => 'bar_session', 'session_user_id' => 4),
- array('session_id' => null, 'session_user_id' => 1) // use generated SID
- ),
- array(
- 'u' => array('1', null),
- 'k' => array(null, null),
- 'sid' => array($_SID, null),
- ),
- 'Check if an anonymous new session was created',
- ),
- );
- }
-
- /**
- * @dataProvider session_begin_attempts
- */
- public function test_session_begin_valid_session($session_id, $user_id, $user_agent, $expected_sessions, $expected_cookies, $message)
- {
- $session = new phpbb_mock_session_testable;
- $session->page = array('page' => 'page', 'forum' => 0);
-
- // set up all the global variables used in session_create
- global $SID, $_SID, $db, $config, $cache;
-
- $config = $this->get_config();
- $db = $this->new_dbal();
- $cache_data = array(
- '_bots' => array(),
- );
- $cache = new phpbb_mock_cache;
- $SID = $_SID = null;
-
- $_COOKIE['_sid'] = $session_id;
- $_COOKIE['_u'] = $user_id;
- $_SERVER['HTTP_USER_AGENT'] = $user_agent;
-
- $config['session_length'] = time(); // need to do this to allow sessions started at time 0
- $session->session_begin();
-
- $sql = 'SELECT session_id, session_user_id
- FROM phpbb_sessions';
-
- // little tickery to allow using a dataProvider with dynamic expected result
- foreach ($expected_sessions as $i => $s)
- {
- if (is_null($s['session_id']))
- {
- $expected_sessions[$i]['session_id'] = $session->session_id;
- }
- }
-
- $this->assertResultEquals(
- $sql,
- $expected_sessions,
- 'Check if no new session was created'
- );
-
- $session->check_cookies($this, $expected_cookies);
-
- $cache->check($this, $cache_data);
- }
- static public function get_config()
- {
- return array(
- 'allow_autologin' => false,
- 'auth_method' => 'db',
- 'forwarded_for_check' => true,
- 'active_sessions' => 0, // disable
- 'rand_seed' => 'foo',
- 'rand_seed_last_update' => 0,
- 'max_autologin_time' => 0,
- 'session_length' => 100,
- 'form_token_lifetime' => 100,
- 'cookie_name' => '',
- 'limit_load' => 0,
- 'limit_search_load' => 0,
- 'ip_check' => 3,
- 'browser_check' => 1,
- );
- }
-}
-
diff --git a/tests/session/session_init.php b/tests/session/session_init.php
deleted file mode 100644
index f6fa564880..0000000000
--- a/tests/session/session_init.php
+++ /dev/null
@@ -1,76 +0,0 @@
-<?php
-/**
-*
-* @package testing
-* @copyright (c) 2008 phpBB Group
-* @license http://opensource.org/licenses/gpl-license.php GNU Public License
-*
-*/
-
-require_once 'test_framework/framework.php';
-require_once 'mock/cache.php';
-require_once 'mock/session_testable.php';
-
-class phpbb_session_init_test extends phpbb_database_test_case
-{
- public function getDataSet()
- {
- return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/sessions_empty.xml');
- }
-
- // also see security/extract_current_page.php
-
- public function test_login_session_create()
- {
- $session = new phpbb_mock_session_testable;
- $session->page = array('page' => 'page', 'forum' => 0);
-
- // set up all the global variables used in session_create
- global $SID, $_SID, $db, $config, $cache;
-
- $config = $this->get_config();
- $db = $this->new_dbal();
- $cache_data = array(
- '_bots' => array(),
- );
- $cache = new phpbb_mock_cache;
- $SID = $_SID = null;
-
- $session->session_create(3);
-
- $sql = 'SELECT session_user_id
- FROM phpbb_sessions';
-
- $this->assertResultEquals(
- $sql,
- array(array('session_user_id' => 3)),
- 'Check if exacly one session for user id 3 was created'
- );
-
- $cookie_expire = $session->time_now + (($config['max_autologin_time']) ? 86400 * (int) $config['max_autologin_time'] : 31536000);
-
- $session->check_cookies($this, array(
- 'u' => array(null, $cookie_expire),
- 'k' => array(null, $cookie_expire),
- 'sid' => array($_SID, $cookie_expire),
- ));
-
- $cache->check($this, $cache_data);
- }
-
- static public function get_config()
- {
- return array(
- 'allow_autologin' => false,
- 'auth_method' => 'db',
- 'forwarded_for_check' => true,
- 'active_sessions' => 0, // disable
- 'rand_seed' => 'foo',
- 'rand_seed_last_update' => 0,
- 'max_autologin_time' => 0,
- 'session_length' => 100,
- 'form_token_lifetime' => 100,
- );
- }
-}
-
diff --git a/tests/session/testable_factory.php b/tests/session/testable_factory.php
new file mode 100644
index 0000000000..f3ef19a257
--- /dev/null
+++ b/tests/session/testable_factory.php
@@ -0,0 +1,171 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2011 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+require_once dirname(__FILE__) . '/../mock/session_testable.php';
+
+/**
+* This class exists to setup an instance of phpbb's session class for testing.
+*
+* The session class has rather complex dependencies, so in order to make its
+* tests more * understandable and to make its dependencies more visible this
+* factory class sets up all the necessary global state & variable contents.
+*/
+class phpbb_session_testable_factory
+{
+ protected $config_data;
+ protected $cache_data;
+ protected $cookies;
+
+ protected $config;
+ protected $cache;
+
+ /**
+ * Initialises the factory with a set of default config and cache values.
+ */
+ public function __construct()
+ {
+ // default configuration values
+ $this->config_data = array(
+ 'allow_autologin' => false,
+ 'auth_method' => 'db',
+ 'forwarded_for_check' => true,
+ 'active_sessions' => 0, // disable
+ 'rand_seed' => 'foo',
+ 'rand_seed_last_update' => 0,
+ 'max_autologin_time' => 0,
+ 'session_length' => 100,
+ 'form_token_lifetime' => 100,
+ 'cookie_name' => '',
+ 'limit_load' => 0,
+ 'limit_search_load' => 0,
+ 'ip_check' => 3,
+ 'browser_check' => 1,
+ );
+
+ $this->cache_data = array(
+ '_bots' => array(),
+ );
+
+ $this->cookies = array();
+
+ $this->server_data = $_SERVER;
+ }
+
+ /**
+ * Retrieve the configured session class instance
+ *
+ * @param dbal $dbal The database connection to use for session data
+ * @return phpbb_mock_session_testable A session instance
+ */
+ public function get_session(dbal $dbal)
+ {
+ // set up all the global variables used by session
+ global $SID, $_SID, $db, $config, $cache;
+
+ $config = $this->config = $this->get_config_data();
+ $db = $dbal;
+
+ $cache = $this->cache = new phpbb_mock_cache($this->get_cache_data());
+ $SID = $_SID = null;
+
+ $_COOKIE = $this->cookies;
+ $_SERVER = $this->server_data;
+
+ $session = new phpbb_mock_session_testable;
+ return $session;
+ }
+
+ /**
+ * Set the cookies which should be present in the request data.
+ *
+ * @param array $cookies The cookie data, structured like $_COOKIE contents.
+ */
+ public function set_cookies(array $cookies)
+ {
+ $this->cookies = $cookies;
+ }
+
+ /**
+ * Check if the cache used for the generated session contains correct data.
+ *
+ * @param PHPUnit_Framework_Assert $test The test case to call assert methods
+ * on
+ */
+ public function check(PHPUnit_Framework_Assert $test)
+ {
+ $this->cache->check($test, $this->get_cache_data());
+ }
+
+ /**
+ * Merge config data with the current config data to be supplied to session.
+ *
+ * New values overwrite new ones.
+ *
+ * @param array $config_data The config data to merge with previous data
+ */
+ public function merge_config_data(array $config_data)
+ {
+ $this->config_data = array_merge($this->config_data, $config_data);
+ }
+
+ /**
+ * Retrieve the entire config data to be passed to the session.
+ *
+ * @return array Configuration
+ */
+ public function get_config_data()
+ {
+ return $this->config_data;
+ }
+
+ /**
+ * Merge the cache contents with more data.
+ *
+ * New values overwrite old ones.
+ *
+ * @param array $cache_data The additional cache data
+ */
+ public function merge_cache_data(array $cache_data)
+ {
+ $this->cache_data = array_merge($this->cache_data, $cache_data);
+ }
+
+ /**
+ * Retrieve the entire cache data to be passed to the session.
+ *
+ * @return array Cache contents
+ */
+ public function get_cache_data()
+ {
+ return $this->cache_data;
+ }
+
+ /**
+ * Merge the current server info ($_SERVER) with more data.
+ *
+ * New values overwrite old ones.
+ *
+ * @param array $server_data The additional server variables
+ */
+ public function merge_server_data($server_data)
+ {
+ return $this->server_data = array_merge($this->server_data, $server_data);
+ }
+
+ /**
+ * Retrieve all server variables to be passed to the session.
+ *
+ * @return array Server variables
+ */
+ public function get_server_data()
+ {
+ return $this->server_data;
+ }
+}
+
diff --git a/tests/template/template_test.php b/tests/template/template_test.php
index 35df17e4c6..33c82d53ad 100644
--- a/tests/template/template_test.php
+++ b/tests/template/template_test.php
@@ -7,8 +7,8 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
-require_once __DIR__ . '/../../phpBB/includes/template.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/template.php';
class phpbb_template_template_test extends phpbb_test_case
{
@@ -343,8 +343,7 @@ class phpbb_template_template_test extends phpbb_test_case
*/
public function test_template($file, array $vars, array $block_vars, array $destroy, $expected)
{
- global $phpEx;
- $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.' . $phpEx;
+ $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.php';
$this->assertFileNotExists($cache_file);
@@ -394,11 +393,9 @@ class phpbb_template_template_test extends phpbb_test_case
public function test_php()
{
- global $phpEx;
-
$GLOBALS['config']['tpl_allow_php'] = true;
- $cache_file = $this->template->cachepath . 'php.html.' . $phpEx;
+ $cache_file = $this->template->cachepath . 'php.html.php';
$this->assertFileNotExists($cache_file);
@@ -409,21 +406,14 @@ class phpbb_template_template_test extends phpbb_test_case
public function test_includephp()
{
- $this->markTestIncomplete('Include PHP test file paths are broken');
-
$GLOBALS['config']['tpl_allow_php'] = true;
- $cache_file = $this->template->cachepath . 'includephp.html.' . PHP_EXT;
-
- $cwd = getcwd();
- chdir(dirname(__FILE__) . '/templates');
+ $cache_file = $this->template->cachepath . 'includephp.html.php';
$this->run_template('includephp.html', array(), array(), array(), 'testing included php', $cache_file);
$this->template->set_filenames(array('test' => 'includephp.html'));
- $this->assertEquals('testing included php', $this->display('test'), "Testing $file");
-
- chdir($cwd);
+ $this->assertEquals('testing included php', $this->display('test'), "Testing INCLUDEPHP");
$GLOBALS['config']['tpl_allow_php'] = false;
}
@@ -437,17 +427,16 @@ class phpbb_template_template_test extends phpbb_test_case
false,
'insert',
<<<EOT
-outer - 0/4 - before
-outer - 1/4
-middle - 0/2
-middle - 1/2
-outer - 2/4
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 3/4
-middle - 0/2
-middle - 1/2
+outer - 0 - before
+outer - 1
+middle - 0
+middle - 1
+outer - 2
+middle - 0
+middle - 1
+outer - 3
+middle - 0
+middle - 1
EOT
,
'Test inserting before on top level block',
@@ -458,17 +447,16 @@ EOT
true,
'insert',
<<<EOT
-outer - 0/4
-middle - 0/2
-middle - 1/2
-outer - 1/4
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 2/4
-middle - 0/2
-middle - 1/2
-outer - 3/4 - after
+outer - 0
+middle - 0
+middle - 1
+outer - 1
+middle - 0
+middle - 1
+outer - 2
+middle - 0
+middle - 1
+outer - 3 - after
EOT
,
'Test inserting after on top level block',
@@ -479,17 +467,16 @@ EOT
1,
'insert',
<<<EOT
-outer - 0/4
-middle - 0/2
-middle - 1/2
-outer - 1/4 - pos #1
-outer - 2/4
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 3/4
-middle - 0/2
-middle - 1/2
+outer - 0
+middle - 0
+middle - 1
+outer - 1 - pos #1
+outer - 2
+middle - 0
+middle - 1
+outer - 3
+middle - 0
+middle - 1
EOT
,
'Test inserting at 1 on top level block',
@@ -500,172 +487,27 @@ EOT
0,
'change',
<<<EOT
-outer - 0/3 - pos #1
-middle - 0/2
-middle - 1/2
-outer - 1/3
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 2/3
-middle - 0/2
-middle - 1/2
+outer - 0 - pos #1
+middle - 0
+middle - 1
+outer - 1
+middle - 0
+middle - 1
+outer - 2
+middle - 0
+middle - 1
EOT
,
'Test inserting at 1 on top level block',
),
- array(
- 'outer[0].middle',
- array('VARIABLE' => 'before'),
- false,
- 'insert',
- <<<EOT
-outer - 0/3
-middle - 0/3 - before
-middle - 1/3
-middle - 2/3
-outer - 1/3
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 2/3
-middle - 0/2
-middle - 1/2
-EOT
-,
- 'Test inserting before on nested block',
- ),
- array(
- 'outer[0].middle',
- array('VARIABLE' => 'after'),
- true,
- 'insert',
- <<<EOT
-outer - 0/3
-middle - 0/3
-middle - 1/3
-middle - 2/3 - after
-outer - 1/3
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 2/3
-middle - 0/2
-middle - 1/2
-EOT
-,
- 'Test inserting after on nested block',
- ),
- array(
- 'outer[0].middle',
- array('VARIABLE' => 'pos #1'),
- 1,
- 'insert',
- <<<EOT
-outer - 0/3
-middle - 0/3
-middle - 1/3 - pos #1
-middle - 2/3
-outer - 1/3
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 2/3
-middle - 0/2
-middle - 1/2
-EOT
-,
- 'Test inserting at pos 1 on nested block',
- ),
- array(
- 'outer[1].middle',
- array('VARIABLE' => 'before'),
- false,
- 'insert',
- <<<EOT
-outer - 0/3
-middle - 0/2
-middle - 1/2
-outer - 1/3
-middle - 0/4 - before
-middle - 1/4
-middle - 2/4
-middle - 3/4
-outer - 2/3
-middle - 0/2
-middle - 1/2
-EOT
-,
- 'Test inserting before on nested block (pos 1)',
- ),
- array(
- 'outer[].middle',
- array('VARIABLE' => 'before'),
- false,
- 'insert',
- <<<EOT
-outer - 0/3
-middle - 0/2
-middle - 1/2
-outer - 1/3
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 2/3
-middle - 0/3 - before
-middle - 1/3
-middle - 2/3
-EOT
-,
- 'Test inserting before on nested block (end)',
- ),
- array(
- 'outer.middle',
- array('VARIABLE' => 'before'),
- false,
- 'insert',
- <<<EOT
-outer - 0/3
-middle - 0/2
-middle - 1/2
-outer - 1/3
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 2/3
-middle - 0/3 - before
-middle - 1/3
-middle - 2/3
-EOT
-,
- 'Test inserting before on nested block (end)',
- ),
);
}
-/*
- <<<EOT
-outer - 0/3
-middle - 0/2
-middle - 1/2
-outer - 1/3
-middle - 0/3
-middle - 1/3
-middle - 2/3
-outer - 2/3
-middle - 0/2
-middle - 1/2
-EOT
-,
-*/
-
/**
* @dataProvider alter_block_array_data
*/
public function test_alter_block_array($alter_block, array $vararray, $key, $mode, $expect, $description)
{
- $this->markTestIncomplete('Alter Block Test is broken');
-
$this->template->set_filenames(array('test' => 'loop_nested.html'));
// @todo Change this
@@ -675,12 +517,11 @@ EOT
$this->template->assign_block_vars('outer', array());
$this->template->assign_block_vars('outer.middle', array());
$this->template->assign_block_vars('outer.middle', array());
- $this->template->assign_block_vars('outer.middle', array());
$this->template->assign_block_vars('outer', array());
$this->template->assign_block_vars('outer.middle', array());
$this->template->assign_block_vars('outer.middle', array());
- $this->assertEquals("outer - 0/3\nmiddle - 0/2\nmiddle - 1/2\nouter - 1/3\nmiddle - 0/3\nmiddle - 1/3\nmiddle - 2/3\nouter - 2/3\nmiddle - 0/2\nmiddle - 1/2", $this->display('test'), 'Ensuring template is built correctly before modification');
+ $this->assertEquals("outer - 0\nmiddle - 0\nmiddle - 1\nouter - 1\nmiddle - 0\nmiddle - 1\nouter - 2\nmiddle - 0\nmiddle - 1", $this->display('test'), 'Ensuring template is built correctly before modification');
$this->template->alter_block_array($alter_block, $vararray, $key, $mode);
$this->assertEquals($expect, $this->display('test'), $description);
diff --git a/tests/template/templates/includephp.html b/tests/template/templates/includephp.html
index 117d4273f0..70ebdac0d0 100644
--- a/tests/template/templates/includephp.html
+++ b/tests/template/templates/includephp.html
@@ -1 +1 @@
-<!-- INCLUDEPHP ../templates/_dummy_include.php.inc -->
+<!-- INCLUDEPHP ../tests/template/templates/_dummy_include.php.inc -->
diff --git a/tests/template/templates/loop_nested.html b/tests/template/templates/loop_nested.html
index 571df97b4c..9b251cd453 100644
--- a/tests/template/templates/loop_nested.html
+++ b/tests/template/templates/loop_nested.html
@@ -1,8 +1,8 @@
<!-- BEGIN outer -->
- {outer.S_BLOCK_NAME} - {outer.S_ROW_NUM}/{outer.S_NUM_ROWS}<!-- IF outer.VARIABLE --> - {outer.VARIABLE}<!-- ENDIF -->
+ outer - {outer.S_ROW_COUNT}<!-- IF outer.VARIABLE --> - {outer.VARIABLE}<!-- ENDIF -->
<!-- BEGIN middle -->
- {middle.S_BLOCK_NAME} - {middle.S_ROW_NUM}/{middle.S_NUM_ROWS}<!-- IF middle.VARIABLE --> - {middle.VARIABLE}<!-- ENDIF -->
+ middle - {middle.S_ROW_COUNT}<!-- IF middle.VARIABLE --> - {middle.VARIABLE}<!-- ENDIF -->
<!-- END middle -->
<!-- END outer -->
diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php
index a325855da4..e1b368dcea 100644
--- a/tests/test_framework/phpbb_database_test_case.php
+++ b/tests/test_framework/phpbb_database_test_case.php
@@ -9,7 +9,7 @@
abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_TestCase
{
- private static $already_connected;
+ static private $already_connected;
protected $test_case_helpers;
@@ -38,66 +38,6 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
return $this->test_case_helpers;
}
- public function get_dbms_data($dbms)
- {
- $available_dbms = array(
- 'firebird' => array(
- 'SCHEMA' => 'firebird',
- 'DELIM' => ';;',
- 'PDO' => 'firebird',
- ),
- 'mysqli' => array(
- 'SCHEMA' => 'mysql_41',
- 'DELIM' => ';',
- 'PDO' => 'mysql',
- ),
- 'mysql' => array(
- 'SCHEMA' => 'mysql',
- 'DELIM' => ';',
- 'PDO' => 'mysql',
- ),
- 'mssql' => array(
- 'SCHEMA' => 'mssql',
- 'DELIM' => 'GO',
- 'PDO' => 'odbc',
- ),
- 'mssql_odbc'=> array(
- 'SCHEMA' => 'mssql',
- 'DELIM' => 'GO',
- 'PDO' => 'odbc',
- ),
- 'mssqlnative' => array(
- 'SCHEMA' => 'mssql',
- 'DELIM' => 'GO',
- 'PDO' => 'sqlsrv',
- ),
- 'oracle' => array(
- 'SCHEMA' => 'oracle',
- 'DELIM' => '/',
- 'PDO' => 'oci',
- ),
- 'postgres' => array(
- 'SCHEMA' => 'postgres',
- 'DELIM' => ';',
- 'PDO' => 'pgsql',
- ),
- 'sqlite' => array(
- 'SCHEMA' => 'sqlite',
- 'DELIM' => ';',
- 'PDO' => 'sqlite2',
- ),
- );
-
- if (isset($available_dbms[$dbms]))
- {
- return $available_dbms[$dbms];
- }
- else
- {
- trigger_error('Database unsupported', E_USER_ERROR);
- }
- }
-
public function get_database_config()
{
if (isset($_SERVER['PHPBB_TEST_DBMS']))
@@ -111,9 +51,9 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
'dbpasswd' => isset($_SERVER['PHPBB_TEST_DBPASSWD']) ? $_SERVER['PHPBB_TEST_DBPASSWD'] : '',
);
}
- else if (file_exists(__DIR__ . '/../test_config.php'))
+ else if (file_exists(dirname(__FILE__) . '/../test_config.php'))
{
- include(__DIR__ . '/../test_config.php');
+ include(dirname(__FILE__) . '/../test_config.php');
return array(
'dbms' => $dbms,
@@ -129,7 +69,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
// Silently use sqlite
return array(
'dbms' => 'sqlite',
- 'dbhost' => __DIR__ . '/../phpbb_unit_tests.sqlite2', // filename
+ 'dbhost' => dirname(__FILE__) . '/../phpbb_unit_tests.sqlite2', // filename
'dbport' => '',
'dbname' => '',
'dbuser' => '',
@@ -142,232 +82,26 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
}
}
- // NOTE: This function is not the same as split_sql_file from functions_install
- public function split_sql_file($sql, $dbms)
- {
- $dbms_data = $this->get_dbms_data($dbms);
-
- $sql = str_replace("\r" , '', $sql);
- $data = preg_split('/' . preg_quote($dbms_data['DELIM'], '/') . '$/m', $sql);
-
- $data = array_map('trim', $data);
-
- // The empty case
- $end_data = end($data);
-
- if (empty($end_data))
- {
- unset($data[key($data)]);
- }
-
- if ($dbms == 'sqlite')
- {
- // remove comment lines starting with # - they are not proper sqlite
- // syntax and break sqlite2
- foreach ($data as $i => $query)
- {
- $data[$i] = preg_replace('/^#.*$/m', "\n", $query);
- }
- }
-
- return $data;
- }
-
- /**
- * Retrieves a list of all tables from the database.
- *
- * @param PDO $pdo
- * @param string $dbms
- * @return array(string)
- */
- function get_tables($pdo, $dbms)
- {
- switch ($pdo)
- {
- case 'mysql':
- case 'mysql4':
- case 'mysqli':
- $sql = 'SHOW TABLES';
- break;
-
- case 'sqlite':
- $sql = 'SELECT name
- FROM sqlite_master
- WHERE type = "table"';
- break;
-
- case 'mssql':
- case 'mssql_odbc':
- case 'mssqlnative':
- $sql = "SELECT name
- FROM sysobjects
- WHERE type='U'";
- break;
-
- case 'postgres':
- $sql = 'SELECT relname
- FROM pg_stat_user_tables';
- break;
-
- case 'firebird':
- $sql = 'SELECT rdb$relation_name
- FROM rdb$relations
- WHERE rdb$view_source is null
- AND rdb$system_flag = 0';
- break;
-
- case 'oracle':
- $sql = 'SELECT table_name
- FROM USER_TABLES';
- break;
- }
-
- $result = $pdo->query($sql);
-
- $tables = array();
- while ($row = $result->fetch(PDO::FETCH_NUM))
- {
- $tables[] = current($row);
- }
-
- return $tables;
- }
-
- /**
- * Returns a PDO connection for the configured database.
- *
- * @param array $config The database configuration
- * @param array $dbms Information on the used DBMS.
- * @param bool $use_db Whether the DSN should be tied to a
- * particular database making it impossible
- * to delete that database.
- * @return PDO The PDO database connection.
- */
- public function new_pdo($config, $dbms, $use_db)
- {
- $dsn = $dbms['PDO'] . ':';
-
- switch ($dbms['PDO'])
- {
- case 'sqlite2':
- $dsn .= $config['dbhost'];
- break;
-
- case 'sqlsrv':
- // prefix the hostname (or DSN) with Server= so using just (local)\SQLExpress
- // works for example, further parameters can still be appended using ;x=y
- $dsn .= 'Server=';
- // no break -> rest like ODBC
- case 'odbc':
- // for ODBC assume dbhost is a suitable DSN
- // e.g. Driver={SQL Server Native Client 10.0};Server=(local)\SQLExpress;
- $dsn .= $config['dbhost'];
-
- if ($use_db)
- {
- $dsn .= ';Database=' . $config['dbname'];
- }
- break;
-
- default:
- $dsn .= 'host=' . $config['dbhost'];
-
- if ($use_db)
- {
- $dsn .= ';dbname=' . $config['dbname'];
- }
- break;
- }
-
- $pdo = new PDO($dsn, $config['dbuser'], $config['dbpasswd']);;
-
- // good for debug
- // $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-
- return $pdo;
- }
-
- private function recreate_db($config, $dbms)
- {
- switch ($config['dbms'])
- {
- case 'sqlite':
- if (file_exists($config['dbhost']))
- {
- unlink($config['dbhost']);
- }
- break;
-
- default:
- $pdo = $this->new_pdo($config, $dbms, false);
-
- try
- {
- $pdo->exec('DROP DATABASE ' . $config['dbname']);
- }
- catch (PDOException $e)
- {
- // try to delete all tables if dropping the database was not possible.
- foreach ($this->get_tables() as $table)
- {
- try
- {
- $pdo->exec('DROP TABLE ' . $table);
- }
- catch (PDOException $e){} // ignore non-existent tables
- }
- }
-
- $pdo->exec('CREATE DATABASE ' . $config['dbname']);
- break;
- }
- }
-
- private function load_schema($pdo, $config, $dbms)
- {
- if ($config['dbms'] == 'mysql')
- {
- $sth = $pdo->query('SELECT VERSION() AS version');
- $row = $sth->fetch(PDO::FETCH_ASSOC);
-
- if (version_compare($row['version'], '4.1.3', '>='))
- {
- $dbms['SCHEMA'] .= '_41';
- }
- else
- {
- $dbms['SCHEMA'] .= '_40';
- }
- }
-
- $sql = $this->split_sql_file(file_get_contents(__DIR__ . "/../../phpBB/install/schemas/{$dbms['SCHEMA']}_schema.sql"), $config['dbms']);
-
- foreach ($sql as $query)
- {
- $pdo->exec($query);
- }
- }
-
public function getConnection()
{
$config = $this->get_database_config();
- $dbms = $this->get_dbms_data($config['dbms']);
+
+ $manager = $this->create_connection_manager($config);
if (!self::$already_connected)
{
- $this->recreate_db($config, $dbms);
+ $manager->recreate_db();
}
- $pdo = $this->new_pdo($config, $dbms, true);
+ $manager->connect();
if (!self::$already_connected)
{
- $this->load_schema($pdo, $config, $dbms);
-
+ $manager->load_schema();
self::$already_connected = true;
}
- return $this->createDefaultDBConnection($pdo, 'testdb');
+ return $this->createDefaultDBConnection($manager->get_pdo(), 'testdb');
}
public function new_dbal()
@@ -376,7 +110,7 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
$config = $this->get_database_config();
- require_once __DIR__ . '/../../phpBB/includes/db/' . $config['dbms'] . '.php';
+ require_once dirname(__FILE__) . '/../../phpBB/includes/db/' . $config['dbms'] . '.php';
$dbal = 'dbal_' . $config['dbms'];
$db = new $dbal();
$db->sql_connect($config['dbhost'], $config['dbuser'], $config['dbpasswd'], $config['dbname'], $config['dbport']);
@@ -399,4 +133,9 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
{
$this->get_test_case_helpers()->setExpectedTriggerError($errno, $message);
}
+
+ protected function create_connection_manager($config)
+ {
+ return new phpbb_database_test_connection_manager($config);
+ }
}
diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php
new file mode 100644
index 0000000000..a7559e2183
--- /dev/null
+++ b/tests/test_framework/phpbb_database_test_connection_manager.php
@@ -0,0 +1,346 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2011 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+class phpbb_database_test_connection_manager
+{
+ private $config;
+ private $dbms;
+ private $pdo;
+
+ /**
+ * Constructor
+ *
+ * @param array $config Tests database configuration as returned by
+ * phpbb_database_test_case::get_database_config()
+ */
+ public function __construct($config)
+ {
+ $this->config = $config;
+ $this->dbms = $this->get_dbms_data($this->config['dbms']);
+ }
+
+ /**
+ * Return the current PDO instance
+ */
+ public function get_pdo()
+ {
+ return $this->pdo;
+ }
+
+ /**
+ * Creates a PDO connection for the configured database.
+ *
+ * @param bool $use_db Whether the DSN should be tied to a
+ * particular database making it impossible
+ * to delete that database.
+ */
+ public function connect($use_db = true)
+ {
+ $dsn = $this->dbms['PDO'] . ':';
+
+ switch ($this->dbms['PDO'])
+ {
+ case 'sqlite2':
+ $dsn .= $this->config['dbhost'];
+ break;
+
+ case 'sqlsrv':
+ // prefix the hostname (or DSN) with Server= so using just (local)\SQLExpress
+ // works for example, further parameters can still be appended using ;x=y
+ $dsn .= 'Server=';
+ // no break -> rest like ODBC
+ case 'odbc':
+ // for ODBC assume dbhost is a suitable DSN
+ // e.g. Driver={SQL Server Native Client 10.0};Server=(local)\SQLExpress;
+ $dsn .= $this->config['dbhost'];
+
+ if ($use_db)
+ {
+ $dsn .= ';Database=' . $this->config['dbname'];
+ }
+ break;
+
+ default:
+ $dsn .= 'host=' . $this->config['dbhost'];
+
+ if ($use_db)
+ {
+ $dsn .= ';dbname=' . $this->config['dbname'];
+ }
+ break;
+ }
+
+ try
+ {
+ $this->pdo = new PDO($dsn, $this->config['dbuser'], $this->config['dbpasswd']);
+ }
+ catch (PDOException $e)
+ {
+ $cleaned_dsn = str_replace($this->config['dbpasswd'], '*password*', $dsn);
+ throw new Exception("Unable do connect to $cleaned_dsn using PDO with error: {$e->getMessage()}");
+ }
+
+ // good for debug
+ // $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ }
+
+ /**
+ * Load the phpBB database schema into the database
+ */
+ public function load_schema()
+ {
+ $this->ensure_connected(__METHOD__);
+
+ $directory = dirname(__FILE__) . '/../../phpBB/install/schemas/';
+ $this->load_schema_from_file($directory);
+ }
+
+ /**
+ * Drop the database if it exists and re-create it
+ *
+ * Note: This does not load the schema, and it is suggested
+ * to re-connect after calling to get use_db isolation.
+ */
+ public function recreate_db()
+ {
+ switch ($this->config['dbms'])
+ {
+ case 'sqlite':
+ if (file_exists($this->config['dbhost']))
+ {
+ unlink($this->config['dbhost']);
+ }
+ break;
+
+ default:
+ $this->connect(false);
+
+ try
+ {
+ $this->pdo->exec('DROP DATABASE ' . $this->config['dbname']);
+ }
+ catch (PDOException $e)
+ {
+ // try to delete all tables if dropping the database was not possible.
+ foreach ($this->get_tables() as $table)
+ {
+ $this->pdo->exec('DROP TABLE ' . $table);
+ }
+ }
+
+ $this->pdo->exec('CREATE DATABASE ' . $this->config['dbname']);
+ break;
+ }
+ }
+
+ /**
+ * Retrieves a list of all tables from the database.
+ *
+ * @return array(string)
+ */
+ public function get_tables()
+ {
+ $this->ensure_connected(__METHOD__);
+
+ switch ($this->config['dbms'])
+ {
+ case 'mysql':
+ case 'mysql4':
+ case 'mysqli':
+ $sql = 'SHOW TABLES';
+ break;
+
+ case 'sqlite':
+ $sql = 'SELECT name
+ FROM sqlite_master
+ WHERE type = "table"';
+ break;
+
+ case 'mssql':
+ case 'mssql_odbc':
+ case 'mssqlnative':
+ $sql = "SELECT name
+ FROM sysobjects
+ WHERE type='U'";
+ break;
+
+ case 'postgres':
+ $sql = 'SELECT relname
+ FROM pg_stat_user_tables';
+ break;
+
+ case 'firebird':
+ $sql = 'SELECT rdb$relation_name
+ FROM rdb$relations
+ WHERE rdb$view_source is null
+ AND rdb$system_flag = 0';
+ break;
+
+ case 'oracle':
+ $sql = 'SELECT table_name
+ FROM USER_TABLES';
+ break;
+ }
+
+ $result = $this->pdo->query($sql);
+
+ $tables = array();
+ while ($row = $result->fetch(PDO::FETCH_NUM))
+ {
+ $tables[] = current($row);
+ }
+
+ return $tables;
+ }
+
+ /**
+ * Throw an exception if not connected
+ */
+ protected function ensure_connected($method_name)
+ {
+ if (null === $this->pdo)
+ {
+ throw new Exception(sprintf('You must connect before calling %s', $method_name));
+ }
+ }
+
+ /**
+ * Compile the correct schema filename (as per create_schema_files) and
+ * load it into the database.
+ */
+ protected function load_schema_from_file($directory)
+ {
+ $schema = $this->dbms['SCHEMA'];
+
+ if ($this->config['dbms'] == 'mysql')
+ {
+ $sth = $this->pdo->query('SELECT VERSION() AS version');
+ $row = $sth->fetch(PDO::FETCH_ASSOC);
+
+ if (version_compare($row['version'], '4.1.3', '>='))
+ {
+ $schema .= '_41';
+ }
+ else
+ {
+ $schema .= '_40';
+ }
+ }
+
+ $filename = $directory . $schema . '_schema.sql';
+ $sql = $this->split_sql(file_get_contents($filename));
+
+ foreach ($sql as $query)
+ {
+ $this->pdo->exec($query);
+ }
+ }
+
+ /**
+ * Split contents of an SQL file into an array of SQL statements
+ *
+ * Note: This method is not the same as split_sql_file from functions_install.
+ *
+ * @param string $sql Raw contents of an SQL file
+ *
+ * @return Array of runnable SQL statements
+ */
+ protected function split_sql($sql)
+ {
+ $sql = str_replace("\r" , '', $sql);
+ $data = preg_split('/' . preg_quote($this->dbms['DELIM'], '/') . '$/m', $sql);
+
+ $data = array_map('trim', $data);
+
+ // The empty case
+ $end_data = end($data);
+
+ if (empty($end_data))
+ {
+ unset($data[key($data)]);
+ }
+
+ if ($this->config['dbms'] == 'sqlite')
+ {
+ // remove comment lines starting with # - they are not proper sqlite
+ // syntax and break sqlite2
+ foreach ($data as $i => $query)
+ {
+ $data[$i] = preg_replace('/^#.*$/m', "\n", $query);
+ }
+ }
+
+ return $data;
+ }
+
+ /**
+ * Map a phpBB dbms driver name to dbms data array
+ */
+ protected function get_dbms_data($dbms)
+ {
+ $available_dbms = array(
+ 'firebird' => array(
+ 'SCHEMA' => 'firebird',
+ 'DELIM' => ';;',
+ 'PDO' => 'firebird',
+ ),
+ 'mysqli' => array(
+ 'SCHEMA' => 'mysql_41',
+ 'DELIM' => ';',
+ 'PDO' => 'mysql',
+ ),
+ 'mysql' => array(
+ 'SCHEMA' => 'mysql',
+ 'DELIM' => ';',
+ 'PDO' => 'mysql',
+ ),
+ 'mssql' => array(
+ 'SCHEMA' => 'mssql',
+ 'DELIM' => 'GO',
+ 'PDO' => 'odbc',
+ ),
+ 'mssql_odbc'=> array(
+ 'SCHEMA' => 'mssql',
+ 'DELIM' => 'GO',
+ 'PDO' => 'odbc',
+ ),
+ 'mssqlnative' => array(
+ 'SCHEMA' => 'mssql',
+ 'DELIM' => 'GO',
+ 'PDO' => 'sqlsrv',
+ ),
+ 'oracle' => array(
+ 'SCHEMA' => 'oracle',
+ 'DELIM' => '/',
+ 'PDO' => 'oci',
+ ),
+ 'postgres' => array(
+ 'SCHEMA' => 'postgres',
+ 'DELIM' => ';',
+ 'PDO' => 'pgsql',
+ ),
+ 'sqlite' => array(
+ 'SCHEMA' => 'sqlite',
+ 'DELIM' => ';',
+ 'PDO' => 'sqlite2',
+ ),
+ );
+
+ if (isset($available_dbms[$dbms]))
+ {
+ return $available_dbms[$dbms];
+ }
+ else
+ {
+ $message = "Supplied dbms \"$dbms\" is not a valid phpBB dbms, must be one of: ";
+ $message .= implode(', ', array_keys($available_dbms));
+ throw new Exception($message);
+ }
+ }
+}
diff --git a/tests/text_processing/make_clickable_test.php b/tests/text_processing/make_clickable_test.php
index 75a35daf82..29b982d709 100644
--- a/tests/text_processing/make_clickable_test.php
+++ b/tests/text_processing/make_clickable_test.php
@@ -7,8 +7,8 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/functions.php';
-require_once __DIR__ . '/../../phpBB/includes/functions_content.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php';
class phpbb_text_processing_make_clickable_test extends phpbb_test_case
{
diff --git a/tests/user/lang_test.php b/tests/user/lang_test.php
new file mode 100644
index 0000000000..6c60583a7b
--- /dev/null
+++ b/tests/user/lang_test.php
@@ -0,0 +1,58 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2011 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+require_once dirname(__FILE__) . '/../../phpBB/includes/session.php';
+
+class phpbb_user_lang_test extends phpbb_test_case
+{
+ public function test_user_lang_sprintf()
+ {
+ $user = new user;
+ $user->lang = array(
+ 'FOO' => 'BAR',
+ 'BARZ' => 'PENG',
+ 'EMPTY' => '',
+ 'ZERO' => '0',
+ 'STR' => '%d %s, %d topics',
+ 'STR2' => '%d foos',
+ 'ARRY' => array(
+ 0 => 'No posts', // 0
+ 1 => '1 post', // 1
+ 2 => '%d posts', // 2+
+ ),
+ );
+
+ // No param
+ $this->assertEquals($user->lang('FOO'), 'BAR');
+ $this->assertEquals($user->lang('EMPTY'), '');
+ $this->assertEquals($user->lang('ZERO'), '0');
+
+ // Invalid index
+ $this->assertEquals($user->lang('VOID'), 'VOID');
+
+ // Unnecessary param
+ $this->assertEquals($user->lang('FOO', 2), 'BAR');
+ $this->assertEquals($user->lang('FOO', 2, 3), 'BAR');
+ $this->assertEquals($user->lang('FOO', 2, 3, 'BARZ'), 'BAR');
+
+ // String
+ $this->assertEquals($user->lang('STR', 24, 'x', 42), '24 x, 42 topics');
+ $this->assertEquals($user->lang('STR2', 64), '64 foos');
+
+ // Array
+ $this->assertEquals($user->lang('ARRY', 0), 'No posts');
+ $this->assertEquals($user->lang('ARRY', 1), '1 post');
+ $this->assertEquals($user->lang('ARRY', 2), '2 posts');
+ $this->assertEquals($user->lang('ARRY', 123), '123 posts');
+
+ // Bug PHPBB3-9949
+ $this->assertEquals($user->lang('ARRY', 1, 2), '1 post');
+ $this->assertEquals($user->lang('ARRY', 1, 's', 2), '1 post');
+ }
+}
diff --git a/tests/utf/normalizer_test.php b/tests/utf/normalizer_test.php
index 9a9011c0fe..38b4ec1b6b 100644
--- a/tests/utf/normalizer_test.php
+++ b/tests/utf/normalizer_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/utf/utf_normalizer.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_normalizer.php';
/**
* @group slow
@@ -16,8 +16,8 @@ class phpbb_utf_normalizer_test extends phpbb_test_case
{
static public function setUpBeforeClass()
{
- self::download('http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt', __DIR__.'/data');
- self::download('http://www.unicode.org/Public/UNIDATA/UnicodeData.txt', __DIR__.'/data');
+ self::download('http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt', dirname(__FILE__).'/data');
+ self::download('http://www.unicode.org/Public/UNIDATA/UnicodeData.txt', dirname(__FILE__).'/data');
}
public function test_normalizer()
@@ -62,7 +62,7 @@ class phpbb_utf_normalizer_test extends phpbb_test_case
$tested_chars = array();
- $fp = fopen(__DIR__.'/data/NormalizationTest.txt', 'rb');
+ $fp = fopen(dirname(__FILE__).'/data/NormalizationTest.txt', 'rb');
while (!feof($fp))
{
$line = fgets($fp);
@@ -117,7 +117,7 @@ class phpbb_utf_normalizer_test extends phpbb_test_case
*/
public function test_invariants(array $tested_chars)
{
- $fp = fopen(__DIR__.'/data/UnicodeData.txt', 'rb');
+ $fp = fopen(dirname(__FILE__).'/data/UnicodeData.txt', 'rb');
while (!feof($fp))
{
diff --git a/tests/utf/utf8_clean_string_test.php b/tests/utf/utf8_clean_string_test.php
index 148297ad4b..e5a771eafa 100644
--- a/tests/utf/utf8_clean_string_test.php
+++ b/tests/utf/utf8_clean_string_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php';
class phpbb_utf_utf8_clean_string_test extends phpbb_test_case
{
diff --git a/tests/utf/utf8_wordwrap_test.php b/tests/utf/utf8_wordwrap_test.php
index fbc947b92a..03fa9dc38c 100644
--- a/tests/utf/utf8_wordwrap_test.php
+++ b/tests/utf/utf8_wordwrap_test.php
@@ -7,7 +7,7 @@
*
*/
-require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php';
+require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php';
class phpbb_utf_utf8_wordwrap_test extends phpbb_test_case
{