xref: /dokuwiki/_test/tests/inc/common_saveWikiText.test.php (revision 4b5aebc14b3cb0ef994231900623500c020612ac)
1023953f0SAndreas Gohr<?php
2023953f0SAndreas Gohr
3023953f0SAndreas Gohrclass common_saveWikiText_test extends DokuWikiTest {
4023953f0SAndreas Gohr
5023953f0SAndreas Gohr    /**
6023953f0SAndreas Gohr     * Execute a whole bunch of saves on the same page and check the results
7023953f0SAndreas Gohr     */
8023953f0SAndreas Gohr    function test_savesequence() {
9023953f0SAndreas Gohr        global $REV;
10023953f0SAndreas Gohr
11023953f0SAndreas Gohr        $page = 'page';
12023953f0SAndreas Gohr        $file = wikiFN($page);
13023953f0SAndreas Gohr
14023953f0SAndreas Gohr        // create the page
15023953f0SAndreas Gohr        $this->assertFileNotExists($file);
16023953f0SAndreas Gohr        saveWikiText($page, 'teststring', 'first save', false);
17023953f0SAndreas Gohr        $this->assertFileExists($file);
18023953f0SAndreas Gohr        $lastmod = filemtime($file);
19023953f0SAndreas Gohr
20023953f0SAndreas Gohr        $pagelog = new PageChangeLog($page);
21023953f0SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
22023953f0SAndreas Gohr        $this->assertEquals(1, count($revisions));
23023953f0SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
24023953f0SAndreas Gohr        $this->assertEquals('first save', $revinfo['sum']);
25023953f0SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_CREATE, $revinfo['type']);
26*4b5aebc1SGerrit Uitslag        $this->assertEquals(10, $revinfo['sizechange']);
27023953f0SAndreas Gohr
28023953f0SAndreas Gohr        sleep(1); // wait for new revision ID
29023953f0SAndreas Gohr
30023953f0SAndreas Gohr        // save with same content should be ignored
31023953f0SAndreas Gohr        saveWikiText($page, 'teststring', 'second save', false);
32023953f0SAndreas Gohr        clearstatcache(false, $file);
33023953f0SAndreas Gohr        $this->assertEquals($lastmod, filemtime($file));
34023953f0SAndreas Gohr
35023953f0SAndreas Gohr        $pagelog = new PageChangeLog($page);
36023953f0SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
37023953f0SAndreas Gohr        $this->assertEquals(1, count($revisions));
38023953f0SAndreas Gohr
39023953f0SAndreas Gohr        // update the page with new text
40*4b5aebc1SGerrit Uitslag        saveWikiText($page, 'teststring2long', 'third save', false);
41023953f0SAndreas Gohr        clearstatcache(false, $file);
42023953f0SAndreas Gohr        $newmod = filemtime($file);
43023953f0SAndreas Gohr        $this->assertNotEquals($lastmod, $newmod);
44023953f0SAndreas Gohr        $lastmod = $newmod;
45023953f0SAndreas Gohr
46023953f0SAndreas Gohr        $pagelog = new PageChangeLog($page);
47023953f0SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
48023953f0SAndreas Gohr        $this->assertEquals(2, count($revisions));
49023953f0SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
50023953f0SAndreas Gohr        $this->assertEquals('third save', $revinfo['sum']);
51023953f0SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_EDIT, $revinfo['type']);
52*4b5aebc1SGerrit Uitslag        $this->assertEquals(5, $revinfo['sizechange']);
53023953f0SAndreas Gohr
54023953f0SAndreas Gohr        sleep(1); // wait for new revision ID
55023953f0SAndreas Gohr
56023953f0SAndreas Gohr        // add a minor edit (unauthenticated)
57*4b5aebc1SGerrit Uitslag        saveWikiText($page, 'teststring3long', 'fourth save', true);
58023953f0SAndreas Gohr        clearstatcache(false, $file);
59023953f0SAndreas Gohr        $newmod = filemtime($file);
60023953f0SAndreas Gohr        $this->assertNotEquals($lastmod, $newmod);
61023953f0SAndreas Gohr        $lastmod = $newmod;
62023953f0SAndreas Gohr
63023953f0SAndreas Gohr        $pagelog = new PageChangeLog($page);
64023953f0SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
65023953f0SAndreas Gohr        $this->assertEquals(3, count($revisions));
66023953f0SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
67023953f0SAndreas Gohr        $this->assertEquals('fourth save', $revinfo['sum']);
68023953f0SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_EDIT, $revinfo['type']);
69*4b5aebc1SGerrit Uitslag        $this->assertEquals(0, $revinfo['sizechange']);
70023953f0SAndreas Gohr
71023953f0SAndreas Gohr        sleep(1); // wait for new revision ID
72023953f0SAndreas Gohr
73023953f0SAndreas Gohr        // add a minor edit (authenticated)
74023953f0SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'user';
75023953f0SAndreas Gohr        saveWikiText($page, 'teststring4', 'fifth save', true);
76023953f0SAndreas Gohr        clearstatcache(false, $file);
77023953f0SAndreas Gohr        $newmod = filemtime($file);
78023953f0SAndreas Gohr        $this->assertNotEquals($lastmod, $newmod);
79023953f0SAndreas Gohr        $lastmod = $newmod;
80023953f0SAndreas Gohr
81023953f0SAndreas Gohr        $pagelog = new PageChangeLog($page);
82023953f0SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
83023953f0SAndreas Gohr        $this->assertEquals(4, count($revisions));
84023953f0SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
85023953f0SAndreas Gohr        $this->assertEquals('fifth save', $revinfo['sum']);
86023953f0SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_MINOR_EDIT, $revinfo['type']);
87*4b5aebc1SGerrit Uitslag        $this->assertEquals(-4, $revinfo['sizechange']);
88023953f0SAndreas Gohr
89023953f0SAndreas Gohr        sleep(1); // wait for new revision ID
90023953f0SAndreas Gohr
91023953f0SAndreas Gohr        // delete
92023953f0SAndreas Gohr        saveWikiText($page, '', 'sixth save', false);
93023953f0SAndreas Gohr        clearstatcache(false, $file);
94023953f0SAndreas Gohr        $this->assertFileNotExists($file);
95023953f0SAndreas Gohr
96023953f0SAndreas Gohr        $pagelog = new PageChangeLog($page);
97023953f0SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
98023953f0SAndreas Gohr        $this->assertEquals(5, count($revisions));
99023953f0SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
100023953f0SAndreas Gohr        $this->assertEquals('sixth save', $revinfo['sum']);
101023953f0SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_DELETE, $revinfo['type']);
102*4b5aebc1SGerrit Uitslag        $this->assertEquals(-11, $revinfo['sizechange']);
103023953f0SAndreas Gohr
104023953f0SAndreas Gohr        sleep(1); // wait for new revision ID
105023953f0SAndreas Gohr
106023953f0SAndreas Gohr        // restore
107023953f0SAndreas Gohr        $REV = $lastmod;
108023953f0SAndreas Gohr        saveWikiText($page, 'teststring4', 'seventh save', true);
109023953f0SAndreas Gohr        clearstatcache(false, $file);
110023953f0SAndreas Gohr        $this->assertFileExists($file);
111023953f0SAndreas Gohr        $newmod = filemtime($file);
112023953f0SAndreas Gohr        $this->assertNotEquals($lastmod, $newmod);
113023953f0SAndreas Gohr        $lastmod = $newmod;
114023953f0SAndreas Gohr
115023953f0SAndreas Gohr        $pagelog = new PageChangeLog($page);
116023953f0SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
117023953f0SAndreas Gohr        $this->assertEquals(6, count($revisions));
118023953f0SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
119023953f0SAndreas Gohr        $this->assertEquals('seventh save', $revinfo['sum']);
120023953f0SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_REVERT, $revinfo['type']);
121023953f0SAndreas Gohr        $this->assertEquals($REV, $revinfo['extra']);
122*4b5aebc1SGerrit Uitslag        $this->assertEquals(11, $revinfo['sizechange']);
123023953f0SAndreas Gohr        $REV = '';
124023953f0SAndreas Gohr
125023953f0SAndreas Gohr        sleep(1); // wait for new revision ID
126023953f0SAndreas Gohr
127023953f0SAndreas Gohr        // create external edit
128023953f0SAndreas Gohr        file_put_contents($file, 'teststring5');
129023953f0SAndreas Gohr
130023953f0SAndreas Gohr        sleep(1); // wait for new revision ID
131023953f0SAndreas Gohr
132023953f0SAndreas Gohr        // save on top of external edit
133023953f0SAndreas Gohr        saveWikiText($page, 'teststring6', 'eigth save', false);
134023953f0SAndreas Gohr        clearstatcache(false, $file);
135023953f0SAndreas Gohr        $newmod = filemtime($file);
136023953f0SAndreas Gohr        $this->assertNotEquals($lastmod, $newmod);
137023953f0SAndreas Gohr        $lastmod = $newmod;
138023953f0SAndreas Gohr
139023953f0SAndreas Gohr        $pagelog = new PageChangeLog($page);
140023953f0SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
141023953f0SAndreas Gohr        $this->assertEquals(8, count($revisions)); // two more revisions now!
142023953f0SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
143023953f0SAndreas Gohr        $this->assertEquals('eigth save', $revinfo['sum']);
144023953f0SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_EDIT, $revinfo['type']);
145*4b5aebc1SGerrit Uitslag        $this->assertEquals(0, $revinfo['sizechange']);
146023953f0SAndreas Gohr
147023953f0SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[1]);
148023953f0SAndreas Gohr        $this->assertEquals('external edit', $revinfo['sum']);
149023953f0SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_EDIT, $revinfo['type']);
150*4b5aebc1SGerrit Uitslag        $this->assertEquals(0, $revinfo['sizechange']);
151023953f0SAndreas Gohr
152023953f0SAndreas Gohr    }
153023953f0SAndreas Gohr}
154