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