Skip to content

Commit

Permalink
Reverted a change from this afternoon to fix includes.
Browse files Browse the repository at this point in the history
Added a phpunits for first parts, rest follow soon hopefully.

Fixed a few bugs I found while writing the tests.
  • Loading branch information
paul999 committed Jun 18, 2011
1 parent aadea7a commit 6324547
Show file tree
Hide file tree
Showing 73 changed files with 841 additions and 6 deletions.
11 changes: 9 additions & 2 deletions includes/tests/test_base.php
Expand Up @@ -248,7 +248,14 @@ protected function extract_dir($file)
**/
public function unittest($function, $parameters)
{
call_user_func_array(array($this, $function), $parameters);
}
return call_user_func_array(array($this, $function), $parameters);
}

public function setFilename($file)
{
$this->file_name = $file;
$this->file_contents = @file_get_contents($file);
$this->file_contents_file = @file($file);
}
}
?>
10 changes: 6 additions & 4 deletions includes/tests/tests_code.php
Expand Up @@ -238,7 +238,7 @@ protected function test_dbal()

$functions = array(
'mysql_',
'mysqli_',
'mysqli_',
'oci_',
'sqlite_',
'pg_',
Expand All @@ -251,6 +251,7 @@ protected function test_dbal()

foreach ($functions as $function)
{

if (preg_match("#(^\s*|[^a-z0-9_])" . preg_quote($function, '#') . "{1}([a-zA-Z0-9_]+){1,}\s*\({1}#si", $this->file_contents))
{
$return = $this->display_line_code(mpv::ERROR_FAIL, 'USAGE_' . strtoupper(str_replace(array('_', '$', '('), '', $function)), false, "#(^\s*|[^a-z0-9_])" . preg_quote($function, '#') . "{1}([a-zA-Z0-9_]+){1,}\s*\({1}#si", array('new', 'function'));
Expand All @@ -272,7 +273,7 @@ protected function test_code()
$functions = array(
'eval',
'exec',
'sytem',
'system',
'passthru',
'getenv',
'die',
Expand Down Expand Up @@ -312,7 +313,7 @@ protected function test_code()

foreach ($functions_notice as $function)
{
if (preg_match("#(^\s*|[^a-z0-9_])" . preg_quote($function, '#') . "{1}\s*\({1}#si", $this->file_contents))
if (preg_match("#(^\s*|[^a-z0-9_])" . preg_quote($function, '#') . "{1}\s*\({0,1}#si", $this->file_contents))
{
$return = $this->display_line_code(mpv::ERROR_NOTICE, 'USAGE_' . strtoupper(str_replace(array('_', '$', '('), '', $function)), false, "#(^\s*|[^a-z0-9_])" . preg_quote($function) . "([ \(|\(| ]+)#si");
}
Expand Down Expand Up @@ -496,7 +497,7 @@ protected function test_include()
$content_new = substr($content_new, 0, $loc + 2);
}

if (preg_match("#^(include_once|require_once|include|require)(\s'|\s\"|\s\$|\s\(|\()#", $content_new))
if (preg_match("#(include_once|require_once|include|require)(\s'|\s\"|\s\$|\s\(|\()#", $content_new))
{
if (strpos($content_new, '$phpbb_root_path') === false && strpos($content_new, '$phpbb_admin_path') === false)
{
Expand All @@ -512,5 +513,6 @@ protected function test_include()
}
}
}
return $return;
}
}
25 changes: 25 additions & 0 deletions phpunit.xml.all
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="true"
backupStaticAttributes="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="phpBB Test Suite">
<directory suffix="_test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<blacklist>
<directory>./tests/</directory>
</blacklist>
</filter>
</phpunit>
31 changes: 31 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="true"
backupStaticAttributes="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="phpBB Test Suite">
<directory suffix="_test.php">./tests/</directory>
</testsuite>
</testsuites>

<groups>
<exclude>
<group>slow</group>
</exclude>
</groups>

<filter>
<blacklist>
<directory>./tests/</directory>
</blacklist>
</filter>
</phpunit>
13 changes: 13 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,13 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once 'test_framework/phpbb_test_case_helpers.php';
require_once 'test_framework/phpbb_test_case.php';
require_once 'includes/tests/test_base.php';
require_once 'test_framework/mpv_mock.php';
50 changes: 50 additions & 0 deletions tests/code/binary_test.php
@@ -0,0 +1,50 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once dirname(__FILE__) . '/../../includes/tests/tests_code.php';

class phpbb_binary_test extends phpbb_test_case
{
private $test;
public static function provider()
{
// array(Input -> redirect(), expected triggered error (else false), expected returned result url (else false))
return array(
array('testcode/noExtension', false, true),
array('testcode/ignoreFile.php', false, false),
array('testcode/noBinary', 'FILE_NON_BINARY', false),
);
}

protected function setUp()
{
parent::setUp();

$this->test = new mpv_tests_code(new mpv);
}

/**
* @dataProvider provider
*/
public function test_binary($test, $expected_error, $expected_result)
{
global $user;

if ($expected_error !== false)
{
$this->setExpectedTriggerError(E_USER_ERROR, $expected_error);
}

$result = $this->test->unittest('check_binary', array(('tests/code/' . $test)));

$this->assertEquals($expected_result, $result);

}
}

59 changes: 59 additions & 0 deletions tests/code/db_test.php
@@ -0,0 +1,59 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once dirname(__FILE__) . '/../../includes/tests/tests_code.php';

class phpbb_mysql_test extends phpbb_test_case
{
private $test;
public static function provider()
{
// array(Input -> redirect(), expected triggered error (else false), expected returned result url (else false))
return array(
array('testcode/dbal/mysql', 'USAGE_MYSQL', false),
array('testcode/dbal/mysqli', 'USAGE_MYSQLI', false),
array('testcode/dbal/oci', 'USAGE_OCI', false),
array('testcode/dbal/sqlite', 'USAGE_SQLITE', false),
array('testcode/dbal/pg', 'USAGE_PG', false),
array('testcode/dbal/mssql', 'USAGE_MSSQL', false),
array('testcode/dbal/odbc', 'USAGE_ODBC', false),
array('testcode/dbal/sqlsrv', 'USAGE_SQLSRV', false),
array('testcode/dbal/ibase', 'USAGE_IBASE', false),
array('testcode/dbal/db2', 'USAGE_DB2', false),
array('testcode/short2', false, true),
);
}

protected function setUp()
{
parent::setUp();

$this->test = new mpv_tests_code(new mpv);
}

/**
* @dataProvider provider
*/
public function test_mysql($test, $expected_error, $expected_result)
{
global $user;
$this->test->setFilename('tests/code/' . $test);

if ($expected_error !== false)
{
$this->setExpectedTriggerError(E_USER_ERROR, $expected_error);
}

$result = $this->test->unittest('test_dbal', array());

$this->assertEquals($expected_result, $result);

}
}

49 changes: 49 additions & 0 deletions tests/code/empty_test.php
@@ -0,0 +1,49 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once dirname(__FILE__) . '/../../includes/tests/tests_code.php';

class phpbb_empty_test extends phpbb_test_case
{
private $test;
public static function provider()
{
// array(Input -> redirect(), expected triggered error (else false), expected returned result url (else false))
return array(
array('testcode/emptyFile', 'FILE_EMPTY', false),
array('testcode/noExtension', false, true),
);
}

protected function setUp()
{
parent::setUp();

$this->test = new mpv_tests_code(new mpv);
}

/**
* @dataProvider provider
*/
public function test_empty($test, $expected_error, $expected_result)
{
global $user;
$this->test->setFilename('tests/code/' . $test);

if ($expected_error !== false)
{
$this->setExpectedTriggerError(E_USER_ERROR, $expected_error);
}

$result = $this->test->unittest('test_empty', array());

$this->assertEquals($expected_result, $result);
}
}

68 changes: 68 additions & 0 deletions tests/code/function_test.php
@@ -0,0 +1,68 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

require_once dirname(__FILE__) . '/../../includes/tests/tests_code.php';

class phpbb_function_test extends phpbb_test_case
{
private $test;
public static function provider()
{
// array(Input -> redirect(), expected triggered error (else false), expected returned result url (else false))
return array(
array('testcode/dbal/mysql', false, true),
array('testcode/functions/eval', 'USAGE_EVAL', false),
array('testcode/functions/exec', 'USAGE_EXEC', false),
array('testcode/functions/system', 'USAGE_SYSTEM', false),
array('testcode/functions/passthru', 'USAGE_PASSTHRU', false),
array('testcode/functions/getenv', 'USAGE_GETENV', false),
array('testcode/functions/die', 'USAGE_DIE', false),
array('testcode/functions/sha1', 'USAGE_SHA1', false),
array('testcode/functions/addslashes', 'USAGE_ADDSLASHES', false),
array('testcode/functions/stripslashes', 'USAGE_STRIPSLASHES', false),

array('testcode/functions/backticks', 'USAGE_`', false),

array('testcode/functions/include_once', 'USAGE_INCLUDEONCE', false),
array('testcode/functions/include_once2', 'USAGE_INCLUDEONCE', false),
array('testcode/functions/require_once', 'USAGE_REQUIREONCE', false),
array('testcode/functions/require_once2', 'USAGE_REQUIREONCE', false),
array('testcode/functions/md5', 'USAGE_MD5', false),
array('testcode/functions/md52', false, true),
array('testcode/short2', false, true),
);
}

protected function setUp()
{
parent::setUp();

$this->test = new mpv_tests_code(new mpv);
}

/**
* @dataProvider provider
*/
public function test_function($test, $expected_error, $expected_result)
{
global $user;
$this->test->setFilename('tests/code/' . $test);

if ($expected_error !== false)
{
$this->setExpectedTriggerError(E_USER_ERROR, $expected_error);
}

$result = $this->test->unittest('test_code', array());

if ($expected_error === false)
$this->assertEquals($expected_result, $result);
}
}

0 comments on commit 6324547

Please sign in to comment.