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