diff options
Diffstat (limited to 'tests')
129 files changed, 7413 insertions, 614 deletions
diff --git a/tests/RUNNING_TESTS.txt b/tests/RUNNING_TESTS.md index cede81d59d..f2688ab675 100644 --- a/tests/RUNNING_TESTS.txt +++ b/tests/RUNNING_TESTS.md @@ -7,9 +7,14 @@ Prerequisites PHPUnit ------- -phpBB unit tests use PHPUnit framework. Version 3.5 or better is required -to run the tests. PHPUnit prefers to be installed via PEAR; refer to -http://www.phpunit.de/ for more information. +phpBB unit tests use the PHPUnit framework (see http://www.phpunit.de for more +information). Version 3.5 or higher is required to run the tests. PHPUnit can +be installed via Composer together with other development dependencies as +follows. + + $ cd phpBB + $ php ../composer.phar install --dev + $ cd .. PHP extensions -------------- @@ -45,9 +50,11 @@ Database Tests By default all tests requiring a database connection will use sqlite. If you do not have sqlite installed the tests will be skipped. If you wish to run the tests on a different database you have to create a test_config.php file within -your tests directory following the same format as phpBB's config.php. An -example for mysqli can be found below. More information on configuration -options can be found on the wiki (see below). +your tests directory following the same format as phpBB's config.php. Testing +makes use of a seperate database defined in this config file and before running +the tests each time this database is deleted. An example for mysqli can be +found below. More information on configuration options can be found on the +wiki (see below). <?php $dbms = 'phpbb_db_driver_mysqli'; @@ -77,14 +84,16 @@ In order to run tests on some of the databases that we support, it will be necessary to provide a custom DSN string in test_config.php. This is only needed for MSSQL 2000+ (PHP module), MSSQL via ODBC, and Firebird when PDO_Firebird does not work on your system -(https://bugs.php.net/bug.php?id=61183). The variable must be named $custom_dsn. +(https://bugs.php.net/bug.php?id=61183). The variable must be named `$custom_dsn`. Examples: Firebird using http://www.firebirdsql.org/en/odbc-driver/ -$custom_dsn = "Driver={Firebird/InterBase(r) driver};dbname=$dbhost:$dbname"; + + $custom_dsn = "Driver={Firebird/InterBase(r) driver};dbname=$dbhost:$dbname"; MSSQL -$custom_dsn = "Driver={SQL Server Native Client 10.0};Server=$dbhost;Database=$dbname"; + + $custom_dsn = "Driver={SQL Server Native Client 10.0};Server=$dbhost;Database=$dbname"; The other fields in test_config.php should be filled out as you would normally to connect to that database in phpBB. @@ -113,7 +122,7 @@ Running Once the prerequisites are installed, run the tests from the project root directory (above phpBB): - $ phpunit + $ phpBB/vendor/bin/phpunit Slow tests -------------- @@ -123,10 +132,38 @@ Thus these tests are in the `slow` group, which is excluded by default. You can enable slow tests by copying the phpunit.xml.all file to phpunit.xml. If you only want the slow tests, run: - $ phpunit --group slow + $ phpBB/vendor/bin/phpunit --group slow + +Functional tests +----------------- + +Functional tests test software the way a user would. They simulate a user +browsing the website, but they do these steps in an automated way. +phpBB allows you to write such tests. + +Running +======= + +Running the tests requires your phpBB3 repository to be accessible through a +local web server. You will need to supply the URL to the webserver in +the 'tests/test_config.php' file. This is as simple as defining the +'$phpbb_functional_url' variable, which contains the URL for the directory containing +the board. Make sure you include the trailing slash. Note that without extensive +changes to the test framework, you cannot use a board outside of the repository +on which to run tests. + + $phpbb_functional_url = 'http://localhost/phpBB3/'; + +To then run the tests, you run PHPUnit, but use the phpunit.xml.functional +config file instead of the default one. Specify this through the "-c" option: + + $ phpBB/vendor/bin/phpunit -c phpunit.xml.functional + +This will change your board's config.php file, but it makes a backup at +config_dev.php, so you can restore it after the test run is complete. More Information ================ Further information is available on phpbb wiki: -http://wiki.phpbb.com/Unit_Tests +http://wiki.phpbb.com/Automated_Tests diff --git a/tests/auth/fixtures/user.xml b/tests/auth/fixtures/user.xml new file mode 100644 index 0000000000..34584babbf --- /dev/null +++ b/tests/auth/fixtures/user.xml @@ -0,0 +1,33 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_users"> + <column>user_id</column> + <column>username</column> + <column>username_clean</column> + <column>user_password</column> + <column>user_passchg</column> + <column>user_pass_convert</column> + <column>user_email</column> + <column>user_type</column> + <column>user_login_attempts</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>1</value> + <value>foobar</value> + <value>foobar</value> + <value>$H$9E45lK6J8nLTSm9oJE5aNCSTFK9wqa/</value> + <value>0</value> + <value>0</value> + <value>example@example.com</value> + <value>0</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/auth/provider_apache_test.php b/tests/auth/provider_apache_test.php new file mode 100644 index 0000000000..0ca6ef763e --- /dev/null +++ b/tests/auth/provider_apache_test.php @@ -0,0 +1,206 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_auth_provider_apache_test extends phpbb_database_test_case +{ + protected $provider; + protected $user; + protected $request; + + protected function setup() + { + parent::setUp(); + + global $phpbb_root_path, $phpEx; + + $db = $this->new_dbal(); + $config = new phpbb_config(array()); + $this->request = $this->getMock('phpbb_request'); + $this->user = $this->getMock('phpbb_user'); + + $this->provider = new phpbb_auth_provider_apache($db, $config, $this->request, $this->user, $phpbb_root_path, $phpEx); + } + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/user.xml'); + } + + /** + * Test to see if a user is identified to Apache. Expects false if they are. + */ + public function test_init() + { + $this->user->data['username'] = 'foobar'; + $this->request->expects($this->once()) + ->method('is_set') + ->with('PHP_AUTH_USER', + phpbb_request_interface::SERVER) + ->will($this->returnValue(true)); + $this->request->expects($this->once()) + ->method('server') + ->with('PHP_AUTH_USER') + ->will($this->returnValue('foobar')); + + $this->assertFalse($this->provider->init()); + } + + public function test_login() + { + $username = 'foobar'; + $password = 'example'; + + $this->request->expects($this->once()) + ->method('is_set') + ->with('PHP_AUTH_USER', + phpbb_request_interface::SERVER) + ->will($this->returnValue(true)); + $this->request->expects($this->at(1)) + ->method('server') + ->with('PHP_AUTH_USER') + ->will($this->returnValue('foobar')); + $this->request->expects($this->at(2)) + ->method('server') + ->with('PHP_AUTH_PW') + ->will($this->returnValue('example')); + + $expected = array( + 'status' => LOGIN_SUCCESS, + 'error_msg' => false, + 'user_row' => array( + 'user_id' => '1', + 'username' => 'foobar', + 'user_password' => '$H$9E45lK6J8nLTSm9oJE5aNCSTFK9wqa/', + 'user_passchg' => '0', + 'user_email' => 'example@example.com', + 'user_type' => '0', + ), + ); + + $this->assertEquals($expected, $this->provider->login($username, $password)); + } + + public function test_autologin() + { + $this->request->expects($this->once()) + ->method('is_set') + ->with('PHP_AUTH_USER', + phpbb_request_interface::SERVER) + ->will($this->returnValue(true)); + $this->request->expects($this->at(1)) + ->method('server') + ->with('PHP_AUTH_USER') + ->will($this->returnValue('foobar')); + $this->request->expects($this->at(2)) + ->method('server') + ->with('PHP_AUTH_PW') + ->will($this->returnValue('example')); + + $expected = array( + 'user_id' => '1', + 'user_type' => '0', + 'group_id' => '3', + 'user_permissions' => '', + 'user_perm_from' => '0', + 'user_ip' => '', + 'user_regdate' => '0', + 'username' => 'foobar', + 'username_clean' => 'foobar', + 'user_password' => '$H$9E45lK6J8nLTSm9oJE5aNCSTFK9wqa/', + 'user_passchg' => '0', + 'user_pass_convert' => '0', + 'user_email' => 'example@example.com', + 'user_email_hash' => '0', + 'user_birthday' => '', + 'user_lastvisit' => '0', + 'user_lastmark' => '0', + 'user_lastpost_time' => '0', + 'user_lastpage' => '', + 'user_last_confirm_key' => '', + 'user_last_search' => '0', + 'user_warnings' => '0', + 'user_last_warning' => '0', + 'user_login_attempts' => '0', + 'user_inactive_reason' => '0', + 'user_inactive_time' => '0', + 'user_posts' => '0', + 'user_lang' => '', + 'user_timezone' => 'UTC', + 'user_dateformat' => 'd M Y H:i', + 'user_style' => '0', + 'user_rank' => '0', + 'user_colour' => '', + 'user_new_privmsg' => '0', + 'user_unread_privmsg' => '0', + 'user_last_privmsg' => '0', + 'user_message_rules' => '0', + 'user_full_folder' => '-3', + 'user_emailtime' => '0', + 'user_topic_show_days' => '0', + 'user_topic_sortby_type' => 't', + 'user_topic_sortby_dir' => 'd', + 'user_post_show_days' => '0', + 'user_post_sortby_type' => 't', + 'user_post_sortby_dir' => 'a', + 'user_notify' => '0', + 'user_notify_pm' => '1', + 'user_notify_type' => '0', + 'user_allow_pm' => '1', + 'user_allow_viewonline' => '1', + 'user_allow_viewemail' => '1', + 'user_allow_massemail' => '1', + 'user_options' => '230271', + 'user_avatar' => '', + 'user_avatar_type' => '', + 'user_avatar_width' => '0', + 'user_avatar_height' => '0', + 'user_sig' => '', + 'user_sig_bbcode_uid' => '', + 'user_sig_bbcode_bitfield' => '', + 'user_from' => '', + 'user_icq' => '', + 'user_aim' => '', + 'user_yim' => '', + 'user_msnm' => '', + 'user_jabber' => '', + 'user_website' => '', + 'user_occ' => '', + 'user_interests' => '', + 'user_actkey' => '', + 'user_newpasswd' => '', + 'user_form_salt' => '', + 'user_new' => '1', + 'user_reminded' => '0', + 'user_reminded_time' => '0', + ); + + $this->assertEquals($expected, $this->provider->autologin()); + } + + public function test_validate_session() + { + $user = array( + 'username' => 'foobar', + 'user_type' + ); + $this->request->expects($this->once()) + ->method('is_set') + ->with('PHP_AUTH_USER', + phpbb_request_interface::SERVER) + ->will($this->returnValue(true)); + $this->request->expects($this->once()) + ->method('server') + ->with('PHP_AUTH_USER') + ->will($this->returnValue('foobar')); + + $this->assertTrue($this->provider->validate_session($user)); + } +} diff --git a/tests/auth/provider_db_test.php b/tests/auth/provider_db_test.php new file mode 100644 index 0000000000..d876683f84 --- /dev/null +++ b/tests/auth/provider_db_test.php @@ -0,0 +1,50 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_auth_provider_db_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/user.xml'); + } + + public function test_login() + { + global $phpbb_root_path, $phpEx; + + $db = $this->new_dbal(); + $config = new phpbb_config(array( + 'ip_login_limit_max' => 0, + 'ip_login_limit_use_forwarded' => 0, + 'max_login_attempts' => 0, + )); + $request = $this->getMock('phpbb_request'); + $user = $this->getMock('phpbb_user'); + $provider = new phpbb_auth_provider_db($db, $config, $request, $user, $phpbb_root_path, $phpEx); + + $expected = array( + 'status' => LOGIN_SUCCESS, + 'error_msg' => false, + 'user_row' => array( + 'user_id' => '1', + 'username' => 'foobar', + 'user_password' => '$H$9E45lK6J8nLTSm9oJE5aNCSTFK9wqa/', + 'user_passchg' => '0', + 'user_pass_convert' => '0', + 'user_email' => 'example@example.com', + 'user_type' => '0', + 'user_login_attempts' => '0', + ), + ); + + $this->assertEquals($expected, $provider->login('foobar', 'example')); + } +} diff --git a/tests/avatar/manager_test.php b/tests/avatar/manager_test.php index f01ea47c25..cb895b521a 100644 --- a/tests/avatar/manager_test.php +++ b/tests/avatar/manager_test.php @@ -1,90 +1,160 @@ -<?php
-/**
-*
-* @package testing
-* @version $Id$
-* @copyright (c) 2012 phpBB Group
-* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
-*
-*/
-
-require_once dirname(__FILE__) . '/driver/foobar.php';
-
-class phpbb_avatar_manager_test extends PHPUnit_Framework_TestCase
-{
- public function setUp()
- {
- global $phpbb_root_path, $phpEx;
-
- // Mock phpbb_container
- $this->phpbb_container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $this->phpbb_container->expects($this->any())
- ->method('get')
- ->with('avatar.driver.foobar')->will($this->returnValue('avatar.driver.foobar'));
-
- // Prepare dependencies for avatar manager and driver
- $config = new phpbb_config(array());
- $request = $this->getMock('phpbb_request');
- $cache = $this->getMock('phpbb_cache_driver_interface');
-
- $this->avatar_foobar = $this->getMock('phpbb_avatar_driver_foobar', array('get_name'), array($config, $phpbb_root_path, $phpEx, $cache));
- $this->avatar_foobar->expects($this->any())
- ->method('get_name')
- ->will($this->returnValue('avatar.driver.foobar'));
- $this->avatar_barfoo = $this->getMock('phpbb_avatar_driver_barfoo', array('get_name'));
- $this->avatar_barfoo->expects($this->any())
- ->method('get_name')
- ->will($this->returnValue('avatar.driver.barfoo'));
-
- $avatar_drivers = array($this->avatar_foobar, $this->avatar_barfoo);
-
- $config['allow_avatar_' . get_class($this->avatar_foobar)] = true;
- $config['allow_avatar_' . get_class($this->avatar_barfoo)] = false;
-
- // Set up avatar manager
- $this->manager = new phpbb_avatar_manager($config, $avatar_drivers, $this->phpbb_container);
- }
-
- public function test_get_driver()
- {
- $driver = $this->manager->get_driver('avatar.driver.foobar', false);
- $this->assertEquals('avatar.driver.foobar', $driver);
-
- $driver = $this->manager->get_driver('avatar.driver.foo_wrong', false);
- $this->assertNull($driver);
-
- $driver = $this->manager->get_driver('avatar.driver.foobar');
- $this->assertEquals('avatar.driver.foobar', $driver);
-
- $driver = $this->manager->get_driver('avatar.driver.foo_wrong');
- $this->assertNull($driver);
- }
-
- public function test_get_all_drivers()
- {
- $drivers = $this->manager->get_all_drivers();
- $this->assertArrayHasKey('avatar.driver.foobar', $drivers);
- $this->assertArrayHasKey('avatar.driver.barfoo', $drivers);
- $this->assertEquals('avatar.driver.foobar', $drivers['avatar.driver.foobar']);
- $this->assertEquals('avatar.driver.barfoo', $drivers['avatar.driver.barfoo']);
- }
-
- public function test_get_enabled_drivers()
- {
- $drivers = $this->manager->get_enabled_drivers();
- $this->assertArrayHasKey('avatar.driver.foobar', $drivers);
- $this->assertArrayNotHasKey('avatar.driver.barfoo', $drivers);
- $this->assertEquals('avatar.driver.foobar', $drivers['avatar.driver.foobar']);
- }
-
- public function test_get_avatar_settings()
- {
- $avatar_settings = $this->manager->get_avatar_settings($this->avatar_foobar);
-
- $expected_settings = array(
- 'allow_avatar_' . get_class($this->avatar_foobar) => array('lang' => 'ALLOW_' . strtoupper(get_class($this->avatar_foobar)), 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false),
- );
-
- $this->assertEquals($expected_settings, $avatar_settings);
- }
-}
+<?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__) . '/driver/foobar.php'; + +class phpbb_avatar_manager_test extends PHPUnit_Framework_TestCase +{ + public function setUp() + { + global $phpbb_root_path, $phpEx; + + // Mock phpbb_container + $this->phpbb_container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $this->phpbb_container->expects($this->any()) + ->method('get') + ->with('avatar.driver.foobar')->will($this->returnValue('avatar.driver.foobar')); + + // Prepare dependencies for avatar manager and driver + $config = new phpbb_config(array()); + $request = $this->getMock('phpbb_request'); + $cache = $this->getMock('phpbb_cache_driver_interface'); + + $this->avatar_foobar = $this->getMock('phpbb_avatar_driver_foobar', array('get_name'), array($config, $phpbb_root_path, $phpEx, $cache)); + $this->avatar_foobar->expects($this->any()) + ->method('get_name') + ->will($this->returnValue('avatar.driver.foobar')); + $this->avatar_barfoo = $this->getMock('phpbb_avatar_driver_barfoo', array('get_name')); + $this->avatar_barfoo->expects($this->any()) + ->method('get_name') + ->will($this->returnValue('avatar.driver.barfoo')); + + $avatar_drivers = array($this->avatar_foobar, $this->avatar_barfoo); + + $config['allow_avatar_' . get_class($this->avatar_foobar)] = true; + $config['allow_avatar_' . get_class($this->avatar_barfoo)] = false; + + // Set up avatar manager + $this->manager = new phpbb_avatar_manager($config, $avatar_drivers, $this->phpbb_container); + } + + public function test_get_driver() + { + $driver = $this->manager->get_driver('avatar.driver.foobar', false); + $this->assertEquals('avatar.driver.foobar', $driver); + + $driver = $this->manager->get_driver('avatar.driver.foo_wrong', false); + $this->assertNull($driver); + + $driver = $this->manager->get_driver('avatar.driver.foobar'); + $this->assertEquals('avatar.driver.foobar', $driver); + + $driver = $this->manager->get_driver('avatar.driver.foo_wrong'); + $this->assertNull($driver); + } + + public function test_get_all_drivers() + { + $drivers = $this->manager->get_all_drivers(); + $this->assertArrayHasKey('avatar.driver.foobar', $drivers); + $this->assertArrayHasKey('avatar.driver.barfoo', $drivers); + $this->assertEquals('avatar.driver.foobar', $drivers['avatar.driver.foobar']); + $this->assertEquals('avatar.driver.barfoo', $drivers['avatar.driver.barfoo']); + } + + public function test_get_enabled_drivers() + { + $drivers = $this->manager->get_enabled_drivers(); + $this->assertArrayHasKey('avatar.driver.foobar', $drivers); + $this->assertArrayNotHasKey('avatar.driver.barfoo', $drivers); + $this->assertEquals('avatar.driver.foobar', $drivers['avatar.driver.foobar']); + } + + public function test_get_avatar_settings() + { + $avatar_settings = $this->manager->get_avatar_settings($this->avatar_foobar); + + $expected_settings = array( + 'allow_avatar_' . get_class($this->avatar_foobar) => array('lang' => 'ALLOW_' . strtoupper(get_class($this->avatar_foobar)), 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false), + ); + + $this->assertEquals($expected_settings, $avatar_settings); + } + + public function database_row_data() + { + return array( + array( + array( + 'user_avatar' => '', + 'user_avatar_type' => '', + 'user_avatar_width' => '', + 'user_avatar_height' => '', + ), + array( + 'avatar' => '', + 'avatar_type' => '', + 'avatar_width' => '', + 'avatar_height' => '', + ), + ), + array( + array( + 'group_avatar' => '', + 'group_avatar_type' => '', + 'group_avatar_width' => '', + 'group_avatar_height' => '', + ), + array( + 'avatar' => '', + 'avatar_type' => '', + 'avatar_width' => '', + 'avatar_height' => '', + ), + ), + array( + array(), + array( + 'avatar' => '', + 'avatar_type' => '', + 'avatar_width' => '', + 'avatar_height' => '', + ), + ), + array( + array( + 'foobar_avatar' => '', + 'foobar_avatar_type' => '', + 'foobar_avatar_width' => '', + 'foobar_avatar_height' => '', + ), + array( + 'foobar_avatar' => '', + 'foobar_avatar_type' => '', + 'foobar_avatar_width' => '', + 'foobar_avatar_height' => '', + ), + ), + ); + } + + /** + * @dataProvider database_row_data + */ + public function test_clean_row(array $input, array $output) + { + $cleaned_row = array(); + + $cleaned_row = phpbb_avatar_manager::clean_row($input); + foreach ($output as $key => $null) + { + $this->assertArrayHasKey($key, $cleaned_row); + } + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 1017e0c72f..a38740c82d 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -16,11 +16,11 @@ $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 = 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 = 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 = new phpbb_class_loader('phpbb_', $phpbb_root_path . 'includes/', "php"); $phpbb_class_loader->register(); require_once 'test_framework/phpbb_test_case_helpers.php'; diff --git a/tests/class_loader/class_loader_test.php b/tests/class_loader/class_loader_test.php index 76af4dde37..bf27c7c217 100644 --- a/tests/class_loader/class_loader_test.php +++ b/tests/class_loader/class_loader_test.php @@ -71,8 +71,8 @@ class phpbb_class_loader_test extends PHPUnit_Framework_TestCase $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); + $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/'; diff --git a/tests/controller/controller_test.php b/tests/controller/controller_test.php index 198fb3c6dd..c06bf7d548 100644 --- a/tests/controller/controller_test.php +++ b/tests/controller/controller_test.php @@ -14,7 +14,7 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; -class phpbb_controller_test extends phpbb_test_case +class phpbb_controller_controller_test extends phpbb_test_case { public function setUp() { diff --git a/tests/controller/helper_url_test.php b/tests/controller/helper_url_test.php new file mode 100644 index 0000000000..2c22700ca6 --- /dev/null +++ b/tests/controller/helper_url_test.php @@ -0,0 +1,59 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_controller_helper_url_test extends phpbb_test_case +{ + + public function helper_url_data() + { + return array( + array('foo/bar?t=1&f=2', false, true, false, 'app.php?t=1&f=2&controller=foo/bar', 'parameters in url-argument'), + array('foo/bar', 't=1&f=2', true, false, 'app.php?controller=foo/bar&t=1&f=2', 'parameters in params-argument using amp'), + array('foo/bar', 't=1&f=2', false, false, 'app.php?controller=foo/bar&t=1&f=2', 'parameters in params-argument using &'), + array('foo/bar', array('t' => 1, 'f' => 2), true, false, 'app.php?controller=foo/bar&t=1&f=2', 'parameters in params-argument as array'), + + // Custom sid parameter + array('foo/bar', 't=1&f=2', true, 'custom-sid', 'app.php?controller=foo/bar&t=1&f=2&sid=custom-sid', 'using session_id'), + + // Testing anchors + array('foo/bar?t=1&f=2#anchor', false, true, false, 'app.php?t=1&f=2&controller=foo/bar#anchor', 'anchor in url-argument'), + array('foo/bar', 't=1&f=2#anchor', true, false, 'app.php?controller=foo/bar&t=1&f=2#anchor', 'anchor in params-argument'), + array('foo/bar', array('t' => 1, 'f' => 2, '#' => 'anchor'), true, false, 'app.php?controller=foo/bar&t=1&f=2#anchor', 'anchor in params-argument (array)'), + + // Anchors and custom sid + array('foo/bar?t=1&f=2#anchor', false, true, 'custom-sid', 'app.php?t=1&f=2&controller=foo/bar&sid=custom-sid#anchor', 'anchor in url-argument using session_id'), + array('foo/bar', 't=1&f=2#anchor', true, 'custom-sid', 'app.php?controller=foo/bar&t=1&f=2&sid=custom-sid#anchor', 'anchor in params-argument using session_id'), + array('foo/bar', array('t' => 1, 'f' => 2, '#' => 'anchor'), true, 'custom-sid', 'app.php?controller=foo/bar&t=1&f=2&sid=custom-sid#anchor', 'anchor in params-argument (array) using session_id'), + + // Empty parameters should not append the & + array('foo/bar', false, true, false, 'app.php?controller=foo/bar', 'no params using bool false'), + array('foo/bar', '', true, false, 'app.php?controller=foo/bar', 'no params using empty string'), + array('foo/bar', array(), true, false, 'app.php?controller=foo/bar', 'no params using empty array'), + ); + } + + /** + * @dataProvider helper_url_data + */ + public function test_helper_url($route, $params, $is_amp, $session_id, $expected, $description) + { + global $phpbb_dispatcher; + + $phpbb_dispatcher = new phpbb_mock_event_dispatcher; + $this->style_resource_locator = new phpbb_style_resource_locator(); + $this->user = $this->getMock('phpbb_user'); + $this->template = new phpbb_template($phpbb_root_path, $phpEx, $config, $this->user, $this->style_resource_locator, new phpbb_template_context()); + + $helper = new phpbb_controller_helper($this->template, $this->user, '', 'php'); + $this->assertEquals($helper->url($route, $params, $is_amp, $session_id), $expected); + } +} + diff --git a/tests/dbal/migrator_test.php b/tests/dbal/migrator_test.php index 89669b85ec..1e40c9c6d6 100644 --- a/tests/dbal/migrator_test.php +++ b/tests/dbal/migrator_test.php @@ -55,14 +55,18 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case 'phpbb_', $tools ); + + $container = new phpbb_mock_container_builder(); + $container->set('migrator', $migrator); + $this->extension_manager = new phpbb_extension_manager( - new phpbb_mock_container_builder(), + $container, $this->db, $this->config, - $this->migrator, + new phpbb_filesystem(), 'phpbb_ext', dirname(__FILE__) . '/../../phpBB/', - '.php', + 'php', null ); } @@ -144,15 +148,8 @@ class phpbb_dbal_migrator_test extends phpbb_database_test_case $this->migrator->update(); } - if ($migrator_test_if_true_failed) - { - $this->fail('True test failed'); - } - - if ($migrator_test_if_false_failed) - { - $this->fail('False test failed'); - } + $this->assertFalse($migrator_test_if_true_failed, 'True test failed'); + $this->assertFalse($migrator_test_if_false_failed, 'False test failed'); } public function test_recall() diff --git a/tests/dbal/migrator_tool_module.php b/tests/dbal/migrator_tool_module_test.php index 6937b6f8c5..3303086b26 100644 --- a/tests/dbal/migrator_tool_module.php +++ b/tests/dbal/migrator_tool_module_test.php @@ -21,7 +21,7 @@ class phpbb_dbal_migrator_tool_module_test extends phpbb_database_test_case public function setup() { // Need global $db, $user for delete_module function in acp_modules - global $phpbb_root_path, $phpEx, $skip_add_log, $db, $user; + global $phpbb_root_path, $phpEx, $skip_add_log, $db, $user, $phpbb_log; parent::setup(); @@ -32,6 +32,11 @@ class phpbb_dbal_migrator_tool_module_test extends phpbb_database_test_case $this->cache = new phpbb_cache_service(new phpbb_cache_driver_null(), new phpbb_config(array()), $this->db, $phpbb_root_path, $phpEx); $user = $this->user = new phpbb_user(); + $cache = new phpbb_mock_cache; + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $auth = $this->getMock('phpbb_auth'); + $phpbb_log = new phpbb_log($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE); + $this->tool = new phpbb_db_migration_tool_module($this->db, $this->cache, $this->user, $phpbb_root_path, $phpEx, 'phpbb_modules'); } diff --git a/tests/dbal/migrator_tool_permission.php b/tests/dbal/migrator_tool_permission_test.php index 438ab2b28e..438ab2b28e 100644 --- a/tests/dbal/migrator_tool_permission.php +++ b/tests/dbal/migrator_tool_permission_test.php diff --git a/tests/dbal/sql_insert_buffer_test.php b/tests/dbal/sql_insert_buffer_test.php new file mode 100644 index 0000000000..45339a6b50 --- /dev/null +++ b/tests/dbal/sql_insert_buffer_test.php @@ -0,0 +1,116 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_dbal_sql_insert_buffer_test extends phpbb_database_test_case +{ + protected $db; + protected $buffer; + + public function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->buffer = new phpbb_db_sql_insert_buffer($this->db, 'phpbb_config', 2); + $this->assert_config_count(2); + } + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function test_multi_insert_disabled_insert_and_flush() + { + $this->db->multi_insert = false; + $this->assertTrue($this->buffer->insert($this->get_row(1))); + $this->assert_config_count(3); + $this->assertFalse($this->buffer->flush()); + $this->assert_config_count(3); + } + + public function test_multi_insert_enabled_insert_and_flush() + { + $this->check_multi_insert_support(); + $this->assertFalse($this->buffer->insert($this->get_row(1))); + $this->assert_config_count(2); + $this->assertTrue($this->buffer->flush()); + $this->assert_config_count(3); + } + + public function test_multi_insert_disabled_insert_with_flush() + { + $this->db->multi_insert = false; + $this->assertTrue($this->buffer->insert($this->get_row(1))); + $this->assert_config_count(3); + $this->assertTrue($this->buffer->insert($this->get_row(2))); + $this->assert_config_count(4); + } + + public function test_multi_insert_enabled_insert_with_flush() + { + $this->check_multi_insert_support(); + $this->assertFalse($this->buffer->insert($this->get_row(1))); + $this->assert_config_count(2); + $this->assertTrue($this->buffer->insert($this->get_row(2))); + $this->assert_config_count(4); + } + + public function test_multi_insert_disabled_insert_all_and_flush() + { + $this->db->multi_insert = false; + $this->assertTrue($this->buffer->insert_all($this->get_rows(3))); + $this->assert_config_count(5); + } + + public function test_multi_insert_enabled_insert_all_and_flush() + { + $this->check_multi_insert_support(); + $this->assertTrue($this->buffer->insert_all($this->get_rows(3))); + $this->assert_config_count(4); + $this->assertTrue($this->buffer->flush()); + $this->assert_config_count(5); + } + + protected function assert_config_count($num_configs) + { + $sql = 'SELECT COUNT(*) AS num_configs + FROM phpbb_config'; + $result = $this->db->sql_query($sql); + $this->assertEquals($num_configs, $this->db->sql_fetchfield('num_configs')); + $this->db->sql_freeresult($result); + } + + protected function check_multi_insert_support() + { + if (!$this->db->multi_insert) + { + $this->markTestSkipped('Database does not support multi_insert'); + } + } + + protected function get_row($rownum) + { + return array( + 'config_name' => "name$rownum", + 'config_value' => "value$rownum", + 'is_dynamic' => '0', + ); + } + + protected function get_rows($n) + { + $result = array(); + for ($i = 0; $i < $n; ++$i) + { + $result[] = $this->get_row($i); + } + return $result; + } +} diff --git a/tests/download/http_user_agent_test.php b/tests/download/http_user_agent_test.php new file mode 100644 index 0000000000..166a186913 --- /dev/null +++ b/tests/download/http_user_agent_test.php @@ -0,0 +1,130 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_user_agent_test extends phpbb_test_case +{ + public function user_agents_check_greater_ie_version() + { + return array( + // user agent + // IE version + // expected + array( + 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)', + 7, + true, + ), + array( + 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)', + 7, + true, + ), + array( + 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)', + 7, + true, + ), + array( + 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)', + 7, + false, + ), + array( + 'Mozilla/4.0 (compatible; MSIE 6.1; Windows XP; .NET CLR 1.1.4322; .NET CLR 2.0.50727)', + 7, + false, + ), + array( + 'Mozilla/4.0 (compatible; MSIE 6.01; Windows NT 6.0)', + 7, + false, + ), + array( + 'Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)', + 7, + false, + ), + array( + 'Mozilla/5.0 (Windows NT 6.2; Win64; x64;) Gecko/20100101 Firefox/20.0', + 7, + false, + ), + array( + 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36', + 7, + false, + ), + array( + 'Googlebot-Image/1.0', + 7, + false, + ), + array( + 'Googlebot/2.1 ( http://www.google.com/bot.html)', + 7, + false, + ), + array( + 'Lynx/2.8.3dev.9 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6', + 7, + false, + ), + array( + 'Links (0.9x; Linux 2.4.7-10 i686)', + 7, + false, + ), + array( + 'Opera/9.60 (Windows NT 5.1; U; de) Presto/2.1.1', + 7, + false, + ), + array( + 'Mozilla/4.0 (compatible; MSIE 5.0; Windows NT;)', + 7, + false, + ), + array( + 'Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 4.0) Opera 6.01 [en]', + 7, + false, + ), + array( + 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.24', + 7, + false, + ), + array( + 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)', + 8, + true, + ), + array( + 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)', + 9, + true, + ), + array( + 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)', + 10, + false, + ), + ); + } + + /** + * @dataProvider user_agents_check_greater_ie_version + */ + public function test_is_greater_ie_version($user_agent, $version, $expected) + { + $this->assertEquals($expected, phpbb_is_greater_ie_version($user_agent, $version)); + } +} diff --git a/tests/extension/ext/bar/styles/prosilver/template/foobar_body.html b/tests/extension/ext/bar/styles/prosilver/template/foobar_body.html new file mode 100644 index 0000000000..00c2a84a18 --- /dev/null +++ b/tests/extension/ext/bar/styles/prosilver/template/foobar_body.html @@ -0,0 +1 @@ +bertie rules! diff --git a/tests/extension/ext/barfoo/acp/a_info.php b/tests/extension/ext/barfoo/acp/a_info.php new file mode 100644 index 0000000000..cd7e4e574b --- /dev/null +++ b/tests/extension/ext/barfoo/acp/a_info.php @@ -0,0 +1,16 @@ +<?php + +class phpbb_ext_barfoo_acp_a_info +{ + public function module() + { + return array( + 'filename' => 'phpbb_ext_barfoo_acp_a_module', + 'title' => 'Barfoo', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('ACP_MODS')), + ), + ); + } +} diff --git a/tests/extension/ext/barfoo/acp/a_module.php b/tests/extension/ext/barfoo/acp/a_module.php new file mode 100644 index 0000000000..5bedb49645 --- /dev/null +++ b/tests/extension/ext/barfoo/acp/a_module.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_barfoo_acp_a_module +{ +} diff --git a/tests/extension/ext/barfoo/ext.php b/tests/extension/ext/barfoo/ext.php new file mode 100644 index 0000000000..2e11ece8d1 --- /dev/null +++ b/tests/extension/ext/barfoo/ext.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_barfoo_ext extends phpbb_extension_base +{ +} diff --git a/tests/extension/ext/foo/acp/a_info.php b/tests/extension/ext/foo/acp/a_info.php new file mode 100644 index 0000000000..3e9bbffaca --- /dev/null +++ b/tests/extension/ext/foo/acp/a_info.php @@ -0,0 +1,16 @@ +<?php + +class phpbb_ext_foo_acp_a_info +{ + public function module() + { + return array( + 'filename' => 'phpbb_ext_foo_acp_a_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('ACP_MODS')), + ), + ); + } +} diff --git a/tests/extension/ext/foo/acp/a_module.php b/tests/extension/ext/foo/acp/a_module.php new file mode 100644 index 0000000000..093b4b1ad7 --- /dev/null +++ b/tests/extension/ext/foo/acp/a_module.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_foo_acp_a_module +{ +} diff --git a/tests/extension/ext/foo/acp/fail_info.php b/tests/extension/ext/foo/acp/fail_info.php new file mode 100644 index 0000000000..99aa09551e --- /dev/null +++ b/tests/extension/ext/foo/acp/fail_info.php @@ -0,0 +1,19 @@ +<?php +/* +* Due to the mismatch between the class name and the file name, this module +* file shouldn't be found by the extension finder +*/ +class phpbb_ext_foo_acp_foo_info +{ + public function module() + { + return array( + 'filename' => 'phpbb_ext_foo_acp_fail_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('ACP_MODS')), + ), + ); + } +} diff --git a/tests/extension/ext/foo/acp/fail_module.php b/tests/extension/ext/foo/acp/fail_module.php new file mode 100644 index 0000000000..a200d92d2f --- /dev/null +++ b/tests/extension/ext/foo/acp/fail_module.php @@ -0,0 +1,8 @@ +<?php +/* +* Due to the mismatch between the class name and the file name of the module +* info file, this module's info file shouldn't be found +*/ +class phpbb_ext_foo_acp_fail_module +{ +} diff --git a/tests/extension/ext/foo/mcp/a_info.php b/tests/extension/ext/foo/mcp/a_info.php new file mode 100644 index 0000000000..84a36b9134 --- /dev/null +++ b/tests/extension/ext/foo/mcp/a_info.php @@ -0,0 +1,16 @@ +<?php + +class phpbb_ext_foo_mcp_a_info +{ + public function module() + { + return array( + 'filename' => 'phpbb_ext_foo_mcp_a_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('MCP_MAIN')), + ), + ); + } +} diff --git a/tests/extension/ext/foo/mcp/a_module.php b/tests/extension/ext/foo/mcp/a_module.php new file mode 100644 index 0000000000..59d9f8cc6f --- /dev/null +++ b/tests/extension/ext/foo/mcp/a_module.php @@ -0,0 +1,5 @@ +<?php + +class phpbb_ext_foo_mcp_a_module +{ +} diff --git a/tests/extension/finder_test.php b/tests/extension/finder_test.php index c96b11a73c..6f3cebbd7c 100644 --- a/tests/extension/finder_test.php +++ b/tests/extension/finder_test.php @@ -6,6 +6,7 @@ * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; class phpbb_extension_finder_test extends phpbb_test_case { @@ -66,7 +67,7 @@ class phpbb_extension_finder_test extends phpbb_test_case public function test_prefix_get_directories() { $dirs = $this->finder - ->prefix('t') + ->prefix('ty') ->get_directories(); sort($dirs); @@ -142,13 +143,45 @@ class phpbb_extension_finder_test extends phpbb_test_case ); } + public function test_uncleansub_directory_get_classes() + { + $classes = $this->finder + ->directory('/sub/../sub/type') + ->get_classes(); + + sort($classes); + $this->assertEquals( + array( + 'phpbb_ext_foo_sub_type_alternative', + ), + $classes + ); + } + + public function test_find_from_extension() + { + $files = $this->finder + ->extension_directory('/type') + ->find_from_extension('foo', dirname(__FILE__) . '/ext/foo/'); + $classes = $this->finder->get_classes_from_files($files); + + sort($classes); + $this->assertEquals( + array( + 'phpbb_ext_foo_type_alternative', + 'phpbb_ext_foo_type_dummy_empty', + ), + $classes + ); + } + /** * These do not work because of changes with PHPBB3-11386 * They do not seem neccessary to me, so I am commenting them out for now 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'); + $finder = new phpbb_extension_finder($this->extension_manager, new phpbb_filesystem(), dirname(__FILE__) . '/', $cache, 'php', '_custom_cache_name'); $files = $finder->suffix('_class.php')->get_files(); $expected_files = array( @@ -188,6 +221,7 @@ class phpbb_extension_finder_test extends phpbb_test_case $finder = new phpbb_extension_finder( $this->extension_manager, + new phpbb_filesystem(), dirname(__FILE__) . '/', new phpbb_mock_cache(array( '_ext_finder' => array( diff --git a/tests/extension/includes/acp/acp_foobar.php b/tests/extension/includes/acp/acp_foobar.php new file mode 100644 index 0000000000..c256a432e2 --- /dev/null +++ b/tests/extension/includes/acp/acp_foobar.php @@ -0,0 +1,28 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* @package acp +*/ +class acp_foobar +{ + var $u_action; + + function main($id, $mode) + { + } +} diff --git a/tests/extension/includes/acp/info/acp_foobar.php b/tests/extension/includes/acp/info/acp_foobar.php new file mode 100644 index 0000000000..b89cfb9574 --- /dev/null +++ b/tests/extension/includes/acp/info/acp_foobar.php @@ -0,0 +1,26 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @package module_install +*/ +class acp_foobar_info +{ + function module() + { + return array( + 'filename' => 'acp_foobar', + 'title' => 'ACP Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'test' => array('title' => 'Test', 'auth' => '', 'cat' => array('ACP_GENERAL')), + ), + ); + } +} diff --git a/tests/extension/manager_test.php b/tests/extension/manager_test.php index 1f311116f4..a23e5a18d9 100644 --- a/tests/extension/manager_test.php +++ b/tests/extension/manager_test.php @@ -30,7 +30,7 @@ class phpbb_extension_manager_test extends phpbb_database_test_case public function test_available() { - $this->assertEquals(array('bar', 'foo', 'vendor/moo'), array_keys($this->extension_manager->all_available())); + $this->assertEquals(array('bar', 'barfoo', 'foo', 'vendor/moo'), array_keys($this->extension_manager->all_available())); } public function test_enabled() @@ -107,14 +107,17 @@ class phpbb_extension_manager_test extends phpbb_database_test_case $table_prefix, array() ); + $container = new phpbb_mock_container_builder(); + $container->set('migrator', $migrator); + return new phpbb_extension_manager( - new phpbb_mock_container_builder(), + $container, $db, $config, - $migrator, + new phpbb_filesystem(), 'phpbb_ext', dirname(__FILE__) . '/', - '.' . $php_ext, + $php_ext, ($with_cache) ? new phpbb_mock_cache() : null ); } diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index 081a32e277..d410333f09 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -7,7 +7,9 @@ * */ -class metadata_manager_test extends phpbb_database_test_case +require_once dirname(__FILE__) . '/../../phpBB/includes/db/db_tools.php'; + +class phpbb_extension_metadata_manager_test extends phpbb_database_test_case { protected $class_loader; protected $extension_manager; @@ -36,7 +38,7 @@ class metadata_manager_test extends phpbb_database_test_case $this->db = $this->new_dbal(); $this->db_tools = new phpbb_db_tools($this->db); $this->phpbb_root_path = dirname(__FILE__) . '/'; - $this->phpEx = '.php'; + $this->phpEx = 'php'; $this->user = new phpbb_user(); $this->table_prefix = 'phpbb_'; @@ -59,11 +61,14 @@ class metadata_manager_test extends phpbb_database_test_case $this->table_prefix, array() ); + $container = new phpbb_mock_container_builder(); + $container->set('migrator', $migrator); + $this->extension_manager = new phpbb_extension_manager( - new phpbb_mock_container_builder(), + $container, $this->db, $this->config, - $this->migrator, + new phpbb_filesystem(), 'phpbb_ext', $this->phpbb_root_path, $this->phpEx, @@ -415,31 +420,16 @@ class metadata_manager_test extends phpbb_database_test_case * Get an instance of the metadata manager * * @param string $ext_name - * @return phpbb_extension_metadata_manager_test + * @return phpbb_mock_metadata_manager */ private function get_metadata_manager($ext_name) { - return new phpbb_extension_metadata_manager_test( + return new phpbb_mock_metadata_manager( $ext_name, - $this->db, + $this->config, $this->extension_manager, - $this->phpbb_root_path, - $this->phpEx, $this->template, - $this->config + $this->phpbb_root_path ); } } - -class phpbb_extension_metadata_manager_test extends phpbb_extension_metadata_manager -{ - public function set_metadata($metadata) - { - $this->metadata = $metadata; - } - - public function merge_metadata($metadata) - { - $this->metadata = array_merge($this->metadata, $metadata); - } -}
\ No newline at end of file diff --git a/tests/extension/modules_test.php b/tests/extension/modules_test.php new file mode 100644 index 0000000000..fe71747c5d --- /dev/null +++ b/tests/extension/modules_test.php @@ -0,0 +1,192 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/ext/foo/acp/a_info.php'; +require_once dirname(__FILE__) . '/ext/foo/mcp/a_info.php'; +require_once dirname(__FILE__) . '/ext/foo/acp/fail_info.php'; +require_once dirname(__FILE__) . '/ext/barfoo/acp/a_info.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/acp/acp_modules.php'; + +class phpbb_extension_modules_test extends phpbb_test_case +{ + protected $extension_manager; + protected $finder; + + public function setUp() + { + global $phpbb_extension_manager; + + $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/', + ), + )); + $phpbb_extension_manager = $this->extension_manager; + + $this->acp_modules = new acp_modules(); + } + + public function test_get_module_infos() + { + global $phpbb_root_path; + + // Correctly set the root path for this test to this directory, so the classes can be found + $phpbb_root_path = dirname(__FILE__) . '/'; + + // Find acp module info files + $this->acp_modules->module_class = 'acp'; + $acp_modules = $this->acp_modules->get_module_infos(); + $this->assertEquals(array( + 'phpbb_ext_foo_acp_a_module' => array( + 'filename' => 'phpbb_ext_foo_acp_a_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('ACP_MODS')), + ), + ), + 'acp_foobar' => array( + 'filename' => 'acp_foobar', + 'title' => 'ACP Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'test' => array('title' => 'Test', 'auth' => '', 'cat' => array('ACP_GENERAL')), + ), + ), + ), $acp_modules); + + // Find mcp module info files + $this->acp_modules->module_class = 'mcp'; + $acp_modules = $this->acp_modules->get_module_infos(); + $this->assertEquals(array( + 'phpbb_ext_foo_mcp_a_module' => array( + 'filename' => 'phpbb_ext_foo_mcp_a_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('MCP_MAIN')), + ), + ), + ), $acp_modules); + + // Find a specific module info file (mcp_a_module) + $this->acp_modules->module_class = 'mcp'; + $acp_modules = $this->acp_modules->get_module_infos('mcp_a_module'); + $this->assertEquals(array( + 'phpbb_ext_foo_mcp_a_module' => array( + 'filename' => 'phpbb_ext_foo_mcp_a_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('MCP_MAIN')), + ), + ), + ), $acp_modules); + + // Find a specific module info file (mcp_a_module) with passing the module_class + $this->acp_modules->module_class = ''; + $acp_modules = $this->acp_modules->get_module_infos('mcp_a_module', 'mcp'); + $this->assertEquals(array( + 'phpbb_ext_foo_mcp_a_module' => array( + 'filename' => 'phpbb_ext_foo_mcp_a_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('MCP_MAIN')), + ), + ), + ), $acp_modules); + + // The mcp module info file we're looking for shouldn't exist + $this->acp_modules->module_class = 'mcp'; + $acp_modules = $this->acp_modules->get_module_infos('mcp_a_fail'); + $this->assertEquals(array(), $acp_modules); + + // As there are no ucp modules we shouldn't find any + $this->acp_modules->module_class = 'ucp'; + $acp_modules = $this->acp_modules->get_module_infos(); + $this->assertEquals(array(), $acp_modules); + + // Get module info of specified extension module + $this->acp_modules->module_class = 'acp'; + $acp_modules = $this->acp_modules->get_module_infos('phpbb_ext_foo_acp_a_module'); + $this->assertEquals(array( + 'phpbb_ext_foo_acp_a_module' => array ( + 'filename' => 'phpbb_ext_foo_acp_a_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array ( + 'config' => array ('title' => 'Config', 'auth' => '', 'cat' => array ('ACP_MODS')), + ), + ), + ), $acp_modules); + + // No specific module and module class set to an incorrect name + $acp_modules = $this->acp_modules->get_module_infos('', 'wcp', true); + $this->assertEquals(array(), $acp_modules); + + // No specific module, no module_class set in the function parameter, and an incorrect module class + $this->acp_modules->module_class = 'wcp'; + $acp_modules = $this->acp_modules->get_module_infos(); + $this->assertEquals(array(), $acp_modules); + + // No specific module, module class set to false (will default to the above acp) + // Setting $use_all_available will cause get_module_infos() to also load not enabled extensions (barfoo) + $this->acp_modules->module_class = 'acp'; + $acp_modules = $this->acp_modules->get_module_infos('', false, true); + $this->assertEquals(array( + 'phpbb_ext_foo_acp_a_module' => array( + 'filename' => 'phpbb_ext_foo_acp_a_module', + 'title' => 'Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('ACP_MODS')), + ), + ), + 'acp_foobar' => array( + 'filename' => 'acp_foobar', + 'title' => 'ACP Foobar', + 'version' => '3.1.0-dev', + 'modes' => array( + 'test' => array('title' => 'Test', 'auth' => '', 'cat' => array('ACP_GENERAL')), + ), + ), + 'phpbb_ext_barfoo_acp_a_module' => array( + 'filename' => 'phpbb_ext_barfoo_acp_a_module', + 'title' => 'Barfoo', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('ACP_MODS')), + ), + ) + ), $acp_modules); + + // Specific module set to disabled extension + $acp_modules = $this->acp_modules->get_module_infos('phpbb_ext_barfoo_acp_a_module', 'acp', true); + $this->assertEquals(array( + 'phpbb_ext_barfoo_acp_a_module' => array( + 'filename' => 'phpbb_ext_barfoo_acp_a_module', + 'title' => 'Barfoo', + 'version' => '3.1.0-dev', + 'modes' => array( + 'config' => array('title' => 'Config', 'auth' => '', 'cat' => array('ACP_MODS')), + ), + ) + ), $acp_modules); + } +} diff --git a/tests/extension/style_path_provider_test.php b/tests/extension/style_path_provider_test.php new file mode 100644 index 0000000000..e1021c20ac --- /dev/null +++ b/tests/extension/style_path_provider_test.php @@ -0,0 +1,50 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_extension_style_path_provider_test extends phpbb_test_case +{ + protected $relative_root_path; + protected $root_path; + + public function setUp() + { + $this->relative_root_path = './'; + $this->root_path = dirname(__FILE__) . '/'; + } + + public function test_find() + { + $phpbb_style_path_provider = new phpbb_style_path_provider(); + $phpbb_style_path_provider->set_styles(array($this->relative_root_path . 'styles/prosilver')); + $phpbb_style_extension_path_provider = new phpbb_style_extension_path_provider(new phpbb_mock_extension_manager( + $this->root_path, + 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/', + ), + )), $phpbb_style_path_provider, $this->relative_root_path); + + $this->assertEquals(array( + 'style' => array( + $this->relative_root_path . 'styles/prosilver', + ), + 'bar' => array( + $this->root_path . 'ext/bar/styles/prosilver', + ), + ), $phpbb_style_extension_path_provider->find()); + } +} diff --git a/tests/extension/subdir/style_path_provider_test.php b/tests/extension/subdir/style_path_provider_test.php new file mode 100644 index 0000000000..1b5ce62e5f --- /dev/null +++ b/tests/extension/subdir/style_path_provider_test.php @@ -0,0 +1,18 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ +require_once dirname(__FILE__) . '/../style_path_provider_test.php'; + +class phpbb_extension_subdir_style_path_provider_test extends phpbb_extension_style_path_provider_test +{ + public function setUp() + { + $this->relative_root_path = '../'; + $this->root_path = dirname(__FILE__) . '/../'; + } +} diff --git a/tests/functions/clean_path_test.php b/tests/filesystem/clean_path_test.php index bcbe9838d9..50951fc88c 100644 --- a/tests/functions/clean_path_test.php +++ b/tests/filesystem/clean_path_test.php @@ -7,11 +7,17 @@ * */ -require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; - -class phpbb_clean_path_test extends phpbb_test_case +class phpbb_filesystem_clean_path_test extends phpbb_test_case { - public function clean_path_test_data() + protected $filesystem; + + public function setUp() + { + parent::setUp(); + $this->filesystem = new phpbb_filesystem(); + } + + public function clean_path_data() { return array( array('foo', 'foo'), @@ -33,12 +39,10 @@ class phpbb_clean_path_test extends phpbb_test_case } /** - * @dataProvider clean_path_test_data + * @dataProvider clean_path_data */ public function test_clean_path($input, $expected) { - $output = phpbb_clean_path($input); - - $this->assertEquals($expected, $output); + $this->assertEquals($expected, $this->filesystem->clean_path($input)); } } diff --git a/tests/functional/acp_groups_test.php b/tests/functional/acp_groups_test.php new file mode 100644 index 0000000000..cdf8bf5117 --- /dev/null +++ b/tests/functional/acp_groups_test.php @@ -0,0 +1,120 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/common_groups_test.php'; + +/** +* @group functional +*/ +class phpbb_functional_acp_groups_test extends phpbb_functional_common_groups_test +{ + protected $form_data; + + protected function get_url() + { + return 'adm/index.php?i=groups&mode=manage&action=edit'; + } + + public function acp_group_test_data() + { + return array( + 'both_yes' => array( + 5, + true, + true, + ), + 'legend_no_teampage' => array( + 5, + true, + false, + ), + 'no_legend_teampage' => array( + 5, + false, + true, + ), + 'both_no' => array( + 5, + false, + false, + ), + 'no_change' => array( + 5, + NULL, + NULL, + ), + 'back_to_default' => array( + 5, + true, + true, + ), + // Remove and add moderators back in order to reset + // group order to default one + 'mods_both_no' => array( + 4, + false, + false, + ), + 'mods_back_to_default' => array( + 4, + true, + true, + ), + ); + } + + /** + * @dataProvider acp_group_test_data + */ + public function test_acp_groups_teampage($group_id, $tick_legend, $tick_teampage) + { + $this->group_manage_login(); + + // Manage Administrators group + $form = $this->get_group_manage_form($group_id); + $this->form_data[0] = $form->getValues(); + + if (isset($tick_legend) && isset($tick_teampage)) + { + if ($tick_legend) + { + $form['group_legend']->tick(); + } + else + { + $form['group_legend']->untick(); + } + + if ($tick_teampage) + { + $form['group_teampage']->tick(); + } + else + { + $form['group_teampage']->untick(); + } + } + $crawler = self::submit($form); + $this->assertContains($this->lang('GROUP_UPDATED'), $crawler->text()); + + $form = $this->get_group_manage_form($group_id); + if (!isset($tick_legend) && !isset($tick_teampage)) + { + $this->form_data[1] = $form->getValues(); + unset($this->form_data[0]['creation_time'], $this->form_data[0]['form_token'], $this->form_data[1]['creation_time'], $this->form_data[1]['form_token']); + $this->assertEquals($this->form_data[0], $this->form_data[1]); + } + else + { + $this->form_data = $form->getValues(); + $this->assertEquals($tick_legend, $this->form_data['group_legend']); + $this->assertEquals($tick_teampage, $this->form_data['group_teampage']); + } + } +} diff --git a/tests/functional/acp_permissions_test.php b/tests/functional/acp_permissions_test.php index f7fd44fc89..a3d272906f 100644 --- a/tests/functional/acp_permissions_test.php +++ b/tests/functional/acp_permissions_test.php @@ -25,25 +25,22 @@ class phpbb_functional_acp_permissions_test extends phpbb_functional_test_case { // Permissions tab // XXX hardcoded id - $crawler = $this->request('GET', 'adm/index.php?i=16&sid=' . $this->sid); - $this->assert_response_success(); + $crawler = self::request('GET', 'adm/index.php?i=16&sid=' . $this->sid); // these language strings are html - $this->assertContains($this->lang('ACP_PERMISSIONS_EXPLAIN'), $this->client->getResponse()->getContent()); + $this->assertContains($this->lang('ACP_PERMISSIONS_EXPLAIN'), $this->get_content()); } public function test_select_user() { // User permissions - $crawler = $this->request('GET', 'adm/index.php?i=acp_permissions&icat=16&mode=setting_user_global&sid=' . $this->sid); - $this->assert_response_success(); - $this->assertContains($this->lang('ACP_USERS_PERMISSIONS_EXPLAIN'), $this->client->getResponse()->getContent()); + $crawler = self::request('GET', 'adm/index.php?i=acp_permissions&icat=16&mode=setting_user_global&sid=' . $this->sid); + $this->assertContains($this->lang('ACP_USERS_PERMISSIONS_EXPLAIN'), $this->get_content()); // Select admin $form = $crawler->selectButton($this->lang('SUBMIT'))->form(); $data = array('username[0]' => 'admin'); $form->setValues($data); - $crawler = $this->client->submit($form); - $this->assert_response_success(); + $crawler = self::submit($form); $this->assertContains($this->lang('ACL_SET'), $crawler->filter('h1')->eq(1)->text()); } @@ -91,8 +88,7 @@ class phpbb_functional_acp_permissions_test extends phpbb_functional_test_case public function test_change_permission($description, $permission_type, $permission, $mode, $object_name, $object_id) { // Get the form - $crawler = $this->request('GET', "adm/index.php?i=acp_permissions&icat=16&mode=$mode&${object_name}[0]=$object_id&type=$permission_type&sid=" . $this->sid); - $this->assert_response_success(); + $crawler = self::request('GET', "adm/index.php?i=acp_permissions&icat=16&mode=$mode&${object_name}[0]=$object_id&type=$permission_type&sid=" . $this->sid); $this->assertContains($this->lang('ACL_SET'), $crawler->filter('h1')->eq(1)->text()); // XXX globals for phpbb_auth, refactor it later @@ -114,8 +110,7 @@ class phpbb_functional_acp_permissions_test extends phpbb_functional_test_case // set to never $data = array("setting[$object_id][0][$permission]" => '0'); $form->setValues($data); - $crawler = $this->client->submit($form); - $this->assert_response_success(); + $crawler = self::submit($form); $this->assertContains($this->lang('AUTH_UPDATED'), $crawler->text()); // check acl again diff --git a/tests/functional/auth_test.php b/tests/functional/auth_test.php index f92a4a2210..ff4d3ced5c 100644 --- a/tests/functional/auth_test.php +++ b/tests/functional/auth_test.php @@ -17,8 +17,7 @@ class phpbb_functional_auth_test extends phpbb_functional_test_case $this->login(); // check for logout link - $crawler = $this->request('GET', 'index.php'); - $this->assert_response_success(); + $crawler = self::request('GET', 'index.php'); $this->assertContains($this->lang('LOGOUT_USER', 'admin'), $crawler->filter('.navbar')->text()); } @@ -26,8 +25,7 @@ class phpbb_functional_auth_test extends phpbb_functional_test_case { $this->create_user('anothertestuser'); $this->login('anothertestuser'); - $crawler = $this->request('GET', 'index.php'); - $this->assert_response_success(); + $crawler = self::request('GET', 'index.php'); $this->assertContains('anothertestuser', $crawler->filter('.icon-logout')->text()); } @@ -40,13 +38,11 @@ class phpbb_functional_auth_test extends phpbb_functional_test_case $this->add_lang('ucp'); // logout - $crawler = $this->request('GET', 'ucp.php?sid=' . $this->sid . '&mode=logout'); - $this->assert_response_success(); + $crawler = self::request('GET', 'ucp.php?sid=' . $this->sid . '&mode=logout'); $this->assertContains($this->lang('LOGOUT_REDIRECT'), $crawler->filter('#message')->text()); // look for a register link, which should be visible only when logged out - $crawler = $this->request('GET', 'index.php'); - $this->assert_response_success(); + $crawler = self::request('GET', 'index.php'); $this->assertContains($this->lang('REGISTER'), $crawler->filter('.navbar')->text()); } @@ -56,8 +52,7 @@ class phpbb_functional_auth_test extends phpbb_functional_test_case $this->admin_login(); // check that we are logged in - $crawler = $this->request('GET', 'adm/index.php?sid=' . $this->sid); - $this->assert_response_success(); + $crawler = self::request('GET', 'adm/index.php?sid=' . $this->sid); $this->assertContains($this->lang('ADMIN_PANEL'), $crawler->filter('h1')->text()); } } diff --git a/tests/functional/browse_test.php b/tests/functional/browse_test.php index b5748059c6..18a2ad9464 100644 --- a/tests/functional/browse_test.php +++ b/tests/functional/browse_test.php @@ -14,22 +14,19 @@ class phpbb_functional_browse_test extends phpbb_functional_test_case { public function test_index() { - $crawler = $this->request('GET', 'index.php'); - $this->assert_response_success(); + $crawler = self::request('GET', 'index.php'); $this->assertGreaterThan(0, $crawler->filter('.topiclist')->count()); } public function test_viewforum() { - $crawler = $this->request('GET', 'viewforum.php?f=2'); - $this->assert_response_success(); + $crawler = self::request('GET', 'viewforum.php?f=2'); $this->assertGreaterThan(0, $crawler->filter('.topiclist')->count()); } public function test_viewtopic() { - $crawler = $this->request('GET', 'viewtopic.php?t=1'); - $this->assert_response_success(); + $crawler = self::request('GET', 'viewtopic.php?t=1'); $this->assertGreaterThan(0, $crawler->filter('.postbody')->count()); } } diff --git a/tests/functional/common_groups_test.php b/tests/functional/common_groups_test.php new file mode 100644 index 0000000000..8c014aebed --- /dev/null +++ b/tests/functional/common_groups_test.php @@ -0,0 +1,63 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @group functional +*/ +abstract class phpbb_functional_common_groups_test extends phpbb_functional_test_case +{ + abstract protected function get_url(); + + /** + * Get group_manage form + * @param int $group_id ID of the group that should be managed + */ + protected function get_group_manage_form($group_id = 5) + { + // Manage Administrators group + $crawler = self::request('GET', $this->get_url() . "&g=$group_id&sid=" . $this->sid); + $form = $crawler->selectButton($this->lang('SUBMIT'))->form(); + return $form; + } + + /** + * Execute login calls and add_lang() calls for tests + */ + protected function group_manage_login() + { + $this->login(); + $this->admin_login(); + $this->add_lang(array('ucp', 'acp/groups')); + } + + public function groups_manage_test_data() + { + return array( + array('', 'GROUP_UPDATED'), + array('aa0000', 'GROUP_UPDATED'), + + array('AAG000','WRONG_DATA_COLOUR'), + array('#AA0000', 'WRONG_DATA_COLOUR'), + ); + } + + /** + * @dataProvider groups_manage_test_data + */ + public function test_groups_manage($input, $expected) + { + $this->group_manage_login(); + + // Manage Administrators group + $form = $this->get_group_manage_form(); + $form['group_colour']->setValue($input); + $crawler = self::submit($form); + $this->assertContains($this->lang($expected), $crawler->text()); + } +} diff --git a/tests/extension/acp.php b/tests/functional/extension_acp_test.php index 790df77c0d..8614c0c963 100644 --- a/tests/extension/acp.php +++ b/tests/functional/extension_acp_test.php @@ -7,31 +7,30 @@ * */ -class acp_test extends phpbb_functional_test_case +/** +* @group functional +*/ +class phpbb_functional_extension_acp_test extends phpbb_functional_test_case { - static private $copied_files = array(); static private $helper; - /** - * This should only be called once before the tests are run. - * This is used to copy the extensions to the phpBB install - */ + static protected $fixtures = array( + './', + ); + static public function setUpBeforeClass() { - global $phpbb_root_path; - parent::setUpBeforeClass(); self::$helper = new phpbb_test_case_helpers(self); + self::$helper->copy_ext_fixtures(dirname(__FILE__) . '/../extension/ext/', self::$fixtures); + } - // First, move any extensions setup on the board to a temp directory - self::$copied_files = self::$helper->copy_dir($phpbb_root_path . 'ext/', $phpbb_root_path . 'store/temp_ext/'); - - // Then empty the ext/ directory on the board (for accurate test cases) - self::$helper->empty_dir($phpbb_root_path . 'ext/'); + static public function tearDownAfterClass() + { + parent::tearDownAfterClass(); - // Copy our ext/ files from the test case to the board - self::$copied_files = array_merge(self::$copied_files, self::$helper->copy_dir(dirname(__FILE__) . '/ext/', $phpbb_root_path . 'ext/')); + self::$helper->restore_original_ext_dir(); } public function setUp() @@ -76,29 +75,12 @@ class acp_test extends phpbb_functional_test_case $this->add_lang('acp/extensions'); } - /** - * This should only be called once after the tests are run. - * This is used to remove the files copied to the phpBB install - */ - static public function tearDownAfterClass() - { - global $phpbb_root_path; - - // Copy back the board installed extensions from the temp directory - self::$helper->copy_dir($phpbb_root_path . 'store/temp_ext/', $phpbb_root_path . 'ext/'); - - self::$copied_files[] = $phpbb_root_path . 'store/temp_ext/'; - - // Remove all of the files we copied around (from board ext -> temp_ext, from test ext -> board ext) - self::$helper->remove_files(self::$copied_files); - } - public function test_list() { - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&sid=' . $this->sid); $this->assertCount(1, $crawler->filter('.ext_enabled')); - $this->assertCount(4, $crawler->filter('.ext_disabled')); + $this->assertCount(5, $crawler->filter('.ext_disabled')); $this->assertContains('phpBB Foo Extension', $crawler->filter('.ext_enabled')->eq(0)->text()); $this->assertContainsLang('PURGE', $crawler->filter('.ext_enabled')->eq(0)->text()); @@ -117,7 +99,7 @@ class acp_test extends phpbb_functional_test_case public function test_details() { - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=details&ext_name=foo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=details&ext_name=foo&sid=' . $this->sid); $validation = array( 'DISPLAY_NAME' => 'phpBB Foo Extension', @@ -160,46 +142,46 @@ class acp_test extends phpbb_functional_test_case public function test_enable_pre() { // Foo is already enabled (redirect to list) - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable_pre&ext_name=foo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable_pre&ext_name=foo&sid=' . $this->sid); $this->assertContainsLang('EXTENSION_NAME', $crawler->filter('html')->text()); $this->assertContainsLang('EXTENSION_OPTIONS', $crawler->filter('html')->text()); $this->assertContainsLang('EXTENSION_ACTIONS', $crawler->filter('html')->text()); - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); $this->assertContainsLang('ENABLE_CONFIRM', $crawler->filter('html')->text()); } public function test_disable_pre() { // Moo is not enabled (redirect to list) - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable_pre&ext_name=vendor%2Fmoo&sid=' . $this->sid); $this->assertContainsLang('EXTENSION_NAME', $crawler->filter('html')->text()); $this->assertContainsLang('EXTENSION_OPTIONS', $crawler->filter('html')->text()); $this->assertContainsLang('EXTENSION_ACTIONS', $crawler->filter('html')->text()); - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable_pre&ext_name=foo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable_pre&ext_name=foo&sid=' . $this->sid); $this->assertContainsLang('DISABLE_CONFIRM', $crawler->filter('html')->text()); } public function test_purge_pre() { // test2 is not available (error) - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge_pre&ext_name=test2&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge_pre&ext_name=test2&sid=' . $this->sid); $this->assertContains('The required file does not exist', $crawler->filter('html')->text()); - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge_pre&ext_name=foo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge_pre&ext_name=foo&sid=' . $this->sid); $this->assertContainsLang('PURGE_CONFIRM', $crawler->filter('html')->text()); } public function test_actions() { - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=enable&ext_name=vendor%2Fmoo&sid=' . $this->sid); $this->assertContainsLang('ENABLE_SUCCESS', $crawler->filter('html')->text()); - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=disable&ext_name=vendor%2Fmoo&sid=' . $this->sid); $this->assertContainsLang('DISABLE_SUCCESS', $crawler->filter('html')->text()); - $crawler = $this->request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge&ext_name=vendor%2Fmoo&sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=purge&ext_name=vendor%2Fmoo&sid=' . $this->sid); $this->assertContainsLang('PURGE_SUCCESS', $crawler->filter('html')->text()); } }
\ No newline at end of file diff --git a/tests/functional/extension_controller_test.php b/tests/functional/extension_controller_test.php index f28b321942..9ddf1e3e5c 100644 --- a/tests/functional/extension_controller_test.php +++ b/tests/functional/extension_controller_test.php @@ -6,6 +6,7 @@ * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ +require_once dirname(__FILE__) . '/../../phpBB/includes/db/db_tools.php'; /** * @group functional @@ -14,65 +15,27 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c { protected $phpbb_extension_manager; + static private $helper; + static protected $fixtures = array( - 'foo/bar/config/routing.yml', - 'foo/bar/config/services.yml', - 'foo/bar/controller/controller.php', - 'foo/bar/styles/prosilver/template/foo_bar_body.html', + 'foo/bar/config/', + 'foo/bar/controller/', + 'foo/bar/styles/prosilver/template/', ); - /** - * 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(); - $directories = array( - $phpbb_root_path . 'ext/foo/bar/', - $phpbb_root_path . 'ext/foo/bar/config/', - $phpbb_root_path . 'ext/foo/bar/controller/', - $phpbb_root_path . 'ext/foo/bar/styles/prosilver/template', - ); - - foreach ($directories as $dir) - { - if (!is_dir($dir)) - { - mkdir($dir, 0777, true); - } - } - - foreach (self::$fixtures as $fixture) - { - copy( - "tests/functional/fixtures/ext/$fixture", - "{$phpbb_root_path}ext/$fixture"); - } + self::$helper = new phpbb_test_case_helpers(self); + self::$helper->copy_ext_fixtures(dirname(__FILE__) . '/fixtures/ext/', self::$fixtures); } - /** - * This should only be called once after the tests are run. - * This is used to remove the fixtures from the phpBB install - */ static public function tearDownAfterClass() { - global $phpbb_root_path; - - foreach (self::$fixtures as $fixture) - { - unlink("{$phpbb_root_path}ext/$fixture"); - } + parent::tearDownAfterClass(); - rmdir("{$phpbb_root_path}ext/foo/bar/config"); - rmdir("{$phpbb_root_path}ext/foo/bar/controller"); - rmdir("{$phpbb_root_path}ext/foo/bar/styles/prosilver/template"); - rmdir("{$phpbb_root_path}ext/foo/bar/styles/prosilver"); - rmdir("{$phpbb_root_path}ext/foo/bar/styles"); - rmdir("{$phpbb_root_path}ext/foo/bar"); - rmdir("{$phpbb_root_path}ext/foo"); + self::$helper->restore_original_ext_dir(); } public function setUp() @@ -90,8 +53,8 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c public function test_foo_bar() { $this->phpbb_extension_manager->enable('foo/bar'); - $crawler = $this->request('GET', 'app.php?controller=foo/bar'); - $this->assert_response_success(); + $crawler = self::request('GET', 'app.php?controller=foo/bar', array(), false); + self::assert_response_status_code(); $this->assertContains("foo/bar controller handle() method", $crawler->filter('body')->text()); $this->phpbb_extension_manager->purge('foo/bar'); } @@ -102,8 +65,7 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c public function test_controller_with_template() { $this->phpbb_extension_manager->enable('foo/bar'); - $crawler = $this->request('GET', 'app.php?controller=foo/template'); - $this->assert_response_success(); + $crawler = self::request('GET', 'app.php?controller=foo/template'); $this->assertContains("I am a variable", $crawler->filter('#content')->text()); $this->phpbb_extension_manager->purge('foo/bar'); } @@ -115,8 +77,8 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c public function test_missing_argument() { $this->phpbb_extension_manager->enable('foo/bar'); - $crawler = $this->request('GET', 'app.php?controller=foo/baz'); - $this->assertEquals(500, $this->client->getResponse()->getStatus()); + $crawler = self::request('GET', 'app.php?controller=foo/baz', array(), false); + $this->assert_response_html(500); $this->assertContains('Missing value for argument #1: test in class phpbb_ext_foo_bar_controller:baz', $crawler->filter('body')->text()); $this->phpbb_extension_manager->purge('foo/bar'); } @@ -127,8 +89,8 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c public function test_exception_should_result_in_500_status_code() { $this->phpbb_extension_manager->enable('foo/bar'); - $crawler = $this->request('GET', 'app.php?controller=foo/exception'); - $this->assertEquals(500, $this->client->getResponse()->getStatus()); + $crawler = self::request('GET', 'app.php?controller=foo/exception', array(), false); + $this->assert_response_html(500); $this->assertContains('Exception thrown from foo/exception route', $crawler->filter('body')->text()); $this->phpbb_extension_manager->purge('foo/bar'); } @@ -144,8 +106,8 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c */ public function test_error_ext_disabled_or_404() { - $crawler = $this->request('GET', 'app.php?controller=does/not/exist'); - $this->assertEquals(404, $this->client->getResponse()->getStatus()); + $crawler = self::request('GET', 'app.php?controller=does/not/exist', array(), false); + $this->assert_response_html(404); $this->assertContains('No route found for "GET /does/not/exist"', $crawler->filter('body')->text()); } } diff --git a/tests/functional/extension_module_test.php b/tests/functional/extension_module_test.php new file mode 100644 index 0000000000..c573ea5410 --- /dev/null +++ b/tests/functional/extension_module_test.php @@ -0,0 +1,99 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/db/db_tools.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/acp/acp_modules.php'; + +/** +* @group functional +*/ +class phpbb_functional_extension_module_test extends phpbb_functional_test_case +{ + protected $phpbb_extension_manager; + + static private $helper; + + static protected $fixtures = array( + './', + ); + + static public function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + self::$helper = new phpbb_test_case_helpers(self); + self::$helper->copy_ext_fixtures(dirname(__FILE__) . '/fixtures/ext/', self::$fixtures); + } + + static public function tearDownAfterClass() + { + parent::tearDownAfterClass(); + + self::$helper->restore_original_ext_dir(); + } + + public function setUp() + { + global $db; + + parent::setUp(); + + $this->phpbb_extension_manager = $this->get_extension_manager(); + $this->phpbb_extension_manager->enable('foo/bar'); + + $modules = new acp_modules(); + $db = $this->get_db(); + + $sql = 'SELECT module_id + FROM ' . MODULES_TABLE . " + WHERE module_langname = 'acp' + AND module_class = 'ACP_CAT_DOT_MODS'"; + $result = $db->sql_query($sql); + $module_id = (int) $db->sql_fetchfield('module_id'); + $db->sql_freeresult($result); + + $parent_data = array( + 'module_basename' => '', + 'module_enabled' => 1, + 'module_display' => 1, + 'parent_id' => $module_id, + 'module_class' => 'acp', + 'module_langname' => 'ACP_FOOBAR_TITLE', + 'module_mode' => '', + 'module_auth' => '', + ); + $modules->update_module_data($parent_data, true); + + $module_data = array( + 'module_basename' => 'phpbb_ext_foo_bar_acp_main_module', + 'module_enabled' => 1, + 'module_display' => 1, + 'parent_id' => $parent_data['module_id'], + 'module_class' => 'acp', + 'module_langname' => 'ACP_FOOBAR_TITLE', + 'module_mode' => 'mode', + 'module_auth' => '', + ); + $modules->update_module_data($module_data, true); + + $this->purge_cache(); + } + + /** + * Check a controller for extension foo/bar. + */ + public function test_foo_bar() + { + $this->login(); + $this->admin_login(); + $crawler = self::request('GET', 'adm/index.php?i=phpbb_ext_foo_bar_acp_main_module&mode=mode&sid=' . $this->sid); + $this->assertContains("Bertie rulez!", $crawler->filter('#main')->text()); + $this->phpbb_extension_manager->purge('foo/bar'); + } +} diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php new file mode 100644 index 0000000000..6c1720735c --- /dev/null +++ b/tests/functional/extension_permission_lang_test.php @@ -0,0 +1,80 @@ +<?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_extension_permission_lang_test extends phpbb_functional_test_case +{ + protected $phpbb_extension_manager; + + static private $helper; + + static protected $fixtures = array( + 'foo/bar/language/en/', + ); + + static public function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + self::$helper = new phpbb_test_case_helpers(self); + self::$helper->copy_ext_fixtures(dirname(__FILE__) . '/fixtures/ext/', self::$fixtures); + } + + static public function tearDownAfterClass() + { + parent::tearDownAfterClass(); + + self::$helper->restore_original_ext_dir(); + } + + public function setUp() + { + parent::setUp(); + + $this->get_db(); + + $acl_ary = array( + 'auth_option' => 'u_foo', + 'is_global' => 1, + ); + + $sql = 'INSERT INTO phpbb_acl_options ' . $this->db->sql_build_array('INSERT', $acl_ary); + $this->db->sql_query($sql); + + $this->phpbb_extension_manager = $this->get_extension_manager(); + + $this->purge_cache(); + + $this->login(); + $this->admin_login(); + $this->add_lang('acp/permissions'); + } + + public function test_auto_include_permission_lang_from_extensions() + { + $this->phpbb_extension_manager->enable('foo/bar'); + + // User permissions + $crawler = self::request('GET', 'adm/index.php?i=acp_permissions&icat=16&mode=setting_user_global&sid=' . $this->sid); + + // Select admin + $form = $crawler->selectButton($this->lang('SUBMIT'))->form(); + $data = array('username[0]' => 'admin'); + $form->setValues($data); + $crawler = self::submit($form); + + // language from language/en/acp/permissions_phpbb.php + $this->assertContains('Can attach files', $crawler->filter('body')->text()); + + // language from ext/foo/bar/language/en/permissions_foo.php + $this->assertContains('Can view foo', $crawler->filter('body')->text()); + } +} diff --git a/tests/functional/fileupload_form_test.php b/tests/functional/fileupload_form_test.php index b663c89e95..998c402fa3 100644 --- a/tests/functional/fileupload_form_test.php +++ b/tests/functional/fileupload_form_test.php @@ -32,7 +32,7 @@ class phpbb_functional_fileupload_form_test extends phpbb_functional_test_case 'error' => UPLOAD_ERR_OK, ); - $crawler = $this->client->request( + $crawler = self::$client->request( 'POST', 'posting.php?mode=reply&f=2&t=1&sid=' . $this->sid, array('add_file' => $this->lang('ADD_FILE')), @@ -66,9 +66,8 @@ class phpbb_functional_fileupload_form_test extends phpbb_functional_test_case { $this->markTestIncomplete('Test fails intermittently.'); $crawler = $this->upload_file('valid.jpg', 'image/jpeg'); - $this->assert_response_success(); // ensure there was no error message rendered - $this->assertNotContains('<h2>' . $this->lang('INFORMATION') . '</h2>', $this->client->getResponse()->getContent()); + $this->assertNotContains('<h2>' . $this->lang('INFORMATION') . '</h2>', $this->get_content()); $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 index 0deb79acf6..8e361ab77b 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -58,7 +58,7 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case 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'); + $file = $upload->remote_upload(self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); $this->assertEquals(0, sizeof($file->error)); $this->assertTrue(file_exists($file->filename)); } @@ -66,7 +66,8 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case 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'); + $file = $upload->remote_upload(self::$root_url . 'styles/prosilver/theme/images/forum_read.gif'); + $this->assertEquals(1, sizeof($file->error)); $this->assertEquals('WRONG_FILESIZE', $file->error[0]); } } diff --git a/tests/functional/fixtures/ext/foo/bar/acp/main_info.php b/tests/functional/fixtures/ext/foo/bar/acp/main_info.php new file mode 100644 index 0000000000..21e38b09b5 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/acp/main_info.php @@ -0,0 +1,32 @@ +<?php + +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +class phpbb_ext_foo_bar_acp_main_info +{ + function module() + { + return array( + 'filename' => 'phpbb_ext_foo_bar_acp_main_module', + 'title' => 'ACP_FOOBAR_TITLE', + 'version' => '1.0.0', + 'modes' => array( + 'mode' => array('title' => 'ACP_FOOBAR_MODE', 'auth' => '', 'cat' => array('ACP_FOOBAR_TITLE')), + ), + ); + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/acp/main_module.php b/tests/functional/fixtures/ext/foo/bar/acp/main_module.php new file mode 100644 index 0000000000..c4ab69fb38 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/acp/main_module.php @@ -0,0 +1,28 @@ +<?php + +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +class phpbb_ext_foo_bar_acp_main_module +{ + var $u_action; + + function main($id, $mode) + { + $this->tpl_name = 'foobar'; + $this->page_title = 'Bertie'; + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/adm/style/foobar.html b/tests/functional/fixtures/ext/foo/bar/adm/style/foobar.html new file mode 100644 index 0000000000..3cb45c269c --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/adm/style/foobar.html @@ -0,0 +1,3 @@ +<!-- INCLUDE overall_header.html --> +Bertie rulez! +<!-- INCLUDE overall_footer.html --> diff --git a/tests/functional/fixtures/ext/foo/bar/composer.json b/tests/functional/fixtures/ext/foo/bar/composer.json new file mode 100644 index 0000000000..067a9d38eb --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/composer.json @@ -0,0 +1,23 @@ +{ + "name": "foo/bar", + "type": "phpbb3-extension", + "description": "Testing extensions", + "homepage": "", + "version": "1.0.0", + "time": "2013-03-21 01:01:01", + "licence": "GPL-2.0", + "authors": [{ + "name": "Joas Schilling", + "username": "nickvergessen", + "email": "nickvergessen@phpbb.com", + "homepage": "http://www.phpbb.com", + "role": "Developer" + }], + "require": { + "php": ">=5.3", + "phpbb": ">=3.1.0-dev" + }, + "extra": { + "display-name": "phpBB 3.1 Extension Testing" + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php b/tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php new file mode 100644 index 0000000000..cd4b9a32d1 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php @@ -0,0 +1,6 @@ +<?php + +// Admin Permissions +$lang = array_merge($lang, array( + 'acl_u_foo' => array('lang' => 'Can view foo', 'cat' => 'misc'), +)); diff --git a/tests/functional/forgot_password_test.php b/tests/functional/forgot_password_test.php new file mode 100644 index 0000000000..906224efbb --- /dev/null +++ b/tests/functional/forgot_password_test.php @@ -0,0 +1,44 @@ +<?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_forgot_password_test extends phpbb_functional_test_case +{ + public function test_forgot_password_enabled() + { + global $config; + $this->add_lang('ucp'); + $crawler = self::request('GET', 'ucp.php?mode=sendpassword'); + $this->assertEquals($this->lang('SEND_PASSWORD'), $crawler->filter('h2')->text()); + } + + public function test_forgot_password_disabled() + { + $this->login(); + $this->admin_login(); + $this->add_lang('ucp'); + $crawler = self::request('GET', 'adm/index.php?sid=' . $this->sid . '&i=acp_board&mode=security'); + + $form = $crawler->selectButton('Submit')->form(); + $values = $form->getValues(); + + $values["config[allow_password_reset]"] = 0; + $form->setValues($values); + $crawler = self::submit($form); + + $this->logout(); + + $crawler = self::request('GET', 'ucp.php?mode=sendpassword'); + $this->assertContains($this->lang('UCP_PASSWORD_RESET_DISABLED', '', ''), $crawler->text()); + + } + +} diff --git a/tests/functional/memberlist_test.php b/tests/functional/memberlist_test.php index 879bee2f0e..738ec4f9dd 100644 --- a/tests/functional/memberlist_test.php +++ b/tests/functional/memberlist_test.php @@ -17,18 +17,15 @@ class phpbb_functional_memberlist_test extends phpbb_functional_test_case $this->create_user('memberlist-test-user'); // logs in as admin $this->login(); - $crawler = $this->request('GET', 'memberlist.php?sid=' . $this->sid); - $this->assert_response_success(); + $crawler = self::request('GET', 'memberlist.php?sid=' . $this->sid); $this->assertContains('memberlist-test-user', $crawler->text()); // restrict by first character - $crawler = $this->request('GET', 'memberlist.php?first_char=m&sid=' . $this->sid); - $this->assert_response_success(); + $crawler = self::request('GET', 'memberlist.php?first_char=m&sid=' . $this->sid); $this->assertContains('memberlist-test-user', $crawler->text()); // make sure results for wrong character are not returned - $crawler = $this->request('GET', 'memberlist.php?first_char=a&sid=' . $this->sid); - $this->assert_response_success(); + $crawler = self::request('GET', 'memberlist.php?first_char=a&sid=' . $this->sid); $this->assertNotContains('memberlist-test-user', $crawler->text()); } @@ -36,8 +33,73 @@ class phpbb_functional_memberlist_test extends phpbb_functional_test_case { $this->login(); // XXX hardcoded user id - $crawler = $this->request('GET', 'memberlist.php?mode=viewprofile&u=2&sid=' . $this->sid); - $this->assert_response_success(); + $crawler = self::request('GET', 'memberlist.php?mode=viewprofile&u=2&sid=' . $this->sid); $this->assertContains('admin', $crawler->filter('h2')->text()); } + + protected function get_memberlist_leaders_table_crawler() + { + $crawler = self::request('GET', 'memberlist.php?mode=leaders&sid=' . $this->sid); + return $crawler->filter('.forumbg-table'); + } + + public function test_leaders() + { + $this->login(); + $this->create_user('memberlist-test-moderator'); + + $crawler = $this->get_memberlist_leaders_table_crawler(); + + // Admin in admin group, but not in moderators + $this->assertContains('admin', $crawler->eq(0)->text()); + $this->assertNotContains('admin', $crawler->eq(1)->text()); + + // memberlist-test-user in neither group + $this->assertNotContains('memberlist-test-user', $crawler->eq(0)->text()); + $this->assertNotContains('memberlist-test-user', $crawler->eq(1)->text()); + + // memberlist-test-moderator in neither group + $this->assertNotContains('memberlist-test-moderator', $crawler->eq(0)->text()); + $this->assertNotContains('memberlist-test-moderator', $crawler->eq(1)->text()); + } + + public function test_leaders_remove_users() + { + $this->login(); + + // Remove admin from admins, but is now in moderators + $this->remove_user_group('ADMINISTRATORS', array('admin')); + $crawler = $this->get_memberlist_leaders_table_crawler(); + $this->assertNotContains('admin', $crawler->eq(0)->text()); + $this->assertContains('admin', $crawler->eq(1)->text()); + + // Remove admin from moderators, should not be visible anymore + $this->remove_user_group('GLOBAL_MODERATORS', array('admin')); + $crawler = $this->get_memberlist_leaders_table_crawler(); + $this->assertNotContains('admin', $crawler->eq(0)->text()); + $this->assertNotContains('admin', $crawler->eq(1)->text()); + } + + public function test_leaders_add_users() + { + $this->login(); + + // Add memberlist-test-moderator to moderators + $this->add_user_group('GLOBAL_MODERATORS', array('memberlist-test-moderator')); + $crawler = $this->get_memberlist_leaders_table_crawler(); + $this->assertNotContains('memberlist-test-moderator', $crawler->eq(0)->text()); + $this->assertContains('memberlist-test-moderator', $crawler->eq(1)->text()); + + // Add admin to moderators, should be visible as moderator + $this->add_user_group('GLOBAL_MODERATORS', array('admin'), true); + $crawler = $this->get_memberlist_leaders_table_crawler(); + $this->assertNotContains('admin', $crawler->eq(0)->text()); + $this->assertContains('admin', $crawler->eq(1)->text()); + + // Add admin to admins as leader, should be visible as admin, not moderator + $this->add_user_group('ADMINISTRATORS', array('admin'), true, true); + $crawler = $this->get_memberlist_leaders_table_crawler(); + $this->assertContains('admin', $crawler->eq(0)->text()); + $this->assertNotContains('admin', $crawler->eq(1)->text()); + } } diff --git a/tests/functional/metadata_manager_test.php b/tests/functional/metadata_manager_test.php new file mode 100644 index 0000000000..c55e7373ea --- /dev/null +++ b/tests/functional/metadata_manager_test.php @@ -0,0 +1,85 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/db/db_tools.php'; + +/** +* @group functional +*/ +class phpbb_functional_metadata_manager_test extends phpbb_functional_test_case +{ + protected $phpbb_extension_manager; + + static private $helper; + + static protected $fixtures = array( + 'foo/bar/', + ); + + static public function setUpBeforeClass() + { + parent::setUpBeforeClass(); + + self::$helper = new phpbb_test_case_helpers(self); + self::$helper->copy_ext_fixtures(dirname(__FILE__) . '/fixtures/ext/', self::$fixtures); + } + + static public function tearDownAfterClass() + { + parent::tearDownAfterClass(); + + self::$helper->restore_original_ext_dir(); + } + + public function setUp() + { + parent::setUp(); + + $this->phpbb_extension_manager = $this->get_extension_manager(); + + $this->purge_cache(); + $this->phpbb_extension_manager->enable('foo/bar'); + + $this->login(); + $this->admin_login(); + $this->add_lang('acp/extensions'); + } + + public function test_extensions_list() + { + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&sid=' . $this->sid); + $this->assertContains($this->lang('EXTENSIONS_EXPLAIN'), $crawler->filter('#main')->text()); + $this->assertContains('phpBB 3.1 Extension Testing', $crawler->filter('#main')->text()); + $this->assertContains('Details', $crawler->filter('#main')->text()); + } + + public function test_extensions_details() + { + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=details&ext_name=foo%2Fbar&sid=' . $this->sid); + + // Test whether the details are displayed + $this->assertContains($this->lang('CLEAN_NAME'), $crawler->filter('#main')->text()); + $this->assertContains('foo/bar', $crawler->filter('#meta_name')->text()); + + $this->assertContains($this->lang('PHP_VERSION'), $crawler->filter('#main')->text()); + $this->assertContains('>=5.3', $crawler->filter('#require_php')->text()); + // Details should be html escaped + // However, text() only returns the displayed text, so HTML Special Chars are decoded. + // So we test this directly on the content of the response. + $this->assertContains('<p id="require_php">>=5.3</p>', $this->get_content()); + } + + public function test_extensions_details_notexists() + { + $crawler = self::request('GET', 'adm/index.php?i=acp_extensions&mode=main&action=details&ext_name=not%2Fexists&sid=' . $this->sid); + + // Error message because the files do not exist + $this->assertContains('The required file does not exist:', $crawler->filter('#main')->text()); + } +} diff --git a/tests/functional/notification_test.php b/tests/functional/notification_test.php new file mode 100644 index 0000000000..7f33ad1859 --- /dev/null +++ b/tests/functional/notification_test.php @@ -0,0 +1,55 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @group functional +*/ +class phpbb_functional_notification_test extends phpbb_functional_test_case +{ + static public function user_subscription_data() + { + return array( + // Rows inserted by phpBB/install/schemas/schema_data.sql + // Also see PHPBB3-11460 + array('post_notification', true), + array('topic_notification', true), + array('post_email', true), + array('topic_email', true), + + // Default behaviour for in-board notifications: + // If user did not opt-out, in-board notifications are on. + array('bookmark_notification', true), + array('quote_notification', true), + + // Default behaviour for email notifications: + // If user did not opt-in, email notifications are off. + array('bookmark_email', false), + array('quote_email', false), + ); + } + + /** + * @dataProvider user_subscription_data + */ + public function test_user_subscriptions($checkbox_name, $expected_status) + { + $this->login(); + $crawler = self::request('GET', 'ucp.php?i=ucp_notifications&mode=notification_options'); + + $cplist = $crawler->filter('.table1'); + if ($expected_status) + { + $this->assert_checkbox_is_checked($cplist, $checkbox_name); + } + else + { + $this->assert_checkbox_is_unchecked($cplist, $checkbox_name); + } + } +} diff --git a/tests/functional/paging_test.php b/tests/functional/paging_test.php new file mode 100644 index 0000000000..d5adc6ad0a --- /dev/null +++ b/tests/functional/paging_test.php @@ -0,0 +1,39 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +/** +* @group functional +*/ +class phpbb_functional_paging_test extends phpbb_functional_test_case +{ + + public function test_pagination() + { + $this->login(); + + $post = $this->create_topic(2, 'Test Topic 1', 'This is a test topic posted by the testing framework.'); + for ($post_id = 1; $post_id < 20; $post_id++) + { + $this->create_post(2, $post['topic_id'], 'Re: Test Topic 1', 'This is a test post no' . $post_id . ' posted by the testing framework.'); + } + $crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid={$this->sid}"); + $this->assertContains('post no9', $crawler->text()); + $this->assertNotContains('post no19', $crawler->text()); + + $next_link = $crawler->filter('#viewtopic > fieldset > a.arrow-right')->attr('href'); + $crawler = self::request('GET', $next_link); + $this->assertContains('post no19', $crawler->text()); + $this->assertNotContains('post no9', $crawler->text()); + + $prev_link = $crawler->filter('#viewtopic > fieldset > a.arrow-left')->attr('href'); + $crawler = self::request('GET', $prev_link); + $this->assertContains('post no9', $crawler->text()); + $this->assertNotContains('post no19', $crawler->text()); + } +} diff --git a/tests/functional/posting_test.php b/tests/functional/posting_test.php index d05207edf0..7fd1e4fdcf 100644 --- a/tests/functional/posting_test.php +++ b/tests/functional/posting_test.php @@ -19,121 +19,17 @@ class phpbb_functional_posting_test extends phpbb_functional_test_case // Test creating topic $post = $this->create_topic(2, 'Test Topic 1', 'This is a test topic posted by the testing framework.'); - $crawler = $this->request('GET', "viewtopic.php?t={$post['topic_id']}&sid={$this->sid}"); + $crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid={$this->sid}"); $this->assertContains('This is a test topic posted by the testing framework.', $crawler->filter('html')->text()); // Test creating a reply $post2 = $this->create_post(2, $post['topic_id'], 'Re: Test Topic 1', 'This is a test post posted by the testing framework.'); - $crawler = $this->request('GET', "viewtopic.php?t={$post2['topic_id']}&sid={$this->sid}"); + $crawler = self::request('GET', "viewtopic.php?t={$post2['topic_id']}&sid={$this->sid}"); $this->assertContains('This is a test post posted by the testing framework.', $crawler->filter('html')->text()); // Test quoting a message - $crawler = $this->request('GET', "posting.php?mode=quote&f=2&t={$post2['topic_id']}&p={$post2['post_id']}&sid={$this->sid}"); - $this->assert_response_success(); + $crawler = self::request('GET', "posting.php?mode=quote&f=2&t={$post2['topic_id']}&p={$post2['post_id']}&sid={$this->sid}"); $this->assertContains('This is a test post posted by the testing framework.', $crawler->filter('html')->text()); } - - /** - * Creates a topic - * - * Be sure to login before creating - * - * @param int $forum_id - * @param string $subject - * @param string $message - * @param array $additional_form_data Any additional form data to be sent in the request - * @return array post_id, topic_id - */ - public function create_topic($forum_id, $subject, $message, $additional_form_data = array()) - { - $posting_url = "posting.php?mode=post&f={$forum_id}&sid={$this->sid}"; - - $form_data = array_merge(array( - 'subject' => $subject, - 'message' => $message, - 'post' => true, - ), $additional_form_data); - - return $this->submit_post($posting_url, 'POST_TOPIC', $form_data); - } - - /** - * Creates a post - * - * Be sure to login before creating - * - * @param int $forum_id - * @param string $subject - * @param string $message - * @param array $additional_form_data Any additional form data to be sent in the request - * @return array post_id, topic_id - */ - public function create_post($forum_id, $topic_id, $subject, $message, $additional_form_data = array()) - { - $posting_url = "posting.php?mode=reply&f={$forum_id}&t={$topic_id}&sid={$this->sid}"; - - $form_data = array_merge(array( - 'subject' => $subject, - 'message' => $message, - 'post' => true, - ), $additional_form_data); - - return $this->submit_post($posting_url, 'POST_REPLY', $form_data); - } - - /** - * Helper for submitting posts - * - * @param string $posting_url - * @param string $posting_contains - * @param array $form_data - * @return array post_id, topic_id - */ - protected function submit_post($posting_url, $posting_contains, $form_data) - { - $this->add_lang('posting'); - - $crawler = $this->request('GET', $posting_url); - $this->assert_response_success(); - $this->assertContains($this->lang($posting_contains), $crawler->filter('html')->text()); - - $hidden_fields = array( - $crawler->filter('[type="hidden"]')->each(function ($node, $i) { - return array('name' => $node->getAttribute('name'), 'value' => $node->getAttribute('value')); - }), - ); - - foreach ($hidden_fields as $fields) - { - foreach($fields as $field) - { - $form_data[$field['name']] = $field['value']; - } - } - - // Bypass time restriction that said that if the lastclick time (i.e. time when the form was opened) - // is not at least 2 seconds before submission, cancel the form - $form_data['lastclick'] = 0; - - // I use a request because the form submission method does not allow you to send data that is not - // contained in one of the actual form fields that the browser sees (i.e. it ignores "hidden" inputs) - // Instead, I send it as a request with the submit button "post" set to true. - $crawler = $this->client->request('POST', $posting_url, $form_data); - $this->assert_response_success(); - $this->assertContains($this->lang('POST_STORED'), $crawler->filter('html')->text()); - - $url = $crawler->selectLink($this->lang('VIEW_MESSAGE', '', ''))->link()->getUri(); - - $matches = $topic_id = $post_id = false; - preg_match_all('#&t=([0-9]+)(&p=([0-9]+))?#', $url, $matches); - - $topic_id = (int) (isset($matches[1][0])) ? $matches[1][0] : 0; - $post_id = (int) (isset($matches[3][0])) ? $matches[3][0] : 0; - - return array( - 'topic_id' => $topic_id, - 'post_id' => $post_id, - ); - } } diff --git a/tests/functional/report_post_captcha.php b/tests/functional/report_post_captcha.php new file mode 100644 index 0000000000..af713775c5 --- /dev/null +++ b/tests/functional/report_post_captcha.php @@ -0,0 +1,61 @@ +<?php +/** + * + * @package testing + * @copyright (c) 2013 phpBB Group + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 + * + */ + +/** + * @group functional + */ +class phpbb_functional_report_post_captcha_test extends phpbb_functional_test_case +{ + public function test_user_report_post() + { + $this->login(); + $crawler = self::request('GET', 'report.php?f=2&p=1'); + $this->assertNotContains($this->lang('CONFIRM_CODE'), $crawler->filter('html')->text()); + } + + public function test_guest_report_post() + { + $crawler = self::request('GET', 'report.php?f=2&p=1'); + $this->add_lang('mcp'); + $this->assertContains($this->lang('USER_CANNOT_REPORT'), $crawler->filter('html')->text()); + + $this->set_reporting_guest(1); + $crawler = self::request('GET', 'report.php?f=2&p=1'); + $this->assertContains($this->lang('CONFIRM_CODE'), $crawler->filter('html')->text()); + $this->set_reporting_guest(-1); + } + + protected function set_reporting_guest($report_post_allowed) + { + $this->login(); + $this->admin_login(); + + $crawler = self::request('GET', 'adm/index.php?i=permissions&icat=12&mode=setting_group_local&sid=' . $this->sid); + $form = $crawler->selectButton('Submit')->form(); + $values = $form->getValues(); + $values["group_id[0]"] = 1; + $form->setValues($values); + $crawler = self::submit($form); + + $form = $crawler->selectButton('Submit')->form(); + $values = $form->getValues(); + $values["forum_id"] = 2; + $form->setValues($values); + $crawler = self::submit($form); + + $this->add_lang('acp/permissions'); + $form = $crawler->selectButton($this->lang('APPLY_ALL_PERMISSIONS'))->form(); + $values = $form->getValues(); + $values["setting[1][2][f_report]"] = $report_post_allowed; + $form->setValues($values); + $crawler = self::submit($form); + + $crawler = self::request('GET', 'ucp.php?mode=logout&sid=' . $this->sid); + } +} diff --git a/tests/functional/ucp_groups_test.php b/tests/functional/ucp_groups_test.php new file mode 100644 index 0000000000..f48c793ea1 --- /dev/null +++ b/tests/functional/ucp_groups_test.php @@ -0,0 +1,53 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/common_groups_test.php'; + +/** +* @group functional +*/ +class phpbb_functional_ucp_groups_test extends phpbb_functional_common_groups_test +{ + protected $db; + + protected function get_url() + { + return 'ucp.php?i=groups&mode=manage&action=edit'; + } + + protected function get_teampage_settings() + { + if (!isset($this->db)) + { + $this->db = $this->get_db(); + } + $sql = 'SELECT g.group_legend AS group_legend, t.teampage_position AS group_teampage + FROM ' . GROUPS_TABLE . ' g + LEFT JOIN ' . TEAMPAGE_TABLE . ' t + ON (t.group_id = g.group_id) + WHERE g.group_id = 5'; + $result = $this->db->sql_query($sql); + $group_row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + return $group_row; + } + + public function test_ucp_groups_teampage() + { + $this->group_manage_login(); + + // Test if group_legend or group_teampage are modified while + // submitting the ucp_group_manage page + $form = $this->get_group_manage_form(); + $teampage_settings = $this->get_teampage_settings(); + $crawler = self::submit($form); + $this->assertContains($this->lang('GROUP_UPDATED'), $crawler->text()); + $this->assertEquals($teampage_settings, $this->get_teampage_settings()); + } +} diff --git a/tests/functions/fixtures/language_select.xml b/tests/functions/fixtures/language_select.xml new file mode 100644 index 0000000000..d7232a3d28 --- /dev/null +++ b/tests/functions/fixtures/language_select.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_lang"> + <column>lang_id</column> + <column>lang_iso</column> + <column>lang_local_name</column> + <column>lang_english_name</column> + <row> + <value>1</value> + <value>en</value> + <value>English</value> + <value>English</value> + </row> + <row> + <value>2</value> + <value>cs</value> + <value>Čeština</value> + <value>Czech</value> + </row> + </table> +</dataset> diff --git a/tests/functions/fixtures/obtain_online.xml b/tests/functions/fixtures/obtain_online.xml new file mode 100644 index 0000000000..05bbe6a05e --- /dev/null +++ b/tests/functions/fixtures/obtain_online.xml @@ -0,0 +1,121 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_sessions"> + <column>session_id</column> + <column>session_user_id</column> + <column>session_forum_id</column> + <column>session_time</column> + <column>session_ip</column> + <column>session_viewonline</column> + </table> + <table name="phpbb_users"> + <column>user_id</column> + <column>username_clean</column> + <column>username</column> + <column>user_allow_viewonline</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>1</value> + <value>anonymous</value> + <value>anonymous</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>2</value> + <value>2</value> + <value>2</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>3</value> + <value>3</value> + <value>3</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>4</value> + <value>4</value> + <value>4</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>5</value> + <value>5</value> + <value>5</value> + <value>1</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>6</value> + <value>6</value> + <value>6</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>7</value> + <value>7</value> + <value>7</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>8</value> + <value>8</value> + <value>8</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>9</value> + <value>9</value> + <value>9</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>10</value> + <value>10</value> + <value>10</value> + <value>0</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/functions/fixtures/style_select.xml b/tests/functions/fixtures/style_select.xml new file mode 100644 index 0000000000..ca95f94461 --- /dev/null +++ b/tests/functions/fixtures/style_select.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_styles"> + <column>style_id</column> + <column>style_name</column> + <column>style_active</column> + <column>style_parent_tree</column> + <row> + <value>1</value> + <value>prosilver</value> + <value>1</value> + <value></value> + </row> + <row> + <value>2</value> + <value>subsilver2</value> + <value>1</value> + <value></value> + </row> + <row> + <value>3</value> + <value>zoo</value> + <value>0</value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/functions/fixtures/validate_email.xml b/tests/functions/fixtures/validate_email.xml new file mode 100644 index 0000000000..de7fce8a08 --- /dev/null +++ b/tests/functions/fixtures/validate_email.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_users"> + <column>user_id</column> + <column>username</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <column>user_email_hash</column> + <row> + <value>1</value> + <value>admin</value> + <value>admin</value> + <value></value> + <value></value> + <value></value> + <value></value> + <value>143317126117</value> + </row> + </table> +</dataset> diff --git a/tests/functions/fixtures/validate_username.xml b/tests/functions/fixtures/validate_username.xml new file mode 100644 index 0000000000..fbe398469c --- /dev/null +++ b/tests/functions/fixtures/validate_username.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_groups"> + <column>group_name</column> + <column>group_desc</column> + <row> + <value>foobar_group</value> + <value>test123</value> + </row> + </table> + <table name="phpbb_users"> + <column>user_id</column> + <column>username</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>1</value> + <value>admin</value> + <value>admin</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>2</value> + <value>moderator</value> + <value>moderator</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/functions/get_remote_file_test.php b/tests/functions/get_remote_file_test.php new file mode 100644 index 0000000000..4032ca5b58 --- /dev/null +++ b/tests/functions/get_remote_file_test.php @@ -0,0 +1,75 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_admin.php'; + +/** +* @group slow +*/ +class phpbb_functions_get_remote_file extends phpbb_test_case +{ + public function test_version_phpbb_com() + { + $hostname = 'version.phpbb.com'; + + if (!phpbb_checkdnsrr($hostname, 'A')) + { + $this->markTestSkipped(sprintf( + 'Could not find a DNS record for hostname %s. ' . + 'Assuming network is down.', + $hostname + )); + } + + $errstr = $errno = null; + $file = get_remote_file($hostname, '/phpbb', '30x.txt', $errstr, $errno); + + $this->assertNotEquals( + 0, + strlen($file), + 'Failed asserting that the response is not empty.' + ); + + $this->assertSame( + '', + $errstr, + 'Failed asserting that the error string is empty.' + ); + + $this->assertSame( + 0, + $errno, + 'Failed asserting that the error number is 0 (i.e. no error occurred).' + ); + + $lines = explode("\n", $file); + + $this->assertGreaterThanOrEqual( + 2, + sizeof($lines), + 'Failed asserting that the version file has at least two lines.' + ); + + $this->assertStringStartsWith( + '3.', + $lines[0], + "Failed asserting that the first line of the version file starts with '3.'" + ); + + $this->assertNotSame( + false, + filter_var($lines[1], FILTER_VALIDATE_URL), + 'Failed asserting that the second line of the version file is a valid URL.' + ); + + $this->assertContains('http', $lines[1]); + $this->assertContains('phpbb.com', $lines[1], '', true); + } +} diff --git a/tests/functions/is_absolute_test.php b/tests/functions/is_absolute_test.php new file mode 100644 index 0000000000..6d26793d82 --- /dev/null +++ b/tests/functions/is_absolute_test.php @@ -0,0 +1,56 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_functions_is_absolute_test extends phpbb_test_case +{ + static public function is_absolute_data() + { + return array( + // Empty + array('', false), + + // Absolute unix style + array('/etc/phpbb', true), + // Unix does not support \ so that is not an absolute path + array('\etc\phpbb', false), + + // Absolute windows style + array('c:\windows', true), + array('C:\Windows', true), + array('c:/windows', true), + array('C:/Windows', true), + + // Executable + array('etc/phpbb', false), + array('explorer.exe', false), + + // Relative subdir + array('Windows\System32', false), + array('Windows\System32\explorer.exe', false), + array('Windows/System32', false), + array('Windows/System32/explorer.exe', false), + + // Relative updir + array('..\Windows\System32', false), + array('..\Windows\System32\explorer.exe', false), + array('../Windows/System32', false), + array('../Windows/System32/explorer.exe', false), + ); + } + + /** + * @dataProvider is_absolute_data + */ + public function test_is_absolute($path, $expected) + { + $this->assertEquals($expected, phpbb_is_absolute($path)); + } +} diff --git a/tests/functions/language_select_test.php b/tests/functions/language_select_test.php new file mode 100644 index 0000000000..3341e2a256 --- /dev/null +++ b/tests/functions/language_select_test.php @@ -0,0 +1,39 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_functions_language_select_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/language_select.xml'); + } + + static public function language_select_data() + { + return array( + array('', '<option value="cs">Čeština</option><option value="en">English</option>'), + array('en', '<option value="cs">Čeština</option><option value="en" selected="selected">English</option>'), + array('cs', '<option value="cs" selected="selected">Čeština</option><option value="en">English</option>'), + array('de', '<option value="cs">Čeština</option><option value="en">English</option>'), + ); + } + + /** + * @dataProvider language_select_data + */ + public function test_language_select($default, $expected) + { + global $db; + $db = $this->new_dbal(); + + $this->assertEquals($expected, language_select($default)); + } +} diff --git a/tests/functions/obtain_online_test.php b/tests/functions/obtain_online_test.php new file mode 100644 index 0000000000..fe372431a9 --- /dev/null +++ b/tests/functions/obtain_online_test.php @@ -0,0 +1,227 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; + +class phpbb_functions_obtain_online_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/obtain_online.xml'); + } + + protected function setUp() + { + parent::setUp(); + + global $config, $db; + + $db = $this->db = $this->new_dbal(); + $config = array( + 'load_online_time' => 5, + ); + } + + static public function obtain_guest_count_data() + { + return array( + array(0, 2), + array(1, 1), + ); + } + + /** + * @dataProvider obtain_guest_count_data + */ + public function test_obtain_guest_count($forum_id, $expected) + { + $this->db->sql_query('DELETE FROM phpbb_sessions'); + + $time = time(); + $this->create_guest_sessions($time); + $this->assertEquals($expected, obtain_guest_count($forum_id)); + } + + static public function obtain_users_online_data() + { + return array( + array(0, false, array( + 'online_users' => array(2 => 2, 3 => 3, 6 => 6, 7 => 7, 10 => 10), + 'hidden_users' => array(6 => 6, 7 => 7, 10 => 10), + 'total_online' => 5, + 'visible_online' => 2, + 'hidden_online' => 3, + 'guests_online' => 0, + )), + array(0, true, array( + 'online_users' => array(2 => 2, 3 => 3, 6 => 6, 7 => 7, 10 => 10), + 'hidden_users' => array(6 => 6, 7 => 7, 10 => 10), + 'total_online' => 7, + 'visible_online' => 2, + 'hidden_online' => 3, + 'guests_online' => 2, + )), + array(1, false, array( + 'online_users' => array(3 => 3, 7 => 7), + 'hidden_users' => array(7 => 7), + 'total_online' => 2, + 'visible_online' => 1, + 'hidden_online' => 1, + 'guests_online' => 0, + )), + array(1, true, array( + 'online_users' => array(3 => 3, 7 => 7), + 'hidden_users' => array(7 => 7), + 'total_online' => 3, + 'visible_online' => 1, + 'hidden_online' => 1, + 'guests_online' => 1, + )), + array(2, false, array( + 'online_users' => array(), + 'hidden_users' => array(), + 'total_online' => 0, + 'visible_online' => 0, + 'hidden_online' => 0, + 'guests_online' => 0, + )), + array(2, true, array( + 'online_users' => array(), + 'hidden_users' => array(), + 'total_online' => 0, + 'visible_online' => 0, + 'hidden_online' => 0, + 'guests_online' => 0, + )), + ); + } + + /** + * @dataProvider obtain_users_online_data + */ + public function test_obtain_users_online($forum_id, $display_guests, $expected) + { + $this->db->sql_query('DELETE FROM phpbb_sessions'); + + global $config; + $config['load_online_guests'] = $display_guests; + + $time = time(); + $this->create_guest_sessions($time); + $this->create_user_sessions($time); + $this->assertEquals($expected, obtain_users_online($forum_id)); + } + + static public function obtain_users_online_string_data() + { + return array( + array(0, false, array( + 'online_userlist' => 'REGISTERED_USERS 2, 3', + 'l_online_users' => 'ONLINE_USERS_TOTAL 5 REG_USERS_TOTAL 2 HIDDEN_USERS_TOTAL 3', + )), + array(0, true, array( + 'online_userlist' => 'REGISTERED_USERS 2, 3', + 'l_online_users' => 'ONLINE_USERS_TOTAL_GUESTS 7 REG_USERS_TOTAL 2 HIDDEN_USERS_TOTAL 3 GUEST_USERS_TOTAL 2', + )), + array(1, false, array( + 'online_userlist' => 'BROWSING_FORUM 3', + 'l_online_users' => 'ONLINE_USERS_TOTAL 2 REG_USERS_TOTAL 1 HIDDEN_USERS_TOTAL 1', + )), + array(1, true, array( + 'online_userlist' => 'BROWSING_FORUM_GUESTS 1 3', + 'l_online_users' => 'ONLINE_USERS_TOTAL_GUESTS 3 REG_USERS_TOTAL 1 HIDDEN_USERS_TOTAL 1 GUEST_USERS_TOTAL 1', + )), + array(2, false, array( + 'online_userlist' => 'BROWSING_FORUM NO_ONLINE_USERS', + 'l_online_users' => 'ONLINE_USERS_TOTAL 0 REG_USERS_TOTAL 0 HIDDEN_USERS_TOTAL 0', + )), + array(2, true, array( + 'online_userlist' => 'BROWSING_FORUM_GUESTS 0 NO_ONLINE_USERS', + 'l_online_users' => 'ONLINE_USERS_TOTAL_GUESTS 0 REG_USERS_TOTAL 0 HIDDEN_USERS_TOTAL 0 GUEST_USERS_TOTAL 0', + )), + ); + } + + /** + * @dataProvider obtain_users_online_string_data + */ + public function test_obtain_users_online_string($forum_id, $display_guests, $expected) + { + $this->db->sql_query('DELETE FROM phpbb_sessions'); + + global $config, $user, $auth, $phpbb_dispatcher; + $config['load_online_guests'] = $display_guests; + $user = new phpbb_mock_lang(); + $user->lang = $this->load_language(); + $auth = $this->getMock('phpbb_auth'); + $acl_get_map = array( + array('u_viewonline', true), + array('u_viewprofile', true), + ); + $auth->expects($this->any()) + ->method('acl_get') + ->with($this->stringContains('_'), + $this->anything()) + ->will($this->returnValueMap($acl_get_map)); + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + + $time = time(); + $this->create_guest_sessions($time); + $this->create_user_sessions($time); + + $online_users = obtain_users_online($forum_id); + $this->assertEquals($expected, obtain_users_online_string($online_users, $forum_id)); + } + + protected function create_guest_sessions($time) + { + $this->add_session(1, '0001', '192.168.0.1', 0, true, $time); + $this->add_session(1, '0002', '192.168.0.2', 1, true, $time); + $this->add_session(1, '0003', '192.168.0.3', 0, true, $time, 10); + $this->add_session(1, '0004', '192.168.0.4', 1, true, $time, 10); + } + + protected function create_user_sessions($time) + { + $this->add_session(2, '0005', '192.168.0.5', 0, true, $time); + $this->add_session(3, '0006', '192.168.0.6', 1, true, $time); + $this->add_session(4, '0007', '192.168.0.7', 0, true, $time, 10); + $this->add_session(5, '0008', '192.168.0.8', 1, true, $time, 10); + $this->add_session(6, '0005', '192.168.0.9', 0, false, $time); + $this->add_session(7, '0006', '192.168.0.10', 1, false, $time); + $this->add_session(8, '0007', '192.168.0.11', 0, false, $time, 10); + $this->add_session(9, '0008', '192.168.0.12', 1, false, $time, 10); + $this->add_session(10, '009', '192.168.0.13', 0, false, $time); + } + + protected function add_session($user_id, $session_id, $user_ip, $forum_id, $view_online, $time, $time_delta = 0) + { + $sql_ary = array( + 'session_id' => $user_id . '_' . $forum_id . '_session00000000000000000' . $session_id, + 'session_user_id' => $user_id, + 'session_ip' => $user_ip, + 'session_forum_id' => $forum_id, + 'session_time' => $time - $time_delta * 60, + 'session_viewonline' => $view_online, + ); + $this->db->sql_query('INSERT INTO phpbb_sessions ' . $this->db->sql_build_array('INSERT', $sql_ary)); + } + + protected function load_language() + { + return array( + 'NO_ONLINE_USERS' => 'NO_ONLINE_USERS', + 'REGISTERED_USERS' => 'REGISTERED_USERS', + 'BROWSING_FORUM' => 'BROWSING_FORUM %s', + 'BROWSING_FORUM_GUEST' => 'BROWSING_FORUM_GUEST %s %d', + 'BROWSING_FORUM_GUESTS' => 'BROWSING_FORUM_GUESTS %s %d', + ); + } +} diff --git a/tests/functions/style_select_test.php b/tests/functions/style_select_test.php new file mode 100644 index 0000000000..1e44f3c2cb --- /dev/null +++ b/tests/functions/style_select_test.php @@ -0,0 +1,41 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_functions_style_select_test extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/style_select.xml'); + } + + static public function style_select_data() + { + return array( + array('', false, '<option value="1">prosilver</option><option value="2">subsilver2</option>'), + array('', true, '<option value="1">prosilver</option><option value="2">subsilver2</option><option value="3">zoo</option>'), + array('1', false, '<option value="1" selected="selected">prosilver</option><option value="2">subsilver2</option>'), + array('1', true, '<option value="1" selected="selected">prosilver</option><option value="2">subsilver2</option><option value="3">zoo</option>'), + array('3', false, '<option value="1">prosilver</option><option value="2">subsilver2</option>'), + array('3', true, '<option value="1">prosilver</option><option value="2">subsilver2</option><option value="3" selected="selected">zoo</option>'), + ); + } + + /** + * @dataProvider style_select_data + */ + public function test_style_select($default, $all, $expected) + { + global $db; + $db = $this->new_dbal(); + + $this->assertEquals($expected, style_select($default, $all)); + } +} diff --git a/tests/functions/validate_data_helper.php b/tests/functions/validate_data_helper.php new file mode 100644 index 0000000000..b92a3aa5eb --- /dev/null +++ b/tests/functions/validate_data_helper.php @@ -0,0 +1,36 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_functions_validate_data_helper +{ + protected $test_case; + + public function __construct($test_case) + { + $this->test_case = $test_case; + } + + /** + * Test provided input data with supplied checks and compare to expected + * results + * + * @param array $data Array containing one or more subarrays with the + * test data. The first element of a subarray is the + * expected result, the second one is the input, and the + * third is the data that should be passed to the function + * validate_data(). + */ + public function assert_valid_data($data) + { + foreach ($data as $key => $test) + { + $this->test_case->assertEquals($test[0], validate_data(array($test[1]), array($test[2]))); + } + } +} diff --git a/tests/functions/validate_date_test.php b/tests/functions/validate_date_test.php new file mode 100644 index 0000000000..1dcd1361a2 --- /dev/null +++ b/tests/functions/validate_date_test.php @@ -0,0 +1,82 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_date_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_date() + { + $this->helper->assert_valid_data(array( + 'empty' => array( + array('INVALID'), + '', + array('date'), + ), + 'empty_opt' => array( + array(), + '', + array('date', true), + ), + 'double_single' => array( + array(), + '17-06-1990', + array('date'), + ), + 'single_single' => array( + array(), + '05-05-2009', + array('date'), + ), + 'double_double' => array( + array(), + '17-12-1990', + array('date'), + ), + 'month_high' => array( + array('INVALID'), + '17-17-1990', + array('date'), + ), + 'month_low' => array( + array('INVALID'), + '01-00-1990', + array('date'), + ), + 'day_high' => array( + array('INVALID'), + '64-01-1990', + array('date'), + ), + 'day_low' => array( + array('INVALID'), + '00-12-1990', + array('date'), + ), + // Currently fails + /* + 'zero_year' => array( + array(), + '01-01-0000', + array('date'), + ), + */ + )); + } +} diff --git a/tests/functions/validate_email_test.php b/tests/functions/validate_email_test.php new file mode 100644 index 0000000000..9a6ce39251 --- /dev/null +++ b/tests/functions/validate_email_test.php @@ -0,0 +1,108 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/../mock/user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_email_test extends phpbb_database_test_case +{ + protected $db; + protected $user; + protected $helper; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/validate_email.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->user = new phpbb_mock_user; + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + /** + * Get validation prerequesites + * + * @param bool $check_mx Whether mx records should be checked + */ + protected function set_validation_prerequisites($check_mx) + { + global $config, $db, $user; + + $config['email_check_mx'] = $check_mx; + $db = $this->db; + $user = $this->user; + $user->optionset('banned_users', array('banned@example.com')); + } + + public function test_validate_email() + { + $this->set_validation_prerequisites(false); + + $this->helper->assert_valid_data(array( + 'empty' => array( + array(), + '', + array('email'), + ), + 'allowed' => array( + array(), + 'foobar@example.com', + array('email', 'foobar@example.com'), + ), + 'invalid' => array( + array('EMAIL_INVALID'), + 'fööbar@example.com', + array('email'), + ), + 'valid_complex' => array( + array(), + "'%$~test@example.com", + array('email'), + ), + 'taken' => array( + array('EMAIL_TAKEN'), + 'admin@example.com', + array('email'), + ), + 'banned' => array( + array('EMAIL_BANNED'), + 'banned@example.com', + array('email'), + ), + )); + } + + /** + * @group slow + */ + public function test_validate_email_mx() + { + $this->set_validation_prerequisites(true); + + $this->helper->assert_valid_data(array( + 'valid' => array( + array(), + 'foobar@phpbb.com', + array('email'), + ), + 'no_mx' => array( + array('DOMAIN_NO_MX_RECORD'), + 'test@does-not-exist.phpbb.com', + array('email'), + ), + )); + } +} diff --git a/tests/functions/validate_hex_colour_test.php b/tests/functions/validate_hex_colour_test.php new file mode 100644 index 0000000000..812ebe5eeb --- /dev/null +++ b/tests/functions/validate_hex_colour_test.php @@ -0,0 +1,121 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; + +class phpbb_functions_validate_hex_colour_test extends phpbb_test_case +{ + public function positive_match_data() + { + return array( + array('a00'), + array('AFF'), + array('AA0000'), + array('aa00FF'), + array('000'), + array('000000'), + ); + } + + public function negative_match_data() + { + return array( + // Invalid prefix + array('#aa0'), + array('#AA0000'), + array('vAA0000'), + + // Invalid suffix + array('AA0000v'), + + // Correct length, but out of hex range + array('ag0'), + array('AAG000'), + + // Too long + array('AA00000'), + array('AA0000 '), + array('AA0000 abf'), + array('AA0000 AA0000'), + + // empty() + array('0'), + ); + } + + public function optional_only_data() + { + return array( + // The empty colour, i.e. "no colour". + array(''), + ); + } + + public function strict_negative_match_data() + { + return array_merge( + $this->negative_match_data(), + $this->optional_only_data() + ); + } + + public function nonstrict_positive_match_data() + { + return array_merge( + $this->positive_match_data(), + $this->optional_only_data() + ); + } + + /** + * @dataProvider positive_match_data + */ + public function test_strict_positive_match($input) + { + $this->assertFalse( + phpbb_validate_hex_colour($input, false), + "Failed asserting that $input passes as a valid hex colour." + ); + } + + /** + * @dataProvider strict_negative_match_data + */ + public function test_strict_negative_match($input) + { + $this->assertSame( + 'WRONG_DATA', + phpbb_validate_hex_colour($input, false), + "Failed asserting that $input does not pass as a valid hex colour." + ); + } + + /** + * @dataProvider nonstrict_positive_match_data + */ + public function test_nonstrict_positive_match($input) + { + $this->assertFalse( + phpbb_validate_hex_colour($input, true), + "Failed asserting that $input passes as a valid or optional hex colour." + ); + } + + /** + * @dataProvider negative_match_data + */ + public function test_nonstrict_negative_match($input) + { + $this->assertSame( + 'WRONG_DATA', + phpbb_validate_hex_colour($input, true), + "Failed asserting that $input does not pass as a valid or optional hex colour." + ); + } +} diff --git a/tests/functions/validate_jabber_test.php b/tests/functions/validate_jabber_test.php new file mode 100644 index 0000000000..5a53c963bd --- /dev/null +++ b/tests/functions/validate_jabber_test.php @@ -0,0 +1,79 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_jabber_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_jabber() + { + $this->helper->assert_valid_data(array( + 'empty' => array( + array(), + '', + array('jabber'), + ), + 'no_seperator' => array( + array('WRONG_DATA'), + 'testjabber.ccc', + array('jabber'), + ), + 'no_user' => array( + array('WRONG_DATA'), + '@jabber.ccc', + array('jabber'), + ), + 'no_realm' => array( + array('WRONG_DATA'), + 'user@', + array('jabber'), + ), + 'dot_realm' => array( + array('WRONG_DATA'), + 'user@.....', + array('jabber'), + ), + '-realm' => array( + array('WRONG_DATA'), + 'user@-jabber.ccc', + array('jabber'), + ), + 'realm-' => array( + array('WRONG_DATA'), + 'user@jabber.ccc-', + array('jabber'), + ), + 'correct' => array( + array(), + 'user@jabber.09A-z.org', + array('jabber'), + ), + 'prohibited' => array( + array('WRONG_DATA'), + 'u@ser@jabber.ccc.org', + array('jabber'), + ), + 'prohibited_char' => array( + array('WRONG_DATA'), + 'u<s>er@jabber.ccc.org', + array('jabber'), + ), + )); + } +} diff --git a/tests/functions/validate_lang_iso_test.php b/tests/functions/validate_lang_iso_test.php new file mode 100644 index 0000000000..c8a5b71021 --- /dev/null +++ b/tests/functions/validate_lang_iso_test.php @@ -0,0 +1,60 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_lang_iso_test extends phpbb_database_test_case +{ + protected $db; + protected $helper; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/language_select.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_lang_iso() + { + global $db; + + $db = $this->db; + + $this->helper->assert_valid_data(array( + 'empty' => array( + array('WRONG_DATA'), + '', + array('language_iso_name'), + ), + 'en' => array( + array(), + 'en', + array('language_iso_name'), + ), + 'cs' => array( + array(), + 'cs', + array('language_iso_name'), + ), + 'de' => array( + array('WRONG_DATA'), + 'de', + array('language_iso_name'), + ), + )); + } +} diff --git a/tests/functions/validate_match_test.php b/tests/functions/validate_match_test.php new file mode 100644 index 0000000000..73a363e003 --- /dev/null +++ b/tests/functions/validate_match_test.php @@ -0,0 +1,49 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_match_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_match() + { + $this->helper->assert_valid_data(array( + 'empty_opt' => array( + array(), + '', + array('match', true, '/[a-z]$/'), + ), + 'empty_empty_match' => array( + array(), + '', + array('match'), + ), + 'foobar' => array( + array(), + 'foobar', + array('match', false, '/[a-z]$/'), + ), + 'foobar_fail' => array( + array('WRONG_DATA'), + 'foobar123', + array('match', false, '/[a-z]$/'), + ), + )); + } +} diff --git a/tests/functions/validate_num_test.php b/tests/functions/validate_num_test.php new file mode 100644 index 0000000000..4deac02ebc --- /dev/null +++ b/tests/functions/validate_num_test.php @@ -0,0 +1,59 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_num_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_num() + { + $this->helper->assert_valid_data(array( + 'empty' => array( + array(), + '', + array('num'), + ), + 'zero' => array( + array(), + '0', + array('num'), + ), + 'five_minmax_correct' => array( + array(), + '5', + array('num', false, 2, 6), + ), + 'five_minmax_short' => array( + array('TOO_SMALL'), + '5', + array('num', false, 7, 10), + ), + 'five_minmax_long' => array( + array('TOO_LARGE'), + '5', + array('num', false, 2, 3), + ), + 'string' => array( + array(), + 'foobar', + array('num'), + ), + )); + } +} diff --git a/tests/functions/validate_password_test.php b/tests/functions/validate_password_test.php new file mode 100644 index 0000000000..4639f6cc89 --- /dev/null +++ b/tests/functions/validate_password_test.php @@ -0,0 +1,96 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_password_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function validate_password_data() + { + return array( + array('PASS_TYPE_ANY', array( + 'empty' => array(), + 'foobar_any' => array(), + 'foobar_mixed' => array(), + 'foobar_alpha' => array(), + 'foobar_symbol' => array(), + )), + array('PASS_TYPE_CASE', array( + 'empty' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_mixed' => array(), + 'foobar_alpha' => array(), + 'foobar_symbol' => array(), + )), + array('PASS_TYPE_ALPHA', array( + 'empty' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_mixed' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_symbol' => array(), + )), + array('PASS_TYPE_SYMBOL', array( + 'empty' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_mixed' => array('INVALID_CHARS'), + 'foobar_alpha' => array('INVALID_CHARS'), + 'foobar_symbol' => array(), + )), + ); + } + + /** + * @dataProvider validate_password_data + */ + public function test_validate_password($pass_complexity, $expected) + { + global $config; + + // Set complexity to mixed case letters, numbers and symbols + $config['pass_complex'] = $pass_complexity; + + $this->helper->assert_valid_data(array( + 'empty' => array( + $expected['empty'], + '', + array('password'), + ), + 'foobar_any' => array( + $expected['foobar_any'], + 'foobar', + array('password'), + ), + 'foobar_mixed' => array( + $expected['foobar_mixed'], + 'FooBar', + array('password'), + ), + 'foobar_alpha' => array( + $expected['foobar_alpha'], + 'F00bar', + array('password'), + ), + 'foobar_symbol' => array( + $expected['foobar_symbol'], + 'fooBar123*', + array('password'), + ), + )); + } +} diff --git a/tests/functions/validate_string_test.php b/tests/functions/validate_string_test.php new file mode 100644 index 0000000000..ab44c28541 --- /dev/null +++ b/tests/functions/validate_string_test.php @@ -0,0 +1,70 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_string_test extends phpbb_test_case +{ + protected $helper; + + protected function setUp() + { + parent::setUp(); + + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function test_validate_string() + { + $this->helper->assert_valid_data(array( + 'empty_opt' => array( + array(), + '', + array('string', true), + ), + 'empty' => array( + array(), + '', + array('string'), + ), + 'foo' => array( + array(), + 'foobar', + array('string'), + ), + 'foo_minmax_correct' => array( + array(), + 'foobar', + array('string', false, 2, 6), + ), + 'foo_minmax_short' => array( + array('TOO_SHORT'), + 'foobar', + array('string', false, 7, 9), + ), + 'foo_minmax_long' => array( + array('TOO_LONG'), + 'foobar', + array('string', false, 2, 5), + ), + 'empty_short' => array( + array('TOO_SHORT'), + '', + array('string', false, 1, 6), + ), + 'empty_length_opt' => array( + array(), + '', + array('string', true, 1, 6), + ), + )); + } +} diff --git a/tests/functions/validate_username_test.php b/tests/functions/validate_username_test.php new file mode 100644 index 0000000000..0819974e54 --- /dev/null +++ b/tests/functions/validate_username_test.php @@ -0,0 +1,190 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; +require_once dirname(__FILE__) . '/../mock/cache.php'; +require_once dirname(__FILE__) . '/validate_data_helper.php'; + +class phpbb_functions_validate_data_test extends phpbb_database_test_case +{ + protected $db; + protected $cache; + protected $helper; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/validate_username.xml'); + } + + protected function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->cache = new phpbb_mock_cache; + $this->helper = new phpbb_functions_validate_data_helper($this); + } + + public function validate_username_data() + { + return array( + array('USERNAME_CHARS_ANY', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array(), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array(), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array(), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('USERNAME_TAKEN'), + )), + array('USERNAME_ALPHA_ONLY', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array('INVALID_CHARS'), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array('INVALID_CHARS'), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('INVALID_CHARS'), + )), + array('USERNAME_ALPHA_SPACERS', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array(), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array('INVALID_CHARS'), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('USERNAME_TAKEN'), + )), + array('USERNAME_LETTER_NUM', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array('INVALID_CHARS'), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array('INVALID_CHARS'), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('INVALID_CHARS'), + )), + array('USERNAME_LETTER_NUM_SPACERS', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array('INVALID_CHARS'), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array(), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array(), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('USERNAME_TAKEN'), + )), + array('USERNAME_ASCII', array( + 'foobar_allow' => array(), + 'foobar_ascii' => array(), + 'foobar_any' => array(), + 'foobar_alpha' => array(), + 'foobar_alpha_spacers' => array(), + 'foobar_letter_num' => array(), + 'foobar_letter_num_sp' => array('INVALID_CHARS'), + 'foobar_quot' => array('INVALID_CHARS'), + 'barfoo_disallow' => array('USERNAME_DISALLOWED'), + 'admin_taken' => array('USERNAME_TAKEN'), + 'group_taken' => array('USERNAME_TAKEN'), + )), + ); + } + + /** + * @dataProvider validate_username_data + */ + public function test_validate_username($allow_name_chars, $expected) + { + global $cache, $config, $db; + + $db = $this->db; + $cache = $this->cache; + $cache->put('_disallowed_usernames', array('barfoo')); + + $config['allow_name_chars'] = $allow_name_chars; + + $this->helper->assert_valid_data(array( + 'foobar_allow' => array( + $expected['foobar_allow'], + 'foobar', + array('username', 'foobar'), + ), + 'foobar_ascii' => array( + $expected['foobar_ascii'], + 'foobar', + array('username'), + ), + 'foobar_any' => array( + $expected['foobar_any'], + 'f*~*^=oo_bar1', + array('username'), + ), + 'foobar_alpha' => array( + $expected['foobar_alpha'], + 'fo0Bar', + array('username'), + ), + 'foobar_alpha_spacers' => array( + $expected['foobar_alpha_spacers'], + 'Fo0-[B]_a+ R', + array('username'), + ), + 'foobar_letter_num' => array( + $expected['foobar_letter_num'], + 'fo0Bar0', + array('username'), + ), + 'foobar_letter_num_sp' => array( + $expected['foobar_letter_num_sp'], + 'Fö0-[B]_a+ R', + array('username'), + ), + 'foobar_quot' => array( + $expected['foobar_quot'], + '"foobar"', + array('username'), + ), + 'barfoo_disallow' => array( + $expected['barfoo_disallow'], + 'barfoo', + array('username'), + ), + 'admin_taken' => array( + $expected['admin_taken'], + 'admin', + array('username'), + ), + 'group_taken' => array( + $expected['group_taken'], + 'foobar_group', + array('username'), + ), + )); + } +} diff --git a/tests/functions_acp/build_cfg_template_test.php b/tests/functions_acp/build_cfg_template_test.php index bf5330fd88..acf4da1bd6 100644 --- a/tests/functions_acp/build_cfg_template_test.php +++ b/tests/functions_acp/build_cfg_template_test.php @@ -49,6 +49,7 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case global $user, $phpbb_dispatcher; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = new phpbb_mock_user(); $user->lang = new phpbb_mock_lang(); $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); @@ -58,20 +59,20 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case { return array( array( - array('dimension', 20, 255), - 'key_name', + array('dimension', 5, 15), + 'number_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" />', + '<input id="number_key_name" type="number" size="2" maxlength="2" min="5" max="15" name="config[config_key_name_width]" value="10" /> x <input type="number" size="2" maxlength="2" min="5" max="15" name="config[config_key_name_height]" value="20" />', ), array( - array('dimension', 0, 255), - 'key_name', + array('dimension', 0, 15), + 'number_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" />', + '<input id="number_key_name" type="number" size="2" maxlength="2" min="0" max="15" name="config[config_key_name_width]" value="10" /> x <input type="number" size="2" maxlength="2" min="0" max="15" name="config[config_key_name_height]" value="20" />', ), ); } @@ -84,6 +85,43 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case global $user, $phpbb_dispatcher; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = new phpbb_mock_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_number_data() + { + return array( + array( + array('number', 5, 15), + 'number_key_name', + array('config_key_name' => 10), + 'config_key_name', + array(), + '<input id="number_key_name" type="number" maxlength="2" min="5" max="15" name="config[config_key_name]" value="10" />', + ), + array( + array('number', -1, 9999), + 'number_key_name', + array('config_key_name' => 10), + 'config_key_name', + array(), + '<input id="number_key_name" type="number" maxlength="4" min="-1" max="9999" name="config[config_key_name]" value="10" />', + ), + ); + } + + /** + * @dataProvider build_cfg_template_number_data + */ + public function test_build_cfg_template_number($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user, $phpbb_dispatcher; + + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = new phpbb_mock_user(); $user->lang = new phpbb_mock_lang(); $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); @@ -111,6 +149,7 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case global $user, $phpbb_dispatcher; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = new phpbb_mock_user(); $user->lang = new phpbb_mock_lang(); $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); @@ -162,6 +201,7 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case global $user, $phpbb_dispatcher; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = new phpbb_mock_user(); $user->lang = new phpbb_mock_lang(); $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); @@ -189,6 +229,7 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case global $user, $phpbb_dispatcher; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = new phpbb_mock_user(); $user->lang = new phpbb_mock_lang(); $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); diff --git a/tests/lock/db_test.php b/tests/lock/db_test.php index f7b1557a0c..de7a23fd05 100644 --- a/tests/lock/db_test.php +++ b/tests/lock/db_test.php @@ -32,13 +32,18 @@ class phpbb_lock_db_test extends phpbb_database_test_case public function test_new_lock() { + $this->assertFalse($this->lock->owns_lock()); + $this->assertTrue($this->lock->acquire()); + $this->assertTrue($this->lock->owns_lock()); $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->assertFalse($lock2->owns_lock()); $this->lock->release(); + $this->assertFalse($this->lock->owns_lock()); $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); } @@ -50,31 +55,40 @@ class phpbb_lock_db_test extends phpbb_database_test_case public function test_double_lock() { + $this->assertFalse($this->lock->owns_lock()); + $this->assertTrue($this->lock->acquire()); + $this->assertTrue($this->lock->owns_lock()); $this->assertTrue(isset($this->config['test_lock']), 'Lock was created'); $value = $this->config['test_lock']; $this->assertFalse($this->lock->acquire()); + $this->assertTrue($this->lock->owns_lock()); $this->assertEquals($value, $this->config['test_lock'], 'Second lock failed'); $this->lock->release(); + $this->assertFalse($this->lock->owns_lock()); $this->assertEquals('0', $this->config['test_lock'], 'Lock was released'); } public function test_double_unlock() { $this->assertTrue($this->lock->acquire()); + $this->assertTrue($this->lock->owns_lock()); $this->assertFalse(empty($this->config['test_lock']), 'First lock is acquired'); $this->lock->release(); + $this->assertFalse($this->lock->owns_lock()); $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->assertTrue($lock2->owns_lock()); $this->assertFalse(empty($this->config['test_lock']), 'Second lock is acquired'); $this->lock->release(); + $this->assertTrue($lock2->owns_lock()); $this->assertFalse(empty($this->config['test_lock']), 'Double release of first lock is ignored'); $lock2->release(); diff --git a/tests/lock/flock_test.php b/tests/lock/flock_test.php index 1edc96b3a4..8f0b866ab3 100644 --- a/tests/lock/flock_test.php +++ b/tests/lock/flock_test.php @@ -26,15 +26,21 @@ class phpbb_lock_flock_test extends phpbb_test_case $lock = new phpbb_lock_flock($path); $ok = $lock->acquire(); $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); $lock->release(); + $this->assertFalse($lock->owns_lock()); $ok = $lock->acquire(); $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); $lock->release(); + $this->assertFalse($lock->owns_lock()); $ok = $lock->acquire(); $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); $lock->release(); + $this->assertFalse($lock->owns_lock()); } /* This hangs the process. @@ -77,15 +83,18 @@ class phpbb_lock_flock_test extends phpbb_test_case $ok = $lock->acquire(); $delta = time() - $start; $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); $this->assertGreaterThan(0.5, $delta, 'First lock acquired too soon'); $lock->release(); + $this->assertFalse($lock->owns_lock()); // acquire again, this should be instantaneous $start = time(); $ok = $lock->acquire(); $delta = time() - $start; $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); $this->assertLessThan(0.1, $delta, 'Second lock not acquired instantaneously'); // reap the child @@ -99,8 +108,10 @@ class phpbb_lock_flock_test extends phpbb_test_case $lock = new phpbb_lock_flock($path); $ok = $lock->acquire(); $this->assertTrue($ok); + $this->assertTrue($lock->owns_lock()); sleep(2); $lock->release(); + $this->assertFalse($lock->owns_lock()); // and go away silently pcntl_exec('/usr/bin/env', array('true')); diff --git a/tests/log/function_add_log_test.php b/tests/log/function_add_log_test.php index 864b364862..7aa42be6df 100644 --- a/tests/log/function_add_log_test.php +++ b/tests/log/function_add_log_test.php @@ -16,7 +16,7 @@ class phpbb_log_function_add_log_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/empty_log.xml'); } - public static function test_add_log_function_data() + public static function add_log_function_data() { return array( /** @@ -138,7 +138,7 @@ class phpbb_log_function_add_log_test extends phpbb_database_test_case } /** - * @dataProvider test_add_log_function_data + * @dataProvider add_log_function_data */ public function test_add_log_function($expected, $user_id, $mode, $required1, $additional1 = null, $additional2 = null, $additional3 = null) { diff --git a/tests/log/function_view_log_test.php b/tests/log/function_view_log_test.php index 2ecf77aeb8..1ab9488568 100644 --- a/tests/log/function_view_log_test.php +++ b/tests/log/function_view_log_test.php @@ -22,7 +22,7 @@ class phpbb_log_function_view_log_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/full_log.xml'); } - public static function test_view_log_function_data() + public static function view_log_function_data() { global $phpEx, $phpbb_dispatcher; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); @@ -296,7 +296,7 @@ class phpbb_log_function_view_log_test extends phpbb_database_test_case } /** - * @dataProvider test_view_log_function_data + * @dataProvider view_log_function_data */ public function test_view_log_function($expected, $expected_returned, $mode, $log_count, $limit = 5, $offset = 0, $forum_id = 0, $topic_id = 0, $user_id = 0, $limit_days = 0, $sort_by = 'l.log_id ASC', $keywords = '') { diff --git a/tests/mock/cache.php b/tests/mock/cache.php index 71af3037f5..440592665a 100644 --- a/tests/mock/cache.php +++ b/tests/mock/cache.php @@ -53,6 +53,21 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface ); } + /** + * Obtain disallowed usernames. Input data via standard put method. + */ + public function obtain_disallowed_usernames() + { + if (($usernames = $this->get('_disallowed_usernames')) !== false) + { + return $usernames; + } + else + { + return array(); + } + } + public function checkVar(PHPUnit_Framework_Assert $test, $var_name, $data) { $test->assertTrue(isset($this->data[$var_name])); diff --git a/tests/mock/extension_manager.php b/tests/mock/extension_manager.php index fdda4cbadc..10b3595206 100644 --- a/tests/mock/extension_manager.php +++ b/tests/mock/extension_manager.php @@ -12,7 +12,8 @@ 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->php_ext = 'php'; $this->extensions = $extensions; + $this->filesystem = new phpbb_filesystem(); } } diff --git a/tests/mock/lang.php b/tests/mock/lang.php index 781b3d060e..ac814b45db 100644 --- a/tests/mock/lang.php +++ b/tests/mock/lang.php @@ -30,4 +30,9 @@ class phpbb_mock_lang implements ArrayAccess public function offsetUnset($offset) { } + + public function lang() + { + return implode(' ', func_get_args()); + } } diff --git a/tests/mock/metadata_manager.php b/tests/mock/metadata_manager.php new file mode 100644 index 0000000000..a7fbf0681c --- /dev/null +++ b/tests/mock/metadata_manager.php @@ -0,0 +1,21 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_mock_metadata_manager extends phpbb_extension_metadata_manager +{ + public function set_metadata($metadata) + { + $this->metadata = $metadata; + } + + public function merge_metadata($metadata) + { + $this->metadata = array_merge($this->metadata, $metadata); + } +} diff --git a/tests/mock/user.php b/tests/mock/user.php index ec14ce430e..bd547b3973 100644 --- a/tests/mock/user.php +++ b/tests/mock/user.php @@ -33,4 +33,17 @@ class phpbb_mock_user { $this->options[$item] = $value; } + + public function check_ban($user_id = false, $user_ips = false, $user_email = false, $return = false) + { + $banned_users = $this->optionget('banned_users'); + foreach ($banned_users as $banned) + { + if ($banned == $user_id || $banned == $user_ips || $banned == $user_email) + { + return true; + } + } + return false; + } } diff --git a/tests/notification/fixtures/notification.xml b/tests/notification/fixtures/notification.xml index 38e5f811dd..c7b2d03ff1 100644 --- a/tests/notification/fixtures/notification.xml +++ b/tests/notification/fixtures/notification.xml @@ -1,5 +1,13 @@ <?xml version="1.0" encoding="UTF-8" ?> <dataset> + <table name="phpbb_bookmarks"> + </table> <table name="phpbb_notifications"> </table> + <table name="phpbb_notification_types"> + </table> + <table name="phpbb_topics_watch"> + </table> + <table name="phpbb_user_notifications"> + </table> </dataset> diff --git a/tests/notification/fixtures/submit_post_bookmark.xml b/tests/notification/fixtures/submit_post_bookmark.xml new file mode 100644 index 0000000000..d4bf8df73f --- /dev/null +++ b/tests/notification/fixtures/submit_post_bookmark.xml @@ -0,0 +1,175 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_bookmarks"> + <column>topic_id</column> + <column>user_id</column> + <row> + <value>1</value> + <value>2</value> + </row> + <row> + <value>1</value> + <value>3</value> + </row> + <row> + <value>1</value> + <value>4</value> + </row> + <row> + <value>1</value> + <value>5</value> + </row> + <row> + <value>1</value> + <value>6</value> + </row> + <row> + <value>1</value> + <value>7</value> + </row> + </table> + <table name="phpbb_notifications"> + <column>notification_type_id</column> + <column>user_id</column> + <column>item_id</column> + <column>item_parent_id</column> + <column>notification_read</column> + <column>notification_data</column> + <row> + <value>1</value> + <value>5</value> + <value>1</value> + <value>1</value> + <value>0</value> + <value></value> + </row> + </table> + <table name="phpbb_notification_types"> + <column>notification_type_id</column> + <column>notification_type_name</column> + <column>notification_type_enabled</column> + <row> + <value>1</value> + <value>bookmark</value> + <value>1</value> + </row> + </table> + <table name="phpbb_posts"> + <column>post_id</column> + <column>topic_id</column> + <column>forum_id</column> + <column>post_text</column> + <row> + <value>1</value> + <value>1</value> + <value>1</value> + <value></value> + </row> + </table> + <table name="phpbb_topics"> + <column>topic_id</column> + <column>forum_id</column> + <row> + <value>1</value> + <value>1</value> + </row> + </table> + <table name="phpbb_users"> + <column>user_id</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>2</value> + <value>poster</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>3</value> + <value>test</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>4</value> + <value>unauthorized</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>5</value> + <value>notified</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>6</value> + <value>disabled</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>7</value> + <value>default</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> + <table name="phpbb_user_notifications"> + <column>item_type</column> + <column>item_id</column> + <column>user_id</column> + <column>method</column> + <column>notify</column> + <row> + <value>bookmark</value> + <value>0</value> + <value>2</value> + <value></value> + <value>1</value> + </row> + <row> + <value>bookmark</value> + <value>0</value> + <value>3</value> + <value></value> + <value>1</value> + </row> + <row> + <value>bookmark</value> + <value>0</value> + <value>4</value> + <value></value> + <value>1</value> + </row> + <row> + <value>bookmark</value> + <value>0</value> + <value>5</value> + <value></value> + <value>1</value> + </row> + <row> + <value>bookmark</value> + <value>0</value> + <value>6</value> + <value></value> + <value>0</value> + </row> + </table> +</dataset> diff --git a/tests/notification/fixtures/submit_post_post.xml b/tests/notification/fixtures/submit_post_post.xml new file mode 100644 index 0000000000..b0ffa042c5 --- /dev/null +++ b/tests/notification/fixtures/submit_post_post.xml @@ -0,0 +1,219 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_forums_watch"> + <column>forum_id</column> + <column>user_id</column> + <column>notify_status</column> + <row> + <value>1</value> + <value>6</value> + <value>0</value> + </row> + <row> + <value>1</value> + <value>7</value> + <value>0</value> + </row> + <row> + <value>1</value> + <value>8</value> + <value>0</value> + </row> + </table> + <table name="phpbb_notifications"> + <column>notification_type_id</column> + <column>user_id</column> + <column>item_id</column> + <column>item_parent_id</column> + <column>notification_read</column> + <column>notification_data</column> + <row> + <value>1</value> + <value>5</value> + <value>1</value> + <value>1</value> + <value>0</value> + <value></value> + </row> + <row> + <value>1</value> + <value>8</value> + <value>1</value> + <value>1</value> + <value>0</value> + <value></value> + </row> + </table> + <table name="phpbb_notification_types"> + <column>notification_type_id</column> + <column>notification_type_name</column> + <column>notification_type_enabled</column> + <row> + <value>1</value> + <value>post</value> + <value>1</value> + </row> + </table> + <table name="phpbb_posts"> + <column>post_id</column> + <column>topic_id</column> + <column>forum_id</column> + <column>post_text</column> + <row> + <value>1</value> + <value>1</value> + <value>1</value> + <value></value> + </row> + </table> + <table name="phpbb_topics"> + <column>topic_id</column> + <column>forum_id</column> + <row> + <value>1</value> + <value>1</value> + </row> + </table> + <table name="phpbb_topics_watch"> + <column>topic_id</column> + <column>user_id</column> + <column>notify_status</column> + <row> + <value>1</value> + <value>2</value> + <value>0</value> + </row> + <row> + <value>1</value> + <value>3</value> + <value>0</value> + </row> + <row> + <value>1</value> + <value>4</value> + <value>0</value> + </row> + <row> + <value>1</value> + <value>5</value> + <value>0</value> + </row> + <row> + <value>1</value> + <value>6</value> + <value>0</value> + </row> + </table> + <table name="phpbb_users"> + <column>user_id</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>2</value> + <value>poster</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>3</value> + <value>test</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>4</value> + <value>unauthorized</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>5</value> + <value>notified</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>6</value> + <value>disabled</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>7</value> + <value>default</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> + <table name="phpbb_user_notifications"> + <column>item_type</column> + <column>item_id</column> + <column>user_id</column> + <column>method</column> + <column>notify</column> + <row> + <value>post</value> + <value>0</value> + <value>2</value> + <value></value> + <value>1</value> + </row> + <row> + <value>post</value> + <value>0</value> + <value>3</value> + <value></value> + <value>1</value> + </row> + <row> + <value>post</value> + <value>0</value> + <value>4</value> + <value></value> + <value>1</value> + </row> + <row> + <value>post</value> + <value>0</value> + <value>5</value> + <value></value> + <value>1</value> + </row> + <row> + <value>post</value> + <value>0</value> + <value>6</value> + <value></value> + <value>1</value> + </row> + <row> + <value>post</value> + <value>0</value> + <value>7</value> + <value></value> + <value>1</value> + </row> + <row> + <value>post</value> + <value>0</value> + <value>8</value> + <value></value> + <value>1</value> + </row> + </table> +</dataset> diff --git a/tests/notification/fixtures/submit_post_post_in_queue.xml b/tests/notification/fixtures/submit_post_post_in_queue.xml new file mode 100644 index 0000000000..090e90ea49 --- /dev/null +++ b/tests/notification/fixtures/submit_post_post_in_queue.xml @@ -0,0 +1,177 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_notifications"> + <column>notification_type_id</column> + <column>user_id</column> + <column>item_id</column> + <column>item_parent_id</column> + <column>notification_read</column> + <column>notification_data</column> + <row> + <value>1</value> + <value>6</value> + <value>1</value> + <value>1</value> + <value>0</value> + <value></value> + </row> + </table> + <table name="phpbb_notification_types"> + <column>notification_type_id</column> + <column>notification_type_name</column> + <column>notification_type_enabled</column> + <row> + <value>1</value> + <value>post_in_queue</value> + <value>1</value> + </row> + </table> + <table name="phpbb_posts"> + <column>post_id</column> + <column>topic_id</column> + <column>forum_id</column> + <column>post_text</column> + <row> + <value>1</value> + <value>1</value> + <value>1</value> + <value></value> + </row> + </table> + <table name="phpbb_topics"> + <column>topic_id</column> + <column>forum_id</column> + <row> + <value>1</value> + <value>1</value> + </row> + </table> + <table name="phpbb_users"> + <column>user_id</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>2</value> + <value>poster</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>3</value> + <value>test</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>4</value> + <value>unauthorized-mod</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>5</value> + <value>unauthorized-read</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>6</value> + <value>notified</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>7</value> + <value>disabled</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>8</value> + <value>default</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>9</value> + <value>test glboal-permissions</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> + <table name="phpbb_user_notifications"> + <column>item_type</column> + <column>item_id</column> + <column>user_id</column> + <column>method</column> + <column>notify</column> + <row> + <value>needs_approval</value> + <value>0</value> + <value>2</value> + <value></value> + <value>1</value> + </row> + <row> + <value>needs_approval</value> + <value>0</value> + <value>3</value> + <value></value> + <value>1</value> + </row> + <row> + <value>needs_approval</value> + <value>0</value> + <value>4</value> + <value></value> + <value>1</value> + </row> + <row> + <value>needs_approval</value> + <value>0</value> + <value>5</value> + <value></value> + <value>1</value> + </row> + <row> + <value>needs_approval</value> + <value>0</value> + <value>6</value> + <value></value> + <value>1</value> + </row> + <row> + <value>needs_approval</value> + <value>0</value> + <value>7</value> + <value></value> + <value>0</value> + </row> + <row> + <value>needs_approval</value> + <value>0</value> + <value>9</value> + <value></value> + <value>1</value> + </row> + </table> +</dataset> diff --git a/tests/notification/fixtures/submit_post_quote.xml b/tests/notification/fixtures/submit_post_quote.xml new file mode 100644 index 0000000000..f22ed97d91 --- /dev/null +++ b/tests/notification/fixtures/submit_post_quote.xml @@ -0,0 +1,147 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_notifications"> + <column>notification_type_id</column> + <column>user_id</column> + <column>item_id</column> + <column>item_parent_id</column> + <column>notification_read</column> + <column>notification_data</column> + <row> + <value>1</value> + <value>5</value> + <value>1</value> + <value>1</value> + <value>0</value> + <value></value> + </row> + </table> + <table name="phpbb_notification_types"> + <column>notification_type_id</column> + <column>notification_type_name</column> + <column>notification_type_enabled</column> + <row> + <value>1</value> + <value>quote</value> + <value>1</value> + </row> + </table> + <table name="phpbb_posts"> + <column>post_id</column> + <column>topic_id</column> + <column>forum_id</column> + <column>post_text</column> + <row> + <value>1</value> + <value>1</value> + <value>1</value> + <value></value> + </row> + </table> + <table name="phpbb_topics"> + <column>topic_id</column> + <column>forum_id</column> + <row> + <value>1</value> + <value>1</value> + </row> + </table> + <table name="phpbb_users"> + <column>user_id</column> + <column>username_clean</column> + <column>user_permissions</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> + <row> + <value>2</value> + <value>poster</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>3</value> + <value>test</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>4</value> + <value>unauthorized</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>5</value> + <value>notified</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>6</value> + <value>disabled</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>7</value> + <value>default</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> + <table name="phpbb_user_notifications"> + <column>item_type</column> + <column>item_id</column> + <column>user_id</column> + <column>method</column> + <column>notify</column> + <row> + <value>quote</value> + <value>0</value> + <value>2</value> + <value></value> + <value>1</value> + </row> + <row> + <value>quote</value> + <value>0</value> + <value>3</value> + <value></value> + <value>1</value> + </row> + <row> + <value>quote</value> + <value>0</value> + <value>4</value> + <value></value> + <value>1</value> + </row> + <row> + <value>quote</value> + <value>0</value> + <value>5</value> + <value></value> + <value>1</value> + </row> + <row> + <value>quote</value> + <value>0</value> + <value>6</value> + <value></value> + <value>0</value> + </row> + </table> +</dataset> diff --git a/tests/mock/notifications_notification_manager.php b/tests/notification/manager_helper.php index c995afb9ab..7a794f922f 100644 --- a/tests/mock/notifications_notification_manager.php +++ b/tests/notification/manager_helper.php @@ -19,7 +19,7 @@ if (!defined('IN_PHPBB')) * Notifications service class * @package notifications */ -class phpbb_mock_notifications_notification_manager extends phpbb_notification_manager +class phpbb_notification_manager_helper extends phpbb_notification_manager { public function set_var($name, $value) { @@ -28,12 +28,10 @@ class phpbb_mock_notifications_notification_manager extends phpbb_notification_m // Extra dependencies for get_*_class functions protected $auth = null; - protected $cache = null; protected $config = null; - public function setDependencies($auth, $cache, $config) + public function setDependencies($auth, $config) { $this->auth = $auth; - $this->cache = $cache; $this->config = $config; } @@ -44,7 +42,7 @@ class phpbb_mock_notifications_notification_manager extends phpbb_notification_m { $item_type = 'phpbb_notification_type_' . $item_type; - $item = new $item_type($this->user_loader, $this->db, $this->cache, $this->user, $this->auth, $this->config, $this->phpbb_root_path, $this->php_ext, $this->notification_types_table, $this->notifications_table, $this->user_notifications_table); + $item = new $item_type($this->user_loader, $this->db, $this->cache->get_driver(), $this->user, $this->auth, $this->config, $this->phpbb_root_path, $this->php_ext, $this->notification_types_table, $this->notifications_table, $this->user_notifications_table); $item->set_notification_manager($this); @@ -60,7 +58,7 @@ class phpbb_mock_notifications_notification_manager extends phpbb_notification_m { $method_name = 'phpbb_notification_method_' . $method_name; - $method = new $method_name($this->user_loader, $this->db, $this->cache, $this->user, $this->auth, $this->config, $this->phpbb_root_path, $this->php_ext, $this->notification_types_table, $this->notifications_table, $this->user_notifications_table); + $method = new $method_name($this->user_loader, $this->db, $this->cache->get_driver(), $this->user, $this->auth, $this->config, $this->phpbb_root_path, $this->php_ext, $this->notification_types_table, $this->notifications_table, $this->user_notifications_table); $method->set_notification_manager($this); diff --git a/tests/notification/notification.php b/tests/notification/notification_test.php index 13c868a0c7..8f7eb3b8a8 100644 --- a/tests/notification/notification.php +++ b/tests/notification/notification_test.php @@ -7,6 +7,8 @@ * */ +require_once dirname(__FILE__) . '/manager_helper.php'; + class phpbb_notification_test extends phpbb_database_test_case { protected $notifications, $db, $container, $user, $config, $auth, $cache; @@ -22,11 +24,6 @@ class phpbb_notification_test extends phpbb_database_test_case global $phpbb_root_path, $phpEx; - if (!function_exists('set_var')) - { - include($phpbb_root_path . 'includes/functions.' . $phpEx); - } - include_once(__DIR__ . '/ext/test/notification/type/test.' . $phpEx); $this->db = $this->new_dbal(); @@ -36,19 +33,26 @@ class phpbb_notification_test extends phpbb_database_test_case 'allow_topic_notify' => true, 'allow_forum_notify' => true, )); - $this->user = new phpbb_mock_user(); + $this->user = new phpbb_user(); $this->user_loader = new phpbb_user_loader($this->db, $phpbb_root_path, $phpEx, 'phpbb_users'); $this->auth = new phpbb_mock_notifications_auth(); - $this->cache = new phpbb_mock_cache(); + $this->cache = new phpbb_cache_service( + new phpbb_cache_driver_null(), + $this->config, + $this->db, + $phpbb_root_path, + $phpEx + ); $this->container = new phpbb_mock_container_builder(); - $this->notifications = new phpbb_mock_notifications_notification_manager( + $this->notifications = new phpbb_notification_manager_helper( array(), array(), $this->container, $this->user_loader, $this->db, + $this->cache, $this->user, $phpbb_root_path, $phpEx, @@ -57,7 +61,7 @@ class phpbb_notification_test extends phpbb_database_test_case 'phpbb_user_notifications' ); - $this->notifications->setDependencies($this->auth, $this->cache, $this->config); + $this->notifications->setDependencies($this->auth, $this->config); $types = array(); foreach (array( @@ -92,7 +96,36 @@ class phpbb_notification_test extends phpbb_database_test_case { global $phpbb_root_path, $phpEx; - return new $type($this->user_loader, $this->db, $this->cache, $this->user, $this->auth, $this->config, $phpbb_root_path, $phpEx, 'phpbb_notification_types', 'phpbb_notifications', 'phpbb_user_notifications'); + return new $type($this->user_loader, $this->db, $this->cache->get_driver(), $this->user, $this->auth, $this->config, $phpbb_root_path, $phpEx, 'phpbb_notification_types', 'phpbb_notifications', 'phpbb_user_notifications'); + } + + public function test_get_notification_type_id() + { + // They should be inserted the first time + $this->assertEquals(1, $this->notifications->get_notification_type_id('post')); + $this->assertEquals(2, $this->notifications->get_notification_type_id('quote')); + $this->assertEquals(3, $this->notifications->get_notification_type_id('test')); + + $this->assertEquals(array( + 'test' => 3, + 'quote' => 2, + 'post' => 1, + ), + $this->notifications->get_notification_type_ids(array( + 'test', + 'quote', + 'post', + ) + )); + $this->assertEquals(2, $this->notifications->get_notification_type_id('quote')); + + try + { + $this->assertEquals(3, $this->notifications->get_notification_type_id('fail')); + + $this->fail('Non-existent type should throw an exception'); + } + catch (Exception $e) {} } public function test_get_subscription_types() @@ -126,6 +159,20 @@ class phpbb_notification_test extends phpbb_database_test_case public function test_notifications() { + $this->db->sql_query('DELETE FROM phpbb_notification_types'); + + $types = array('quote', 'bookmark', 'post', 'test'); + foreach ($types as $id => $type) + { + $this->db->sql_query('INSERT INTO phpbb_notification_types ' . + $this->db->sql_build_array('INSERT', array( + 'notification_type_id' => ($id + 1), + 'notification_type_name' => $type, + 'notification_type_enabled' => 1, + )) + ); + } + // Used to test post notifications later $this->db->sql_query('INSERT INTO ' . TOPICS_WATCH_TABLE . ' ' . $this->db->sql_build_array('INSERT', array( 'topic_id' => 2, @@ -200,7 +247,7 @@ class phpbb_notification_test extends phpbb_database_test_case $expected = array( 1 => array( - 'item_type' => 'test', + 'notification_type_id' => 4, 'item_id' => 1, 'item_parent_id' => 1, 'user_id' => 0, @@ -209,7 +256,7 @@ class phpbb_notification_test extends phpbb_database_test_case 'notification_data' => array(), ), 2 => array( - 'item_type' => 'test', + 'notification_type_id' => 4, 'item_id' => 2, 'item_parent_id' => 2, 'user_id' => 0, @@ -218,7 +265,7 @@ class phpbb_notification_test extends phpbb_database_test_case 'notification_data' => array(), ), 3 => array( - 'item_type' => 'test', + 'notification_type_id' => 4, 'item_id' => 3, 'item_parent_id' => 2, 'user_id' => 0, @@ -227,7 +274,7 @@ class phpbb_notification_test extends phpbb_database_test_case 'notification_data' => array(), ), 4 => array( - 'item_type' => 'post', + 'notification_type_id' => 3, 'item_id' => 4, 'item_parent_id' => 2, 'user_id' => 0, @@ -243,7 +290,7 @@ class phpbb_notification_test extends phpbb_database_test_case ), ), 5 => array( - 'item_type' => 'bookmark', + 'notification_type_id' => 2, 'item_id' => 5, 'item_parent_id' => 2, 'user_id' => 0, @@ -306,7 +353,7 @@ class phpbb_notification_test extends phpbb_database_test_case $expected = array( 1 => array( - 'item_type' => 'test', + 'notification_type_id' => 4, 'item_id' => 1, 'item_parent_id' => 2, 'user_id' => 0, @@ -315,7 +362,7 @@ class phpbb_notification_test extends phpbb_database_test_case 'notification_data' => array(), ), 2 => array( - 'item_type' => 'test', + 'notification_type_id' => 4, 'item_id' => 2, 'item_parent_id' => 2, 'user_id' => 0, @@ -324,7 +371,7 @@ class phpbb_notification_test extends phpbb_database_test_case 'notification_data' => array(), ), 3 => array( - 'item_type' => 'test', + 'notification_type_id' => 4, 'item_id' => 3, 'item_parent_id' => 2, 'user_id' => 0, @@ -333,7 +380,7 @@ class phpbb_notification_test extends phpbb_database_test_case 'notification_data' => array(), ), 4 => array( - 'item_type' => 'post', + 'notification_type_id' => 3, 'item_id' => 4, 'item_parent_id' => 2, 'user_id' => 0, @@ -349,7 +396,7 @@ class phpbb_notification_test extends phpbb_database_test_case ), ), 5 => array( - 'item_type' => 'bookmark', + 'notification_type_id' => 2, 'item_id' => 5, 'item_parent_id' => 2, 'user_id' => 0, diff --git a/tests/notification/submit_post_base.php b/tests/notification/submit_post_base.php new file mode 100644 index 0000000000..59daf6c9cb --- /dev/null +++ b/tests/notification/submit_post_base.php @@ -0,0 +1,153 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_posting.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; + +class phpbb_notification_submit_post_base extends phpbb_database_test_case +{ + protected $notifications, $db, $container, $user, $config, $auth, $cache; + + protected $item_type = ''; + + protected $poll_data = array(); + protected $post_data = array( + 'forum_id' => 1, + 'topic_id' => 1, + 'topic_title' => 'topic_title', + 'icon_id' => 0, + 'enable_bbcode' => 0, + 'enable_smilies' => 0, + 'enable_urls' => 0, + 'enable_sig' => 0, + 'message' => '', + 'message_md5' => '', + 'attachment_data' => array(), + 'bbcode_bitfield' => '', + 'bbcode_uid' => '', + 'post_edit_locked' => false, + //'force_approved_state' => 1, + ); + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/submit_post_' . $this->item_type . '.xml'); + } + + public function setUp() + { + parent::setUp(); + + global $auth, $cache, $config, $db, $phpbb_container, $phpbb_dispatcher, $user, $request, $phpEx, $phpbb_root_path; + + // Database + $this->db = $this->new_dbal(); + $db = $this->db; + + // Auth + $auth = $this->getMock('phpbb_auth'); + $auth->expects($this->any()) + ->method('acl_get') + ->with($this->stringContains('_'), + $this->anything()) + ->will($this->returnValueMap(array( + array('f_noapprove', 1, true), + array('f_postcount', 1, true), + array('m_edit', 1, false), + ))); + + // Config + $config = new phpbb_config(array('num_topics' => 1,'num_posts' => 1,)); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); + + $cache = new phpbb_cache_service( + new phpbb_cache_driver_null(), + $config, + $db, + $phpbb_root_path, + $phpEx + ); + + // Event dispatcher + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + + // User + $user = $this->getMock('phpbb_user'); + $user->ip = ''; + $user->data = array( + 'user_id' => 2, + 'username' => 'user-name', + 'is_registered' => true, + 'user_colour' => '', + ); + + // Request + $type_cast_helper = $this->getMock('phpbb_request_type_cast_helper_interface'); + $request = $this->getMock('phpbb_request'); + + // Container + $phpbb_container = new phpbb_mock_container_builder(); + + $user_loader = new phpbb_user_loader($db, $phpbb_root_path, $phpEx, USERS_TABLE); + + // Notification Types + $notification_types = array('quote', 'bookmark', 'post', 'post_in_queue', 'topic', 'approve_topic', 'approve_post'); + $notification_types_array = array(); + foreach ($notification_types as $type) + { + $class_name = 'phpbb_notification_type_' . $type; + $class = new $class_name( + $user_loader, $db, $cache->get_driver(), $user, $auth, $config, + $phpbb_root_path, $phpEx, + NOTIFICATION_TYPES_TABLE, NOTIFICATIONS_TABLE, USER_NOTIFICATIONS_TABLE); + + $phpbb_container->set('notification.type.' . $type, $class); + + $notification_types_array['notification.type.' . $type] = $class; + } + + // Notification Manager + $phpbb_notifications = new phpbb_notification_manager($notification_types_array, array(), + $phpbb_container, $user_loader, $db, $cache, $user, + $phpbb_root_path, $phpEx, + NOTIFICATION_TYPES_TABLE, NOTIFICATIONS_TABLE, USER_NOTIFICATIONS_TABLE); + $phpbb_container->set('notification_manager', $phpbb_notifications); + } + + /** + * @dataProvider submit_post_data + */ + public function test_submit_post($additional_post_data, $expected_before, $expected_after) + { + $sql = 'SELECT user_id, item_id, item_parent_id + FROM ' . NOTIFICATIONS_TABLE . ' n, ' . NOTIFICATION_TYPES_TABLE . " nt + WHERE nt.notification_type_name = '" . $this->item_type . "' + AND n.notification_type_id = nt.notification_type_id + ORDER BY user_id, item_id ASC"; + $result = $this->db->sql_query($sql); + $this->assertEquals($expected_before, $this->db->sql_fetchrowset($result)); + $this->db->sql_freeresult($result); + + $poll_data = $this->poll_data; + $post_data = array_merge($this->post_data, $additional_post_data); + submit_post('reply', '', 'poster-name', POST_NORMAL, $poll_data, $post_data, false, false); + + $sql = 'SELECT user_id, item_id, item_parent_id + FROM ' . NOTIFICATIONS_TABLE . ' n, ' . NOTIFICATION_TYPES_TABLE . " nt + WHERE nt.notification_type_name = '" . $this->item_type . "' + AND n.notification_type_id = nt.notification_type_id + ORDER BY user_id ASC, item_id ASC"; + $result = $this->db->sql_query($sql); + $this->assertEquals($expected_after, $this->db->sql_fetchrowset($result)); + $this->db->sql_freeresult($result); + } +} diff --git a/tests/notification/submit_post_type_bookmark_test.php b/tests/notification/submit_post_type_bookmark_test.php new file mode 100644 index 0000000000..861017ff5f --- /dev/null +++ b/tests/notification/submit_post_type_bookmark_test.php @@ -0,0 +1,90 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/submit_post_base.php'; + +class phpbb_notification_submit_post_type_bookmark_test extends phpbb_notification_submit_post_base +{ + protected $item_type = 'bookmark'; + + public function setUp() + { + parent::setUp(); + + global $auth; + + // Add additional permissions + $auth->expects($this->any()) + ->method('acl_get_list') + ->with($this->anything(), + $this->stringContains('_'), + $this->greaterThan(0)) + ->will($this->returnValueMap(array( + array( + array('3', '4', '5', '6', '7'), + 'f_read', + 1, + array( + 1 => array( + 'f_read' => array(3, 5, 6, 7), + ), + ), + ), + ))); + } + + /** + * submit_post() Notifications test + * + * submit_post() $mode = 'reply' + * Notification item_type = 'bookmark' + */ + public function submit_post_data() + { + return array( + /** + * Normal post + * + * User => State description + * 2 => Poster, should NOT receive a notification + * 3 => Bookmarked, should receive a notification + * 4 => Bookmarked, but unauthed to read, should NOT receive a notification + * 5 => Bookmarked, but already notified, should NOT receive a new notification + * 6 => Bookmarked, but option disabled, should NOT receive a notification + * 7 => Bookmarked, option set to default, should receive a notification + */ + array( + array(), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + ), + array( + array('user_id' => 3, 'item_id' => 2, 'item_parent_id' => 1), + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + array('user_id' => 7, 'item_id' => 2, 'item_parent_id' => 1), + ), + ), + + /** + * Unapproved post + * + * No new notifications + */ + array( + array('force_approved_state' => false), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + ), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + ), + ), + ); + } +} diff --git a/tests/notification/submit_post_type_post_in_queue_test.php b/tests/notification/submit_post_type_post_in_queue_test.php new file mode 100644 index 0000000000..6a7ac44e39 --- /dev/null +++ b/tests/notification/submit_post_type_post_in_queue_test.php @@ -0,0 +1,107 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/submit_post_base.php'; + +class phpbb_notification_submit_post_type_post_in_queue_test extends phpbb_notification_submit_post_base +{ + protected $item_type = 'post_in_queue'; + + public function setUp() + { + parent::setUp(); + + global $auth; + + // Add additional permissions + $auth->expects($this->any()) + ->method('acl_get_list') + ->with($this->anything(), + $this->stringContains('_'), + $this->greaterThan(0)) + ->will($this->returnValueMap(array( + array( + false, + 'm_approve', + array(1, 0), + array( + 0 => array( + 'm_approve' => array(9), + ), + 1 => array( + 'm_approve' => array(3, 4, 6, 7, 8), + ), + ), + ), + array( + array(3, 4, 6, 7, 8, 9), + 'f_read', + 1, + array( + 1 => array( + 'f_read' => array(3, 6, 7, 8, 9), + ), + ), + ), + ))); + } + + /** + * submit_post() Notifications test + * + * submit_post() $mode = 'reply' + * Notification item_type = 'post_in_queue' + */ + public function submit_post_data() + { + return array( + /** + * Normal post + * + * No new notifications + */ + array( + array(), + array( + array('user_id' => 6, 'item_id' => 1, 'item_parent_id' => 1), + ), + array( + array('user_id' => 6, 'item_id' => 1, 'item_parent_id' => 1), + ), + ), + + /** + * Unapproved post + * + * User => State description + * 2 => Poster, should NOT receive a notification + * 3 => Moderator, should receive a notification + * 4 => Moderator, but unauthed to read, should NOT receive a notification + * 5 => Moderator, but unauthed to approve, should NOT receive a notification + * 6 => Moderator, but already notified, should STILL receive a new notification + * 7 => Moderator, but option disabled, should NOT receive a notification + * 8 => Moderator, option set to default, should receive a notification + * 9 => Moderator, has only global mod permissions, should receive a notification + */ + array( + array('force_approved_state' => false), + array( + array('user_id' => 6, 'item_id' => 1, 'item_parent_id' => 1), + ), + array( + array('user_id' => 3, 'item_id' => 2, 'item_parent_id' => 1), + array('user_id' => 6, 'item_id' => 1, 'item_parent_id' => 1), + array('user_id' => 6, 'item_id' => 2, 'item_parent_id' => 1), + array('user_id' => 8, 'item_id' => 2, 'item_parent_id' => 1), + array('user_id' => 9, 'item_id' => 2, 'item_parent_id' => 1), + ), + ), + ); + } +} diff --git a/tests/notification/submit_post_type_post_test.php b/tests/notification/submit_post_type_post_test.php new file mode 100644 index 0000000000..473247a764 --- /dev/null +++ b/tests/notification/submit_post_type_post_test.php @@ -0,0 +1,96 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/submit_post_base.php'; + +class phpbb_notification_submit_post_type_post_test extends phpbb_notification_submit_post_base +{ + protected $item_type = 'post'; + + public function setUp() + { + parent::setUp(); + + global $auth; + + // Add additional permissions + $auth->expects($this->any()) + ->method('acl_get_list') + ->with($this->anything(), + $this->stringContains('_'), + $this->greaterThan(0)) + ->will($this->returnValueMap(array( + array( + array('3', '4', '5', '6', '7', '8'), + 'f_read', + 1, + array( + 1 => array( + 'f_read' => array(3, 5, 6, 7, 8), + ), + ), + ), + ))); + } + + /** + * submit_post() Notifications test + * + * submit_post() $mode = 'reply' + * Notification item_type = 'post' + */ + public function submit_post_data() + { + return array( + /** + * Normal post + * + * User => State description + * 2 => Poster, should NOT receive a notification + * 3 => Topic subscribed, should receive a notification + * 4 => Topic subscribed, but unauthed to read, should NOT receive a notification + * 5 => Topic subscribed, but already notified, should NOT receive a new notification + * 6 => Topic and forum subscribed, should receive ONE notification + * 7 => Forum subscribed, should receive a notification + * 8 => Forum subscribed, but already notified, should NOT receive a new notification + */ + array( + array(), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + array('user_id' => 8, 'item_id' => 1, 'item_parent_id' => 1), + ), + array( + array('user_id' => 3, 'item_id' => 2, 'item_parent_id' => 1), + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + array('user_id' => 6, 'item_id' => 2, 'item_parent_id' => 1), + array('user_id' => 7, 'item_id' => 2, 'item_parent_id' => 1), + array('user_id' => 8, 'item_id' => 1, 'item_parent_id' => 1), + ), + ), + + /** + * Unapproved post + * + * No new notifications + */ + array( + array('force_approved_state' => false), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + array('user_id' => 8, 'item_id' => 1, 'item_parent_id' => 1), + ), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + array('user_id' => 8, 'item_id' => 1, 'item_parent_id' => 1), + ), + ), + ); + } +} diff --git a/tests/notification/submit_post_type_quote_test.php b/tests/notification/submit_post_type_quote_test.php new file mode 100644 index 0000000000..2b66d9c6a1 --- /dev/null +++ b/tests/notification/submit_post_type_quote_test.php @@ -0,0 +1,113 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/submit_post_base.php'; + +class phpbb_notification_submit_post_type_quote_test extends phpbb_notification_submit_post_base +{ + protected $item_type = 'quote'; + + public function setUp() + { + parent::setUp(); + + global $auth; + + // Add additional permissions + $auth->expects($this->any()) + ->method('acl_get_list') + ->with($this->anything(), + $this->stringContains('_'), + $this->greaterThan(0)) + ->will($this->returnValueMap(array( + array( + array('3', '4', '5', '6', '7'), + 'f_read', + 1, + array( + 1 => array( + 'f_read' => array(3, 5, 6, 7), + ), + ), + ), + ))); + } + + /** + * submit_post() Notifications test + * + * submit_post() $mode = 'reply' + * Notification item_type = 'quote' + */ + public function submit_post_data() + { + return array( + /** + * Normal post + * + * User => State description + * 2 => Poster, should NOT receive a notification + * 3 => Quoted, should receive a notification + * 4 => Quoted, but unauthed to read, should NOT receive a notification + * 5 => Quoted, but already notified, should NOT receive a new notification + * 6 => Quoted, but option disabled, should NOT receive a notification + * 7 => Quoted, option set to default, should receive a notification + */ + array( + array( + 'message' => implode(' ', array( + '[quote="poster":uid]poster should not be notified[/quote:uid]', + '[quote="test":uid]test should be notified[/quote:uid]', + '[quote="unauthorized":uid]unauthorized to read, should not receive a notification[/quote:uid]', + '[quote="notified":uid]already notified, should not receive a new notification[/quote:uid]', + '[quote="disabled":uid]option disabled, should not receive a notification[/quote:uid]', + '[quote="default":uid]option set to default, should receive a notification[/quote:uid]', + '[quote="doesn\'t exist":uid]user does not exist, should not receive a notification[/quote:uid]', + )), + 'bbcode_uid' => 'uid', + ), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + ), + array( + array('user_id' => 3, 'item_id' => 2, 'item_parent_id' => 1), + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + array('user_id' => 7, 'item_id' => 2, 'item_parent_id' => 1), + ), + ), + + /** + * Unapproved post + * + * No new notifications + */ + array( + array( + 'message' => implode(' ', array( + '[quote="poster":uid]poster should not be notified[/quote:uid]', + '[quote="test":uid]test should be notified[/quote:uid]', + '[quote="unauthorized":uid]unauthorized to read, should not receive a notification[/quote:uid]', + '[quote="notified":uid]already notified, should not receive a new notification[/quote:uid]', + '[quote="disabled":uid]option disabled, should not receive a notification[/quote:uid]', + '[quote="default":uid]option set to default, should receive a notification[/quote:uid]', + '[quote="doesn\'t exist":uid]user does not exist, should not receive a notification[/quote:uid]', + )), + 'bbcode_uid' => 'uid', + 'force_approved_state' => false, + ), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + ), + array( + array('user_id' => 5, 'item_id' => 1, 'item_parent_id' => 1), + ), + ), + ); + } +} diff --git a/tests/search/common_test_case.php b/tests/search/common_test_case.php index dd04f7048c..029637b00b 100644 --- a/tests/search/common_test_case.php +++ b/tests/search/common_test_case.php @@ -86,6 +86,104 @@ abstract class phpbb_search_common_test_case extends phpbb_search_test_case array('-fooo', '-baar'), array(), ), + array( + 'fooo -fooo', + 'all', + true, + array('fooo', '-fooo'), + array(), + ), + array( + 'fooo fooo-', + 'all', + true, + array('fooo', 'fooo'), + array(), + ), + array( + '-fooo fooo', + 'all', + true, + array('-fooo', 'fooo'), + array(), + ), + array( + 'fooo- fooo', + 'all', + true, + array('fooo', 'fooo'), + array(), + ), + array( + 'fooo-baar fooo', + 'all', + true, + array('fooo', 'baar', 'fooo'), + array(), + ), + array( + 'fooo-baar -fooo', + 'all', + true, + array('fooo', 'baar', '-fooo'), + array(), + ), + array( + 'fooo-baar fooo-', + 'all', + true, + array('fooo', 'baar', 'fooo'), + array(), + ), + array( + 'fooo-baar baar', + 'all', + true, + array('fooo', 'baar', 'baar'), + array(), + ), + array( + 'fooo-baar -baar', + 'all', + true, + array('fooo', 'baar', '-baar'), + array(), + ), + array( + 'fooo-baar baar-', + 'all', + true, + array('fooo', 'baar', 'baar'), + array(), + ), + array( + 'fooo-baar fooo-baar', + 'all', + true, + array('fooo', 'baar', 'fooo', 'baar'), + array(), + ), + array( + 'fooo-baar -fooo-baar', + 'all', + true, + array('fooo', 'baar', '-fooo', 'baar'), + array(), + ), + array( + 'fooo-baar fooo-baar-', + 'all', + true, + array('fooo', 'baar', 'fooo', 'baar'), + array(), + ), + array( + 'fooo-baar-baaz', + 'all', + true, + array('fooo', 'baar', 'baaz'), + array(), + ), ); } diff --git a/tests/security/redirect_test.php b/tests/security/redirect_test.php index 1325466137..8e36780ca4 100644 --- a/tests/security/redirect_test.php +++ b/tests/security/redirect_test.php @@ -18,11 +18,11 @@ class phpbb_security_redirect_test extends phpbb_security_test_base // array(Input -> redirect(), expected triggered error (else false), expected returned result url (else false)) return array( array('data://x', false, 'http://localhost/phpBB'), - array('bad://localhost/phpBB/index.php', 'Tried to redirect to potentially insecure url.', false), + array('bad://localhost/phpBB/index.php', 'INSECURE_REDIRECT', false), array('http://www.otherdomain.com/somescript.php', false, 'http://localhost/phpBB'), - array("http://localhost/phpBB/memberlist.php\n\rConnection: close", 'Tried to redirect to potentially insecure url.', false), + array("http://localhost/phpBB/memberlist.php\n\rConnection: close", 'INSECURE_REDIRECT', false), array('javascript:test', false, 'http://localhost/phpBB/../javascript:test'), - array('http://localhost/phpBB/index.php;url=', 'Tried to redirect to potentially insecure url.', false), + array('http://localhost/phpBB/index.php;url=', 'INSECURE_REDIRECT', false), ); } diff --git a/tests/session/continue_test.php b/tests/session/continue_test.php index ad78d92299..e5a7f7a4a1 100644 --- a/tests/session/continue_test.php +++ b/tests/session/continue_test.php @@ -53,7 +53,20 @@ class phpbb_session_continue_test extends phpbb_database_test_case */ public function test_session_begin_valid_session($session_id, $user_id, $user_agent, $ip, $expected_sessions, $expected_cookies, $message) { + global $phpbb_container, $phpbb_root_path, $phpEx; + $db = $this->new_dbal(); + $config = new phpbb_config(array()); + $request = $this->getMock('phpbb_request'); + $user = $this->getMock('phpbb_user'); + + $auth_provider = new phpbb_auth_provider_db($db, $config, $request, $user, $phpbb_root_path, $phpEx); + $phpbb_container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $phpbb_container->expects($this->any()) + ->method('get') + ->with('auth.provider.db') + ->will($this->returnValue($auth_provider)); + $session_factory = new phpbb_session_testable_factory; $session_factory->set_cookies(array( '_sid' => $session_id, diff --git a/tests/session/init_test.php b/tests/session/creation_test.php index 830de34ed0..fde76d6b06 100644 --- a/tests/session/init_test.php +++ b/tests/session/creation_test.php @@ -9,7 +9,7 @@ require_once dirname(__FILE__) . '/testable_factory.php'; -class phpbb_session_init_test extends phpbb_database_test_case +class phpbb_session_creation_test extends phpbb_database_test_case { public function getDataSet() { @@ -20,7 +20,20 @@ class phpbb_session_init_test extends phpbb_database_test_case public function test_login_session_create() { + global $phpbb_container, $phpbb_root_path, $phpEx; + $db = $this->new_dbal(); + $config = new phpbb_config(array()); + $request = $this->getMock('phpbb_request'); + $user = $this->getMock('phpbb_user'); + + $auth_provider = new phpbb_auth_provider_db($db, $config, $request, $user, $phpbb_root_path, $phpEx); + $phpbb_container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $phpbb_container->expects($this->any()) + ->method('get') + ->with('auth.provider.db') + ->will($this->returnValue($auth_provider)); + $session_factory = new phpbb_session_testable_factory; $session = $session_factory->get_session($db); @@ -34,10 +47,11 @@ class phpbb_session_init_test extends phpbb_database_test_case $this->assertSqlResultEquals( array(array('session_user_id' => 3)), $sql, - 'Check if exacly one session for user id 3 was created' + 'Check if exactly one session for user id 3 was created' ); - $cookie_expire = $session->time_now + 31536000; // default is one year + $one_year_in_seconds = 365 * 24 * 60 * 60; + $cookie_expire = $session->time_now + $one_year_in_seconds; $session->check_cookies($this, array( 'u' => array(null, $cookie_expire), diff --git a/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/event_variable_spacing.html b/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/event_variable_spacing.html new file mode 100644 index 0000000000..028f8aa0d1 --- /dev/null +++ b/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/event_variable_spacing.html @@ -0,0 +1,6 @@ +|{VARIABLE}| +{VARIABLE}|{VARIABLE}| + +|{VARIABLE} + +<div class="{VARIABLE}">test</div> diff --git a/tests/template/datasets/ext_trivial/styles/silver/template/variable_spacing.html b/tests/template/datasets/ext_trivial/styles/silver/template/variable_spacing.html new file mode 100644 index 0000000000..49d8a6b873 --- /dev/null +++ b/tests/template/datasets/ext_trivial/styles/silver/template/variable_spacing.html @@ -0,0 +1 @@ +<!-- EVENT event_variable_spacing --> diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index 6cea9b92e3..0ac50c7f2b 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -16,9 +16,10 @@ class phpbb_template_template_events_test extends phpbb_template_template_test_c return array( /* array( - '', // file + '', // Description '', // dataset array(), // style names + '', // file array(), // vars array(), // block vars array(), // destroy diff --git a/tests/template/template_spacing_test.php b/tests/template/template_spacing_test.php new file mode 100644 index 0000000000..83f8711b38 --- /dev/null +++ b/tests/template/template_spacing_test.php @@ -0,0 +1,87 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 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_spacing_test extends phpbb_template_template_test_case +{ + public function template_data() + { + return array( + /* + array( + '', // Description + '', // dataset + array(), // style names + '', // file + array(), // vars + array(), // block vars + array(), // destroy + '', // expected result + ), + */ + array( + 'Spacing in templates', + 'ext_trivial', + array(), + 'variable_spacing.html', + array( + 'VARIABLE' => '{}', + ), + array(), + array(), + '|{}| +{}|{}| +|{} +<div class="{}">test</div>', + ), + ); + } + + /** + * @dataProvider template_data + */ + public function test_template($desc, $dataset, $style_names, $file, array $vars, array $block_vars, array $destroy, $expected) + { + // Run test + $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.php'; + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); + } + + /** + * @dataProvider template_data + */ + public function test_event($desc, $dataset, $style_names, $file, array $vars, array $block_vars, array $destroy, $expected) + { + // Reset the engine state + $this->setup_engine_for_events($dataset, $style_names); + + // Run test + $cache_file = $this->template->cachepath . str_replace('/', '.', $file) . '.php'; + $this->run_template($file, $vars, $block_vars, $destroy, $expected, $cache_file); + } + + protected function setup_engine_for_events($dataset, $style_names, 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 = dirname(__FILE__) . "/datasets/$dataset/styles/silver/template"; + $this->style_resource_locator = new phpbb_style_resource_locator(); + $this->extension_manager = new phpbb_mock_filesystem_extension_manager( + dirname(__FILE__) . "/datasets/$dataset/" + ); + $this->template = new phpbb_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, new phpbb_template_context, $this->extension_manager); + $this->style_provider = new phpbb_style_path_provider(); + $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('silver', array($this->template_path), $style_names, ''); + } +} diff --git a/tests/template/template_test.php b/tests/template/template_test.php index f43157775a..a3c0b69123 100644 --- a/tests/template/template_test.php +++ b/tests/template/template_test.php @@ -133,6 +133,20 @@ class phpbb_template_template_test extends phpbb_template_template_test_case "xyz\nabc\nabc\nbar\nbar\nabc", ), array( + 'define_advanced.html', + array(), + array('loop' => array(array(), array(), array(), array(), array(), array(), array()), 'test' => array(array()), 'test.deep' => array(array()), 'test.deep.defines' => array(array())), + array(), + "abc\nzxc\ncde\nbcd", + ), + array( + 'define_unclosed.html', + array(), + array(), + array(), + "test", + ), + array( 'expressions.html', array(), array(), @@ -197,8 +211,6 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array('loop'), '', ), - /* Currently fail on develop: - http://tracker.phpbb.com/browse/PHPBB3-11323 array( 'include_define_variable.html', array('VARIABLE' => 'variable.html'), @@ -213,7 +225,6 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array(), 'value', ), - */ /* no top level nested loops array( 'loop_vars.html', diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index 3c997cb00e..5d38ebed67 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -83,9 +83,14 @@ class phpbb_template_template_test_case extends phpbb_test_case $this->markTestSkipped("Template cache directory ({$template_cache_dir}) is not writable."); } - foreach (glob($this->template->cachepath . '*') as $file) + $file_array = scandir($template_cache_dir); + $file_prefix = basename($this->template->cachepath); + foreach ($file_array as $file) { - unlink($file); + if (strpos($file, $file_prefix) === 0) + { + unlink($template_cache_dir . '/' . $file); + } } $this->setup_engine(); @@ -95,9 +100,15 @@ class phpbb_template_template_test_case extends phpbb_test_case { if (is_object($this->template)) { - foreach (glob($this->template->cachepath . '*') as $file) + $template_cache_dir = dirname($this->template->cachepath); + $file_array = scandir($template_cache_dir); + $file_prefix = basename($this->template->cachepath); + foreach ($file_array as $file) { - unlink($file); + if (strpos($file, $file_prefix) === 0) + { + unlink($template_cache_dir . '/' . $file); + } } } } diff --git a/tests/template/templates/define.html b/tests/template/templates/define.html index 4459fffbe0..4e6d0ee793 100644 --- a/tests/template/templates/define.html +++ b/tests/template/templates/define.html @@ -7,5 +7,3 @@ {$VALUE} <!-- UNDEFINE $VALUE --> {$VALUE} -<!-- DEFINE $VALUE --> - diff --git a/tests/template/templates/define_advanced.html b/tests/template/templates/define_advanced.html new file mode 100644 index 0000000000..83467a5b4b --- /dev/null +++ b/tests/template/templates/define_advanced.html @@ -0,0 +1,12 @@ +<!-- DEFINE $VALUE --> +abc +<!-- ENDDEFINE --> +{$VALUE} +<!-- DEFINE $VALUE1 --> +bcd +<!-- ENDDEFINE --> +<!-- DEFINE $VALUE2 --> +cde +<!-- ENDDEFINE --> +<!-- INCLUDE define_include2.html --> +{$INCLUDED_VALUE3} diff --git a/tests/template/templates/define_include2.html b/tests/template/templates/define_include2.html new file mode 100644 index 0000000000..874f3e1852 --- /dev/null +++ b/tests/template/templates/define_include2.html @@ -0,0 +1,11 @@ +<!-- DEFINE $INCLUDED_VALUE1 --> +zxc +<!-- ENDDEFINE --> +<!-- DEFINE $INCLUDED_VALUE2 --> +qwe +<!-- ENDDEFINE --> +{$INCLUDED_VALUE1} +<!-- DEFINE $INCLUDED_VALUE3 --> +{$VALUE2} +{$VALUE1} +<!-- ENDDEFINE --> diff --git a/tests/template/templates/define_unclosed.html b/tests/template/templates/define_unclosed.html new file mode 100644 index 0000000000..1c975eab2b --- /dev/null +++ b/tests/template/templates/define_unclosed.html @@ -0,0 +1,2 @@ +<!-- DEFINE $VALUE --> +test diff --git a/tests/template/templates/variable_spacing.html b/tests/template/templates/variable_spacing.html new file mode 100644 index 0000000000..028f8aa0d1 --- /dev/null +++ b/tests/template/templates/variable_spacing.html @@ -0,0 +1,6 @@ +|{VARIABLE}| +{VARIABLE}|{VARIABLE}| + +|{VARIABLE} + +<div class="{VARIABLE}">test</div> diff --git a/tests/test_framework/phpbb_database_test_case.php b/tests/test_framework/phpbb_database_test_case.php index 72bce2e38a..c72ea5f765 100644 --- a/tests/test_framework/phpbb_database_test_case.php +++ b/tests/test_framework/phpbb_database_test_case.php @@ -11,6 +11,8 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test { static private $already_connected; + private $db_connections; + protected $test_case_helpers; protected $fixture_xml_data; @@ -28,6 +30,22 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test 'phpbb_database_test_case' => array('already_connected'), ); + + $this->db_connections = array(); + } + + protected function tearDown() + { + parent::tearDown(); + + // Close all database connections from this test + if (!empty($this->db_connections)) + { + foreach ($this->db_connections as $db) + { + $db->sql_close(); + } + } } protected function setUp() @@ -44,6 +62,21 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test } } + /** + * Performs synchronisations for a given table/column set on the database + * + * @param array $table_column_map Information about the tables/columns to synchronise + * + * @return null + */ + protected function database_synchronisation($table_column_map) + { + $config = $this->get_database_config(); + $manager = $this->create_connection_manager($config); + $manager->connect(); + $manager->database_synchronisation($table_column_map); + } + public function createXMLDataSet($path) { $db_config = $this->get_database_config(); @@ -121,6 +154,8 @@ abstract class phpbb_database_test_case extends PHPUnit_Extensions_Database_Test $db = new $config['dbms'](); $db->sql_connect($config['dbhost'], $config['dbuser'], $config['dbpasswd'], $config['dbname'], $config['dbport']); + $this->db_connections[] = $db; + return $db; } diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index 29058cc815..c93a777701 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -142,6 +142,28 @@ class phpbb_database_test_connection_manager } $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + switch ($this->config['dbms']) + { + case 'phpbb_db_driver_mysql': + case 'phpbb_db_driver_mysqli': + $this->pdo->exec('SET NAMES utf8'); + + /* + * The phpBB MySQL drivers set the STRICT_ALL_TABLES and + * STRICT_TRANS_TABLES flags/modes, so as a minimum requirement + * we want to make sure those are set for the PDO side of the + * test suite. + * + * The TRADITIONAL flag implies STRICT_ALL_TABLES and + * STRICT_TRANS_TABLES as well as other useful strictness flags + * the phpBB MySQL driver does not set. + */ + $this->pdo->exec("SET SESSION sql_mode='TRADITIONAL'"); + break; + + default: + } } /** @@ -458,11 +480,32 @@ class phpbb_database_test_connection_manager */ public function post_setup_synchronisation($xml_data_set) { + $table_names = $xml_data_set->getTableNames(); + + $tables = array(); + foreach ($table_names as $table) + { + $tables[$table] = $xml_data_set->getTableMetaData($table)->getColumns(); + } + + $this->database_synchronisation($tables); + } + + /** + * Performs synchronisations on the database after a fixture has been loaded + * + * @param array $table_column_map Array of tables/columns to synchronise + * array(table1 => array(column1, column2)) + * + * @return null + */ + public function database_synchronisation($table_column_map) + { $this->ensure_connected(__METHOD__); $queries = array(); - // Get escaped versions of the table names used in the fixture - $table_names = array_map(array($this->pdo, 'PDO::quote'), $xml_data_set->getTableNames()); + // Get escaped versions of the table names to synchronise + $table_names = array_map(array($this->pdo, 'PDO::quote'), array_keys($table_column_map)); switch ($this->config['dbms']) { @@ -519,7 +562,7 @@ class phpbb_database_test_connection_manager while ($row = $result->fetch(PDO::FETCH_ASSOC)) { // Get the columns used in the fixture for this table - $column_names = $xml_data_set->getTableMetaData($row['table_name'])->getColumns(); + $column_names = $table_column_map[$row['table_name']]; // Skip sequences that weren't specified in the fixture if (!in_array($row['column_name'], $column_names)) diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 887dfea3b5..7e2e750e30 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -12,8 +12,9 @@ require_once __DIR__ . '/../../phpBB/includes/functions_install.php'; class phpbb_functional_test_case extends phpbb_test_case { - protected $client; - protected $root_url; + static protected $client; + static protected $cookieJar; + static protected $root_url; protected $cache = null; protected $db = null; @@ -39,6 +40,7 @@ class phpbb_functional_test_case extends phpbb_test_case parent::setUpBeforeClass(); self::$config = phpbb_test_case_helpers::get_test_config(); + self::$root_url = self::$config['phpbb_functional_url']; // Important: this is used both for installation and by // test cases for querying the tables. @@ -64,12 +66,12 @@ class phpbb_functional_test_case extends phpbb_test_case $this->bootstrap(); - $this->cookieJar = new CookieJar; - $this->client = new Goutte\Client(array(), null, $this->cookieJar); + self::$cookieJar = new CookieJar; + self::$client = new Goutte\Client(array(), null, self::$cookieJar); // Reset the curl handle because it is 0 at this point and not a valid // resource - $this->client->getClient()->getCurlMulti()->reset(true); - $this->root_url = self::$config['phpbb_functional_url']; + self::$client->getClient()->getCurlMulti()->reset(true); + // Clear the language array so that things // that were added in other tests are gone $this->lang = array(); @@ -77,9 +79,55 @@ class phpbb_functional_test_case extends phpbb_test_case $this->purge_cache(); } - public function request($method, $path) + /** + * Perform a request to page + * + * @param string $method HTTP Method + * @param string $path Page path, relative from phpBB root path + * @param array $form_data An array of form field values + * @param bool $assert_response_html Should we perform standard assertions for a normal html page + * @return Symfony\Component\DomCrawler\Crawler + */ + static public function request($method, $path, $form_data = array(), $assert_response_html = true) + { + $crawler = self::$client->request($method, self::$root_url . $path, $form_data); + + if ($assert_response_html) + { + self::assert_response_html(); + } + + return $crawler; + } + + /** + * Submits a form + * + * @param Symfony\Component\DomCrawler\Form $form A Form instance + * @param array $values An array of form field values + * @param bool $assert_response_html Should we perform standard assertions for a normal html page + * @return Symfony\Component\DomCrawler\Crawler + */ + static public function submit(Symfony\Component\DomCrawler\Form $form, array $values = array(), $assert_response_html = true) + { + $crawler = self::$client->submit($form, $values); + + if ($assert_response_html) + { + self::assert_response_html(); + } + + return $crawler; + } + + /** + * Get Client Content + * + * @return string HTML page + */ + static public function get_content() { - return $this->client->request($method, $this->root_url . $path); + return self::$client->getResponse()->getContent(); } // bootstrap, called after board is set up @@ -148,14 +196,17 @@ class phpbb_functional_test_case extends phpbb_test_case self::$config['table_prefix'], array() ); + $container = new phpbb_mock_container_builder(); + $container->set('migrator', $migrator); + $extension_manager = new phpbb_extension_manager( - new phpbb_mock_container_builder(), + $container, $db, $config, - $migrator, + new phpbb_filesystem(), self::$config['table_prefix'] . 'ext', dirname(__FILE__) . '/', - '.' . $php_ext, + $php_ext, $this->get_cache_driver() ); @@ -168,40 +219,117 @@ class phpbb_functional_test_case extends phpbb_test_case self::recreate_database(self::$config); - if (file_exists($phpbb_root_path . "config.$phpEx")) + $config_file = $phpbb_root_path . "config.$phpEx"; + $config_file_dev = $phpbb_root_path . "config_dev.$phpEx"; + $config_file_test = $phpbb_root_path . "config_test.$phpEx"; + + if (file_exists($config_file)) { - if (!file_exists($phpbb_root_path . "config_dev.$phpEx")) + if (!file_exists($config_file_dev)) { - rename($phpbb_root_path . "config.$phpEx", $phpbb_root_path . "config_dev.$phpEx"); + rename($config_file, $config_file_dev); } else { - unlink($phpbb_root_path . "config.$phpEx"); + unlink($config_file); } } - // begin data - $data = array(); + self::$cookieJar = new CookieJar; + self::$client = new Goutte\Client(array(), null, self::$cookieJar); + // Set client manually so we can increase the cURL timeout + self::$client->setClient(new Guzzle\Http\Client('', array( + Guzzle\Http\Client::DISABLE_REDIRECTS => true, + 'curl.options' => array( + CURLOPT_TIMEOUT => 120, + ), + ))); + + // Reset the curl handle because it is 0 at this point and not a valid + // resource + self::$client->getClient()->getCurlMulti()->reset(true); + + $parseURL = parse_url(self::$config['phpbb_functional_url']); + + $crawler = self::request('GET', 'install/index.php?mode=install'); + self::assertContains('Welcome to Installation', $crawler->filter('#main')->text()); + $form = $crawler->selectButton('submit')->form(); + + // install/index.php?mode=install&sub=requirements + $crawler = self::submit($form); + self::assertContains('Installation compatibility', $crawler->filter('#main')->text()); + $form = $crawler->selectButton('submit')->form(); + + // install/index.php?mode=install&sub=database + $crawler = self::submit($form); + self::assertContains('Database configuration', $crawler->filter('#main')->text()); + $form = $crawler->selectButton('submit')->form(array( + // Installer uses 3.0-style dbms name + 'dbms' => str_replace('phpbb_db_driver_', '', self::$config['dbms']), + 'dbhost' => self::$config['dbhost'], + 'dbport' => self::$config['dbport'], + 'dbname' => self::$config['dbname'], + 'dbuser' => self::$config['dbuser'], + 'dbpasswd' => self::$config['dbpasswd'], + 'table_prefix' => self::$config['table_prefix'], + )); - $data = array_merge($data, self::$config); + // install/index.php?mode=install&sub=database + $crawler = self::submit($form); + self::assertContains('Successful connection', $crawler->filter('#main')->text()); + $form = $crawler->selectButton('submit')->form(); - $data = array_merge($data, array( + // install/index.php?mode=install&sub=administrator + $crawler = self::submit($form); + self::assertContains('Administrator configuration', $crawler->filter('#main')->text()); + $form = $crawler->selectButton('submit')->form(array( 'default_lang' => 'en', 'admin_name' => 'admin', - 'admin_pass1' => 'admin', - 'admin_pass2' => 'admin', + 'admin_pass1' => 'adminadmin', + 'admin_pass2' => 'adminadmin', 'board_email' => 'nobody@example.com', )); - $parseURL = parse_url(self::$config['phpbb_functional_url']); + // install/index.php?mode=install&sub=administrator + $crawler = self::submit($form); + self::assertContains('Tests passed', $crawler->filter('#main')->text()); + $form = $crawler->selectButton('submit')->form(); + + // We have to skip install/index.php?mode=install&sub=config_file + // because that step will create a config.php file if phpBB has the + // permission to do so. We have to create the config file on our own + // in order to get the DEBUG constants defined. + $config_php_data = phpbb_create_config_file_data(self::$config, self::$config['dbms'], true, true); + $config_created = file_put_contents($config_file, $config_php_data) !== false; + if (!$config_created) + { + self::markTestSkipped("Could not write $config_file file."); + } - $data = array_merge($data, array( - 'email_enable' => false, - 'smtp_delivery' => false, - 'smtp_host' => '', - 'smtp_auth' => '', - 'smtp_user' => '', - 'smtp_pass' => '', + // We also have to create a install lock that is normally created by + // the installer. The file will be removed by the final step of the + // installer. + $install_lock_file = $phpbb_root_path . 'cache/install_lock'; + $lock_created = file_put_contents($install_lock_file, '') !== false; + if (!$lock_created) + { + self::markTestSkipped("Could not create $lock_created file."); + } + @chmod($install_lock_file, 0666); + + // install/index.php?mode=install&sub=advanced + $form_data = $form->getValues(); + unset($form_data['submit']); + + $crawler = self::request('POST', 'install/index.php?mode=install&sub=advanced', $form_data); + self::assertContains('The settings on this page are only necessary to set if you know that you require something different from the default.', $crawler->filter('#main')->text()); + $form = $crawler->selectButton('submit')->form(array( + 'email_enable' => true, + 'smtp_delivery' => true, + 'smtp_host' => 'nxdomain.phpbb.com', + 'smtp_auth' => 'PLAIN', + 'smtp_user' => 'nxuser', + 'smtp_pass' => 'nxpass', 'cookie_secure' => false, 'force_server_vars' => false, 'server_protocol' => $parseURL['scheme'] . '://', @@ -209,48 +337,18 @@ class phpbb_functional_test_case extends phpbb_test_case 'server_port' => isset($parseURL['port']) ? (int) $parseURL['port'] : 80, 'script_path' => $parseURL['path'], )); - // end data - - $content = self::do_request('install'); - self::assertNotSame(false, $content); - self::assertContains('Welcome to Installation', $content); - - // Installer uses 3.0-style dbms name - $data['dbms'] = str_replace('phpbb_db_driver_', '', $data['dbms']); - $content = self::do_request('create_table', $data); - self::assertNotSame(false, $content); - self::assertContains('The database tables used by phpBB', $content); - // 3.0 or 3.1 - self::assertContains('have been created and populated with some initial data.', $content); - - $content = self::do_request('config_file', $data); - self::assertNotSame(false, $content); - self::assertContains('Configuration file', $content); - file_put_contents($phpbb_root_path . "config.$phpEx", phpbb_create_config_file_data($data, self::$config['dbms'], true, true)); - - $content = self::do_request('final', $data); - self::assertNotSame(false, $content); - self::assertContains('You have successfully installed', $content); - copy($phpbb_root_path . "config.$phpEx", $phpbb_root_path . "config_test.$phpEx"); - } - static private function do_request($sub, $post_data = null) - { - $context = null; + // install/index.php?mode=install&sub=create_table + $crawler = self::submit($form); + self::assertContains('The database tables used by phpBB', $crawler->filter('#main')->text()); + self::assertContains('have been created and populated with some initial data.', $crawler->filter('#main')->text()); + $form = $crawler->selectButton('submit')->form(); - if ($post_data) - { - $context = stream_context_create(array( - 'http' => array( - 'method' => 'POST', - 'header' => 'Content-Type: application/x-www-form-urlencoded', - 'content' => http_build_query($post_data), - 'ignore_errors' => true, - ), - )); - } + // install/index.php?mode=install&sub=final + $crawler = self::submit($form); + self::assertContains('You have successfully installed', $crawler->text()); - return file_get_contents(self::$config['phpbb_functional_url'] . 'install/index.php?mode=install&sub=' . $sub, false, $context); + copy($config_file, $config_file_test); } static private function recreate_database($config) @@ -311,24 +409,107 @@ class phpbb_functional_test_case extends phpbb_test_case 'user_lang' => 'en', 'user_timezone' => 0, 'user_dateformat' => '', - 'user_password' => phpbb_hash($username), + 'user_password' => phpbb_hash($username . $username), ); return user_add($user_row); } + protected function remove_user_group($group_name, $usernames) + { + global $db, $cache, $auth, $config, $phpbb_dispatcher, $phpbb_log, $phpbb_container, $phpbb_root_path, $phpEx; + + $config = new phpbb_config(array()); + $config['coppa_enable'] = 0; + + $db = $this->get_db(); + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = $this->getMock('phpbb_user'); + $auth = $this->getMock('phpbb_auth'); + + $phpbb_log = new phpbb_log($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE); + $cache = new phpbb_mock_null_cache; + + $cache_driver = new phpbb_cache_driver_null(); + $phpbb_container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $phpbb_container + ->expects($this->any()) + ->method('get') + ->with('cache.driver') + ->will($this->returnValue($cache_driver)); + + if (!function_exists('utf_clean_string')) + { + require_once(__DIR__ . '/../../phpBB/includes/utf/utf_tools.php'); + } + if (!function_exists('group_user_del')) + { + require_once(__DIR__ . '/../../phpBB/includes/functions_user.php'); + } + + $sql = 'SELECT group_id + FROM ' . GROUPS_TABLE . " + WHERE group_name = '" . $db->sql_escape($group_name) . "'"; + $result = $db->sql_query($sql); + $group_id = (int) $db->sql_fetchfield('group_id'); + $db->sql_freeresult($result); + + return group_user_del($group_id, false, $usernames, $group_name); + } + + protected function add_user_group($group_name, $usernames, $default = false, $leader = false) + { + global $db, $cache, $auth, $config, $phpbb_dispatcher, $phpbb_log, $phpbb_container, $phpbb_root_path, $phpEx; + + $config = new phpbb_config(array()); + $config['coppa_enable'] = 0; + + $db = $this->get_db(); + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $user = $this->getMock('phpbb_user'); + $auth = $this->getMock('phpbb_auth'); + + $phpbb_log = new phpbb_log($db, $user, $auth, $phpbb_dispatcher, $phpbb_root_path, 'adm/', $phpEx, LOG_TABLE); + $cache = new phpbb_mock_null_cache; + + $cache_driver = new phpbb_cache_driver_null(); + $phpbb_container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $phpbb_container + ->expects($this->any()) + ->method('get') + ->with('cache.driver') + ->will($this->returnValue($cache_driver)); + + if (!function_exists('utf_clean_string')) + { + require_once(__DIR__ . '/../../phpBB/includes/utf/utf_tools.php'); + } + if (!function_exists('group_user_del')) + { + require_once(__DIR__ . '/../../phpBB/includes/functions_user.php'); + } + + $sql = 'SELECT group_id + FROM ' . GROUPS_TABLE . " + WHERE group_name = '" . $db->sql_escape($group_name) . "'"; + $result = $db->sql_query($sql); + $group_id = (int) $db->sql_fetchfield('group_id'); + $db->sql_freeresult($result); + + return group_user_add($group_id, false, $usernames, $group_name, $default, $leader); + } + protected function login($username = 'admin') { $this->add_lang('ucp'); - $crawler = $this->request('GET', 'ucp.php'); + $crawler = self::request('GET', 'ucp.php'); $this->assertContains($this->lang('LOGIN_EXPLAIN_UCP'), $crawler->filter('html')->text()); $form = $crawler->selectButton($this->lang('LOGIN'))->form(); - $crawler = $this->client->submit($form, array('username' => $username, 'password' => $username)); - $this->assert_response_success(); + $crawler = self::submit($form, array('username' => $username, 'password' => $username . $username)); $this->assertContains($this->lang('LOGIN_REDIRECT'), $crawler->filter('html')->text()); - $cookies = $this->cookieJar->all(); + $cookies = self::$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); @@ -340,6 +521,16 @@ class phpbb_functional_test_case extends phpbb_test_case } } + protected function logout() + { + $this->add_lang('ucp'); + + $crawler = self::request('GET', 'ucp.php?sid=' . $this->sid . '&mode=logout'); + $this->assertContains($this->lang('LOGOUT_REDIRECT'), $crawler->filter('#message')->text()); + unset($this->sid); + + } + /** * Login to the ACP * You must run login() before calling this. @@ -355,7 +546,7 @@ class phpbb_functional_test_case extends phpbb_test_case return; } - $crawler = $this->request('GET', 'adm/index.php?sid=' . $this->sid); + $crawler = self::request('GET', 'adm/index.php?sid=' . $this->sid); $this->assertContains($this->lang('LOGIN_ADMIN_CONFIRM'), $crawler->filter('html')->text()); $form = $crawler->selectButton($this->lang('LOGIN'))->form(); @@ -364,11 +555,10 @@ class phpbb_functional_test_case extends phpbb_test_case { if (strpos($field, 'password_') === 0) { - $crawler = $this->client->submit($form, array('username' => $username, $field => $username)); - $this->assert_response_success(); + $crawler = self::submit($form, array('username' => $username, $field => $username . $username)); $this->assertContains($this->lang('LOGIN_ADMIN_SUCCESS'), $crawler->filter('html')->text()); - $cookies = $this->cookieJar->all(); + $cookies = self::$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); @@ -433,19 +623,38 @@ class phpbb_functional_test_case extends phpbb_test_case $this->assertContains(html_entity_decode($this->lang($needle), ENT_QUOTES), $haystack, $message); } + /* + * Perform some basic assertions for the page + * + * Checks for debug/error output before the actual page content and the status code + * + * @param mixed $status_code Expected status code, false to disable check + * @return null + */ + static public function assert_response_html($status_code = 200) + { + if ($status_code !== false) + { + self::assert_response_status_code($status_code); + } + + // Any output before the doc type means there was an error + $content = self::$client->getResponse()->getContent(); + self::assertStringStartsWith('<!DOCTYPE', trim($content), 'Output found before DOCTYPE specification.'); + } + /** * Heuristic function to check that the response is success. * * When php decides to die with a fatal error, it still sends 200 OK * status code. This assertion tries to catch that. * + * @param int $status_code Expected status code * @return null */ - public function assert_response_success() + static public function assert_response_status_code($status_code = 200) { - $this->assertEquals(200, $this->client->getResponse()->getStatus()); - $content = $this->client->getResponse()->getContent(); - $this->assertNotContains('Fatal error:', $content); + self::assertEquals($status_code, self::$client->getResponse()->getStatus()); } public function assert_filter($crawler, $expr, $msg = null) @@ -463,4 +672,169 @@ class phpbb_functional_test_case extends phpbb_test_case $this->assertGreaterThan(0, count($nodes), $msg); return $nodes; } + + /** + * Asserts that exactly one checkbox with name $name exists within the scope + * of $crawler and that the checkbox is checked. + * + * @param Symfony\Component\DomCrawler\Crawler $crawler + * @param string $name + * @param string $message + * + * @return null + */ + public function assert_checkbox_is_checked($crawler, $name, $message = '') + { + $this->assertSame( + 'checked', + $this->assert_find_one_checkbox($crawler, $name)->attr('checked'), + $message ?: "Failed asserting that checkbox $name is checked." + ); + } + + /** + * Asserts that exactly one checkbox with name $name exists within the scope + * of $crawler and that the checkbox is unchecked. + * + * @param Symfony\Component\DomCrawler\Crawler $crawler + * @param string $name + * @param string $message + * + * @return null + */ + public function assert_checkbox_is_unchecked($crawler, $name, $message = '') + { + $this->assertSame( + '', + $this->assert_find_one_checkbox($crawler, $name)->attr('checked'), + $message ?: "Failed asserting that checkbox $name is unchecked." + ); + } + + /** + * Searches for an input element of type checkbox with the name $name using + * $crawler. Contains an assertion that only one such checkbox exists within + * the scope of $crawler. + * + * @param Symfony\Component\DomCrawler\Crawler $crawler + * @param string $name + * @param string $message + * + * @return Symfony\Component\DomCrawler\Crawler + */ + public function assert_find_one_checkbox($crawler, $name, $message = '') + { + $query = sprintf('//input[@type="checkbox" and @name="%s"]', $name); + $result = $crawler->filterXPath($query); + + $this->assertEquals( + 1, + sizeof($result), + $message ?: 'Failed asserting that exactly one checkbox with name' . + " $name exists in crawler scope." + ); + + return $result; + } + + /** + * Creates a topic + * + * Be sure to login before creating + * + * @param int $forum_id + * @param string $subject + * @param string $message + * @param array $additional_form_data Any additional form data to be sent in the request + * @return array post_id, topic_id + */ + public function create_topic($forum_id, $subject, $message, $additional_form_data = array()) + { + $posting_url = "posting.php?mode=post&f={$forum_id}&sid={$this->sid}"; + + $form_data = array_merge(array( + 'subject' => $subject, + 'message' => $message, + 'post' => true, + ), $additional_form_data); + + return self::submit_post($posting_url, 'POST_TOPIC', $form_data); + } + + /** + * Creates a post + * + * Be sure to login before creating + * + * @param int $forum_id + * @param string $subject + * @param string $message + * @param array $additional_form_data Any additional form data to be sent in the request + * @return array post_id, topic_id + */ + public function create_post($forum_id, $topic_id, $subject, $message, $additional_form_data = array()) + { + $posting_url = "posting.php?mode=reply&f={$forum_id}&t={$topic_id}&sid={$this->sid}"; + + $form_data = array_merge(array( + 'subject' => $subject, + 'message' => $message, + 'post' => true, + ), $additional_form_data); + + return self::submit_post($posting_url, 'POST_REPLY', $form_data); + } + + /** + * Helper for submitting posts + * + * @param string $posting_url + * @param string $posting_contains + * @param array $form_data + * @return array post_id, topic_id + */ + protected function submit_post($posting_url, $posting_contains, $form_data) + { + $this->add_lang('posting'); + + $crawler = self::request('GET', $posting_url); + $this->assertContains($this->lang($posting_contains), $crawler->filter('html')->text()); + + $hidden_fields = array( + $crawler->filter('[type="hidden"]')->each(function ($node, $i) { + return array('name' => $node->getAttribute('name'), 'value' => $node->getAttribute('value')); + }), + ); + + foreach ($hidden_fields as $fields) + { + foreach($fields as $field) + { + $form_data[$field['name']] = $field['value']; + } + } + + // Bypass time restriction that said that if the lastclick time (i.e. time when the form was opened) + // is not at least 2 seconds before submission, cancel the form + $form_data['lastclick'] = 0; + + // I use a request because the form submission method does not allow you to send data that is not + // contained in one of the actual form fields that the browser sees (i.e. it ignores "hidden" inputs) + // Instead, I send it as a request with the submit button "post" set to true. + $crawler = self::request('POST', $posting_url, $form_data); + $this->assertContains($this->lang('POST_STORED'), $crawler->filter('html')->text()); + + $url = $crawler->selectLink($this->lang('VIEW_MESSAGE', '', ''))->link()->getUri(); + + $matches = $topic_id = $post_id = false; + preg_match_all('#&t=([0-9]+)(&p=([0-9]+))?#', $url, $matches); + + $topic_id = (int) (isset($matches[1][0])) ? $matches[1][0] : 0; + $post_id = (int) (isset($matches[3][0])) ? $matches[3][0] : 0; + + return array( + 'topic_id' => $topic_id, + 'post_id' => $post_id, + ); + } } diff --git a/tests/test_framework/phpbb_test_case_helpers.php b/tests/test_framework/phpbb_test_case_helpers.php index 20ae384f21..50b2bf03ec 100644 --- a/tests/test_framework/phpbb_test_case_helpers.php +++ b/tests/test_framework/phpbb_test_case_helpers.php @@ -18,6 +18,56 @@ class phpbb_test_case_helpers $this->test_case = $test_case; } + /** + * This should only be called once before the tests are run. + * This is used to copy the fixtures to the phpBB install + */ + public function copy_ext_fixtures($fixtures_dir, $fixtures) + { + global $phpbb_root_path; + + if (file_exists($phpbb_root_path . 'ext/')) + { + // First, move any extensions setup on the board to a temp directory + $this->copy_dir($phpbb_root_path . 'ext/', $phpbb_root_path . 'store/temp_ext/'); + + // Then empty the ext/ directory on the board (for accurate test cases) + $this->empty_dir($phpbb_root_path . 'ext/'); + } + + // Copy our ext/ files from the test case to the board + foreach ($fixtures as $fixture) + { + $this->copy_dir($fixtures_dir . $fixture, $phpbb_root_path . 'ext/' . $fixture); + } + } + + /** + * This should only be called once after the tests are run. + * This is used to remove the fixtures from the phpBB install + */ + public function restore_original_ext_dir() + { + global $phpbb_root_path; + + // Remove all of the files we copied from test ext -> board ext + $this->empty_dir($phpbb_root_path . 'ext/'); + + // Copy back the board installed extensions from the temp directory + if (file_exists($phpbb_root_path . 'store/temp_ext/')) + { + $this->copy_dir($phpbb_root_path . 'store/temp_ext/', $phpbb_root_path . 'ext/'); + + // Remove all of the files we copied from board ext -> temp_ext + $this->empty_dir($phpbb_root_path . 'store/temp_ext/'); + } + + if (file_exists($phpbb_root_path . 'store/temp_ext/')) + { + $this->empty_dir($phpbb_root_path . 'store/temp_ext/'); + } + } + public function setExpectedTriggerError($errno, $message = '') { $exceptionName = ''; @@ -203,27 +253,6 @@ class phpbb_test_case_helpers } /** - * Remove files/directories that are listed in an array - * Designed for use with $this->copy_dir() - * - * @param array $file_list - */ - public function remove_files($file_list) - { - foreach ($file_list as $file) - { - if (is_dir($file)) - { - rmdir($file); - } - else - { - unlink($file); - } - } - } - - /** * Empty directory (remove any subdirectories/files below) * * @param array $file_list diff --git a/tests/tree/fixtures/phpbb_forums.xml b/tests/tree/fixtures/phpbb_forums.xml new file mode 100644 index 0000000000..8f133078a9 --- /dev/null +++ b/tests/tree/fixtures/phpbb_forums.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_forums"> + <column>forum_id</column> + <column>parent_id</column> + <column>left_id</column> + <column>right_id</column> + <column>forum_parents</column> + <column>forum_name</column> + <column>forum_desc</column> + <column>forum_rules</column> + </table> +</dataset> diff --git a/tests/tree/nestedset_forum_base.php b/tests/tree/nestedset_forum_base.php new file mode 100644 index 0000000000..43680609f8 --- /dev/null +++ b/tests/tree/nestedset_forum_base.php @@ -0,0 +1,115 @@ +<?php +/** +* +* @package tree +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +class phpbb_tests_tree_nestedset_forum_base extends phpbb_database_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/phpbb_forums.xml'); + } + + protected $forum_data = array( + // \__/ + 1 => array('forum_id' => 1, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + 2 => array('forum_id' => 2, 'parent_id' => 1, 'user_id' => 0, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + 3 => array('forum_id' => 3, 'parent_id' => 1, 'user_id' => 0, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + // \ / + // \/ + 4 => array('forum_id' => 4, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => 'a:0:{}'), + 5 => array('forum_id' => 5, 'parent_id' => 4, 'user_id' => 0, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + 6 => array('forum_id' => 6, 'parent_id' => 5, 'user_id' => 0, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + + // \_ _/ + // \/ + 7 => array('forum_id' => 7, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + 8 => array('forum_id' => 8, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + 9 => array('forum_id' => 9, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + 10 => array('forum_id' => 10, 'parent_id' => 9, 'user_id' => 0, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + 11 => array('forum_id' => 11, 'parent_id' => 7, 'user_id' => 0, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'), + + // Non-existent forums + 0 => array('forum_id' => 0, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 0, 'right_id' => 0, 'forum_parents' => 'a:0:{}'), + 200 => array('forum_id' => 200, 'parent_id' => 0, 'user_id' => 0, 'left_id' => 0, 'right_id' => 0, 'forum_parents' => 'a:0:{}'), + ); + + protected $set, + $config, + $lock, + $db; + + public function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + + global $config; + + $config = $this->config = new phpbb_config(array('nestedset_forum_lock' => 0)); + set_config(null, null, null, $this->config); + + $this->lock = new phpbb_lock_db('nestedset_forum_lock', $this->config, $this->db); + $this->set = new phpbb_tree_nestedset_forum($this->db, $this->lock, 'phpbb_forums'); + + $this->set_up_forums(); + } + + protected function set_up_forums() + { + static $forums; + + if (empty($forums)) + { + $this->create_forum('Parent with two flat children'); + $this->create_forum('Flat child #1', 1); + $this->create_forum('Flat child #2', 1); + + $this->create_forum('Parent with two nested children'); + $this->create_forum('Nested child #1', 4); + $this->create_forum('Nested child #2', 5); + + $this->create_forum('Parent with flat and nested children'); + $this->create_forum('Mixed child #1', 7); + $this->create_forum('Mixed child #2', 7); + $this->create_forum('Nested child #1 of Mixed child #2', 9); + $this->create_forum('Mixed child #3', 7); + + // Updating forum_parents column here so it's not empty + // This is required, so we can see whether the methods + // correctly clear the values. + $sql = "UPDATE phpbb_forums + SET forum_parents = 'a:0:{}'"; + $this->db->sql_query($sql); + + // Copy the forums into a static array, so we can reuse the list later + $sql = 'SELECT * + FROM phpbb_forums'; + $result = $this->db->sql_query($sql); + $forums = $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + } + else + { + $buffer = new phpbb_db_sql_insert_buffer($this->db, 'phpbb_forums'); + $buffer->insert_all($forums); + $buffer->flush(); + + $this->database_synchronisation(array( + 'phpbb_forums' => array('forum_id'), + )); + } + } + + protected function create_forum($name, $parent_id = 0) + { + $forum = $this->set->insert(array('forum_name' => $name, 'forum_desc' => '', 'forum_rules' => '')); + $this->set->change_parent($forum['forum_id'], $parent_id); + } +} diff --git a/tests/tree/nestedset_forum_get_data_test.php b/tests/tree/nestedset_forum_get_data_test.php new file mode 100644 index 0000000000..ca1863e55e --- /dev/null +++ b/tests/tree/nestedset_forum_get_data_test.php @@ -0,0 +1,119 @@ +<?php +/** +* +* @package tree +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/nestedset_forum_base.php'; + +class phpbb_tests_tree_nestedset_forum_get_data_test extends phpbb_tests_tree_nestedset_forum_base +{ + public function get_path_and_subtree_data_data() + { + return array( + array(1, true, true, array(1, 2, 3)), + array(1, true, false, array(2, 3)), + array(1, false, true, array(3, 2, 1)), + array(1, false, false, array(3, 2)), + + array(2, true, true, array(1, 2)), + array(2, true, false, array(1)), + array(2, false, true, array(2, 1)), + array(2, false, false, array(1)), + + array(5, true, true, array(4, 5, 6)), + array(5, true, false, array(4, 6)), + array(5, false, true, array(6, 5, 4)), + array(5, false, false, array(6, 4)), + ); + } + + /** + * @dataProvider get_path_and_subtree_data_data + */ + public function test_get_path_and_subtree_data($forum_id, $order_asc, $include_item, $expected) + { + $this->assertEquals($expected, array_keys($this->set->get_path_and_subtree_data($forum_id, $order_asc, $include_item))); + } + + public function get_path_data_data() + { + return array( + array(1, true, true, array(1)), + array(1, true, false, array()), + array(1, false, true, array(1)), + array(1, false, false, array()), + + array(2, true, true, array(1, 2)), + array(2, true, false, array(1)), + array(2, false, true, array(2, 1)), + array(2, false, false, array(1)), + + array(5, true, true, array(4, 5)), + array(5, true, false, array(4)), + array(5, false, true, array(5, 4)), + array(5, false, false, array(4)), + ); + } + + /** + * @dataProvider get_path_data_data + */ + public function test_get_path_data($forum_id, $order_asc, $include_item, $expected) + { + $this->assertEquals($expected, array_keys($this->set->get_path_data($forum_id, $order_asc, $include_item))); + } + + public function get_subtree_data_data() + { + return array( + array(1, true, true, array(1, 2, 3)), + array(1, true, false, array(2, 3)), + array(1, false, true, array(3, 2, 1)), + array(1, false, false, array(3, 2)), + + array(2, true, true, array(2)), + array(2, true, false, array()), + array(2, false, true, array(2)), + array(2, false, false, array()), + + array(5, true, true, array(5, 6)), + array(5, true, false, array(6)), + array(5, false, true, array(6, 5)), + array(5, false, false, array(6)), + ); + } + + /** + * @dataProvider get_subtree_data_data + */ + public function test_get_subtree_data($forum_id, $order_asc, $include_item, $expected) + { + $this->assertEquals($expected, array_keys($this->set->get_subtree_data($forum_id, $order_asc, $include_item))); + } + + public function get_path_basic_data_data() + { + return array( + array(1, '', array()), + array(1, serialize(array()), array()), + array(2, '', array(1)), + array(2, serialize(array(1 => array())), array(1)), + array(10, '', array(7, 9)), + array(10, serialize(array(7 => array(), 9 => array())), array(7, 9)), + ); + } + + /** + * @dataProvider get_path_basic_data_data + */ + public function test_get_path_basic_data($forum_id, $forum_parents, $expected) + { + $forum_data = $this->forum_data[$forum_id]; + $forum_data['forum_parents'] = $forum_parents; + $this->assertEquals($expected, array_keys($this->set->get_path_basic_data($forum_data))); + } +} diff --git a/tests/tree/nestedset_forum_insert_delete_test.php b/tests/tree/nestedset_forum_insert_delete_test.php new file mode 100644 index 0000000000..d0e9e02c2e --- /dev/null +++ b/tests/tree/nestedset_forum_insert_delete_test.php @@ -0,0 +1,120 @@ +<?php +/** +* +* @package tree +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/nestedset_forum_base.php'; + +class phpbb_tests_tree_nestedset_forum_add_remove_test extends phpbb_tests_tree_nestedset_forum_base +{ + public function delete_data() + { + return array( + array(1, array(1, 2, 3), array( + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 16), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 8, 'right_id' => 9), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 13), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 11, 'right_id' => 12), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + )), + array(2, array(2), array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 4), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 5, 'right_id' => 10), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 6, 'right_id' => 9), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 7, 'right_id' => 8), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 20), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19), + )), + ); + } + + /** + * @dataProvider delete_data + */ + public function test_delete($forum_id, $expected_deleted, $expected) + { + $this->assertEquals($expected_deleted, $this->set->delete($forum_id)); + + $result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id + FROM phpbb_forums + ORDER BY left_id, forum_id ASC"); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } + + public function delete_throws_data() + { + return array( + array('Not an item', 0), + array('Item does not exist', 200), + ); + } + + /** + * @dataProvider delete_throws_data + * + * @expectedException OutOfBoundsException + * @expectedExceptionMessage FORUM_NESTEDSET_INVALID_ITEM + */ + public function test_delete_throws($explain, $forum_id) + { + $this->set->delete($forum_id); + } + + public function insert_data() + { + return array( + array(array( + 'forum_desc' => '', + 'forum_rules' => '', + 'forum_id' => 12, + 'parent_id' => 0, + 'left_id' => 23, + 'right_id' => 24, + 'forum_parents' => '', + ), array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + + array('forum_id' => 12, 'parent_id' => 0, 'left_id' => 23, 'right_id' => 24), + )), + ); + } + + /** + * @dataProvider insert_data + */ + public function test_insert($expected_data, $expected) + { + $this->assertEquals($expected_data, $this->set->insert(array( + 'forum_desc' => '', + 'forum_rules' => '', + ))); + + $result = $this->db->sql_query('SELECT forum_id, parent_id, left_id, right_id + FROM phpbb_forums + ORDER BY left_id, forum_id ASC'); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } +} diff --git a/tests/tree/nestedset_forum_move_test.php b/tests/tree/nestedset_forum_move_test.php new file mode 100644 index 0000000000..fe506c8278 --- /dev/null +++ b/tests/tree/nestedset_forum_move_test.php @@ -0,0 +1,569 @@ +<?php +/** +* +* @package tree +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/nestedset_forum_base.php'; + +class phpbb_tests_tree_nestedset_forum_move_test extends phpbb_tests_tree_nestedset_forum_base +{ + public function move_data() + { + return array( + array('Move first item up', + 1, 1, false, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5), + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + array('Move last item down', + 7, -1, false, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5), + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + array('Move first item down', + 1, -1, true, array( + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4), + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 8, 'right_id' => 9), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 10, 'right_id' => 11), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + array('Move second item up', + 4, 1, true, array( + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4), + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 8, 'right_id' => 9), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 10, 'right_id' => 11), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + array('Move last item up', + 7, 1, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 16), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 8, 'right_id' => 9), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 13), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 11, 'right_id' => 12), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 17, 'right_id' => 22), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 18, 'right_id' => 21), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20), + )), + array('Move last item up by 2', + 7, 2, true, array( + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 10), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 4, 'right_id' => 7), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 5, 'right_id' => 6), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 8, 'right_id' => 9), + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 16), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 12, 'right_id' => 13), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 17, 'right_id' => 22), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 18, 'right_id' => 21), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20), + )), + array('Move last item up by 100', + 7, 100, true, array( + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 10), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 4, 'right_id' => 7), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 5, 'right_id' => 6), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 8, 'right_id' => 9), + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 16), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 12, 'right_id' => 13), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 17, 'right_id' => 22), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 18, 'right_id' => 21), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20), + )), + ); + } + + /** + * @dataProvider move_data + */ + public function test_move($explain, $forum_id, $delta, $expected_moved, $expected) + { + $this->assertEquals($expected_moved, $this->set->move($forum_id, $delta)); + + $result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id + FROM phpbb_forums + ORDER BY left_id, forum_id ASC"); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } + + public function move_down_data() + { + return array( + array('Move last item down', + 7, false, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5), + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + array('Move first item down', + 1, true, array( + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4), + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 8, 'right_id' => 9), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 10, 'right_id' => 11), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + ); + } + + /** + * @dataProvider move_down_data + */ + public function test_move_down($explain, $forum_id, $expected_moved, $expected) + { + $this->assertEquals($expected_moved, $this->set->move_down($forum_id)); + + $result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id + FROM phpbb_forums + ORDER BY left_id, forum_id ASC"); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } + + public function move_up_data() + { + return array( + array('Move first item up', + 1, false, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5), + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + array('Move second item up', + 4, true, array( + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 2, 'right_id' => 5), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 3, 'right_id' => 4), + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 8, 'right_id' => 9), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 10, 'right_id' => 11), + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + ); + } + + /** + * @dataProvider move_up_data + */ + public function test_move_up($explain, $forum_id, $expected_moved, $expected) + { + $this->assertEquals($expected_moved, $this->set->move_up($forum_id)); + + $result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id + FROM phpbb_forums + ORDER BY left_id, forum_id ASC"); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } + + public function move_children_data() + { + return array( + array('Item has no children', + 2, 1, false, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'), + )), + array('Move to same parent', + 4, 4, false, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'), + )), + array('Move single child up', + 5, 1, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 8, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 6, 'parent_id' => 1, 'left_id' => 6, 'right_id' => 7, 'forum_parents' => ''), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 12, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'), + )), + array('Move nested children up', + 4, 1, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 1, 'left_id' => 6, 'right_id' => 9, 'forum_parents' => ''), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => ''), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 12, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'), + )), + array('Move single child down', + 5, 7, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 9, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 6, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''), + + )), + array('Move nested children down', + 4, 7, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 13, 'right_id' => 14, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 17, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 21, 'forum_parents' => ''), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20, 'forum_parents' => ''), + )), + array('Move single child to parent 0', + 5, 0, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 9, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 20, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 6, 'parent_id' => 0, 'left_id' => 21, 'right_id' => 22, 'forum_parents' => ''), + )), + array('Move nested children to parent 0', + 4, 0, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 13, 'right_id' => 14, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 17, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 5, 'parent_id' => 0, 'left_id' => 19, 'right_id' => 22, 'forum_parents' => ''), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''), + )), + ); + } + + /** + * @dataProvider move_children_data + */ + public function test_move_children($explain, $forum_id, $target_id, $expected_moved, $expected) + { + $this->assertEquals($expected_moved, $this->set->move_children($forum_id, $target_id)); + + $result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id, forum_parents + FROM phpbb_forums + ORDER BY left_id, forum_id ASC"); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } + + public function move_children_throws_item_data() + { + return array( + array('Item 0 does not exist', 0, 5), + array('Item does not exist', 200, 5), + ); + } + + /** + * @dataProvider move_children_throws_item_data + * + * @expectedException OutOfBoundsException + * @expectedExceptionMessage FORUM_NESTEDSET_INVALID_ITEM + */ + public function test_move_children_throws_item($explain, $forum_id, $target_id) + { + $this->set->move_children($forum_id, $target_id); + } + + public function move_children_throws_parent_data() + { + return array( + array('New parent is child', 4, 5), + array('New parent is child 2', 7, 9), + array('New parent does not exist', 1, 200), + ); + } + + /** + * @dataProvider move_children_throws_parent_data + * + * @expectedException OutOfBoundsException + * @expectedExceptionMessage FORUM_NESTEDSET_INVALID_PARENT + */ + public function test_move_children_throws_parent($explain, $forum_id, $target_id) + { + $this->set->move_children($forum_id, $target_id); + } + + public function change_parent_data() + { + return array( + array('Move single child up', + 6, 1, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 8, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 6, 'parent_id' => 1, 'left_id' => 6, 'right_id' => 7, 'forum_parents' => ''), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 12, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'), + )), + array('Move nested children up', + 5, 1, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 1, 'left_id' => 6, 'right_id' => 9, 'forum_parents' => ''), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => ''), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 12, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => 'a:0:{}'), + )), + array('Move single child down', + 6, 7, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 9, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 6, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''), + )), + array('Move nested children down', + 5, 7, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 22, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 13, 'right_id' => 14, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 17, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 21, 'forum_parents' => ''), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 19, 'right_id' => 20, 'forum_parents' => ''), + )), + array('Move single child to parent 0', + 6, 0, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 10, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 9, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 11, 'right_id' => 20, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 13, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 17, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 15, 'right_id' => 16, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 18, 'right_id' => 19, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 6, 'parent_id' => 0, 'left_id' => 21, 'right_id' => 22, 'forum_parents' => ''), + )), + array('Move nested children to parent 0', + 5, 0, true, array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 8, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 9, 'right_id' => 18, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 10, 'right_id' => 11, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 12, 'right_id' => 15, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 13, 'right_id' => 14, 'forum_parents' => 'a:0:{}'), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 17, 'forum_parents' => 'a:0:{}'), + + array('forum_id' => 5, 'parent_id' => 0, 'left_id' => 19, 'right_id' => 22, 'forum_parents' => ''), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''), + )), + ); + } + + /** + * @dataProvider change_parent_data + */ + public function test_change_parent($explain, $forum_id, $target_id, $expected_moved, $expected) + { + $this->assertEquals($expected_moved, $this->set->change_parent($forum_id, $target_id)); + + $result = $this->db->sql_query("SELECT forum_id, parent_id, left_id, right_id, forum_parents + FROM phpbb_forums + ORDER BY left_id, forum_id ASC"); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } + + public function change_parent_throws_item_data() + { + return array( + array('Item 0 does not exist', 0, 5), + array('Item does not exist', 200, 5), + ); + } + + /** + * @dataProvider change_parent_throws_item_data + * + * @expectedException OutOfBoundsException + * @expectedExceptionMessage FORUM_NESTEDSET_INVALID_ITEM + */ + public function test_change_parent_throws_item($explain, $forum_id, $target_id) + { + $this->set->change_parent($forum_id, $target_id); + } + + public function change_parent_throws_parent_data() + { + return array( + array('New parent is child', 4, 5), + array('New parent is child 2', 7, 9), + array('New parent does not exist', 1, 200), + ); + } + + /** + * @dataProvider change_parent_throws_parent_data + * + * @expectedException OutOfBoundsException + * @expectedExceptionMessage FORUM_NESTEDSET_INVALID_PARENT + */ + public function test_change_parent_throws_parent($explain, $forum_id, $target_id) + { + $this->set->change_parent($forum_id, $target_id); + } +} diff --git a/tests/tree/nestedset_forum_regenerate_test.php b/tests/tree/nestedset_forum_regenerate_test.php new file mode 100644 index 0000000000..38338dbc4d --- /dev/null +++ b/tests/tree/nestedset_forum_regenerate_test.php @@ -0,0 +1,72 @@ +<?php +/** +* +* @package tree +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/nestedset_forum_base.php'; + +class phpbb_tests_tree_nestedset_forum_regenerate_test extends phpbb_tests_tree_nestedset_forum_base +{ + protected $fixed_set = array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6, 'forum_parents' => ''), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3, 'forum_parents' => ''), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5, 'forum_parents' => ''), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12, 'forum_parents' => ''), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11, 'forum_parents' => ''), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10, 'forum_parents' => ''), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22, 'forum_parents' => ''), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15, 'forum_parents' => ''), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19, 'forum_parents' => ''), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18, 'forum_parents' => ''), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21, 'forum_parents' => ''), + ); + + public function regenerate_left_right_ids_data() + { + return array( + array('UPDATE phpbb_forums + SET left_id = 0, + right_id = 0', false), + array('UPDATE phpbb_forums + SET left_id = 28, + right_id = 28 + WHERE left_id > 12', false), + array('UPDATE phpbb_forums + SET left_id = left_id * 2, + right_id = right_id * 2', false), + array('UPDATE phpbb_forums + SET left_id = left_id * 2, + right_id = right_id * 2 + WHERE left_id > 12', false), + array('UPDATE phpbb_forums + SET left_id = left_id - 4, + right_id = right_id * 4 + WHERE left_id > 4', false), + array('UPDATE phpbb_forums + SET left_id = 0, + right_id = 0 + WHERE left_id > 12', true), + ); + } + + /** + * @dataProvider regenerate_left_right_ids_data + */ + public function test_regenerate_left_right_ids($breaking_query, $reset_ids) + { + $result = $this->db->sql_query($breaking_query); + + $this->assertEquals(23, $this->set->regenerate_left_right_ids(1, 0, $reset_ids)); + + $result = $this->db->sql_query('SELECT forum_id, parent_id, left_id, right_id, forum_parents + FROM phpbb_forums + ORDER BY left_id, forum_id ASC'); + $this->assertEquals($this->fixed_set, $this->db->sql_fetchrowset($result)); + } +} diff --git a/tests/tree/nestedset_forum_test.php b/tests/tree/nestedset_forum_test.php new file mode 100644 index 0000000000..516c794ffc --- /dev/null +++ b/tests/tree/nestedset_forum_test.php @@ -0,0 +1,116 @@ +<?php +/** +* +* @package tree +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +require_once dirname(__FILE__) . '/nestedset_forum_base.php'; + +class pphpbb_tests_tree_nestedset_forum_test extends phpbb_tests_tree_nestedset_forum_base +{ + public function forum_constructor_data() + { + return array( + array(array( + array('forum_id' => 1, 'parent_id' => 0, 'left_id' => 1, 'right_id' => 6), + array('forum_id' => 2, 'parent_id' => 1, 'left_id' => 2, 'right_id' => 3), + array('forum_id' => 3, 'parent_id' => 1, 'left_id' => 4, 'right_id' => 5), + + array('forum_id' => 4, 'parent_id' => 0, 'left_id' => 7, 'right_id' => 12), + array('forum_id' => 5, 'parent_id' => 4, 'left_id' => 8, 'right_id' => 11), + array('forum_id' => 6, 'parent_id' => 5, 'left_id' => 9, 'right_id' => 10), + + array('forum_id' => 7, 'parent_id' => 0, 'left_id' => 13, 'right_id' => 22), + array('forum_id' => 8, 'parent_id' => 7, 'left_id' => 14, 'right_id' => 15), + array('forum_id' => 9, 'parent_id' => 7, 'left_id' => 16, 'right_id' => 19), + array('forum_id' => 10, 'parent_id' => 9, 'left_id' => 17, 'right_id' => 18), + array('forum_id' => 11, 'parent_id' => 7, 'left_id' => 20, 'right_id' => 21), + )), + ); + } + + /** + * @dataProvider forum_constructor_data + */ + public function test_forum_constructor($expected) + { + $result = $this->db->sql_query('SELECT forum_id, parent_id, left_id, right_id + FROM phpbb_forums + ORDER BY left_id, forum_id ASC'); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } + + public function get_sql_where_data() + { + return array( + array('SELECT forum_id + FROM phpbb_forums + %s + ORDER BY forum_id ASC', + 'WHERE', '', array( + array('forum_id' => 1), + array('forum_id' => 2), + array('forum_id' => 3), + + array('forum_id' => 4), + array('forum_id' => 5), + array('forum_id' => 6), + + array('forum_id' => 7), + array('forum_id' => 8), + array('forum_id' => 9), + array('forum_id' => 10), + array('forum_id' => 11), + )), + array('SELECT f.forum_id + FROM phpbb_forums f + %s + ORDER BY f.forum_id ASC', + 'WHERE', 'f.', array( + array('forum_id' => 1), + array('forum_id' => 2), + array('forum_id' => 3), + + array('forum_id' => 4), + array('forum_id' => 5), + array('forum_id' => 6), + + array('forum_id' => 7), + array('forum_id' => 8), + array('forum_id' => 9), + array('forum_id' => 10), + array('forum_id' => 11), + )), + array('SELECT forum_id + FROM phpbb_forums + WHERE forum_id < 4 %s + ORDER BY forum_id ASC', + 'AND', '', array( + array('forum_id' => 1), + array('forum_id' => 2), + array('forum_id' => 3), + )), + array('SELECT f.forum_id + FROM phpbb_forums f + WHERE f.forum_id < 4 %s + ORDER BY f.forum_id ASC', + 'AND', 'f.', array( + array('forum_id' => 1), + array('forum_id' => 2), + array('forum_id' => 3), + )), + ); + } + + /** + * @dataProvider get_sql_where_data + */ + public function test_get_sql_where($sql_query, $operator, $column_prefix, $expected) + { + $result = $this->db->sql_query(sprintf($sql_query, $this->set->get_sql_where($operator, $column_prefix))); + $this->assertEquals($expected, $this->db->sql_fetchrowset($result)); + } +} diff --git a/tests/user/fixtures/user_loader.xml b/tests/user/fixtures/user_loader.xml index 737376f326..1fed8b5838 100644 --- a/tests/user/fixtures/user_loader.xml +++ b/tests/user/fixtures/user_loader.xml @@ -2,22 +2,38 @@ <dataset> <table name="phpbb_users"> <column>user_id</column> + <column>user_permissions</column> <column>username</column> <column>username_clean</column> + <column>user_sig</column> + <column>user_occ</column> + <column>user_interests</column> <row> <value>1</value> + <value></value> <value>Guest</value> <value>guest</value> + <value></value> + <value></value> + <value></value> </row> <row> <value>2</value> + <value></value> <value>Admin</value> <value>admin</value> + <value></value> + <value></value> + <value></value> </row> <row> <value>3</value> + <value></value> <value>Test</value> <value>test</value> + <value></value> + <value></value> + <value></value> </row> </table> </dataset> diff --git a/tests/user/user_loader.php b/tests/user/user_loader.php deleted file mode 100644 index 0beb804729..0000000000 --- a/tests/user/user_loader.php +++ /dev/null @@ -1,49 +0,0 @@ -<?php -/** -* -* @package testing -* @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 -* -*/ - -include_once(__DIR__ . '/../../phpBB/includes/utf/utf_tools.php'); - -class phpbb_user_lang_test extends phpbb_database_test_case -{ - public function getDataSet() - { - return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/user_loader.xml'); - } - - public function test_user_loader() - { - $db = $this->new_dbal(); - - $user_loader = new phpbb_user_loader($db, __DIR__ . '/../../phpBB/', 'php', 'phpbb_users'); - - $user_loader->load_users(array(2)); - - $user = $user_loader->get_user(1); - $this->assertEquals(1, $user['user_id']); - $this->assertEquals('Guest', $user['username']); - - $user = $user_loader->get_user(2); - $this->assertEquals(2, $user['user_id']); - $this->assertEquals('Admin', $user['username']); - - // Not loaded - $user = $user_loader->get_user(3); - $this->assertEquals(1, $user['user_id']); - $this->assertEquals('Guest', $user['username']); - - $user = $user_loader->get_user(3, true); - $this->assertEquals(3, $user['user_id']); - $this->assertEquals('Test', $user['username']); - - $user_id = $user_loader->load_user_by_username('Test'); - $user = $user_loader->get_user($user_id); - $this->assertEquals(3, $user['user_id']); - $this->assertEquals('Test', $user['username']); - } -} diff --git a/tests/user/user_loader_test.php b/tests/user/user_loader_test.php new file mode 100644 index 0000000000..5cdb654b18 --- /dev/null +++ b/tests/user/user_loader_test.php @@ -0,0 +1,63 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +include_once(__DIR__ . '/../../phpBB/includes/utf/utf_tools.php'); + +class phpbb_user_loader_test extends phpbb_database_test_case +{ + protected $db; + protected $user_loader; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/user_loader.xml'); + } + + public function setUp() + { + parent::setUp(); + + $this->db = $this->new_dbal(); + $this->user_loader = new phpbb_user_loader($this->db, __DIR__ . '/../../phpBB/', 'php', 'phpbb_users'); + } + + public function test_load_get() + { + $this->user_loader->load_users(array(2)); + + $user = $this->user_loader->get_user(1); + $this->assertEquals(1, $user['user_id']); + $this->assertEquals('Guest', $user['username']); + + $user = $this->user_loader->get_user(2); + $this->assertEquals(2, $user['user_id']); + $this->assertEquals('Admin', $user['username']); + } + + public function test_load_get_unloaded() + { + $this->user_loader->load_users(array(2)); + + $user = $this->user_loader->get_user(3); + $this->assertEquals(1, $user['user_id']); + $this->assertEquals('Guest', $user['username']); + + $user = $this->user_loader->get_user(3, true); + $this->assertEquals(3, $user['user_id']); + $this->assertEquals('Test', $user['username']); + } + + public function test_load_user_by_username() + { + $user_id = $this->user_loader->load_user_by_username('Test'); + $user = $this->user_loader->get_user($user_id); + $this->assertEquals(3, $user['user_id']); + $this->assertEquals('Test', $user['username']); + } +} |