From ed6bf75f5a78a346e3c3192b2bab79450e206598 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Thu, 28 Nov 2013 00:12:38 +0000 Subject: Add unit tests for usermanager import & export functions --- lib/plugins/usermanager/_test/csv_export.test.php | 59 ++++++++ lib/plugins/usermanager/_test/csv_import.test.php | 155 ++++++++++++++++++++++ lib/plugins/usermanager/_test/mocks.class.php | 43 ++++++ 3 files changed, 257 insertions(+) create mode 100644 lib/plugins/usermanager/_test/csv_export.test.php create mode 100644 lib/plugins/usermanager/_test/csv_import.test.php create mode 100644 lib/plugins/usermanager/_test/mocks.class.php (limited to 'lib/plugins/usermanager/_test') diff --git a/lib/plugins/usermanager/_test/csv_export.test.php b/lib/plugins/usermanager/_test/csv_export.test.php new file mode 100644 index 000000000..d0942a861 --- /dev/null +++ b/lib/plugins/usermanager/_test/csv_export.test.php @@ -0,0 +1,59 @@ +usermanager = new admin_mock_usermanager(); + parent::setUp(); + } + + /** + * based on standard test user/conf setup + * + * users per _test/conf/users.auth.php + * expected to be: testuser:179ad45c6ce2cb97cf1029e212046e81:Arthur Dent:arthur@example.com + */ + function test_export() { + $expected = 'User,"Real Name",Email,Groups +testuser,"Arthur Dent",arthur@example.com, +'; + $this->assertEquals($expected, $this->usermanager->tryExport()); + } + + /** + * when configured to use a different locale, the column headings in the first line of the + * exported csv data should reflect the langauge strings of that locale + */ + function test_export_withlocale(){ + global $conf; + $old_conf = $conf; + $conf['lang'] = 'de'; + + $this->usermanager->localised = false; + $this->usermanager->setupLocale(); + + $conf = $old_conf; + + $expected = 'Benutzername,"Voller Name",E-Mail,Gruppen +testuser,"Arthur Dent",arthur@example.com, +'; + $this->assertEquals($expected, $this->usermanager->tryExport()); + } + + function test_export_withfilter(){ + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + +} diff --git a/lib/plugins/usermanager/_test/csv_import.test.php b/lib/plugins/usermanager/_test/csv_import.test.php new file mode 100644 index 000000000..5deb201ba --- /dev/null +++ b/lib/plugins/usermanager/_test/csv_import.test.php @@ -0,0 +1,155 @@ +importfile = tempnam(TMP_DIR, 'csv'); + + $this->old_files = $_FILES; + $_FILES = array( + 'import' => array( + 'name' => 'import.csv', + 'tmp_name' => $this->importfile, + 'type' => 'text/plain', + 'size' => 1, + 'error' => 0, + ), + ); + + $this->usermanager = new admin_mock_usermanager(); + parent::setUp(); + } + + function tearDown() { + $_FILES = $this->old_files; + parent::tearDown(); + } + + function doImportTest($importCsv, $expectedResult, $expectedNewUsers, $expectedFailures) { + global $auth; + $before_users = $auth->retrieveUsers(); + + io_savefile($this->importfile, $importCsv); + $result = $this->usermanager->tryImport(); + + $after_users = $auth->retrieveUsers(); + $import_count = count($after_users) - count($before_users); + $new_users = array_diff_key($after_users, $before_users); + $diff_users = array_diff_assoc($after_users, $before_users); + + $expectedCount = count($expectedNewUsers); + + $this->assertEquals($expectedResult, $result); // import result as expected + $this->assertEquals($expectedCount, $import_count); // number of new users matches expected number imported + $this->assertEquals($expectedNewUsers, $this->stripPasswords($new_users)); // new user data matches imported user data + $this->assertEquals($expectedCount, $this->countPasswords($new_users)); // new users have a password + $this->assertEquals($expectedCount, $this->usermanager->mock_email_notifications_sent); // new users notified of their passwords + $this->assertEquals($new_users, $diff_users); // no other users were harmed in the testing of this import + $this->assertEquals($expectedFailures, $this->usermanager->getImportFailures()); // failures as expected + } + + function test_import() { + $csv = 'User,"Real Name",Email,Groups +importuser,"Ford Prefect",ford@example.com,user +'; + $expected = array( + 'importuser' => array( + 'name' => 'Ford Prefect', + 'mail' => 'ford@example.com', + 'grps' => array('user'), + ), + ); + + $this->doImportTest($csv, true, $expected, array()); + } + + function test_importExisting() { + $csv = 'User,"Real Name",Email,Groups +importuser,"Ford Prefect",ford@example.com,user +'; + $failures = array( + '2' => array( + 'error' => $this->usermanager->lang['import_error_create'], + 'user' => array( + 'importuser', + 'Ford Prefect', + 'ford@example.com', + 'user', + ), + 'orig' => 'importuser,"Ford Prefect",ford@example.com,user'.NL, + ), + ); + + $this->doImportTest($csv, true, array(), $failures); + } + + function test_importUtf8() { + $csv = 'User,"Real Name",Email,Groups +importutf8,"Førd Prefect",ford@example.com,user +'; + $expected = array( + 'importutf8' => array( + 'name' => 'Førd Prefect', + 'mail' => 'ford@example.com', + 'grps' => array('user'), + ), + ); + + $this->doImportTest($csv, true, $expected, array()); + } + + /** + * utf8: u+00F8 (ø) <=> 0xF8 :iso-8859-1 + */ + function test_importIso8859() { + $csv = 'User,"Real Name",Email,Groups +importiso8859,"F'.chr(0xF8).'rd Prefect",ford@example.com,user +'; + $expected = array( + 'importiso8859' => array( + 'name' => 'Førd Prefect', + 'mail' => 'ford@example.com', + 'grps' => array('user'), + ), + ); + + $this->doImportTest($csv, true, $expected, array()); + } + + private function stripPasswords($array){ + foreach ($array as $user => $data) { + unset($array[$user]['pass']); + } + return $array; + } + + private function countPasswords($array){ + $count = 0; + foreach ($array as $user => $data) { + if (!empty($data['pass'])) { + $count++; + } + } + return $count; + } + +} + diff --git a/lib/plugins/usermanager/_test/mocks.class.php b/lib/plugins/usermanager/_test/mocks.class.php new file mode 100644 index 000000000..edf5438b2 --- /dev/null +++ b/lib/plugins/usermanager/_test/mocks.class.php @@ -0,0 +1,43 @@ +_import_failures; + } + + public function tryExport() { + ob_start(); + $this->_export(); + return ob_get_clean(); + } + + public function tryImport() { + return $this->_import(); + } + + // no need to send email notifications (mostly) + protected function _notifyUser($user, $password, $status_alert=true) { + if ($this->mock_email_notifications) { + $this->mock_email_notifications_sent++; + return true; + } else { + return parent::_notifyUser($user, $password, $status_alert); + } + } + + protected function _isUploadedFile($file) { + return file_exists($file); + } +} + -- cgit v1.2.3 From 211955bef84071b025e83bdeeefe496a5002fdd8 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Thu, 28 Nov 2013 00:49:31 +0000 Subject: test import against auth plugin which doesn't allow adding users --- lib/plugins/usermanager/_test/csv_import.test.php | 18 +++++++++++++++++- lib/plugins/usermanager/_test/mocks.class.php | 7 +++++++ 2 files changed, 24 insertions(+), 1 deletion(-) (limited to 'lib/plugins/usermanager/_test') diff --git a/lib/plugins/usermanager/_test/csv_import.test.php b/lib/plugins/usermanager/_test/csv_import.test.php index 5deb201ba..1f0ee7436 100644 --- a/lib/plugins/usermanager/_test/csv_import.test.php +++ b/lib/plugins/usermanager/_test/csv_import.test.php @@ -47,7 +47,7 @@ class plugin_usermanager_csv_import_test extends DokuWikiTest { global $auth; $before_users = $auth->retrieveUsers(); - io_savefile($this->importfile, $importCsv); + io_savefile($this->importfile, $importCsv); $result = $this->usermanager->tryImport(); $after_users = $auth->retrieveUsers(); @@ -66,6 +66,22 @@ class plugin_usermanager_csv_import_test extends DokuWikiTest { $this->assertEquals($expectedFailures, $this->usermanager->getImportFailures()); // failures as expected } + function test_cantImport(){ + global $auth; + $oldauth = $auth; + + $auth = new auth_mock_authplain(); + $auth->setCanDo('addUser', false); + + $csv = 'User,"Real Name",Email,Groups +importuser,"Ford Prefect",ford@example.com,user +'; + + $this->doImportTest($csv, false, array(), array()); + + $auth = $oldauth; + } + function test_import() { $csv = 'User,"Real Name",Email,Groups importuser,"Ford Prefect",ford@example.com,user diff --git a/lib/plugins/usermanager/_test/mocks.class.php b/lib/plugins/usermanager/_test/mocks.class.php index edf5438b2..f3cc72c27 100644 --- a/lib/plugins/usermanager/_test/mocks.class.php +++ b/lib/plugins/usermanager/_test/mocks.class.php @@ -41,3 +41,10 @@ class admin_mock_usermanager extends admin_plugin_usermanager { } } +class auth_mock_authplain extends auth_plugin_authplain { + + public function setCanDo($op, $canDo) { + $this->cando[$op] = $canDo; + } + +} -- cgit v1.2.3 From c9454ee3a82b29418a7ee59f0bb35006ea6a7fb1 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 1 Dec 2013 16:23:02 +0000 Subject: Per FS#2884, implement a local version of str_getcsv() to maintain compatibility with php 5.2.x (str_getcsv() is only available in php 5.3+ and is used by user manager import feature. --- lib/plugins/usermanager/_test/csv_import.test.php | 14 ++++++++++++++ lib/plugins/usermanager/_test/mocks.class.php | 8 ++++++++ 2 files changed, 22 insertions(+) (limited to 'lib/plugins/usermanager/_test') diff --git a/lib/plugins/usermanager/_test/csv_import.test.php b/lib/plugins/usermanager/_test/csv_import.test.php index 1f0ee7436..5133c1256 100644 --- a/lib/plugins/usermanager/_test/csv_import.test.php +++ b/lib/plugins/usermanager/_test/csv_import.test.php @@ -150,6 +150,20 @@ importiso8859,"F'.chr(0xF8).'rd Prefect",ford@example.com,user $this->doImportTest($csv, true, $expected, array()); } + /** + * Verify usermanager::str_getcsv() behaves identically to php 5.3's str_getcsv() + * within the context/parameters required by _import() + * + * @requires PHP 5.3 + * @deprecated remove when dokuwiki requires 5.3+ + * also associated usermanager & mock usermanager access methods + */ + private function test_getcsvcompatibility() { + $line = 'importuser,"Ford Prefect",ford@example.com,user'.NL; + + $this->assertEquals(str_getcsv($line), $this->usermanager->access_str_getcsv($line)); + } + private function stripPasswords($array){ foreach ($array as $user => $data) { unset($array[$user]['pass']); diff --git a/lib/plugins/usermanager/_test/mocks.class.php b/lib/plugins/usermanager/_test/mocks.class.php index f3cc72c27..91c74768c 100644 --- a/lib/plugins/usermanager/_test/mocks.class.php +++ b/lib/plugins/usermanager/_test/mocks.class.php @@ -26,6 +26,14 @@ class admin_mock_usermanager extends admin_plugin_usermanager { return $this->_import(); } + /** + * @deprecated remove when dokuwiki requires php 5.3+ + * also associated unit test & usermanager methods + */ + public function access_str_getcsv($line){ + return $this->str_getcsv($line); + } + // no need to send email notifications (mostly) protected function _notifyUser($user, $password, $status_alert=true) { if ($this->mock_email_notifications) { -- cgit v1.2.3 From 3946d21690c98d095fff4a0ad5be4c47c0230ed7 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 1 Dec 2013 16:44:37 +0000 Subject: comment (hide) incomplete export with filter test --- lib/plugins/usermanager/_test/csv_export.test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/plugins/usermanager/_test') diff --git a/lib/plugins/usermanager/_test/csv_export.test.php b/lib/plugins/usermanager/_test/csv_export.test.php index d0942a861..667fc71dc 100644 --- a/lib/plugins/usermanager/_test/csv_export.test.php +++ b/lib/plugins/usermanager/_test/csv_export.test.php @@ -49,11 +49,11 @@ testuser,"Arthur Dent",arthur@example.com, '; $this->assertEquals($expected, $this->usermanager->tryExport()); } - +/* function test_export_withfilter(){ $this->markTestIncomplete( 'This test has not been implemented yet.' ); } - +*/ } -- cgit v1.2.3 From 1da08fca34cb20a1b12d83e70a1d915f66cf6438 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 1 Dec 2013 16:45:53 +0000 Subject: remove incorrect 'private' restriction from tset method --- lib/plugins/usermanager/_test/csv_import.test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/plugins/usermanager/_test') diff --git a/lib/plugins/usermanager/_test/csv_import.test.php b/lib/plugins/usermanager/_test/csv_import.test.php index 5133c1256..3968356bc 100644 --- a/lib/plugins/usermanager/_test/csv_import.test.php +++ b/lib/plugins/usermanager/_test/csv_import.test.php @@ -158,7 +158,7 @@ importiso8859,"F'.chr(0xF8).'rd Prefect",ford@example.com,user * @deprecated remove when dokuwiki requires 5.3+ * also associated usermanager & mock usermanager access methods */ - private function test_getcsvcompatibility() { + function test_getcsvcompatibility() { $line = 'importuser,"Ford Prefect",ford@example.com,user'.NL; $this->assertEquals(str_getcsv($line), $this->usermanager->access_str_getcsv($line)); -- cgit v1.2.3