aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/dbal/write_sequence_test.php55
-rw-r--r--tests/lint_test.php74
-rw-r--r--tests/session/append_sid_test.php101
-rw-r--r--tests/test_framework/phpbb_database_test_case.php20
-rw-r--r--tests/test_framework/phpbb_database_test_connection_manager.php107
5 files changed, 305 insertions, 52 deletions
diff --git a/tests/dbal/write_sequence_test.php b/tests/dbal/write_sequence_test.php
new file mode 100644
index 0000000000..8975cfbfb1
--- /dev/null
+++ b/tests/dbal/write_sequence_test.php
@@ -0,0 +1,55 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2012 phpBB Group
+* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
+*
+*/
+
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+
+class phpbb_dbal_write_sequence_test extends phpbb_database_test_case
+{
+ public function getDataSet()
+ {
+ return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/three_users.xml');
+ }
+
+ static public function write_sequence_data()
+ {
+ return array(
+ array(
+ 'ticket/11219',
+ 4,
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider write_sequence_data
+ */
+ public function test_write_sequence($username, $expected)
+ {
+ $db = $this->new_dbal();
+
+ $sql = 'INSERT INTO phpbb_users ' . $db->sql_build_array('INSERT', array(
+ 'username' => $username,
+ 'username_clean' => $username,
+ 'user_permissions' => '',
+ 'user_sig' => '',
+ 'user_occ' => '',
+ 'user_interests' => '',
+ ));
+ $db->sql_query($sql);
+
+ $this->assertEquals($expected, $db->sql_nextid());
+
+ $sql = "SELECT user_id
+ FROM phpbb_users
+ WHERE username_clean = '" . $db->sql_escape($username) . "'";
+ $result = $db->sql_query_limit($sql, 1);
+
+ $this->assertEquals($expected, $db->sql_fetchfield('user_id'));
+ }
+}
diff --git a/tests/lint_test.php b/tests/lint_test.php
new file mode 100644
index 0000000000..905067072d
--- /dev/null
+++ b/tests/lint_test.php
@@ -0,0 +1,74 @@
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2012 phpBB Group
+* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
+*
+*/
+
+class phpbb_lint_test extends phpbb_test_case
+{
+ static protected $exclude;
+
+ static public function setUpBeforeClass()
+ {
+ $output = array();
+ $status = 1;
+ exec('(php -v) 2>&1', $output, $status);
+ if ($status)
+ {
+ $output = implode("\n", $output);
+ self::markTestSkipped("php is not in PATH or broken: $output");
+ }
+
+ self::$exclude = array(
+ // PHP Fatal error: Cannot declare class Container because the name is already in use in /var/www/projects/phpbb3/tests/../phpBB/vendor/symfony/dependency-injection/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services1-1.php on line 20
+ // https://gist.github.com/e003913ffd493da63cbc
+ dirname(__FILE__) . '/../phpBB/vendor',
+ );
+ }
+
+ public function test_lint()
+ {
+ if (version_compare(PHP_VERSION, '5.3.0', '<'))
+ {
+ $this->markTestSkipped('phpBB uses PHP 5.3 syntax in some files, linting on PHP < 5.3 will fail');
+ }
+
+ $root = dirname(__FILE__) . '/..';
+ $this->check($root);
+ }
+
+ protected function check($root)
+ {
+ $dh = opendir($root);
+ while (($filename = readdir($dh)) !== false)
+ {
+ if ($filename == '.' || $filename == '..' || $filename == 'git')
+ {
+ continue;
+ }
+ $path = $root . '/' . $filename;
+ // skip symlinks to avoid infinite loops
+ if (is_link($path))
+ {
+ continue;
+ }
+ if (is_dir($path) && !in_array($path, self::$exclude))
+ {
+ $this->check($path);
+ }
+ else if (substr($filename, strlen($filename)-4) == '.php')
+ {
+ // assume php binary is called php and it is in PATH
+ $cmd = '(php -l ' . escapeshellarg($path) . ') 2>&1';
+ $output = array();
+ $status = 1;
+ exec($cmd, $output, $status);
+ $output = implode("\n", $output);
+ $this->assertEquals(0, $status, "php -l failed for $path:\n$output");
+ }
+ }
+ }
+}
diff --git a/tests/session/append_sid_test.php b/tests/session/append_sid_test.php
index 88f6f0718e..ce7bf71215 100644
--- a/tests/session/append_sid_test.php
+++ b/tests/session/append_sid_test.php
@@ -1,51 +1,50 @@
-<?php
-/**
-*
-* @package testing
-* @copyright (c) 2011 phpBB Group
-* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
-*
-*/
-
-require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
-
-class phpbb_session_append_sid_test extends phpbb_test_case
-{
-
- public function append_sid_data()
- {
- return array(
- array('viewtopic.php?t=1&amp;f=2', false, true, false, 'viewtopic.php?t=1&amp;f=2', 'parameters in url-argument'),
- array('viewtopic.php', 't=1&amp;f=2', true, false, 'viewtopic.php?t=1&amp;f=2', 'parameters in params-argument using amp'),
- array('viewtopic.php', 't=1&f=2', false, false, 'viewtopic.php?t=1&f=2', 'parameters in params-argument using &'),
- array('viewtopic.php', array('t' => 1, 'f' => 2), true, false, 'viewtopic.php?t=1&amp;f=2', 'parameters in params-argument as array'),
-
- // Custom sid parameter
- array('viewtopic.php', 't=1&amp;f=2', true, 'custom-sid', 'viewtopic.php?t=1&amp;f=2&amp;sid=custom-sid', 'using session_id'),
-
- // Testing anchors
- array('viewtopic.php?t=1&amp;f=2#anchor', false, true, false, 'viewtopic.php?t=1&amp;f=2#anchor', 'anchor in url-argument'),
- array('viewtopic.php', 't=1&amp;f=2#anchor', true, false, 'viewtopic.php?t=1&amp;f=2#anchor', 'anchor in params-argument'),
- array('viewtopic.php', array('t' => 1, 'f' => 2, '#' => 'anchor'), true, false, 'viewtopic.php?t=1&amp;f=2#anchor', 'anchor in params-argument (array)'),
-
- // Anchors and custom sid
- array('viewtopic.php?t=1&amp;f=2#anchor', false, true, 'custom-sid', 'viewtopic.php?t=1&amp;f=2&amp;sid=custom-sid#anchor', 'anchor in url-argument using session_id'),
- array('viewtopic.php', 't=1&amp;f=2#anchor', true, 'custom-sid', 'viewtopic.php?t=1&amp;f=2&amp;sid=custom-sid#anchor', 'anchor in params-argument using session_id'),
- array('viewtopic.php', array('t' => 1, 'f' => 2, '#' => 'anchor'), true, 'custom-sid', 'viewtopic.php?t=1&amp;f=2&amp;sid=custom-sid#anchor', 'anchor in params-argument (array) using session_id'),
-
- // Empty parameters should not append the ?
- array('viewtopic.php', false, true, false, 'viewtopic.php', 'no params using bool false'),
- array('viewtopic.php', '', true, false, 'viewtopic.php', 'no params using empty string'),
- array('viewtopic.php', array(), true, false, 'viewtopic.php', 'no params using empty array'),
- );
- }
-
- /**
- * @dataProvider append_sid_data
- */
- public function test_append_sid($url, $params, $is_amp, $session_id, $expected, $description)
- {
- $this->assertEquals($expected, append_sid($url, $params, $is_amp, $session_id));
- }
-}
-
+<?php
+/**
+*
+* @package testing
+* @copyright (c) 2011 phpBB Group
+* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
+*
+*/
+
+require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
+
+class phpbb_session_append_sid_test extends phpbb_test_case
+{
+
+ public function append_sid_data()
+ {
+ return array(
+ array('viewtopic.php?t=1&amp;f=2', false, true, false, 'viewtopic.php?t=1&amp;f=2', 'parameters in url-argument'),
+ array('viewtopic.php', 't=1&amp;f=2', true, false, 'viewtopic.php?t=1&amp;f=2', 'parameters in params-argument using amp'),
+ array('viewtopic.php', 't=1&f=2', false, false, 'viewtopic.php?t=1&f=2', 'parameters in params-argument using &'),
+ array('viewtopic.php', array('t' => 1, 'f' => 2), true, false, 'viewtopic.php?t=1&amp;f=2', 'parameters in params-argument as array'),
+
+ // Custom sid parameter
+ array('viewtopic.php', 't=1&amp;f=2', true, 'custom-sid', 'viewtopic.php?t=1&amp;f=2&amp;sid=custom-sid', 'using session_id'),
+
+ // Testing anchors
+ array('viewtopic.php?t=1&amp;f=2#anchor', false, true, false, 'viewtopic.php?t=1&amp;f=2#anchor', 'anchor in url-argument'),
+ array('viewtopic.php', 't=1&amp;f=2#anchor', true, false, 'viewtopic.php?t=1&amp;f=2#anchor', 'anchor in params-argument'),
+ array('viewtopic.php', array('t' => 1, 'f' => 2, '#' => 'anchor'), true, false, 'viewtopic.php?t=1&amp;f=2#anchor', 'anchor in params-argument (array)'),
+
+ // Anchors and custom sid
+ array('viewtopic.php?t=1&amp;f=2#anchor', false, true, 'custom-sid', 'viewtopic.php?t=1&amp;f=2&amp;sid=custom-sid#anchor', 'anchor in url-argument using session_id'),
+ array('viewtopic.php', 't=1&amp;f=2#anchor', true, 'custom-sid', 'viewtopic.php?t=1&amp;f=2&amp;sid=custom-sid#anchor', 'anchor in params-argument using session_id'),
+ array('viewtopic.php', array('t' => 1, 'f' => 2, '#' => 'anchor'), true, 'custom-sid', 'viewtopic.php?t=1&amp;f=2&amp;sid=custom-sid#anchor', 'anchor in params-argument (array) using session_id'),
+
+ // Empty parameters should not append the ?
+ array('viewtopic.php', false, true, false, 'viewtopic.php', 'no params using bool false'),
+ array('viewtopic.php', '', true, false, 'viewtopic.php', 'no params using empty string'),
+ array('viewtopic.php', array(), true, false, 'viewtopic.php', 'no params using empty array'),
+ );
+ }
+
+ /**
+ * @dataProvider append_sid_data
+ */
+ public function test_append_sid($url, $params, $is_amp, $session_id, $expected, $description)
+ {
+ $this->assertEquals($expected, append_sid($url, $params, $is_amp, $session_id));
+ }
+}
diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php
index 75a3c0944b..429bb92bf1 100644
--- a/tests/test_framework/phpbb_database_test_case.php
+++ b/tests/test_framework/phpbb_database_test_case.php
@@ -13,6 +13,8 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
protected $test_case_helpers;
+ protected $fixture_xml_data;
+
public function __construct($name = NULL, array $data = array(), $dataName = '')
{
parent::__construct($name, $data, $dataName);
@@ -28,6 +30,20 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
);
}
+ protected function setUp()
+ {
+ parent::setUp();
+
+ // Resynchronise tables if a fixture was loaded
+ if (isset($this->fixture_xml_data))
+ {
+ $config = $this->get_database_config();
+ $manager = $this->create_connection_manager($config);
+ $manager->connect();
+ $manager->post_setup_synchronisation($this->fixture_xml_data);
+ }
+ }
+
public function createXMLDataSet($path)
{
$db_config = $this->get_database_config();
@@ -47,7 +63,9 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test
$path = $meta_data['uri'];
}
- return parent::createXMLDataSet($path);
+ $this->fixture_xml_data = parent::createXMLDataSet($path);
+
+ return $this->fixture_xml_data;
}
public function get_test_case_helpers()
diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php
index a43215bcf2..d7c2804aa7 100644
--- a/tests/test_framework/phpbb_database_test_connection_manager.php
+++ b/tests/test_framework/phpbb_database_test_connection_manager.php
@@ -426,4 +426,111 @@ class phpbb_database_test_connection_manager
$this->pdo->exec($query);
}
}
+
+ /**
+ * Performs synchronisations on the database after a fixture has been loaded
+ *
+ * @param PHPUnit_Extensions_Database_DataSet_XmlDataSet $xml_data_set Information about the tables contained within the loaded fixture
+ *
+ * @return null
+ */
+ public function post_setup_synchronisation($xml_data_set)
+ {
+ $this->ensure_connected(__METHOD__);
+ $queries = array();
+
+ // Get escaped versions of the table names used in the fixture
+ $table_names = array_map(array($this->pdo, 'PDO::quote'), $xml_data_set->getTableNames());
+
+ switch ($this->config['dbms'])
+ {
+ case 'oracle':
+ // Get all of the information about the sequences
+ $sql = "SELECT t.table_name, tc.column_name, d.referenced_name as sequence_name, s.increment_by, s.min_value
+ FROM USER_TRIGGERS t
+ JOIN USER_DEPENDENCIES d ON (d.name = t.trigger_name)
+ JOIN USER_TRIGGER_COLS tc ON (tc.trigger_name = t.trigger_name)
+ JOIN USER_SEQUENCES s ON (s.sequence_name = d.referenced_name)
+ WHERE d.referenced_type = 'SEQUENCE'
+ AND d.type = 'TRIGGER'
+ AND t.table_name IN (" . implode(', ', array_map('strtoupper', $table_names)) . ')';
+
+ $result = $this->pdo->query($sql);
+
+ while ($row = $result->fetch(PDO::FETCH_ASSOC))
+ {
+ // Get the current max value of the table
+ $sql = "SELECT MAX({$row['COLUMN_NAME']}) AS max FROM {$row['TABLE_NAME']}";
+ $max_result = $this->pdo->query($sql);
+ $max_row = $max_result->fetch(PDO::FETCH_ASSOC);
+
+ if (!$max_row)
+ {
+ continue;
+ }
+
+ $max_val = (int) $max_row['MAX'];
+ $max_val++;
+
+ /**
+ * This is not the "proper" way, but the proper way does not allow you to completely reset
+ * tables with no rows since you have to select the next value to make the change go into effect.
+ * You would have to go past the minimum value to set it correctly, but that's illegal.
+ * Since we have no objects attached to our sequencers (triggers aren't attached), this works fine.
+ */
+ $queries[] = 'DROP SEQUENCE ' . $row['SEQUENCE_NAME'];
+ $queries[] = "CREATE SEQUENCE {$row['SEQUENCE_NAME']}
+ MINVALUE {$row['MIN_VALUE']}
+ INCREMENT BY {$row['INCREMENT_BY']}
+ START WITH $max_val";
+ }
+ break;
+
+ case 'postgres':
+ // Get the sequences attached to the tables
+ $sql = 'SELECT column_name, table_name FROM information_schema.columns
+ WHERE table_name IN (' . implode(', ', $table_names) . ")
+ AND strpos(column_default, '_seq''::regclass') > 0";
+ $result = $this->pdo->query($sql);
+
+ $setval_queries = array();
+ while ($row = $result->fetch(PDO::FETCH_ASSOC))
+ {
+ // Get the columns used in the fixture for this table
+ $column_names = $xml_data_set->getTableMetaData($row['table_name'])->getColumns();
+
+ // Skip sequences that weren't specified in the fixture
+ if (!in_array($row['column_name'], $column_names))
+ {
+ continue;
+ }
+
+ // Get the old value if it exists, or use 1 if it doesn't
+ $sql = "SELECT COALESCE((SELECT MAX({$row['column_name']}) + 1 FROM {$row['table_name']}), 1) AS val";
+ $result_max = $this->pdo->query($sql);
+ $row_max = $result_max->fetch(PDO::FETCH_ASSOC);
+
+ if ($row_max)
+ {
+ $seq_name = $this->pdo->quote($row['table_name'] . '_seq');
+ $max_val = (int) $row_max['val'];
+
+ // The last parameter is false so that the system doesn't increment it again
+ $setval_queries[] = "SETVAL($seq_name, $max_val, false)";
+ }
+ }
+
+ // Combine all of the SETVALs into one query
+ if (sizeof($setval_queries))
+ {
+ $queries[] = 'SELECT ' . implode(', ', $setval_queries);
+ }
+ break;
+ }
+
+ foreach ($queries as $query)
+ {
+ $this->pdo->exec($query);
+ }
+ }
}