diff options
Diffstat (limited to 'tests')
157 files changed, 5269 insertions, 330 deletions
diff --git a/tests/bbcode/parser_test.php b/tests/bbcode/parser_test.php new file mode 100644 index 0000000000..8c7fbc7128 --- /dev/null +++ b/tests/bbcode/parser_test.php @@ -0,0 +1,29 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2008 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +// require_once dirname(__FILE__) . '/../../phpBB/includes/bbcode/bbcode_parser_base.php'; +// require_once dirname(__FILE__) . '/../../phpBB/includes/bbcode/bbcode_parser.php'; + +class phpbb_bbcode_parser_test extends PHPUnit_Framework_TestCase +{ + public function test_both_passes() + { + $this->markTestIncomplete('New bbcode parser has not been backported from feature/ascraeus-experiment yet.'); + + $parser = new phpbb_bbcode_parser(); + + $result = $parser->first_pass('[i]Italic [u]underlined text[/u][/i]'); + $result = $parser->second_pass($result); + + $expected = '<span style="font-style: italic">Italic <span style="text-decoration: underline">underlined text</span></span>'; + + $this->assertEquals($expected, $result, 'Simple nested BBCode first+second pass'); + } +} diff --git a/tests/bbcode/url_bbcode_test.php b/tests/bbcode/url_bbcode_test.php index 6b5afe5808..d5df386714 100644 --- a/tests/bbcode/url_bbcode_test.php +++ b/tests/bbcode/url_bbcode_test.php @@ -11,7 +11,6 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/bbcode.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/message_parser.php'; -require_once dirname(__FILE__) . '/../mock/user.php'; class phpbb_url_bbcode_test extends phpbb_test_case { @@ -51,8 +50,9 @@ class phpbb_url_bbcode_test extends phpbb_test_case */ public function test_url($description, $message, $expected) { - global $user; + global $user, $request; $user = new phpbb_mock_user; + $request = new phpbb_mock_request; $bbcode = new bbcode_firstpass(); $bbcode->message = $message; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d687db622a..1017e0c72f 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -14,28 +14,17 @@ require_once $phpbb_root_path . 'includes/startup.php'; $table_prefix = 'phpbb_'; require_once $phpbb_root_path . 'includes/constants.php'; +require_once $phpbb_root_path . 'includes/class_loader.' . $phpEx; + +$phpbb_class_loader_mock = new phpbb_class_loader('phpbb_mock_', $phpbb_root_path . '../tests/mock/', ".php"); +$phpbb_class_loader_mock->register(); +$phpbb_class_loader_ext = new phpbb_class_loader('phpbb_ext_', $phpbb_root_path . 'ext/', ".php"); +$phpbb_class_loader_ext->register(); +$phpbb_class_loader = new phpbb_class_loader('phpbb_', $phpbb_root_path . 'includes/', ".php"); +$phpbb_class_loader->register(); 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'; - -if (version_compare(PHP_VERSION, '5.3.0-dev', '>=')) -{ - if (getenv('PHPBB_NO_COMPOSER_AUTOLOAD')) - { - if (getenv('PHPBB_AUTOLOAD')) - { - require(getenv('PHPBB_AUTOLOAD')); - } - } - else - { - if (!file_exists($phpbb_root_path . 'vendor/autoload.php')) - { - trigger_error('You have not set up composer dependencies. See http://getcomposer.org/.', E_USER_ERROR); - } - require($phpbb_root_path . 'vendor/autoload.php'); - } - require_once 'test_framework/phpbb_functional_test_case.php'; -} +require_once 'test_framework/phpbb_functional_test_case.php'; diff --git a/tests/cache/cache_test.php b/tests/cache/cache_test.php new file mode 100644 index 0000000000..564bd35863 --- /dev/null +++ b/tests/cache/cache_test.php @@ -0,0 +1,70 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 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_cache_test extends phpbb_test_case +{ + private $cache_dir; + + public function __construct() + { + $this->cache_dir = dirname(__FILE__) . '/../tmp/cache/'; + } + + protected function setUp() + { + if (file_exists($this->cache_dir)) + { + // cache directory possibly left after aborted + // or failed run earlier + $this->remove_cache_dir(); + } + $this->create_cache_dir(); + } + + protected function tearDown() + { + if (file_exists($this->cache_dir)) + { + $this->remove_cache_dir(); + } + } + + private function create_cache_dir() + { + $this->get_test_case_helpers()->makedirs($this->cache_dir); + } + + private function remove_cache_dir() + { + $iterator = new DirectoryIterator($this->cache_dir); + foreach ($iterator as $file) + { + if ($file != '.' && $file != '..') + { + unlink($this->cache_dir . '/' . $file); + } + } + rmdir($this->cache_dir); + } + + public function test_cache_driver_file() + { + $driver = new phpbb_cache_driver_file($this->cache_dir); + $driver->put('test_key', 'test_value'); + $driver->save(); + + $this->assertEquals( + 'test_value', + $driver->get('test_key'), + 'File ACM put and get' + ); + } +} diff --git a/tests/class_loader/class_loader_test.php b/tests/class_loader/class_loader_test.php new file mode 100644 index 0000000000..76af4dde37 --- /dev/null +++ b/tests/class_loader/class_loader_test.php @@ -0,0 +1,103 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_class_loader_test extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + global $phpbb_class_loader; + $phpbb_class_loader->unregister(); + + global $phpbb_class_loader_ext; + $phpbb_class_loader_ext->unregister(); + } + + public function tearDown() + { + global $phpbb_class_loader_ext; + $phpbb_class_loader_ext->register(); + + global $phpbb_class_loader; + $phpbb_class_loader->register(); + } + + public function test_resolve_path() + { + $prefix = dirname(__FILE__) . '/'; + $class_loader = new phpbb_class_loader('phpbb_', $prefix . 'includes/'); + + $prefix .= 'includes/'; + + $this->assertEquals( + '', + $class_loader->resolve_path('phpbb_dir'), + 'Class with same name as a directory is unloadable' + ); + + $this->assertEquals( + $prefix . 'class_name.php', + $class_loader->resolve_path('phpbb_class_name'), + 'Top level class' + ); + $this->assertEquals( + $prefix . 'dir/class_name.php', + $class_loader->resolve_path('phpbb_dir_class_name'), + 'Class in a directory' + ); + $this->assertEquals( + $prefix . 'dir/subdir/class_name.php', + $class_loader->resolve_path('phpbb_dir_subdir_class_name'), + 'Class in a sub-directory' + ); + $this->assertEquals( + $prefix . 'dir2/dir2.php', + $class_loader->resolve_path('phpbb_dir2'), + 'Class with name of dir within dir (short class name)' + ); + } + + public function test_resolve_cached() + { + $cache_map = array( + 'class_loader_phpbb_' => array('phpbb_a_cached_name' => 'a/cached_name'), + 'class_loader_phpbb_ext_' => array('phpbb_ext_foo' => 'foo'), + ); + $cache = new phpbb_mock_cache($cache_map); + + $prefix = dirname(__FILE__) . '/'; + $class_loader = new phpbb_class_loader('phpbb_', $prefix . 'includes/', '.php', $cache); + $class_loader_ext = new phpbb_class_loader('phpbb_ext_', $prefix . 'includes/', '.php', $cache); + + $prefix .= 'includes/'; + + $this->assertEquals( + $prefix . 'dir/class_name.php', + $class_loader->resolve_path('phpbb_dir_class_name'), + 'Class in a directory' + ); + + $this->assertFalse($class_loader->resolve_path('phpbb_ext_foo')); + $this->assertFalse($class_loader_ext->resolve_path('phpbb_a_cached_name')); + + $this->assertEquals( + $prefix . 'a/cached_name.php', + $class_loader->resolve_path('phpbb_a_cached_name'), + 'Cached class found' + ); + + $this->assertEquals( + $prefix . 'foo.php', + $class_loader_ext->resolve_path('phpbb_ext_foo'), + 'Cached class found in alternative loader' + ); + + $cache_map['class_loader_phpbb_']['phpbb_dir_class_name'] = 'dir/class_name'; + $cache->check($this, $cache_map); + } +} diff --git a/tests/class_loader/includes/class_name.php b/tests/class_loader/includes/class_name.php new file mode 100644 index 0000000000..e941173cdd --- /dev/null +++ b/tests/class_loader/includes/class_name.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_class_name +{ +} + diff --git a/tests/class_loader/includes/dir.php b/tests/class_loader/includes/dir.php new file mode 100644 index 0000000000..1c8930d8e7 --- /dev/null +++ b/tests/class_loader/includes/dir.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_dir +{ +} + diff --git a/tests/class_loader/includes/dir/class_name.php b/tests/class_loader/includes/dir/class_name.php new file mode 100644 index 0000000000..0675aa8fc5 --- /dev/null +++ b/tests/class_loader/includes/dir/class_name.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_dir_class_name +{ +} + diff --git a/tests/class_loader/includes/dir/subdir/class_name.php b/tests/class_loader/includes/dir/subdir/class_name.php new file mode 100644 index 0000000000..7321a609cc --- /dev/null +++ b/tests/class_loader/includes/dir/subdir/class_name.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_dir_subdir_class_name +{ +} + diff --git a/tests/class_loader/includes/dir2/dir2.php b/tests/class_loader/includes/dir2/dir2.php new file mode 100644 index 0000000000..01cf4086ff --- /dev/null +++ b/tests/class_loader/includes/dir2/dir2.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_dir2 +{ +} + diff --git a/tests/config/config_test.php b/tests/config/config_test.php new file mode 100644 index 0000000000..5845cc4590 --- /dev/null +++ b/tests/config/config_test.php @@ -0,0 +1,120 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_config_test extends phpbb_test_case +{ + public function test_offset_exists() + { + $config = new phpbb_config(array('foo' => 'bar')); + + $this->assertTrue(isset($config['foo'])); + $this->assertFalse(isset($config['foobar'])); + } + + public function test_offset_get() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertEquals('bar', $config['foo']); + } + + public function test_offset_get_missing() + { + $config = new phpbb_config(array()); + $this->assertEquals('', $config['foo']); + } + + public function test_offset_set() + { + $config = new phpbb_config(array()); + $config['foo'] = 'x'; + $this->assertEquals('x', $config['foo']); + } + + public function test_offset_unset_fails() + { + $this->setExpectedTriggerError(E_USER_ERROR); + $config = new phpbb_config(array('foo' => 'x')); + unset($config['foo']); + } + + public function test_count() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertEquals(1, count($config)); + } + + public function test_iterate() + { + $vars = array('foo' => '23', 'bar' => '42'); + $config = new phpbb_config($vars); + + $count = 0; + foreach ($config as $key => $value) + { + $this->assertTrue(isset($vars[$key])); + $this->assertEquals($vars[$key], $value); + + $count++; + } + + $this->assertEquals(count($vars), $count); + } + + public function test_set_overwrite() + { + $config = new phpbb_config(array('foo' => 'x')); + $config->set('foo', 'bar'); + $this->assertEquals('bar', $config['foo']); + } + + public function test_set_new() + { + $config = new phpbb_config(array()); + $config->set('foo', 'bar'); + $this->assertEquals('bar', $config['foo']); + } + + public function test_set_atomic_overwrite() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertTrue($config->set_atomic('foo', 'bar', '23')); + $this->assertEquals('23', $config['foo']); + } + + public function test_set_atomic_new() + { + $config = new phpbb_config(array()); + $this->assertTrue($config->set_atomic('foo', false, '23')); + $this->assertEquals('23', $config['foo']); + } + + public function test_set_atomic_failure() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertFalse($config->set_atomic('foo', 'wrong', '23')); + $this->assertEquals('bar', $config['foo']); + } + + public function test_increment() + { + $config = new phpbb_config(array('foo' => '23')); + $config->increment('foo', 3); + $this->assertEquals(26, $config['foo']); + $config->increment('foo', 1); + $this->assertEquals(27, $config['foo']); + } + + public function test_delete() + { + $config = new phpbb_config(array('foo' => 'bar')); + + $config->delete('foo'); + $this->assertFalse(isset($config['foo'])); + } +} diff --git a/tests/config/db_test.php b/tests/config/db_test.php new file mode 100644 index 0000000000..0b8f73d53a --- /dev/null +++ b/tests/config/db_test.php @@ -0,0 +1,164 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_config_db_test extends phpbb_database_test_case +{ + private $cache; + private $db; + private $config; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function setUp() + { + parent::setUp(); + + $this->cache = new phpbb_mock_cache; + $this->db = $this->new_dbal(); + $this->config = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); + } + + public function test_load_config() + { + $this->assertEquals('23', $this->config['foo']); + $this->assertEquals('42', $this->config['bar']); + } + + public function test_load_cached() + { + $cache = new phpbb_mock_cache(array('config' => array('x' => 'y'))); + $this->config = new phpbb_config_db($this->db, $cache, 'phpbb_config'); + + $this->assertTrue(!isset($this->config['foo'])); + $this->assertEquals('42', $this->config['bar']); + + $this->assertEquals('y', $this->config['x']); + } + + public function test_offset_set() + { + $this->config['foo'] = 'x'; // temporary set + $this->assertEquals('x', $this->config['foo']); + + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); + $this->assertEquals('23', $config2['foo']); + } + + public function test_set_overwrite() + { + $this->config->set('foo', '17'); + $this->assertEquals('17', $this->config['foo']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '17')); + } + + public function test_set_overwrite_uncached() + { + $this->config->set('bar', '17', false); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23')); + } + + public function test_set_new() + { + $this->config->set('foobar', '5'); + $this->assertEquals('5', $this->config['foobar']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23', 'foobar' => '5')); + } + + public function test_set_new_uncached() + { + $this->config->set('foobar', '5', false); + $this->assertEquals('5', $this->config['foobar']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->db, $this->cache, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23')); + } + + public function test_set_atomic_overwrite() + { + $this->assertTrue($this->config->set_atomic('foo', '23', '17')); + $this->assertEquals('17', $this->config['foo']); + } + + public function test_set_atomic_new() + { + $this->assertTrue($this->config->set_atomic('foobar', false, '5')); + $this->assertEquals('5', $this->config['foobar']); + } + + public function test_set_atomic_failure() + { + $this->assertFalse($this->config->set_atomic('foo', 'wrong', '17')); + $this->assertEquals('23', $this->config['foo']); + } + + public function test_increment() + { + $this->config->increment('foo', 3); + $this->assertEquals(26, $this->config['foo']); + $this->config->increment('foo', 1); + $this->assertEquals(27, $this->config['foo']); + } + + public function test_increment_new() + { + $this->config->increment('foobar', 3); + $this->assertEquals(3, $this->config['foobar']);; + } + + public function test_delete() + { + $this->assertTrue(isset($this->config['foo'])); + $this->config->delete('foo'); + $this->cache->checkVarUnset($this, 'foo'); + $this->assertFalse(isset($this->config['foo'])); + + // re-read config and populate cache + $cache2 = new phpbb_mock_cache; + $config2 = new phpbb_config_db($this->db, $cache2, 'phpbb_config'); + $cache2->checkVarUnset($this, 'foo'); + $this->assertFalse(isset($config2['foo'])); + } + + public function test_delete_write_read_not_cacheable() + { + // bar is dynamic + $this->assertTrue(isset($this->config['bar'])); + $this->config->delete('bar'); + $this->cache->checkVarUnset($this, 'bar'); + $this->assertFalse(isset($this->config['bar'])); + + $this->config->set('bar', 'new bar', false); + $this->assertEquals('new bar', $this->config['bar']); + } + + public function test_delete_write_read_cacheable() + { + // foo is not dynamic + $this->assertTrue(isset($this->config['foo'])); + $this->config->delete('foo'); + $this->cache->checkVarUnset($this, 'foo'); + $this->assertFalse(isset($this->config['foo'])); + + $this->config->set('foo', 'new foo', true); + $this->assertEquals('new foo', $this->config['foo']); + } +} diff --git a/tests/config/fixtures/config.xml b/tests/config/fixtures/config.xml new file mode 100644 index 0000000000..9d395b685c --- /dev/null +++ b/tests/config/fixtures/config.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_config"> + <column>config_name</column> + <column>config_value</column> + <column>is_dynamic</column> + <row> + <value>foo</value> + <value>23</value> + <value>0</value> + </row> + <row> + <value>bar</value> + <value>42</value> + <value>1</value> + </row> + </table> +</dataset> diff --git a/tests/cron/ext/testext/cron/dummy_task.php b/tests/cron/ext/testext/cron/dummy_task.php new file mode 100644 index 0000000000..996f5b39cf --- /dev/null +++ b/tests/cron/ext/testext/cron/dummy_task.php @@ -0,0 +1,23 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_ext_testext_cron_dummy_task extends phpbb_cron_task_base +{ + public static $was_run = 0; + + public function run() + { + self::$was_run++; + } + + public function should_run() + { + return true; + } +} diff --git a/tests/cron/includes/cron/task/core/dummy_task.php b/tests/cron/includes/cron/task/core/dummy_task.php new file mode 100644 index 0000000000..6e2e2db636 --- /dev/null +++ b/tests/cron/includes/cron/task/core/dummy_task.php @@ -0,0 +1,23 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_cron_task_core_dummy_task extends phpbb_cron_task_base +{ + public static $was_run = 0; + + public function run() + { + self::$was_run++; + } + + public function should_run() + { + return true; + } +} diff --git a/tests/cron/includes/cron/task/core/second_dummy_task.php b/tests/cron/includes/cron/task/core/second_dummy_task.php new file mode 100644 index 0000000000..8cd0bddfc0 --- /dev/null +++ b/tests/cron/includes/cron/task/core/second_dummy_task.php @@ -0,0 +1,23 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_cron_task_core_second_dummy_task extends phpbb_cron_task_base +{ + public static $was_run = 0; + + public function run() + { + self::$was_run++; + } + + public function should_run() + { + return true; + } +} diff --git a/tests/cron/manager_test.php b/tests/cron/manager_test.php new file mode 100644 index 0000000000..3e40a6d338 --- /dev/null +++ b/tests/cron/manager_test.php @@ -0,0 +1,76 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/includes/cron/task/core/dummy_task.php'; +require_once dirname(__FILE__) . '/includes/cron/task/core/second_dummy_task.php'; +require_once dirname(__FILE__) . '/ext/testext/cron/dummy_task.php'; +require_once dirname(__FILE__) . '/tasks/simple_ready.php'; +require_once dirname(__FILE__) . '/tasks/simple_not_runnable.php'; +require_once dirname(__FILE__) . '/tasks/simple_should_not_run.php'; + +class phpbb_cron_manager_test extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->manager = new phpbb_cron_manager(array( + 'phpbb_cron_task_core_dummy_task', + 'phpbb_cron_task_core_second_dummy_task', + 'phpbb_ext_testext_cron_dummy_task', + )); + $this->task_name = 'phpbb_cron_task_core_dummy_task'; + } + + public function test_manager_finds_shipped_task_by_name() + { + $task = $this->manager->find_task($this->task_name); + $this->assertInstanceOf('phpbb_cron_task_wrapper', $task); + $this->assertEquals($this->task_name, $task->get_name()); + } + + public function test_manager_instantiates_task_by_name() + { + $task = $this->manager->instantiate_task($this->task_name, array()); + $this->assertInstanceOf('phpbb_cron_task_wrapper', $task); + $this->assertEquals($this->task_name, $task->get_name()); + } + + public function test_manager_finds_all_ready_tasks() + { + $tasks = $this->manager->find_all_ready_tasks(); + $this->assertEquals(3, sizeof($tasks)); + } + + public function test_manager_finds_one_ready_task() + { + $task = $this->manager->find_one_ready_task(); + $this->assertInstanceOf('phpbb_cron_task_wrapper', $task); + } + + public function test_manager_finds_only_ready_tasks() + { + $manager = new phpbb_cron_manager(array( + 'phpbb_cron_task_core_simple_ready', + 'phpbb_cron_task_core_simple_not_runnable', + 'phpbb_cron_task_core_simple_should_not_run', + )); + $tasks = $manager->find_all_ready_tasks(); + $task_names = $this->tasks_to_names($tasks); + $this->assertEquals(array('phpbb_cron_task_core_simple_ready'), $task_names); + } + + private function tasks_to_names($tasks) + { + $names = array(); + foreach ($tasks as $task) + { + $names[] = get_class($task->task); + } + return $names; + } +} diff --git a/tests/cron/task_provider_test.php b/tests/cron/task_provider_test.php new file mode 100644 index 0000000000..b42f40bb4a --- /dev/null +++ b/tests/cron/task_provider_test.php @@ -0,0 +1,41 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_cron_task_provider_test extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + $this->extension_manager = new phpbb_mock_extension_manager( + dirname(__FILE__) . '/', + array( + 'testext' => array( + 'ext_name' => 'testext', + 'ext_active' => true, + 'ext_path' => 'ext/testext/' + ), + )); + $this->provider = new phpbb_cron_task_provider($this->extension_manager); + } + + public function test_manager_finds_shipped_tasks() + { + $tasks = array(); + foreach ($this->provider as $task) + { + $tasks[] = $task; + } + sort($tasks); + + $this->assertEquals(array( + 'phpbb_cron_task_core_dummy_task', + 'phpbb_cron_task_core_second_dummy_task', + 'phpbb_ext_testext_cron_dummy_task', + ), $tasks); + } +} diff --git a/tests/cron/tasks/simple_not_runnable.php b/tests/cron/tasks/simple_not_runnable.php new file mode 100644 index 0000000000..837f28f1c0 --- /dev/null +++ b/tests/cron/tasks/simple_not_runnable.php @@ -0,0 +1,13 @@ +<?php + +class phpbb_cron_task_core_simple_not_runnable extends phpbb_cron_task_base +{ + public function run() + { + } + + public function is_runnable() + { + return false; + } +} diff --git a/tests/cron/tasks/simple_ready.php b/tests/cron/tasks/simple_ready.php new file mode 100644 index 0000000000..de5f10e491 --- /dev/null +++ b/tests/cron/tasks/simple_ready.php @@ -0,0 +1,8 @@ +<?php + +class phpbb_cron_task_core_simple_ready extends phpbb_cron_task_base +{ + public function run() + { + } +} diff --git a/tests/cron/tasks/simple_should_not_run.php b/tests/cron/tasks/simple_should_not_run.php new file mode 100644 index 0000000000..c2a41616f6 --- /dev/null +++ b/tests/cron/tasks/simple_should_not_run.php @@ -0,0 +1,13 @@ +<?php + +class phpbb_cron_task_core_simple_should_not_run extends phpbb_cron_task_base +{ + public function run() + { + } + + public function should_run() + { + return false; + } +} diff --git a/tests/datetime/from_format_test.php b/tests/datetime/from_format_test.php new file mode 100644 index 0000000000..c28925272e --- /dev/null +++ b/tests/datetime/from_format_test.php @@ -0,0 +1,57 @@ +<?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/session.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/user.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/datetime.php'; +require_once dirname(__FILE__) . '/../mock/lang.php'; + +class phpbb_datetime_from_format_test extends phpbb_test_case +{ + public function from_format_data() + { + return array( + array( + 'UTC', + 'Y-m-d', + '2012-06-08', + ), + + array( + 'Europe/Berlin', + 'Y-m-d H:i:s', + '2012-06-08 14:01:02', + ), + ); + } + + /** + * @dataProvider from_format_data() + */ + public function test_from_format($timezone, $format, $expected) + { + global $user; + + $user = new phpbb_user(); + $user->timezone = new DateTimeZone($timezone); + $user->lang['datetime'] = array( + 'TODAY' => 'Today', + 'TOMORROW' => 'Tomorrow', + 'YESTERDAY' => 'Yesterday', + 'AGO' => array( + 0 => 'less than a minute ago', + 1 => '%d minute ago', + 2 => '%d minutes ago', + ), + ); + + $timestamp = $user->get_timestamp_from_format($format, $expected, new DateTimeZone($timezone)); + $this->assertEquals($expected, $user->format_date($timestamp, $format, true)); + } +} diff --git a/tests/dbal/case_test.php b/tests/dbal/case_test.php new file mode 100644 index 0000000000..57a1729a39 --- /dev/null +++ b/tests/dbal/case_test.php @@ -0,0 +1,69 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_dbal_case_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); + } + + public function test_case_int() + { + $db = $this->new_dbal(); + + $sql = 'SELECT ' . $db->sql_case('1 = 1', '1', '2') . ' AS test_num + FROM phpbb_config'; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals(1, (int) $db->sql_fetchfield('test_num')); + + $sql = 'SELECT ' . $db->sql_case('1 = 0', '1', '2') . ' AS test_num + FROM phpbb_config'; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals(2, (int) $db->sql_fetchfield('test_num')); + } + + public function test_case_string() + { + $db = $this->new_dbal(); + + $sql = 'SELECT ' . $db->sql_case('1 = 1', "'foo'", "'bar'") . ' AS test_string + FROM phpbb_config'; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals('foo', $db->sql_fetchfield('test_string')); + + $sql = 'SELECT ' . $db->sql_case('1 = 0', "'foo'", "'bar'") . ' AS test_string + FROM phpbb_config'; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals('bar', $db->sql_fetchfield('test_string')); + } + + public function test_case_column() + { + $db = $this->new_dbal(); + + $sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS test_string + FROM phpbb_config + WHERE config_name = 'config1'"; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals('config1', $db->sql_fetchfield('test_string')); + + $sql = 'SELECT ' . $db->sql_case("config_name = 'config1'", 'config_name', 'config_value') . " AS test_string + FROM phpbb_config + WHERE config_value = 'bar'"; + $result = $db->sql_query_limit($sql, 1); + + $this->assertEquals('bar', $db->sql_fetchfield('test_string')); + } +} diff --git a/tests/dbal/concatenate_test.php b/tests/dbal/concatenate_test.php new file mode 100644 index 0000000000..0891fa58a0 --- /dev/null +++ b/tests/dbal/concatenate_test.php @@ -0,0 +1,64 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_dbal_concatenate_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); + } + + public function test_concatenate_string() + { + $db = $this->new_dbal(); + + $sql = 'SELECT config_name, ' . $db->sql_concatenate('config_name', "'" . $db->sql_escape('append') . "'") . ' AS string + FROM phpbb_config'; + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array( + 'config_name' => 'config1', + 'string' => 'config1append', + ), + array( + 'config_name' => 'config2', + 'string' => 'config2append', + ), + ), + $db->sql_fetchrowset($result) + ); + } + + public function test_concatenate_statement() + { + $db = $this->new_dbal(); + + $sql = 'SELECT config_name, ' . $db->sql_concatenate('config_name', 'config_value') . ' AS string + FROM phpbb_config'; + $result = $db->sql_query($sql); + + $db->sql_return_on_error(false); + + $this->assertEquals(array( + array( + 'config_name' => 'config1', + 'string' => 'config1foo', + ), + array( + 'config_name' => 'config2', + 'string' => 'config2bar', + ), + ), + $db->sql_fetchrowset($result) + ); + } +} diff --git a/tests/dbal/fixtures/styles.xml b/tests/dbal/fixtures/styles.xml index 47b384c47f..dcbe39d3b0 100644 --- a/tests/dbal/fixtures/styles.xml +++ b/tests/dbal/fixtures/styles.xml @@ -5,35 +5,39 @@ <column>style_name</column> <column>style_copyright</column> <column>style_active</column> - <column>template_id</column> - <column>theme_id</column> - <column>imageset_id</column> + <column>style_path</column> + <column>bbcode_bitfield</column> + <column>style_parent_id</column> + <column>style_parent_tree</column> <row> <value>1</value> <value>prosilver</value> <value>&copy; phpBB Group</value> <value>1</value> - <value>1</value> - <value>1</value> - <value>1</value> + <value>prosilver</value> + <value>kNg=</value> + <value>0</value> + <value></value> </row> <row> <value>2</value> <value>prosilver2</value> <value>&copy; phpBB Group</value> <value>0</value> - <value>2</value> - <value>2</value> - <value>2</value> + <value>prosilver2</value> + <value>kNg=</value> + <value>0</value> + <value></value> </row> <row> <value>3</value> <value>Prosilver1</value> <value>&copy; phpBB Group</value> <value>0</value> - <value>3</value> - <value>3</value> - <value>3</value> + <value>prosilver1</value> + <value>kNg=</value> + <value>1</value> + <value>prosilver</value> </row> </table> </dataset> diff --git a/tests/dbal/order_lower_test.php b/tests/dbal/order_lower_test.php index b50494d506..84d454742f 100644 --- a/tests/dbal/order_lower_test.php +++ b/tests/dbal/order_lower_test.php @@ -14,7 +14,7 @@ class phpbb_dbal_order_lower_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/styles.xml'); } - public function test_cross_join() + public function test_order_lower() { $db = $this->new_dbal(); @@ -33,27 +33,30 @@ class phpbb_dbal_order_lower_test extends phpbb_database_test_case 'style_name' => 'prosilver', 'style_copyright' => '© phpBB Group', 'style_active' => 1, - 'template_id' => 1, - 'theme_id' => 1, - 'imageset_id' => 1 + 'style_path' => 'prosilver', + 'bbcode_bitfield' => 'kNg=', + 'style_parent_id' => 0, + 'style_parent_tree' => '', ), array( 'style_id' => 3, 'style_name' => 'Prosilver1', 'style_copyright' => '© phpBB Group', 'style_active' => 0, - 'template_id' => 3, - 'theme_id' => 3, - 'imageset_id' => 3 + 'style_path' => 'prosilver1', + 'bbcode_bitfield' => 'kNg=', + 'style_parent_id' => 1, + 'style_parent_tree' => 'prosilver', ), array( 'style_id' => 2, 'style_name' => 'prosilver2', 'style_copyright' => '© phpBB Group', 'style_active' => 0, - 'template_id' => 2, - 'theme_id' => 2, - 'imageset_id' => 2 + 'style_path' => 'prosilver2', + 'bbcode_bitfield' => 'kNg=', + 'style_parent_id' => 0, + 'style_parent_tree' => '', ) ), $db->sql_fetchrowset($result) diff --git a/tests/dbal/schema_test.php b/tests/dbal/schema_test.php new file mode 100644 index 0000000000..2a332fddba --- /dev/null +++ b/tests/dbal/schema_test.php @@ -0,0 +1,38 @@ +<?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_dbal_schema_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); + } + + public function test_config_value_multibyte() + { + $db = $this->new_dbal(); + + $value = str_repeat("\xC3\x84", 255); + $sql = "INSERT INTO phpbb_config + (config_name, config_value) + VALUES ('name', '$value')"; + $result = $db->sql_query($sql); + + $sql = "SELECT config_value + FROM phpbb_config + WHERE config_name = 'name'"; + $result = $db->sql_query_limit($sql, 1); + $row = $db->sql_fetchrow($result); + $db->sql_freeresult($result); + + $this->assertEquals($value, $row['config_value']); + } +} diff --git a/tests/dbal/select_test.php b/tests/dbal/select_test.php index 81cd13b006..bd524100a2 100644 --- a/tests/dbal/select_test.php +++ b/tests/dbal/select_test.php @@ -17,7 +17,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml'); } - public static function return_on_error_select_data() + public function return_on_error_select_data() { return array( array('phpbb_users', "username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), @@ -44,7 +44,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $this->assertEquals($expected, $db->sql_fetchrowset($result)); } - public static function fetchrow_data() + public function fetchrow_data() { return array( array('', array(array('username_clean' => 'barfoo'), @@ -95,7 +95,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $db->sql_freeresult($result); } - public static function fetchfield_data() + public function fetchfield_data() { return array( array('', array('barfoo', 'foobar', 'bertie')), @@ -192,7 +192,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $this->assertEquals($expected, $ary); } - public static function like_expression_data() + public function like_expression_data() { // * = any_char; # = one_char return array( @@ -229,7 +229,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $db->sql_freeresult($result); } - public static function in_set_data() + public function in_set_data() { return array( array('user_id', 3, false, false, array(array('username_clean' => 'bertie'))), @@ -303,7 +303,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $db->sql_freeresult($result); } - public static function build_array_data() + public function build_array_data() { return array( array(array('username_clean' => 'barfoo'), array(array('username_clean' => 'barfoo'))), diff --git a/tests/dbal/write_test.php b/tests/dbal/write_test.php index 596c50a220..987161a831 100644 --- a/tests/dbal/write_test.php +++ b/tests/dbal/write_test.php @@ -16,7 +16,7 @@ class phpbb_dbal_write_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); } - public static function build_array_insert_data() + public function build_array_insert_data() { return array( array(array( @@ -104,7 +104,7 @@ class phpbb_dbal_write_test extends phpbb_database_test_case $db->sql_freeresult($result); } - public static function update_data() + public function update_data() { return array( array( diff --git a/tests/download/http_byte_range_test.php b/tests/download/http_byte_range_test.php new file mode 100644 index 0000000000..23b9169fe3 --- /dev/null +++ b/tests/download/http_byte_range_test.php @@ -0,0 +1,64 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_download.php'; + +class phpbb_download_http_byte_range_test extends phpbb_test_case +{ + public function test_find_range_request() + { + // Missing 'bytes=' prefix + $GLOBALS['request'] = new phpbb_mock_request(); + $GLOBALS['request']->set_header('Range', 'bztes='); + $this->assertEquals(false, phpbb_find_range_request()); + unset($GLOBALS['request']); + + $GLOBALS['request'] = new phpbb_mock_request(); + $_ENV['HTTP_RANGE'] = 'bztes='; + $this->assertEquals(false, phpbb_find_range_request()); + unset($_ENV['HTTP_RANGE']); + + $GLOBALS['request'] = new phpbb_mock_request(); + $GLOBALS['request']->set_header('Range', 'bytes=0-0,123-125'); + $this->assertEquals(array('0-0', '123-125'), phpbb_find_range_request()); + unset($GLOBALS['request']); + } + + /** + * @dataProvider parse_range_request_data() + */ + public function test_parse_range_request($request_array, $filesize, $expected) + { + $this->assertEquals($expected, phpbb_parse_range_request($request_array, $filesize)); + } + + public function parse_range_request_data() + { + return array( + // Does not read until the end of file. + array( + array('3-4'), + 10, + false, + ), + + // Valid request, handle second range. + array( + array('0-0', '120-125'), + 125, + array( + 'byte_pos_start' => 120, + 'byte_pos_end' => 124, + 'bytes_requested' => 5, + 'bytes_total' => 125, + ) + ), + ); + } +} diff --git a/tests/event/dispatcher_test.php b/tests/event/dispatcher_test.php new file mode 100644 index 0000000000..f8fe060d99 --- /dev/null +++ b/tests/event/dispatcher_test.php @@ -0,0 +1,29 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_event_dispatcher_test extends phpbb_test_case +{ + public function test_trigger_event() + { + $dispatcher = new phpbb_event_dispatcher(); + + $dispatcher->addListener('core.test_event', function (phpbb_event_data $event) { + $event['foo'] = $event['foo'] . '2'; + $event['bar'] = $event['bar'] . '2'; + }); + + $foo = 'foo'; + $bar = 'bar'; + + $vars = array('foo', 'bar'); + $result = $dispatcher->trigger_event('core.test_event', compact($vars)); + + $this->assertSame(array('foo' => 'foo2', 'bar' => 'bar2'), $result); + } +} diff --git a/tests/extension/ext/bar/ext.php b/tests/extension/ext/bar/ext.php new file mode 100644 index 0000000000..5585edf9ac --- /dev/null +++ b/tests/extension/ext/bar/ext.php @@ -0,0 +1,24 @@ +<?php + +class phpbb_ext_bar_ext extends phpbb_extension_base +{ + static public $state; + + public function enable_step($old_state) + { + // run 4 steps, then quit + if ($old_state === 4) + { + return false; + } + + if ($old_state === false) + { + $old_state = 0; + } + + self::$state = ++$old_state; + + return self::$state; + } +} diff --git a/tests/extension/ext/bar/my/hidden_class.php b/tests/extension/ext/bar/my/hidden_class.php new file mode 100644 index 0000000000..0261d7c59a --- /dev/null +++ b/tests/extension/ext/bar/my/hidden_class.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_bar_my_hidden_class +{ +} diff --git a/tests/extension/ext/foo/a_class.php b/tests/extension/ext/foo/a_class.php new file mode 100644 index 0000000000..b7be1ad654 --- /dev/null +++ b/tests/extension/ext/foo/a_class.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_foo_a_class +{ +} diff --git a/tests/extension/ext/foo/b_class.php b/tests/extension/ext/foo/b_class.php new file mode 100644 index 0000000000..4645266122 --- /dev/null +++ b/tests/extension/ext/foo/b_class.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_foo_b_class +{ +} diff --git a/tests/extension/ext/foo/ext.php b/tests/extension/ext/foo/ext.php new file mode 100644 index 0000000000..60b3ad1f16 --- /dev/null +++ b/tests/extension/ext/foo/ext.php @@ -0,0 +1,13 @@ +<?php + +class phpbb_ext_foo_ext extends phpbb_extension_base +{ + static public $disabled; + + public function disable_step($old_state) + { + self::$disabled = true; + + return false; + } +} diff --git a/tests/extension/ext/foo/sub/type/alternative.php b/tests/extension/ext/foo/sub/type/alternative.php new file mode 100644 index 0000000000..2ea7353f4b --- /dev/null +++ b/tests/extension/ext/foo/sub/type/alternative.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_foo_sub_type_alternative +{ +} diff --git a/tests/extension/ext/foo/type/alternative.php b/tests/extension/ext/foo/type/alternative.php new file mode 100644 index 0000000000..404b66b965 --- /dev/null +++ b/tests/extension/ext/foo/type/alternative.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_foo_type_alternative +{ +} diff --git a/tests/extension/ext/foo/type/dummy/empty.txt b/tests/extension/ext/foo/type/dummy/empty.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/extension/ext/foo/type/dummy/empty.txt diff --git a/tests/extension/ext/foo/typewrong/error.php b/tests/extension/ext/foo/typewrong/error.php new file mode 100644 index 0000000000..ba22cfae9a --- /dev/null +++ b/tests/extension/ext/foo/typewrong/error.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_foo_typewrong_error +{ +} diff --git a/tests/extension/ext/vendor/moo/ext.php b/tests/extension/ext/vendor/moo/ext.php new file mode 100644 index 0000000000..e0ac1a22cc --- /dev/null +++ b/tests/extension/ext/vendor/moo/ext.php @@ -0,0 +1,13 @@ +<?php + +class phpbb_ext_vendor_moo_ext extends phpbb_extension_base +{ + static public $purged; + + public function purge_step($old_state) + { + self::$purged = true; + + return false; + } +} diff --git a/tests/extension/ext/vendor/moo/feature_class.php b/tests/extension/ext/vendor/moo/feature_class.php new file mode 100644 index 0000000000..c3bcc4451c --- /dev/null +++ b/tests/extension/ext/vendor/moo/feature_class.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_vendor_moo_feature_class +{ +} diff --git a/tests/extension/finder_test.php b/tests/extension/finder_test.php new file mode 100644 index 0000000000..622f404786 --- /dev/null +++ b/tests/extension/finder_test.php @@ -0,0 +1,203 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_extension_finder_test extends phpbb_test_case +{ + protected $extension_manager; + protected $finder; + + public function setUp() + { + $this->extension_manager = new phpbb_mock_extension_manager( + dirname(__FILE__) . '/', + array( + 'foo' => array( + 'ext_name' => 'foo', + 'ext_active' => '1', + 'ext_path' => 'ext/foo/', + ), + 'bar' => array( + 'ext_name' => 'bar', + 'ext_active' => '1', + 'ext_path' => 'ext/bar/', + ), + )); + + $this->finder = $this->extension_manager->get_finder(); + } + + public function test_suffix_get_classes() + { + $classes = $this->finder + ->core_path('includes/default/') + ->extension_suffix('_class') + ->get_classes(); + + sort($classes); + $this->assertEquals( + array( + 'phpbb_default_implementation', + 'phpbb_ext_bar_my_hidden_class', + 'phpbb_ext_foo_a_class', + 'phpbb_ext_foo_b_class', + ), + $classes + ); + } + + public function test_get_directories() + { + $dirs = $this->finder + ->directory('/type') + ->get_directories(); + + sort($dirs); + $this->assertEquals(array( + dirname(__FILE__) . '/ext/foo/type/', + ), $dirs); + } + + public function test_prefix_get_directories() + { + $dirs = $this->finder + ->prefix('t') + ->get_directories(); + + sort($dirs); + $this->assertEquals(array( + dirname(__FILE__) . '/ext/foo/sub/type/', + dirname(__FILE__) . '/ext/foo/type/', + dirname(__FILE__) . '/ext/foo/typewrong/', + ), $dirs); + } + + public function test_prefix_get_classes() + { + $classes = $this->finder + ->core_path('includes/default/') + ->extension_prefix('hidden_') + ->get_classes(); + + sort($classes); + $this->assertEquals( + array( + 'phpbb_default_implementation', + 'phpbb_ext_bar_my_hidden_class', + ), + $classes + ); + } + + public function test_directory_get_classes() + { + $classes = $this->finder + ->core_path('includes/default/') + ->extension_directory('type') + ->get_classes(); + + sort($classes); + $this->assertEquals( + array( + 'phpbb_default_implementation', + 'phpbb_ext_foo_sub_type_alternative', + 'phpbb_ext_foo_type_alternative', + ), + $classes + ); + } + + public function test_absolute_directory_get_classes() + { + $classes = $this->finder + ->directory('/type/') + ->get_classes(); + + sort($classes); + $this->assertEquals( + array( + 'phpbb_ext_foo_type_alternative', + ), + $classes + ); + } + + public function test_sub_directory_get_classes() + { + $classes = $this->finder + ->directory('/sub/type') + ->get_classes(); + + sort($classes); + $this->assertEquals( + array( + 'phpbb_ext_foo_sub_type_alternative', + ), + $classes + ); + } + + public function test_get_classes_create_cache() + { + $cache = new phpbb_mock_cache; + $finder = new phpbb_extension_finder($this->extension_manager, dirname(__FILE__) . '/', $cache, '.php', '_custom_cache_name'); + $files = $finder->suffix('_class.php')->get_files(); + + $expected_files = array( + 'ext/bar/my/hidden_class.php' => 'bar', + 'ext/foo/a_class.php' => 'foo', + 'ext/foo/b_class.php' => 'foo', + ); + + $query = array( + 'core_path' => false, + 'core_suffix' => '_class.php', + 'core_prefix' => false, + 'core_directory' => false, + 'extension_suffix' => '_class.php', + 'extension_prefix' => false, + 'extension_directory' => false, + 'is_dir' => false, + ); + + $cache->checkAssociativeVar($this, '_custom_cache_name', array( + md5(serialize($query)) => $expected_files, + ), false); + } + + public function test_cached_get_files() + { + $query = array( + 'core_path' => 'includes/foo', + 'core_suffix' => false, + 'core_prefix' => false, + 'core_directory' => 'bar', + 'extension_suffix' => false, + 'extension_prefix' => false, + 'extension_directory' => false, + 'is_dir' => false, + ); + + $finder = new phpbb_extension_finder($this->extension_manager, dirname(__FILE__) . '/', new phpbb_mock_cache(array( + '_ext_finder' => array( + md5(serialize($query)) => array('file_name' => 'extension'), + ), + ))); + + $classes = $finder + ->core_path($query['core_path']) + ->core_directory($query['core_directory']) + ->get_files(); + + sort($classes); + $this->assertEquals( + array(dirname(__FILE__) . '/file_name'), + $classes + ); + } +} diff --git a/tests/extension/fixtures/extensions.xml b/tests/extension/fixtures/extensions.xml new file mode 100644 index 0000000000..6eb6fd11a5 --- /dev/null +++ b/tests/extension/fixtures/extensions.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_ext"> + <column>ext_name</column> + <column>ext_active</column> + <column>ext_state</column> + <row> + <value>foo</value> + <value>1</value> + <value></value> + </row> + <row> + <value>vendor/moo</value> + <value>0</value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/extension/includes/default/implementation.php b/tests/extension/includes/default/implementation.php new file mode 100644 index 0000000000..91d5f8aa2f --- /dev/null +++ b/tests/extension/includes/default/implementation.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_default_impl_class implements phpbb_default_interface +{ +} diff --git a/tests/extension/manager_test.php b/tests/extension/manager_test.php new file mode 100644 index 0000000000..45bed247ae --- /dev/null +++ b/tests/extension/manager_test.php @@ -0,0 +1,101 @@ +<?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__) . '/ext/bar/ext.php'; +require_once dirname(__FILE__) . '/ext/foo/ext.php'; +require_once dirname(__FILE__) . '/ext/vendor/moo/ext.php'; + +class phpbb_extension_manager_test extends phpbb_database_test_case +{ + protected $extension_manager; + protected $class_loader; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/extensions.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->extension_manager = new phpbb_extension_manager( + $this->new_dbal(), + 'phpbb_ext', + dirname(__FILE__) . '/', + '.php', + new phpbb_mock_cache + ); + } + + public function test_available() + { + $this->assertEquals(array('bar', 'foo', 'vendor/moo'), array_keys($this->extension_manager->all_available())); + } + + public function test_enabled() + { + $this->assertEquals(array('foo'), array_keys($this->extension_manager->all_enabled())); + } + + public function test_configured() + { + $this->assertEquals(array('foo', 'vendor/moo'), array_keys($this->extension_manager->all_configured())); + } + + public function test_enable() + { + phpbb_ext_bar_ext::$state = 0; + + $this->assertEquals(array('foo'), array_keys($this->extension_manager->all_enabled())); + $this->extension_manager->enable('bar'); + $this->assertEquals(array('bar', 'foo'), array_keys($this->extension_manager->all_enabled())); + $this->assertEquals(array('bar', 'foo', 'vendor/moo'), array_keys($this->extension_manager->all_configured())); + + $this->assertEquals(4, phpbb_ext_bar_ext::$state); + } + + public function test_disable() + { + phpbb_ext_foo_ext::$disabled = false; + + $this->assertEquals(array('foo'), array_keys($this->extension_manager->all_enabled())); + $this->extension_manager->disable('foo'); + $this->assertEquals(array(), array_keys($this->extension_manager->all_enabled())); + $this->assertEquals(array('foo', 'vendor/moo'), array_keys($this->extension_manager->all_configured())); + + $this->assertTrue(phpbb_ext_foo_ext::$disabled); + } + + public function test_purge() + { + phpbb_ext_vendor_moo_ext::$purged = false; + + $this->assertEquals(array('foo'), array_keys($this->extension_manager->all_enabled())); + $this->assertEquals(array('foo', 'vendor/moo'), array_keys($this->extension_manager->all_configured())); + $this->extension_manager->purge('vendor/moo'); + $this->assertEquals(array('foo'), array_keys($this->extension_manager->all_enabled())); + $this->assertEquals(array('foo'), array_keys($this->extension_manager->all_configured())); + + $this->assertTrue(phpbb_ext_vendor_moo_ext::$purged); + } + + public function test_enabled_no_cache() + { + $extension_manager = new phpbb_extension_manager( + $this->new_dbal(), + 'phpbb_ext', + dirname(__FILE__) . '/', + '.php' + ); + + $this->assertEquals(array('foo'), array_keys($extension_manager->all_enabled())); + } + +} diff --git a/tests/functional/extension_controller_test.php b/tests/functional/extension_controller_test.php new file mode 100644 index 0000000000..e9409d9d3f --- /dev/null +++ b/tests/functional/extension_controller_test.php @@ -0,0 +1,144 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @group functional +*/ +class phpbb_functional_extension_controller_test extends phpbb_functional_test_case +{ + protected $phpbb_extension_manager; + /** + * This should only be called once before the tests are run. + * This is used to copy the fixtures to the phpBB install + */ + static public function setUpBeforeClass() + { + global $phpbb_root_path; + parent::setUpBeforeClass(); + + // these directories need to be created before the files can be copied + $directories = array( + $phpbb_root_path . 'ext/error/class/', + $phpbb_root_path . 'ext/error/classtype/', + $phpbb_root_path . 'ext/error/disabled/', + $phpbb_root_path . 'ext/foo/bar/', + $phpbb_root_path . 'ext/foo/bar/styles/prosilver/template/', + $phpbb_root_path . 'ext/foobar/', + $phpbb_root_path . 'ext/foobar/styles/prosilver/template/', + ); + + foreach ($directories as $dir) + { + if (!is_dir($dir)) + { + mkdir($dir, 0777, true); + } + } + + $fixtures = array( + 'error/class/controller.php', + 'error/class/ext.php', + 'error/classtype/controller.php', + 'error/classtype/ext.php', + 'error/disabled/controller.php', + 'error/disabled/ext.php', + 'foo/bar/controller.php', + 'foo/bar/ext.php', + 'foo/bar/styles/prosilver/template/foobar_body.html', + 'foobar/controller.php', + 'foobar/ext.php', + 'foobar/styles/prosilver/template/foobar_body.html', + ); + + foreach ($fixtures as $fixture) + { + if (!copy("tests/functional/fixtures/ext/$fixture", "{$phpbb_root_path}ext/$fixture")) + { + echo 'Could not copy file ' . $fixture; + } + } + } + + public function setUp() + { + parent::setUp(); + + $this->phpbb_extension_manager = $this->get_extension_manager(); + + $this->purge_cache(); + } + + /** + * Check an extension at ./ext/foobar/ which should have the class + * phpbb_ext_foobar_controller + */ + public function test_foobar() + { + $this->phpbb_extension_manager->enable('foobar'); + $crawler = $this->request('GET', 'index.php?ext=foobar'); + $this->assertContains("This is for testing purposes.", $crawler->filter('#page-body')->text()); + $this->phpbb_extension_manager->purge('foobar'); + } + + /** + * Check an extension at ./ext/foo/bar/ which should have the class + * phpbb_ext_foo_bar_controller + */ + public function test_foo_bar() + { + $this->phpbb_extension_manager->enable('foo/bar'); + $crawler = $this->request('GET', 'index.php?ext=foo/bar'); + $this->assertContains("This is for testing purposes.", $crawler->filter('#page-body')->text()); + $this->phpbb_extension_manager->purge('foo/bar'); + } + + /** + * Check the error produced by extension at ./ext/error/class which has class + * phpbb_ext_foobar_controller + */ + public function test_error_class_name() + { + $this->phpbb_extension_manager->enable('error/class'); + $crawler = $this->request('GET', 'index.php?ext=error/class'); + $this->assertContains("The extension error/class is missing a controller class and cannot be accessed through the front-end.", $crawler->filter('#message')->text()); + $this->phpbb_extension_manager->purge('error/class'); + } + + /** + * Check the error produced by extension at ./ext/error/classtype which has class + * phpbb_ext_error_classtype_controller but does not implement phpbb_extension_controller_interface + */ + public function test_error_class_type() + { + $this->phpbb_extension_manager->enable('error/classtype'); + $crawler = $this->request('GET', 'index.php?ext=error/classtype'); + $this->assertContains("The extension controller class phpbb_ext_error_classtype_controller is not an instance of the phpbb_extension_controller_interface.", $crawler->filter('#message')->text()); + $this->phpbb_extension_manager->purge('error/classtype'); + } + + /** + * Check the error produced by extension at ./ext/error/disabled that is (obviously) + * a disabled extension + */ + public function test_error_ext_disabled() + { + $crawler = $this->request('GET', 'index.php?ext=error/disabled'); + $this->assertContains("The extension error/disabled is not enabled", $crawler->filter('#message')->text()); + } + + /** + * Check the error produced by extension at ./ext/error/404 that is (obviously) + * not existant + */ + public function test_error_ext_missing() + { + $crawler = $this->request('GET', 'index.php?ext=error/404'); + $this->assertContains("The extension error/404 does not exist.", $crawler->filter('#message')->text()); + } +} diff --git a/tests/functional/fileupload_form_test.php b/tests/functional/fileupload_form_test.php new file mode 100644 index 0000000000..f7267fa659 --- /dev/null +++ b/tests/functional/fileupload_form_test.php @@ -0,0 +1,69 @@ +<?php +/** + * + * @package testing + * @copyright (c) 2012 phpBB Group + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 + * + */ + +/** + * @group functional + */ +class phpbb_functional_fileupload_form_test extends phpbb_functional_test_case +{ + private $path; + + public function setUp() + { + parent::setUp(); + $this->path = __DIR__ . '/fixtures/files/'; + $this->add_lang('posting'); + $this->login(); + } + + private function upload_file($filename, $mimetype) + { + $file = array( + 'tmp_name' => $this->path . $filename, + 'name' => $filename, + 'type' => $mimetype, + 'size' => filesize($this->path . $filename), + 'error' => UPLOAD_ERR_OK, + ); + + $crawler = $this->client->request( + 'POST', + 'posting.php?mode=reply&f=2&t=1&sid=' . $this->sid, + array('add_file' => $this->lang('ADD_FILE')), + array('fileupload' => $file) + ); + + return $crawler; + } + + public function test_empty_file() + { + $crawler = $this->upload_file('empty.png', 'image/png'); + $this->assertEquals($this->lang('ATTACHED_IMAGE_NOT_IMAGE'), $crawler->filter('div#message p')->text()); + } + + public function test_invalid_extension() + { + $crawler = $this->upload_file('illegal-extension.bif', 'application/octet-stream'); + $this->assertEquals($this->lang('DISALLOWED_EXTENSION', 'bif'), $crawler->filter('p.error')->text()); + } + + public function test_too_large() + { + $this->markTestIncomplete('Functional tests use an admin account which ignores maximum upload size.'); + $crawler = $this->upload_file('too-large.png', 'image/png'); + $this->assertEquals($this->lang('WRONG_FILESIZE', '256', 'KiB'), $crawler->filter('p.error')->text()); + } + + public function test_valid_file() + { + $crawler = $this->upload_file('valid.jpg', 'image/jpeg'); + $this->assertContains($this->lang('POSTED_ATTACHMENTS'), $crawler->filter('#postform h3')->eq(1)->text()); + } +} diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php new file mode 100644 index 0000000000..0deb79acf6 --- /dev/null +++ b/tests/functional/fileupload_remote_test.php @@ -0,0 +1,72 @@ +<?php +/** + * + * @package testing + * @copyright (c) 2012 phpBB Group + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 + * + */ + +/** + * @group functional + */ +class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case +{ + public function setUp() + { + parent::setUp(); + // Only doing this within the functional framework because we need a + // URL + + // Global $config required by unique_id + // Global $user required by fileupload::remote_upload + global $config, $user; + + if (!is_array($config)) + { + $config = array(); + } + + $config['rand_seed'] = ''; + $config['rand_seed_last_update'] = time() + 600; + + $user = new phpbb_mock_user(); + $user->lang = new phpbb_mock_lang(); + } + + public function tearDown() + { + global $config, $user; + $user = null; + $config = array(); + } + + public function test_invalid_extension() + { + $upload = new fileupload('', array('jpg'), 100); + $file = $upload->remote_upload('http://example.com/image.gif'); + $this->assertEquals('URL_INVALID', $file->error[0]); + } + + public function test_non_existant() + { + $upload = new fileupload('', array('jpg'), 100); + $file = $upload->remote_upload('http://example.com/image.jpg'); + $this->assertEquals('EMPTY_REMOTE_DATA', $file->error[0]); + } + + public function test_successful_upload() + { + $upload = new fileupload('', array('gif'), 1000); + $file = $upload->remote_upload($this->root_url . 'styles/prosilver/theme/images/forum_read.gif'); + $this->assertEquals(0, sizeof($file->error)); + $this->assertTrue(file_exists($file->filename)); + } + + public function test_too_large() + { + $upload = new fileupload('', array('gif'), 100); + $file = $upload->remote_upload($this->root_url . 'styles/prosilver/theme/images/forum_read.gif'); + $this->assertEquals('WRONG_FILESIZE', $file->error[0]); + } +} diff --git a/tests/functional/fixtures/ext/error/class/controller.php b/tests/functional/fixtures/ext/error/class/controller.php new file mode 100644 index 0000000000..74bbbee540 --- /dev/null +++ b/tests/functional/fixtures/ext/error/class/controller.php @@ -0,0 +1,14 @@ +<?php + +class phpbb_ext_foobar_controller extends phpbb_extension_controller +{ + public function handle() + { + $this->template->set_filenames(array( + 'body' => 'index_body.html' + )); + + page_header('Test extension'); + page_footer(); + } +} diff --git a/tests/functional/fixtures/ext/error/class/ext.php b/tests/functional/fixtures/ext/error/class/ext.php new file mode 100644 index 0000000000..f97ad2b838 --- /dev/null +++ b/tests/functional/fixtures/ext/error/class/ext.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_ext_error_class_ext extends phpbb_extension_base +{ + +} diff --git a/tests/functional/fixtures/ext/error/classtype/controller.php b/tests/functional/fixtures/ext/error/classtype/controller.php new file mode 100644 index 0000000000..55ac651bdf --- /dev/null +++ b/tests/functional/fixtures/ext/error/classtype/controller.php @@ -0,0 +1,15 @@ +<?php + +class phpbb_ext_error_classtype_controller +{ + public function handle() + { + global $template; + $template->set_filenames(array( + 'body' => 'index_body.html' + )); + + page_header('Test extension'); + page_footer(); + } +} diff --git a/tests/functional/fixtures/ext/error/classtype/ext.php b/tests/functional/fixtures/ext/error/classtype/ext.php new file mode 100644 index 0000000000..35b1cd15a2 --- /dev/null +++ b/tests/functional/fixtures/ext/error/classtype/ext.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_ext_error_classtype_ext extends phpbb_extension_base +{ + +} diff --git a/tests/functional/fixtures/ext/error/disabled/controller.php b/tests/functional/fixtures/ext/error/disabled/controller.php new file mode 100644 index 0000000000..57b913f377 --- /dev/null +++ b/tests/functional/fixtures/ext/error/disabled/controller.php @@ -0,0 +1,14 @@ +<?php + +class phpbb_ext_error_disabled_controller extends phpbb_extension_controller +{ + public function handle() + { + $this->template->set_filenames(array( + 'body' => 'index_body.html' + )); + + page_header('Test extension'); + page_footer(); + } +} diff --git a/tests/functional/fixtures/ext/error/disabled/ext.php b/tests/functional/fixtures/ext/error/disabled/ext.php new file mode 100644 index 0000000000..aec8051848 --- /dev/null +++ b/tests/functional/fixtures/ext/error/disabled/ext.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_ext_error_disabled_ext extends phpbb_extension_base +{ + +} diff --git a/tests/functional/fixtures/ext/foo/bar/controller.php b/tests/functional/fixtures/ext/foo/bar/controller.php new file mode 100644 index 0000000000..3375e317b3 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/controller.php @@ -0,0 +1,14 @@ +<?php + +class phpbb_ext_foo_bar_controller extends phpbb_extension_controller +{ + public function handle() + { + $this->template->set_filenames(array( + 'body' => 'foobar_body.html' + )); + + page_header('Test extension'); + page_footer(); + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/ext.php b/tests/functional/fixtures/ext/foo/bar/ext.php new file mode 100644 index 0000000000..3a2068631e --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/ext.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_ext_foo_bar_ext extends phpbb_extension_base +{ + +} diff --git a/tests/functional/fixtures/ext/foo/bar/styles/prosilver/template/foobar_body.html b/tests/functional/fixtures/ext/foo/bar/styles/prosilver/template/foobar_body.html new file mode 100644 index 0000000000..4addf2666f --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/styles/prosilver/template/foobar_body.html @@ -0,0 +1,5 @@ +<!-- INCLUDE overall_header.html --> + +<div id="welcome">This is for testing purposes.</div> + +<!-- INCLUDE overall_footer.html --> diff --git a/tests/functional/fixtures/ext/foobar/controller.php b/tests/functional/fixtures/ext/foobar/controller.php new file mode 100644 index 0000000000..ff35f12ee0 --- /dev/null +++ b/tests/functional/fixtures/ext/foobar/controller.php @@ -0,0 +1,14 @@ +<?php + +class phpbb_ext_foobar_controller extends phpbb_extension_controller +{ + public function handle() + { + $this->template->set_filenames(array( + 'body' => 'foobar_body.html' + )); + + page_header('Test extension'); + page_footer(); + } +} diff --git a/tests/functional/fixtures/ext/foobar/ext.php b/tests/functional/fixtures/ext/foobar/ext.php new file mode 100644 index 0000000000..7cf443d369 --- /dev/null +++ b/tests/functional/fixtures/ext/foobar/ext.php @@ -0,0 +1,6 @@ +<?php + +class phpbb_ext_foobar_ext extends phpbb_extension_base +{ + +} diff --git a/tests/functional/fixtures/ext/foobar/styles/prosilver/template/foobar_body.html b/tests/functional/fixtures/ext/foobar/styles/prosilver/template/foobar_body.html new file mode 100644 index 0000000000..4addf2666f --- /dev/null +++ b/tests/functional/fixtures/ext/foobar/styles/prosilver/template/foobar_body.html @@ -0,0 +1,5 @@ +<!-- INCLUDE overall_header.html --> + +<div id="welcome">This is for testing purposes.</div> + +<!-- INCLUDE overall_footer.html --> diff --git a/tests/functional/fixtures/files/empty.png b/tests/functional/fixtures/files/empty.png new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/functional/fixtures/files/empty.png diff --git a/tests/functional/fixtures/files/illegal-extension.bif b/tests/functional/fixtures/files/illegal-extension.bif Binary files differnew file mode 100644 index 0000000000..3cd5038e38 --- /dev/null +++ b/tests/functional/fixtures/files/illegal-extension.bif diff --git a/tests/functional/fixtures/files/too-large.png b/tests/functional/fixtures/files/too-large.png Binary files differnew file mode 100644 index 0000000000..ed4b0abd80 --- /dev/null +++ b/tests/functional/fixtures/files/too-large.png diff --git a/tests/functional/fixtures/files/valid.jpg b/tests/functional/fixtures/files/valid.jpg Binary files differnew file mode 100644 index 0000000000..95a87ddbdf --- /dev/null +++ b/tests/functional/fixtures/files/valid.jpg diff --git a/tests/functions_acp/build_cfg_template_test.php b/tests/functions_acp/build_cfg_template_test.php new file mode 100644 index 0000000000..12121f6678 --- /dev/null +++ b/tests/functions_acp/build_cfg_template_test.php @@ -0,0 +1,191 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case +{ + public function build_cfg_template_text_data() + { + return array( + array( + array('text', 20, 255), + 'key_name', + array('config_key_name' => '1'), + 'config_key_name', + array(), + '<input id="key_name" type="text" size="20" maxlength="255" name="config[config_key_name]" value="1" />', + ), + array( + array('password', 20, 128), + 'key_name', + array('config_key_name' => '2'), + 'config_key_name', + array(), + '<input id="key_name" type="password" size="20" maxlength="128" name="config[config_key_name]" value="2" autocomplete="off" />', + ), + array( + array('text', 0, 255), + 'key_name', + array('config_key_name' => '3'), + 'config_key_name', + array(), + '<input id="key_name" type="text" maxlength="255" name="config[config_key_name]" value="3" />', + ), + ); + } + + /** + * @dataProvider build_cfg_template_text_data + */ + public function test_build_cfg_template_text($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } + + public function build_cfg_template_dimension_data() + { + return array( + array( + array('dimension', 20, 255), + 'key_name', + array('config_key_name_width' => 10, 'config_key_name_height' => 20), + 'config_key_name', + array(), + '<input id="key_name" type="text" size="20" maxlength="255" name="config[config_key_name_width]" value="10" /> x <input type="text" size="20" maxlength="255" name="config[config_key_name_height]" value="20" />', + ), + array( + array('dimension', 0, 255), + 'key_name', + array('config_key_name_width' => 10, 'config_key_name_height' => 20), + 'config_key_name', + array(), + '<input id="key_name" type="text" maxlength="255" name="config[config_key_name_width]" value="10" /> x <input type="text" maxlength="255" name="config[config_key_name_height]" value="20" />', + ), + ); + } + + /** + * @dataProvider build_cfg_template_dimension_data + */ + public function test_build_cfg_template_dimension($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } + + public function build_cfg_template_textarea_data() + { + return array( + array( + array('textarea', 5, 30), + 'key_name', + array('config_key_name' => 'phpBB'), + 'config_key_name', + array(), + '<textarea id="key_name" name="config[config_key_name]" rows="5" cols="30">phpBB</textarea>', + ), + ); + } + + /** + * @dataProvider build_cfg_template_textarea_data + */ + public function test_build_cfg_template_textarea($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } + + public function build_cfg_template_radio_data() + { + return array( + array( + array('radio', 'enabled_disabled'), + 'key_name', + array('config_key_name' => '0'), + 'config_key_name', + array(), + '<label><input type="radio" id="key_name" name="config[config_key_name]" value="1" class="radio" /> ENABLED</label><label><input type="radio" name="config[config_key_name]" value="0" checked="checked" class="radio" /> DISABLED</label>', + ), + array( + array('radio', 'enabled_disabled'), + 'key_name', + array('config_key_name' => '1'), + 'config_key_name', + array(), + '<label><input type="radio" id="key_name" name="config[config_key_name]" value="1" checked="checked" class="radio" /> ENABLED</label><label><input type="radio" name="config[config_key_name]" value="0" class="radio" /> DISABLED</label>', + ), + array( + array('radio', 'yes_no'), + 'key_name', + array('config_key_name' => '0'), + 'config_key_name', + array(), + '<label><input type="radio" id="key_name" name="config[config_key_name]" value="1" class="radio" /> YES</label><label><input type="radio" name="config[config_key_name]" value="0" checked="checked" class="radio" /> NO</label>', + ), + array( + array('radio', 'yes_no'), + 'key_name', + array('config_key_name' => '1'), + 'config_key_name', + array(), + '<label><input type="radio" id="key_name" name="config[config_key_name]" value="1" checked="checked" class="radio" /> YES</label><label><input type="radio" name="config[config_key_name]" value="0" class="radio" /> NO</label>', + ), + ); + } + + /** + * @dataProvider build_cfg_template_radio_data + */ + public function test_build_cfg_template_radio($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } + + public function build_cfg_template_append_data() + { + return array( + array( + array('textarea', 5, 30), + 'key_name', + array('config_key_name' => 'phpBB'), + 'config_key_name', + array('append' => 'Bertie is cool!'), + '<textarea id="key_name" name="config[config_key_name]" rows="5" cols="30">phpBB</textarea>Bertie is cool!', + ), + ); + } + + /** + * @dataProvider build_cfg_template_append_data + */ + public function test_build_cfg_template_append($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } +} diff --git a/tests/functions_acp/build_select_test.php b/tests/functions_acp/build_select_test.php new file mode 100644 index 0000000000..aca49b7655 --- /dev/null +++ b/tests/functions_acp/build_select_test.php @@ -0,0 +1,55 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_built_select_test extends phpbb_test_case +{ + public function build_select_data() + { + return array( + array( + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + '<option value="test">TEST</option><option value="second">SEC_OPTION</option>', + ), + array( + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + '<option value="test" selected="selected">TEST</option><option value="second">SEC_OPTION</option>', + ), + array( + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'second', + '<option value="test">TEST</option><option value="second" selected="selected">SEC_OPTION</option>', + ), + ); + } + + /** + * @dataProvider build_select_data + */ + public function test_build_select($option_ary, $option_default, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_select($option_ary, $option_default)); + } +} diff --git a/tests/functions_acp/h_radio_test.php b/tests/functions_acp/h_radio_test.php new file mode 100644 index 0000000000..a61f2e8975 --- /dev/null +++ b/tests/functions_acp/h_radio_test.php @@ -0,0 +1,120 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_h_radio_test extends phpbb_test_case +{ + public function h_radio_data() + { + return array( + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + false, + false, + '<label><input type="radio" name="test_name" value="test" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + false, + false, + '<label><input type="radio" name="test_name" value="test" checked="checked" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + 'test_id', + false, + '<label><input type="radio" name="test_name" id="test_id" value="test" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + 'test_id', + false, + '<label><input type="radio" name="test_name" id="test_id" value="test" checked="checked" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" class="radio" /> SEC_OPTION</label>', + ), + + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + false, + 'k', + '<label><input type="radio" name="test_name" value="test" accesskey="k" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" accesskey="k" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + false, + 'k', + '<label><input type="radio" name="test_name" value="test" checked="checked" accesskey="k" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" accesskey="k" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + 'test_id', + 'k', + '<label><input type="radio" name="test_name" id="test_id" value="test" accesskey="k" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" accesskey="k" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + 'test_id', + 'k', + '<label><input type="radio" name="test_name" id="test_id" value="test" checked="checked" accesskey="k" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" accesskey="k" class="radio" /> SEC_OPTION</label>', + ), + ); + } + + /** + * @dataProvider h_radio_data + */ + public function test_h_radio($name, $input_ary, $input_default, $id, $key, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, h_radio($name, $input_ary, $input_default, $id, $key)); + } +} diff --git a/tests/functions_acp/validate_config_vars_test.php b/tests/functions_acp/validate_config_vars_test.php new file mode 100644 index 0000000000..7cd7fa3892 --- /dev/null +++ b/tests/functions_acp/validate_config_vars_test.php @@ -0,0 +1,158 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_validate_config_vars_test extends phpbb_test_case +{ + /** + * Data sets that don't throw an error. + */ + public function validate_config_vars_fit_data() + { + return array( + array( + array( + 'test_bool' => array('lang' => 'TEST_BOOL', 'validate' => 'bool'), + 'test_string' => array('lang' => 'TEST_STRING', 'validate' => 'string'), + 'test_string' => array('lang' => 'TEST_STRING', 'validate' => 'string'), + 'test_string_128' => array('lang' => 'TEST_STRING_128', 'validate' => 'string:128'), + 'test_string_128' => array('lang' => 'TEST_STRING_128', 'validate' => 'string:128'), + 'test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64'), + 'test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64'), + 'test_int' => array('lang' => 'TEST_INT', 'validate' => 'int'), + 'test_int_32' => array('lang' => 'TEST_INT', 'validate' => 'int:32'), + 'test_int_32_64' => array('lang' => 'TEST_INT', 'validate' => 'int:32:64'), + 'test_lang' => array('lang' => 'TEST_LANG', 'validate' => 'lang'), + /* + 'test_sp' => array('lang' => 'TEST_SP', 'validate' => 'script_path'), + 'test_rpath' => array('lang' => 'TEST_RPATH', 'validate' => 'rpath'), + 'test_rwpath' => array('lang' => 'TEST_RWPATH', 'validate' => 'rwpath'), + 'test_path' => array('lang' => 'TEST_PATH', 'validate' => 'path'), + 'test_wpath' => array('lang' => 'TEST_WPATH', 'validate' => 'wpath'), + */ + ), + array( + 'test_bool' => true, + 'test_string' => str_repeat('a', 255), + 'test_string' => str_repeat("\xC3\x84", 255), + 'test_string_128' => str_repeat('a', 128), + 'test_string_128' => str_repeat("\xC3\x84", 128), + 'test_string_32_64' => str_repeat('a', 48), + 'test_string_32_64' => str_repeat("\xC3\x84", 48), + 'test_int' => 128, + 'test_int_32' => 32, + 'test_int_32_64' => 48, + 'test_lang' => 'en', + ), + ), + ); + } + + /** + * @dataProvider validate_config_vars_fit_data + */ + public function test_validate_config_vars_fit($test_data, $cfg_array) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_config_vars($test_data, $cfg_array, $phpbb_error); + + $this->assertEquals(array(), $phpbb_error); + } + + /** + * Data sets that throw the error. + */ + public function validate_config_vars_error_data() + { + return array( + array( + array('test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64')), + array('test_string_32_64' => str_repeat('a', 20)), + array('SETTING_TOO_SHORT'), + ), + array( + array('test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64')), + array('test_string_32_64' => str_repeat("\xC3\x84", 20)), + array('SETTING_TOO_SHORT'), + ), + array( + array('test_string' => array('lang' => 'TEST_STRING', 'validate' => 'string')), + array('test_string' => str_repeat('a', 256)), + array('SETTING_TOO_LONG'), + ), + array( + array('test_string' => array('lang' => 'TEST_STRING', 'validate' => 'string')), + array('test_string' => str_repeat("\xC3\x84", 256)), + array('SETTING_TOO_LONG'), + ), + array( + array('test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64')), + array('test_string_32_64' => str_repeat('a', 65)), + array('SETTING_TOO_LONG'), + ), + array( + array('test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64')), + array('test_string_32_64' => str_repeat("\xC3\x84", 65)), + array('SETTING_TOO_LONG'), + ), + + array( + array('test_int_32' => array('lang' => 'TEST_INT', 'validate' => 'int:32')), + array('test_int_32' => 31), + array('SETTING_TOO_LOW'), + ), + array( + array('test_int_32_64' => array('lang' => 'TEST_INT', 'validate' => 'int:32:64')), + array('test_int_32_64' => 31), + array('SETTING_TOO_LOW'), + ), + array( + array('test_int_32_64' => array('lang' => 'TEST_INT', 'validate' => 'int:32:64')), + array('test_int_32_64' => 65), + array('SETTING_TOO_BIG'), + ), + array( + array( + 'test_int_min' => array('lang' => 'TEST_INT_MIN', 'validate' => 'int:32:64'), + 'test_int_max' => array('lang' => 'TEST_INT_MAX', 'validate' => 'int:32:64'), + ), + array( + 'test_int_min' => 52, + 'test_int_max' => 48, + ), + array('SETTING_TOO_LOW'), + ), + array( + array('test_lang' => array('lang' => 'TEST_LANG', 'validate' => 'lang')), + array('test_lang' => 'this_is_no_language'), + array('WRONG_DATA_LANG'), + ), + ); + } + + /** + * @dataProvider validate_config_vars_error_data + */ + public function test_validate_config_vars_error($test_data, $cfg_array, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_config_vars($test_data, $cfg_array, $phpbb_error); + + $this->assertEquals($expected, $phpbb_error); + } +} diff --git a/tests/functions_acp/validate_range_test.php b/tests/functions_acp/validate_range_test.php new file mode 100644 index 0000000000..8606158251 --- /dev/null +++ b/tests/functions_acp/validate_range_test.php @@ -0,0 +1,170 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_validate_range_test extends phpbb_test_case +{ + /** + * Data sets that don't throw an error. + */ + public function validate_range_data_fit() + { + return array( + array(array(array('column_type' => 'BOOL', 'lang' => 'TEST', 'value' => 0))), + array(array(array('column_type' => 'BOOL', 'lang' => 'TEST', 'value' => 1))), + + array(array(array('column_type' => 'USINT', 'lang' => 'TEST', 'value' => 0))), + array(array(array('column_type' => 'USINT', 'lang' => 'TEST', 'value' => 65535))), + array(array(array('column_type' => 'USINT:32:128', 'lang' => 'TEST', 'value' => 35))), + + array(array(array('column_type' => 'UINT', 'lang' => 'TEST', 'value' => 0))), + array(array(array('column_type' => 'UINT', 'lang' => 'TEST', 'value' => (int) 0x7fffffff))), + array(array(array('column_type' => 'UINT:32:128', 'lang' => 'TEST', 'value' => 35))), + + array(array(array('column_type' => 'INT', 'lang' => 'TEST', 'value' => (int) -2147483648))), + array(array(array('column_type' => 'INT', 'lang' => 'TEST', 'value' => (int) 0x7fffffff))), + array(array(array('column_type' => 'INT:-32:128', 'lang' => 'TEST', 'value' => -28))), + array(array(array('column_type' => 'INT:-32:128', 'lang' => 'TEST', 'value' => 35))), + + array(array(array('column_type' => 'TINT', 'lang' => 'TEST', 'value' => -128))), + array(array(array('column_type' => 'TINT', 'lang' => 'TEST', 'value' => 127))), + array(array(array('column_type' => 'TINT:-32:64', 'lang' => 'TEST', 'value' => -16))), + array(array(array('column_type' => 'TINT:-32:64', 'lang' => 'TEST', 'value' => 16))), + + array(array(array('column_type' => 'VCHAR', 'lang' => 'TEST', 'value' => ''))), + array(array(array('column_type' => 'VCHAR', 'lang' => 'TEST', 'value' => str_repeat('a', 255)))), + array(array(array('column_type' => 'VCHAR', 'lang' => 'TEST', 'value' => str_repeat("\xC3\x84", 255)))), + array(array(array('column_type' => 'VCHAR:128', 'lang' => 'TEST', 'value' => str_repeat('a', 128)))), + array(array(array('column_type' => 'VCHAR:128', 'lang' => 'TEST', 'value' => str_repeat("\xC3\x84", 128)))), + ); + } + + /** + * @dataProvider validate_range_data_fit + */ + public function test_validate_range_fit($test_data) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_range($test_data, $phpbb_error); + + $this->assertEquals(array(), $phpbb_error); + } + + /** + * Data sets that throw the SETTING_TOO_LOW-error. + */ + public function validate_range_data_too_low() + { + return array( + array(array(array('column_type' => 'BOOL', 'lang' => 'TEST', 'value' => -1))), + + array(array(array('column_type' => 'USINT', 'lang' => 'TEST', 'value' => -1))), + array(array(array('column_type' => 'USINT:32:128', 'lang' => 'TEST', 'value' => 31))), + + array(array(array('column_type' => 'UINT', 'lang' => 'TEST', 'value' => -1))), + array(array(array('column_type' => 'UINT:32:128', 'lang' => 'TEST', 'value' => 31))), + + array(array(array('column_type' => 'INT', 'lang' => 'TEST', 'value' => ((int) -2147483648) - 1))), + array(array(array('column_type' => 'INT:32:128', 'lang' => 'TEST', 'value' => 31))), + array(array(array('column_type' => 'INT:-32:128', 'lang' => 'TEST', 'value' => -33))), + + array(array(array('column_type' => 'TINT', 'lang' => 'TEST', 'value' => -129))), + array(array(array('column_type' => 'TINT:32:64', 'lang' => 'TEST', 'value' => 31))), + array(array(array('column_type' => 'TINT:-32:64', 'lang' => 'TEST', 'value' => -33))), + ); + } + + /** + * @dataProvider validate_range_data_too_low + */ + public function test_validate_range_too_low($test_data) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_range($test_data, $phpbb_error); + + $this->assertEquals(array('SETTING_TOO_LOW'), $phpbb_error); + } + + /** + * Data sets that throw the SETTING_TOO_BIG-error. + */ + public function validate_range_data_too_big() + { + return array( + array(array(array('column_type' => 'BOOL', 'lang' => 'TEST', 'value' => 2))), + + array(array(array('column_type' => 'USINT', 'lang' => 'TEST', 'value' => 65536))), + array(array(array('column_type' => 'USINT:32:128', 'lang' => 'TEST', 'value' => 129))), + + array(array(array('column_type' => 'UINT', 'lang' => 'TEST', 'value' => ((int) 0x7fffffff) + 1))), + array(array(array('column_type' => 'UINT:32:128', 'lang' => 'TEST', 'value' => 129))), + + array(array(array('column_type' => 'INT', 'lang' => 'TEST', 'value' => ((int) 0x7fffffff) + 1))), + array(array(array('column_type' => 'INT:-32:-16', 'lang' => 'TEST', 'value' => -15))), + array(array(array('column_type' => 'INT:-32:128', 'lang' => 'TEST', 'value' => 129))), + + array(array(array('column_type' => 'TINT', 'lang' => 'TEST', 'value' => 128))), + array(array(array('column_type' => 'TINT:-32:-16', 'lang' => 'TEST', 'value' => -15))), + array(array(array('column_type' => 'TINT:-32:64', 'lang' => 'TEST', 'value' => 65))), + ); + } + + /** + * @dataProvider validate_range_data_too_big + */ + public function test_validate_range_too_big($test_data) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_range($test_data, $phpbb_error); + + $this->assertEquals(array('SETTING_TOO_BIG'), $phpbb_error); + } + + /** + * Data sets that throw the SETTING_TOO_LONG-error. + */ + public function validate_range_data_too_long() + { + return array( + array(array(array('column_type' => 'VCHAR', 'lang' => 'TEST', 'value' => str_repeat('a', 256)))), + array(array(array('column_type' => 'VCHAR', 'lang' => 'TEST', 'value' => str_repeat("\xC3\x84", 256)))), + array(array(array('column_type' => 'VCHAR:128', 'lang' => 'TEST', 'value' => str_repeat('a', 129)))), + array(array(array('column_type' => 'VCHAR:128', 'lang' => 'TEST', 'value' => str_repeat("\xC3\x84", 129)))), + ); + } + + /** + * @dataProvider validate_range_data_too_long + */ + public function test_validate_range_too_long($test_data) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_range($test_data, $phpbb_error); + + $this->assertEquals(array('SETTING_TOO_LONG'), $phpbb_error); + } +} diff --git a/tests/group_positions/fixtures/group_positions.xml b/tests/group_positions/fixtures/group_positions.xml new file mode 100644 index 0000000000..00ea18fe4f --- /dev/null +++ b/tests/group_positions/fixtures/group_positions.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_groups"> + <column>group_id</column> + <column>group_teampage</column> + <column>group_legend</column> + <column>group_desc</column> + <row> + <value>1</value> + <value>0</value> + <value>0</value> + <value></value> + </row> + <row> + <value>2</value> + <value>1</value> + <value>0</value> + <value></value> + </row> + <row> + <value>3</value> + <value>2</value> + <value>1</value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/group_positions/group_positions_test.php b/tests/group_positions/group_positions_test.php new file mode 100644 index 0000000000..c17e25511b --- /dev/null +++ b/tests/group_positions/group_positions_test.php @@ -0,0 +1,287 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + + +class phpbb_group_positions_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/group_positions.xml'); + } + + public function get_group_value_data() + { + return array( + array('teampage', 1, 0), + array('teampage', 2, 1), + array('legend', 1, 0), + array('legend', 3, 1), + ); + } + + /** + * @dataProvider get_group_value_data + */ + public function test_get_group_value($field, $group_id, $expected) + { + global $db; + + $db = $this->new_dbal(); + + $test_class = new phpbb_group_positions($db, $field); + $this->assertEquals($expected, $test_class->get_group_value($group_id)); + } + + public function get_group_count_data() + { + return array( + array('teampage', 2), + array('legend', 1), + ); + } + + /** + * @dataProvider get_group_count_data + */ + public function test_get_group_count($field, $expected) + { + global $db; + + $db = $this->new_dbal(); + + $test_class = new phpbb_group_positions($db, $field); + $this->assertEquals($expected, $test_class->get_group_count()); + } + + public function add_group_data() + { + return array( + array('teampage', 1, array( + array('group_id' => 1, 'group_teampage' => 3, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 2, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + ); + } + + /** + * @dataProvider add_group_data + */ + public function test_add_group($field, $group_id, $expected) + { + global $db; + + $db = $this->new_dbal(); + $test_class = new phpbb_group_positions($db, $field); + $test_class->add_group($group_id); + + $result = $db->sql_query('SELECT group_id, group_teampage, group_legend + FROM ' . GROUPS_TABLE . ' + ORDER BY group_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + } + + public function delete_group_data() + { + return array( + array('teampage', 1, false, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 2, false, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1), + )), + array('teampage', 3, false, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 0, 'group_legend' => 1), + )), + array('teampage', 1, true, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 2, true, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1), + )), + array('teampage', 3, true, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + ); + } + + /** + * @dataProvider delete_group_data + */ + public function test_delete_group($field, $group_id, $skip_group, $expected) + { + global $db; + + $db = $this->new_dbal(); + $test_class = new phpbb_group_positions($db, $field); + $test_class->delete_group($group_id, $skip_group); + + $result = $db->sql_query('SELECT group_id, group_teampage, group_legend + FROM ' . GROUPS_TABLE . ' + ORDER BY group_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + } + + public function move_up_data() + { + return array( + array('teampage', 1, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 2, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 3, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 2, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1), + )), + ); + } + + /** + * @dataProvider move_up_data + */ + public function test_move_up($field, $group_id, $expected) + { + global $db; + + $db = $this->new_dbal(); + $test_class = new phpbb_group_positions($db, $field); + $test_class->move_up($group_id); + + $result = $db->sql_query('SELECT group_id, group_teampage, group_legend + FROM ' . GROUPS_TABLE . ' + ORDER BY group_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + } + + public function move_down_data() + { + return array( + array('teampage', 1, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 2, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 2, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1), + )), + array('teampage', 3, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + ); + } + + /** + * @dataProvider move_down_data + */ + public function test_move_down($field, $group_id, $expected) + { + global $db; + + $db = $this->new_dbal(); + $test_class = new phpbb_group_positions($db, $field); + $test_class->move_down($group_id); + + $result = $db->sql_query('SELECT group_id, group_teampage, group_legend + FROM ' . GROUPS_TABLE . ' + ORDER BY group_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + } + + public function move_data() + { + return array( + array('teampage', 1, 1, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 1, -1, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 3, 3, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 2, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1), + )), + array('teampage', 2, 0, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + array('teampage', 2, -1, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 2, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1), + )), + array('teampage', 2, -3, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 2, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1), + )), + array('teampage', 3, -1, array( + array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0), + array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0), + array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1), + )), + ); + } + + /** + * @dataProvider move_data + */ + public function test_move($field, $group_id, $increment, $expected) + { + global $db; + + $db = $this->new_dbal(); + $test_class = new phpbb_group_positions($db, $field); + $test_class->move($group_id, $increment); + + $result = $db->sql_query('SELECT group_id, group_teampage, group_legend + FROM ' . GROUPS_TABLE . ' + ORDER BY group_id ASC'); + + $this->assertEquals($expected, $db->sql_fetchrowset($result)); + } +} + diff --git a/tests/lock/db_test.php b/tests/lock/db_test.php new file mode 100644 index 0000000000..f7b1557a0c --- /dev/null +++ b/tests/lock/db_test.php @@ -0,0 +1,83 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 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_lock_db_test extends phpbb_database_test_case +{ + private $db; + private $config; + private $lock; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); + } + + public function setUp() + { + global $db, $config; + + $db = $this->db = $this->new_dbal(); + $config = $this->config = new phpbb_config(array('rand_seed' => '', 'rand_seed_last_update' => '0')); + set_config(null, null, null, $this->config); + $this->lock = new phpbb_lock_db('test_lock', $this->config, $this->db); + } + + public function test_new_lock() + { + $this->assertTrue($this->lock->acquire()); + $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); + + $lock2 = new phpbb_lock_db('test_lock', $this->config, $this->db); + $this->assertFalse($lock2->acquire()); + + $this->lock->release(); + $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); + } + + public function test_expire_lock() + { + $lock = new phpbb_lock_db('foo_lock', $this->config, $this->db); + $this->assertTrue($lock->acquire()); + } + + public function test_double_lock() + { + $this->assertTrue($this->lock->acquire()); + $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); + + $value = $this->config['test_lock']; + + $this->assertFalse($this->lock->acquire()); + $this->assertEquals($value, $this->config['test_lock'], 'Second lock failed'); + + $this->lock->release(); + $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); + } + + public function test_double_unlock() + { + $this->assertTrue($this->lock->acquire()); + $this->assertFalse(empty($this->config['test_lock']), 'First lock is acquired'); + + $this->lock->release(); + $this->assertEquals('0', $this->config['test_lock'], 'First lock is released'); + + $lock2 = new phpbb_lock_db('test_lock', $this->config, $this->db); + $this->assertTrue($lock2->acquire()); + $this->assertFalse(empty($this->config['test_lock']), 'Second lock is acquired'); + + $this->lock->release(); + $this->assertFalse(empty($this->config['test_lock']), 'Double release of first lock is ignored'); + + $lock2->release(); + $this->assertEquals('0', $this->config['test_lock'], 'Second lock is released'); + } +} diff --git a/tests/lock/fixtures/config.xml b/tests/lock/fixtures/config.xml new file mode 100644 index 0000000000..f36c8b929a --- /dev/null +++ b/tests/lock/fixtures/config.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_config"> + <column>config_name</column> + <column>config_value</column> + <column>is_dynamic</column> + <row> + <value>foo_lock</value> + <value>1 abcd</value> + <value>1</value> + </row> + </table> +</dataset> diff --git a/tests/mock/cache.php b/tests/mock/cache.php index 650545c3d6..c6d08afef0 100644 --- a/tests/mock/cache.php +++ b/tests/mock/cache.php @@ -2,21 +2,18 @@ /** * * @package testing -* @copyright (c) 2008 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ -class phpbb_mock_cache +class phpbb_mock_cache implements phpbb_cache_driver_interface { + protected $data; + public function __construct($data = array()) { $this->data = $data; - - if (!isset($this->data['_bots'])) - { - $this->data['_bots'] = array(); - } } public function get($var_name) @@ -35,14 +32,6 @@ class phpbb_mock_cache } /** - * Obtain active bots - */ - public function obtain_bots() - { - return $this->data['_bots']; - } - - /** * Obtain list of word censors. We don't need to parse them here, * that is tested elsewhere. */ @@ -64,17 +53,32 @@ class phpbb_mock_cache ); } - public function set_bots($bots) + public function checkVar(PHPUnit_Framework_Assert $test, $var_name, $data) { - $this->data['_bots'] = $bots; + $test->assertTrue(isset($this->data[$var_name])); + $test->assertEquals($data, $this->data[$var_name]); } - public function checkVar(PHPUnit_Framework_Assert $test, $var_name, $data) + public function checkAssociativeVar(PHPUnit_Framework_Assert $test, $var_name, $data, $sort = true) { $test->assertTrue(isset($this->data[$var_name])); + + if ($sort) + { + foreach ($this->data[$var_name] as &$content) + { + sort($content); + } + } + $test->assertEquals($data, $this->data[$var_name]); } + public function checkVarUnset(PHPUnit_Framework_Assert $test, $var_name) + { + $test->assertFalse(isset($this->data[$var_name])); + } + public function check(PHPUnit_Framework_Assert $test, $data, $ignore_db_info = true) { $cache_data = $this->data; @@ -91,5 +95,53 @@ class phpbb_mock_cache $test->assertEquals($data, $cache_data); } -} + function load() + { + } + function unload() + { + } + function save() + { + } + function tidy() + { + } + function purge() + { + } + function destroy($var_name, $table = '') + { + unset($this->data[$var_name]); + } + public function _exists($var_name) + { + } + public function sql_load($query) + { + } + public function sql_save($query, &$query_result, $ttl) + { + } + public function sql_exists($query_id) + { + } + public function sql_fetchrow($query_id) + { + } + public function sql_fetchfield($query_id, $field) + { + } + public function sql_rowseek($rownum, $query_id) + { + } + public function sql_freeresult($query_id) + { + } + + public function obtain_bots() + { + return isset($this->data['_bots']) ? $this->data['_bots'] : array(); + } +} diff --git a/tests/mock/extension_manager.php b/tests/mock/extension_manager.php new file mode 100644 index 0000000000..fdda4cbadc --- /dev/null +++ b/tests/mock/extension_manager.php @@ -0,0 +1,18 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_mock_extension_manager extends phpbb_extension_manager +{ + public function __construct($phpbb_root_path, $extensions = array()) + { + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = '.php'; + $this->extensions = $extensions; + } +} diff --git a/tests/mock/filespec.php b/tests/mock/filespec.php new file mode 100644 index 0000000000..9d2a5c84de --- /dev/null +++ b/tests/mock/filespec.php @@ -0,0 +1,32 @@ +<?php +/** + * + * @package testing + * @copyright (c) 2012 phpBB Group + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 + * + */ + +/** + * Mock filespec class with some basic values to help with testing the + * fileupload class + */ +class phpbb_mock_filespec +{ + public $filesize; + public $realname; + public $extension; + public $width; + public $height; + public $error = array(); + + public function check_content($disallowed_content) + { + return true; + } + + public function get($property) + { + return $this->$property; + } +} diff --git a/tests/mock/fileupload.php b/tests/mock/fileupload.php new file mode 100644 index 0000000000..409036ba63 --- /dev/null +++ b/tests/mock/fileupload.php @@ -0,0 +1,52 @@ +<?php +/** + * + * @package testing + * @copyright (c) 2012 phpBB Group + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 + * + */ + +/** + * Mock fileupload class with some basic values to help with testing the + * filespec class + */ +class phpbb_mock_fileupload +{ + public $max_filesize = 100; + public $error_prefix = ''; + + public function valid_dimensions($filespec) + { + return true; + } + + /** + * Copied verbatim from phpBB/includes/functions_upload.php's fileupload + * class to ensure the correct behaviour of filespec::move_file. + * + * Maps file extensions to the constant in second index of the array + * returned by getimagesize() + */ + public function image_types() + { + return array( + IMAGETYPE_GIF => array('gif'), + IMAGETYPE_JPEG => array('jpg', 'jpeg'), + IMAGETYPE_PNG => array('png'), + IMAGETYPE_SWF => array('swf'), + IMAGETYPE_PSD => array('psd'), + IMAGETYPE_BMP => array('bmp'), + IMAGETYPE_TIFF_II => array('tif', 'tiff'), + IMAGETYPE_TIFF_MM => array('tif', 'tiff'), + IMAGETYPE_JPC => array('jpg', 'jpeg'), + IMAGETYPE_JP2 => array('jpg', 'jpeg'), + IMAGETYPE_JPX => array('jpg', 'jpeg'), + IMAGETYPE_JB2 => array('jpg', 'jpeg'), + IMAGETYPE_SWC => array('swc'), + IMAGETYPE_IFF => array('iff'), + IMAGETYPE_WBMP => array('wbmp'), + IMAGETYPE_XBM => array('xbm'), + ); + } +} diff --git a/tests/mock/lang.php b/tests/mock/lang.php new file mode 100644 index 0000000000..781b3d060e --- /dev/null +++ b/tests/mock/lang.php @@ -0,0 +1,33 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* phpbb_mock_lang +* mock a user with some language-keys specified +*/ +class phpbb_mock_lang implements ArrayAccess +{ + public function offsetExists($offset) + { + return true; + } + + public function offsetGet($offset) + { + return $offset; + } + + public function offsetSet($offset, $value) + { + } + + public function offsetUnset($offset) + { + } +} diff --git a/tests/mock/request.php b/tests/mock/request.php new file mode 100644 index 0000000000..946dfdada9 --- /dev/null +++ b/tests/mock/request.php @@ -0,0 +1,82 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_mock_request implements phpbb_request_interface +{ + protected $data; + + public function __construct($get = array(), $post = array(), $cookie = array(), $server = array(), $request = false) + { + $this->data[phpbb_request_interface::GET] = $get; + $this->data[phpbb_request_interface::POST] = $post; + $this->data[phpbb_request_interface::COOKIE] = $cookie; + $this->data[phpbb_request_interface::REQUEST] = ($request === false) ? $post + $get : $request; + $this->data[phpbb_request_interface::SERVER] = $server; + } + + public function overwrite($var_name, $value, $super_global = phpbb_request_interface::REQUEST) + { + $this->data[$super_global][$var_name] = $value; + } + + public function variable($var_name, $default, $multibyte = false, $super_global = phpbb_request_interface::REQUEST) + { + return isset($this->data[$super_global][$var_name]) ? $this->data[$super_global][$var_name] : $default; + } + + public function server($var_name, $default = '') + { + $super_global = phpbb_request_interface::SERVER; + return isset($this->data[$super_global][$var_name]) ? $this->data[$super_global][$var_name] : $default; + } + + public function header($header_name, $default = '') + { + $var_name = 'HTTP_' . str_replace('-', '_', strtoupper($header_name)); + return $this->server($var_name, $default); + } + + public function is_set_post($name) + { + return $this->is_set($name, phpbb_request_interface::POST); + } + + public function is_set($var, $super_global = phpbb_request_interface::REQUEST) + { + return isset($this->data[$super_global][$var]); + } + + public function is_ajax() + { + return false; + } + + public function is_secure() + { + return false; + } + + public function variable_names($super_global = phpbb_request_interface::REQUEST) + { + return array_keys($this->data[$super_global]); + } + + /* custom methods */ + + public function set_header($header_name, $value) + { + $var_name = 'HTTP_' . str_replace('-', '_', strtoupper($header_name)); + $this->data[phpbb_request_interface::SERVER][$var_name] = $value; + } + + public function merge($super_global = phpbb_request_interface::REQUEST, $values) + { + $this->data[$super_global] = array_merge($this->data[$super_global], $values); + } +} diff --git a/tests/mock/session_testable.php b/tests/mock/session_testable.php index 70a58fb6cc..56ff8c8b32 100644 --- a/tests/mock/session_testable.php +++ b/tests/mock/session_testable.php @@ -8,7 +8,6 @@ */ 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. @@ -17,7 +16,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/session.php'; * test it without warnings about sent headers. This class only stores cookie * data for later verification. */ -class phpbb_mock_session_testable extends session +class phpbb_mock_session_testable extends phpbb_session { private $_cookies = array(); diff --git a/tests/network/inet_ntop_pton_test.php b/tests/network/inet_ntop_pton_test.php new file mode 100644 index 0000000000..a59c2103bd --- /dev/null +++ b/tests/network/inet_ntop_pton_test.php @@ -0,0 +1,54 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 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_network_inet_ntop_pton_test extends phpbb_test_case +{ + public function data_provider() + { + return array( + array('127.0.0.1', '7f000001'), + array('192.232.131.223', 'c0e883df'), + array('13.1.68.3', '0d014403'), + array('129.144.52.38', '81903426'), + + array('2001:280:0:10::5', '20010280000000100000000000000005'), + array('fe80::200:4cff:fefe:172f', 'fe8000000000000002004cfffefe172f'), + + array('::', '00000000000000000000000000000000'), + array('::1', '00000000000000000000000000000001'), + array('1::', '00010000000000000000000000000000'), + + array('1:1:0:0:1::', '00010001000000000001000000000000'), + + array('0:2:3:4:5:6:7:8', '00000002000300040005000600070008'), + array('1:2:0:4:5:6:7:8', '00010002000000040005000600070008'), + array('1:2:3:4:5:6:7:0', '00010002000300040005000600070000'), + + array('2001:0:0:1::1', '20010000000000010000000000000001'), + ); + } + + /** + * @dataProvider data_provider + */ + public function test_inet_ntop($address, $hex) + { + $this->assertEquals($address, phpbb_inet_ntop(pack('H*', $hex))); + } + + /** + * @dataProvider data_provider + */ + public function test_inet_pton($address, $hex) + { + $this->assertEquals($hex, bin2hex(phpbb_inet_pton($address))); + } +} diff --git a/tests/network/ip_normalise_test.php b/tests/network/ip_normalise_test.php new file mode 100644 index 0000000000..28059f376a --- /dev/null +++ b/tests/network/ip_normalise_test.php @@ -0,0 +1,64 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 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_network_ip_normalise_test extends phpbb_test_case +{ + public function data_provider() + { + return array( + // From: A Recommendation for IPv6 Address Text Representation + // http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-07 + + // Section 4: A Recommendation for IPv6 Text Representation + // Section 4.1: Handling Leading Zeros in a 16 Bit Field + array('2001:0db8::0001', '2001:db8::1'), + + // Section 4.2: "::" Usage + // Section 4.2.1: Shorten As Much As Possible + array('2001:db8::0:1', '2001:db8::1'), + + // Section 4.2.2: Handling One 16 Bit 0 Field + array('2001:db8::1:1:1:1:1', '2001:db8:0:1:1:1:1:1'), + + // Section 4.2.3: Choice in Placement of "::" + array('2001:db8:0:0:1:0:0:1', '2001:db8::1:0:0:1'), + + // Section 4.3: Lower Case + array('2001:DB8::1', '2001:db8::1'), + + // Section 5: Text Representation of Special Addresses + // We want to show IPv4-mapped addresses as plain IPv4 addresses, though. + array('::ffff:192.168.0.1', '192.168.0.1'), + array('0000::0000:ffff:c000:0280', '192.0.2.128'), + + // IPv6 addresses with the last 32-bit written in dotted-quad notation + // should be converted to hex-only IPv6 addresses. + array('2001:db8::192.0.2.128', '2001:db8::c000:280'), + + // Any string not passing the IPv4 or IPv6 regular expression + // is supposed to result in false being returned. + // Valid and invalid IP addresses are tested in + // tests/regex/ipv4.php and tests/regex/ipv6.php. + array('', false), + array('192.168.1.256', false), + array('::ffff:192.168.255.256', false), + array('::1111:2222:3333:4444:5555:6666::', false), + ); + } + + /** + * @dataProvider data_provider + */ + public function test_ip_normalise($ip_address, $expected) + { + $this->assertEquals($expected, phpbb_ip_normalise($ip_address)); + } +} diff --git a/tests/request/deactivated_super_global_test.php b/tests/request/deactivated_super_global_test.php new file mode 100644 index 0000000000..2e19928a5a --- /dev/null +++ b/tests/request/deactivated_super_global_test.php @@ -0,0 +1,22 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2009 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_deactivated_super_global_test extends phpbb_test_case +{ + /** + * Checks that on write access the correct error is thrown + */ + public function test_write_triggers_error() + { + $this->setExpectedTriggerError(E_USER_ERROR); + $obj = new phpbb_request_deactivated_super_global($this->getMock('phpbb_request_interface'), 'obj', phpbb_request_interface::POST); + $obj->offsetSet(0, 0); + } +} diff --git a/tests/request/request_test.php b/tests/request/request_test.php new file mode 100644 index 0000000000..bca5125b7a --- /dev/null +++ b/tests/request/request_test.php @@ -0,0 +1,143 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2009 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_request_test extends phpbb_test_case +{ + private $type_cast_helper; + private $request; + + protected function setUp() + { + // populate super globals + $_POST['test'] = 1; + $_GET['test'] = 2; + $_COOKIE['test'] = 3; + $_REQUEST['test'] = 3; + $_GET['unset'] = ''; + + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['HTTP_ACCEPT'] = 'application/json'; + $_SERVER['HTTP_SOMEVAR'] = '<value>'; + + $this->type_cast_helper = $this->getMock('phpbb_request_type_cast_helper_interface'); + $this->request = new phpbb_request($this->type_cast_helper); + } + + public function test_toggle_super_globals() + { + $this->assertTrue($this->request->super_globals_disabled(), 'Superglobals were not disabled'); + + $this->request->enable_super_globals(); + + $this->assertFalse($this->request->super_globals_disabled(), 'Superglobals were not enabled'); + + $this->assertEquals(1, $_POST['test'], 'Checking $_POST after enable_super_globals'); + $this->assertEquals(2, $_GET['test'], 'Checking $_GET after enable_super_globals'); + $this->assertEquals(3, $_COOKIE['test'], 'Checking $_COOKIE after enable_super_globals'); + $this->assertEquals(3, $_REQUEST['test'], 'Checking $_REQUEST after enable_super_globals'); + + $_POST['x'] = 2; + $this->assertEquals($_POST, $GLOBALS['_POST'], 'Checking whether $_POST can still be accessed via $GLOBALS[\'_POST\']'); + } + + public function test_server() + { + $this->assertEquals('example.com', $this->request->server('HTTP_HOST')); + } + + public function test_server_escaping() + { + $this->type_cast_helper + ->expects($this->once()) + ->method('recursive_set_var') + ->with( + $this->anything(), + '', + true + ); + + $this->request->server('HTTP_SOMEVAR'); + } + + public function test_header() + { + $this->assertEquals('application/json', $this->request->header('Accept')); + } + + public function test_header_escaping() + { + $this->type_cast_helper + ->expects($this->once()) + ->method('recursive_set_var') + ->with( + $this->anything(), + '', + true + ); + + $this->request->header('SOMEVAR'); + } + + /** + * Checks that directly accessing $_POST will trigger + * an error. + */ + public function test_disable_post_super_global() + { + $this->setExpectedTriggerError(E_USER_ERROR); + $_POST['test'] = 3; + } + + public function test_is_set_post() + { + $this->assertTrue($this->request->is_set_post('test')); + $this->assertFalse($this->request->is_set_post('unset')); + } + + public function test_is_ajax_without_ajax() + { + $this->assertFalse($this->request->is_ajax()); + } + + public function test_is_ajax_with_ajax() + { + $this->request->enable_super_globals(); + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'; + $this->request = new phpbb_request($this->type_cast_helper); + + $this->assertTrue($this->request->is_ajax()); + } + + public function test_is_secure() + { + $this->assertFalse($this->request->is_secure()); + + $this->request->enable_super_globals(); + $_SERVER['HTTPS'] = 'on'; + $this->request = new phpbb_request($this->type_cast_helper); + + $this->assertTrue($this->request->is_secure()); + } + + public function test_variable_names() + { + $expected = array('test', 'unset'); + $result = $this->request->variable_names(); + $this->assertEquals($expected, $result); + } + + /** + * Makes sure super globals work properly after these tests + */ + protected function tearDown() + { + $this->request->enable_super_globals(); + } +} diff --git a/tests/request/request_var_test.php b/tests/request/request_var_test.php index 8e609c00af..0e85d4694b 100644 --- a/tests/request/request_var_test.php +++ b/tests/request/request_var_test.php @@ -10,9 +10,18 @@ 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 +class phpbb_request_var_test extends phpbb_test_case { /** + * Makes sure request_var has its standard behaviour. + */ + protected function setUp() + { + parent::setUp(); + request_var(false, false, false, false, false); + } + + /** * @dataProvider request_variables */ public function test_post($variable_value, $default, $multibyte, $expected) @@ -73,7 +82,48 @@ class phpbb_request_request_var_test extends phpbb_test_case unset($_GET[$var], $_POST[$var], $_REQUEST[$var], $_COOKIE[$var]); } - public static function request_variables() + /** + * @dataProvider deep_access + * Only possible with 3.1.x (later) + */ + public function test_deep_multi_dim_array_access($path, $default, $expected) + { + $this->unset_variables('var'); + + // cannot set $_REQUEST directly because in phpbb_request implementation + // $_REQUEST = $_POST + $_GET + $_POST['var'] = array( + 0 => array( + 'b' => array( + true => array( + 5 => 'c', + 6 => 'd', + ), + ), + ), + 2 => array( + 3 => array( + false => 5, + ), + ), + ); + + $result = request_var($path, $default); + $this->assertEquals($expected, $result, 'Testing deep access to multidimensional input arrays: ' . $path); + } + + public function deep_access() + { + return array( + // array(path, default, expected result) + array(array('var', 0, 'b', true, 5), '', 'c'), + array(array('var', 0, 'b', true, 6), '', 'd'), + array(array('var', 2, 3, false), 0, 5), + array(array('var', 0, 'b', true), array(0 => ''), array(5 => 'c', 6 => 'd')), + ); + } + + public function request_variables() { return array( // strings @@ -173,6 +223,50 @@ class phpbb_request_request_var_test extends phpbb_test_case 'abc' => array() ) ), + array( + // input: + array( + 0 => array(0 => array(3, '4', 'ab'), 1 => array()), + 1 => array(array(3, 4)), + ), + // default: + array(0 => array(0 => array(0))), + false, + // expected: + array( + 0 => array(0 => array(3, 4, 0), 1 => array()), + 1 => array(array(3, 4)) + ) + ), + array( + // input: + array( + 'ü' => array(array('c' => 'd')), + 'ä' => array(4 => array('a' => 2, 'ö' => 3)), + ), + // default: + array('' => array(0 => array('' => 0))), + false, + // expected: + array( + '??' => array(4 => array('a' => 2, '??' => 3)), + ) + ), + array( + // input: + array( + 'ü' => array(array('c' => 'd')), + 'ä' => array(4 => array('a' => 2, 'ö' => 3)), + ), + // default: + array('' => array(0 => array('' => 0))), + true, + // expected: + array( + 'ü' => array(array('c' => 0)), + 'ä' => array(4 => array('a' => 2, 'ö' => 3)), + ) + ), ); } diff --git a/tests/request/type_cast_helper_test.php b/tests/request/type_cast_helper_test.php new file mode 100644 index 0000000000..d553d5b8cd --- /dev/null +++ b/tests/request/type_cast_helper_test.php @@ -0,0 +1,51 @@ +<?php +/** +* +* @package testing +* @version $Id$ +* @copyright (c) 2009 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; + +class phpbb_type_cast_helper_test extends phpbb_test_case +{ + private $type_cast_helper; + + protected function setUp() + { + $this->type_cast_helper = new phpbb_request_type_cast_helper(); + } + + public function test_addslashes_recursively() + { + $data = array('some"string' => array('that"' => 'really"', 'needs"' => '"escaping')); + $expected = array('some\\"string' => array('that\\"' => 'really\\"', 'needs\\"' => '\\"escaping')); + + $this->type_cast_helper->addslashes_recursively($data); + + $this->assertEquals($expected, $data); + } + + public function test_simple_recursive_set_var() + { + $data = 'eviL<3'; + $expected = 'eviL<3'; + + $this->type_cast_helper->recursive_set_var($data, '', true); + + $this->assertEquals($expected, $data); + } + + public function test_nested_recursive_set_var() + { + $data = array('eviL<3'); + $expected = array('eviL<3'); + + $this->type_cast_helper->recursive_set_var($data, array(0 => ''), true); + + $this->assertEquals($expected, $data); + } +} diff --git a/tests/security/base.php b/tests/security/base.php index 2658798237..08878ad60d 100644 --- a/tests/security/base.php +++ b/tests/security/base.php @@ -14,20 +14,20 @@ abstract class phpbb_security_test_base extends phpbb_test_case */ protected function setUp() { - global $user, $phpbb_root_path; + global $user, $phpbb_root_path, $request; // Put this into a global function being run by every test to init a proper user session - $_SERVER['HTTP_HOST'] = 'localhost'; - $_SERVER['SERVER_NAME'] = 'localhost'; - $_SERVER['SERVER_ADDR'] = '127.0.0.1'; - $_SERVER['SERVER_PORT'] = 80; - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; - $_SERVER['QUERY_STRING'] = ''; - $_SERVER['REQUEST_URI'] = '/tests/'; - $_SERVER['SCRIPT_NAME'] = '/tests/index.php'; - $_SERVER['PHP_SELF'] = '/tests/index.php'; - $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14'; - $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'; + $server['HTTP_HOST'] = 'localhost'; + $server['SERVER_NAME'] = 'localhost'; + $server['SERVER_ADDR'] = '127.0.0.1'; + $server['SERVER_PORT'] = 80; + $server['REMOTE_ADDR'] = '127.0.0.1'; + $server['QUERY_STRING'] = ''; + $server['REQUEST_URI'] = '/tests/'; + $server['SCRIPT_NAME'] = '/tests/index.php'; + $server['PHP_SELF'] = '/tests/index.php'; + $server['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14'; + $server['HTTP_ACCEPT_LANGUAGE'] = 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3'; /* [HTTP_ACCEPT_ENCODING] => gzip,deflate @@ -36,14 +36,16 @@ abstract class phpbb_security_test_base extends phpbb_test_case [SCRIPT_FILENAME] => /var/www/tests/index.php */ + $request = new phpbb_mock_request(array(), array(), array(), $server); + // Set no user and trick a bit to circumvent errors - $user = new user(); + $user = new phpbb_user(); $user->lang = true; - $user->browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : ''; - $user->referer = (!empty($_SERVER['HTTP_REFERER'])) ? htmlspecialchars((string) $_SERVER['HTTP_REFERER']) : ''; - $user->forwarded_for = (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) ? (string) $_SERVER['HTTP_X_FORWARDED_FOR'] : ''; - $user->host = (!empty($_SERVER['HTTP_HOST'])) ? (string) strtolower($_SERVER['HTTP_HOST']) : ((!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME')); - $user->page = session::extract_current_page($phpbb_root_path); + $user->browser = $server['HTTP_USER_AGENT']; + $user->referer = ''; + $user->forwarded_for = ''; + $user->host = $server['HTTP_HOST']; + $user->page = phpbb_session::extract_current_page($phpbb_root_path); } protected function tearDown() diff --git a/tests/security/extract_current_page_test.php b/tests/security/extract_current_page_test.php index 4911f7b452..d77cbbcaf3 100644 --- a/tests/security/extract_current_page_test.php +++ b/tests/security/extract_current_page_test.php @@ -10,11 +10,10 @@ require_once dirname(__FILE__) . '/base.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 { - public static function security_variables() + public function security_variables() { return array( array('http://localhost/phpBB/index.php', 'mark=forums&x="><script>alert(/XSS/);</script>', 'mark=forums&x=%22%3E%3Cscript%3Ealert(/XSS/);%3C/script%3E'), @@ -27,10 +26,14 @@ class phpbb_security_extract_current_page_test extends phpbb_security_test_base */ public function test_query_string_php_self($url, $query_string, $expected) { - $_SERVER['PHP_SELF'] = $url; - $_SERVER['QUERY_STRING'] = $query_string; + global $request; - $result = session::extract_current_page('./'); + $request->merge(phpbb_request_interface::SERVER, array( + 'PHP_SELF' => $url, + 'QUERY_STRING' => $query_string, + )); + + $result = phpbb_session::extract_current_page('./'); $label = 'Running extract_current_page on ' . $query_string . ' with PHP_SELF filled.'; $this->assertEquals($expected, $result['query_string'], $label); @@ -41,10 +44,14 @@ class phpbb_security_extract_current_page_test extends phpbb_security_test_base */ public function test_query_string_request_uri($url, $query_string, $expected) { - $_SERVER['REQUEST_URI'] = $url . '?' . $query_string; - $_SERVER['QUERY_STRING'] = $query_string; + global $request; + + $request->merge(phpbb_request_interface::SERVER, array( + 'PHP_SELF' => $url, + 'QUERY_STRING' => $query_string, + )); - $result = session::extract_current_page('./'); + $result = phpbb_session::extract_current_page('./'); $label = 'Running extract_current_page on ' . $query_string . ' with REQUEST_URI filled.'; $this->assertEquals($expected, $result['query_string'], $label); diff --git a/tests/security/redirect_test.php b/tests/security/redirect_test.php index 4848a938c6..1325466137 100644 --- a/tests/security/redirect_test.php +++ b/tests/security/redirect_test.php @@ -10,11 +10,10 @@ require_once dirname(__FILE__) . '/base.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 { - public static function provider() + public function provider() { // array(Input -> redirect(), expected triggered error (else false), expected returned result url (else false)) return array( @@ -30,7 +29,7 @@ class phpbb_security_redirect_test extends phpbb_security_test_base protected function setUp() { parent::setUp(); - + $GLOBALS['config'] = array( 'force_server_vars' => '0', ); @@ -57,4 +56,3 @@ class phpbb_security_redirect_test extends phpbb_security_test_base } } } - diff --git a/tests/session/continue_test.php b/tests/session/continue_test.php index c4f7f8d75b..ad78d92299 100644 --- a/tests/session/continue_test.php +++ b/tests/session/continue_test.php @@ -7,7 +7,6 @@ * */ -require_once dirname(__FILE__) . '/../mock/cache.php'; require_once dirname(__FILE__) . '/testable_factory.php'; class phpbb_session_continue_test extends phpbb_database_test_case diff --git a/tests/session/init_test.php b/tests/session/init_test.php index 2ce6c4a4ac..830de34ed0 100644 --- a/tests/session/init_test.php +++ b/tests/session/init_test.php @@ -7,7 +7,6 @@ * */ -require_once dirname(__FILE__) . '/../mock/cache.php'; require_once dirname(__FILE__) . '/testable_factory.php'; class phpbb_session_init_test extends phpbb_database_test_case diff --git a/tests/session/testable_factory.php b/tests/session/testable_factory.php index 00f79738ef..cb85a01c5c 100644 --- a/tests/session/testable_factory.php +++ b/tests/session/testable_factory.php @@ -7,8 +7,6 @@ * */ -require_once dirname(__FILE__) . '/../mock/session_testable.php'; - /** * This class exists to setup an instance of phpbb's session class for testing. * @@ -24,6 +22,7 @@ class phpbb_session_testable_factory protected $config; protected $cache; + protected $request; /** * Initialises the factory with a set of default config and cache values. @@ -66,17 +65,24 @@ class phpbb_session_testable_factory public function get_session(dbal $dbal) { // set up all the global variables used by session - global $SID, $_SID, $db, $config, $cache; + global $SID, $_SID, $db, $config, $cache, $request; + + $request = $this->request = new phpbb_mock_request( + array(), + array(), + $this->cookies, + $this->server_data + ); + request_var(null, null, null, null, $request); + + $config = $this->config = new phpbb_config($this->get_config_data()); + set_config(null, null, null, $config); - $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; } diff --git a/tests/template/includephp_test.php b/tests/template/includephp_test.php new file mode 100644 index 0000000000..626735f15f --- /dev/null +++ b/tests/template/includephp_test.php @@ -0,0 +1,47 @@ +<?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__) . '/template_test_case.php'; + +class phpbb_template_includephp_test extends phpbb_template_template_test_case +{ + public function test_includephp_relative() + { + $this->setup_engine(array('tpl_allow_php' => true)); + + $cache_file = $this->template->cachepath . 'includephp_relative.html.php'; + + $this->run_template('includephp_relative.html', array(), array(), array(), "Path is relative to board root.\ntesting included php", $cache_file); + + $this->template->set_filenames(array('test' => 'includephp_relative.html')); + $this->assertEquals("Path is relative to board root.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP"); + } + + public function test_includephp_absolute() + { + $path_to_php = dirname(__FILE__) . '/templates/_dummy_include.php.inc'; + $this->assertTrue(phpbb_is_absolute($path_to_php)); + $template_text = "Path is absolute.\n<!-- INCLUDEPHP $path_to_php -->"; + + $cache_dir = dirname($this->template->cachepath) . '/'; + $fp = fopen($cache_dir . 'includephp_absolute.html', 'w'); + fputs($fp, $template_text); + fclose($fp); + + $this->setup_engine(array('tpl_allow_php' => true)); + + $this->style->set_custom_style('tests', $cache_dir); + $cache_file = $this->template->cachepath . 'includephp_absolute.html.php'; + + $this->run_template('includephp_absolute.html', array(), array(), array(), "Path is absolute.\ntesting included php", $cache_file); + + $this->template->set_filenames(array('test' => 'includephp_absolute.html')); + $this->assertEquals("Path is absolute.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP"); + } +} diff --git a/tests/template/invalid_constructs_test.php b/tests/template/invalid_constructs_test.php new file mode 100644 index 0000000000..19d192b8b6 --- /dev/null +++ b/tests/template/invalid_constructs_test.php @@ -0,0 +1,87 @@ +<?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__) . '/template_test_case.php'; + +class phpbb_template_invalid_constructs_test extends phpbb_template_template_test_case +{ + public function template_data() + { + return array( + array( + 'Unknown tag', + 'invalid/unknown_tag.html', + array(), + array(), + array(), + 'invalid/output/unknown_tag.html', + ), + /* + * Produces a parse error which is fatal, therefore + * destroying the test suite. + array( + 'ENDIF without IF', + 'invalid/endif_without_if.html', + array(), + array(), + array(), + 'invalid/output/endif_without_if.html', + ), + */ + ); + } + + public function template_data_error() + { + return array( + array( + 'Include a nonexistent file', + 'invalid/include_nonexistent_file.html', + array(), + array(), + array(), + E_USER_ERROR, + 'invalid/output/include_nonexistent_file.html', + ), + ); + } + + /** + * @dataProvider template_data + */ + public function test_template($description, $file, $vars, $block_vars, $destroy, $expected) + { + $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.php'; + + $this->assertFileNotExists($cache_file); + + $expected = file_get_contents(dirname(__FILE__) . '/templates/' . $expected); + // apparently the template engine does not put + // the trailing newline into compiled templates + $expected = trim($expected); + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); + } + + /** + * @dataProvider template_data_error + */ + public function test_template_error($description, $file, $vars, $block_vars, $destroy, $error, $expected) + { + $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.php'; + + $this->assertFileNotExists($cache_file); + + $expected = file_get_contents(dirname(__FILE__) . '/templates/' . $expected); + // apparently the template engine does not put + // the trailing newline into compiled templates + $expected = trim($expected); + $this->setExpectedTriggerError($error, $expected); + $this->run_template($file, $vars, $block_vars, $destroy, '', $cache_file); + } +} diff --git a/tests/template/parent_templates/parent_and_child.html b/tests/template/parent_templates/parent_and_child.html new file mode 100644 index 0000000000..71984b48ad --- /dev/null +++ b/tests/template/parent_templates/parent_and_child.html @@ -0,0 +1 @@ +Parent template. diff --git a/tests/template/parent_templates/parent_and_child.js b/tests/template/parent_templates/parent_and_child.js new file mode 100644 index 0000000000..6d9bb163bf --- /dev/null +++ b/tests/template/parent_templates/parent_and_child.js @@ -0,0 +1 @@ +// JavaScript file in a parent style. diff --git a/tests/template/parent_templates/parent_only.html b/tests/template/parent_templates/parent_only.html new file mode 100644 index 0000000000..8cfb90eca2 --- /dev/null +++ b/tests/template/parent_templates/parent_only.html @@ -0,0 +1 @@ +Only in parent. diff --git a/tests/template/parent_templates/parent_only.js b/tests/template/parent_templates/parent_only.js new file mode 100644 index 0000000000..9c3007d83f --- /dev/null +++ b/tests/template/parent_templates/parent_only.js @@ -0,0 +1 @@ +// JavaScript file only in parent style. diff --git a/tests/template/renderer_eval_test.php b/tests/template/renderer_eval_test.php new file mode 100644 index 0000000000..7ebb8b9bda --- /dev/null +++ b/tests/template/renderer_eval_test.php @@ -0,0 +1,31 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_template_renderer_eval_test extends phpbb_test_case +{ + public function test_eval() + { + $compiled_code = '<a href="<?php echo \'Test\'; ?>">'; + $valid_code = '<a href="Test">'; + $context = new phpbb_template_context(); + $template = new phpbb_template_renderer_eval($compiled_code, NULL); + ob_start(); + try + { + $template->render($context, array()); + } + catch (Exception $exception) + { + ob_end_clean(); + throw $exception; + } + $output = ob_get_clean(); + $this->assertEquals($valid_code, $output); + } +} diff --git a/tests/template/subdir/includephp_from_subdir_test.php b/tests/template/subdir/includephp_from_subdir_test.php new file mode 100644 index 0000000000..517cb85a30 --- /dev/null +++ b/tests/template/subdir/includephp_from_subdir_test.php @@ -0,0 +1,29 @@ +<?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__) . '/../template_test_case.php'; + +class phpbb_template_subdir_includephp_from_subdir_test extends phpbb_template_template_test_case +{ + // Exact copy of test_includephp_relatve from ../includephp_test.php. + // Verifies that relative php inclusion works when including script + // (and thus current working directory) is in a subdirectory of + // board root. + public function test_includephp_relative() + { + $this->setup_engine(array('tpl_allow_php' => true)); + + $cache_file = $this->template->cachepath . 'includephp_relative.html.php'; + + $this->run_template('includephp_relative.html', array(), array(), array(), "Path is relative to board root.\ntesting included php", $cache_file); + + $this->template->set_filenames(array('test' => 'includephp_relative.html')); + $this->assertEquals("Path is relative to board root.\ntesting included php", $this->display('test'), "Testing INCLUDEPHP"); + } +} diff --git a/tests/template/template_compile_test.php b/tests/template/template_compile_test.php new file mode 100644 index 0000000000..0cfcd6ceb5 --- /dev/null +++ b/tests/template/template_compile_test.php @@ -0,0 +1,31 @@ +<?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_template_template_compile_test extends phpbb_test_case +{ + private $template_compile; + private $template_path; + + protected function setUp() + { + $this->template_compile = new phpbb_template_compile(false, null, ''); + $this->template_path = dirname(__FILE__) . '/templates'; + } + + public function test_in_phpbb() + { + $output = $this->template_compile->compile_file($this->template_path . '/trivial.html'); + $this->assertTrue(strlen($output) > 0); + $statements = explode(';', $output); + $first_statement = $statements[0]; + $this->assertTrue(!!preg_match('#if.*defined.*IN_PHPBB.*exit#', $first_statement)); + } +} diff --git a/tests/template/template_includejs_test.php b/tests/template/template_includejs_test.php new file mode 100644 index 0000000000..a8f9a9037f --- /dev/null +++ b/tests/template/template_includejs_test.php @@ -0,0 +1,30 @@ +<?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__) . '/template_test_case_with_tree.php'; + +class phpbb_template_template_includejs_test extends phpbb_template_template_test_case_with_tree +{ + public function test_includejs_compilation() + { + // Reset the engine state + $this->setup_engine(array('assets_version' => 1)); + + // Prepare correct result + $scripts = array( + '<script src="' . $this->test_path . '/templates/parent_and_child.js?assets_version=1"></script>', + '<script src="' . $this->test_path . '/parent_templates/parent_only.js?assets_version=1"></script>', + '<script src="' . $this->test_path . '/templates/child_only.js?assets_version=1"></script>' + ); + + // Run test + $cache_file = $this->template->cachepath . 'includejs.html.php'; + $this->run_template('includejs.html', array('PARENT' => 'parent_only.js'), array(), array(), implode('', $scripts), $cache_file); + } +} diff --git a/tests/template/template_inheritance_test.php b/tests/template/template_inheritance_test.php new file mode 100644 index 0000000000..febfed9ef0 --- /dev/null +++ b/tests/template/template_inheritance_test.php @@ -0,0 +1,64 @@ +<?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__) . '/template_test_case_with_tree.php'; + +class phpbb_template_template_inheritance_test extends phpbb_template_template_test_case_with_tree +{ + /** + * @todo put test data into templates/xyz.test + */ + public function template_data() + { + return array( + // First element of the array is test name - keep them distinct + array( + 'simple inheritance - only parent template exists', + 'parent_only.html', + array(), + array(), + array(), + "Only in parent.", + ), + array( + 'simple inheritance - only child template exists', + 'child_only.html', + array(), + array(), + array(), + "Only in child.", + ), + array( + 'simple inheritance - both parent and child templates exist', + 'parent_and_child.html', + array(), + array(), + array(), + "Child template.", + ), + ); + } + + /** + * @dataProvider template_data + */ + public function test_template($name, $file, array $vars, array $block_vars, array $destroy, $expected) + { + $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.php'; + + $this->assertFileNotExists($cache_file); + + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); + + // Reset the engine state + $this->setup_engine(); + + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); + } +} diff --git a/tests/template/template_locate_test.php b/tests/template/template_locate_test.php new file mode 100644 index 0000000000..be9ae06809 --- /dev/null +++ b/tests/template/template_locate_test.php @@ -0,0 +1,68 @@ +<?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__) . '/template_test_case_with_tree.php'; + +class phpbb_template_template_locate_test extends phpbb_template_template_test_case_with_tree +{ + public function template_data() + { + return array( + // First element of the array is test name - keep them distinct + array( + 'simple inheritance - only parent template exists', + $this->test_path . '/parent_templates/parent_only.html', + 'parent_only.html', + false, + true, + ), + array( + 'simple inheritance - only child template exists', + $this->test_path . '/templates/child_only.html', + 'child_only.html', + false, + true, + ), + array( + 'simple inheritance - both parent and child templates exist', + $this->test_path . '/templates/parent_and_child.html', + 'parent_and_child.html', + false, + true, + ), + array( + 'find first template - only child template exists in main style', + 'child_only.html', + array('parent_only.html', 'child_only.html'), + false, + false, + ), + array( + 'find first template - both templates exist in main style', + 'parent_and_child.html', + array('parent_and_child.html', 'child_only.html'), + false, + false, + ), + ); + } + + /** + * @dataProvider template_data + */ + public function test_template($name, $expected, $files, $return_default, $return_full_path) + { + // Reset the engine state + $this->setup_engine(); + + // Locate template + $result = $this->template->locate($files, $return_default, $return_full_path); + $this->assertSame($expected, $result); + } +} diff --git a/tests/template/template_test.php b/tests/template/template_test.php index aaee7bb4a0..f8677ed913 100644 --- a/tests/template/template_test.php +++ b/tests/template/template_test.php @@ -8,93 +8,14 @@ */ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; -require_once dirname(__FILE__) . '/../../phpBB/includes/template.php'; +require_once dirname(__FILE__) . '/template_test_case.php'; -class phpbb_template_template_test extends phpbb_test_case +class phpbb_template_template_test extends phpbb_template_template_test_case { - private $template; - private $template_path; - - // Keep the contents of the cache for debugging? - const PRESERVE_CACHE = true; - - private function display($handle) - { - // allow the templates to throw notices - $error_level = error_reporting(); - error_reporting($error_level & ~E_NOTICE); - - ob_start(); - - try - { - $this->assertTrue($this->template->display($handle, false)); - } - catch (Exception $exception) - { - // reset the error level even when an error occured - // PHPUnit turns trigger_error into exceptions as well - error_reporting($error_level); - ob_end_clean(); - throw $exception; - } - - $result = self::trim_template_result(ob_get_clean()); - - // reset error level - error_reporting($error_level); - return $result; - } - - private static function trim_template_result($result) - { - return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result))))); - } - - private function setup_engine() - { - $this->template_path = dirname(__FILE__) . '/templates'; - $this->template = new template(); - $this->template->set_custom_template($this->template_path, 'tests'); - } - - protected function setUp() - { - // Test the engine can be used - $this->setup_engine(); - - $template_cache_dir = dirname($this->template->cachepath); - if (!is_writable($template_cache_dir)) - { - $this->markTestSkipped("Template cache directory ({$template_cache_dir}) is not writable."); - } - - foreach (glob($this->template->cachepath . '*') as $file) - { - unlink($file); - } - - $GLOBALS['config'] = array( - 'load_tplcompile' => true, - 'tpl_allow_php' => false, - ); - } - - protected function tearDown() - { - if (is_object($this->template)) - { - foreach (glob($this->template->cachepath . '*') as $file) - { - unlink($file); - } - } - } - /** * @todo put test data into templates/xyz.test */ - public static function template_data() + public function template_data() { return array( /* @@ -111,7 +32,7 @@ class phpbb_template_template_test extends phpbb_test_case array(), array(), array(), - "pass\npass\n<!-- DUMMY var -->", + "pass\npass\npass\n<!-- DUMMY var -->", ), array( 'variable.html', @@ -125,14 +46,14 @@ class phpbb_template_template_test extends phpbb_test_case array(), array(), array(), - '0', + '03', ), array( 'if.html', array('S_VALUE' => true), array(), array(), - "1\n0", + '1', ), array( 'if.html', @@ -181,22 +102,22 @@ class phpbb_template_template_test extends phpbb_test_case array(), array('loop' => array(array('VARIABLE' => 'x'))), array(), - "first\n0\nx\nset\nlast", - ),/* no nested top level loops + "first\n0 - a\nx - b\nset\nlast", + ), array( 'loop_vars.html', array(), array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), array(), - "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", + "first\n0 - a\nx - b\nset\n1 - a\ny - b\nset\nlast", ), array( 'loop_vars.html', array(), array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), array(), - "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast\n0\n\n1\nlast inner\ninner loop", - ),*/ + "first\n0 - a\nx - b\nset\n1 - a\ny - b\nset\nlast\n0 - c\n1 - c\nlast inner\ninner loop", + ), array( 'loop_advanced.html', array(), @@ -209,14 +130,23 @@ class phpbb_template_template_test extends phpbb_test_case array(), array('loop' => array(array(), array(), array(), array(), array(), array(), array()), 'test' => array(array()), 'test.deep' => array(array()), 'test.deep.defines' => array(array())), array(), - "xyz\nabc", + "xyz\nabc\nabc\nbar\nbar\nabc", ), array( 'expressions.html', array(), array(), array(), - trim(str_repeat("pass", 39)), + trim(str_repeat("pass\n", 10) . "\n" + . str_repeat("pass\n", 4) . "\n" + . str_repeat("pass\n", 2) . "\n" + . str_repeat("pass\n", 6) . "\n" + . str_repeat("pass\n", 2) . "\n" + . str_repeat("pass\n", 6) . "\n" + . str_repeat("pass\n", 2) . "\n" + . str_repeat("pass\n", 2) . "\n" + . str_repeat("pass\n", 3) . "\n" + . str_repeat("pass\n", 2) . "\n"), ), array( 'php.html', @@ -233,6 +163,27 @@ class phpbb_template_template_test extends phpbb_test_case 'value', ), array( + 'include_define.html', + array('VARIABLE' => 'value'), + array(), + array(), + 'value', + ), + array( + 'include_loop.html', + array(), + array('loop' => array(array('NESTED_FILE' => 'include_loop1.html')), 'loop.inner' => array(array('NESTED_FILE' => 'include_loop1.html'), array('NESTED_FILE' => 'include_loop2.html'), array('NESTED_FILE' => 'include_loop3.html'))), + array(), + "1\n_1\n_02\n_3", + ), + array( + 'include_variable.html', + array('FILE' => 'variable.html', 'VARIABLE' => 'value'), + array(), + array(), + 'value', + ), + array( 'loop_vars.html', array(), array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), @@ -247,26 +198,75 @@ class phpbb_template_template_test extends phpbb_test_case "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", ),*/ array( + // Just like a regular loop but the name begins + // with an underscore + 'loop_underscore.html', + array(), + array(), + array(), + "noloop\nnoloop", + ), + array( 'lang.html', array(), array(), array(), - "{ VARIABLE }\n{ VARIABLE }", + "{ VARIABLE }\n{ 1_VARIABLE }\n{ VARIABLE }\n{ 1_VARIABLE }", ), array( 'lang.html', - array('L_VARIABLE' => "Value'"), + array('L_VARIABLE' => "Value'", 'L_1_VARIABLE' => "1 O'Clock"), array(), array(), - "Value'\nValue\'", + "Value'\n1 O'Clock\nValue\'\n1 O\'Clock", ), array( 'lang.html', - array('LA_VARIABLE' => "Value'"), + array('LA_VARIABLE' => "Value'", 'LA_1_VARIABLE' => "1 O'Clock"), + array(), + array(), + "{ VARIABLE }\n{ 1_VARIABLE }\nValue'\n1 O'Clock", + ), + array( + 'loop_nested_multilevel_ref.html', + array(), array(), array(), - "{ VARIABLE }\nValue'", + "top-level content", ), + array( + 'loop_nested_multilevel_ref.html', + array(), + array('outer' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'outer.inner' => array(array('VARIABLE' => 'z'), array('VARIABLE' => 'zz'))), + array(), + // I don't completely understand this output, hopefully it's correct + "top-level content\nouter x\nouter y\ninner z\nfirst row\n\ninner zz", + ), + array( + 'loop_nested_deep_multilevel_ref.html', + array(), + array('outer' => array(array()), 'outer.middle' => array(array()), 'outer.middle.inner' => array(array('VARIABLE' => 'z'), array('VARIABLE' => 'zz'))), + array(), + // I don't completely understand this output, hopefully it's correct + "top-level content\nouter\nmiddle\ninner z\nfirst row of 2 in inner\n\ninner zz", + ), + array( + 'loop_size.html', + array(), + array('loop' => array(array()), 'empty_loop' => array()), + array(), + "nonexistent = 0\n! nonexistent\n\nempty = 0\n! empty\nloop\n\nin loop", + ), + /* Does not pass with the current implementation. + array( + 'loop_reuse.html', + array(), + array('one' => array(array('VAR' => 'a'), array('VAR' => 'b')), 'one.one' => array(array('VAR' => 'c'), array('VAR' => 'd'))), + array(), + // Not entirely sure what should be outputted but the current output of "a" is most certainly wrong + "a\nb\nc\nd", + ), + */ ); } @@ -277,7 +277,7 @@ class phpbb_template_template_test extends phpbb_test_case $this->template->set_filenames(array('test' => $filename)); $this->assertFileNotExists($this->template_path . '/' . $filename, 'Testing missing file, file cannot exist'); - $expecting = sprintf('template->_tpl_load_file(): File %s does not exist or is empty', realpath($this->template_path . '/../') . '/templates/' . $filename); + $expecting = sprintf('style resource locator: File for handle test does not exist. Could not find: %s', $this->test_path . '/templates/' . $filename); $this->setExpectedTriggerError(E_USER_ERROR, $expecting); $this->display('test'); @@ -285,7 +285,7 @@ class phpbb_template_template_test extends phpbb_test_case public function test_empty_file() { - $expecting = 'template->set_filenames: Empty filename specified for test'; + $expecting = 'style resource locator: set_filenames: Empty filename specified for test'; $this->setExpectedTriggerError(E_USER_ERROR, $expecting); $this->template->set_filenames(array('test' => '')); @@ -293,52 +293,12 @@ class phpbb_template_template_test extends phpbb_test_case public function test_invalid_handle() { - $expecting = 'template->_tpl_load(): No file specified for handle test'; + $expecting = 'No file specified for handle test'; $this->setExpectedTriggerError(E_USER_ERROR, $expecting); $this->display('test'); } - private function run_template($file, array $vars, array $block_vars, array $destroy, $expected, $cache_file) - { - $this->template->set_filenames(array('test' => $file)); - $this->template->assign_vars($vars); - - foreach ($block_vars as $block => $loops) - { - foreach ($loops as $_vars) - { - $this->template->assign_block_vars($block, $_vars); - } - } - - foreach ($destroy as $block) - { - $this->template->destroy_block_vars($block); - } - - try - { - $this->assertEquals($expected, $this->display('test'), "Testing $file"); - $this->assertFileExists($cache_file); - } - catch (ErrorException $e) - { - if (file_exists($cache_file)) - { - copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file)); - } - - throw $e; - } - - // For debugging - if (self::PRESERVE_CACHE) - { - copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file)); - } - } - /** * @dataProvider template_data */ @@ -380,46 +340,61 @@ class phpbb_template_template_test extends phpbb_test_case $this->template->destroy_block_vars($block); } - $error_level = error_reporting(); - error_reporting($error_level & ~E_NOTICE); - $this->assertEquals($expected, self::trim_template_result($this->template->assign_display('test')), "Testing assign_display($file)"); $this->template->assign_display('test', 'VARIABLE', false); - error_reporting($error_level); - $this->assertEquals($expected, $this->display('container'), "Testing assign_display($file)"); } - public function test_php() + public function test_append_var_without_assign_var() { - $GLOBALS['config']['tpl_allow_php'] = true; + $this->template->set_filenames(array( + 'append_var' => 'variable.html' + )); - $cache_file = $this->template->cachepath . 'php.html.php'; + $items = array('This ', 'is ', 'a ', 'test'); + $expecting = implode('', $items); + + foreach ($items as $word) + { + $this->template->append_var('VARIABLE', $word); + } - $this->assertFileNotExists($cache_file); + $this->assertEquals($expecting, $this->display('append_var')); + } - $this->run_template('php.html', array(), array(), array(), 'test', $cache_file); + public function test_append_var_with_assign_var() + { + $this->template->set_filenames(array( + 'append_var' => 'variable.html' + )); - $GLOBALS['config']['tpl_allow_php'] = false; + $start = 'This '; + $items = array('is ', 'a ', 'test'); + $expecting = $start . implode('', $items); + + $this->template->assign_var('VARIABLE', $start); + foreach ($items as $word) + { + $this->template->append_var('VARIABLE', $word); + } + + $this->assertEquals($expecting, $this->display('append_var')); } - public function test_includephp() + public function test_php() { - $GLOBALS['config']['tpl_allow_php'] = true; + $this->setup_engine(array('tpl_allow_php' => true)); - $cache_file = $this->template->cachepath . 'includephp.html.php'; - - $this->run_template('includephp.html', array(), array(), array(), 'testing included php', $cache_file); + $cache_file = $this->template->cachepath . 'php.html.php'; - $this->template->set_filenames(array('test' => 'includephp.html')); - $this->assertEquals('testing included php', $this->display('test'), "Testing INCLUDEPHP"); + $this->assertFileNotExists($cache_file); - $GLOBALS['config']['tpl_allow_php'] = false; + $this->run_template('php.html', array(), array(), array(), 'test', $cache_file); } - public static function alter_block_array_data() + public function alter_block_array_data() { return array( array( @@ -527,5 +502,5 @@ EOT $this->template->alter_block_array($alter_block, $vararray, $key, $mode); $this->assertEquals($expect, $this->display('test'), $description); } -} +} diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php new file mode 100644 index 0000000000..6f76cb049d --- /dev/null +++ b/tests/template/template_test_case.php @@ -0,0 +1,144 @@ +<?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_template_template_test_case extends phpbb_test_case +{ + protected $style; + protected $template; + protected $template_path; + protected $style_resource_locator; + protected $style_provider; + + protected $test_path = 'tests/template'; + + // Keep the contents of the cache for debugging? + const PRESERVE_CACHE = true; + + protected function display($handle) + { + ob_start(); + + try + { + $this->assertTrue($this->template->display($handle, false)); + } + catch (Exception $exception) + { + // reset output buffering even when an error occured + // PHPUnit turns trigger_error into exceptions as well + ob_end_clean(); + throw $exception; + } + + $result = self::trim_template_result(ob_get_clean()); + + return $result; + } + + protected static function trim_template_result($result) + { + return str_replace("\n\n", "\n", implode("\n", array_map('trim', explode("\n", trim($result))))); + } + + protected function config_defaults() + { + $defaults = array( + 'load_tplcompile' => true, + 'tpl_allow_php' => false, + ); + return $defaults; + } + + protected function setup_engine(array $new_config = array()) + { + global $phpbb_root_path, $phpEx, $user; + + $defaults = $this->config_defaults(); + $config = new phpbb_config(array_merge($defaults, $new_config)); + + $this->template_path = $this->test_path . '/templates'; + $this->style_resource_locator = new phpbb_style_resource_locator(); + $this->style_provider = new phpbb_style_path_provider(); + $this->template = new phpbb_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator); + $this->style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); + $this->style->set_custom_style('tests', $this->template_path, ''); + } + + protected function setUp() + { + // Test the engine can be used + $this->setup_engine(); + + $template_cache_dir = dirname($this->template->cachepath); + if (!is_writable($template_cache_dir)) + { + $this->markTestSkipped("Template cache directory ({$template_cache_dir}) is not writable."); + } + + foreach (glob($this->template->cachepath . '*') as $file) + { + unlink($file); + } + + $this->setup_engine(); + } + + protected function tearDown() + { + if (is_object($this->template)) + { + foreach (glob($this->template->cachepath . '*') as $file) + { + unlink($file); + } + } + } + + protected function run_template($file, array $vars, array $block_vars, array $destroy, $expected, $cache_file) + { + $this->template->set_filenames(array('test' => $file)); + $this->template->assign_vars($vars); + + foreach ($block_vars as $block => $loops) + { + foreach ($loops as $_vars) + { + $this->template->assign_block_vars($block, $_vars); + } + } + + foreach ($destroy as $block) + { + $this->template->destroy_block_vars($block); + } + + try + { + $this->assertEquals($expected, $this->display('test'), "Testing $file"); + $this->assertFileExists($cache_file); + } + catch (ErrorException $e) + { + if (file_exists($cache_file)) + { + copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file)); + } + throw $e; + } + + // For debugging. + // When testing eval path the cache file may not exist. + if (self::PRESERVE_CACHE && file_exists($cache_file)) + { + copy($cache_file, str_replace('ctpl_', 'tests_ctpl_', $cache_file)); + } + } +} diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php new file mode 100644 index 0000000000..05ccb7ee55 --- /dev/null +++ b/tests/template/template_test_case_with_tree.php @@ -0,0 +1,29 @@ +<?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__) . '/template_test_case.php'; + +class phpbb_template_template_test_case_with_tree extends phpbb_template_template_test_case +{ + protected function setup_engine(array $new_config = array()) + { + global $phpbb_root_path, $phpEx, $user; + + $defaults = $this->config_defaults(); + $config = new phpbb_config(array_merge($defaults, $new_config)); + + $this->template_path = $this->test_path . '/templates'; + $this->parent_template_path = $this->test_path . '/parent_templates'; + $this->style_resource_locator = new phpbb_style_resource_locator(); + $this->style_provider = new phpbb_style_path_provider(); + $this->template = new phpbb_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator); + $this->style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); + $this->style->set_custom_style('tests', array($this->template_path, $this->parent_template_path), ''); + } +} diff --git a/tests/template/templates/basic.html b/tests/template/templates/basic.html index c1dd690260..e5c6f280fb 100644 --- a/tests/template/templates/basic.html +++ b/tests/template/templates/basic.html @@ -16,5 +16,8 @@ fail <!-- BEGINELSE --> pass <!-- END empty --> +<!-- IF not S_EMPTY --> +pass +<!-- ENDIF --> <!-- DUMMY var --> diff --git a/tests/template/templates/child_only.html b/tests/template/templates/child_only.html new file mode 100644 index 0000000000..6121fef5c5 --- /dev/null +++ b/tests/template/templates/child_only.html @@ -0,0 +1 @@ +Only in child. diff --git a/tests/template/templates/child_only.js b/tests/template/templates/child_only.js new file mode 100644 index 0000000000..542b26526c --- /dev/null +++ b/tests/template/templates/child_only.js @@ -0,0 +1 @@ +// JavaScript file only in a child style. diff --git a/tests/template/templates/define.html b/tests/template/templates/define.html index 82237d21a3..4459fffbe0 100644 --- a/tests/template/templates/define.html +++ b/tests/template/templates/define.html @@ -2,6 +2,9 @@ {$VALUE} <!-- DEFINE $VALUE = 'abc' --> {$VALUE} +<!-- INCLUDE define_include.html --> +{$INCLUDED_VALUE} +{$VALUE} <!-- UNDEFINE $VALUE --> {$VALUE} <!-- DEFINE $VALUE --> diff --git a/tests/template/templates/define_include.html b/tests/template/templates/define_include.html new file mode 100644 index 0000000000..9c470c296a --- /dev/null +++ b/tests/template/templates/define_include.html @@ -0,0 +1,3 @@ +{$VALUE} +<!-- DEFINE $INCLUDED_VALUE = 'bar' --> +{$INCLUDED_VALUE} diff --git a/tests/template/templates/expressions.html b/tests/template/templates/expressions.html index c40d967dab..e1283f165f 100644 --- a/tests/template/templates/expressions.html +++ b/tests/template/templates/expressions.html @@ -1,86 +1,57 @@ <!-- IF 10 is even -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 9 is even -->fail<!-- ELSE -->pass<!-- ENDIF --> - <!-- IF not 390 is even -->fail<!-- ELSE -->pass<!-- ENDIF --> - <!-- IF 9 is odd -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 32 is odd -->fail<!-- ELSE -->pass<!-- ENDIF --> - <!-- IF 32 is div by 16 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 10 is not even -->fail<!-- ELSE -->pass<!-- ENDIF --> - <!-- IF 24 == 24 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 24 eq 24 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF ((((((24 == 24)))))) -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 24 != 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 24 <> 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 24 ne 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 24 neq 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 10 lt 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 10 < 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 10 le 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 10 lte 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 10 <= 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 20 le 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 20 lte 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 20 <= 20 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 9 gt 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 9 > 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 9 >= 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 9 gte 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 9 ge 1 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 9 >= 9 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 9 gte 9 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 9 ge 9 -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF true && (10 > 4) -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF true and (10 > 4) -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF false || true -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF false or true -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF !false -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF not false -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF not not not false -->pass<!-- ELSE -->fail<!-- ENDIF --> <!-- IF 6 % 4 == 2 -->pass<!-- ELSE -->fail<!-- ENDIF --> - <!-- IF 24 mod 12 == 0 -->pass<!-- ELSE -->fail<!-- ENDIF --> diff --git a/tests/template/templates/if.html b/tests/template/templates/if.html index c502e52f51..eed431019e 100644 --- a/tests/template/templates/if.html +++ b/tests/template/templates/if.html @@ -3,9 +3,9 @@ <!-- ELSEIF S_OTHER_VALUE --> 2 <!-- ELSE --> -0 +03 <!-- ENDIF --> -<!-- IF (S_VALUE > S_OTHER_VALUE) --> -0 +<!-- IF S_VALUE and S_OTHER_VALUE and (S_VALUE > S_OTHER_VALUE) --> +04 <!-- ENDIF --> diff --git a/tests/template/templates/include_define.html b/tests/template/templates/include_define.html new file mode 100644 index 0000000000..2419c8cba1 --- /dev/null +++ b/tests/template/templates/include_define.html @@ -0,0 +1,2 @@ +<!-- DEFINE $DEF = 'variable.html' --> +<!-- INCLUDE {$DEF} --> diff --git a/tests/template/templates/include_loop.html b/tests/template/templates/include_loop.html new file mode 100644 index 0000000000..d5c3d9bc82 --- /dev/null +++ b/tests/template/templates/include_loop.html @@ -0,0 +1,4 @@ +<!-- BEGIN loop --> +<!-- INCLUDE {loop.NESTED_FILE} --> +<!-- BEGIN inner -->_<!-- INCLUDE {inner.NESTED_FILE} --><!-- END inner --> +<!-- END loop --> diff --git a/tests/template/templates/include_loop1.html b/tests/template/templates/include_loop1.html new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/tests/template/templates/include_loop1.html @@ -0,0 +1 @@ +1 diff --git a/tests/template/templates/include_loop2.html b/tests/template/templates/include_loop2.html new file mode 100644 index 0000000000..9e22bcb8e3 --- /dev/null +++ b/tests/template/templates/include_loop2.html @@ -0,0 +1 @@ +02 diff --git a/tests/template/templates/include_loop3.html b/tests/template/templates/include_loop3.html new file mode 100644 index 0000000000..00750edc07 --- /dev/null +++ b/tests/template/templates/include_loop3.html @@ -0,0 +1 @@ +3 diff --git a/tests/template/templates/include_variable.html b/tests/template/templates/include_variable.html new file mode 100644 index 0000000000..b907e0b44f --- /dev/null +++ b/tests/template/templates/include_variable.html @@ -0,0 +1 @@ +<!-- INCLUDE {FILE} --> diff --git a/tests/template/templates/includejs.html b/tests/template/templates/includejs.html new file mode 100644 index 0000000000..8a2587d76b --- /dev/null +++ b/tests/template/templates/includejs.html @@ -0,0 +1,5 @@ +<!-- INCLUDEJS parent_and_child.js --> +<!-- INCLUDEJS {PARENT} --> +<!-- DEFINE $TEST = 'child_only.js' --> +<!-- INCLUDEJS {$TEST} --> +{SCRIPTS}
\ No newline at end of file diff --git a/tests/template/templates/includephp.html b/tests/template/templates/includephp_relative.html index 70ebdac0d0..297c9efcb0 100644 --- a/tests/template/templates/includephp.html +++ b/tests/template/templates/includephp_relative.html @@ -1 +1,2 @@ +Path is relative to board root. <!-- INCLUDEPHP ../tests/template/templates/_dummy_include.php.inc --> diff --git a/tests/template/templates/invalid/endif_without_if.html b/tests/template/templates/invalid/endif_without_if.html new file mode 100644 index 0000000000..e371ffd150 --- /dev/null +++ b/tests/template/templates/invalid/endif_without_if.html @@ -0,0 +1 @@ +<!-- ENDIF --> diff --git a/tests/template/templates/invalid/include_nonexistent_file.html b/tests/template/templates/invalid/include_nonexistent_file.html new file mode 100644 index 0000000000..617d2fdaaa --- /dev/null +++ b/tests/template/templates/invalid/include_nonexistent_file.html @@ -0,0 +1 @@ +<!-- INCLUDE nonexistent.html --> diff --git a/tests/template/templates/invalid/output/endif_without_if.html b/tests/template/templates/invalid/output/endif_without_if.html new file mode 100644 index 0000000000..5f2239c964 --- /dev/null +++ b/tests/template/templates/invalid/output/endif_without_if.html @@ -0,0 +1 @@ +Parse error (fatal, destroys php runtime). diff --git a/tests/template/templates/invalid/output/include_nonexistent_file.html b/tests/template/templates/invalid/output/include_nonexistent_file.html new file mode 100644 index 0000000000..8a118d2713 --- /dev/null +++ b/tests/template/templates/invalid/output/include_nonexistent_file.html @@ -0,0 +1 @@ +style resource locator: File for handle nonexistent.html does not exist. Could not find: diff --git a/tests/template/templates/invalid/output/unknown_tag.html b/tests/template/templates/invalid/output/unknown_tag.html new file mode 100644 index 0000000000..1489e5e31a --- /dev/null +++ b/tests/template/templates/invalid/output/unknown_tag.html @@ -0,0 +1 @@ +<!-- UNKNOWNTAG variable.html --> diff --git a/tests/template/templates/invalid/unknown_tag.html b/tests/template/templates/invalid/unknown_tag.html new file mode 100644 index 0000000000..1489e5e31a --- /dev/null +++ b/tests/template/templates/invalid/unknown_tag.html @@ -0,0 +1 @@ +<!-- UNKNOWNTAG variable.html --> diff --git a/tests/template/templates/lang.html b/tests/template/templates/lang.html index 2b5ea1cafe..3eecc298cb 100644 --- a/tests/template/templates/lang.html +++ b/tests/template/templates/lang.html @@ -1,3 +1,5 @@ {L_VARIABLE} +{L_1_VARIABLE} {LA_VARIABLE} +{LA_1_VARIABLE} diff --git a/tests/template/templates/loop_expressions.html b/tests/template/templates/loop_expressions.html new file mode 100644 index 0000000000..6bff53f388 --- /dev/null +++ b/tests/template/templates/loop_expressions.html @@ -0,0 +1,11 @@ +<!-- BEGIN loop --> + +<!-- IF loop.S_ROW_NUM is even by 4 -->on<!-- ELSE -->off<!-- ENDIF --> + +<!-- END loop --> + +<!-- BEGIN loop --> + +<!-- IF loop.S_ROW_NUM is odd by 3 -->on<!-- ELSE -->off<!-- ENDIF --> + +<!-- END loop --> diff --git a/tests/template/templates/loop_nested.html b/tests/template/templates/loop_nested.html index 9b251cd453..45b1ef85d4 100644 --- a/tests/template/templates/loop_nested.html +++ b/tests/template/templates/loop_nested.html @@ -1,8 +1,6 @@ <!-- BEGIN outer --> outer - {outer.S_ROW_COUNT}<!-- IF outer.VARIABLE --> - {outer.VARIABLE}<!-- ENDIF --> - <!-- BEGIN middle --> middle - {middle.S_ROW_COUNT}<!-- IF middle.VARIABLE --> - {middle.VARIABLE}<!-- ENDIF --> - <!-- END middle --> <!-- END outer --> diff --git a/tests/template/templates/loop_nested_deep_multilevel_ref.html b/tests/template/templates/loop_nested_deep_multilevel_ref.html new file mode 100644 index 0000000000..bcc2a7c07b --- /dev/null +++ b/tests/template/templates/loop_nested_deep_multilevel_ref.html @@ -0,0 +1,13 @@ +top-level content +<!-- BEGIN outer --> + outer + <!-- BEGIN middle --> + {outer.middle.S_BLOCK_NAME} + <!-- BEGIN inner --> + inner {inner.VARIABLE} + <!-- IF outer.middle.inner.S_FIRST_ROW --> + first row of {outer.middle.inner.S_NUM_ROWS} in {middle.inner.S_BLOCK_NAME} + <!-- ENDIF --> + <!-- END inner --> + <!-- END middle --> +<!-- END outer --> diff --git a/tests/template/templates/loop_nested_multilevel_ref.html b/tests/template/templates/loop_nested_multilevel_ref.html new file mode 100644 index 0000000000..f2f1c746ed --- /dev/null +++ b/tests/template/templates/loop_nested_multilevel_ref.html @@ -0,0 +1,10 @@ +top-level content +<!-- BEGIN outer --> + outer {outer.VARIABLE} + <!-- BEGIN inner --> + inner {inner.VARIABLE} + <!-- IF outer.inner.S_FIRST_ROW --> + first row + <!-- ENDIF --> + <!-- END inner --> +<!-- END outer --> diff --git a/tests/template/templates/loop_reuse.html b/tests/template/templates/loop_reuse.html new file mode 100644 index 0000000000..bd0354ae6f --- /dev/null +++ b/tests/template/templates/loop_reuse.html @@ -0,0 +1,6 @@ +<!-- BEGIN one --> + {one.VAR} + <!-- BEGIN one --> + {one.one.VAR} + <!-- END one --> +<!-- END one --> diff --git a/tests/template/templates/loop_size.html b/tests/template/templates/loop_size.html new file mode 100644 index 0000000000..f1938441df --- /dev/null +++ b/tests/template/templates/loop_size.html @@ -0,0 +1,39 @@ +<!-- IF .nonexistent_loop --> + nonexistent +<!-- ENDIF --> + +<!-- IF .nonexistent_loop == 0 --> + nonexistent = 0 +<!-- ENDIF --> + +<!-- IF ! .nonexistent_loop --> + ! nonexistent +<!-- ENDIF --> + +<!-- IF .empty_loop --> + empty +<!-- ENDIF --> + +<!-- IF .empty_loop == 0 --> + empty = 0 +<!-- ENDIF --> + +<!-- IF ! .empty_loop --> + ! empty +<!-- ENDIF --> + +<!-- IF .loop --> + loop +<!-- ENDIF --> + +<!-- IF .loop == 0 --> + loop = 0 +<!-- ENDIF --> + +<!-- IF ! .loop --> + ! loop +<!-- ENDIF --> + +<!-- BEGIN loop --> + in loop +<!-- END --> diff --git a/tests/template/templates/loop_underscore.html b/tests/template/templates/loop_underscore.html new file mode 100644 index 0000000000..dafce5dea6 --- /dev/null +++ b/tests/template/templates/loop_underscore.html @@ -0,0 +1,21 @@ +<!-- BEGIN _underscore_loop --> +loop +<!-- BEGINELSE --> +noloop +<!-- END loop --> + +<!-- IF ._underscore_loop --> +loop +<!-- ELSE --> +noloop +<!-- ENDIF --> + +<!-- IF ._underscore_loop == 2 --> +loop +<!-- ENDIF --> + +<!-- BEGIN _underscore_loop --> +<!-- BEGIN !block --> +loop#{loop.S_ROW_COUNT}-block#{block.S_ROW_COUNT} +<!-- END !block --> +<!-- END _underscore_loop --> diff --git a/tests/template/templates/loop_vars.html b/tests/template/templates/loop_vars.html index 4f02fd2e6c..d94a0ae0f7 100644 --- a/tests/template/templates/loop_vars.html +++ b/tests/template/templates/loop_vars.html @@ -1,21 +1,14 @@ <!-- BEGIN loop --> <!-- IF loop.S_FIRST_ROW -->first<!-- ENDIF --> - -{loop.S_ROW_COUNT} - -{loop.VARIABLE} - +{loop.S_ROW_NUM} - a +{loop.VARIABLE} - b <!-- IF loop.VARIABLE -->set<!-- ENDIF --> - <!-- IF loop.S_LAST_ROW --> last <!-- ENDIF --> <!-- BEGIN inner --> - -{inner.S_ROW_COUNT} - +{inner.S_ROW_NUM} - c <!-- IF inner.S_LAST_ROW and inner.S_ROW_COUNT and inner.S_NUM_ROWS -->last inner<!-- ENDIF --> - <!-- END inner --> <!-- END loop --> <!-- IF .loop.inner -->inner loop<!-- ENDIF --> diff --git a/tests/template/templates/parent_and_child.html b/tests/template/templates/parent_and_child.html new file mode 100644 index 0000000000..16223d91e7 --- /dev/null +++ b/tests/template/templates/parent_and_child.html @@ -0,0 +1 @@ +Child template. diff --git a/tests/template/templates/parent_and_child.js b/tests/template/templates/parent_and_child.js new file mode 100644 index 0000000000..d544d94d83 --- /dev/null +++ b/tests/template/templates/parent_and_child.js @@ -0,0 +1 @@ +// JavaScript file in a child style. diff --git a/tests/template/templates/trivial.html b/tests/template/templates/trivial.html new file mode 100644 index 0000000000..3a1c1c5324 --- /dev/null +++ b/tests/template/templates/trivial.html @@ -0,0 +1 @@ +This is a trivial template. diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 2423299b7c..b953017d0a 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -15,6 +15,10 @@ class phpbb_functional_test_case extends phpbb_test_case protected $client; protected $root_url; + protected $cache = null; + protected $db = null; + protected $extension_manager = null; + /** * Session ID for current test's session (each test makes its own) * @var string @@ -77,6 +81,60 @@ class phpbb_functional_test_case extends phpbb_test_case } } + protected function get_db() + { + global $phpbb_root_path, $phpEx; + // so we don't reopen an open connection + if (!($this->db instanceof dbal)) + { + if (!class_exists('dbal_' . self::$config['dbms'])) + { + include($phpbb_root_path . 'includes/db/' . self::$config['dbms'] . ".$phpEx"); + } + $sql_db = 'dbal_' . self::$config['dbms']; + $this->db = new $sql_db(); + $this->db->sql_connect(self::$config['dbhost'], self::$config['dbuser'], self::$config['dbpasswd'], self::$config['dbname'], self::$config['dbport']); + } + return $this->db; + } + + protected function get_cache_driver() + { + if (!$this->cache) + { + $this->cache = new phpbb_cache_driver_file; + } + + return $this->cache; + } + + protected function purge_cache() + { + $cache = $this->get_cache_driver(); + + $cache->purge(); + $cache->unload(); + $cache->load(); + } + + protected function get_extension_manager() + { + global $phpbb_root_path, $phpEx; + + if (!$this->extension_manager) + { + $this->extension_manager = new phpbb_extension_manager( + $this->get_db(), + self::$config['table_prefix'] . 'ext', + $phpbb_root_path, + ".$phpEx", + $this->get_cache_driver() + ); + } + + return $this->extension_manager; + } + protected function install_board() { global $phpbb_root_path, $phpEx; @@ -182,7 +240,7 @@ class phpbb_functional_test_case extends phpbb_test_case $login = $this->client->submit($form, array('username' => 'admin', 'password' => 'admin')); $cookies = $this->cookieJar->all(); - + // The session id is stored in a cookie that ends with _sid - we assume there is only one such cookie foreach ($cookies as $cookie); { diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php index 29adfc6817..46feef550a 100644 --- a/tests/test_framework/phpbb_test_case_helpers.php +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -42,6 +42,11 @@ class phpbb_test_case_helpers $this->test_case->setExpectedException($exceptionName, (string) $message, $errno); } + public function makedirs($path) + { + mkdir($path, 0777, true); + } + static public function get_test_config() { $config = array(); diff --git a/tests/text_processing/censor_text_test.php b/tests/text_processing/censor_text_test.php index f0e13638a5..043d8eb27d 100644 --- a/tests/text_processing/censor_text_test.php +++ b/tests/text_processing/censor_text_test.php @@ -9,8 +9,6 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; -require_once dirname(__FILE__) . '/../mock/user.php'; -require_once dirname(__FILE__) . '/../mock/cache.php'; class phpbb_text_processing_censor_text_test extends phpbb_test_case { @@ -19,7 +17,7 @@ class phpbb_text_processing_censor_text_test extends phpbb_test_case global $cache, $user; $cache = new phpbb_mock_cache; $user = new phpbb_mock_user; - + $user->optionset('viewcensors', false); return array( @@ -61,7 +59,7 @@ class phpbb_text_processing_censor_text_test extends phpbb_test_case array('badword1 badword2 badword3 badword4', 'replacement1 replacement2 replacement3 replacement4'), array('badword1 badword2 badword3 badword4d', 'replacement1 replacement2 replacement3 badword4d'), array('abadword1 badword2 badword3 badword4', 'replacement1 replacement2 replacement3 replacement4'), - + array("new\nline\ntest", "new\nline\ntest"), array("tab\ttest\t", "tab\ttest\t"), array('öäü', 'öäü'), diff --git a/tests/text_processing/make_clickable_test.php b/tests/text_processing/make_clickable_test.php index 8697907311..d94fac2ae4 100644 --- a/tests/text_processing/make_clickable_test.php +++ b/tests/text_processing/make_clickable_test.php @@ -12,7 +12,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; class phpbb_text_processing_make_clickable_test extends phpbb_test_case { - public static function make_clickable_data() + public function make_clickable_data() { // value => whether it should work $prefix_texts = array( diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php new file mode 100644 index 0000000000..c7ff2e78e0 --- /dev/null +++ b/tests/upload/filespec_test.php @@ -0,0 +1,275 @@ +<?php +/** + * + * @package testing + * @copyright (c) 2012 phpBB Group + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 + * + */ + +require_once __DIR__ . '/../../phpBB/includes/functions.php'; +require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; +require_once __DIR__ . '/../../phpBB/includes/functions_upload.php'; + +class phpbb_filespec_test extends phpbb_test_case +{ + const TEST_COUNT = 100; + const PREFIX = 'phpbb_'; + const MAX_STR_LEN = 50; + const UPLOAD_MAX_FILESIZE = 1000; + + private $config; + public $path; + + protected function setUp() + { + // Global $config required by unique_id + // Global $user required by filespec::additional_checks and + // filespec::move_file + global $config, $user; + + if (!is_array($config)) + { + $config = array(); + } + + $config['rand_seed'] = ''; + $config['rand_seed_last_update'] = time() + 600; + // This config value is normally pulled from the database which is set + // to this value at install time. + // See: phpBB/install/schemas/schema_data.sql + $config['mime_triggers'] = 'body|head|html|img|plaintext|a href|pre|script|table|title'; + + $user = new phpbb_mock_user(); + $user->lang = new phpbb_mock_lang(); + + $this->config = &$config; + $this->path = __DIR__ . '/fixture/'; + + // Create copies of the files for use in testing move_file + $iterator = new DirectoryIterator($this->path); + foreach ($iterator as $fileinfo) + { + if ($fileinfo->isDot() || $fileinfo->isDir()) + { + continue; + } + + copy($fileinfo->getPathname(), $this->path . 'copies/' . $fileinfo->getFilename() . '_copy'); + if ($fileinfo->getFilename() === 'txt') + { + copy($fileinfo->getPathname(), $this->path . 'copies/' . $fileinfo->getFilename() . '_copy_2'); + } + } + } + + private function get_filespec($override = array()) + { + // Initialise a blank filespec object for use with trivial methods + $upload_ary = array( + 'name' => '', + 'type' => '', + 'size' => '', + 'tmp_name' => '', + 'error' => '', + ); + + return new filespec(array_merge($upload_ary, $override), null); + } + + protected function tearDown() + { + global $user; + $this->config = array(); + $user = null; + + $iterator = new DirectoryIterator($this->path . 'copies'); + foreach ($iterator as $fileinfo) + { + $name = $fileinfo->getFilename(); + if ($name[0] !== '.') + { + unlink($fileinfo->getPathname()); + } + } + } + + public function additional_checks_variables() + { + // False here just indicates the file is too large and fails the + // filespec::additional_checks method because of it. All other code + // paths in that method are covered elsewhere. + return array( + array('gif', true), + array('jpg', false), + array('png', true), + array('tif', false), + array('txt', false), + ); + } + + /** + * @dataProvider additional_checks_variables + */ + public function test_additional_checks($filename, $expected) + { + $upload = new phpbb_mock_fileupload(); + $filespec = $this->get_filespec(); + $filespec->upload = $upload; + $filespec->file_moved = true; + $filespec->filesize = $filespec->get_filesize($this->path . $filename); + + $this->assertEquals($expected, $filespec->additional_checks()); + } + + public function check_content_variables() + { + // False here indicates that a file is non-binary and contains + // disallowed content that makes IE report the mimetype incorrectly. + return array( + array('gif', true), + array('jpg', true), + array('png', true), + array('tif', true), + array('txt', false), + ); + } + + /** + * @dataProvider check_content_variables + */ + public function test_check_content($filename, $expected) + { + $disallowed_content = explode('|', $this->config['mime_triggers']); + $filespec = $this->get_filespec(array('tmp_name' => $this->path . $filename)); + $this->assertEquals($expected, $filespec->check_content($disallowed_content)); + } + + public function clean_filename_variables() + { + $chunks = str_split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\'\\" /:*?<>|[];(){},#+=-_`', 8); + return array( + array($chunks[0] . $chunks[7]), + array($chunks[1] . $chunks[8]), + array($chunks[2] . $chunks[9]), + array($chunks[3] . $chunks[4]), + array($chunks[5] . $chunks[6]), + ); + } + + /** + * @dataProvider clean_filename_variables + */ + public function test_clean_filename_real($filename) + { + $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|'); + $filespec = $this->get_filespec(array('name' => $filename)); + $filespec->clean_filename('real', self::PREFIX); + $name = $filespec->realname; + + $this->assertEquals(0, preg_match('/%(\w{2})/', $name)); + foreach ($bad_chars as $char) + { + $this->assertFalse(strpos($name, $char)); + } + } + + public function test_clean_filename_unique() + { + $filenames = array(); + for ($tests = 0; $tests < self::TEST_COUNT; $tests++) + { + $filespec = $this->get_filespec(); + $filespec->clean_filename('unique', self::PREFIX); + $name = $filespec->realname; + + $this->assertEquals(strlen($name), 32 + strlen(self::PREFIX)); + $this->assertRegExp('#^[A-Za-z0-9]+$#', substr($name, strlen(self::PREFIX))); + $this->assertFalse(isset($filenames[$name])); + $filenames[$name] = true; + } + } + + public function get_extension_variables() + { + return array( + array('file.png', 'png'), + array('file.phpbb.gif', 'gif'), + array('file..', ''), + array('.file..jpg.webp', 'webp'), + ); + } + + /** + * @dataProvider get_extension_variables + */ + public function test_get_extension($filename, $expected) + { + $filespec = $this->get_filespec(); + $this->assertEquals($expected, $filespec->get_extension($filename)); + } + + public function is_image_variables() + { + return array( + array('gif', 'image/gif', true), + array('jpg', 'image/jpg', true), + array('png', 'image/png', true), + array('tif', 'image/tif', true), + array('txt', 'text/plain', false), + ); + } + + /** + * @dataProvider is_image_variables + */ + public function test_is_image($filename, $mimetype, $expected) + { + $filespec = $this->get_filespec(array('tmp_name' => $this->path . $filename, 'type' => $mimetype)); + $this->assertEquals($expected, $filespec->is_image()); + } + + public function move_file_variables() + { + return array( + array('gif_copy', 'gif_moved', 'image/gif', 'gif', false, true), + array('non_existant', 'still_non_existant', 'text/plain', 'txt', 'GENERAL_UPLOAD_ERROR', false), + array('txt_copy', 'txt_as_img', 'image/jpg', 'txt', false, true), + array('txt_copy_2', 'txt_moved', 'text/plain', 'txt', false, true), + array('jpg_copy', 'jpg_moved', 'image/png', 'jpg', false, true), + array('png_copy', 'png_moved', 'image/png', 'jpg', 'IMAGE_FILETYPE_MISMATCH', true), + ); + } + + /** + * @dataProvider move_file_variables + */ + public function test_move_file($tmp_name, $realname, $mime_type, $extension, $error, $expected) + { + // Global $phpbb_root_path and $phpEx are required by phpbb_chmod + global $phpbb_root_path, $phpEx; + $phpbb_root_path = ''; + $phpEx = 'php'; + + $upload = new phpbb_mock_fileupload(); + $upload->max_filesize = self::UPLOAD_MAX_FILESIZE; + + $filespec = $this->get_filespec(array( + 'tmp_name' => $this->path . 'copies/' . $tmp_name, + 'name' => $realname, + 'type' => $mime_type, + )); + $filespec->extension = $extension; + $filespec->upload = $upload; + $filespec->local = true; + + $this->assertEquals($expected, $filespec->move_file($this->path . 'copies')); + $this->assertEquals($filespec->file_moved, file_exists($this->path . 'copies/' . $realname)); + if ($error) + { + $this->assertEquals($error, $filespec->error[0]); + } + + $phpEx = ''; + } +} diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php new file mode 100644 index 0000000000..076855ab56 --- /dev/null +++ b/tests/upload/fileupload_test.php @@ -0,0 +1,115 @@ +<?php +/** + * + * @package testing + * @copyright (c) 2012 phpBB Group + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 + * + */ + +require_once __DIR__ . '/../../phpBB/includes/functions.php'; +require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; +require_once __DIR__ . '/../../phpBB/includes/functions_upload.php'; + +class phpbb_fileupload_test extends phpbb_test_case +{ + private $path; + + protected function setUp() + { + // Global $config required by unique_id + // Global $user required by several functions dealing with translations + global $config, $user; + + if (!is_array($config)) + { + $config = array(); + } + + $config['rand_seed'] = ''; + $config['rand_seed_last_update'] = time() + 600; + + $user = new phpbb_mock_user(); + $user->lang = new phpbb_mock_lang(); + $this->path = __DIR__ . '/fixture/'; + } + + private function gen_valid_filespec() + { + $filespec = new phpbb_mock_filespec(); + $filespec->filesize = 1; + $filespec->extension = 'jpg'; + $filespec->realname = 'valid'; + $filespec->width = 2; + $filespec->height = 2; + + return $filespec; + } + + protected function tearDown() + { + // Clear globals + global $config, $user; + $config = array(); + $user = null; + } + + public function test_common_checks_invalid_extension() + { + $upload = new fileupload('', array('png'), 100); + $file = $this->gen_valid_filespec(); + $upload->common_checks($file); + $this->assertEquals('DISALLOWED_EXTENSION', $file->error[0]); + } + + public function test_common_checks_invalid_filename() + { + $upload = new fileupload('', array('jpg'), 100); + $file = $this->gen_valid_filespec(); + $file->realname = 'invalid?'; + $upload->common_checks($file); + $this->assertEquals('INVALID_FILENAME', $file->error[0]); + } + + public function test_common_checks_too_large() + { + $upload = new fileupload('', array('jpg'), 100); + $file = $this->gen_valid_filespec(); + $file->filesize = 1000; + $upload->common_checks($file); + $this->assertEquals('WRONG_FILESIZE', $file->error[0]); + } + + public function test_common_checks_valid_file() + { + $upload = new fileupload('', array('jpg'), 1000); + $file = $this->gen_valid_filespec(); + $upload->common_checks($file); + $this->assertEquals(0, sizeof($file->error)); + } + + public function test_local_upload() + { + $upload = new fileupload('', array('jpg'), 1000); + + copy($this->path . 'jpg', $this->path . 'jpg.jpg'); + $file = $upload->local_upload($this->path . 'jpg.jpg'); + $this->assertEquals(0, sizeof($file->error)); + unlink($this->path . 'jpg.jpg'); + } + + public function test_valid_dimensions() + { + $upload = new fileupload('', false, false, 1, 1, 100, 100); + + $file1 = $this->gen_valid_filespec(); + $file2 = $this->gen_valid_filespec(); + $file2->height = 101; + $file3 = $this->gen_valid_filespec(); + $file3->width = 0; + + $this->assertTrue($upload->valid_dimensions($file1)); + $this->assertFalse($upload->valid_dimensions($file2)); + $this->assertFalse($upload->valid_dimensions($file3)); + } +} diff --git a/tests/upload/fixture/copies/.gitkeep b/tests/upload/fixture/copies/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/upload/fixture/copies/.gitkeep diff --git a/tests/upload/fixture/gif b/tests/upload/fixture/gif Binary files differnew file mode 100644 index 0000000000..b636f4b8df --- /dev/null +++ b/tests/upload/fixture/gif diff --git a/tests/upload/fixture/jpg b/tests/upload/fixture/jpg Binary files differnew file mode 100644 index 0000000000..3cd5038e38 --- /dev/null +++ b/tests/upload/fixture/jpg diff --git a/tests/upload/fixture/png b/tests/upload/fixture/png Binary files differnew file mode 100644 index 0000000000..5514ad40e9 --- /dev/null +++ b/tests/upload/fixture/png diff --git a/tests/upload/fixture/tif b/tests/upload/fixture/tif Binary files differnew file mode 100644 index 0000000000..248b50f9cb --- /dev/null +++ b/tests/upload/fixture/tif diff --git a/tests/upload/fixture/txt b/tests/upload/fixture/txt new file mode 100644 index 0000000000..a78c858f5c --- /dev/null +++ b/tests/upload/fixture/txt @@ -0,0 +1,2 @@ +<HTML>mime trigger</HTML> +The HTML tags should remain uppercase so that case-insensitivity can be checked. diff --git a/tests/user/lang_test.php b/tests/user/lang_test.php index d33f430955..d7ff451a70 100644 --- a/tests/user/lang_test.php +++ b/tests/user/lang_test.php @@ -7,13 +7,11 @@ * */ -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 = new phpbb_user; $user->lang = array( 'FOO' => 'BAR', 'BARZ' => 'PENG', @@ -26,6 +24,26 @@ class phpbb_user_lang_test extends phpbb_test_case 1 => '1 post', // 1 2 => '%d posts', // 2+ ), + 'ARRY_NO_ZERO' => array( + 1 => '1 post', // 1 + 2 => '%d posts', // 0, 2+ + ), + 'ARRY_MISSING' => array( + 1 => '%d post', // 1 + //Missing second plural + ), + 'ARRY_FLOAT' => array( + 1 => '1 post', // 1.x + 2 => '%1$.1f posts', // 0.x, 2+.x + ), + 'ARRY_EMPTY' => array( + ), + 'dateformat' => array( + 'AGO' => array( + 1 => '%d second', + 2 => '%d seconds', + ), + ), ); // No param @@ -51,8 +69,45 @@ class phpbb_user_lang_test extends phpbb_test_case $this->assertEquals($user->lang('ARRY', 2), '2 posts'); $this->assertEquals($user->lang('ARRY', 123), '123 posts'); - // Bug PHPBB3-9949 + // Empty array returns the language key + $this->assertEquals($user->lang('ARRY_EMPTY', 123), 'ARRY_EMPTY'); + + // No 0 key defined + $this->assertEquals($user->lang('ARRY_NO_ZERO', 0), '0 posts'); + $this->assertEquals($user->lang('ARRY_NO_ZERO', 1), '1 post'); + $this->assertEquals($user->lang('ARRY_NO_ZERO', 2), '2 posts'); + + // Array with missing keys + $this->assertEquals($user->lang('ARRY_MISSING', 2), '2 post'); + + // Floats as array key + $this->assertEquals($user->lang('ARRY_FLOAT', 1.3), '1 post'); + $this->assertEquals($user->lang('ARRY_FLOAT', 2.0), '2.0 posts'); + $this->assertEquals($user->lang('ARRY_FLOAT', 2.51), '2.5 posts'); + + // Use sub key, if first paramenter is an array + $this->assertEquals($user->lang(array('dateformat', 'AGO'), 2), '2 seconds'); + + // ticket PHPBB3-9949 - use first int to determinate the plural-form to use $this->assertEquals($user->lang('ARRY', 1, 2), '1 post'); $this->assertEquals($user->lang('ARRY', 1, 's', 2), '1 post'); + + // ticket PHPBB3-10345 - different plural rules, not just 0/1/2+ + $user = new phpbb_user; + $user->lang = array( + 'PLURAL_RULE' => 13, + 'ARRY' => array( + 0 => '%d is 0', // 0 + 1 => '%d is 1', // 1 + 2 => '%d ends with 01-10', // ending with 01-10 + 3 => '%d ends with 11-19', // ending with 11-19 + 4 => '%d is part of the last rule', // everything else + ), + ); + $this->assertEquals($user->lang('ARRY', 0), '0 is 0'); + $this->assertEquals($user->lang('ARRY', 1), '1 is 1'); + $this->assertEquals($user->lang('ARRY', 103), '103 ends with 01-10'); + $this->assertEquals($user->lang('ARRY', 15), '15 ends with 11-19'); + $this->assertEquals($user->lang('ARRY', 300), '300 is part of the last rule'); } } diff --git a/tests/utf/utf8_clean_string_test.php b/tests/utf/utf8_clean_string_test.php index 70bd549d5b..ae11e00fbd 100644 --- a/tests/utf/utf8_clean_string_test.php +++ b/tests/utf/utf8_clean_string_test.php @@ -11,7 +11,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; class phpbb_utf_utf8_clean_string_test extends phpbb_test_case { - public static function cleanable_strings() + public function cleanable_strings() { return array( array('MiXed CaSe', 'mixed case', 'Checking case folding'), diff --git a/tests/wrapper/phpbb_php_ini_fake.php b/tests/wrapper/phpbb_php_ini_fake.php new file mode 100644 index 0000000000..49bc5936e5 --- /dev/null +++ b/tests/wrapper/phpbb_php_ini_fake.php @@ -0,0 +1,16 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2011 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_php_ini_fake extends phpbb_php_ini +{ + function get($varname) + { + return $varname; + } +} diff --git a/tests/wrapper/phpbb_php_ini_test.php b/tests/wrapper/phpbb_php_ini_test.php new file mode 100644 index 0000000000..8e08d5c204 --- /dev/null +++ b/tests/wrapper/phpbb_php_ini_test.php @@ -0,0 +1,86 @@ +<?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_php_ini_fake.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; + +class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case +{ + protected $php_ini; + + public function setUp() + { + $this->php_ini = new phpbb_php_ini_fake; + } + + public function test_get_string() + { + $this->assertSame(false, $this->php_ini->get_string(false)); + $this->assertSame('phpbb', $this->php_ini->get_string(' phpbb ')); + } + + public function test_get_bool() + { + $this->assertSame(true, $this->php_ini->get_bool('ON')); + $this->assertSame(true, $this->php_ini->get_bool('on')); + $this->assertSame(true, $this->php_ini->get_bool('1')); + + $this->assertSame(false, $this->php_ini->get_bool('OFF')); + $this->assertSame(false, $this->php_ini->get_bool('off')); + $this->assertSame(false, $this->php_ini->get_bool('0')); + $this->assertSame(false, $this->php_ini->get_bool('')); + } + + public function test_get_int() + { + $this->assertSame(1234, $this->php_ini->get_int('1234')); + $this->assertSame(-12345, $this->php_ini->get_int('-12345')); + $this->assertSame(false, $this->php_ini->get_int('phpBB')); + } + + public function test_get_float() + { + $this->assertSame(1234.0, $this->php_ini->get_float('1234')); + $this->assertSame(-12345.0, $this->php_ini->get_float('-12345')); + $this->assertSame(false, $this->php_ini->get_float('phpBB')); + } + + public function test_get_bytes_invalid() + { + $this->assertSame(false, $this->php_ini->get_bytes(false)); + $this->assertSame(false, $this->php_ini->get_bytes('phpBB')); + $this->assertSame(false, $this->php_ini->get_bytes('k')); + $this->assertSame(false, $this->php_ini->get_bytes('-k')); + $this->assertSame(false, $this->php_ini->get_bytes('M')); + $this->assertSame(false, $this->php_ini->get_bytes('-M')); + } + + /** + * @dataProvider get_bytes_data + */ + public function test_get_bytes($expected, $value) + { + $actual = $this->php_ini->get_bytes($value); + + $this->assertTrue(is_float($actual) || is_int($actual)); + $this->assertEquals($expected, $actual); + } + + static public function get_bytes_data() + { + return array( + array(32 * pow(2, 20), '32m'), + array(- 32 * pow(2, 20), '-32m'), + array(8 * pow(2, 30), '8G'), + array(- 8 * pow(2, 30), '-8G'), + array(1234, '1234'), + array(-12345, '-12345'), + ); + } +} |