1<?php
2
3/**
4 * Class parserutils_set_metadata_test
5 */
6class parserutils_set_metadata_test extends DokuWikiTest {
7    // the id used for this test case
8    private $id;
9
10    /**
11     * Set up fake user environment with for the gieven user
12     *
13     * @param string $user
14     */
15    function helper_prepare_user($user = '1') {
16        global $INFO, $USERINFO;
17
18        // prepare fake users
19        static $users = [
20            '1' => [
21                'pass' => '179ad45c6ce2cb97cf1029e212046e81',
22                'name' => 'Tester1',
23                'mail' => 'tester1@example.com',
24                'grps' => array('admin', 'user'),
25            ]
26            ,
27            'tester2' => [
28                'pass' => '179ad45c6ce2cb97cf1029e212046e81',
29                'name' => 'Tester2',
30                'mail' => 'tester2@example.com',
31                'grps' => array('user'),
32            ]
33        ];
34        if(!isset($users[$user])) throw new RuntimeException('requested non-existing user');
35
36        // set up globals
37        $_SERVER['REMOTE_ADDR'] = '1.2.3.4';
38        $USERINFO = $users[$user];
39        $INFO['userinfo'] = $USERINFO;
40        $_SERVER['REMOTE_USER'] = $user;
41    }
42
43    /**
44     *  test array merge, including contributors with numeric keys and array data overwritting
45     */
46    function test_array_replace(){
47        // prepare user
48        $this->helper_prepare_user('1');
49
50        // prepare page
51        $this->id = 'test:set_metadata_array_replace';
52        saveWikiText($this->id, 'Test', 'Test data setup');
53        $meta = p_get_metadata($this->id);
54
55        $this->assertEquals('1', $meta['user'], 'Initial page has wrong user ID');
56        // $this->assertEquals(empty($meta['contributor']), true, 'Initial page should have no contributors');
57
58        // first revision with numeric user
59        $this->waitForTick();
60        saveWikiText($this->id, 'Test1', 'Test first edit');
61        $meta = p_get_metadata($this->id);
62
63        $last_edit_date = $meta['date']['modified'];
64        $this->assertEquals(array('1'=>'Tester1'), $meta['contributor'], 'First edit contributors error');
65
66        // second revision with alphabetic user
67        $this->waitForTick();
68        $this->helper_prepare_user('tester2');
69        saveWikiText($this->id, 'Test2', 'Test second edit');
70        $meta = p_get_metadata($this->id);
71
72        $this->assertNotEquals($last_edit_date, $meta['date']['modified'], 'First edit date merge error');
73        $this->assertEquals(array('tester2'=>'Tester2', '1'=>'Tester1'), $meta['contributor'], 'Second edit contributors error');
74
75        // third revision with the first user
76        $this->waitForTick();
77        $this->helper_prepare_user('1');
78        saveWikiText($this->id, 'Test3', 'Test third edit');
79        $meta = p_get_metadata($this->id);
80
81        $this->assertEquals(array('tester2'=>'Tester2', '1'=>'Tester1'), $meta['contributor'], 'Third edit contributors error');
82    }
83}
84