1<?php
2
3namespace dokuwiki\plugin\farmsync\test;
4use dokuwiki\plugin\farmsync\meta\MediaUpdates;
5use dokuwiki\plugin\farmsync\meta\PageUpdates;
6use dokuwiki\plugin\farmsync\meta\TemplateUpdates;
7
8/**
9 * @group plugin_farmsync
10 * @group plugins
11 *
12 */
13class getPagesFromLine_farmsync_test extends \DokuWikiTest {
14    protected $pluginsEnabled = array('farmsync');
15
16    public static function setUpBeforeClass() {
17        parent::setUpBeforeClass();
18        $sourcedir = substr(DOKU_TMP_DATA, 0, -1) . '_sourceGetPagesFromLine/';
19        mkdir($sourcedir);
20        mkdir($sourcedir . 'media');
21        mkdir($sourcedir . 'media/wiki');
22        mkdir($sourcedir . 'pages');
23
24        io_saveFile($sourcedir . 'pages/test/page.txt', "ABC");
25        io_saveFile($sourcedir . 'pages/test/page2.txt', "ABC");
26        io_saveFile($sourcedir . 'pages/base.txt', "ABC");
27
28        io_saveFile($sourcedir . 'pages/start/all.txt', "ABC");
29        io_saveFile($sourcedir . 'pages/start/all/all.txt', "ABC");
30        io_saveFile($sourcedir . 'pages/start/all/start.txt', "ABC");
31
32        io_saveFile($sourcedir . 'pages/start/nostart.txt', "ABC");
33        io_saveFile($sourcedir . 'pages/start/nostart/nostart.txt', "ABC");
34
35        io_saveFile($sourcedir . 'pages/start/outeronly.txt', "ABC");
36
37        io_saveFile($sourcedir . 'pages/page/template.txt', "ABC");
38        io_saveFile($sourcedir . 'pages/namespace/_template.txt', "ABC");
39
40        copy(DOKU_TMP_DATA . 'media/wiki/dokuwiki-128.png', $sourcedir . 'media/wiki/dokuwiki-128.png');
41    }
42
43    public function setUp() {
44        parent::setUp();
45        saveWikiText('wiki', '', 'deleted');
46        saveWikiText('wiki:wiki', '', 'deleted');
47        saveWikiText('wiki:start', '', 'deleted');
48        saveWikiText('wiki:template', '', 'deleted');
49        if (file_exists(wikiFN('wiki:_template', null, false))) unlink(wikiFN('wiki:_template', null, false));
50    }
51
52
53
54    /**
55     * @dataProvider test_getPagesFromLine_dataProvider
56     */
57    public function test_getPagesFromLine_param($pattern, $expectedResult, $expectedMsgCount, $failureMsg) {
58        // arrange
59        $mock_farm_util = new mock\FarmSyncUtil();
60        $sourceanimal = 'sourceanimal';
61        $mock_farm_util->setAnimalDataDir($sourceanimal, substr(DOKU_TMP_DATA, 0, -1) . '_sourceGetPagesFromLine/');
62        $pageUpdater = new PageUpdates($sourceanimal, array(), array());
63        $pageUpdater->farm_util = $mock_farm_util;
64
65        // act
66        $actual_result = $pageUpdater->getDocumentsFromLine($sourceanimal, $pattern);
67
68        // assert
69        global $MSG;
70        $this->assertEquals($expectedResult, $actual_result, $failureMsg);
71        $this->assertEquals(count($MSG), $expectedMsgCount);
72    }
73
74    public function test_getPagesFromLine_dataProvider() {
75        return array(
76            array(
77                'test:page',
78                array('test:page'),
79                0,
80                'singleExistingPage'
81            ),
82            array(
83                'test:*',
84                array('test:page', 'test:page2'),
85                0,
86                'oneLevelNS'
87            ),
88            array(
89                ':*',
90                array(':base'),
91                0,
92                'oneLevelNS_base'
93            ),
94            array(
95                'foo',
96                array(),
97                1,
98                'pageMissing'
99            ),
100            array(
101                'start:all:',
102                array('start:all:start'),
103                0,
104                'startPage'
105            ),
106            array(
107                'start:nostart:',
108                array('start:nostart:nostart'),
109                0,
110                'startPage_inNSlikeNS'
111            ),
112            array(
113                'start:outeronly:',
114                array('start:outeronly'),
115                0,
116                'startPage_likeNS'
117            ),
118            array(
119                'wiki:',
120                array(),
121                1,
122                'startPage_missing'
123            ),
124            array(
125                'wiki:_template',
126                array(),
127                0,
128                'template_as_page'
129            )
130        );
131    }
132
133    /**
134     * @dataProvider test_getTemplatesFromLine_dataProvider
135     */
136    public function test_getTemplatesFromLine_param($pattern, $expectedResult, $expectedMsgCount, $failureMsg) {
137        // arrange
138        $mock_farm_util = new mock\FarmSyncUtil();
139        $sourceanimal = 'sourceanimal';
140        $mock_farm_util->setAnimalDataDir($sourceanimal, substr(DOKU_TMP_DATA, 0, -1) . '_sourceGetPagesFromLine/');
141        $templateUpdater = new TemplateUpdates($sourceanimal, array(), array());
142        $templateUpdater->farm_util = $mock_farm_util;
143
144        // act
145        $actual_result = $templateUpdater->getDocumentsFromLine($sourceanimal, $pattern);
146
147        // assert
148        global $MSG;
149        $this->assertEquals($expectedResult, $actual_result, $failureMsg);
150        $this->assertEquals(count($MSG), $expectedMsgCount);
151    }
152
153    public function test_getTemplatesFromLine_dataProvider() {
154        return array(
155            array(
156                'wiki:_template',
157                array(),
158                1,
159                'template_missing'
160            ),
161            array(
162                'page:_template',
163                array(),
164                1,
165                'template_existing_as_page'
166            ),
167            array(
168                'namespace:_template',
169                array('namespace:_template'),
170                0,
171                'template_existing'
172            ),
173            array(
174                'namespace:*',
175                array('namespace:_template'),
176                0,
177                'template_ns'
178            ),
179            array(
180                ':**',
181                array(':namespace:_template'),
182                0,
183                'template_ns_deep'
184            )
185        );
186    }
187
188    /**
189     * @dataProvider test_getMediaFromLine_dataProvider
190     */
191    public function test_getMediaFromLine_param($pattern, $expectedResult, $expectedMsgCount, $failureMsg) {
192        // arrange
193        $mock_farm_util = new mock\FarmSyncUtil();
194        $sourceanimal = 'sourceanimal';
195        $mock_farm_util->setAnimalDataDir($sourceanimal, substr(DOKU_TMP_DATA, 0, -1) . '_sourceGetPagesFromLine/');
196        $mediaUpdater = new MediaUpdates($sourceanimal, array(), array());
197        $mediaUpdater->farm_util = $mock_farm_util;
198
199        // act
200        $actual_result = $mediaUpdater->getDocumentsFromLine($sourceanimal, $pattern);
201
202        // assert
203        global $MSG;
204        $this->assertEquals($expectedResult, $actual_result, $failureMsg);
205        $this->assertEquals(count($MSG), $expectedMsgCount);
206    }
207
208    public function test_getMediaFromLine_dataProvider() {
209        return array(
210            array(
211                'wiki:*',
212                array('wiki:dokuwiki-128.png'),
213                0,
214                'media_ns'
215            ),
216            array(
217                'wiki:dokuwiki-128.png',
218                array('wiki:dokuwiki-128.png'),
219                0,
220                'media specific file'
221            ),
222            array(
223                ':**',
224                array(':wiki:dokuwiki-128.png'),
225                0,
226                'media deep ns file'
227            ),
228            array(
229                'wiki:missing.png',
230                array(),
231                1,
232                'media missing file'
233            ),
234        );
235    }
236}
237