diff options
author | Maat <maat-pub@mageia.biz> | 2020-05-08 21:52:11 +0200 |
---|---|---|
committer | Maat <maat-pub@mageia.biz> | 2020-05-08 21:52:11 +0200 |
commit | 8ea437e30605e0f66b5220bf904a61d7c1d11ddd (patch) | |
tree | e0db2bb4a012d5b06a633160b19f62f4868ecd28 /tests/console/user | |
parent | 36bc1870f21fac04736a1049c1d5b8e127d729f4 (diff) | |
parent | 2fdd46b36431ae0f58bb2e78e42553168db9a0ff (diff) | |
download | forums-8ea437e30605e0f66b5220bf904a61d7c1d11ddd.tar forums-8ea437e30605e0f66b5220bf904a61d7c1d11ddd.tar.gz forums-8ea437e30605e0f66b5220bf904a61d7c1d11ddd.tar.bz2 forums-8ea437e30605e0f66b5220bf904a61d7c1d11ddd.tar.xz forums-8ea437e30605e0f66b5220bf904a61d7c1d11ddd.zip |
Merge remote-tracking branch 'upstream/prep-release-3.2.9'
Diffstat (limited to 'tests/console/user')
-rw-r--r-- | tests/console/user/activate_test.php | 86 | ||||
-rw-r--r-- | tests/console/user/add_test.php | 134 | ||||
-rw-r--r-- | tests/console/user/base.php | 126 | ||||
-rw-r--r-- | tests/console/user/delete_test.php | 93 | ||||
-rw-r--r-- | tests/console/user/fixtures/config.xml | 63 | ||||
-rw-r--r-- | tests/console/user/reclean_test.php | 49 |
6 files changed, 551 insertions, 0 deletions
diff --git a/tests/console/user/activate_test.php b/tests/console/user/activate_test.php new file mode 100644 index 0000000000..1588a76e47 --- /dev/null +++ b/tests/console/user/activate_test.php @@ -0,0 +1,86 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\user\activate; + +require_once dirname(__FILE__) . '/base.php'; + +class phpbb_console_user_activate_test extends phpbb_console_user_base +{ + protected $notifications; + + public function setUp() + { + parent::setUp(); + + $this->notifications = $this->getMockBuilder('\phpbb\notification\manager') + ->disableOriginalConstructor() + ->getMock(); + } + + public function get_command_tester() + { + $application = new Application(); + $application->add(new activate( + $this->user, + $this->db, + $this->config, + $this->language, + $this->log, + $this->notifications, + $this->user_loader, + $this->phpbb_root_path, + $this->php_ext + )); + + $command = $application->find('user:activate'); + $this->command_name = $command->getName(); + + return new CommandTester($command); + } + + public function activate_test_data() + { + return array( + // Test an inactive user + array('Test', false, 'USER_ADMIN_ACTIVATED'), + array('Test', true, 'CLI_DESCRIPTION_USER_ACTIVATE_INACTIVE'), + + // Test an active user + array('Test 2', false, 'CLI_DESCRIPTION_USER_ACTIVATE_ACTIVE'), + array('Test 2', true, 'USER_ADMIN_DEACTIVED'), + + // Test a non existent user + array('Foo', false, 'NO_USER'), + array('Foo', true, 'NO_USER'), + ); + } + + /** + * @dataProvider activate_test_data + */ + public function test_activate($username, $deactivate, $expected) + { + $command_tester = $this->get_command_tester(); + + $command_tester->execute(array( + 'command' => $this->command_name, + 'username' => $username, + '--deactivate' => $deactivate, + )); + + $this->assertContains($expected, $command_tester->getDisplay()); + } +} diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php new file mode 100644 index 0000000000..bdfb8a8d2a --- /dev/null +++ b/tests/console/user/add_test.php @@ -0,0 +1,134 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\user\add; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\Question; + +require_once dirname(__FILE__) . '/base.php'; + +class phpbb_console_user_add_test extends phpbb_console_user_base +{ + public function get_command_tester($question_answers = []) + { + $application = new Application(); + $application->add(new add( + $this->user, + $this->db, + $this->config, + $this->language, + $this->passwords_manager, + $this->phpbb_root_path, + $this->php_ext + )); + + $command = $application->find('user:add'); + $this->command_name = $command->getName(); + + if (!empty($question_answers)) + { + $ask = function(InputInterface $input, OutputInterface $output, Question $question) use ($question_answers) + { + $text = $question->getQuestion(); + + // handle a question + foreach ($question_answers as $expected_question => $answer) + { + if (strpos($text, $expected_question) !== false) + { + $response = $answer; + } + } + + if (!isset($response)) + { + throw new \RuntimeException('Was asked for input on an unhandled question: ' . $text); + } + + $output->writeln(print_r($response, true)); + return $response; + }; + $helper = $this->getMock('\Symfony\Component\Console\Helper\QuestionHelper', array('ask')); + $helper->expects($this->any()) + ->method('ask') + ->will($this->returnCallback($ask)); + $this->question = $helper; + $command->getHelperSet()->set($helper, 'question'); + } + else + { + $this->question = $command->getHelper('question'); + } + + return new CommandTester($command); + } + + public function test_add_no_dialog() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(2, $this->get_user_id('Admin')); + + $command_tester->execute(array( + 'command' => $this->command_name, + '--username' => 'foo', + '--password' => 'bar', + '--email' => 'foo@test.com' + )); + + $this->assertNotEquals(null, $this->get_user_id('foo')); + $this->assertContains('CLI_USER_ADD_SUCCESS', $command_tester->getDisplay()); + } + + public function test_add_dialog() + { + $command_tester = $this->get_command_tester([ + 'USERNAME' => 'bar', + 'PASSWORD' => 'password', + 'EMAIL_ADDRESS' => 'bar@test.com', + ]); + + $this->assertEquals(2, $this->get_user_id('Admin')); + + $this->question->setInputStream($this->getInputStream("bar\npassword\npassword\nbar@test.com\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + )); + + $this->assertNotEquals(null, $this->get_user_id('bar')); + $this->assertContains('CLI_USER_ADD_SUCCESS', $command_tester->getDisplay()); + + } + + public function test_add_no_dialog_invalid() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(3, $this->get_user_id('Test')); + + $command_tester->execute(array( + 'command' => $this->command_name, + '--username' => 'Test', + '--password' => '1', + '--email' => 'foo' + )); + + $this->assertContains('USERNAME_TAKEN', $command_tester->getDisplay()); + $this->assertContains('TOO_SHORT', $command_tester->getDisplay()); + $this->assertContains('EMAIL_INVALID', $command_tester->getDisplay()); + } +} diff --git a/tests/console/user/base.php b/tests/console/user/base.php new file mode 100644 index 0000000000..ad328ac893 --- /dev/null +++ b/tests/console/user/base.php @@ -0,0 +1,126 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +abstract class phpbb_console_user_base extends phpbb_database_test_case +{ + protected $db; + protected $config; + protected $user; + protected $language; + protected $log; + protected $passwords_manager; + protected $command_name; + protected $question; + protected $user_loader; + protected $phpbb_root_path; + protected $php_ext; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function setUp() + { + global $auth, $db, $cache, $config, $user, $phpbb_dispatcher, $phpbb_container, $phpbb_root_path, $phpEx; + + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $phpbb_container = new phpbb_mock_container_builder(); + $phpbb_container->set('cache.driver', new phpbb_mock_cache()); + $phpbb_container->set('notification_manager', new phpbb_mock_notification_manager()); + + $auth = $this->getMock('\phpbb\auth\auth'); + + $cache = $phpbb_container->get('cache.driver'); + + $config = $this->config = new \phpbb\config\config(array( + 'board_timezone' => 'UTC', + 'default_lang' => 'en', + 'email_enable' => false, + 'min_name_chars' => 3, + 'max_name_chars' => 10, + 'min_pass_chars' => 3, + 'max_pass_chars' => 10, + 'pass_complex' => 'PASS_TYPE_ANY', + )); + + $db = $this->db = $this->new_dbal(); + + $this->language = $this->getMockBuilder('\phpbb\language\language') + ->disableOriginalConstructor() + ->getMock(); + $this->language->expects($this->any()) + ->method('lang') + ->will($this->returnArgument(0)); + $user = $this->user = $this->getMock('\phpbb\user', array(), array( + $this->language, + '\phpbb\datetime' + )); + + $this->user_loader = new \phpbb\user_loader($db, $phpbb_root_path, $phpEx, USERS_TABLE); + + $driver_helper = new \phpbb\passwords\driver\helper($this->config); + $passwords_drivers = array( + 'passwords.driver.bcrypt_2y' => new \phpbb\passwords\driver\bcrypt_2y($this->config, $driver_helper), + 'passwords.driver.bcrypt' => new \phpbb\passwords\driver\bcrypt($this->config, $driver_helper), + 'passwords.driver.salted_md5' => new \phpbb\passwords\driver\salted_md5($this->config, $driver_helper), + 'passwords.driver.phpass' => new \phpbb\passwords\driver\phpass($this->config, $driver_helper), + ); + + $passwords_helper = new \phpbb\passwords\helper; + $this->passwords_manager = new \phpbb\passwords\manager($this->config, $passwords_drivers, $passwords_helper, array_keys($passwords_drivers)); + + $this->phpbb_root_path = $phpbb_root_path; + $this->php_ext = $phpEx; + + $this->log = $this->getMockBuilder('\phpbb\log\log') + ->disableOriginalConstructor() + ->getMock(); + + $phpbb_container->set('auth.provider.db', new phpbb_mock_auth_provider()); + $provider_collection = new \phpbb\auth\provider_collection($phpbb_container, $config); + $provider_collection->add('auth.provider.db'); + $phpbb_container->set( + 'auth.provider_collection', + $provider_collection + ); + $phpbb_container->setParameter('tables.auth_provider_oauth_token_storage', 'phpbb_oauth_tokens'); + $phpbb_container->setParameter('tables.auth_provider_oauth_states', 'phpbb_oauth_states'); + $phpbb_container->setParameter('tables.auth_provider_oauth_account_assoc', 'phpbb_oauth_accounts'); + + $phpbb_container->setParameter('tables.user_notifications', 'phpbb_user_notifications'); + + parent::setUp(); + } + + public function get_user_id($username) + { + $sql = 'SELECT user_id + FROM ' . USERS_TABLE . ' + WHERE ' . 'username = ' . "'" . $username . "'"; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + return $row['user_id']; + } + + public function getInputStream($input) + { + $stream = fopen('php://memory', 'r+', false); + fputs($stream, $input); + rewind($stream); + + return $stream; + } +} diff --git a/tests/console/user/delete_test.php b/tests/console/user/delete_test.php new file mode 100644 index 0000000000..88f91afab1 --- /dev/null +++ b/tests/console/user/delete_test.php @@ -0,0 +1,93 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\user\delete; + +require_once dirname(__FILE__) . '/base.php'; + +class phpbb_console_user_delete_test extends phpbb_console_user_base +{ + public function get_command_tester() + { + $application = new Application(); + $application->add(new delete( + $this->user, + $this->db, + $this->language, + $this->log, + $this->user_loader, + $this->phpbb_root_path, + $this->php_ext + )); + + $command = $application->find('user:delete'); + $this->command_name = $command->getName(); + $this->question = $command->getHelper('question'); + + return new CommandTester($command); + } + + public function test_delete() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(3, $this->get_user_id('Test')); + + $this->question->setInputStream($this->getInputStream("yes\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + 'username' => 'Test', + '--delete-posts' => false, + )); + + $this->assertNull($this->get_user_id('Test')); + $this->assertContains('USER_DELETED', $command_tester->getDisplay()); + } + + public function test_delete_non_user() + { + $command_tester = $this->get_command_tester(); + + $this->assertNull($this->get_user_id('Foo')); + + $this->question->setInputStream($this->getInputStream("yes\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + 'username' => 'Foo', + '--delete-posts' => false, + )); + + $this->assertContains('NO_USER', $command_tester->getDisplay()); + } + + public function test_delete_cancel() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(3, $this->get_user_id('Test')); + + $this->question->setInputStream($this->getInputStream("no\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + 'username' => 'Test', + '--delete-posts' => false, + )); + + $this->assertNotNull($this->get_user_id('Test')); + } +} diff --git a/tests/console/user/fixtures/config.xml b/tests/console/user/fixtures/config.xml new file mode 100644 index 0000000000..a988ba463f --- /dev/null +++ b/tests/console/user/fixtures/config.xml @@ -0,0 +1,63 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_users"> + <column>user_id</column> + <column>user_permissions</column> + <column>username</column> + <column>username_clean</column> + <column>user_sig</column> + <column>user_type</column> + <row> + <value>1</value> + <value></value> + <value>Guest</value> + <value>guest</value> + <value></value> + <value>0</value> + </row> + <row> + <value>2</value> + <value></value> + <value>Admin</value> + <value>admin</value> + <value></value> + <value>3</value> + </row> + <row> + <value>3</value> + <value></value> + <value>Test</value> + <value>test</value> + <value></value> + <value>1</value> + </row> + <row> + <value>4</value> + <value></value> + <value>Test 2</value> + <value>test 2</value> + <value></value> + <value>0</value> + </row> + <row> + <value>5</value> + <value></value> + <value>Test Unclean</value> + <value>Test Unclean</value> + <value></value> + <value>0</value> + </row> + </table> + <table name="phpbb_groups"> + <column>group_id</column> + <column>group_name</column> + <column>group_type</column> + <column>group_desc</column> + <row> + <value>1</value> + <value>REGISTERED</value> + <value>3</value> + <value>foobar</value> + </row> + </table> +</dataset> diff --git a/tests/console/user/reclean_test.php b/tests/console/user/reclean_test.php new file mode 100644 index 0000000000..1bf0b8ef5a --- /dev/null +++ b/tests/console/user/reclean_test.php @@ -0,0 +1,49 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +use Symfony\Component\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use phpbb\console\command\user\reclean; + +require_once dirname(__FILE__) . '/base.php'; + +class phpbb_console_user_reclean_test extends phpbb_console_user_base +{ + public function get_command_tester() + { + $application = new Application(); + $application->add(new reclean( + $this->user, + $this->db, + $this->language + )); + + $command = $application->find('user:reclean'); + $this->command_name = $command->getName(); + + return new CommandTester($command); + } + + public function test_reclean() + { + $command_tester = $this->get_command_tester(); + + $exit_status = $command_tester->execute(array('command' => $this->command_name)); + $this->assertSame(0, $exit_status); + + $result = $this->db->sql_query('SELECT user_id FROM ' . USERS_TABLE . " WHERE username_clean = 'test unclean'"); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + $this->assertNotNull($row['user_id']); + } +} |