summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristopher Smith <chris@jalakai.co.uk>2013-12-01 11:19:45 -0800
committerChristopher Smith <chris@jalakai.co.uk>2013-12-01 11:19:45 -0800
commitc9ed716319616d0ca0f754dab861f5e12949b102 (patch)
treea8c8c025ba322122d34b3399753dc0a18fd66154 /lib
parent11faf6c73d0b3396116683c2f2e042de15a77175 (diff)
parent1da08fca34cb20a1b12d83e70a1d915f66cf6438 (diff)
downloadrpg-c9ed716319616d0ca0f754dab861f5e12949b102.tar.gz
rpg-c9ed716319616d0ca0f754dab861f5e12949b102.tar.bz2
Merge pull request #455 from splitbrain/user_importexport_fixes
Usermanager import/export fixes
Diffstat (limited to 'lib')
-rw-r--r--lib/plugins/usermanager/_test/csv_export.test.php59
-rw-r--r--lib/plugins/usermanager/_test/csv_import.test.php185
-rw-r--r--lib/plugins/usermanager/_test/mocks.class.php58
-rw-r--r--lib/plugins/usermanager/admin.php44
4 files changed, 342 insertions, 4 deletions
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..667fc71dc
--- /dev/null
+++ b/lib/plugins/usermanager/_test/csv_export.test.php
@@ -0,0 +1,59 @@
+<?php
+
+/**
+ * @group plugin_usermanager
+ * @group admin_plugins
+ * @group plugins
+ * @group bundled_plugins
+ */
+require_once(dirname(__FILE__).'/mocks.class.php');
+
+class plugin_usermanager_csv_export_test extends DokuWikiTest {
+
+ protected $usermanager;
+
+ function setUp() {
+ $this->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..3968356bc
--- /dev/null
+++ b/lib/plugins/usermanager/_test/csv_import.test.php
@@ -0,0 +1,185 @@
+<?php
+
+/**
+ * @group plugin_usermanager
+ * @group admin_plugins
+ * @group plugins
+ * @group bundled_plugins
+ */
+
+require_once(dirname(__FILE__).'/mocks.class.php');
+
+/**
+ * !!!!! NOTE !!!!!
+ *
+ * At present, users imported in individual tests remain in the user list for subsequent tests
+ */
+class plugin_usermanager_csv_import_test extends DokuWikiTest {
+
+ private $old_files;
+ protected $usermanager;
+ protected $importfile;
+
+ function setUp() {
+ $this->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_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
+';
+ $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());
+ }
+
+ /**
+ * 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
+ */
+ 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']);
+ }
+ 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..91c74768c
--- /dev/null
+++ b/lib/plugins/usermanager/_test/mocks.class.php
@@ -0,0 +1,58 @@
+<?php
+
+/**
+ * test wrapper to allow access to private/protected functions/properties
+ *
+ * NB: for plugin introspection methods, getPluginType() & getPluginName() to work
+ * this class name needs to start "admin_" and end "_usermanager". Internally
+ * these methods are used in setting up the class, e.g. for language strings
+ */
+class admin_mock_usermanager extends admin_plugin_usermanager {
+
+ public $mock_email_notifications = true;
+ public $mock_email_notifications_sent = 0;
+
+ public function getImportFailures() {
+ return $this->_import_failures;
+ }
+
+ public function tryExport() {
+ ob_start();
+ $this->_export();
+ return ob_get_clean();
+ }
+
+ public function tryImport() {
+ 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) {
+ $this->mock_email_notifications_sent++;
+ return true;
+ } else {
+ return parent::_notifyUser($user, $password, $status_alert);
+ }
+ }
+
+ protected function _isUploadedFile($file) {
+ return file_exists($file);
+ }
+}
+
+class auth_mock_authplain extends auth_plugin_authplain {
+
+ public function setCanDo($op, $canDo) {
+ $this->cando[$op] = $canDo;
+ }
+
+}
diff --git a/lib/plugins/usermanager/admin.php b/lib/plugins/usermanager/admin.php
index c4d71cb22..156037f09 100644
--- a/lib/plugins/usermanager/admin.php
+++ b/lib/plugins/usermanager/admin.php
@@ -814,6 +814,8 @@ class admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
fputcsv($fd, $line);
}
fclose($fd);
+ if (defined('DOKU_UNITTEST')){ return; }
+
die;
}
@@ -822,7 +824,7 @@ class admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
*
* csv file should have 4 columns, user_id, full name, email, groups (comma separated)
*
- * @return bool whether succesful
+ * @return bool whether successful
*/
protected function _import() {
// check we are allowed to add users
@@ -830,7 +832,7 @@ class admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
if (!$this->_auth->canDo('addUser')) return false;
// check file uploaded ok.
- if (empty($_FILES['import']['size']) || !empty($FILES['import']['error']) && is_uploaded_file($FILES['import']['tmp_name'])) {
+ if (empty($_FILES['import']['size']) || !empty($_FILES['import']['error']) && $this->_isUploadedFile($_FILES['import']['tmp_name'])) {
msg($this->lang['import_error_upload'],-1);
return false;
}
@@ -845,7 +847,7 @@ class admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
if (!utf8_check($csv)) {
$csv = utf8_encode($csv);
}
- $raw = str_getcsv($csv);
+ $raw = $this->_getcsv($csv);
$error = ''; // clean out any errors from the previous line
// data checks...
if (1 == ++$line) {
@@ -867,6 +869,7 @@ class admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
$import_success_count++;
} else {
$import_fail_count++;
+ array_splice($raw, 1, 1); // remove the spliced in password
$this->_import_failures[$line] = array('error' => $error, 'user' => $raw, 'orig' => $csv);
}
}
@@ -940,7 +943,7 @@ class admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
*
* @param array $user data of user
* @param string &$error reference catched error message
- * @return bool whether succesful
+ * @return bool whether successful
*/
protected function _addImportUser($user, & $error){
if (!$this->_auth->triggerUserMod('create', $user)) {
@@ -973,4 +976,37 @@ class admin_plugin_usermanager extends DokuWiki_Admin_Plugin {
die;
}
+ /**
+ * wrapper for is_uploaded_file to facilitate overriding by test suite
+ */
+ protected function _isUploadedFile($file) {
+ return is_uploaded_file($file);
+ }
+
+ /**
+ * wrapper for str_getcsv() to simplify maintaining compatibility with php 5.2
+ *
+ * @deprecated remove when dokuwiki php requirement increases to 5.3+
+ * also associated unit test & mock access method
+ */
+ protected function _getcsv($csv) {
+ return function_exists('str_getcsv') ? str_getcsv($csv) : $this->str_getcsv($csv);
+ }
+
+ /**
+ * replacement str_getcsv() function for php < 5.3
+ * loosely based on www.php.net/str_getcsv#88311
+ *
+ * @deprecated remove when dokuwiki php requirement increases to 5.3+
+ */
+ protected function str_getcsv($str) {
+ $fp = fopen("php://temp/maxmemory:1048576", 'r+'); // 1MiB
+ fputs($fp, $str);
+ rewind($fp);
+
+ $data = fgetcsv($fp);
+
+ fclose($fp);
+ return $data;
+ }
}