diff options
author | Nils Adermann <naderman@naderman.de> | 2013-09-16 01:24:05 +0200 |
---|---|---|
committer | Nils Adermann <naderman@naderman.de> | 2013-09-16 01:24:05 +0200 |
commit | 21bbb5850349326464204bdb1bea7ecf5a88c10a (patch) | |
tree | c2e2ce66583cf94367301fab73e308c9dd8eddb9 /tests | |
parent | bb395bbc50df53bf2e005d95d45f34c7c8934ff0 (diff) | |
parent | ae6f37d559a71fb115cdb954452ebab5fb8fc69f (diff) | |
download | forums-21bbb5850349326464204bdb1bea7ecf5a88c10a.tar forums-21bbb5850349326464204bdb1bea7ecf5a88c10a.tar.gz forums-21bbb5850349326464204bdb1bea7ecf5a88c10a.tar.bz2 forums-21bbb5850349326464204bdb1bea7ecf5a88c10a.tar.xz forums-21bbb5850349326464204bdb1bea7ecf5a88c10a.zip |
Merge remote-tracking branch 'github-phpbb/develop' into ticket/11700
* github-phpbb/develop: (586 commits)
[ticket/11735] Display disabled checkbox in subsilver for read notifications
[ticket/11735] Display disabled checkbox when notification is already read
[ticket/11844] update acp/authentication language var
[ticket/11795] Remove PM popup
[ticket/11795] Remove outdated comment from forum_fn.js
[ticket/11795] Move find user JS to forum_fn
[ticket/11795] Replace TWIG with phpBB syntax in ACP
[ticket/11795] Move MSN scripts to forum_fn.js
[ticket/11795] Use phpBB template syntax instead of TWIG
[ticket/11795] Move PM popup JS to forum_fn.js
[ticket/11795] Get rid of pagination JS variables
[ticket/11795] Get rid of onload_functions
[ticket/11795] Use data-reset-on-edit attr to reset elements
[ticket/11795] Redo form elements auto-focus
[ticket/11811] Remove outline on :focus
[ticket/11836] Fix subsilver fatal error
[ticket/11837] Replace escaped single quote with utf-8 single quote
[ticket/11836] Fix fatal error on unsupported provider for auth link
[ticket/11837] Translate UCP_AUTH_LINK_NOT_SUPPORTED
[ticket/11809] Ensure code.js is first script included after jQuery
...
Conflicts:
phpBB/config/services.yml
phpBB/develop/create_schema_files.php
phpBB/develop/mysql_upgrader.php
phpBB/download/file.php
phpBB/includes/bbcode.php
phpBB/includes/functions_container.php
phpBB/install/database_update.php
phpBB/install/index.php
phpBB/phpbb/controller/helper.php
phpBB/phpbb/controller/resolver.php
phpBB/phpbb/request/request_interface.php
phpBB/phpbb/session.php
phpBB/phpbb/style/extension_path_provider.php
phpBB/phpbb/style/path_provider.php
phpBB/phpbb/style/path_provider_interface.php
phpBB/phpbb/style/resource_locator.php
phpBB/phpbb/style/style.php
phpBB/phpbb/template/locator.php
phpBB/phpbb/template/template.php
phpBB/phpbb/template/twig/node/includeasset.php
phpBB/phpbb/template/twig/node/includecss.php
phpBB/phpbb/template/twig/node/includejs.php
phpBB/phpbb/template/twig/twig.php
tests/controller/helper_url_test.php
tests/di/create_container_test.php
tests/extension/style_path_provider_test.php
tests/notification/notification_test.php
tests/session/continue_test.php
tests/session/creation_test.php
tests/template/template_events_test.php
tests/template/template_test_case.php
tests/template/template_test_case_with_tree.php
tests/test_framework/phpbb_functional_test_case.php
Diffstat (limited to 'tests')
76 files changed, 2440 insertions, 646 deletions
diff --git a/tests/auth/fixtures/oauth_tokens.xml b/tests/auth/fixtures/oauth_tokens.xml new file mode 100644 index 0000000000..9bfb5a4422 --- /dev/null +++ b/tests/auth/fixtures/oauth_tokens.xml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_oauth_tokens"> + <column>user_id</column> + <column>session_id</column> + <column>provider</column> + <column>oauth_token</column> + </table> +</dataset> + diff --git a/tests/auth/provider_oauth_token_storage_test.php b/tests/auth/provider_oauth_token_storage_test.php new file mode 100644 index 0000000000..401f049405 --- /dev/null +++ b/tests/auth/provider_oauth_token_storage_test.php @@ -0,0 +1,207 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2013 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +use OAuth\OAuth2\Token\StdOAuth2Token; + +class phpbb_auth_provider_oauth_token_storage_test extends phpbb_database_test_case +{ + protected $db; + protected $service_name; + protected $session_id; + protected $token_storage; + protected $token_storage_table; + protected $user; + + protected function setup() + { + parent::setUp(); + + global $phpbb_root_path, $phpEx; + + $this->db = $this->new_dbal(); + $this->user = $this->getMock('phpbb_user'); + $this->service_name = 'auth.provider.oauth.service.testing'; + $this->token_storage_table = 'phpbb_oauth_tokens'; + + // Give the user a session_id that we will remember + $this->session_id = '12345'; + $this->user->data['session_id'] = $this->session_id; + + // Set the user id to anonymous + $this->user->data['user_id'] = ANONYMOUS; + + $this->token_storage = new phpbb_auth_provider_oauth_token_storage($this->db, $this->user, $this->token_storage_table); + } + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/oauth_tokens.xml'); + } + + public static function retrieveAccessToken_data() + { + return array( + array(new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES, array('extra' => 'param')), null), + array(null, 'OAuth\Common\Storage\Exception\TokenNotFoundException'), + ); + } + + /** + * @dataProvider retrieveAccessToken_data + */ + public function test_retrieveAccessToken($cache_token, $exception) + { + if ($cache_token) + { + $this->token_storage->storeAccessToken($this->service_name, $cache_token); + $token = $cache_token; + } + + $this->setExpectedException($exception); + + $stored_token = $this->token_storage->retrieveAccessToken($this->service_name); + $this->assertEquals($token, $stored_token); + } + + public function test_retrieveAccessToken_from_db() + { + $expected_token = new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES); + + // Store a token in the database + $temp_storage = new phpbb_auth_provider_oauth_token_storage($this->db, $this->user, $this->token_storage_table); + $temp_storage->storeAccessToken($this->service_name, $expected_token); + unset($temp_storage); + + // Test to see if the token can be retrieved + $stored_token = $this->token_storage->retrieveAccessToken($this->service_name); + $this->assertEquals($expected_token, $stored_token); + } + + /** + * @dataProvider retrieveAccessToken_data + */ + public function test_retrieve_access_token_by_session($cache_token, $exception) + { + if ($cache_token) + { + $this->token_storage->storeAccessToken($this->service_name, $cache_token); + $token = $cache_token; + } + + $this->setExpectedException($exception); + + $stored_token = $this->token_storage->retrieve_access_token_by_session($this->service_name); + $this->assertEquals($token, $stored_token); + } + + public function test_retrieve_access_token_by_session_from_db() + { + $expected_token = new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES); + + // Store a token in the database + $temp_storage = new phpbb_auth_provider_oauth_token_storage($this->db, $this->user, $this->token_storage_table); + $temp_storage->storeAccessToken($this->service_name, $expected_token); + unset($temp_storage); + + // Test to see if the token can be retrieved + $stored_token = $this->token_storage->retrieve_access_token_by_session($this->service_name); + $this->assertEquals($expected_token, $stored_token); + } + + public function test_storeAccessToken() + { + $token = new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES, array('extra' => 'param') ); + $this->token_storage->storeAccessToken($this->service_name, $token); + + // Confirm that the token is cached + $extraParams = $this->token_storage->retrieveAccessToken($this->service_name)->getExtraParams(); + $this->assertEquals( 'param', $extraParams['extra'] ); + $this->assertEquals( 'access', $this->token_storage->retrieveAccessToken($this->service_name)->getAccessToken() ); + + $row = $this->get_token_row_by_session_id($this->session_id); + + // The token is serialized before stored in the database + $this->assertEquals($this->token_storage->json_encode_token($token), $row['oauth_token']); + } + + public static function hasAccessToken_data() + { + return array( + array(null, false), + array(new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES, array('extra' => 'param') ), true), + ); + } + + /** + * @dataProvider hasAccessToken_data + */ + public function test_hasAccessToken($token, $expected) + { + if ($token) + { + $this->token_storage->storeAccessToken($this->service_name, $token); + } + + $has_access_token = $this->token_storage->hasAccessToken($this->service_name); + $this->assertEquals($expected, $has_access_token); + } + + /** + * @dataProvider hasAccessToken_data + */ + public function test_has_access_token_by_session($token, $expected) + { + if ($token) + { + $this->token_storage->storeAccessToken($this->service_name, $token); + } + + $has_access_token = $this->token_storage->has_access_token_by_session($this->service_name); + $this->assertEquals($expected, $has_access_token); + } + + public function test_clearToken() + { + $token = new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES, array('extra' => 'param') ); + $this->token_storage->storeAccessToken($this->service_name, $token); + + $this->token_storage->clearToken($this->service_name); + + // Check that the database has been cleared + $row = $this->get_token_row_by_session_id($this->session_id); + $this->assertFalse($row); + + // Check that the token is no longer in memory + $this->assertFalse($this->token_storage->hasAccessToken($this->service_name)); + } + + public function test_set_user_id() + { + $token = new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES, array('extra' => 'param') ); + $this->token_storage->storeAccessToken($this->service_name, $token); + + $new_user_id = ANONYMOUS + 1; + $this->token_storage->set_user_id($new_user_id); + + $row = $this->get_token_row_by_session_id($this->session_id); + $this->assertEquals($new_user_id, $row['user_id']); + } + + protected function get_token_row_by_session_id($session_id) + { + // Test that the token is stored in the database + $sql = 'SELECT * FROM phpbb_oauth_tokens + WHERE session_id = \'' . $this->db->sql_escape($session_id) . '\''; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + return $row; + } +} diff --git a/tests/controller/helper_url_test.php b/tests/controller/helper_url_test.php index d5d61d40d9..9a75015c9f 100644 --- a/tests/controller/helper_url_test.php +++ b/tests/controller/helper_url_test.php @@ -12,50 +12,92 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; class phpbb_controller_helper_url_test extends phpbb_test_case { - public function helper_url_data() + public function helper_url_data_no_rewrite() { 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'), + array('foo/bar?t=1&f=2', false, true, false, 'app.php/foo/bar?t=1&f=2', 'parameters in url-argument'), + array('foo/bar', 't=1&f=2', true, false, 'app.php/foo/bar?t=1&f=2', 'parameters in params-argument using amp'), + array('foo/bar', 't=1&f=2', false, false, 'app.php/foo/bar?t=1&f=2', 'parameters in params-argument using &'), + array('foo/bar', array('t' => 1, 'f' => 2), true, false, 'app.php/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'), + array('foo/bar', 't=1&f=2', true, 'custom-sid', 'app.php/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)'), + array('foo/bar?t=1&f=2#anchor', false, true, false, 'app.php/foo/bar?t=1&f=2#anchor', 'anchor in url-argument'), + array('foo/bar', 't=1&f=2#anchor', true, false, 'app.php/foo/bar?t=1&f=2#anchor', 'anchor in params-argument'), + array('foo/bar', array('t' => 1, 'f' => 2, '#' => 'anchor'), true, false, 'app.php/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'), + array('foo/bar?t=1&f=2#anchor', false, true, 'custom-sid', 'app.php/foo/bar?t=1&f=2&sid=custom-sid#anchor', 'anchor in url-argument using session_id'), + array('foo/bar', 't=1&f=2#anchor', true, 'custom-sid', 'app.php/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/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'), + array('foo/bar', false, true, false, 'app.php/foo/bar', 'no params using bool false'), + array('foo/bar', '', true, false, 'app.php/foo/bar', 'no params using empty string'), + array('foo/bar', array(), true, false, 'app.php/foo/bar', 'no params using empty array'), ); } /** - * @dataProvider helper_url_data + * @dataProvider helper_url_data_no_rewrite() */ - public function test_helper_url($route, $params, $is_amp, $session_id, $expected, $description) + public function test_helper_url_no_rewrite($route, $params, $is_amp, $session_id, $expected, $description) { global $phpbb_dispatcher, $phpbb_root_path, $phpEx; $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\twig\twig($phpbb_root_path, $phpEx, $config, $this->user, new \phpbb\template\context()); - $this->style_resource_locator = new \phpbb\style\resource_locator(); - $this->style_provider = new \phpbb\style\path_provider(); - $this->style = new \phpbb\style\style($phpbb_root_path, $phpEx, new \phpbb\config\config(array()), $this->user, $this->style_resource_locator, $this->style_provider, $this->template); - $helper = new \phpbb\controller\helper($this->template, $this->user, '', 'php'); + // We don't use mod_rewrite in these tests + $config = new \phpbb\config\config(array('enable_mod_rewrite' => '0')); + $helper = new \phpbb\controller\helper($this->template, $this->user, $config, '', 'php'); + $this->assertEquals($helper->url($route, $params, $is_amp, $session_id), $expected); + } + + public function helper_url_data_with_rewrite() + { + return array( + array('foo/bar?t=1&f=2', false, true, false, 'foo/bar?t=1&f=2', 'parameters in url-argument'), + array('foo/bar', 't=1&f=2', true, false, 'foo/bar?t=1&f=2', 'parameters in params-argument using amp'), + array('foo/bar', 't=1&f=2', false, false, 'foo/bar?t=1&f=2', 'parameters in params-argument using &'), + array('foo/bar', array('t' => 1, 'f' => 2), true, false, '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', '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, 'foo/bar?t=1&f=2#anchor', 'anchor in url-argument'), + array('foo/bar', 't=1&f=2#anchor', true, false, 'foo/bar?t=1&f=2#anchor', 'anchor in params-argument'), + array('foo/bar', array('t' => 1, 'f' => 2, '#' => 'anchor'), true, false, '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', 'foo/bar?t=1&f=2&sid=custom-sid#anchor', 'anchor in url-argument using session_id'), + array('foo/bar', 't=1&f=2#anchor', true, 'custom-sid', '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', '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, 'foo/bar', 'no params using bool false'), + array('foo/bar', '', true, false, 'foo/bar', 'no params using empty string'), + array('foo/bar', array(), true, false, 'foo/bar', 'no params using empty array'), + ); + } + + /** + * @dataProvider helper_url_data_with_rewrite() + */ + public function test_helper_url_with_rewrite($route, $params, $is_amp, $session_id, $expected, $description) + { + global $phpbb_dispatcher, $phpbb_root_path, $phpEx; + + $phpbb_dispatcher = new phpbb_mock_event_dispatcher; + $this->user = $this->getMock('\phpbb\user'); + $this->template = new \phpbb\template\twig\twig($phpbb_root_path, $phpEx, $config, $this->user, new \phpbb\template\context()); + + $config = new \phpbb\config\config(array('enable_mod_rewrite' => '1')); + $helper = new \phpbb\controller\helper($this->template, $this->user, $config, '', 'php'); $this->assertEquals($helper->url($route, $params, $is_amp, $session_id), $expected); } } diff --git a/tests/di/create_container_test.php b/tests/di/create_container_test.php index 8d3dfea780..a3a1ad3597 100644 --- a/tests/di/create_container_test.php +++ b/tests/di/create_container_test.php @@ -19,7 +19,7 @@ namespace $phpbb_root_path = __DIR__ . '/../../phpBB/'; $extensions = array( new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'), - new \phpbb\di\extension\core($phpbb_root_path), + new \phpbb\di\extension\core($phpbb_root_path . 'config'), ); $container = phpbb_create_container($extensions, $phpbb_root_path, 'php'); @@ -31,7 +31,7 @@ namespace $phpbb_root_path = __DIR__ . '/../../phpBB/'; $extensions = array( new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'), - new \phpbb\di\extension\core($phpbb_root_path), + new \phpbb\di\extension\core($phpbb_root_path . 'config'), ); $container = phpbb_create_install_container($phpbb_root_path, 'php'); @@ -45,7 +45,7 @@ namespace $config_file = __DIR__ . '/fixtures/config.php'; $extensions = array( new \phpbb\di\extension\config(__DIR__ . '/fixtures/config.php'), - new \phpbb\di\extension\core($phpbb_root_path), + new \phpbb\di\extension\core($phpbb_root_path . 'config'), ); $container = phpbb_create_compiled_container($config_file, $extensions, array(), $phpbb_root_path, 'php'); diff --git a/tests/extension/style_path_provider_test.php b/tests/extension/style_path_provider_test.php deleted file mode 100644 index f2ee957f4c..0000000000 --- a/tests/extension/style_path_provider_test.php +++ /dev/null @@ -1,50 +0,0 @@ -<?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 deleted file mode 100644 index 1b5ce62e5f..0000000000 --- a/tests/extension/subdir/style_path_provider_test.php +++ /dev/null @@ -1,18 +0,0 @@ -<?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/functional/avatar_acp_groups_test.php b/tests/functional/avatar_acp_groups_test.php new file mode 100644 index 0000000000..9fdc29cc76 --- /dev/null +++ b/tests/functional/avatar_acp_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 + * + */ + +require_once dirname(__FILE__) . '/common_avatar_test.php'; + +/** + * @group functional + */ +class phpbb_functional_avatar_acp_groups_test extends phpbb_functional_common_avatar_test +{ + public function get_url() + { + return 'adm/index.php?i=acp_groups&mode=manage&action=edit&g=5'; + } + + public function avatar_acp_groups_data() + { + return array( + // Correct Gravatar + array( + 'GROUP_UPDATED', + 'avatar_driver_gravatar', + array( + 'avatar_gravatar_email' => 'test@example.com', + 'avatar_gravatar_width' => 80, + 'avatar_gravatar_height' => 80, + ), + ), + // Gravatar with incorrect size + array( + 'The submitted avatar is 120 wide and 120 high. Avatars must be at least 20 wide and 20 high, but no larger than 90 wide and 90 high.', + 'avatar_driver_gravatar', + array( + 'avatar_gravatar_email' => 'test@example.com', + 'avatar_gravatar_width' => 120, + 'avatar_gravatar_height' => 120, + ), + ), + // Delete avatar image to reset group settings + array( + 'GROUP_UPDATED', + 'avatar_driver_gravatar', + array( + 'avatar_delete' => array('tick', ''), + ), + ), + ); + } + + /** + * @dataProvider avatar_acp_groups_data + */ + public function test_avatar_acp_groups($expected, $avatar_type, $data) + { + $this->assert_avatar_submit($expected, $avatar_type, $data); + } +} diff --git a/tests/functional/avatar_acp_users_test.php b/tests/functional/avatar_acp_users_test.php new file mode 100644 index 0000000000..0afd05e530 --- /dev/null +++ b/tests/functional/avatar_acp_users_test.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 + * + */ + +require_once dirname(__FILE__) . '/common_avatar_test.php'; + +/** + * @group functional + */ +class phpbb_functional_avatar_acp_users_test extends phpbb_functional_common_avatar_test +{ + public function get_url() + { + return 'adm/index.php?i=acp_users&u=2&mode=avatar'; + } + + public function avatar_acp_users_data() + { + return array( + // Gravatar with incorrect email + array( + 'EMAIL_INVALID_EMAIL', + 'avatar_driver_gravatar', + array( + 'avatar_gravatar_email' => 'test.example.com', + 'avatar_gravatar_width' => 80, + 'avatar_gravatar_height' => 80, + ), + ), + // Remote avatar with correct link + array( + 'USER_AVATAR_UPDATED', + 'avatar_driver_upload', + array( + 'avatar_upload_url' => 'https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', + ), + ), + // Reset avatar settings + array( + 'USER_AVATAR_UPDATED', + 'avatar_driver_gravatar', + array( + 'avatar_delete' => array('tick', ''), + ), + ), + ); + } + + /** + * @dataProvider avatar_acp_users_data + */ + public function test_avatar_acp_users($expected, $avatar_type, $data) + { + $this->assert_avatar_submit($expected, $avatar_type, $data); + } +} diff --git a/tests/functional/avatar_ucp_groups_test.php b/tests/functional/avatar_ucp_groups_test.php new file mode 100644 index 0000000000..233b7d36e1 --- /dev/null +++ b/tests/functional/avatar_ucp_groups_test.php @@ -0,0 +1,71 @@ +<?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_avatar_test.php'; + +/** + * @group functional + */ +class phpbb_functional_avatar_ucp_groups_test extends phpbb_functional_common_avatar_test +{ + public function get_url() + { + return 'ucp.php?i=ucp_groups&mode=manage&action=edit&g=5'; + } + + public function avatar_ucp_groups_data() + { + return array( + // Incorrect URL + array( + 'AVATAR_URL_INVALID', + 'avatar_driver_upload', + array( + 'avatar_upload_url' => 'https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0?s=80', + ), + ), + /* + // Does not work due to DomCrawler issue + // Valid file upload + array( + 'GROUP_UPDATED', + 'avatar_driver_upload', + array( + 'avatar_upload_file' => array('upload', $this->path . 'valid.jpg'), + ), + ), + */ + // Correct remote avatar + array( + 'GROUP_UPDATED', + 'avatar_driver_remote', + array( + 'avatar_remote_url' => 'https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', + 'avatar_remote_width' => 80, + 'avatar_remote_height' => 80, + ), + ), + array( + 'GROUP_UPDATED', + 'avatar_driver_gravatar', + array( + 'avatar_delete' => array('tick', ''), + ), + ), + ); + } + + /** + * @dataProvider avatar_ucp_groups_data + */ + public function test_avatar_ucp_groups($expected, $avatar_type, $data) + { + $this->assert_avatar_submit($expected, $avatar_type, $data); + } +} diff --git a/tests/functional/avatar_ucp_users_test.php b/tests/functional/avatar_ucp_users_test.php new file mode 100644 index 0000000000..f828559e0d --- /dev/null +++ b/tests/functional/avatar_ucp_users_test.php @@ -0,0 +1,78 @@ +<?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_avatar_test.php'; + +/** + * @group functional + */ +class phpbb_functional_avatar_ucp_users_test extends phpbb_functional_common_avatar_test +{ + public function get_url() + { + return 'ucp.php?i=ucp_profile&mode=avatar'; + } + + public function avatar_ucp_groups_data() + { + return array( + // Gravatar with correct settings + array( + 'PROFILE_UPDATED', + 'avatar_driver_gravatar', + array( + 'avatar_gravatar_email' => 'test@example.com', + 'avatar_gravatar_width' => 80, + 'avatar_gravatar_height' => 80, + ), + ), + // Wrong driver selected + array( + 'NO_AVATAR_SELECTED', + 'avatar_driver_upload', + array( + 'avatar_remote_url' => 'https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', + 'avatar_remote_width' => 80, + 'avatar_remote_height' => 80, + ), + ), + array( + 'PROFILE_UPDATED', + 'avatar_driver_gravatar', + array( + 'avatar_delete' => array('tick', ''), + ), + ), + ); + } + + /** + * @dataProvider avatar_ucp_groups_data + */ + public function test_avatar_ucp_groups($expected, $avatar_type, $data) + { + $this->assert_avatar_submit($expected, $avatar_type, $data); + } + + public function test_display_upload_avatar() + { + $this->assert_avatar_submit('PROFILE_UPDATED', + 'avatar_driver_upload', + array( + 'avatar_upload_url' => 'https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0.jpg', + ) + ); + + $crawler = self::request('GET', $this->get_url() . '&sid=' . $this->sid); + $avatar_link = $crawler->filter('img')->attr('src'); + $crawler = self::request('GET', $avatar_link . '&sid=' . $this->sid, array(), false); + $content = self::$client->getResponse()->getContent(); + self::assertEquals(false, stripos(trim($content), 'debug'), 'Output contains debug message'); + } +} diff --git a/tests/functional/browse_test.php b/tests/functional/browse_test.php index 18a2ad9464..c3be301762 100644 --- a/tests/functional/browse_test.php +++ b/tests/functional/browse_test.php @@ -29,4 +29,11 @@ class phpbb_functional_browse_test extends phpbb_functional_test_case $crawler = self::request('GET', 'viewtopic.php?t=1'); $this->assertGreaterThan(0, $crawler->filter('.postbody')->count()); } + + public function test_feed() + { + $crawler = self::request('GET', 'feed.php', array(), false); + self::assert_response_xml(); + $this->assertGreaterThan(0, $crawler->filter('entry')->count()); + } } diff --git a/tests/functional/common_avatar_test.php b/tests/functional/common_avatar_test.php new file mode 100644 index 0000000000..c0f21d07c2 --- /dev/null +++ b/tests/functional/common_avatar_test.php @@ -0,0 +1,80 @@ +<?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_avatar_test extends phpbb_functional_test_case +{ + private $path; + private $form_content; + + abstract function get_url(); + + public function setUp() + { + parent::setUp(); + $this->path = __DIR__ . '/fixtures/files/'; + $this->login(); + $this->admin_login(); + $this->add_lang(array('acp/board', 'ucp', 'acp/users', 'acp/groups')); + $this->set_acp_settings(); + } + + private function set_acp_settings() + { + $crawler = self::request('GET', 'adm/index.php?i=acp_board&mode=avatar&sid=' . $this->sid); + // Check the default entries we should have + $this->assertContainsLang('ALLOW_GRAVATAR', $crawler->text()); + $this->assertContainsLang('ALLOW_REMOTE', $crawler->text()); + $this->assertContainsLang('ALLOW_AVATARS', $crawler->text()); + $this->assertContainsLang('ALLOW_LOCAL', $crawler->text()); + + // Now start setting the needed settings + $form = $crawler->selectButton($this->lang('SUBMIT'))->form(); + $form['config[allow_avatar_local]']->select(1); + $form['config[allow_avatar_gravatar]']->select(1); + $form['config[allow_avatar_remote]']->select(1); + $form['config[allow_avatar_remote_upload]']->select(1); + $crawler = self::submit($form); + $this->assertContainsLang('CONFIG_UPDATED', $crawler->text()); + } + + public function assert_avatar_submit($expected, $type, $data, $button_text = 'SUBMIT') + { + $crawler = self::request('GET', $this->get_url() . '&sid=' . $this->sid); + + // Test if setting a gravatar avatar properly works + $form = $crawler->selectButton($this->lang($button_text))->form(); + $form['avatar_driver']->select($type); + + foreach ($data as $key => $value) + { + if (is_array($value)) + { + $form[$key]->$value[0]($value[1]); + } + else + { + $form[$key]->setValue($value); + } + } + + $crawler = self::submit($form); + + try + { + $this->assertContainsLang($expected, $crawler->text()); + } + catch (Exception $e) + { + $this->assertContains($expected, $crawler->text()); + } + } +} diff --git a/tests/functional/extension_controller_test.php b/tests/functional/extension_controller_test.php index 7d29f0000c..dc6d9c0f65 100644 --- a/tests/functional/extension_controller_test.php +++ b/tests/functional/extension_controller_test.php @@ -52,7 +52,7 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c public function test_foo_bar() { $this->phpbb_extension_manager->enable('foo/bar'); - $crawler = self::request('GET', 'app.php?controller=foo/bar', array(), false); + $crawler = self::request('GET', 'app.php/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'); @@ -64,7 +64,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 = self::request('GET', 'app.php?controller=foo/template'); + $crawler = self::request('GET', 'app.php/foo/template'); $this->assertContains("I am a variable", $crawler->filter('#content')->text()); $this->phpbb_extension_manager->purge('foo/bar'); } @@ -76,7 +76,7 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c public function test_missing_argument() { $this->phpbb_extension_manager->enable('foo/bar'); - $crawler = self::request('GET', 'app.php?controller=foo/baz', array(), false); + $crawler = self::request('GET', 'app.php/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'); @@ -88,7 +88,7 @@ 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 = self::request('GET', 'app.php?controller=foo/exception', array(), false); + $crawler = self::request('GET', 'app.php/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'); @@ -105,7 +105,7 @@ class phpbb_functional_extension_controller_test extends phpbb_functional_test_c */ public function test_error_ext_disabled_or_404() { - $crawler = self::request('GET', 'app.php?controller=does/not/exist', array(), false); + $crawler = self::request('GET', 'app.php/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_global_lang_test.php b/tests/functional/extension_global_lang_test.php new file mode 100644 index 0000000000..fb8f87e6de --- /dev/null +++ b/tests/functional/extension_global_lang_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 +*/ +class phpbb_functional_extension_global_lang_test extends phpbb_functional_test_case +{ + protected $phpbb_extension_manager; + + static private $helper; + + static protected $fixtures = array( + 'foo/bar/language/en/', + 'foo/bar/event/', + ); + + 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(); + + $this->phpbb_extension_manager = $this->get_extension_manager(); + + $this->purge_cache(); + } + + public function test_load_extension_lang_globally() + { + $this->phpbb_extension_manager->enable('foo/bar'); + + // The board index, which should contain an overwritten translation + $crawler = self::request('GET', 'index.php'); + + // language from language/en/common.php + $this->assertNotContains('Skip to content', $crawler->filter('.skiplink')->text()); + + // language from ext/foo/bar/language/en/foo_global.php + $this->assertContains('Overwritten by foo', $crawler->filter('.skiplink')->text()); + } +} diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php index 6c1720735c..19adb89819 100644 --- a/tests/functional/extension_permission_lang_test.php +++ b/tests/functional/extension_permission_lang_test.php @@ -18,6 +18,7 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t static protected $fixtures = array( 'foo/bar/language/en/', + 'foo/bar/event/', ); static public function setUpBeforeClass() @@ -75,6 +76,6 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t $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()); + $this->assertContains('Can view foobar', $crawler->filter('body')->text()); } } diff --git a/tests/functional/fileupload_remote_test.php b/tests/functional/fileupload_remote_test.php index 8e361ab77b..65c4b6b7c4 100644 --- a/tests/functional/fileupload_remote_test.php +++ b/tests/functional/fileupload_remote_test.php @@ -44,14 +44,14 @@ class phpbb_functional_fileupload_remote_test extends phpbb_functional_test_case public function test_invalid_extension() { $upload = new fileupload('', array('jpg'), 100); - $file = $upload->remote_upload('http://example.com/image.gif'); + $file = $upload->remote_upload(self::$root_url . 'develop/blank.gif'); $this->assertEquals('URL_INVALID', $file->error[0]); } - public function test_non_existant() + public function test_empty_file() { $upload = new fileupload('', array('jpg'), 100); - $file = $upload->remote_upload('http://example.com/image.jpg'); + $file = $upload->remote_upload(self::$root_url . 'develop/blank.jpg'); $this->assertEquals('EMPTY_REMOTE_DATA', $file->error[0]); } diff --git a/tests/functional/fixtures/ext/foo/bar/event/permission.php b/tests/functional/fixtures/ext/foo/bar/event/permission.php new file mode 100644 index 0000000000..48688a586a --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/event/permission.php @@ -0,0 +1,40 @@ +<?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; +} + +/** +* Event listener +*/ +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class phpbb_ext_foo_bar_event_permission implements EventSubscriberInterface +{ + static public function getSubscribedEvents() + { + return array( + 'core.permissions' => 'add_permissions', + ); + } + + public function add_permissions($event) + { + $permissions = $event['permissions']; + $permissions['u_foo'] = array('lang' => 'ACL_U_FOOBAR', 'cat' => 'post'); + $event['permissions'] = $permissions; + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/event/user_setup.php b/tests/functional/fixtures/ext/foo/bar/event/user_setup.php new file mode 100644 index 0000000000..3d2d0c5325 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/event/user_setup.php @@ -0,0 +1,43 @@ +<?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; +} + +/** +* Event listener +*/ +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class phpbb_ext_foo_bar_event_user_setup implements EventSubscriberInterface +{ + static public function getSubscribedEvents() + { + return array( + 'core.user_setup' => 'add_global_translations', + ); + } + + public function add_global_translations($event) + { + $lang_set_ext = $event['lang_set_ext']; + $lang_set_ext[] = array( + 'ext_name' => 'foo/bar', + 'lang_set' => 'foo_global', + ); + $event['lang_set_ext'] = $lang_set_ext; + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/language/en/foo_global.php b/tests/functional/fixtures/ext/foo/bar/language/en/foo_global.php new file mode 100644 index 0000000000..a6af8680d3 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/language/en/foo_global.php @@ -0,0 +1,5 @@ +<?php + +$lang = array_merge($lang, array( + 'SKIP' => 'Overwritten by foo', +)); 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 index cd4b9a32d1..64b497c394 100644 --- a/tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php +++ b/tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php @@ -1,6 +1,5 @@ <?php -// Admin Permissions $lang = array_merge($lang, array( - 'acl_u_foo' => array('lang' => 'Can view foo', 'cat' => 'misc'), + 'ACL_U_FOOBAR' => 'Can view foobar with permission foo', )); diff --git a/tests/functional/mcp_test.php b/tests/functional/mcp_test.php new file mode 100644 index 0000000000..f65a7d0784 --- /dev/null +++ b/tests/functional/mcp_test.php @@ -0,0 +1,67 @@ +<?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_mcp_test extends phpbb_functional_test_case +{ + public function test_post_new_topic() + { + $this->login(); + + // Test creating topic + $post = $this->create_topic(2, 'Test Topic 2', 'Testing move post with "Move posts" option from Quick-Moderator Tools.'); + + $crawler = self::request('GET', "viewtopic.php?t={$post['topic_id']}&sid={$this->sid}"); + $this->assertContains('Testing move post with "Move posts" option from Quick-Moderator Tools.', $crawler->filter('html')->text()); + + return $crawler; + } + + /** + * @depends test_post_new_topic + */ + public function test_handle_quickmod($crawler) + { + // Test moving a post + $form = $crawler->selectButton('Go')->eq(1)->form(); + $form['action']->select('merge'); + $crawler = self::submit($form); + + return $crawler; + } + + /** + * @depends test_handle_quickmod + */ + public function test_move_post_to_topic($crawler) + { + // Select the post in MCP + $form = $crawler->selectButton($this->lang('SUBMIT'))->form(array( + 'to_topic_id' => 1, + )); + $form['post_id_list'][0]->tick(); + $crawler = self::submit($form); + $this->assertContains($this->lang('MERGE_POSTS'), $crawler->filter('html')->text()); + + return $crawler; + } + + /** + * @depends test_move_post_to_topic + */ + public function test_confirm_result($crawler) + { + $this->add_lang('mcp'); + $form = $crawler->selectButton('Yes')->form(); + $crawler = self::submit($form); + $this->assertContains($this->lang('POSTS_MERGED_SUCCESS'), $crawler->text()); + } +} diff --git a/tests/functional/report_post_captcha.php b/tests/functional/report_post_captcha_test.php index af713775c5..8283465041 100644 --- a/tests/functional/report_post_captcha.php +++ b/tests/functional/report_post_captcha_test.php @@ -12,13 +12,6 @@ */ 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'); @@ -31,6 +24,18 @@ class phpbb_functional_report_post_captcha_test extends phpbb_functional_test_ca $this->set_reporting_guest(-1); } + 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()); + + $this->add_lang('mcp'); + $form = $crawler->selectButton($this->lang('SUBMIT'))->form(); + $crawler = self::submit($form); + $this->assertContains($this->lang('POST_REPORTED_SUCCESS'), $crawler->text()); + } + protected function set_reporting_guest($report_post_allowed) { $this->login(); diff --git a/tests/mock/auth_provider.php b/tests/mock/auth_provider.php new file mode 100644 index 0000000000..a576ef6b67 --- /dev/null +++ b/tests/mock/auth_provider.php @@ -0,0 +1,23 @@ +<?php +/** + * + * @package testing + * @copyright (c) 2013 phpBB Group + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 + * + */ + +/** + * Mock auth provider class with basic functions to help test sessions. + */ +class phpbb_mock_auth_provider extends phpbb_auth_provider_base +{ + public function login($username, $password) + { + return array( + 'status' => "", + 'error_msg' => "", + 'user_row' => "", + ); + } +} diff --git a/tests/mock/request.php b/tests/mock/request.php index b71361e33e..ed0744c8cf 100644 --- a/tests/mock/request.php +++ b/tests/mock/request.php @@ -74,6 +74,11 @@ class phpbb_mock_request implements \phpbb\request\request_interface return array_keys($this->data[$super_global]); } + public function get_super_global($super_global = phpbb_request_interface::REQUEST) + { + return $this->data[$super_global]; + } + /* custom methods */ public function set_header($header_name, $value) diff --git a/tests/mock/session_testable.php b/tests/mock/session_testable.php index 38e66ec67a..d81ae3163e 100644 --- a/tests/mock/session_testable.php +++ b/tests/mock/session_testable.php @@ -58,5 +58,9 @@ class phpbb_mock_session_testable extends \phpbb\session } } } + + public function setup() + { + } } diff --git a/tests/notification/base.php b/tests/notification/base.php new file mode 100644 index 0000000000..8de162a1fb --- /dev/null +++ b/tests/notification/base.php @@ -0,0 +1,131 @@ +<?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__) . '/manager_helper.php'; + +abstract class phpbb_tests_notification_base extends phpbb_database_test_case +{ + protected $notifications, $db, $container, $user, $config, $auth, $cache; + + protected function get_notification_types() + { + return array( + 'test', + 'approve_post', + 'approve_topic', + 'bookmark', + 'disapprove_post', + 'disapprove_topic', + 'pm', + 'post', + 'post_in_queue', + 'quote', + 'report_pm', + 'report_pm_closed', + 'report_post', + 'report_post_closed', + 'topic', + 'topic_in_queue', + ); + } + + protected function setUp() + { + parent::setUp(); + + global $phpbb_root_path, $phpEx; + + include_once(__DIR__ . '/ext/test/notification/type/test.' . $phpEx); + + global $db, $config, $user, $auth, $cache, $phpbb_container; + + $db = $this->db = $this->new_dbal(); + $config = $this->config = new phpbb_config(array( + 'allow_privmsg' => true, + 'allow_bookmarks' => true, + 'allow_topic_notify' => true, + 'allow_forum_notify' => true, + )); + $user = $this->user = new phpbb_user(); + $this->user_loader = new phpbb_user_loader($this->db, $phpbb_root_path, $phpEx, 'phpbb_users'); + $auth = $this->auth = new phpbb_mock_notifications_auth(); + $cache = $this->cache = new phpbb_cache_service( + new phpbb_cache_driver_null(), + $this->config, + $this->db, + $phpbb_root_path, + $phpEx + ); + + $phpbb_container = $this->container = new phpbb_mock_container_builder(); + + $this->notifications = new phpbb_notification_manager_helper( + array(), + array(), + $this->container, + $this->user_loader, + $this->db, + $this->cache, + $this->user, + $phpbb_root_path, + $phpEx, + 'phpbb_notification_types', + 'phpbb_notifications', + 'phpbb_user_notifications' + ); + + $phpbb_container->set('notification_manager', $this->notifications); + + $this->notifications->setDependencies($this->auth, $this->config); + + $types = array(); + foreach ($this->get_notification_types() as $type) + { + $class = $this->build_type('phpbb_notification_type_' . $type); + + $types[$type] = $class; + $this->container->set('notification.type.' . $type, $class); + } + + $this->notifications->set_var('notification_types', $types); + + $this->db->sql_query('DELETE FROM phpbb_notification_types'); + $this->db->sql_query('DELETE FROM phpbb_notifications'); + $this->db->sql_query('DELETE FROM phpbb_user_notifications'); + } + + protected function build_type($type) + { + global $phpbb_root_path, $phpEx; + + 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'); + } + + protected function assert_notifications($expected, $options = array()) + { + $notifications = $this->notifications->load_notifications(array_merge(array( + 'count_unread' => true, + 'order_by' => 'notification_time', + 'order_dir' => 'ASC', + ), $options)); + + $this->assertEquals(sizeof($expected), $notifications['unread_count']); + + $i = 0; + foreach ($notifications['notifications'] as $notification) + { + foreach ($expected[$i] as $key => $value) + { + $this->assertEquals($value, $notification->$key, $i . ' ' . $key); + } + + $i++; + } + } +} diff --git a/tests/notification/fixtures/group_request.xml b/tests/notification/fixtures/group_request.xml new file mode 100644 index 0000000000..1eb73f1e15 --- /dev/null +++ b/tests/notification/fixtures/group_request.xml @@ -0,0 +1,30 @@ +<?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> + <row> + <value>2</value> + <value>2</value> + <value>2</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + <row> + <value>3</value> + <value>3</value> + <value>3</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/notification/group_request_test.php b/tests/notification/group_request_test.php new file mode 100644 index 0000000000..368e4ae973 --- /dev/null +++ b/tests/notification/group_request_test.php @@ -0,0 +1,109 @@ +<?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__) . '/base.php'; + +class phpbb_notification_group_request_test extends phpbb_tests_notification_base +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/group_request.xml'); + } + + protected function get_notification_types() + { + return array_merge( + parent::get_notification_types(), + array( + 'group_request', + 'group_request_approved', + ) + ); + } + + public function test_notifications() + { + global $phpbb_root_path, $phpEx, $phpbb_dispatcher, $phpbb_log; + + include_once($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx); + include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx); + include_once($phpbb_root_path . 'includes/functions_content.' . $phpEx); + + set_config(false, false, false, $this->config); + + $this->container->set('groupposition.legend', new phpbb_groupposition_legend( + $this->db, + $this->user + )); + $this->container->set('groupposition.teampage', new phpbb_groupposition_teampage( + $this->db, + $this->user, + $this->cache->get_driver() + )); + $phpbb_dispatcher = new phpbb_mock_event_dispatcher; + $phpbb_log = new phpbb_log_null(); + + // Now on to the actual test + + $group_id = false; + group_create($group_id, GROUP_OPEN, 'test', 'test group', array()); + + // Add user 2 as group leader + group_user_add($group_id, 2, false, false, false, true, false); + + // Add user 3 as pending + group_user_add($group_id, 3, false, false, false, false, true); + + $this->assert_notifications( + array( + // user 3 pending notification + array( + 'item_id' => 3, // user_id of requesting join + 'item_parent_id' => $group_id, + 'user_id' => 2, + 'notification_read' => 0, + 'notification_data' => array( + 'group_name' => 'test', + ), + ), + ), + array( + 'user_id' => 2, + ) + ); + + // Approve user 3 joining the group + group_user_attributes('approve', $group_id, array(3)); + + // user 3 pending notification should have been deleted + $this->assert_notifications( + array(), + array( + 'user_id' => 2, + ) + ); + + $this->assert_notifications( + array( + // user 3 approved notification + array( + 'item_id' => $group_id, // user_id of requesting join + 'user_id' => 3, + 'notification_read' => 0, + 'notification_data' => array( + 'group_name' => 'test', + ), + ), + ), + array( + 'user_id' => 3, + ) + ); + } +} diff --git a/tests/notification/notification_test.php b/tests/notification/notification_test.php index 4c7ce9a784..e1788e8670 100644 --- a/tests/notification/notification_test.php +++ b/tests/notification/notification_test.php @@ -7,9 +7,9 @@ * */ -require_once dirname(__FILE__) . '/manager_helper.php'; +require_once dirname(__FILE__) . '/base.php'; -class phpbb_notification_test extends phpbb_database_test_case +class phpbb_notification_test extends phpbb_tests_notification_base { protected $notifications, $db, $container, $user, $config, $auth, $cache; @@ -18,98 +18,17 @@ class phpbb_notification_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/notification.xml'); } - protected function setUp() - { - parent::setUp(); - - global $phpbb_root_path, $phpEx; - - include_once(__DIR__ . '/ext/test/notification/type/test.' . $phpEx); - - $this->db = $this->new_dbal(); - $this->config = new \phpbb\config\config(array( - 'allow_privmsg' => true, - 'allow_bookmarks' => true, - 'allow_topic_notify' => true, - 'allow_forum_notify' => true, - )); - $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\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_notification_manager_helper( - array(), - array(), - $this->container, - $this->user_loader, - $this->db, - $this->cache, - $this->user, - $phpbb_root_path, - $phpEx, - 'phpbb_notification_types', - 'phpbb_notifications', - 'phpbb_user_notifications' - ); - - $this->notifications->setDependencies($this->auth, $this->config); - - $types = array(); - foreach (array( - 'test', - 'approve_post', - 'approve_topic', - 'bookmark', - 'disapprove_post', - 'disapprove_topic', - 'pm', - 'post', - 'post_in_queue', - 'quote', - 'report_pm', - 'report_pm_closed', - 'report_post', - 'report_post_closed', - 'topic', - 'topic_in_queue', - ) as $type) - { - $class = $this->build_type('\phpbb\notification\type\\' . $type); - - $types[$type] = $class; - $this->container->set('notification.type.' . $type, $class); - } - - $this->notifications->set_var('notification_types', $types); - } - - protected function build_type($type) - { - global $phpbb_root_path, $phpEx; - - 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')); + $post_type_id = $this->notifications->get_notification_type_id('post'); + $quote_type_id = $this->notifications->get_notification_type_id('quote'); + $test_type_id = $this->notifications->get_notification_type_id('test'); $this->assertEquals(array( - 'test' => 3, - 'quote' => 2, - 'post' => 1, + 'test' => $test_type_id, + 'quote' => $quote_type_id, + 'post' => $post_type_id, ), $this->notifications->get_notification_type_ids(array( 'test', @@ -117,11 +36,11 @@ class phpbb_notification_test extends phpbb_database_test_case 'post', ) )); - $this->assertEquals(2, $this->notifications->get_notification_type_id('quote')); + $this->assertEquals($quote_type_id, $this->notifications->get_notification_type_id('quote')); try { - $this->assertEquals(3, $this->notifications->get_notification_type_id('fail')); + $this->assertEquals(false, $this->notifications->get_notification_type_id('fail')); $this->fail('Non-existent type should throw an exception'); } @@ -241,88 +160,65 @@ class phpbb_notification_test extends phpbb_database_test_case 'post_time' => 1349413326, )); - $notifications = $this->notifications->load_notifications(array( - 'count_unread' => true, - )); - - $expected = array( - 1 => array( - 'notification_type_id' => 4, - 'item_id' => 1, - 'item_parent_id' => 1, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413321, - 'notification_data' => array(), - ), - 2 => array( - 'notification_type_id' => 4, - 'item_id' => 2, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413322, - 'notification_data' => array(), - ), - 3 => array( - 'notification_type_id' => 4, - 'item_id' => 3, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413323, - 'notification_data' => array(), - ), - 4 => array( - 'notification_type_id' => 3, - 'item_id' => 4, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413324, - 'notification_data' => array( - 'poster_id' => 2, - 'topic_title' => 'test-title', - 'post_subject' => 'Re: test-title', - 'post_username' => '', - 'forum_id' => 2, - 'forum_name' => 'Your first forum', + $this->assert_notifications( + array( + array( + 'item_id' => 1, + 'item_parent_id' => 1, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413321, + 'notification_data' => array(), ), - ), - 5 => array( - 'notification_type_id' => 2, - 'item_id' => 5, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413325, - 'notification_data' => array( - 'poster_id' => 2, - 'topic_title' => 'test-title', - 'post_subject' => 'Re: test-title', - 'post_username' => '', - 'forum_id' => 2, - 'forum_name' => 'Your first forum', + array( + 'item_id' => 2, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413322, + 'notification_data' => array(), ), - ), + array( + 'item_id' => 3, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413323, + 'notification_data' => array(), + ), + array( + 'item_id' => 4, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413324, + 'notification_data' => array( + 'poster_id' => 2, + 'topic_title' => 'test-title', + 'post_subject' => 'Re: test-title', + 'post_username' => '', + 'forum_id' => 2, + 'forum_name' => 'Your first forum', + ), + ), + array( + 'item_id' => 5, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413325, + 'notification_data' => array( + 'poster_id' => 2, + 'topic_title' => 'test-title', + 'post_subject' => 'Re: test-title', + 'post_username' => '', + 'forum_id' => 2, + 'forum_name' => 'Your first forum', + ), + ), + ) ); - $this->assertEquals(sizeof($expected), $notifications['unread_count']); - - $notifications = $notifications['notifications']; - - foreach ($expected as $notification_id => $notification_data) - { - //echo $notifications[$notification_id]; - - $this->assertEquals($notification_id, $notifications[$notification_id]->notification_id, 'notification_id'); - - foreach ($notification_data as $key => $value) - { - $this->assertEquals($value, $notifications[$notification_id]->$key, $key . ' ' . $notification_id); - } - } - // Now test updating ------------------------------- $this->notifications->update_notifications('test', array( @@ -347,86 +243,63 @@ class phpbb_notification_test extends phpbb_database_test_case 'forum_name' => 'Your second forum', // change forum_name )); - $notifications = $this->notifications->load_notifications(array( - 'count_unread' => true, - )); - - $expected = array( - 1 => array( - 'notification_type_id' => 4, - 'item_id' => 1, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413321, - 'notification_data' => array(), - ), - 2 => array( - 'notification_type_id' => 4, - 'item_id' => 2, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413322, - 'notification_data' => array(), - ), - 3 => array( - 'notification_type_id' => 4, - 'item_id' => 3, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1234, - 'notification_data' => array(), - ), - 4 => array( - 'notification_type_id' => 3, - 'item_id' => 4, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413324, - 'notification_data' => array( - 'poster_id' => 2, - 'topic_title' => 'test-title', - 'post_subject' => 'Re: test-title', - 'post_username' => '', - 'forum_id' => 2, - 'forum_name' => 'Your first forum', + $this->assert_notifications( + array( + array( + 'item_id' => 3, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1234, + 'notification_data' => array(), ), - ), - 5 => array( - 'notification_type_id' => 2, - 'item_id' => 5, - 'item_parent_id' => 2, - 'user_id' => 0, - 'notification_read' => 0, - 'notification_time' => 1349413325, - 'notification_data' => array( - 'poster_id' => 2, - 'topic_title' => 'test-title2', - 'post_subject' => 'Re: test-title2', - 'post_username' => '', - 'forum_id' => 3, - 'forum_name' => 'Your second forum', + array( + 'item_id' => 1, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413321, + 'notification_data' => array(), ), - ), + array( + 'item_id' => 2, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413322, + 'notification_data' => array(), + ), + array( + 'item_id' => 4, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413324, + 'notification_data' => array( + 'poster_id' => 2, + 'topic_title' => 'test-title', + 'post_subject' => 'Re: test-title', + 'post_username' => '', + 'forum_id' => 2, + 'forum_name' => 'Your first forum', + ), + ), + array( + 'item_id' => 5, + 'item_parent_id' => 2, + 'user_id' => 0, + 'notification_read' => 0, + 'notification_time' => 1349413325, + 'notification_data' => array( + 'poster_id' => 2, + 'topic_title' => 'test-title2', + 'post_subject' => 'Re: test-title2', + 'post_username' => '', + 'forum_id' => 3, + 'forum_name' => 'Your second forum', + ), + ), + ) ); - - $this->assertEquals(sizeof($expected), $notifications['unread_count']); - - $notifications = $notifications['notifications']; - - foreach ($expected as $notification_id => $notification_data) - { - //echo $notifications[$notification_id]; - - $this->assertEquals($notification_id, $notifications[$notification_id]->notification_id, 'notification_id'); - - foreach ($notification_data as $key => $value) - { - $this->assertEquals($value, $notifications[$notification_id]->$key, $key . ' ' . $notification_id); - } - } } } diff --git a/tests/session/check_ban_test.php b/tests/session/check_ban_test.php new file mode 100644 index 0000000000..8d6c9a866d --- /dev/null +++ b/tests/session/check_ban_test.php @@ -0,0 +1,78 @@ +<?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__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_check_ban_test extends phpbb_session_test_case +{ + protected $user_id = 4; + protected $key_id = 4; + protected $session; + protected $backup_cache; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_banlist.xml'); + } + + static function check_banned_data() + { + return array( + array('All false values, should not be banned', + false, false, false, false, /* should be banned? -> */ false), + array('Matching values in the database, should be banned', + 4, '127.0.0.1', 'bar@example.org', true, /* should be banned? -> */ true), + array('IP Banned, should be banned', + false, '127.1.1.1', false, false, /* should be banned? -> */ true), + ); + } + + public function setUp() + { + parent::setUp(); + // Get session here so that config is mocked correctly + $this->session = $this->session_factory->get_session($this->db); + global $cache, $config, $phpbb_root_path, $phpEx; + $this->backup_cache = $cache; + // Change the global cache object for this test because + // the mock cache object does not hit the database as is needed + // for this test. + $cache = new phpbb_cache_service( + new phpbb_cache_driver_file(), + $config, + $this->db, + $phpbb_root_path, + $phpEx + ); + } + + public function tearDown() + { + parent::tearDown(); + // Set cache back to what it was before the test changed it + global $cache; + $cache = $this->backup_cache; + } + + /** @dataProvider check_banned_data */ + public function test_check_is_banned($test_msg, $user_id, $user_ips, $user_email, $return, $should_be_banned) + { + try + { + $is_banned = $this->session->check_ban($user_id, $user_ips, $user_email, $return); + } + catch (PHPUnit_Framework_Error_Notice $e) + { + // User error was triggered, user must have been banned + $is_banned = true; + } + + $this->assertEquals($should_be_banned, $is_banned, $test_msg); + } +} diff --git a/tests/session/check_isvalid_test.php b/tests/session/check_isvalid_test.php new file mode 100644 index 0000000000..760e2a6f24 --- /dev/null +++ b/tests/session/check_isvalid_test.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 +* +*/ + +require_once dirname(__FILE__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_check_isvalid_test extends phpbb_session_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_full.xml'); + } + + protected function access_with($session_id, $user_id, $user_agent, $ip) + { + $this->session_factory->merge_test_data($session_id, $user_id, $user_agent, $ip); + + $session = $this->session_factory->get_session($this->db); + $session->page = array('page' => 'page', 'forum' => 0); + + $session->session_begin(); + $this->session_factory->check($this); + return $session; + } + + public function test_session_valid_session_exists() + { + $session = $this->access_with('bar_session000000000000000000000', '4', 'user agent', '127.0.0.1'); + $session->check_cookies($this, array()); + + $this->check_sessions_equals(array( + array('session_id' => 'anon_session00000000000000000000', 'session_user_id' => 1), + array('session_id' => 'bar_session000000000000000000000', 'session_user_id' => 4), + ), + 'If a request comes with a valid session id with matching user agent and IP, no new session should be created.' + ); + } + + public function test_session_invalid_make_new_annon_session() + { + $session = $this->access_with('anon_session00000000000000000000', '4', 'user agent', '127.0.0.1'); + $session->check_cookies($this, array( + 'u' => array('1', null), + 'k' => array(null, null), + 'sid' => array($session->session_id, null), + )); + + $this->check_sessions_equals(array( + array('session_id' => $session->session_id, 'session_user_id' => 1), // use generated SID + array('session_id' => 'bar_session000000000000000000000', 'session_user_id' => 4), + ), + 'If a request comes with a valid session id and IP but different user id and user agent, + a new anonymous session is created and the session matching the supplied session id is deleted.' + ); + } +} diff --git a/tests/session/continue_test.php b/tests/session/continue_test.php deleted file mode 100644 index 28019b54b5..0000000000 --- a/tests/session/continue_test.php +++ /dev/null @@ -1,133 +0,0 @@ -<?php -/** -* -* @package testing -* @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 -* -*/ - -require_once dirname(__FILE__) . '/testable_factory.php'; - -class phpbb_session_continue_test extends phpbb_database_test_case -{ - public function getDataSet() - { - return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/sessions_full.xml'); - } - - static public function session_begin_attempts() - { - // The session_id field is defined as CHAR(32) in the database schema. - // Thus the data we put in session_id fields has to have a length of 32 characters on stricter DBMSes. - // Thus we fill those strings up with zeroes until they have a string length of 32. - - return array( - array( - 'bar_session000000000000000000000', '4', 'user agent', '127.0.0.1', - array( - array('session_id' => 'anon_session00000000000000000000', 'session_user_id' => 1), - array('session_id' => 'bar_session000000000000000000000', 'session_user_id' => 4), - ), - array(), - 'If a request comes with a valid session id with matching user agent and IP, no new session should be created.', - ), - array( - 'anon_session00000000000000000000', '4', 'user agent', '127.0.0.1', - array( - array('session_id' => '__new_session_id__', 'session_user_id' => 1), // use generated SID - array('session_id' => 'bar_session000000000000000000000', 'session_user_id' => 4), - ), - array( - 'u' => array('1', null), - 'k' => array(null, null), - 'sid' => array('__new_session_id__', null), - ), - 'If a request comes with a valid session id and IP but different user id and user agent, a new anonymous session is created and the session matching the supplied session id is deleted.', - ), - ); - } - - /** - * @dataProvider session_begin_attempts - */ - public function test_session_begin_valid_session($session_id, $user_id, $user_agent, $ip, $expected_sessions, $expected_cookies, $message) - { - global $phpbb_container, $phpbb_root_path, $phpEx; - - $db = $this->new_dbal(); - $config = new \phpbb\config\config(array()); - $request = $this->getMock('\phpbb\request\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, - '_u' => $user_id, - )); - $session_factory->merge_config_data(array( - 'session_length' => time(), // need to do this to allow sessions started at time 0 - )); - $session_factory->merge_server_data(array( - 'HTTP_USER_AGENT' => $user_agent, - 'REMOTE_ADDR' => $ip, - )); - - $session = $session_factory->get_session($db); - $session->page = array('page' => 'page', 'forum' => 0); - - $session->session_begin(); - - $sql = 'SELECT session_id, session_user_id - FROM phpbb_sessions - ORDER BY session_user_id'; - - $expected_sessions = $this->replace_session($expected_sessions, $session->session_id); - $expected_cookies = $this->replace_session($expected_cookies, $session->session_id); - - $this->assertSqlResultEquals( - $expected_sessions, - $sql, - $message - ); - - $session->check_cookies($this, $expected_cookies); - - $session_factory->check($this); - } - - /** - * Replaces recursively the value __new_session_id__ with the given session - * id. - * - * @param array $array An array of data - * @param string $session_id The new session id to use instead of the - * placeholder. - * @return array The input array with all occurances of __new_session_id__ - * replaced. - */ - public function replace_session($array, $session_id) - { - foreach ($array as $key => &$value) - { - if ($value === '__new_session_id__') - { - $value = $session_id; - } - - if (is_array($value)) - { - $value = $this->replace_session($value, $session_id); - } - } - - return $array; - } -} diff --git a/tests/session/create_test.php b/tests/session/create_test.php new file mode 100644 index 0000000000..442445599b --- /dev/null +++ b/tests/session/create_test.php @@ -0,0 +1,43 @@ +<?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__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_create_test extends phpbb_session_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_full.xml'); + } + + static function bot($bot_agent, $user_id, $bot_ip) + { + return array(array( + 'bot_agent' => $bot_agent, + 'user_id' => $user_id, + 'bot_ip' => $bot_ip, + )); + } + + function test_bot_session() + { + $output = $this->session_facade->session_create( + false, + false, + false, + false, + array(), + 'user agent', + '127.0.0.1', + self::bot('user agent', 13, '127.0.0.1'), + '' + ); + $this->assertEquals(true, $output->data['is_bot'], 'should be a bot'); + } +} diff --git a/tests/session/creation_test.php b/tests/session/creation_test.php deleted file mode 100644 index 6b1be61ade..0000000000 --- a/tests/session/creation_test.php +++ /dev/null @@ -1,69 +0,0 @@ -<?php -/** -* -* @package testing -* @copyright (c) 2011 phpBB Group -* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 -* -*/ - -require_once dirname(__FILE__) . '/testable_factory.php'; - -class phpbb_session_creation_test extends phpbb_database_test_case -{ - public function getDataSet() - { - return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/sessions_empty.xml'); - } - - // also see security/extract_current_page.php - - public function test_login_session_create() - { - global $phpbb_container, $phpbb_root_path, $phpEx; - - $db = $this->new_dbal(); - $config = new \phpbb\config\config(array()); - $request = $this->getMock('\phpbb\request\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); - $session->page = array('page' => 'page', 'forum' => 0); - - $session->session_create(3); - - $sql = 'SELECT session_user_id - FROM phpbb_sessions'; - - $this->assertSqlResultEquals( - array(array('session_user_id' => 3)), - $sql, - 'Check if exactly one session for user id 3 was created' - ); - - $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), - 'k' => array(null, $cookie_expire), - 'sid' => array($session->session_id, $cookie_expire), - )); - - global $SID, $_SID; - $this->assertEquals($session->session_id, $_SID); - $this->assertEquals('?sid=' . $session->session_id, $SID); - - $session_factory->check($this); - } -} - diff --git a/tests/session/extract_hostname_test.php b/tests/session/extract_hostname_test.php new file mode 100644 index 0000000000..bd183fd438 --- /dev/null +++ b/tests/session/extract_hostname_test.php @@ -0,0 +1,51 @@ +<?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__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_extract_hostname_test extends phpbb_session_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_empty.xml'); + } + + static public function extract_current_hostname_data() + { + return array ( + // [Input] $host, $server_name_config, $cookie_domain_config, [Expected] $output + // If host is ip use that + // ipv4 + array('127.0.0.1', 'skipped.org', 'skipped.org', '127.0.0.1'), + // ipv6 + array('::1', 'skipped.org', 'skipped.org', ':'), + array('2002::3235:51f9', 'skipped.org', 'skipped.org', '2002::3235'), + // If no host but server name matches cookie_domain use that + array('', 'example.org', 'example.org', 'example.org'), + // If there is a host uri use that + array('example.org', false, false, 'example.org'), + // 'best approach' guessing + array('', 'example.org', false, 'example.org'), + array('', false, '127.0.0.1', '127.0.0.1'), + array('', false, false, php_uname('n')), + ); + } + + /** @dataProvider extract_current_hostname_data */ + function test_extract_current_hostname($host, $server_name_config, $cookie_domain_config, $expected) + { + $output = $this->session_facade->extract_current_hostname( + $host, + $server_name_config, + $cookie_domain_config + ); + + $this->assertEquals($expected, $output); + } +} diff --git a/tests/session/extract_page_test.php b/tests/session/extract_page_test.php new file mode 100644 index 0000000000..f4ae8de021 --- /dev/null +++ b/tests/session/extract_page_test.php @@ -0,0 +1,115 @@ +<?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__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_extract_page_test extends phpbb_session_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_empty.xml'); + } + + static public function extract_current_page_data() + { + return array( + array( + './', + '/phpBB/index.php', + '', + '/phpBB/', + array( + 'page_name' => 'index.php', + 'page_dir' => '', + 'query_string' => '', + 'script_path' => '/phpBB/', + 'root_script_path' => '/phpBB/', + 'page' => 'index.php', + 'forum' => 0, + ), + ), + array( + './', + '/phpBB/ucp.php', + 'mode=login', + '/phpBB/ucp.php?mode=login', + array( + 'page_name' => 'ucp.php', + 'page_dir' => '', + 'query_string' => 'mode=login', + 'script_path' => '/phpBB/', + 'root_script_path' => '/phpBB/', + 'page' => 'ucp.php?mode=login', + 'forum' => 0, + ), + ), + array( + './', + '/phpBB/ucp.php', + 'mode=register', + '/phpBB/ucp.php?mode=register', + array( + 'page_name' => 'ucp.php', + 'page_dir' => '', + 'query_string' => 'mode=register', + 'script_path' => '/phpBB/', + 'root_script_path' => '/phpBB/', + 'page' => 'ucp.php?mode=register', + 'forum' => 0, + ), + ), + array( + './', + '/phpBB/ucp.php', + 'mode=register', + '/phpBB/ucp.php?mode=register', + array( + 'page_name' => 'ucp.php', + 'page_dir' => '', + 'query_string' => 'mode=register', + 'script_path' => '/phpBB/', + 'root_script_path' => '/phpBB/', + 'page' => 'ucp.php?mode=register', + 'forum' => 0, + ), + ), + array( + './../', + '/phpBB/adm/index.php', + 'sid=e7215d958cdd41a6fc13509bebe53e42', + '/phpBB/adm/index.php?sid=e7215d958cdd41a6fc13509bebe53e42', + array( + 'page_name' => 'index.php', + //'page_dir' => 'adm', + // ^-- Ignored because .. returns different directory in live vs testing + 'query_string' => '', + 'script_path' => '/phpBB/adm/', + 'root_script_path' => '/phpBB/', + //'page' => 'adm/index.php', + 'forum' => 0, + ), + ), + ); + } + + /** @dataProvider extract_current_page_data */ + function test_extract_current_page($root_path, $php_self, $query_string, $request_uri, $expected) + { + $output = $this->session_facade->extract_current_page( + $root_path, + $php_self, + $query_string, + $request_uri + ); + + // This compares the result of the output. + // Any keys that are not in the expected array are overwritten by the output (aka not checked). + $this->assert_array_content_equals(array_merge($output, $expected), $output); + } +} diff --git a/tests/session/fixtures/sessions_banlist.xml b/tests/session/fixtures/sessions_banlist.xml new file mode 100644 index 0000000000..9422fc0665 --- /dev/null +++ b/tests/session/fixtures/sessions_banlist.xml @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <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>1</value> + <value>anonymous</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> + <table name="phpbb_sessions"> + <column>session_id</column> + <column>session_user_id</column> + <column>session_ip</column> + <column>session_browser</column> + <column>session_admin</column> + <row> + <value>bar_session000000000000000000000</value> + <value>4</value> + <value>127.0.0.1</value> + <value>user agent</value> + <value>1</value> + </row> + </table> + <table name="phpbb_banlist"> + <column>ban_id</column> + <column>ban_userid</column> + <column>ban_ip</column> + <column>ban_email</column> + <column>ban_start</column> + <column>ban_end</column> + <column>ban_exclude</column> + <column>ban_reason</column> + <column>ban_give_reason</column> + <row> + <value>2</value> + <value>4</value> + <value>127.0.0.1</value> + <value>bar@example.org</value> + <value>1111</value> + <value>0</value> + <value>0</value> + <value>HAHAHA</value> + <value>1</value> + </row> + <row> + <value>3</value> + <value>0</value> + <value>127.1.1.1</value> + <value></value> + <value>1111</value> + <value>0</value> + <value>0</value> + <value>HAHAHA</value> + <value>1</value> + </row> + </table> +</dataset> diff --git a/tests/session/fixtures/sessions_full.xml b/tests/session/fixtures/sessions_full.xml index 509687f4d2..6bbaf1c9d5 100644 --- a/tests/session/fixtures/sessions_full.xml +++ b/tests/session/fixtures/sessions_full.xml @@ -37,17 +37,20 @@ <column>session_user_id</column> <column>session_ip</column> <column>session_browser</column> + <column>session_admin</column> <row> <value>anon_session00000000000000000000</value> <value>1</value> <value>127.0.0.1</value> <value>anonymous user agent</value> + <value>0</value> </row> <row> <value>bar_session000000000000000000000</value> <value>4</value> <value>127.0.0.1</value> <value>user agent</value> + <value>1</value> </row> </table> </dataset> diff --git a/tests/session/fixtures/sessions_garbage.xml b/tests/session/fixtures/sessions_garbage.xml new file mode 100644 index 0000000000..23c44a975b --- /dev/null +++ b/tests/session/fixtures/sessions_garbage.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <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>4</value> + <value>bar</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> + <table name="phpbb_sessions"> + <column>session_id</column> + <column>session_user_id</column> + <column>session_ip</column> + <column>session_browser</column> + <column>session_admin</column> + <row> + <value>anon_session00000000000000000000</value> + <value>1</value> + <value>127.0.0.1</value> + <value>anonymous user agent</value> + <value>0</value> + </row> + <row> + <value>bar_session000000000000000000000</value> + <value>4</value> + <value>127.0.0.1</value> + <value>user agent</value> + <value>1</value> + </row> + </table> + <table name="phpbb_login_attempts"> + <column>attempt_ip</column> + <column>attempt_browser</column> + <column>attempt_forwarded_for</column> + <column>attempt_time</column> + <column>user_id</column> + <column>username</column> + <column>username_clean</column> + <row> + <value>127.0.0.1</value> + <value>browser</value> + <value></value> + <value>0001</value> + <value>4</value> + <value>bar</value> + <value>bar</value> + </row> + </table> +</dataset> diff --git a/tests/session/fixtures/sessions_key.xml b/tests/session/fixtures/sessions_key.xml new file mode 100644 index 0000000000..246d284557 --- /dev/null +++ b/tests/session/fixtures/sessions_key.xml @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_sessions_keys"> + <column>key_id</column> + <column>user_id</column> + <column>last_ip</column> + <column>last_login</column> + <row> + <value>a87ff679a2f3e71d9181a67b7542122c</value> + <value>4</value> + <value>127.0.0.1</value> + <value>0</value> + </row> + </table> + <table name="phpbb_sessions"> + <column>session_id</column> + <column>session_user_id</column> + <column>session_ip</column> + <column>session_browser</column> + <row> + <value>bar_session000000000000000000000</value> + <value>4</value> + <value>127.0.0.1</value> + <value>user agent</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>4</value> + <value>bar</value> + <value></value> + <value></value> + <value></value> + <value></value> + </row> + </table> +</dataset> diff --git a/tests/session/garbage_collection_test.php b/tests/session/garbage_collection_test.php new file mode 100644 index 0000000000..e7d01785dd --- /dev/null +++ b/tests/session/garbage_collection_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__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_garbage_collection_test extends phpbb_session_test_case +{ + public $session; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_garbage.xml'); + } + + public function setUp() + { + parent::setUp(); + $this->session = $this->session_factory->get_session($this->db); + } + + public function test_cleanup_all() + { + $this->check_sessions_equals( + array( + array( + 'session_id' => 'anon_session00000000000000000000', + 'session_user_id' => 1, + ), + array( + 'session_id' => 'bar_session000000000000000000000', + 'session_user_id' => 4, + ), + ), + 'Before test, should have some sessions.' + ); + // Set session length so it clears all + global $config; + $config['session_length'] = 0; + // There is an error unless the captcha plugin is set + $config['captcha_plugin'] = 'phpbb_captcha_nogd'; + $this->session->session_gc(); + $this->check_sessions_equals( + array(), + 'After setting session time to 0, should remove all.' + ); + } +} diff --git a/tests/session/session_key_test.php b/tests/session/session_key_test.php new file mode 100644 index 0000000000..1cf2101385 --- /dev/null +++ b/tests/session/session_key_test.php @@ -0,0 +1,51 @@ +<?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__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_login_keys_test extends phpbb_session_test_case +{ + protected $user_id = 4; + protected $key_id = 4; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_key.xml'); + } + + public function test_set_key_manually() + { + // With AutoLogin setup + $this->session_factory->merge_config_data(array('allow_autologin' => true)); + $session = $this->session_factory->get_session($this->db); + // Using a user_id and key that is already in the database + $session->cookie_data['u'] = $this->user_id; + $session->cookie_data['k'] = $this->key_id; + // Try to access session + $session->session_create($this->user_id, false, $this->user_id); + + $this->assertEquals($this->user_id, $session->data['user_id'], "session should automatically login"); + } + + public function test_reset_keys() + { + // With AutoLogin setup + $this->session_factory->merge_config_data(array('allow_autologin' => true)); + $session = $this->session_factory->get_session($this->db); + // Reset of the keys for this user + $session->reset_login_keys($this->user_id); + // Using a user_id and key that was in the database (before reset) + $session->cookie_data['u'] = $this->user_id; + $session->cookie_data['k'] = $this->key_id; + // Try to access session + $session->session_create($this->user_id, false, $this->user_id); + + $this->assertNotEquals($this->user_id, $session->data['user_id'], "session should be cleared"); + } +} diff --git a/tests/session/testable_facade.php b/tests/session/testable_facade.php new file mode 100644 index 0000000000..9f0a3c5f59 --- /dev/null +++ b/tests/session/testable_facade.php @@ -0,0 +1,142 @@ +<?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__) . '/testable_factory.php'; +require_once dirname(__FILE__) . '/../../phpBB/phpbb/session.php'; + +/** + * This class exists to expose session.php's functions in a more testable way. + * + * Since many functions in session.php have global variables inside the function, + * this exposes those functions through a testable facade that uses + * testable_factory's mock global variables to modify global variables used in + * the functions. + * + * This is using the facade pattern to provide a testable "front" to the + * functions in sessions.php. + * + */ +class phpbb_session_testable_facade +{ + protected $db; + protected $session_factory; + + function __construct($db, $session_factory) + { + $this->db = $db; + $this->session_factory = $session_factory; + } + + function extract_current_page( + $root_path, + $php_self, + $query_string, + $request_uri + ) + { + $this->session_factory->get_session($this->db); + global $request; + $request->overwrite('PHP_SELF', $php_self, phpbb_request_interface::SERVER); + $request->overwrite('QUERY_STRING', $query_string, phpbb_request_interface::SERVER); + $request->overwrite('REQUEST_URI', $request_uri, phpbb_request_interface::SERVER); + return phpbb_session::extract_current_page($root_path); + } + + function extract_current_hostname( + $host, + $server_name_config, + $cookie_domain_config + ) + { + $session = $this->session_factory->get_session($this->db); + global $config, $request; + $config['server_name'] = $server_name_config; + $config['cookie_domain'] = $cookie_domain_config; + $request->overwrite('SERVER_NAME', $host, phpbb_request_interface::SERVER); + $request->overwrite('Host', $host, phpbb_request_interface::SERVER); + // Note: There is a php_uname function used as a fallthrough + // that this function doesn't override + return $session->extract_current_hostname(); + } + + /** + * + * This function has a lot of dependencies, so instead of naming them all, + * just ask for overrides + * + * @param update_session_page Boolean of whether to set page of the session + * @param config_overrides An array of overrides for the global config object + * @param request_overrides An array of overrides for the global request object + * @return boolean False if the user is identified, otherwise true. + */ + function session_begin( + $update_session_page = true, + $config_overrides = array(), + $request_overrides = array(), + $cookies_overrides = array() + ) + { + $this->session_factory->merge_config_data($config_overrides); + $this->session_factory->merge_server_data($request_overrides); + $this->session_factory->set_cookies($cookies_overrides); + $session = $this->session_factory->get_session($this->db); + $session->session_begin($update_session_page); + return $session; + } + + function session_create( + $user_id = false, + $set_admin = false, + $persist_login = false, + $viewonline = true, + array $config_overrides = array(), + $user_agent = 'user agent', + $ip_address = '127.0.0.1', + array $bot_overrides = array(), + $uri_sid = "" + ) + { + $this->session_factory->merge_config_data($config_overrides); + // Bots + $this->session_factory->merge_cache_data(array('_bots' => $bot_overrides)); + global $request; + $session = $this->session_factory->get_session($this->db); + $session->browser = $user_agent; + $session->ip = $ip_address; + // Uri sid + if ($uri_sid) + { + $_GET['sid'] = $uri_sid; + } + $session->session_create($user_id, $set_admin, $persist_login, $viewonline); + return $session; + } + + function validate_referer( + $check_script_path, + $referer, + $host, + $force_server_vars, + $server_port, + $server_name, + $root_script_path + ) + { + $session = $this->session_factory->get_session($this->db); + global $config, $request; + $session->referer = $referer; + $session->page['root_script_path'] = $root_script_path; + $session->host = $host; + $config['force_server_vars'] = $force_server_vars; + $config['server_name'] = $server_name; + $request->overwrite('SERVER_PORT', $server_port, phpbb_request_interface::SERVER); + return $session->validate_referer($check_script_path); + } +} + diff --git a/tests/session/testable_factory.php b/tests/session/testable_factory.php index 1a7d27b30f..a58e208efc 100644 --- a/tests/session/testable_factory.php +++ b/tests/session/testable_factory.php @@ -2,11 +2,14 @@ /** * * @package testing -* @copyright (c) 2011 phpBB Group +* @copyright (c) 2013 phpBB Group * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ +require_once dirname(__FILE__) . '/../mock/container_builder.php'; +require_once dirname(__FILE__) . '/../mock/auth_provider.php'; + /** * This class exists to setup an instance of phpbb's session class for testing. * @@ -16,6 +19,7 @@ */ class phpbb_session_testable_factory { + protected $container; protected $config_data; protected $cache_data; protected $cookies; @@ -65,7 +69,7 @@ class phpbb_session_testable_factory public function get_session(\phpbb\db\driver\driver $dbal) { // set up all the global variables used by session - global $SID, $_SID, $db, $config, $cache, $request; + global $SID, $_SID, $db, $config, $cache, $request, $phpbb_container; $request = $this->request = new phpbb_mock_request( array(), @@ -83,6 +87,12 @@ class phpbb_session_testable_factory $cache = $this->cache = new phpbb_mock_cache($this->get_cache_data()); $SID = $_SID = null; + $phpbb_container = $this->container = new phpbb_mock_container_builder(); + $phpbb_container->set( + 'auth.provider.db', + new phpbb_mock_auth_provider() + ); + $session = new phpbb_mock_session_testable; return $session; } @@ -165,6 +175,32 @@ class phpbb_session_testable_factory } /** + * Set cookies, merge config and server data in one step. + * + * New values overwrite old ones. + * + * @param $session_id + * @param $user_id + * @param $user_agent + * @param $ip + * @param int $time + */ + public function merge_test_data($session_id, $user_id, $user_agent, $ip, $time = 0) + { + $this->set_cookies(array( + '_sid' => $session_id, + '_u' => $user_id, + )); + $this->merge_config_data(array( + 'session_length' => time() + $time, // need to do this to allow sessions started at time 0 + )); + $this->merge_server_data(array( + 'HTTP_USER_AGENT' => $user_agent, + 'REMOTE_ADDR' => $ip, + )); + } + + /** * Retrieve all server variables to be passed to the session. * * @return array Server variables diff --git a/tests/session/unset_admin_test.php b/tests/session/unset_admin_test.php new file mode 100644 index 0000000000..1d5b1759ab --- /dev/null +++ b/tests/session/unset_admin_test.php @@ -0,0 +1,48 @@ +<?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__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_unset_admin_test extends phpbb_session_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_full.xml'); + } + + function get_test_session() + { + return $this->session_facade->session_begin( + true, + // Config + array( + 'session_length' => time(), // need to do this to allow sessions started at time 0 + ), + // Server + array( + 'HTTP_USER_AGENT' => "user agent", + 'REMOTE_ADDR' => "127.0.0.1", + ), + // Cookies + array( + '_sid' => 'bar_session000000000000000000000', + '_u' => 4, + ) + ); + } + + public function test_unset_admin() + { + $session = $this->get_test_session(); + $this->assertEquals(1, $session->data['session_admin'], 'should be an admin before test starts'); + $session->unset_admin(); + $session = $this->get_test_session(); + $this->assertEquals(0, $session->data['session_admin'], 'should be not be an admin after unset_admin'); + } +} diff --git a/tests/session/validate_referrer_test.php b/tests/session/validate_referrer_test.php new file mode 100644 index 0000000000..a302229287 --- /dev/null +++ b/tests/session/validate_referrer_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__) . '/../test_framework/phpbb_session_test_case.php'; + +class phpbb_session_validate_referrer_test extends phpbb_session_test_case +{ + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/sessions_empty.xml'); + } + + static function referrer_inputs() + { + $ex = "example.org"; + $alt = "example.com"; + return array( + // checkpath referrer host forcevars port servername rootpath pass? + // 0 Referrer or host wasn't collected, therefore should validate + array(false, '', $ex, false, 80, $ex, '', true), + array(false, $ex, '', false, 80, $ex, '', true), + // 2 Referrer doesn't match host or server_name + array(false, $alt, $ex, false, 80, $ex, '', false), + // 3 Everything should check out + array(false, $ex, $ex, false, 80, $ex, '', true), + // 4 Check Script Path + array(true, $ex, $ex, false, 80, $ex, '', true), + array(true, "$ex/foo", $ex, false, 80, $ex, "/foo", true), + array(true, "$ex/bar", $ex, false, 80, $ex, "/foo", false), + // 7 Port (This is not checked unless path is checked) + array(true, "$ex:80/foo", "$ex:80", false, 80, "$ex:80", "/foo", true), + array(true, "$ex:80/bar", "$ex:80", false, 80, "$ex:80", "/foo", false), + array(true, "$ex:79/foo", "$ex:81", false, 81, "$ex:81", "/foo", false), + ); + } + + /** @dataProvider referrer_inputs */ + function test_referrer_inputs( + $check_script_path, + $referrer, + $host, + $force_server_vars, + $server_port, + $server_name, + $root_script_path, + $pass_or_fail + ) + { + // Referrer needs http:// because it's going to get stripped in function. + $referrer = $referrer ? 'http://' . $referrer : ''; + $this->assertEquals( + $pass_or_fail, + $this->session_facade->validate_referer( + $check_script_path, + $referrer, + $host, + $force_server_vars, + $server_port, + $server_name, + $root_script_path + ), + "referrer should" . ($pass_or_fail ? '' : "n't") . " be validated"); + } +} diff --git a/tests/template/datasets/event_inheritance/ext/kappa/styles/all/template/test.html b/tests/template/datasets/event_inheritance/ext/kappa/styles/all/template/event/test.html index 3eb906a09e..3eb906a09e 100644 --- a/tests/template/datasets/event_inheritance/ext/kappa/styles/all/template/test.html +++ b/tests/template/datasets/event_inheritance/ext/kappa/styles/all/template/event/test.html diff --git a/tests/template/datasets/event_inheritance/ext/kappa/styles/silver/template/test.html b/tests/template/datasets/event_inheritance/ext/kappa/styles/silver/template/event/test.html index 3b65d80a6d..3b65d80a6d 100644 --- a/tests/template/datasets/event_inheritance/ext/kappa/styles/silver/template/test.html +++ b/tests/template/datasets/event_inheritance/ext/kappa/styles/silver/template/event/test.html diff --git a/tests/template/datasets/event_inheritance/ext/kappa/styles/silver_inherit/template/test.html b/tests/template/datasets/event_inheritance/ext/kappa/styles/silver_inherit/template/event/test.html index 26826d59e3..26826d59e3 100644 --- a/tests/template/datasets/event_inheritance/ext/kappa/styles/silver_inherit/template/test.html +++ b/tests/template/datasets/event_inheritance/ext/kappa/styles/silver_inherit/template/event/test.html diff --git a/tests/template/datasets/event_inheritance/ext/omega/styles/all/template/test.html b/tests/template/datasets/event_inheritance/ext/omega/styles/all/template/event/test.html index 003d193dc3..003d193dc3 100644 --- a/tests/template/datasets/event_inheritance/ext/omega/styles/all/template/test.html +++ b/tests/template/datasets/event_inheritance/ext/omega/styles/all/template/event/test.html diff --git a/tests/template/datasets/event_inheritance/ext/omega/styles/silver/template/test.html b/tests/template/datasets/event_inheritance/ext/omega/styles/silver/template/event/test.html index 6bf06f5457..6bf06f5457 100644 --- a/tests/template/datasets/event_inheritance/ext/omega/styles/silver/template/test.html +++ b/tests/template/datasets/event_inheritance/ext/omega/styles/silver/template/event/test.html diff --git a/tests/template/datasets/event_inheritance/ext/omega/styles/silver/template/two.html b/tests/template/datasets/event_inheritance/ext/omega/styles/silver/template/event/two.html index 7f8058f4e4..7f8058f4e4 100644 --- a/tests/template/datasets/event_inheritance/ext/omega/styles/silver/template/two.html +++ b/tests/template/datasets/event_inheritance/ext/omega/styles/silver/template/event/two.html diff --git a/tests/template/datasets/event_inheritance/ext/zeta/styles/all/template/test.html b/tests/template/datasets/event_inheritance/ext/zeta/styles/all/template/event/test.html index 5fc7e5ac12..5fc7e5ac12 100644 --- a/tests/template/datasets/event_inheritance/ext/zeta/styles/all/template/test.html +++ b/tests/template/datasets/event_inheritance/ext/zeta/styles/all/template/event/test.html 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/event_variable_spacing.html index 028f8aa0d1..028f8aa0d1 100644 --- 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/event_variable_spacing.html diff --git a/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/event/test_event_loop.html b/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/event/test_event_loop.html new file mode 100644 index 0000000000..235e129f85 --- /dev/null +++ b/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/event/test_event_loop.html @@ -0,0 +1 @@ +{event_loop.S_ROW_COUNT}<!-- IF not event_loop.S_LAST_ROW -->|<!-- ENDIF --> diff --git a/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/universal.html b/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/event/universal.html index f2c5762ade..f2c5762ade 100644 --- a/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/universal.html +++ b/tests/template/datasets/ext_trivial/ext/trivial/styles/all/template/event/universal.html diff --git a/tests/template/datasets/ext_trivial/ext/trivial/styles/silver/template/simple.html b/tests/template/datasets/ext_trivial/ext/trivial/styles/silver/template/event/simple.html index fe32a1ed3f..fe32a1ed3f 100644 --- a/tests/template/datasets/ext_trivial/ext/trivial/styles/silver/template/simple.html +++ b/tests/template/datasets/ext_trivial/ext/trivial/styles/silver/template/event/simple.html diff --git a/tests/template/datasets/ext_trivial/styles/silver/template/event_loop.html b/tests/template/datasets/ext_trivial/styles/silver/template/event_loop.html new file mode 100644 index 0000000000..c70d8f86d7 --- /dev/null +++ b/tests/template/datasets/ext_trivial/styles/silver/template/event_loop.html @@ -0,0 +1,3 @@ +<!-- BEGIN event_loop --> +event_loop<!-- EVENT test_event_loop --> +<!-- END event_loop --> diff --git a/tests/template/includephp_test.php b/tests/template/includephp_test.php index ff7b890d11..a0dd8368cf 100644 --- a/tests/template/includephp_test.php +++ b/tests/template/includephp_test.php @@ -46,7 +46,7 @@ class phpbb_template_includephp_test extends phpbb_template_template_test_case $this->setup_engine(array('tpl_allow_php' => true)); - $this->style->set_custom_style('tests', $cache_dir, array(), ''); + $this->template->set_custom_style('tests', $cache_dir); $this->run_template('includephp_absolute.html', array(), array(), array(), "Path is absolute.\ntesting included php"); diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index 857807bf89..801f0055c8 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -80,6 +80,16 @@ Zeta test event in all', array(), 'two in silver in omega', ), + array( + 'EVENT in loop', + 'ext_trivial', + array('silver'), + 'event_loop.html', + array(), + array('event_loop' => array(array(), array(), array())), + array(), + 'event_loop0|event_loop1|event_loop2', + ), ); } @@ -103,13 +113,10 @@ Zeta test event in all', $config = new \phpbb\config\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\twig\twig($phpbb_root_path, $phpEx, $config, $user, new \phpbb\template\context, $this->extension_manager); - $this->style_provider = new \phpbb\style\path_provider(); - $this->style = new \phpbb\style\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, ''); + $this->template->set_custom_style(((!empty($style_names)) ? $style_names : 'silver'), array($this->template_path)); } } diff --git a/tests/template/template_test.php b/tests/template/template_test.php index 802f0c19ba..2cca20f4c2 100644 --- a/tests/template/template_test.php +++ b/tests/template/template_test.php @@ -64,6 +64,13 @@ class phpbb_template_template_test extends phpbb_template_template_test_case ), array( 'if.html', + array('S_OTHER_OTHER_VALUE' => true), + array(), + array(), + '|S_OTHER_OTHER_VALUE|!false', + ), + array( + 'if.html', array('S_VALUE' => false, 'S_OTHER_VALUE' => true), array(), array(), @@ -93,49 +100,49 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array( 'loop.html', array(), - array('loop' => array(array())), + array('test_loop' => array(array())), array(), "loop\nloop", ), array( 'loop.html', array(), - array('loop' => array(array(), array()), 'loop.block' => array(array())), + array('test_loop' => array(array(), array()), 'test_loop.block' => array(array())), array(), "loop\nloop\nloop\nloop", ), array( 'loop.html', array(), - array('loop' => array(array(), array()), 'loop.block' => array(array()), 'block' => array(array(), array())), + array('test_loop' => array(array(), array()), 'test_loop.block' => array(array()), 'block' => array(array(), array())), array(), "loop\nloop\nloop\nloop\nloop#0-block#0\nloop#0-block#1\nloop#1-block#0\nloop#1-block#1", ), array( 'loop_vars.html', array(), - array('loop' => array(array('VARIABLE' => 'x'))), + array('test_loop' => array(array('VARIABLE' => 'x'))), array(), "first\n0 - a\nx - b\nset\nlast", ), array( 'loop_vars.html', array(), - array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), + array('test_loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y'))), array(), "first\n0 - a\nx - b\nset\n1 - a\ny - b\nset\nlast", ), array( 'loop_vars.html', array(), - array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), + array('test_loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'test_loop.inner' => array(array(), array())), array(), "first\n0 - a\nx - b\nset\n1 - a\ny - b\nset\nlast\n0 - c\n1 - c\nlast inner", ), array( 'loop_advanced.html', array(), - array('loop' => array(array(), array(), array(), array(), array(), array(), array())), + array('test_loop' => array(array(), array(), array(), array(), array(), array(), array())), array(), "101234561\nx\n101234561\nx\n101234561\nx\n1234561\nx\n1\nx\n101\nx\n234\nx\n10\nx\n561\nx\n561", ), @@ -149,14 +156,14 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array( 'define.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('test_loop' => array(array(), array(), array(), array(), array(), array(), array()), 'test' => array(array()), 'test.deep' => array(array()), 'test.deep.defines' => array(array())), array(), - "xyz\nabc\nabc\nbar\nbar\nabc", + "xyz\nabc\n\$VALUE == 'abc'\n(\$VALUE == 'abc')\n!\$DOESNT_EXIST\n(!\$DOESNT_EXIST)\nabc\nbar\nbar\nabc\ntest!@#$%^&*()_-=+{}[]:;\",<.>/?[]|foobar|", ), 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('test_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", ), @@ -200,7 +207,7 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array( 'include_loop.html', array(), - array('loop' => array(array('NESTED_FILE' => 'include_loop1.html')), 'loop.inner' => array(array('NESTED_FILE' => 'include_loop1.html'), array('NESTED_FILE' => 'include_loop2.html'), array('NESTED_FILE' => 'include_loop3.html'))), + array('test_loop' => array(array('NESTED_FILE' => 'include_loop1.html')), 'test_loop.inner' => array(array('NESTED_FILE' => 'include_loop1.html'), array('NESTED_FILE' => 'include_loop2.html'), array('NESTED_FILE' => 'include_loop3.html'))), array(), "1\n_1\n_02\n_3", ), @@ -221,8 +228,8 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array( 'loop_vars.html', array(), - array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), - array('loop'), + array('test_loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'test_loop.inner' => array(array(), array())), + array('test_loop'), '', ), array( @@ -230,12 +237,12 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array('VARIABLE' => 'variable.html'), array(), array(), - 'variable.html', + "variable.html\nvariable.html\nvariable.html", ), array( 'include_loop_define.html', array('VARIABLE' => 'value'), - array('loop' => array(array('NESTED_FILE' => 'variable.html'))), + array('test_loop' => array(array('NESTED_FILE' => 'variable.html'))), array(), 'value', ), @@ -243,8 +250,8 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array( 'loop_vars.html', array(), - array('loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'loop.inner' => array(array(), array())), - array('loop.inner'), + array('test_loop' => array(array('VARIABLE' => 'x'), array('VARIABLE' => 'y')), 'test_loop.inner' => array(array(), array())), + array('test_loop.inner'), "first\n0\n0\n2\nx\nset\n1\n1\n2\ny\nset\nlast", ),*/ array( @@ -295,7 +302,7 @@ class phpbb_template_template_test extends phpbb_template_template_test_case array( 'loop_size.html', array(), - array('loop' => array(array()), 'empty_loop' => array()), + array('test_loop' => array(array()), 'empty_loop' => array()), array(), "nonexistent = 0\n! nonexistent\n\nempty = 0\n! empty\nloop\n\nin loop", ), @@ -410,7 +417,7 @@ class phpbb_template_template_test extends phpbb_template_template_test_case $this->setup_engine(array('tpl_allow_php' => true)); - $this->style->set_custom_style('tests', $cache_dir, array(), ''); + $this->template->set_custom_style('tests', $cache_dir); $this->run_template('php.html', array(), array(), array(), 'test'); } diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index 97c0728875..7251151ef0 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -11,11 +11,8 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; class phpbb_template_template_test_case extends phpbb_test_case { - protected $style; protected $template; protected $template_path; - protected $style_resource_locator; - protected $style_provider; protected $user; protected $test_path = 'tests/template'; @@ -67,11 +64,8 @@ class phpbb_template_template_test_case extends phpbb_test_case $this->user = new \phpbb\user; $this->template_path = $this->test_path . '/templates'; - $this->style_resource_locator = new \phpbb\style\resource_locator(); - $this->style_provider = new \phpbb\style\path_provider(); $this->template = new \phpbb\template\twig\twig($phpbb_root_path, $phpEx, $config, $this->user, new \phpbb\template\context()); - $this->style = new \phpbb\style\style($phpbb_root_path, $phpEx, $config, $this->user, $this->style_resource_locator, $this->style_provider, $this->template); - $this->style->set_custom_style('tests', $this->template_path, array(), ''); + $this->template->set_custom_style('tests', $this->template_path); } protected function setUp() diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php index 9f97e92fd2..461534a49d 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -20,10 +20,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat $this->template_path = $this->test_path . '/templates'; $this->parent_template_path = $this->test_path . '/parent_templates'; - $this->style_resource_locator = new \phpbb\style\resource_locator(); - $this->style_provider = new \phpbb\style\path_provider(); - $this->template = new \phpbb\template\twig\twig($phpbb_root_path, $phpEx, $config, $user, new \phpbb\template\context()); - $this->style = new \phpbb\style\style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); - $this->style->set_custom_style('tests', array($this->template_path, $this->parent_template_path), array(), ''); + $this->template = new \phpbb\template\twig($phpbb_root_path, $phpEx, $config, $user, new \phpbb\template\context()); + $this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path)); } } diff --git a/tests/template/templates/define.html b/tests/template/templates/define.html index 4e6d0ee793..bc20c02ed1 100644 --- a/tests/template/templates/define.html +++ b/tests/template/templates/define.html @@ -2,8 +2,27 @@ {$VALUE} <!-- DEFINE $VALUE = 'abc' --> {$VALUE} +<!-- IF $VALUE != 'abc' --> +$VALUE != 'abc' +<!-- ELSEIF $VALUE == 'abc' --> +$VALUE == 'abc' +<!-- ENDIF --> +<!-- IF ($VALUE == 'abc') --> +($VALUE == 'abc') +<!-- ENDIF --> +<!-- IF !$DOESNT_EXIST --> +!$DOESNT_EXIST +<!-- ENDIF --> +<!-- IF (!$DOESNT_EXIST) --> +(!$DOESNT_EXIST) +<!-- ENDIF --> <!-- INCLUDE define_include.html --> {$INCLUDED_VALUE} {$VALUE} <!-- UNDEFINE $VALUE --> {$VALUE} +<!-- DEFINE $VALUE = 'test!@#$%^&*()_-=+{}[]:;",<.>/?' --> +{$VALUE} +<!-- DEFINE $VALUE = '' --> +[{$VALUE}] +<!-- DEFINE $TEST -->foobar<!-- ENDDEFINE -->|{$TEST}| diff --git a/tests/template/templates/if.html b/tests/template/templates/if.html index c010aff7fa..f6ab6e575a 100644 --- a/tests/template/templates/if.html +++ b/tests/template/templates/if.html @@ -2,6 +2,8 @@ 1 <!-- ELSEIF S_OTHER_VALUE --> 2 +<!-- ELSE IF S_OTHER_OTHER_VALUE --> +|S_OTHER_OTHER_VALUE| <!-- ELSE --> 03 <!-- ENDIF --> diff --git a/tests/template/templates/include_define_variable.html b/tests/template/templates/include_define_variable.html index aff9b574c2..6052657c97 100644 --- a/tests/template/templates/include_define_variable.html +++ b/tests/template/templates/include_define_variable.html @@ -1,2 +1,8 @@ <!-- DEFINE $DEF = '{VARIABLE}' --> <!-- INCLUDE {$DEF} --> + +<!-- DEFINE $DEF_WITH_UNDERSCORES = '{VARIABLE}' --> +<!-- INCLUDE {$DEF_WITH_UNDERSCORES} --> + +<!-- DEFINE $DEF123 = '{VARIABLE}' --> +<!-- INCLUDE {$DEF123} --> diff --git a/tests/template/templates/include_loop.html b/tests/template/templates/include_loop.html index d5c3d9bc82..5cad34b363 100644 --- a/tests/template/templates/include_loop.html +++ b/tests/template/templates/include_loop.html @@ -1,4 +1,4 @@ -<!-- BEGIN loop --> -<!-- INCLUDE {loop.NESTED_FILE} --> -<!-- BEGIN inner -->_<!-- INCLUDE {inner.NESTED_FILE} --><!-- END inner --> -<!-- END loop --> +<!-- BEGIN test_loop --> +<!-- INCLUDE {test_loop.NESTED_FILE} --> +<!-- BEGIN inner -->_<!-- INCLUDE {test_loop.inner.NESTED_FILE} --><!-- END inner --> +<!-- END test_loop --> diff --git a/tests/template/templates/include_loop_define.html b/tests/template/templates/include_loop_define.html index f539b21396..4bab09422e 100644 --- a/tests/template/templates/include_loop_define.html +++ b/tests/template/templates/include_loop_define.html @@ -1,4 +1,4 @@ -<!-- BEGIN loop --> -<!-- DEFINE $DEF = '{loop.NESTED_FILE}' --> +<!-- BEGIN test_loop --> +<!-- DEFINE $DEF = '{test_loop.NESTED_FILE}' --> <!-- INCLUDE {$DEF} --> -<!-- END loop --> +<!-- END test_loop --> diff --git a/tests/template/templates/loop.html b/tests/template/templates/loop.html index de1a10004d..f541e934df 100644 --- a/tests/template/templates/loop.html +++ b/tests/template/templates/loop.html @@ -1,21 +1,21 @@ -<!-- BEGIN loop --> +<!-- BEGIN test_loop --> loop <!-- BEGINELSE --> noloop -<!-- END loop --> +<!-- END test_loop --> -<!-- IF .loop --> +<!-- IF .test_loop --> loop <!-- ELSE --> noloop <!-- ENDIF --> -<!-- IF .loop == 2 --> +<!-- IF .test_loop == 2 --> loop <!-- ENDIF --> -<!-- BEGIN loop --> +<!-- BEGIN test_loop --> <!-- BEGIN !block --> -loop#{loop.S_ROW_COUNT}-block#{block.S_ROW_COUNT} +loop#{test_loop.S_ROW_COUNT}-block#{block.S_ROW_COUNT} <!-- END !block --> -<!-- END loop --> +<!-- END test_loop --> diff --git a/tests/template/templates/loop_advanced.html b/tests/template/templates/loop_advanced.html index c75fe55f03..1f56686eaa 100644 --- a/tests/template/templates/loop_advanced.html +++ b/tests/template/templates/loop_advanced.html @@ -1,19 +1,19 @@ -<!-- BEGIN loop -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(0) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(0) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(0,-1) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(0,-1) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(1) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(1) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(1,1) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(1,1) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(0,1) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(0,1) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(2,4) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(2,4) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(0,-7) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(0,-7) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(-2,6) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(-2,6) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> x -<!-- BEGIN loop(-2,-1) -->{loop.S_FIRST_ROW}{loop.S_ROW_COUNT}{loop.S_LAST_ROW}<!-- END loop --> +<!-- BEGIN test_loop(-2,-1) -->{test_loop.S_FIRST_ROW}{test_loop.S_ROW_COUNT}{test_loop.S_LAST_ROW}<!-- END test_loop --> diff --git a/tests/template/templates/loop_size.html b/tests/template/templates/loop_size.html index 8f581cef10..2b1fcd2dd4 100644 --- a/tests/template/templates/loop_size.html +++ b/tests/template/templates/loop_size.html @@ -22,18 +22,18 @@ ! empty <!-- ENDIF --> -<!-- IF .loop --> +<!-- IF .test_loop --> loop <!-- ENDIF --> -<!-- IF .loop == 0 --> +<!-- IF .test_loop == 0 --> loop = 0 <!-- ENDIF --> -<!-- IF ! .loop --> +<!-- IF ! .test_loop --> ! loop <!-- ENDIF --> -<!-- BEGIN loop --> +<!-- BEGIN test_loop --> in loop -<!-- END loop --> +<!-- END test_loop --> diff --git a/tests/template/templates/loop_vars.html b/tests/template/templates/loop_vars.html index 7d86d4b7b6..70a3eb2cec 100644 --- a/tests/template/templates/loop_vars.html +++ b/tests/template/templates/loop_vars.html @@ -1,13 +1,13 @@ -<!-- BEGIN loop --> -<!-- IF loop.S_FIRST_ROW -->first<!-- ENDIF --> -{loop.S_ROW_NUM} - a -{loop.VARIABLE} - b -<!-- IF loop.VARIABLE -->set<!-- ENDIF --> -<!-- IF loop.S_LAST_ROW --> +<!-- BEGIN test_loop --> +<!-- IF test_loop.S_FIRST_ROW -->first<!-- ENDIF --> +{test_loop.S_ROW_NUM} - a +{test_loop.VARIABLE} - b +<!-- IF test_loop.VARIABLE -->set<!-- ENDIF --> +<!-- IF test_loop.S_LAST_ROW --> last <!-- ENDIF --> <!-- BEGIN inner --> -{inner.S_ROW_NUM} - c -<!-- IF inner.S_LAST_ROW and inner.S_ROW_COUNT and inner.S_NUM_ROWS -->last inner<!-- ENDIF --> +{test_loop.inner.S_ROW_NUM} - c +<!-- IF test_loop.inner.S_LAST_ROW and test_loop.inner.S_ROW_COUNT and test_loop.inner.S_NUM_ROWS -->last inner<!-- ENDIF --> <!-- END inner --> -<!-- END loop --> +<!-- END test_loop --> diff --git a/tests/test_framework/phpbb_database_test_connection_manager.php b/tests/test_framework/phpbb_database_test_connection_manager.php index b6ba4e0c8c..af9bd22662 100644 --- a/tests/test_framework/phpbb_database_test_connection_manager.php +++ b/tests/test_framework/phpbb_database_test_connection_manager.php @@ -138,7 +138,7 @@ class phpbb_database_test_connection_manager catch (PDOException $e) { $cleaned_dsn = str_replace($this->config['dbpasswd'], '*password*', $dsn); - throw new Exception("Unable do connect to $cleaned_dsn using PDO with error: {$e->getMessage()}"); + throw new Exception("Unable to connect to $cleaned_dsn using PDO with error: {$e->getMessage()}"); } $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); diff --git a/tests/test_framework/phpbb_functional_test_case.php b/tests/test_framework/phpbb_functional_test_case.php index 8929e93cfb..d5d3258b1b 100644 --- a/tests/test_framework/phpbb_functional_test_case.php +++ b/tests/test_framework/phpbb_functional_test_case.php @@ -532,13 +532,10 @@ class phpbb_functional_test_case extends phpbb_test_case $phpbb_log = new \phpbb\log\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)); + $cache_driver = new phpbb_cache_driver_null(); + $phpbb_container = new phpbb_mock_container_builder(); + $phpbb_container->set('cache.driver', $cache_driver); + $phpbb_container->set('notification_manager', new phpbb_mock_notification_manager()); if (!function_exists('utf_clean_string')) { @@ -747,6 +744,27 @@ class phpbb_functional_test_case extends phpbb_test_case self::assertStringStartsWith('<!DOCTYPE', trim($content), 'Output found before DOCTYPE specification.'); } + /* + * Perform some basic assertions for an xml 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_xml($status_code = 200) + { + if ($status_code !== false) + { + self::assert_response_status_code($status_code); + } + + // Any output before the xml opening means there was an error + $content = self::$client->getResponse()->getContent(); + self::assertNotContains('[phpBB Debug]', $content); + self::assertStringStartsWith('<?xml', trim($content), 'Output found before XML specification.'); + } + /** * Heuristic function to check that the response is success. * @@ -907,7 +925,7 @@ class phpbb_functional_test_case extends phpbb_test_case $hidden_fields = array( $crawler->filter('[type="hidden"]')->each(function ($node, $i) { - return array('name' => $node->getAttribute('name'), 'value' => $node->getAttribute('value')); + return array('name' => $node->attr('name'), 'value' => $node->attr('value')); }), ); @@ -936,7 +954,7 @@ class phpbb_functional_test_case extends phpbb_test_case ); } - /* + /** * Returns the requested parameter from a URL * * @param string $url diff --git a/tests/test_framework/phpbb_session_test_case.php b/tests/test_framework/phpbb_session_test_case.php new file mode 100644 index 0000000000..e6a2b03bba --- /dev/null +++ b/tests/test_framework/phpbb_session_test_case.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 + * + */ + +require_once dirname(__FILE__) . '/../session/testable_factory.php'; +require_once dirname(__FILE__) . '/../session/testable_facade.php'; + +abstract class phpbb_session_test_case extends phpbb_database_test_case +{ + protected $session_factory; + protected $session_facade; + protected $db; + + function setUp() + { + parent::setUp(); + $this->session_factory = new phpbb_session_testable_factory; + $this->db = $this->new_dbal(); + $this->session_facade = + new phpbb_session_testable_facade($this->db, $this->session_factory); + } + + protected function check_sessions_equals($expected_sessions, $message) + { + $sql = 'SELECT session_id, session_user_id + FROM phpbb_sessions + ORDER BY session_user_id'; + + $this->assertSqlResultEquals($expected_sessions, $sql, $message); + } +} diff --git a/tests/text_processing/generate_text_for_display_test.php b/tests/text_processing/generate_text_for_display_test.php new file mode 100644 index 0000000000..a157fe7d9a --- /dev/null +++ b/tests/text_processing/generate_text_for_display_test.php @@ -0,0 +1,38 @@ +<?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__) . '/../mock/user.php'; +require_once dirname(__FILE__) . '/../mock/cache.php'; + +class phpbb_text_processing_generate_text_for_display_test extends phpbb_test_case +{ + public function setUp() + { + global $cache, $user; + + parent::setUp(); + + $cache = new phpbb_mock_cache; + + $user = new phpbb_mock_user; + $user->optionset('viewcensors', false); + } + + public function test_empty_string() + { + $this->assertSame('', generate_text_for_display('', '', '', 0)); + } + + public function test_zero_string() + { + $this->assertSame('0', generate_text_for_display('0', '', '', 0)); + } +} |