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(); 54ed6bf75fSChristopher Smith $import_count = count($after_users) - count($before_users); 55ed6bf75fSChristopher Smith $new_users = array_diff_key($after_users, $before_users); 56ed6bf75fSChristopher Smith $diff_users = array_diff_assoc($after_users, $before_users); 57ed6bf75fSChristopher Smith 58ed6bf75fSChristopher Smith $expectedCount = count($expectedNewUsers); 59ed6bf75fSChristopher Smith 60ed6bf75fSChristopher Smith $this->assertEquals($expectedResult, $result); // import result as expected 61ed6bf75fSChristopher Smith $this->assertEquals($expectedCount, $import_count); // number of new users matches expected number imported 62ed6bf75fSChristopher Smith $this->assertEquals($expectedNewUsers, $this->stripPasswords($new_users)); // new user data matches imported user data 63ed6bf75fSChristopher Smith $this->assertEquals($expectedCount, $this->countPasswords($new_users)); // new users have a password 64ed6bf75fSChristopher Smith $this->assertEquals($expectedCount, $this->usermanager->mock_email_notifications_sent); // new users notified of their passwords 65ed6bf75fSChristopher Smith $this->assertEquals($new_users, $diff_users); // no other users were harmed in the testing of this import 66ed6bf75fSChristopher Smith $this->assertEquals($expectedFailures, $this->usermanager->getImportFailures()); // failures as expected 67*211955beSChristopher Smith } 68*211955beSChristopher Smith 69*211955beSChristopher Smith function test_cantImport(){ 70*211955beSChristopher Smith global $auth; 71*211955beSChristopher Smith $oldauth = $auth; 72*211955beSChristopher Smith 73*211955beSChristopher Smith $auth = new auth_mock_authplain(); 74*211955beSChristopher Smith $auth->setCanDo('addUser', false); 75*211955beSChristopher Smith 76*211955beSChristopher Smith $csv = 'User,"Real Name",Email,Groups 77*211955beSChristopher Smithimportuser,"Ford Prefect",ford@example.com,user 78*211955beSChristopher Smith'; 79*211955beSChristopher Smith 80*211955beSChristopher Smith $this->doImportTest($csv, false, array(), array()); 81*211955beSChristopher Smith 82*211955beSChristopher Smith $auth = $oldauth; 83ed6bf75fSChristopher Smith } 84ed6bf75fSChristopher Smith 85ed6bf75fSChristopher Smith function test_import() { 86ed6bf75fSChristopher Smith $csv = 'User,"Real Name",Email,Groups 87ed6bf75fSChristopher Smithimportuser,"Ford Prefect",ford@example.com,user 88ed6bf75fSChristopher Smith'; 89ed6bf75fSChristopher Smith $expected = array( 90ed6bf75fSChristopher Smith 'importuser' => array( 91ed6bf75fSChristopher Smith 'name' => 'Ford Prefect', 92ed6bf75fSChristopher Smith 'mail' => 'ford@example.com', 93ed6bf75fSChristopher Smith 'grps' => array('user'), 94ed6bf75fSChristopher Smith ), 95ed6bf75fSChristopher Smith ); 96ed6bf75fSChristopher Smith 97ed6bf75fSChristopher Smith $this->doImportTest($csv, true, $expected, array()); 98ed6bf75fSChristopher Smith } 99ed6bf75fSChristopher Smith 100ed6bf75fSChristopher Smith function test_importExisting() { 101ed6bf75fSChristopher Smith $csv = 'User,"Real Name",Email,Groups 102ed6bf75fSChristopher Smithimportuser,"Ford Prefect",ford@example.com,user 103ed6bf75fSChristopher Smith'; 104ed6bf75fSChristopher Smith $failures = array( 105ed6bf75fSChristopher Smith '2' => array( 106ed6bf75fSChristopher Smith 'error' => $this->usermanager->lang['import_error_create'], 107ed6bf75fSChristopher Smith 'user' => array( 108ed6bf75fSChristopher Smith 'importuser', 109ed6bf75fSChristopher Smith 'Ford Prefect', 110ed6bf75fSChristopher Smith 'ford@example.com', 111ed6bf75fSChristopher Smith 'user', 112ed6bf75fSChristopher Smith ), 113ed6bf75fSChristopher Smith 'orig' => 'importuser,"Ford Prefect",ford@example.com,user'.NL, 114ed6bf75fSChristopher Smith ), 115ed6bf75fSChristopher Smith ); 116ed6bf75fSChristopher Smith 117ed6bf75fSChristopher Smith $this->doImportTest($csv, true, array(), $failures); 118ed6bf75fSChristopher Smith } 119ed6bf75fSChristopher Smith 120ed6bf75fSChristopher Smith function test_importUtf8() { 121ed6bf75fSChristopher Smith $csv = 'User,"Real Name",Email,Groups 122ed6bf75fSChristopher Smithimportutf8,"Førd Prefect",ford@example.com,user 123ed6bf75fSChristopher Smith'; 124ed6bf75fSChristopher Smith $expected = array( 125ed6bf75fSChristopher Smith 'importutf8' => array( 126ed6bf75fSChristopher Smith 'name' => 'Førd Prefect', 127ed6bf75fSChristopher Smith 'mail' => 'ford@example.com', 128ed6bf75fSChristopher Smith 'grps' => array('user'), 129ed6bf75fSChristopher Smith ), 130ed6bf75fSChristopher Smith ); 131ed6bf75fSChristopher Smith 132ed6bf75fSChristopher Smith $this->doImportTest($csv, true, $expected, array()); 133ed6bf75fSChristopher Smith } 134ed6bf75fSChristopher Smith 135ed6bf75fSChristopher Smith /** 136ed6bf75fSChristopher Smith * utf8: u+00F8 (ø) <=> 0xF8 :iso-8859-1 137ed6bf75fSChristopher Smith */ 138ed6bf75fSChristopher Smith function test_importIso8859() { 139ed6bf75fSChristopher Smith $csv = 'User,"Real Name",Email,Groups 140ed6bf75fSChristopher Smithimportiso8859,"F'.chr(0xF8).'rd Prefect",ford@example.com,user 141ed6bf75fSChristopher Smith'; 142ed6bf75fSChristopher Smith $expected = array( 143ed6bf75fSChristopher Smith 'importiso8859' => array( 144ed6bf75fSChristopher Smith 'name' => 'Førd Prefect', 145ed6bf75fSChristopher Smith 'mail' => 'ford@example.com', 146ed6bf75fSChristopher Smith 'grps' => array('user'), 147ed6bf75fSChristopher Smith ), 148ed6bf75fSChristopher Smith ); 149ed6bf75fSChristopher Smith 150ed6bf75fSChristopher Smith $this->doImportTest($csv, true, $expected, array()); 151ed6bf75fSChristopher Smith } 152ed6bf75fSChristopher Smith 153ed6bf75fSChristopher Smith private function stripPasswords($array){ 154ed6bf75fSChristopher Smith foreach ($array as $user => $data) { 155ed6bf75fSChristopher Smith unset($array[$user]['pass']); 156ed6bf75fSChristopher Smith } 157ed6bf75fSChristopher Smith return $array; 158ed6bf75fSChristopher Smith } 159ed6bf75fSChristopher Smith 160ed6bf75fSChristopher Smith private function countPasswords($array){ 161ed6bf75fSChristopher Smith $count = 0; 162ed6bf75fSChristopher Smith foreach ($array as $user => $data) { 163ed6bf75fSChristopher Smith if (!empty($data['pass'])) { 164ed6bf75fSChristopher Smith $count++; 165ed6bf75fSChristopher Smith } 166ed6bf75fSChristopher Smith } 167ed6bf75fSChristopher Smith return $count; 168ed6bf75fSChristopher Smith } 169ed6bf75fSChristopher Smith 170ed6bf75fSChristopher Smith} 171ed6bf75fSChristopher Smith 172