1<?php 2 3/** 4 * @group plugin_usermanager 5 * @group admin_plugins 6 * @group plugins 7 * @group bundled_plugins 8 */ 9 10require_once(dirname(__FILE__).'/mocks.class.php'); 11 12/** 13 * !!!!! NOTE !!!!! 14 * 15 * At present, users imported in individual tests remain in the user list for subsequent tests 16 */ 17class plugin_usermanager_csv_import_test extends DokuWikiTest { 18 19 private $old_files; 20 protected $usermanager; 21 protected $importfile; 22 23 function setUp() { 24 $this->importfile = tempnam(TMP_DIR, 'csv'); 25 26 $this->old_files = $_FILES; 27 $_FILES = array( 28 'import' => array( 29 'name' => 'import.csv', 30 'tmp_name' => $this->importfile, 31 'type' => 'text/plain', 32 'size' => 1, 33 'error' => 0, 34 ), 35 ); 36 37 $this->usermanager = new admin_mock_usermanager(); 38 parent::setUp(); 39 } 40 41 function tearDown() { 42 $_FILES = $this->old_files; 43 parent::tearDown(); 44 } 45 46 function doImportTest($importCsv, $expectedResult, $expectedNewUsers, $expectedFailures) { 47 global $auth; 48 $before_users = $auth->retrieveUsers(); 49 50 io_savefile($this->importfile, $importCsv); 51 $result = $this->usermanager->tryImport(); 52 53 $after_users = $auth->retrieveUsers(); 54 55 $before_users = array_map('serialize', $before_users); 56 $after_users = array_map('serialize', $after_users); 57 $import_count = count($after_users) - count($before_users); 58 $new_users = array_map('unserialize', array_diff_key($after_users, $before_users)); 59 $diff_users = array_map('unserialize', array_diff_assoc($after_users, $before_users)); 60 61 $expectedCount = count($expectedNewUsers); 62 63 $this->assertEquals($expectedResult, $result); // import result as expected 64 $this->assertEquals($expectedCount, $import_count); // number of new users matches expected number imported 65 $this->assertEquals($expectedNewUsers, $this->stripPasswords($new_users)); // new user data matches imported user data 66 $this->assertEquals($expectedCount, $this->countPasswords($new_users)); // new users have a password 67 $this->assertEquals($expectedCount, $this->usermanager->mock_email_notifications_sent); // new users notified of their passwords 68 $this->assertEquals($new_users, $diff_users); // no other users were harmed in the testing of this import 69 $this->assertEquals($expectedFailures, $this->usermanager->getImportFailures()); // failures as expected 70 } 71 72 function test_cantImport(){ 73 global $auth; 74 $oldauth = $auth; 75 76 $auth = new auth_mock_authplain(); 77 $auth->setCanDo('addUser', false); 78 79 $csv = 'User,"Real Name",Email,Groups 80importuser,"Ford Prefect",ford@example.com,user 81'; 82 83 $this->doImportTest($csv, false, array(), array()); 84 85 $auth = $oldauth; 86 } 87 88 function test_import() { 89 $csv = 'User,"Real Name",Email,Groups 90importuser,"Ford Prefect",ford@example.com,user 91'; 92 $expected = array( 93 'importuser' => array( 94 'name' => 'Ford Prefect', 95 'mail' => 'ford@example.com', 96 'grps' => array('user'), 97 ), 98 ); 99 100 $this->doImportTest($csv, true, $expected, array()); 101 } 102 103 function test_importExisting() { 104 $csv = 'User,"Real Name",Email,Groups 105importuser,"Ford Prefect",ford@example.com,user 106'; 107 $failures = array( 108 '2' => array( 109 'error' => $this->usermanager->getLang('import_error_create'), 110 'user' => array( 111 'importuser', 112 'Ford Prefect', 113 'ford@example.com', 114 'user', 115 ), 116 'orig' => 'importuser,"Ford Prefect",ford@example.com,user'.NL, 117 ), 118 ); 119 120 $this->doImportTest($csv, true, array(), $failures); 121 } 122 123 function test_importUtf8() { 124 $csv = 'User,"Real Name",Email,Groups 125importutf8,"Førd Prefect",ford@example.com,user 126'; 127 $expected = array( 128 'importutf8' => array( 129 'name' => 'Førd Prefect', 130 'mail' => 'ford@example.com', 131 'grps' => array('user'), 132 ), 133 ); 134 135 $this->doImportTest($csv, true, $expected, array()); 136 } 137 138 /** 139 * utf8: u+00F8 (ø) <=> 0xF8 :iso-8859-1 140 */ 141 function test_importIso8859() { 142 $csv = 'User,"Real Name",Email,Groups 143importiso8859,"F'.chr(0xF8).'rd Prefect",ford@example.com,user 144'; 145 $expected = array( 146 'importiso8859' => array( 147 'name' => 'Førd Prefect', 148 'mail' => 'ford@example.com', 149 'grps' => array('user'), 150 ), 151 ); 152 153 $this->doImportTest($csv, true, $expected, array()); 154 } 155 156 private function stripPasswords($array){ 157 foreach ($array as $user => $data) { 158 unset($array[$user]['pass']); 159 } 160 return $array; 161 } 162 163 private function countPasswords($array){ 164 $count = 0; 165 foreach ($array as $user => $data) { 166 if (!empty($data['pass'])) { 167 $count++; 168 } 169 } 170 return $count; 171 } 172 173} 174 175