xref: /plugin/struct/_test/action/EditTest.php (revision 850ad33e349567a75f310427bcb4c8bac9898406)
18fed17f3SAndreas Gohr<?php
28fed17f3SAndreas Gohr
38fed17f3SAndreas Gohrnamespace dokuwiki\plugin\struct\test\action;
48fed17f3SAndreas Gohr
58fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta;
68fed17f3SAndreas Gohruse dokuwiki\plugin\struct\test\mock\action_plugin_struct_edit;
78fed17f3SAndreas Gohruse dokuwiki\plugin\struct\test\mock\Assignments;
88fed17f3SAndreas Gohruse dokuwiki\plugin\struct\test\StructTest;
9*850ad33eSAndreas Gohruse DOMWrap\Document;
108fed17f3SAndreas Gohr
118fed17f3SAndreas Gohr/**
128fed17f3SAndreas Gohr * @group plugin_struct
138fed17f3SAndreas Gohr * @group plugins
148fed17f3SAndreas Gohr *
158fed17f3SAndreas Gohr * @covers action_plugin_struct_revert
168fed17f3SAndreas Gohr * @covers action_plugin_struct_edit
178fed17f3SAndreas Gohr */
188fed17f3SAndreas Gohrclass EditTest extends StructTest
198fed17f3SAndreas Gohr{
208fed17f3SAndreas Gohr
218fed17f3SAndreas Gohr    public function setUp(): void
228fed17f3SAndreas Gohr    {
238fed17f3SAndreas Gohr        parent::setUp();
248fed17f3SAndreas Gohr
258fed17f3SAndreas Gohr        $this->loadSchemaJSON('schema1');
268fed17f3SAndreas Gohr        $this->loadSchemaJSON('schema2', 'schema2int');
278fed17f3SAndreas Gohr        $this->saveData(
288fed17f3SAndreas Gohr            'page01',
298fed17f3SAndreas Gohr            'schema1',
308fed17f3SAndreas Gohr            [
318fed17f3SAndreas Gohr                'first' => 'first data',
328fed17f3SAndreas Gohr                'second' => ['second data', 'more data', 'even more'],
338fed17f3SAndreas Gohr                'third' => 'third data',
348fed17f3SAndreas Gohr                'fourth' => 'fourth data'
358fed17f3SAndreas Gohr            ],
368fed17f3SAndreas Gohr            time()
378fed17f3SAndreas Gohr        );
388fed17f3SAndreas Gohr    }
398fed17f3SAndreas Gohr
40*850ad33eSAndreas Gohr    /**
41*850ad33eSAndreas Gohr     * Check if a field has the correct value
42*850ad33eSAndreas Gohr     *
43*850ad33eSAndreas Gohr     * @param Document $pq
44*850ad33eSAndreas Gohr     * @param string $schema
45*850ad33eSAndreas Gohr     * @param string $name
46*850ad33eSAndreas Gohr     * @param string $value
47*850ad33eSAndreas Gohr     */
48*850ad33eSAndreas Gohr    protected function checkField(Document $pq, $schema, $name, $value)
498fed17f3SAndreas Gohr    {
50*850ad33eSAndreas Gohr        $this->assertEquals(
51*850ad33eSAndreas Gohr            1,
52*850ad33eSAndreas Gohr            $pq->find("span.label:contains('$name')")->count(),
53*850ad33eSAndreas Gohr            "Field $schema.$name not found"
54*850ad33eSAndreas Gohr        );
55*850ad33eSAndreas Gohr        $this->assertEquals(
56*850ad33eSAndreas Gohr            $value,
57*850ad33eSAndreas Gohr            $pq->find("input[name='struct_schema_data[$schema][$name]']")->attr('value'),
58*850ad33eSAndreas Gohr            "Field $schema.$name has wrong value"
59*850ad33eSAndreas Gohr        );
608fed17f3SAndreas Gohr    }
618fed17f3SAndreas Gohr
628fed17f3SAndreas Gohr    public function test_createForm_storedData()
638fed17f3SAndreas Gohr    {
648fed17f3SAndreas Gohr        $edit = new action_plugin_struct_edit();
658fed17f3SAndreas Gohr        global $ID;
668fed17f3SAndreas Gohr        $ID = 'page01';
678fed17f3SAndreas Gohr        $test_html = $edit->createForm('schema1');
688fed17f3SAndreas Gohr
69*850ad33eSAndreas Gohr        $doc = new Document();
70*850ad33eSAndreas Gohr        $doc->loadHTML($test_html);
71*850ad33eSAndreas Gohr        $this->assertEquals('schema1', $doc->find('legend')->text());
72*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'first', 'first data');
73*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'second', 'second data, more data, even more');
74*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'third', 'third data');
75*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'fourth', 'fourth data');
768fed17f3SAndreas Gohr    }
778fed17f3SAndreas Gohr
788fed17f3SAndreas Gohr    public function test_createForm_emptyData()
798fed17f3SAndreas Gohr    {
808fed17f3SAndreas Gohr        $edit = new action_plugin_struct_edit();
818fed17f3SAndreas Gohr        global $ID;
828fed17f3SAndreas Gohr        $ID = 'page02';
838fed17f3SAndreas Gohr        $test_html = $edit->createForm('schema1');
848fed17f3SAndreas Gohr
85*850ad33eSAndreas Gohr        $doc = new Document();
86*850ad33eSAndreas Gohr        $doc->loadHTML($test_html);
87*850ad33eSAndreas Gohr        $this->assertEquals('schema1', $doc->find('legend')->text());
88*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'first', '');
89*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'second', '');
90*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'third', '');
91*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'fourth', '');
928fed17f3SAndreas Gohr    }
938fed17f3SAndreas Gohr
948fed17f3SAndreas Gohr    public function test_createForm_postData()
958fed17f3SAndreas Gohr    {
968fed17f3SAndreas Gohr        global $INPUT, $ID;
978fed17f3SAndreas Gohr        $ID = 'page01';
988fed17f3SAndreas Gohr        $structdata = [
998fed17f3SAndreas Gohr            'schema1' => [
1008fed17f3SAndreas Gohr                'first' => 'first post data',
1018fed17f3SAndreas Gohr                'second' => ['second post data', 'more post data', 'even more post data'],
1028fed17f3SAndreas Gohr                'third' => 'third post data',
1038fed17f3SAndreas Gohr                'fourth' => 'fourth post data'
1048fed17f3SAndreas Gohr            ]
1058fed17f3SAndreas Gohr        ];
1068fed17f3SAndreas Gohr        $INPUT->set(action_plugin_struct_edit::getVAR(), $structdata);
1078fed17f3SAndreas Gohr
1088fed17f3SAndreas Gohr        $edit = new action_plugin_struct_edit();
1098fed17f3SAndreas Gohr        $test_html = $edit->createForm('schema1');
1108fed17f3SAndreas Gohr
111*850ad33eSAndreas Gohr        $doc = new Document();
112*850ad33eSAndreas Gohr        $doc->loadHTML($test_html);
113*850ad33eSAndreas Gohr        $this->assertEquals('schema1', $doc->find('legend')->text());
114*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'first', 'first post data');
115*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'second', 'second post data, more post data, even more post data');
116*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'third', 'third post data');
117*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema1', 'fourth', 'fourth post data');
1188fed17f3SAndreas Gohr    }
1198fed17f3SAndreas Gohr
1208fed17f3SAndreas Gohr    public function test_edit_page_wo_schema()
1218fed17f3SAndreas Gohr    {
1228fed17f3SAndreas Gohr        $page = 'test_edit_page_wo_schema';
1238fed17f3SAndreas Gohr
1248fed17f3SAndreas Gohr        $request = new \TestRequest();
1258fed17f3SAndreas Gohr        $response = $request->get(['id' => $page, 'do' => 'edit'], '/doku.php');
1268fed17f3SAndreas Gohr        $structElement = $response->queryHTML('.struct_entry_form');
1278fed17f3SAndreas Gohr
1288fed17f3SAndreas Gohr        $this->assertCount(1, $structElement);
1298fed17f3SAndreas Gohr        $this->assertEquals($structElement->html(), '');
1308fed17f3SAndreas Gohr    }
1318fed17f3SAndreas Gohr
1328fed17f3SAndreas Gohr    public function test_edit_page_with_schema()
1338fed17f3SAndreas Gohr    {
1348fed17f3SAndreas Gohr        $page = 'test_edit_page_with_schema';
1358fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
1368fed17f3SAndreas Gohr        $schema = 'schema2';
1378fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
1388fed17f3SAndreas Gohr
1398fed17f3SAndreas Gohr        $request = new \TestRequest();
1408fed17f3SAndreas Gohr        $response = $request->get(['id' => $page, 'do' => 'edit'], '/doku.php');
1418fed17f3SAndreas Gohr        $test_html = trim($response->queryHTML('.struct_entry_form')->html());
1428fed17f3SAndreas Gohr
143*850ad33eSAndreas Gohr        $doc = new Document();
144*850ad33eSAndreas Gohr        $doc->loadHTML($test_html);
145*850ad33eSAndreas Gohr        $this->assertEquals('schema2', $doc->find('legend')->text());
146*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'afirst', '');
147*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'asecond', '');
148*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'athird', '');
149*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'afourth', '');
1508fed17f3SAndreas Gohr    }
1518fed17f3SAndreas Gohr
1528fed17f3SAndreas Gohr    public function test_preview_page_invaliddata()
1538fed17f3SAndreas Gohr    {
1548fed17f3SAndreas Gohr        $page = 'test_preview_page_invaliddata';
1558fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
1568fed17f3SAndreas Gohr        $schema = 'schema2';
1578fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
1588fed17f3SAndreas Gohr
1598fed17f3SAndreas Gohr        $request = new \TestRequest();
1608fed17f3SAndreas Gohr        $structData = [
1618fed17f3SAndreas Gohr            $schema => [
1628fed17f3SAndreas Gohr                'afirst' => 'foo',
1638fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
1648fed17f3SAndreas Gohr                'athird' => 'foobar',
1658fed17f3SAndreas Gohr                'afourth' => 'Eve'
1668fed17f3SAndreas Gohr            ]
1678fed17f3SAndreas Gohr        ];
1688fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
1698fed17f3SAndreas Gohr        $response = $request->post(['id' => $page, 'do' => 'preview'], '/doku.php');
1708fed17f3SAndreas Gohr        $expected_errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), 'afourth');
1718fed17f3SAndreas Gohr        $actual_errormsg = $response->queryHTML('.error')->html();
1728fed17f3SAndreas Gohr        $test_html = trim($response->queryHTML('.struct_entry_form')->html());
1738fed17f3SAndreas Gohr
1748fed17f3SAndreas Gohr        $this->assertEquals($expected_errormsg, $actual_errormsg, 'If there is invalid data, then there should be an error message.');
1758fed17f3SAndreas Gohr
176*850ad33eSAndreas Gohr        $doc = new Document();
177*850ad33eSAndreas Gohr        $doc->loadHTML($test_html);
178*850ad33eSAndreas Gohr        $this->assertEquals('schema2', $doc->find('legend')->text());
179*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'afirst', 'foo');
180*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'asecond', 'bar, baz');
181*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'athird', 'foobar');
182*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'afourth', 'Eve');
1838fed17f3SAndreas Gohr    }
1848fed17f3SAndreas Gohr
1858fed17f3SAndreas Gohr    public function test_preview_page_validdata()
1868fed17f3SAndreas Gohr    {
1878fed17f3SAndreas Gohr        $page = 'test_preview_page_validdata';
1888fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
1898fed17f3SAndreas Gohr        $schema = 'schema2';
1908fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
1918fed17f3SAndreas Gohr
1928fed17f3SAndreas Gohr        $request = new \TestRequest();
1938fed17f3SAndreas Gohr        $structData = [
1948fed17f3SAndreas Gohr            $schema => [
1958fed17f3SAndreas Gohr                'afirst' => 'foo',
1968fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
1978fed17f3SAndreas Gohr                'athird' => 'foobar',
1988fed17f3SAndreas Gohr                'afourth' => '42'
1998fed17f3SAndreas Gohr            ]
2008fed17f3SAndreas Gohr        ];
2018fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
202*850ad33eSAndreas Gohr        $response = $request->post(['id' => $page, 'do' => 'preview']);
203*850ad33eSAndreas Gohr        $actual_errormsg = $response->queryHTML('.error')->get(0);
204*850ad33eSAndreas Gohr        $this->assertNull($actual_errormsg, "If all data is valid, then there should be no error message.");
205*850ad33eSAndreas Gohr
2068fed17f3SAndreas Gohr        $test_html = trim($response->queryHTML('.struct_entry_form')->html());
2078fed17f3SAndreas Gohr
208*850ad33eSAndreas Gohr        $doc = new Document();
209*850ad33eSAndreas Gohr        $doc->loadHTML($test_html);
210*850ad33eSAndreas Gohr        $this->assertEquals('schema2', $doc->find('legend')->text());
211*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'afirst', 'foo');
212*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'asecond', 'bar, baz');
213*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'athird', 'foobar');
214*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'afourth', '42');
2158fed17f3SAndreas Gohr    }
2168fed17f3SAndreas Gohr
2178fed17f3SAndreas Gohr    public function test_fail_saving_empty_page()
2188fed17f3SAndreas Gohr    {
2198fed17f3SAndreas Gohr        $page = 'test_fail_saving_empty_page';
2208fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
2218fed17f3SAndreas Gohr        $schema = 'schema2';
2228fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
2238fed17f3SAndreas Gohr
2248fed17f3SAndreas Gohr        $request = new \TestRequest();
2258fed17f3SAndreas Gohr        $structData = [
2268fed17f3SAndreas Gohr            $schema => [
2278fed17f3SAndreas Gohr                'afirst' => 'foo',
2288fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
2298fed17f3SAndreas Gohr                'athird' => 'foobar',
2308fed17f3SAndreas Gohr                'afourth' => '42'
2318fed17f3SAndreas Gohr            ]
2328fed17f3SAndreas Gohr        ];
2338fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
2348fed17f3SAndreas Gohr        $request->setPost('summary', 'only struct data saved');
2358fed17f3SAndreas Gohr        $response = $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
2368fed17f3SAndreas Gohr        $expected_errormsg = $this->getLang('emptypage');
2378fed17f3SAndreas Gohr        $actual_errormsg = $response->queryHTML('.error')->html();
2388fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
2398fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
2408fed17f3SAndreas Gohr
2418fed17f3SAndreas Gohr        $this->assertCount(0, $revisions);
2428fed17f3SAndreas Gohr        $this->assertEquals($expected_errormsg, $actual_errormsg, "An empty page should not be saved.");
2438fed17f3SAndreas Gohr    }
2448fed17f3SAndreas Gohr
2458fed17f3SAndreas Gohr    public function test_fail_saveing_page_with_invaliddata()
2468fed17f3SAndreas Gohr    {
2478fed17f3SAndreas Gohr        $page = 'test_fail_saveing_page_with_invaliddata';
2488fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
2498fed17f3SAndreas Gohr        $schema = 'schema2';
2508fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
2518fed17f3SAndreas Gohr
2528fed17f3SAndreas Gohr        $wikitext = 'teststring';
2538fed17f3SAndreas Gohr        $request = new \TestRequest();
2548fed17f3SAndreas Gohr        $structData = [
2558fed17f3SAndreas Gohr            $schema => [
2568fed17f3SAndreas Gohr                'afirst' => 'foo',
2578fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
2588fed17f3SAndreas Gohr                'athird' => 'foobar',
2598fed17f3SAndreas Gohr                'afourth' => 'Eve'
2608fed17f3SAndreas Gohr            ]
2618fed17f3SAndreas Gohr        ];
2628fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
2638fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
2648fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
2658fed17f3SAndreas Gohr        $response = $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
2668fed17f3SAndreas Gohr        $actual_wikitext = trim($response->queryHTML('#wiki__text')->html());
2678fed17f3SAndreas Gohr        $expected_wikitext = $wikitext;
2688fed17f3SAndreas Gohr
2698fed17f3SAndreas Gohr        $actual_errormsg = $response->queryHTML('.error')->html();
2708fed17f3SAndreas Gohr        $expected_errormsg = sprintf($this->getLang('validation_prefix') . $this->getLang('Validation Exception Decimal needed'), 'afourth');
2718fed17f3SAndreas Gohr
2728fed17f3SAndreas Gohr        $test_html = trim($response->queryHTML('.struct_entry_form')->html());
2738fed17f3SAndreas Gohr
2748fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
2758fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
2768fed17f3SAndreas Gohr
2778fed17f3SAndreas Gohr        // assert
2788fed17f3SAndreas Gohr        $this->assertCount(0, $revisions);
2798fed17f3SAndreas Gohr        $this->assertEquals($expected_errormsg, $actual_errormsg, 'If there is invalid data, then there should be an error message.');
2808fed17f3SAndreas Gohr        $this->assertEquals($expected_wikitext, $actual_wikitext);
2818fed17f3SAndreas Gohr
282*850ad33eSAndreas Gohr        $doc = new Document();
283*850ad33eSAndreas Gohr        $doc->loadHTML($test_html);
284*850ad33eSAndreas Gohr        $this->assertEquals('schema2', $doc->find('legend')->text());
285*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'afirst', 'foo');
286*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'asecond', 'bar, baz');
287*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'athird', 'foobar');
288*850ad33eSAndreas Gohr        $this->checkField($doc, 'schema2', 'afourth', 'Eve');
2898fed17f3SAndreas Gohr
2908fed17f3SAndreas Gohr        // todo: assert that no struct data has been saved
2918fed17f3SAndreas Gohr    }
2928fed17f3SAndreas Gohr
2938fed17f3SAndreas Gohr    public function test_save_page()
2948fed17f3SAndreas Gohr    {
2958fed17f3SAndreas Gohr        $page = 'test_save_page';
2968fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
2978fed17f3SAndreas Gohr        $schema = 'schema2';
2988fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
2998fed17f3SAndreas Gohr
3008fed17f3SAndreas Gohr        $request = new \TestRequest();
3018fed17f3SAndreas Gohr        $structData = [
3028fed17f3SAndreas Gohr            $schema => [
3038fed17f3SAndreas Gohr                'afirst' => 'foo',
3048fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
3058fed17f3SAndreas Gohr                'athird' => 'foobar',
3068fed17f3SAndreas Gohr                'afourth' => '42'
3078fed17f3SAndreas Gohr            ]
3088fed17f3SAndreas Gohr        ];
3098fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
3108fed17f3SAndreas Gohr        $request->setPost('wikitext', 'teststring');
3118fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
3128fed17f3SAndreas Gohr        $request->post(array('id' => $page, 'do' => 'save'), '/doku.php');
3138fed17f3SAndreas Gohr
3148fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
3158fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
3168fed17f3SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
3178fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess($schema, $page, $revisions[0]);
3188fed17f3SAndreas Gohr        $actual_struct_data = $schemaData->getDataArray();
3198fed17f3SAndreas Gohr        $expected_struct_data = [
3208fed17f3SAndreas Gohr            'afirst' => 'foo',
3218fed17f3SAndreas Gohr            'asecond' => ['bar', 'baz'],
3228fed17f3SAndreas Gohr            'athird' => 'foobar',
3238fed17f3SAndreas Gohr            'afourth' => 42
3248fed17f3SAndreas Gohr        ];
3258fed17f3SAndreas Gohr
3268fed17f3SAndreas Gohr        $this->assertCount(1, $revisions);
3278fed17f3SAndreas Gohr        $this->assertEquals('content and struct data saved', $revinfo['sum']);
3288fed17f3SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_CREATE, $revinfo['type']);
3298fed17f3SAndreas Gohr        $this->assertEquals($expected_struct_data, $actual_struct_data);
3308fed17f3SAndreas Gohr        // todo: assert that pagerevision and struct data have the same timestamp
3318fed17f3SAndreas Gohr    }
3328fed17f3SAndreas Gohr
3338fed17f3SAndreas Gohr    /**
3348fed17f3SAndreas Gohr     * @group slow
3358fed17f3SAndreas Gohr     */
3368fed17f3SAndreas Gohr    public function test_save_page_without_new_text()
3378fed17f3SAndreas Gohr    {
3388fed17f3SAndreas Gohr        $page = 'test_save_page_without_new_text';
3398fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
3408fed17f3SAndreas Gohr        $schema = 'schema2';
3418fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
3428fed17f3SAndreas Gohr        $wikitext = 'teststring';
3438fed17f3SAndreas Gohr
3448fed17f3SAndreas Gohr        // first save;
3458fed17f3SAndreas Gohr        $request = new \TestRequest();
3468fed17f3SAndreas Gohr        $structData = [
3478fed17f3SAndreas Gohr            $schema => [
3488fed17f3SAndreas Gohr                'afirst' => 'foo',
3498fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
3508fed17f3SAndreas Gohr                'athird' => 'foobar',
3518fed17f3SAndreas Gohr                'afourth' => '42'
3528fed17f3SAndreas Gohr            ]
3538fed17f3SAndreas Gohr        ];
3548fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
3558fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
3568fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
3578fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
3588fed17f3SAndreas Gohr
3598fed17f3SAndreas Gohr        $this->waitForTick(true);
3608fed17f3SAndreas Gohr
3618fed17f3SAndreas Gohr        // second save - only struct data
3628fed17f3SAndreas Gohr        $request = new \TestRequest();
3638fed17f3SAndreas Gohr        $structData = [
3648fed17f3SAndreas Gohr            $schema => [
3658fed17f3SAndreas Gohr                'afirst' => 'foo2',
3668fed17f3SAndreas Gohr                'asecond' => 'bar2, baz2',
3678fed17f3SAndreas Gohr                'athird' => 'foobar2',
3688fed17f3SAndreas Gohr                'afourth' => '43'
3698fed17f3SAndreas Gohr            ]
3708fed17f3SAndreas Gohr        ];
3718fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
3728fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
3738fed17f3SAndreas Gohr        $request->setPost('summary', '2nd revision');
3748fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
3758fed17f3SAndreas Gohr
3768fed17f3SAndreas Gohr        // assert
3778fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
3788fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
3798fed17f3SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
3808fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess($schema, $page, $revisions[0]);
3818fed17f3SAndreas Gohr        $actual_struct_data = $schemaData->getDataArray();
3828fed17f3SAndreas Gohr        $expected_struct_data = [
3838fed17f3SAndreas Gohr            'afirst' => 'foo2',
3848fed17f3SAndreas Gohr            'asecond' => ['bar2', 'baz2'],
3858fed17f3SAndreas Gohr            'athird' => 'foobar2',
3868fed17f3SAndreas Gohr            'afourth' => 43
3878fed17f3SAndreas Gohr        ];
3888fed17f3SAndreas Gohr
3898fed17f3SAndreas Gohr        $this->assertCount(2, $revisions, 'there should be 2 (two) revisions');
3908fed17f3SAndreas Gohr        $this->assertEquals('2nd revision', $revinfo['sum']);
3918fed17f3SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_EDIT, $revinfo['type']);
3928fed17f3SAndreas Gohr        $this->assertEquals($expected_struct_data, $actual_struct_data);
3938fed17f3SAndreas Gohr        // todo: assert that pagerevisions and struct entries have the same timestamps
3948fed17f3SAndreas Gohr    }
3958fed17f3SAndreas Gohr
3968fed17f3SAndreas Gohr    /**
3978fed17f3SAndreas Gohr     * @group slow
3988fed17f3SAndreas Gohr     */
3998fed17f3SAndreas Gohr    public function test_delete_page()
4008fed17f3SAndreas Gohr    {
4018fed17f3SAndreas Gohr        $page = 'test_delete_page';
4028fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
4038fed17f3SAndreas Gohr        $schema = 'schema2';
4048fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
4058fed17f3SAndreas Gohr        $wikitext = 'teststring';
4068fed17f3SAndreas Gohr
4078fed17f3SAndreas Gohr        // first save;
4088fed17f3SAndreas Gohr        $request = new \TestRequest();
4098fed17f3SAndreas Gohr        $structData = [
4108fed17f3SAndreas Gohr            $schema => [
4118fed17f3SAndreas Gohr                'afirst' => 'foo',
4128fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
4138fed17f3SAndreas Gohr                'athird' => 'foobar',
4148fed17f3SAndreas Gohr                'afourth' => '42'
4158fed17f3SAndreas Gohr            ]
4168fed17f3SAndreas Gohr        ];
4178fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
4188fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
4198fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
4208fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
4218fed17f3SAndreas Gohr
4228fed17f3SAndreas Gohr        $this->waitForTick(true);
4238fed17f3SAndreas Gohr
4248fed17f3SAndreas Gohr        // delete
4258fed17f3SAndreas Gohr        $request = new \TestRequest();
4268fed17f3SAndreas Gohr        $structData = [
4278fed17f3SAndreas Gohr            $schema => [
4288fed17f3SAndreas Gohr                'afirst' => 'foo2',
4298fed17f3SAndreas Gohr                'asecond' => 'bar2, baz2',
4308fed17f3SAndreas Gohr                'athird' => 'foobar2',
4318fed17f3SAndreas Gohr                'afourth' => '43'
4328fed17f3SAndreas Gohr            ]
4338fed17f3SAndreas Gohr        ];
4348fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
4358fed17f3SAndreas Gohr        $request->setPost('wikitext', '');
4368fed17f3SAndreas Gohr        $request->setPost('summary', 'delete page');
4378fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save'], '/doku.php');
4388fed17f3SAndreas Gohr
4398fed17f3SAndreas Gohr        // assert
4408fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
4418fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
4428fed17f3SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
4438fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess($schema, $page, $revisions[0]);
4448fed17f3SAndreas Gohr        $actual_struct_data = $schemaData->getDataArray();
4458fed17f3SAndreas Gohr        $expected_struct_data = [
4468fed17f3SAndreas Gohr            'afirst' => '',
4478fed17f3SAndreas Gohr            'asecond' => [],
4488fed17f3SAndreas Gohr            'athird' => '',
4498fed17f3SAndreas Gohr            'afourth' => ''
4508fed17f3SAndreas Gohr        ];
4518fed17f3SAndreas Gohr
4528fed17f3SAndreas Gohr        $this->assertCount(2, $revisions, 'there should be 2 (two) revisions');
4538fed17f3SAndreas Gohr        $this->assertEquals('delete page', $revinfo['sum']);
4548fed17f3SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_DELETE, $revinfo['type']);
4558fed17f3SAndreas Gohr        $this->assertEquals($expected_struct_data, $actual_struct_data);
4568fed17f3SAndreas Gohr        // todo: timestamps?
4578fed17f3SAndreas Gohr    }
4588fed17f3SAndreas Gohr
4598fed17f3SAndreas Gohr    /**
4608fed17f3SAndreas Gohr     * @group slow
4618fed17f3SAndreas Gohr     */
4628fed17f3SAndreas Gohr    public function test_revert_page()
4638fed17f3SAndreas Gohr    {
4648fed17f3SAndreas Gohr        $page = 'test_revert_page';
4658fed17f3SAndreas Gohr        $assignment = Assignments::getInstance();
4668fed17f3SAndreas Gohr        $schema = 'schema2';
4678fed17f3SAndreas Gohr        $assignment->addPattern($page, $schema);
4688fed17f3SAndreas Gohr        $wikitext = 'teststring';
4698fed17f3SAndreas Gohr
4708fed17f3SAndreas Gohr        global $conf;
4718fed17f3SAndreas Gohr        $conf['useacl'] = 1;
4728fed17f3SAndreas Gohr        $conf['superuser'] = 'admin';
4738fed17f3SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'admin'; //now it's testing as admin
4748fed17f3SAndreas Gohr        global $default_server_vars;
4758fed17f3SAndreas Gohr        $default_server_vars['REMOTE_USER'] = 'admin';  //Hack until Issue #1099 is fixed
4768fed17f3SAndreas Gohr        global $USERINFO;
4778fed17f3SAndreas Gohr        $USERINFO['name'] = 'admin';
4788fed17f3SAndreas Gohr        $USERINFO['mail'] = 'admin@example.com';
4798fed17f3SAndreas Gohr        $USERINFO['grps'] = ['admin', 'user'];
4808fed17f3SAndreas Gohr
4818fed17f3SAndreas Gohr        // first save;
4828fed17f3SAndreas Gohr        $request = new \TestRequest();
4838fed17f3SAndreas Gohr        $structData = [
4848fed17f3SAndreas Gohr            $schema => [
4858fed17f3SAndreas Gohr                'afirst' => 'foo',
4868fed17f3SAndreas Gohr                'asecond' => 'bar, baz',
4878fed17f3SAndreas Gohr                'athird' => 'foobar',
4888fed17f3SAndreas Gohr                'afourth' => '42'
4898fed17f3SAndreas Gohr            ]
4908fed17f3SAndreas Gohr        ];
4918fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
4928fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext);
4938fed17f3SAndreas Gohr        $request->setPost('summary', 'content and struct data saved');
4948fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save', 'sectok' => getSecurityToken()], '/doku.php');
4958fed17f3SAndreas Gohr
4968fed17f3SAndreas Gohr        $this->waitForTick(true);
4978fed17f3SAndreas Gohr
4988fed17f3SAndreas Gohr        // second save
4998fed17f3SAndreas Gohr        $request = new \TestRequest();
5008fed17f3SAndreas Gohr        $structData = [
5018fed17f3SAndreas Gohr            $schema => [
5028fed17f3SAndreas Gohr                'afirst' => 'foo2',
5038fed17f3SAndreas Gohr                'asecond' => 'bar2, baz2',
5048fed17f3SAndreas Gohr                'athird' => 'foobar2',
5058fed17f3SAndreas Gohr                'afourth' => '43'
5068fed17f3SAndreas Gohr            ]
5078fed17f3SAndreas Gohr        ];
5088fed17f3SAndreas Gohr        $request->setPost('struct_schema_data', $structData);
5098fed17f3SAndreas Gohr        $request->setPost('wikitext', $wikitext . $wikitext);
5108fed17f3SAndreas Gohr        $request->setPost('summary', 'delete page');
5118fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'save', 'sectok' => getSecurityToken()], '/doku.php');
5128fed17f3SAndreas Gohr
5138fed17f3SAndreas Gohr        $this->waitForTick(true);
5148fed17f3SAndreas Gohr
5158fed17f3SAndreas Gohr        // revert to first save
5168fed17f3SAndreas Gohr        $actpagelog = new \PageChangelog($page);
5178fed17f3SAndreas Gohr        $actrevisions = $actpagelog->getRevisions(0, 200);
5188fed17f3SAndreas Gohr
5198fed17f3SAndreas Gohr        $actrevinfo = $actpagelog->getRevisionInfo($actrevisions[0]);
5208fed17f3SAndreas Gohr        $request = new \TestRequest();
5218fed17f3SAndreas Gohr        $request->setPost('summary', 'revert page');
5228fed17f3SAndreas Gohr        $request->post(['id' => $page, 'do' => 'revert', 'rev' => $actrevinfo['date'], 'sectok' => getSecurityToken()], '/doku.php');
5238fed17f3SAndreas Gohr
5248fed17f3SAndreas Gohr        // assert
5258fed17f3SAndreas Gohr        $pagelog = new \PageChangelog($page);
5268fed17f3SAndreas Gohr        $revisions = $pagelog->getRevisions(-1, 200);
5278fed17f3SAndreas Gohr        $revinfo = $pagelog->getRevisionInfo($revisions[0]);
5288fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess($schema, $page, $revisions[0]);
5298fed17f3SAndreas Gohr        $actual_struct_data = $schemaData->getDataArray();
5308fed17f3SAndreas Gohr        $expected_struct_data = [
5318fed17f3SAndreas Gohr            'afirst' => 'foo',
5328fed17f3SAndreas Gohr            'asecond' => ['bar', 'baz'],
5338fed17f3SAndreas Gohr            'athird' => 'foobar',
5348fed17f3SAndreas Gohr            'afourth' => '42'
5358fed17f3SAndreas Gohr        ];
5368fed17f3SAndreas Gohr
5378fed17f3SAndreas Gohr        $this->assertCount(3, $revisions, 'there should be 3 (three) revisions');
5388fed17f3SAndreas Gohr        $this->assertStringContainsString('restored', $revinfo['sum']);
5398fed17f3SAndreas Gohr        $this->assertEquals(DOKU_CHANGE_TYPE_REVERT, $revinfo['type']);
5408fed17f3SAndreas Gohr        $this->assertEquals($expected_struct_data, $actual_struct_data);
5418fed17f3SAndreas Gohr        // todo: timestamps?
5428fed17f3SAndreas Gohr    }
5438fed17f3SAndreas Gohr
5448fed17f3SAndreas Gohr}
545