From 50761104ba6590de86ec651b5679983eaf2ff600 Mon Sep 17 00:00:00 2001 From: LEZY Thomas Date: Fri, 13 Jun 2014 11:38:02 +0200 Subject: [ticket/12684] Add doc blocks and test file for user:add PHPBB3-12684 --- tests/console/user/add_test.php | 138 +++++++++++++++++++++++++++++++++ tests/console/user/fixtures/config.xml | 43 ++++++++++ 2 files changed, 181 insertions(+) create mode 100644 tests/console/user/add_test.php create mode 100644 tests/console/user/fixtures/config.xml (limited to 'tests') diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php new file mode 100644 index 0000000000..6c28c46815 --- /dev/null +++ b/tests/console/user/add_test.php @@ -0,0 +1,138 @@ + +* @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; + +require_once dirname(__FILE__) . '/../../../phpBB/includes/functions_user.php'; +require_once dirname(__FILE__) . '/../../../phpBB/includes/functions.php'; +require_once dirname(__FILE__) . '/../../../phpBB/includes/utf/utf_tools.php'; + +class phpbb_console_command_user_add_test extends phpbb_database_test_case +{ + protected $db; + protected $config; + protected $user; + protected $passwords_manager; + protected $command_name; + protected $dialog; + + public function getDataSet() + { + return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml'); + } + + public function setUp() + { + global $db, $config, $phpbb_dispatcher, $phpbb_container; + + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $phpbb_container = new phpbb_mock_container_builder(); + $phpbb_container->set('cache.driver', new phpbb_mock_cache()); + + $config = $this->config = new \phpbb\config\config(array( + 'board_timezone' => 'UTC', + 'default_lang' => 'en', + )); + set_config(null, null, null, $this->config); + set_config_count(null, null, null, $this->config); + + $db = $this->db = $this->new_dbal(); + + $this->user = $this->getMock('\phpbb\user'); + $this->user->method('lang')->will($this->returnArgument(0)); + + $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)); + + parent::setUp(); + } + + 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('SUCCESS_ADD_USER', $command_tester->getDisplay()); + } + + public function test_add_dialog() + { + $command_tester = $this->get_command_tester(); + + $this->assertEquals(2, $this->get_user_id('Admin')); + + $this->dialog->setInputStream($this->getInputStream("bar\npass\npass\nbar@test.com\n")); + + $command_tester->execute(array( + 'command' => $this->command_name, + )); + + $this->assertNotEquals(null, $this->get_user_id('bar')); + $this->assertContains('SUCCESS_ADD_USER', $command_tester->getDisplay()); + + } + + public function get_command_tester() + { + $application = new Application(); + $application->add(new add($this->user, $this->db, $this->config, $this->passwords_manager)); + + $command = $application->find('user:add'); + $this->command_name = $command->getName(); + $this->dialog = $command->getHelper('dialog'); + return new CommandTester($command); + } + + 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/fixtures/config.xml b/tests/console/user/fixtures/config.xml new file mode 100644 index 0000000000..fed30dc20d --- /dev/null +++ b/tests/console/user/fixtures/config.xml @@ -0,0 +1,43 @@ + + + + user_id + user_permissions + username + username_clean + user_sig + + 1 + + Guest + guest + + + + 2 + + Admin + admin + + + + 3 + + Test + test + + +
+ + group_id + group_name + group_type + group_desc + + 1 + REGISTERED + 3 + foobar + +
+
-- cgit v1.2.1 From df4a620ba146d895be4910761bc030045abbae93 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 27 Aug 2014 16:56:46 +0200 Subject: [ticket/12684] Fix tests PHPBB3-12684 --- tests/console/user/add_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 6c28c46815..6f80a8658a 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -50,7 +50,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $db = $this->db = $this->new_dbal(); - $this->user = $this->getMock('\phpbb\user'); + $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); $this->user->method('lang')->will($this->returnArgument(0)); $driver_helper = new \phpbb\passwords\driver\helper($this->config); @@ -90,7 +90,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $this->assertEquals(2, $this->get_user_id('Admin')); - $this->dialog->setInputStream($this->getInputStream("bar\npass\npass\nbar@test.com\n")); + $this->dialog->setInputStream($this->getInputStream("bar\npassword\npassword\nbar@test.com\n")); $command_tester->execute(array( 'command' => $this->command_name, -- cgit v1.2.1 From 6fe084a2fd967c188bdca827a46647120a5ea58d Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 11:38:49 -0800 Subject: [ticket/12684] Updates for 3.2 API PHPBB3-12684 --- tests/console/user/add_test.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 6f80a8658a..5e4b76063e 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -27,6 +27,8 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case protected $passwords_manager; protected $command_name; protected $dialog; + protected $phpbb_root_path; + protected $php_ext; public function getDataSet() { @@ -35,22 +37,26 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case public function setUp() { - global $db, $config, $phpbb_dispatcher, $phpbb_container; + global $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()); + + $cache = $phpbb_container->get('cache.driver'); $config = $this->config = new \phpbb\config\config(array( 'board_timezone' => 'UTC', 'default_lang' => 'en', )); - set_config(null, null, null, $this->config); - set_config_count(null, null, null, $this->config); $db = $this->db = $this->new_dbal(); - $this->user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); + $user = $this->user = $this->getMock('\phpbb\user', array(), array( + new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)), + '\phpbb\datetime') + ); $this->user->method('lang')->will($this->returnArgument(0)); $driver_helper = new \phpbb\passwords\driver\helper($this->config); @@ -64,6 +70,9 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $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; + parent::setUp(); } @@ -104,7 +113,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case public function get_command_tester() { $application = new Application(); - $application->add(new add($this->user, $this->db, $this->config, $this->passwords_manager)); + $application->add(new add($this->user, $this->db, $this->config, $this->passwords_manager, $this->phpbb_root_path, $this->php_ext)); $command = $application->find('user:add'); $this->command_name = $command->getName(); -- cgit v1.2.1 From d373428180e884f03a830aa69fe8ff2cd6a5140a Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 11:41:47 -0800 Subject: [ticket/12684] Add input validation PHPBB3-12684 --- tests/console/user/add_test.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests') diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 5e4b76063e..280adb101d 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -49,6 +49,11 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $config = $this->config = new \phpbb\config\config(array( 'board_timezone' => 'UTC', 'default_lang' => 'en', + '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(); @@ -110,6 +115,24 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case } + 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()); + } + public function get_command_tester() { $application = new Application(); -- cgit v1.2.1 From f32b4c0547a596fcdd935560b23ed660f3d3f87c Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 11:42:23 -0800 Subject: [ticket/12684] Add send email option PHPBB3-12684 --- tests/console/user/add_test.php | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 280adb101d..a673d5eb6c 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -49,6 +49,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $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, -- cgit v1.2.1 From fe31060fca4a07696029c50af2c74c88ab5d85fe Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 14:23:32 -0800 Subject: [ticket/12684] Fix tests PHPBB3-12684 --- tests/console/user/add_test.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index a673d5eb6c..8fc921babf 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -24,9 +24,10 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case protected $db; protected $config; protected $user; + protected $language; protected $passwords_manager; protected $command_name; - protected $dialog; + protected $question; protected $phpbb_root_path; protected $php_ext; @@ -59,11 +60,16 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $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( - new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)), - '\phpbb\datetime') - ); - $this->user->method('lang')->will($this->returnArgument(0)); + $this->language, + '\phpbb\datetime' + )); $driver_helper = new \phpbb\passwords\driver\helper($this->config); $passwords_drivers = array( @@ -105,7 +111,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case $this->assertEquals(2, $this->get_user_id('Admin')); - $this->dialog->setInputStream($this->getInputStream("bar\npassword\npassword\nbar@test.com\n")); + $this->question->setInputStream($this->getInputStream("bar\npassword\npassword\nbar@test.com\n")); $command_tester->execute(array( 'command' => $this->command_name, @@ -137,11 +143,11 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case public function get_command_tester() { $application = new Application(); - $application->add(new add($this->user, $this->db, $this->config, $this->passwords_manager, $this->phpbb_root_path, $this->php_ext)); + $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(); - $this->dialog = $command->getHelper('dialog'); + $this->question = $command->getHelper('question'); return new CommandTester($command); } -- cgit v1.2.1 From 0ca4484525f69ea5e7c5cd28f671364604725e40 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 29 Feb 2016 21:03:32 -0800 Subject: [ticket/12684] Move all lang keys to cli PHPBB3-12684 --- tests/console/user/add_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index 8fc921babf..de146ef6ec 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -102,7 +102,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case )); $this->assertNotEquals(null, $this->get_user_id('foo')); - $this->assertContains('SUCCESS_ADD_USER', $command_tester->getDisplay()); + $this->assertContains('CLI_USER_ADD_SUCCESS', $command_tester->getDisplay()); } public function test_add_dialog() @@ -118,7 +118,7 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case )); $this->assertNotEquals(null, $this->get_user_id('bar')); - $this->assertContains('SUCCESS_ADD_USER', $command_tester->getDisplay()); + $this->assertContains('CLI_USER_ADD_SUCCESS', $command_tester->getDisplay()); } -- cgit v1.2.1 From 00c2efca605b4dbd323221a3dab2110c81ae3dcd Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 21 Mar 2016 13:54:41 -0700 Subject: [ticket/12684] Refactor a test PHPBB3-12684 --- tests/console/user/add_test.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/console/user/add_test.php b/tests/console/user/add_test.php index de146ef6ec..00134c18cd 100644 --- a/tests/console/user/add_test.php +++ b/tests/console/user/add_test.php @@ -143,7 +143,15 @@ class phpbb_console_command_user_add_test extends phpbb_database_test_case public function get_command_tester() { $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)); + $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(); -- cgit v1.2.1