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