Completed
Push — adminicons ( 220b8a...d1a314 )
by Andreas
03:46
created

plugin_usermanager_csv_import_test   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2
1
<?php
2
3
/**
4
 * @group plugin_usermanager
5
 * @group admin_plugins
6
 * @group plugins
7
 * @group bundled_plugins
8
 */
9
10
require_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
 */
17
class 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_cantImport(){
70
        global $auth;
71
        $oldauth = $auth;
72
73
        $auth = new auth_mock_authplain();
74
        $auth->setCanDo('addUser', false);
75
76
        $csv = 'User,"Real Name",Email,Groups
77
importuser,"Ford Prefect",[email protected],user
78
';
79
80
        $this->doImportTest($csv, false, array(), array());
81
82
        $auth = $oldauth;
83
    }
84
85
    function test_import() {
86
        $csv = 'User,"Real Name",Email,Groups
87
importuser,"Ford Prefect",[email protected],user
88
';
89
        $expected = array(
90
            'importuser' => array(
91
                'name'  => 'Ford Prefect',
92
                'mail'  => '[email protected]',
93
                'grps'  => array('user'),
94
            ),
95
        );
96
97
        $this->doImportTest($csv, true, $expected, array());
98
    }
99
100
    function test_importExisting() {
101
        $csv = 'User,"Real Name",Email,Groups
102
importuser,"Ford Prefect",[email protected],user
103
';
104
        $failures = array(
105
            '2' => array(
106
                'error' => $this->usermanager->lang['import_error_create'],
107
                'user'  => array(
108
                    'importuser',
109
                    'Ford Prefect',
110
                    '[email protected]',
111
                    'user',
112
                ),
113
                'orig'   => 'importuser,"Ford Prefect",[email protected],user'.NL,
114
            ),
115
        );
116
117
        $this->doImportTest($csv, true, array(), $failures);
118
    }
119
120
    function test_importUtf8() {
121
        $csv = 'User,"Real Name",Email,Groups
122
importutf8,"Førd Prefect",[email protected],user
123
';
124
        $expected = array(
125
            'importutf8' => array(
126
                'name'  => 'Førd Prefect',
127
                'mail'  => '[email protected]',
128
                'grps'  => array('user'),
129
            ),
130
        );
131
132
        $this->doImportTest($csv, true, $expected, array());
133
    }
134
135
    /**
136
     *  utf8: u+00F8 (ø) <=> 0xF8 :iso-8859-1
137
     */
138
    function test_importIso8859() {
139
        $csv = 'User,"Real Name",Email,Groups
140
importiso8859,"F'.chr(0xF8).'rd Prefect",[email protected],user
141
';
142
        $expected = array(
143
            'importiso8859' => array(
144
                'name'  => 'Førd Prefect',
145
                'mail'  => '[email protected]',
146
                'grps'  => array('user'),
147
            ),
148
        );
149
150
        $this->doImportTest($csv, true, $expected, array());
151
    }
152
153
    private function stripPasswords($array){
154
        foreach ($array as $user => $data) {
155
            unset($array[$user]['pass']);
156
        }
157
        return $array;
158
    }
159
160
    private function countPasswords($array){
161
        $count = 0;
162
        foreach ($array as $user => $data) {
163
            if (!empty($data['pass'])) {
164
                $count++;
165
            }
166
        }
167
        return $count;
168
    }
169
170
}
171
172