xref: /dokuwiki/lib/plugins/usermanager/_test/csv_import.test.php (revision 033eb35b208082e0d25132d664962f35869dd3d8)
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 */
17*033eb35bSkalenpwclass plugin_usermanager_csv_import_test extends DokuWikiTest
18*033eb35bSkalenpw{
19ed6bf75fSChristopher Smith    private $old_files;
20ed6bf75fSChristopher Smith    protected $usermanager;
21ed6bf75fSChristopher Smith    protected $importfile;
22ed6bf75fSChristopher Smith
23*033eb35bSkalenpw    public function setUp()
24*033eb35bSkalenpw    {
25ed6bf75fSChristopher Smith        $this->importfile = tempnam(TMP_DIR, 'csv');
26ed6bf75fSChristopher Smith
27ed6bf75fSChristopher Smith        $this->old_files = $_FILES;
28ed6bf75fSChristopher Smith        $_FILES = array(
29ed6bf75fSChristopher Smith            'import'    =>  array(
30ed6bf75fSChristopher Smith                'name'      =>  'import.csv',
31ed6bf75fSChristopher Smith                'tmp_name'  =>  $this->importfile,
32ed6bf75fSChristopher Smith                'type'      =>  'text/plain',
33ed6bf75fSChristopher Smith                'size'      =>  1,
34ed6bf75fSChristopher Smith                'error'     =>  0,
35ed6bf75fSChristopher Smith            ),
36ed6bf75fSChristopher Smith        );
37ed6bf75fSChristopher Smith
38ed6bf75fSChristopher Smith        $this->usermanager = new admin_mock_usermanager();
39ed6bf75fSChristopher Smith        parent::setUp();
40ed6bf75fSChristopher Smith    }
41ed6bf75fSChristopher Smith
42*033eb35bSkalenpw    public function tearDown()
43*033eb35bSkalenpw    {
44ed6bf75fSChristopher Smith        $_FILES = $this->old_files;
45ed6bf75fSChristopher Smith        parent::tearDown();
46ed6bf75fSChristopher Smith    }
47ed6bf75fSChristopher Smith
48*033eb35bSkalenpw    public function doImportTest($importCsv, $expectedResult, $expectedNewUsers, $expectedFailures)
49*033eb35bSkalenpw    {
50ed6bf75fSChristopher Smith        global $auth;
51ed6bf75fSChristopher Smith        $before_users = $auth->retrieveUsers();
52ed6bf75fSChristopher Smith
53*033eb35bSkalenpw        io_saveFile($this->importfile, $importCsv);
54ed6bf75fSChristopher Smith        $result = $this->usermanager->tryImport();
55ed6bf75fSChristopher Smith
56ed6bf75fSChristopher Smith        $after_users = $auth->retrieveUsers();
574a52f4c5SPhy
584a52f4c5SPhy        $before_users = array_map('serialize', $before_users);
594a52f4c5SPhy        $after_users = array_map('serialize', $after_users);
60ed6bf75fSChristopher Smith        $import_count = count($after_users) - count($before_users);
614a52f4c5SPhy        $new_users = array_map('unserialize', array_diff_key($after_users, $before_users));
624a52f4c5SPhy        $diff_users = array_map('unserialize', array_diff_assoc($after_users, $before_users));
63ed6bf75fSChristopher Smith
64ed6bf75fSChristopher Smith        $expectedCount = count($expectedNewUsers);
65ed6bf75fSChristopher Smith
66ed6bf75fSChristopher Smith        $this->assertEquals($expectedResult, $result);                                       // import result as expected
67ed6bf75fSChristopher Smith        $this->assertEquals($expectedCount, $import_count);                                  // number of new users matches expected number imported
68ed6bf75fSChristopher Smith        $this->assertEquals($expectedNewUsers, $this->stripPasswords($new_users));           // new user data matches imported user data
69ed6bf75fSChristopher Smith        $this->assertEquals($expectedCount, $this->countPasswords($new_users));              // new users have a password
70ed6bf75fSChristopher Smith        $this->assertEquals($expectedCount, $this->usermanager->mock_email_notifications_sent);   // new users notified of their passwords
71ed6bf75fSChristopher Smith        $this->assertEquals($new_users, $diff_users);                                        // no other users were harmed in the testing of this import
72ed6bf75fSChristopher Smith        $this->assertEquals($expectedFailures, $this->usermanager->getImportFailures());     // failures as expected
73211955beSChristopher Smith    }
74211955beSChristopher Smith
75*033eb35bSkalenpw    public function test_cantImport()
76*033eb35bSkalenpw    {
77211955beSChristopher Smith        global $auth;
78211955beSChristopher Smith        $oldauth = $auth;
79211955beSChristopher Smith
80211955beSChristopher Smith        $auth = new auth_mock_authplain();
81211955beSChristopher Smith        $auth->setCanDo('addUser', false);
82211955beSChristopher Smith
83211955beSChristopher Smith        $csv = 'User,"Real Name",Email,Groups
84211955beSChristopher Smithimportuser,"Ford Prefect",ford@example.com,user
85211955beSChristopher Smith';
86211955beSChristopher Smith
87211955beSChristopher Smith        $this->doImportTest($csv, false, array(), array());
88211955beSChristopher Smith
89211955beSChristopher Smith        $auth = $oldauth;
90ed6bf75fSChristopher Smith    }
91ed6bf75fSChristopher Smith
92*033eb35bSkalenpw    public function test_import()
93*033eb35bSkalenpw    {
94ed6bf75fSChristopher Smith        $csv = 'User,"Real Name",Email,Groups
95ed6bf75fSChristopher Smithimportuser,"Ford Prefect",ford@example.com,user
96ed6bf75fSChristopher Smith';
97ed6bf75fSChristopher Smith        $expected = array(
98ed6bf75fSChristopher Smith            'importuser' => array(
99ed6bf75fSChristopher Smith                'name'  => 'Ford Prefect',
100ed6bf75fSChristopher Smith                'mail'  => 'ford@example.com',
101ed6bf75fSChristopher Smith                'grps'  => array('user'),
102ed6bf75fSChristopher Smith            ),
103ed6bf75fSChristopher Smith        );
104ed6bf75fSChristopher Smith
105ed6bf75fSChristopher Smith        $this->doImportTest($csv, true, $expected, array());
106ed6bf75fSChristopher Smith    }
107ed6bf75fSChristopher Smith
108*033eb35bSkalenpw    public function test_importExisting()
109*033eb35bSkalenpw    {
110ed6bf75fSChristopher Smith        $csv = 'User,"Real Name",Email,Groups
111ed6bf75fSChristopher Smithimportuser,"Ford Prefect",ford@example.com,user
112ed6bf75fSChristopher Smith';
113ed6bf75fSChristopher Smith        $failures = array(
114ed6bf75fSChristopher Smith            '2' => array(
1159c9753d6SAndreas Gohr                'error' => $this->usermanager->getLang('import_error_create'),
116ed6bf75fSChristopher Smith                'user'  => array(
117ed6bf75fSChristopher Smith                    'importuser',
118ed6bf75fSChristopher Smith                    'Ford Prefect',
119ed6bf75fSChristopher Smith                    'ford@example.com',
120ed6bf75fSChristopher Smith                    'user',
121ed6bf75fSChristopher Smith                ),
122ed6bf75fSChristopher Smith                'orig'   => 'importuser,"Ford Prefect",ford@example.com,user'.NL,
123ed6bf75fSChristopher Smith            ),
124ed6bf75fSChristopher Smith        );
125ed6bf75fSChristopher Smith
126ed6bf75fSChristopher Smith        $this->doImportTest($csv, true, array(), $failures);
127ed6bf75fSChristopher Smith    }
128ed6bf75fSChristopher Smith
129*033eb35bSkalenpw    public function test_importUtf8()
130*033eb35bSkalenpw    {
131ed6bf75fSChristopher Smith        $csv = 'User,"Real Name",Email,Groups
132ed6bf75fSChristopher Smithimportutf8,"Førd Prefect",ford@example.com,user
133ed6bf75fSChristopher Smith';
134ed6bf75fSChristopher Smith        $expected = array(
135ed6bf75fSChristopher Smith            'importutf8' => array(
136ed6bf75fSChristopher Smith                'name'  => 'Førd Prefect',
137ed6bf75fSChristopher Smith                'mail'  => 'ford@example.com',
138ed6bf75fSChristopher Smith                'grps'  => array('user'),
139ed6bf75fSChristopher Smith            ),
140ed6bf75fSChristopher Smith        );
141ed6bf75fSChristopher Smith
142ed6bf75fSChristopher Smith        $this->doImportTest($csv, true, $expected, array());
143ed6bf75fSChristopher Smith    }
144ed6bf75fSChristopher Smith
145ed6bf75fSChristopher Smith    /**
146ed6bf75fSChristopher Smith     *  utf8: u+00F8 (ø) <=> 0xF8 :iso-8859-1
147ed6bf75fSChristopher Smith     */
148*033eb35bSkalenpw    public function test_importIso8859()
149*033eb35bSkalenpw    {
150ed6bf75fSChristopher Smith        $csv = 'User,"Real Name",Email,Groups
151ed6bf75fSChristopher Smithimportiso8859,"F'.chr(0xF8).'rd Prefect",ford@example.com,user
152ed6bf75fSChristopher Smith';
153ed6bf75fSChristopher Smith        $expected = array(
154ed6bf75fSChristopher Smith            'importiso8859' => array(
155ed6bf75fSChristopher Smith                'name'  => 'Førd Prefect',
156ed6bf75fSChristopher Smith                'mail'  => 'ford@example.com',
157ed6bf75fSChristopher Smith                'grps'  => array('user'),
158ed6bf75fSChristopher Smith            ),
159ed6bf75fSChristopher Smith        );
160ed6bf75fSChristopher Smith
161ed6bf75fSChristopher Smith        $this->doImportTest($csv, true, $expected, array());
162ed6bf75fSChristopher Smith    }
163ed6bf75fSChristopher Smith
164*033eb35bSkalenpw    private function stripPasswords($array)
165*033eb35bSkalenpw    {
166ed6bf75fSChristopher Smith        foreach ($array as $user => $data) {
167ed6bf75fSChristopher Smith            unset($array[$user]['pass']);
168ed6bf75fSChristopher Smith        }
169ed6bf75fSChristopher Smith        return $array;
170ed6bf75fSChristopher Smith    }
171ed6bf75fSChristopher Smith
172*033eb35bSkalenpw    private function countPasswords($array)
173*033eb35bSkalenpw    {
174ed6bf75fSChristopher Smith        $count = 0;
175ed6bf75fSChristopher Smith        foreach ($array as $user => $data) {
176ed6bf75fSChristopher Smith            if (!empty($data['pass'])) {
177ed6bf75fSChristopher Smith                $count++;
178ed6bf75fSChristopher Smith            }
179ed6bf75fSChristopher Smith        }
180ed6bf75fSChristopher Smith        return $count;
181ed6bf75fSChristopher Smith    }
182ed6bf75fSChristopher Smith}
183