xref: /plugin/struct/_test/action/EditTest.php (revision 8fed17f342cc190557a6ce94d1787f9e2f63cb6c)
1*8fed17f3SAndreas Gohr<?php
2*8fed17f3SAndreas Gohr
3*8fed17f3SAndreas Gohrnamespace dokuwiki\plugin\struct\test\action;
4*8fed17f3SAndreas Gohr
5*8fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta;
6*8fed17f3SAndreas Gohruse dokuwiki\plugin\struct\test\mock\action_plugin_struct_edit;
7*8fed17f3SAndreas Gohruse dokuwiki\plugin\struct\test\mock\Assignments;
8*8fed17f3SAndreas Gohruse dokuwiki\plugin\struct\test\StructTest;
9*8fed17f3SAndreas Gohr
10*8fed17f3SAndreas Gohr/**
11*8fed17f3SAndreas Gohr * @group plugin_struct
12*8fed17f3SAndreas Gohr * @group plugins
13*8fed17f3SAndreas Gohr *
14*8fed17f3SAndreas Gohr * @covers action_plugin_struct_revert
15*8fed17f3SAndreas Gohr * @covers action_plugin_struct_edit
16*8fed17f3SAndreas Gohr */
17*8fed17f3SAndreas Gohrclass EditTest extends StructTest
18*8fed17f3SAndreas Gohr{
19*8fed17f3SAndreas Gohr
20*8fed17f3SAndreas Gohr    public function setUp(): void
21*8fed17f3SAndreas Gohr    {
22*8fed17f3SAndreas Gohr        parent::setUp();
23*8fed17f3SAndreas Gohr
24*8fed17f3SAndreas Gohr        $this->loadSchemaJSON('schema1');
25*8fed17f3SAndreas Gohr        $this->loadSchemaJSON('schema2', 'schema2int');
26*8fed17f3SAndreas Gohr        $this->saveData(
27*8fed17f3SAndreas Gohr            'page01',
28*8fed17f3SAndreas Gohr            'schema1',
29*8fed17f3SAndreas Gohr            [
30*8fed17f3SAndreas Gohr                'first' => 'first data',
31*8fed17f3SAndreas Gohr                'second' => ['second data', 'more data', 'even more'],
32*8fed17f3SAndreas Gohr                'third' => 'third data',
33*8fed17f3SAndreas Gohr                'fourth' => 'fourth data'
34*8fed17f3SAndreas Gohr            ],
35*8fed17f3SAndreas Gohr            time()
36*8fed17f3SAndreas Gohr        );
37*8fed17f3SAndreas Gohr    }
38*8fed17f3SAndreas Gohr
39*8fed17f3SAndreas Gohr    protected function checkField(\phpQueryObject $pq, $schema, $name, $value)
40*8fed17f3SAndreas Gohr    {
41*8fed17f3SAndreas Gohr        $this->assertEquals(1, $pq->find("span.label:contains('$name')")->length, "Field $schema.$name not found");
42*8fed17f3SAndreas Gohr        $this->assertEquals($value, $pq->find("input[name='struct_schema_data[$schema][$name]']")->val(), "Field $schema.$name has wrong value");
43*8fed17f3SAndreas Gohr    }
44*8fed17f3SAndreas Gohr
45*8fed17f3SAndreas Gohr    public function test_createForm_storedData()
46*8fed17f3SAndreas Gohr    {
47*8fed17f3SAndreas Gohr        $edit = new action_plugin_struct_edit();
48*8fed17f3SAndreas Gohr        global $ID;
49*8fed17f3SAndreas Gohr        $ID = 'page01';
50*8fed17f3SAndreas Gohr        $test_html = $edit->createForm('schema1');
51*8fed17f3SAndreas Gohr
52*8fed17f3SAndreas Gohr        $pq = \phpQuery::newDocument($test_html);
53*8fed17f3SAndreas Gohr        $this->assertEquals('schema1', $pq->find('legend')->text());
54*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'first', 'first data');
55*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'second', 'second data, more data, even more');
56*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'third', 'third data');
57*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'fourth', 'fourth data');
58*8fed17f3SAndreas Gohr    }
59*8fed17f3SAndreas Gohr
60*8fed17f3SAndreas Gohr    public function test_createForm_emptyData()
61*8fed17f3SAndreas Gohr    {
62*8fed17f3SAndreas Gohr        $edit = new action_plugin_struct_edit();
63*8fed17f3SAndreas Gohr        global $ID;
64*8fed17f3SAndreas Gohr        $ID = 'page02';
65*8fed17f3SAndreas Gohr        $test_html = $edit->createForm('schema1');
66*8fed17f3SAndreas Gohr
67*8fed17f3SAndreas Gohr        $pq = \phpQuery::newDocument($test_html);
68*8fed17f3SAndreas Gohr        $this->assertEquals('schema1', $pq->find('legend')->text());
69*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'first', '');
70*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'second', '');
71*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'third', '');
72*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'fourth', '');
73*8fed17f3SAndreas Gohr    }
74*8fed17f3SAndreas Gohr
75*8fed17f3SAndreas Gohr    public function test_createForm_postData()
76*8fed17f3SAndreas Gohr    {
77*8fed17f3SAndreas Gohr        global $INPUT, $ID;
78*8fed17f3SAndreas Gohr        $ID = 'page01';
79*8fed17f3SAndreas Gohr        $structdata = [
80*8fed17f3SAndreas Gohr            'schema1' => [
81*8fed17f3SAndreas Gohr                'first' => 'first post data',
82*8fed17f3SAndreas Gohr                'second' => ['second post data', 'more post data', 'even more post data'],
83*8fed17f3SAndreas Gohr                'third' => 'third post data',
84*8fed17f3SAndreas Gohr                'fourth' => 'fourth post data'
85*8fed17f3SAndreas Gohr            ]
86*8fed17f3SAndreas Gohr        ];
87*8fed17f3SAndreas Gohr        $INPUT->set(action_plugin_struct_edit::getVAR(), $structdata);
88*8fed17f3SAndreas Gohr
89*8fed17f3SAndreas Gohr        $edit = new action_plugin_struct_edit();
90*8fed17f3SAndreas Gohr        $test_html = $edit->createForm('schema1');
91*8fed17f3SAndreas Gohr
92*8fed17f3SAndreas Gohr        $pq = \phpQuery::newDocument($test_html);
93*8fed17f3SAndreas Gohr        $this->assertEquals('schema1', $pq->find('legend')->text());
94*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'first', 'first post data');
95*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'second', 'second post data, more post data, even more post data');
96*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'third', 'third post data');
97*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema1', 'fourth', 'fourth post data');
98*8fed17f3SAndreas Gohr    }
99*8fed17f3SAndreas Gohr
100*8fed17f3SAndreas Gohr    public function test_edit_page_wo_schema()
101*8fed17f3SAndreas Gohr    {
102*8fed17f3SAndreas Gohr        $page = 'test_edit_page_wo_schema';
103*8fed17f3SAndreas Gohr
104*8fed17f3SAndreas Gohr        $request = new \TestRequest();
105*8fed17f3SAndreas Gohr        $response = $request->get(['id' => $page, 'do' => 'edit'], '/doku.php');
106*8fed17f3SAndreas Gohr        $structElement = $response->queryHTML('.struct_entry_form');
107*8fed17f3SAndreas Gohr
108*8fed17f3SAndreas Gohr        $this->assertCount(1, $structElement);
109*8fed17f3SAndreas Gohr        $this->assertEquals($structElement->html(), '');
110*8fed17f3SAndreas Gohr    }
111*8fed17f3SAndreas Gohr
112*8fed17f3SAndreas Gohr    public function test_edit_page_with_schema()
113*8fed17f3SAndreas Gohr    {
114*8fed17f3SAndreas Gohr        $page = 'test_edit_page_with_schema';
115*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
116*8fed17f3SAndreas Gohr        $schema = 'schema2';
117*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
118*8fed17f3SAndreas Gohr
119*8fed17f3SAndreas Gohr        $request = new \TestRequest();
120*8fed17f3SAndreas Gohr        $response = $request->get(['id' => $page, 'do' => 'edit'], '/doku.php');
121*8fed17f3SAndreas Gohr        $test_html = trim($response->queryHTML('.struct_entry_form')->html());
122*8fed17f3SAndreas Gohr
123*8fed17f3SAndreas Gohr        $pq = \phpQuery::newDocument($test_html);
124*8fed17f3SAndreas Gohr        $this->assertEquals('schema2', $pq->find('legend')->text());
125*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'afirst', '');
126*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'asecond', '');
127*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'athird', '');
128*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'afourth', '');
129*8fed17f3SAndreas Gohr    }
130*8fed17f3SAndreas Gohr
131*8fed17f3SAndreas Gohr    public function test_preview_page_invaliddata()
132*8fed17f3SAndreas Gohr    {
133*8fed17f3SAndreas Gohr        $page = 'test_preview_page_invaliddata';
134*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
135*8fed17f3SAndreas Gohr        $schema = 'schema2';
136*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
137*8fed17f3SAndreas Gohr
138*8fed17f3SAndreas Gohr        $request = new \TestRequest();
139*8fed17f3SAndreas Gohr        $structData = [
140*8fed17f3SAndreas Gohr            $schema => [
141*8fed17f3SAndreas Gohr                'afirst' => 'foo',
142*8fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
143*8fed17f3SAndreas Gohr                'athird' => 'foobar',
144*8fed17f3SAndreas Gohr                'afourth' => 'Eve'
145*8fed17f3SAndreas Gohr            ]
146*8fed17f3SAndreas Gohr        ];
147*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
148*8fed17f3SAndreas Gohr        $response = $request->post(['id' => $page, 'do' => 'preview'], '/doku.php');
149*8fed17f3SAndreas Gohr        $expected_errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), 'afourth');
150*8fed17f3SAndreas Gohr        $actual_errormsg = $response->queryHTML('.error')->html();
151*8fed17f3SAndreas Gohr        $test_html = trim($response->queryHTML('.struct_entry_form')->html());
152*8fed17f3SAndreas Gohr
153*8fed17f3SAndreas Gohr        $this->assertEquals($expected_errormsg, $actual_errormsg, 'If there is invalid data, then there should be an error message.');
154*8fed17f3SAndreas Gohr
155*8fed17f3SAndreas Gohr        $pq = \phpQuery::newDocument($test_html);
156*8fed17f3SAndreas Gohr        $this->assertEquals('schema2', $pq->find('legend')->text());
157*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'afirst', 'foo');
158*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'asecond', 'bar, baz');
159*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'athird', 'foobar');
160*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'afourth', 'Eve');
161*8fed17f3SAndreas Gohr    }
162*8fed17f3SAndreas Gohr
163*8fed17f3SAndreas Gohr    public function test_preview_page_validdata()
164*8fed17f3SAndreas Gohr    {
165*8fed17f3SAndreas Gohr        $page = 'test_preview_page_validdata';
166*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
167*8fed17f3SAndreas Gohr        $schema = 'schema2';
168*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
169*8fed17f3SAndreas Gohr
170*8fed17f3SAndreas Gohr        $request = new \TestRequest();
171*8fed17f3SAndreas Gohr        $structData = [
172*8fed17f3SAndreas Gohr            $schema => [
173*8fed17f3SAndreas Gohr                'afirst' => 'foo',
174*8fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
175*8fed17f3SAndreas Gohr                'athird' => 'foobar',
176*8fed17f3SAndreas Gohr                'afourth' => '42'
177*8fed17f3SAndreas Gohr            ]
178*8fed17f3SAndreas Gohr        ];
179*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
180*8fed17f3SAndreas Gohr        $response = $request->post(['id' => $page, 'do' => 'preview'], '/doku.php');
181*8fed17f3SAndreas Gohr        $actual_errormsg = $response->queryHTML('.error')->get();
182*8fed17f3SAndreas Gohr        $test_html = trim($response->queryHTML('.struct_entry_form')->html());
183*8fed17f3SAndreas Gohr
184*8fed17f3SAndreas Gohr        $this->assertEquals($actual_errormsg, [], "If all data is valid, then there should be no error message.");
185*8fed17f3SAndreas Gohr
186*8fed17f3SAndreas Gohr        $pq = \phpQuery::newDocument($test_html);
187*8fed17f3SAndreas Gohr        $this->assertEquals('schema2', $pq->find('legend')->text());
188*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'afirst', 'foo');
189*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'asecond', 'bar, baz');
190*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'athird', 'foobar');
191*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'afourth', '42');
192*8fed17f3SAndreas Gohr    }
193*8fed17f3SAndreas Gohr
194*8fed17f3SAndreas Gohr    public function test_fail_saving_empty_page()
195*8fed17f3SAndreas Gohr    {
196*8fed17f3SAndreas Gohr        $page = 'test_fail_saving_empty_page';
197*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
198*8fed17f3SAndreas Gohr        $schema = 'schema2';
199*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
200*8fed17f3SAndreas Gohr
201*8fed17f3SAndreas Gohr        $request = new \TestRequest();
202*8fed17f3SAndreas Gohr        $structData = [
203*8fed17f3SAndreas Gohr            $schema => [
204*8fed17f3SAndreas Gohr                'afirst' => 'foo',
205*8fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
206*8fed17f3SAndreas Gohr                'athird' => 'foobar',
207*8fed17f3SAndreas Gohr                'afourth' => '42'
208*8fed17f3SAndreas Gohr            ]
209*8fed17f3SAndreas Gohr        ];
210*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
211*8fed17f3SAndreas Gohr        $request->setPost('summary', 'only struct data saved');
212*8fed17f3SAndreas Gohr        $response = $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
213*8fed17f3SAndreas Gohr        $expected_errormsg = $this->getLang('emptypage');
214*8fed17f3SAndreas Gohr        $actual_errormsg = $response->queryHTML('.error')->html();
215*8fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
216*8fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
217*8fed17f3SAndreas Gohr
218*8fed17f3SAndreas Gohr        $this->assertCount(0, $revisions);
219*8fed17f3SAndreas Gohr        $this->assertEquals($expected_errormsg, $actual_errormsg, "An empty page should not be saved.");
220*8fed17f3SAndreas Gohr    }
221*8fed17f3SAndreas Gohr
222*8fed17f3SAndreas Gohr    public function test_fail_saveing_page_with_invaliddata()
223*8fed17f3SAndreas Gohr    {
224*8fed17f3SAndreas Gohr        $page = 'test_fail_saveing_page_with_invaliddata';
225*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
226*8fed17f3SAndreas Gohr        $schema = 'schema2';
227*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
228*8fed17f3SAndreas Gohr
229*8fed17f3SAndreas Gohr        $wikitext = 'teststring';
230*8fed17f3SAndreas Gohr        $request = new \TestRequest();
231*8fed17f3SAndreas Gohr        $structData = [
232*8fed17f3SAndreas Gohr            $schema => [
233*8fed17f3SAndreas Gohr                'afirst' => 'foo',
234*8fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
235*8fed17f3SAndreas Gohr                'athird' => 'foobar',
236*8fed17f3SAndreas Gohr                'afourth' => 'Eve'
237*8fed17f3SAndreas Gohr            ]
238*8fed17f3SAndreas Gohr        ];
239*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
240*8fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
241*8fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
242*8fed17f3SAndreas Gohr        $response = $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
243*8fed17f3SAndreas Gohr        $actual_wikitext = trim($response->queryHTML('#wiki__text')->html());
244*8fed17f3SAndreas Gohr        $expected_wikitext = $wikitext;
245*8fed17f3SAndreas Gohr
246*8fed17f3SAndreas Gohr        $actual_errormsg = $response->queryHTML('.error')->html();
247*8fed17f3SAndreas Gohr        $expected_errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), 'afourth');
248*8fed17f3SAndreas Gohr
249*8fed17f3SAndreas Gohr        $test_html = trim($response->queryHTML('.struct_entry_form')->html());
250*8fed17f3SAndreas Gohr
251*8fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
252*8fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
253*8fed17f3SAndreas Gohr
254*8fed17f3SAndreas Gohr        // assert
255*8fed17f3SAndreas Gohr        $this->assertCount(0, $revisions);
256*8fed17f3SAndreas Gohr        $this->assertEquals($expected_errormsg, $actual_errormsg, 'If there is invalid data, then there should be an error message.');
257*8fed17f3SAndreas Gohr        $this->assertEquals($expected_wikitext, $actual_wikitext);
258*8fed17f3SAndreas Gohr
259*8fed17f3SAndreas Gohr        $pq = \phpQuery::newDocument($test_html);
260*8fed17f3SAndreas Gohr        $this->assertEquals('schema2', $pq->find('legend')->text());
261*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'afirst', 'foo');
262*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'asecond', 'bar, baz');
263*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'athird', 'foobar');
264*8fed17f3SAndreas Gohr        $this->checkField($pq, 'schema2', 'afourth', 'Eve');
265*8fed17f3SAndreas Gohr
266*8fed17f3SAndreas Gohr        // todo: assert that no struct data has been saved
267*8fed17f3SAndreas Gohr    }
268*8fed17f3SAndreas Gohr
269*8fed17f3SAndreas Gohr    public function test_save_page()
270*8fed17f3SAndreas Gohr    {
271*8fed17f3SAndreas Gohr        $page = 'test_save_page';
272*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
273*8fed17f3SAndreas Gohr        $schema = 'schema2';
274*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
275*8fed17f3SAndreas Gohr
276*8fed17f3SAndreas Gohr        $request = new \TestRequest();
277*8fed17f3SAndreas Gohr        $structData = [
278*8fed17f3SAndreas Gohr            $schema => [
279*8fed17f3SAndreas Gohr                'afirst' => 'foo',
280*8fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
281*8fed17f3SAndreas Gohr                'athird' => 'foobar',
282*8fed17f3SAndreas Gohr                'afourth' => '42'
283*8fed17f3SAndreas Gohr            ]
284*8fed17f3SAndreas Gohr        ];
285*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
286*8fed17f3SAndreas Gohr        $request->setPost('wikitext', 'teststring');
287*8fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
288*8fed17f3SAndreas Gohr        $request->post(array('id' => $page, 'do' => 'save'), '/doku.php');
289*8fed17f3SAndreas Gohr
290*8fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
291*8fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
292*8fed17f3SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
293*8fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess($schema, $page, $revisions[0]);
294*8fed17f3SAndreas Gohr        $actual_struct_data = $schemaData->getDataArray();
295*8fed17f3SAndreas Gohr        $expected_struct_data = [
296*8fed17f3SAndreas Gohr            'afirst' => 'foo',
297*8fed17f3SAndreas Gohr            'asecond' => ['bar', 'baz'],
298*8fed17f3SAndreas Gohr            'athird' => 'foobar',
299*8fed17f3SAndreas Gohr            'afourth' => 42
300*8fed17f3SAndreas Gohr        ];
301*8fed17f3SAndreas Gohr
302*8fed17f3SAndreas Gohr        $this->assertCount(1, $revisions);
303*8fed17f3SAndreas Gohr        $this->assertEquals('content and struct data saved', $revinfo['sum']);
304*8fed17f3SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_CREATE, $revinfo['type']);
305*8fed17f3SAndreas Gohr        $this->assertEquals($expected_struct_data, $actual_struct_data);
306*8fed17f3SAndreas Gohr        // todo: assert that pagerevision and struct data have the same timestamp
307*8fed17f3SAndreas Gohr    }
308*8fed17f3SAndreas Gohr
309*8fed17f3SAndreas Gohr    /**
310*8fed17f3SAndreas Gohr     * @group slow
311*8fed17f3SAndreas Gohr     */
312*8fed17f3SAndreas Gohr    public function test_save_page_without_new_text()
313*8fed17f3SAndreas Gohr    {
314*8fed17f3SAndreas Gohr        $page = 'test_save_page_without_new_text';
315*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
316*8fed17f3SAndreas Gohr        $schema = 'schema2';
317*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
318*8fed17f3SAndreas Gohr        $wikitext = 'teststring';
319*8fed17f3SAndreas Gohr
320*8fed17f3SAndreas Gohr        // first save;
321*8fed17f3SAndreas Gohr        $request = new \TestRequest();
322*8fed17f3SAndreas Gohr        $structData = [
323*8fed17f3SAndreas Gohr            $schema => [
324*8fed17f3SAndreas Gohr                'afirst' => 'foo',
325*8fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
326*8fed17f3SAndreas Gohr                'athird' => 'foobar',
327*8fed17f3SAndreas Gohr                'afourth' => '42'
328*8fed17f3SAndreas Gohr            ]
329*8fed17f3SAndreas Gohr        ];
330*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
331*8fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
332*8fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
333*8fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
334*8fed17f3SAndreas Gohr
335*8fed17f3SAndreas Gohr        $this->waitForTick(true);
336*8fed17f3SAndreas Gohr
337*8fed17f3SAndreas Gohr        // second save - only struct data
338*8fed17f3SAndreas Gohr        $request = new \TestRequest();
339*8fed17f3SAndreas Gohr        $structData = [
340*8fed17f3SAndreas Gohr            $schema => [
341*8fed17f3SAndreas Gohr                'afirst' => 'foo2',
342*8fed17f3SAndreas Gohr                'asecond' => 'bar2, baz2',
343*8fed17f3SAndreas Gohr                'athird' => 'foobar2',
344*8fed17f3SAndreas Gohr                'afourth' => '43'
345*8fed17f3SAndreas Gohr            ]
346*8fed17f3SAndreas Gohr        ];
347*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
348*8fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
349*8fed17f3SAndreas Gohr        $request->setPost('summary', '2nd revision');
350*8fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
351*8fed17f3SAndreas Gohr
352*8fed17f3SAndreas Gohr        // assert
353*8fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
354*8fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
355*8fed17f3SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
356*8fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess($schema, $page, $revisions[0]);
357*8fed17f3SAndreas Gohr        $actual_struct_data = $schemaData->getDataArray();
358*8fed17f3SAndreas Gohr        $expected_struct_data = [
359*8fed17f3SAndreas Gohr            'afirst' => 'foo2',
360*8fed17f3SAndreas Gohr            'asecond' => ['bar2', 'baz2'],
361*8fed17f3SAndreas Gohr            'athird' => 'foobar2',
362*8fed17f3SAndreas Gohr            'afourth' => 43
363*8fed17f3SAndreas Gohr        ];
364*8fed17f3SAndreas Gohr
365*8fed17f3SAndreas Gohr        $this->assertCount(2, $revisions, 'there should be 2 (two) revisions');
366*8fed17f3SAndreas Gohr        $this->assertEquals('2nd revision', $revinfo['sum']);
367*8fed17f3SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_EDIT, $revinfo['type']);
368*8fed17f3SAndreas Gohr        $this->assertEquals($expected_struct_data, $actual_struct_data);
369*8fed17f3SAndreas Gohr        // todo: assert that pagerevisions and struct entries have the same timestamps
370*8fed17f3SAndreas Gohr    }
371*8fed17f3SAndreas Gohr
372*8fed17f3SAndreas Gohr    /**
373*8fed17f3SAndreas Gohr     * @group slow
374*8fed17f3SAndreas Gohr     */
375*8fed17f3SAndreas Gohr    public function test_delete_page()
376*8fed17f3SAndreas Gohr    {
377*8fed17f3SAndreas Gohr        $page = 'test_delete_page';
378*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
379*8fed17f3SAndreas Gohr        $schema = 'schema2';
380*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
381*8fed17f3SAndreas Gohr        $wikitext = 'teststring';
382*8fed17f3SAndreas Gohr
383*8fed17f3SAndreas Gohr        // first save;
384*8fed17f3SAndreas Gohr        $request = new \TestRequest();
385*8fed17f3SAndreas Gohr        $structData = [
386*8fed17f3SAndreas Gohr            $schema => [
387*8fed17f3SAndreas Gohr                'afirst' => 'foo',
388*8fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
389*8fed17f3SAndreas Gohr                'athird' => 'foobar',
390*8fed17f3SAndreas Gohr                'afourth' => '42'
391*8fed17f3SAndreas Gohr            ]
392*8fed17f3SAndreas Gohr        ];
393*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
394*8fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
395*8fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
396*8fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
397*8fed17f3SAndreas Gohr
398*8fed17f3SAndreas Gohr        $this->waitForTick(true);
399*8fed17f3SAndreas Gohr
400*8fed17f3SAndreas Gohr        // delete
401*8fed17f3SAndreas Gohr        $request = new \TestRequest();
402*8fed17f3SAndreas Gohr        $structData = [
403*8fed17f3SAndreas Gohr            $schema => [
404*8fed17f3SAndreas Gohr                'afirst' => 'foo2',
405*8fed17f3SAndreas Gohr                'asecond' => 'bar2, baz2',
406*8fed17f3SAndreas Gohr                'athird' => 'foobar2',
407*8fed17f3SAndreas Gohr                'afourth' => '43'
408*8fed17f3SAndreas Gohr            ]
409*8fed17f3SAndreas Gohr        ];
410*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
411*8fed17f3SAndreas Gohr        $request->setPost('wikitext', '');
412*8fed17f3SAndreas Gohr        $request->setPost('summary', 'delete page');
413*8fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
414*8fed17f3SAndreas Gohr
415*8fed17f3SAndreas Gohr        // assert
416*8fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
417*8fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
418*8fed17f3SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
419*8fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess($schema, $page, $revisions[0]);
420*8fed17f3SAndreas Gohr        $actual_struct_data = $schemaData->getDataArray();
421*8fed17f3SAndreas Gohr        $expected_struct_data = [
422*8fed17f3SAndreas Gohr            'afirst' => '',
423*8fed17f3SAndreas Gohr            'asecond' => [],
424*8fed17f3SAndreas Gohr            'athird' => '',
425*8fed17f3SAndreas Gohr            'afourth' => ''
426*8fed17f3SAndreas Gohr        ];
427*8fed17f3SAndreas Gohr
428*8fed17f3SAndreas Gohr        $this->assertCount(2, $revisions, 'there should be 2 (two) revisions');
429*8fed17f3SAndreas Gohr        $this->assertEquals('delete page', $revinfo['sum']);
430*8fed17f3SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_DELETE, $revinfo['type']);
431*8fed17f3SAndreas Gohr        $this->assertEquals($expected_struct_data, $actual_struct_data);
432*8fed17f3SAndreas Gohr        // todo: timestamps?
433*8fed17f3SAndreas Gohr    }
434*8fed17f3SAndreas Gohr
435*8fed17f3SAndreas Gohr    /**
436*8fed17f3SAndreas Gohr     * @group slow
437*8fed17f3SAndreas Gohr     */
438*8fed17f3SAndreas Gohr    public function test_revert_page()
439*8fed17f3SAndreas Gohr    {
440*8fed17f3SAndreas Gohr        $page = 'test_revert_page';
441*8fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
442*8fed17f3SAndreas Gohr        $schema = 'schema2';
443*8fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
444*8fed17f3SAndreas Gohr        $wikitext = 'teststring';
445*8fed17f3SAndreas Gohr
446*8fed17f3SAndreas Gohr        global $conf;
447*8fed17f3SAndreas Gohr        $conf['useacl'] = 1;
448*8fed17f3SAndreas Gohr        $conf['superuser'] = 'admin';
449*8fed17f3SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'admin'; //now it's testing as admin
450*8fed17f3SAndreas Gohr        global $default_server_vars;
451*8fed17f3SAndreas Gohr        $default_server_vars['REMOTE_USER'] = 'admin';  //Hack until Issue #1099 is fixed
452*8fed17f3SAndreas Gohr        global $USERINFO;
453*8fed17f3SAndreas Gohr        $USERINFO['name'] = 'admin';
454*8fed17f3SAndreas Gohr        $USERINFO['mail'] = 'admin@example.com';
455*8fed17f3SAndreas Gohr        $USERINFO['grps'] = ['admin', 'user'];
456*8fed17f3SAndreas Gohr
457*8fed17f3SAndreas Gohr        // first save;
458*8fed17f3SAndreas Gohr        $request = new \TestRequest();
459*8fed17f3SAndreas Gohr        $structData = [
460*8fed17f3SAndreas Gohr            $schema => [
461*8fed17f3SAndreas Gohr                'afirst' => 'foo',
462*8fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
463*8fed17f3SAndreas Gohr                'athird' => 'foobar',
464*8fed17f3SAndreas Gohr                'afourth' => '42'
465*8fed17f3SAndreas Gohr            ]
466*8fed17f3SAndreas Gohr        ];
467*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
468*8fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
469*8fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
470*8fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save', 'sectok' => getSecurityToken()], '/doku.php');
471*8fed17f3SAndreas Gohr
472*8fed17f3SAndreas Gohr        $this->waitForTick(true);
473*8fed17f3SAndreas Gohr
474*8fed17f3SAndreas Gohr        // second save
475*8fed17f3SAndreas Gohr        $request = new \TestRequest();
476*8fed17f3SAndreas Gohr        $structData = [
477*8fed17f3SAndreas Gohr            $schema => [
478*8fed17f3SAndreas Gohr                'afirst' => 'foo2',
479*8fed17f3SAndreas Gohr                'asecond' => 'bar2, baz2',
480*8fed17f3SAndreas Gohr                'athird' => 'foobar2',
481*8fed17f3SAndreas Gohr                'afourth' => '43'
482*8fed17f3SAndreas Gohr            ]
483*8fed17f3SAndreas Gohr        ];
484*8fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
485*8fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext . $wikitext);
486*8fed17f3SAndreas Gohr        $request->setPost('summary', 'delete page');
487*8fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save', 'sectok' => getSecurityToken()], '/doku.php');
488*8fed17f3SAndreas Gohr
489*8fed17f3SAndreas Gohr        $this->waitForTick(true);
490*8fed17f3SAndreas Gohr
491*8fed17f3SAndreas Gohr        // revert to first save
492*8fed17f3SAndreas Gohr        $actpagelog = new \PageChangelog($page);
493*8fed17f3SAndreas Gohr        $actrevisions = $actpagelog->getRevisions(0, 200);
494*8fed17f3SAndreas Gohr
495*8fed17f3SAndreas Gohr        $actrevinfo = $actpagelog->getRevisionInfo($actrevisions[0]);
496*8fed17f3SAndreas Gohr        $request = new \TestRequest();
497*8fed17f3SAndreas Gohr        $request->setPost('summary', 'revert page');
498*8fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'revert', 'rev' => $actrevinfo['date'], 'sectok' => getSecurityToken()], '/doku.php');
499*8fed17f3SAndreas Gohr
500*8fed17f3SAndreas Gohr        // assert
501*8fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
502*8fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
503*8fed17f3SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
504*8fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess($schema, $page, $revisions[0]);
505*8fed17f3SAndreas Gohr        $actual_struct_data = $schemaData->getDataArray();
506*8fed17f3SAndreas Gohr        $expected_struct_data = [
507*8fed17f3SAndreas Gohr            'afirst' => 'foo',
508*8fed17f3SAndreas Gohr            'asecond' => ['bar', 'baz'],
509*8fed17f3SAndreas Gohr            'athird' => 'foobar',
510*8fed17f3SAndreas Gohr            'afourth' => '42'
511*8fed17f3SAndreas Gohr        ];
512*8fed17f3SAndreas Gohr
513*8fed17f3SAndreas Gohr        $this->assertCount(3, $revisions, 'there should be 3 (three) revisions');
514*8fed17f3SAndreas Gohr        $this->assertStringContainsString('restored', $revinfo['sum']);
515*8fed17f3SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_REVERT, $revinfo['type']);
516*8fed17f3SAndreas Gohr        $this->assertEquals($expected_struct_data, $actual_struct_data);
517*8fed17f3SAndreas Gohr        // todo: timestamps?
518*8fed17f3SAndreas Gohr    }
519*8fed17f3SAndreas Gohr
520*8fed17f3SAndreas Gohr}
521