1<?php
2
3namespace dokuwiki\plugin\farmsync\test;
4use dokuwiki\plugin\farmsync\meta\PageUpdates;
5
6
7/**
8 * @group plugin_farmsync
9 * @group plugins
10 *
11 */
12class pageUpdate_farmsync_test extends \DokuWikiTest {
13
14    protected $pluginsEnabled = array('farmsync',);
15
16    public static function setUpBeforeClass() {
17        parent::setUpBeforeClass();
18        $sourcedir = substr(DOKU_TMP_DATA, 0, -1) . '_sourcePageUpdate/';
19        mkdir($sourcedir);
20        mkdir($sourcedir . 'attic');
21        mkdir($sourcedir . 'pages');
22
23        io_saveFile($sourcedir . 'pages/test/page.txt', "ABC");
24        touch($sourcedir . 'pages/test/page.txt', 1400000000);
25        io_saveFile($sourcedir . 'attic/test/page.1400000000.txt.gz', "ABC");
26    }
27
28    public function test_updateAnimal_nonexistingFile() {
29        // arrange
30        $mock_farm_util = new mock\FarmSyncUtil();
31        $mock_farm_util->setPageExists('testanimal', 'test:page', false);
32        $sourceanimal = 'sourceanimal';
33        $mock_farm_util->setAnimalDataDir($sourceanimal, substr(DOKU_TMP_DATA, 0, -1) . '_sourcePageUpdate/');
34        $pageUpdater = new PageUpdates($sourceanimal, array('testanimal'), array('test:page'));
35        $pageUpdater->farm_util = $mock_farm_util;
36
37        // act
38        $pageUpdater->updateEntity('test:page', $sourceanimal, 'testanimal');
39        $updated_pages = $pageUpdater->getResults();
40
41        // assert
42        $this->assertEquals(count($mock_farm_util->receivedWriteCalls), 0);
43        $this->assertEquals(count($mock_farm_util->receivedPageWriteCalls), 1);
44        $this->assertEquals(array(
45            'animal' => 'testanimal',
46            'page' => 'test:page',
47            'content' => 'ABC',
48            'timestamp' => 1400000000
49        ), $mock_farm_util->receivedPageWriteCalls[0]);
50        $this->assertEquals($updated_pages['testanimal']['passed'][0]->getMergeResult(), 'new file');
51    }
52
53    public function test_updateAnimal_identicalFile() {
54        // arrange
55        $mock_farm_util = new mock\FarmSyncUtil();
56        $sourceanimal = 'sourceanimal';
57        $mock_farm_util->setAnimalDataDir($sourceanimal, substr(DOKU_TMP_DATA, 0, -1) . '_sourcePageUpdate/');
58        $mock_farm_util->setPageExists('testanimal', 'test:page', true);
59        $mock_farm_util->setPagemtime('testanimal', 'test:page', 1400000000);
60        $mock_farm_util->setPageContent('testanimal', 'test:page', 'ABC');
61        $pageUpdater = new PageUpdates($sourceanimal, array('testanimal'), array('test:page'));
62        $pageUpdater->farm_util = $mock_farm_util;
63
64        // act
65        $pageUpdater->updateEntity('test:page', $sourceanimal, 'testanimal');
66        $updated_pages = $pageUpdater->getResults();
67
68        // assert
69        $this->assertEquals(count($mock_farm_util->receivedWriteCalls), 0);
70        $this->assertEquals(count($mock_farm_util->receivedPageWriteCalls), 0);
71        $this->assertEquals(array(), $mock_farm_util->receivedWriteCalls);
72        $this->assertEquals($updated_pages['testanimal']['passed'][0]->getMergeResult(), 'unchanged');
73    }
74
75    /**
76     * @group slow
77     */
78    public function test_updateAnimal_remoteUnmodified() {
79        // arrange
80        $sourcedir = substr(DOKU_TMP_DATA, 0, -1) . '_sourcePageUpdate/';
81        $oldrev = 1400000100;
82        $newrev = 1400000200;
83        io_saveFile($sourcedir . "attic/test/page_remoteunmodified.$oldrev.txt.gz", "ABC");
84        io_saveFile($sourcedir . "pages/test/page_remoteunmodified.txt", "ABCD");
85        touch($sourcedir . 'pages/test/page_remoteunmodified.txt', $newrev);
86        io_saveFile($sourcedir . "attic/test/page_remoteunmodified.$newrev.txt.gz", "ABCD");
87        $mock_farm_util = new mock\FarmSyncUtil();
88        $sourceanimal = 'sourceanimal';
89        $mock_farm_util->setAnimalDataDir($sourceanimal, $sourcedir);
90        $mock_farm_util->setPageExists('testanimal', 'test:page_remoteunmodified', true);
91        $mock_farm_util->setPagemtime('testanimal', 'test:page_remoteunmodified', $oldrev);
92        $mock_farm_util->setPageContent('testanimal', 'test:page_remoteunmodified', 'ABC');
93        $pageUpdater = new PageUpdates($sourceanimal, array('testanimal'), array('test:page'));
94        $pageUpdater->farm_util = $mock_farm_util;
95
96        // act
97        $pageUpdater->updateEntity('test:page_remoteunmodified', $sourceanimal, 'testanimal');
98        $updated_pages = $pageUpdater->getResults();
99
100        // assert
101        $this->assertEquals(count($mock_farm_util->receivedWriteCalls), 0);
102        $this->assertEquals(count($mock_farm_util->receivedPageWriteCalls), 1);
103        $this->assertEquals(array(
104            'animal' => 'testanimal',
105            'page' => 'test:page_remoteunmodified',
106            'content' => 'ABCD',
107            'timestamp' => $newrev
108        ), $mock_farm_util->receivedPageWriteCalls[0]);
109        $this->assertEquals($updated_pages['testanimal']['passed'][0]->getMergeResult(), 'file overwritten');
110    }
111
112
113    public function test_updateAnimal_nolocalChanges() {
114        // arrange
115        $mock_farm_util = new mock\FarmSyncUtil();
116        $sourceanimal = 'sourceanimal';
117        $mock_farm_util->setAnimalDataDir($sourceanimal, substr(DOKU_TMP_DATA, 0, -1) . '_sourcePageUpdate/');
118        $mock_farm_util->setPageExists('testanimal', 'test:page', true);
119        $mock_farm_util->setPagemtime('testanimal', 'test:page', 1400000001);
120        $mock_farm_util->setPageContent('testanimal', 'test:page', 'ABCD');
121        $mock_farm_util->setCommonAncestor('sourceanimal', 'testanimal', 'test:page', 'ABC');
122        $pageUpdater = new PageUpdates($sourceanimal, array('testanimal'), array('test:page'));
123        $pageUpdater->farm_util = $mock_farm_util;
124
125        // act
126        $pageUpdater->updateEntity('test:page', $sourceanimal, 'testanimal');
127        $updated_pages = $pageUpdater->getResults();
128
129        // assert
130        $this->assertEquals(count($mock_farm_util->receivedWriteCalls), 0);
131        $this->assertEquals(count($mock_farm_util->receivedPageWriteCalls), 0);
132        $this->assertEquals(array(), $mock_farm_util->receivedWriteCalls);
133        $this->assertEquals($updated_pages['testanimal']['passed'][0]->getMergeResult(), 'unchanged');
134    }
135
136    public function test_updateAnimal_successfulMerge() {
137        // arrange
138        $testpage = 'test:page_successfulmerge';
139        $sourcedir = substr(DOKU_TMP_DATA, 0, -1) . '_sourcePageUpdate/';
140        $sourceanimal = 'sourceanimal';
141        io_saveFile($sourcedir . "pages/test/page_successfulmerge.txt", "ABCX\n\nDEF\n");
142        touch($sourcedir . 'pages/test/page_successfulmerge.txt', 1400000000);
143        $mock_farm_util = new mock\FarmSyncUtil();
144        $mock_farm_util->setAnimalDataDir($sourceanimal, $sourcedir);
145        $mock_farm_util->setPageExists('testanimal', $testpage, true);
146        $mock_farm_util->setPagemtime('testanimal', $testpage, 1400000001);
147        $mock_farm_util->setPageContent('testanimal', $testpage, "ABC\n\nDEFY\n");
148        $mock_farm_util->setCommonAncestor($sourceanimal, 'testanimal', $testpage, "ABC\n\nDEF\n");
149        $pageUpdater = new PageUpdates($sourceanimal, array('testanimal'), array('test:page'));
150        $pageUpdater->farm_util = $mock_farm_util;
151
152        // act
153        $pageUpdater->updateEntity($testpage, $sourceanimal, 'testanimal');
154        $updated_pages = $pageUpdater->getResults();
155
156        // assert
157        $this->assertEquals(count($mock_farm_util->receivedWriteCalls), 0);
158        $this->assertEquals(count($mock_farm_util->receivedPageWriteCalls), 1);
159        $this->assertEquals(array(
160            'animal' => 'testanimal',
161            'page' => 'test:page_successfulmerge',
162            'content' => "ABCX\n\nDEFY\n",
163            'timestamp' => null
164        ), $mock_farm_util->receivedPageWriteCalls[0]);
165        $this->assertEquals($updated_pages['testanimal']['passed'][0]->getMergeResult(), 'merged without conflicts');
166    }
167
168    public function test_updateAnimal_mergeConflicts() {
169        // arrange
170        $testpage = 'test:page_mergeconflict';
171        $sourcedir = substr(DOKU_TMP_DATA, 0, -1) . '_sourcePageUpdate/';
172        $sourceanimal = 'sourceanimal';
173        io_saveFile($sourcedir . "pages/test/page_mergeconflict.txt", "ABCX\n\nDEF\n");
174        touch($sourcedir . 'pages/test/page_mergeconflict.txt', 1400000000);
175        $mock_farm_util = new mock\FarmSyncUtil();
176        $mock_farm_util->setAnimalDataDir($sourceanimal, $sourcedir);
177        $mock_farm_util->setPageExists('testanimal', $testpage, true);
178        $mock_farm_util->setPagemtime('testanimal', $testpage, 1400000001);
179        $mock_farm_util->setPageContent('testanimal', $testpage, "ABCY\n\nDEF\n");
180        $mock_farm_util->setCommonAncestor($sourceanimal, 'testanimal', $testpage, "ABC\n\nDEF\n");
181        $pageUpdater = new PageUpdates($sourceanimal, array('testanimal'), array('test:page'));
182        $pageUpdater->farm_util = $mock_farm_util;
183
184        // act
185        $pageUpdater->updateEntity($testpage, $sourceanimal, 'testanimal');
186        $updated_pages = $pageUpdater->getResults();
187
188        // assert
189        $this->assertEquals(count($mock_farm_util->receivedWriteCalls), 0);
190        $this->assertEquals(count($mock_farm_util->receivedPageWriteCalls), 0);
191        $this->assertEquals($updated_pages['testanimal']['failed'][0]->getMergeResult(), 'merged with conflicts');
192        $this->assertEquals($updated_pages['testanimal']['failed'][0]->getFinalText(), "✎————————————————— The conflicting text in the animal. ————\nABCY\n✏————————————————— The conflicting text in the source. ————\nABCX\n✐————————————————————————————————————\n\nDEF\n");
193    }
194}
195