1<?php
2
3/**
4 * @group integration
5 */
6class EditAndSaveTest extends DokuWikiTest {
7
8    /**
9     * Execute the following requests:
10     * - Section edit a page (headline 2, first occurrence)
11     * - Save a page
12     * - Redirect
13     * Check if the header id is transmitted and if the final redirect
14     * points to the correct header.
15     */
16    function testEditSaveRedirect_Headline2_A() {
17        $request = new TestRequest();
18
19        $input = array(
20            'id'     => 'int:editandsavetest'
21        );
22
23        // Show page
24        $response = $request->post($input);
25        $content = $response->getContent();
26        $this->assertTrue(!empty($content));
27
28        // If the test page has got the right content for our test it should have
29        // two headlines with the title "Headline2"
30        preg_match_all('#<h1[^>]*>Headline2</h1[^>]*>#', $content, $matches, PREG_SET_ORDER);
31        $this->assertEquals(2, count($matches));
32
33        // Get the header ids
34        $result = preg_match('/id="(.*)"/', $matches [0][0], $idA);
35        $this->assertEquals(1, $result);
36        $result = preg_match('/id="(.*)"/', $matches [1][0], $idB);
37        $this->assertEquals(1, $result);
38        $this->assertTrue($idA != $idB);
39
40        // Search the section edit form/button for the second id
41        $pattern  = '/<form class="button btn_secedit".*>.*';
42        $pattern .= '<input type="hidden" name="hid" value="';
43        $pattern .= $idA[1];
44        $pattern .= '" \/>.*<\/form>/';
45        $result = preg_match($pattern, $content, $formA);
46        $this->assertEquals(1, $result);
47
48        // Extract all inputs from the form
49        $result = preg_match_all('/<input type="hidden" name="([^"]*)" value="([^"]*)" \/>/', $formA[0], $matches, PREG_SET_ORDER);
50        $input = array();
51        foreach ($matches as $match) {
52            $input[$match[1]] = $match[2];
53        }
54        $this->assertEquals($input['hid'], $idA[1]);
55
56        // Post the input fields (= do a section edit)
57        $response = $request->post($input, '/doku.php');
58        $content = $response->getContent();
59
60        // Our header id should have been sent back to us in the edit
61        // form as an hidden input field
62        $content = str_replace("\n", " ", $content);
63        $pattern  = '/<form id="dw__editform"[^>]*>.*';
64        $pattern .= '<input type="hidden" name="hid" value="';
65        $pattern .= $idA[1];
66        $pattern .= '" \/>.*<\/form>/';
67        $result = preg_match($pattern, $content, $editForm);
68        $this->assertEquals(1, $result);
69
70        // Extract all inputs from the edit form
71        $result = preg_match_all('/<input type="hidden" name="([^"]*)" value="([^"]*)" \/>/', $editForm[0], $matches, PREG_SET_ORDER);
72        $input = array();
73        foreach ($matches as $match) {
74            $input[$match[1]] = $match[2];
75        }
76        $this->assertEquals($input['hid'], $idA[1]);
77        $input['do'] = 'save';
78
79        // Post the input fields (= save page)
80        $response = $request->post($input, '/doku.php');
81
82        // The response should carry a notification that a redirect
83        // was executed to our header ID
84        $found = $response->getData('send_redirect');
85        $this->assertCount(1, $found);
86        $hash = strpos($found[0], '#');
87        $headerID = substr($found[0], $hash);
88        $this->assertEquals($headerID, '#'.$idA[1]);
89    }
90
91    /**
92     * Execute the following requests:
93     * - Section edit a page (headline 2, second occurrence)
94     * - Save a page
95     * - Redirect
96     * Check if the header id is transmitted and if the final redirect
97     * points to the correct header.
98     */
99    function testEditSaveRedirect_Headline2_B() {
100        $request = new TestRequest();
101
102        $input = array(
103            'id'     => 'int:editandsavetest'
104        );
105
106        // Show page
107        $response = $request->post($input);
108        $content = $response->getContent();
109        $this->assertTrue(!empty($content));
110
111        // If the test page has got the right content for our test it should have
112        // two headlines with the title "Headline2"
113        preg_match_all('#<h1[^>]*>Headline2</h1[^>]*>#', $content, $matches, PREG_SET_ORDER);
114        $this->assertEquals(2, count($matches));
115
116        // Get the header ids
117        $result = preg_match('/id="(.*)"/', $matches [0][0], $idA);
118        $this->assertEquals(1, $result);
119        $result = preg_match('/id="(.*)"/', $matches [1][0], $idB);
120        $this->assertEquals(1, $result);
121        $this->assertTrue($idA != $idB);
122
123        // Search the section edit form/button for the second id
124        $pattern  = '/<form class="button btn_secedit".*>.*';
125        $pattern .= '<input type="hidden" name="hid" value="';
126        $pattern .= $idB[1];
127        $pattern .= '" \/>.*<\/form>/';
128        $result = preg_match($pattern, $content, $formB);
129        $this->assertEquals(1, $result);
130
131        // Extract all inputs from the form
132        $result = preg_match_all('/<input type="hidden" name="([^"]*)" value="([^"]*)" \/>/', $formB[0], $matches, PREG_SET_ORDER);
133        $input = array();
134        foreach ($matches as $match) {
135            $input[$match[1]] = $match[2];
136        }
137        $this->assertEquals($input['hid'], $idB[1]);
138
139        // Post the input fields (= do a section edit)
140        $response = $request->post($input, '/doku.php');
141        $content = $response->getContent();
142
143        // Our header id should have been sent back to us in the edit
144        // form as an hidden input field
145        $content = str_replace("\n", " ", $content);
146        $pattern  = '/<form id="dw__editform"[^>]*>.*';
147        $pattern .= '<input type="hidden" name="hid" value="';
148        $pattern .= $idB[1];
149        $pattern .= '" \/>.*<\/form>/';
150        $result = preg_match($pattern, $content, $editForm);
151        $this->assertEquals(1, $result);
152
153        // Extract all inputs from the edit form
154        $result = preg_match_all('/<input type="hidden" name="([^"]*)" value="([^"]*)" \/>/', $editForm[0], $matches, PREG_SET_ORDER);
155        $input = array();
156        foreach ($matches as $match) {
157            $input[$match[1]] = $match[2];
158        }
159        $this->assertEquals($input['hid'], $idB[1]);
160        $input['do'] = 'save';
161
162        // Post the input fields (= save page)
163        $response = $request->post($input, '/doku.php');
164
165        // The response should carry a notification that a redirect
166        // was executed to our header ID
167        $found = $response->getData('send_redirect');
168        $this->assertCount(1, $found);
169        $hash = strpos($found[0], '#');
170        $headerID = substr($found[0], $hash);
171        $this->assertEquals($headerID, '#'.$idB[1]);
172    }
173}
174