xref: /dokuwiki/_test/tests/Form/TextareaElementTest.php (revision 3990423560cec42274c045493f63387a636b239e)
1*39904235SAndreas Gohr<?php
2*39904235SAndreas Gohr
3*39904235SAndreas Gohrnamespace dokuwiki\test\Form;
4*39904235SAndreas Gohr
5*39904235SAndreas Gohruse dokuwiki\Form;
6*39904235SAndreas Gohr
7*39904235SAndreas Gohrclass TextareaElementTest extends \DokuWikiTest
8*39904235SAndreas Gohr{
9*39904235SAndreas Gohr    /**
10*39904235SAndreas Gohr     * Create a form with a textarea element and return the raw inner HTML of the textarea.
11*39904235SAndreas Gohr     */
12*39904235SAndreas Gohr    private function rawTextareaBody(string $value): string
13*39904235SAndreas Gohr    {
14*39904235SAndreas Gohr        $form = new Form\Form();
15*39904235SAndreas Gohr        $form->addTextarea('wikitext', 'label')->val($value);
16*39904235SAndreas Gohr
17*39904235SAndreas Gohr        $this->assertSame(
18*39904235SAndreas Gohr            1,
19*39904235SAndreas Gohr            preg_match('#<textarea[^>]*>(.*?)</textarea>#s', $form->toHTML(), $m),
20*39904235SAndreas Gohr            'expected exactly one textarea'
21*39904235SAndreas Gohr        );
22*39904235SAndreas Gohr        return $m[1];
23*39904235SAndreas Gohr    }
24*39904235SAndreas Gohr
25*39904235SAndreas Gohr    public function testStartTagIsFollowedByGuardNewline()
26*39904235SAndreas Gohr    {
27*39904235SAndreas Gohr        $this->assertStringStartsWith("\n", $this->rawTextareaBody('hello'));
28*39904235SAndreas Gohr    }
29*39904235SAndreas Gohr
30*39904235SAndreas Gohr    public function testValueIsEmittedInFullAfterGuard()
31*39904235SAndreas Gohr    {
32*39904235SAndreas Gohr        // a value beginning with a newline must appear unaltered after the
33*39904235SAndreas Gohr        // guard: the start tag is followed by the guard newline and then the
34*39904235SAndreas Gohr        // form-encoded value (including its own leading newline)
35*39904235SAndreas Gohr        $value = "\n## heading\n";
36*39904235SAndreas Gohr        $this->assertSame("\n" . formText($value), $this->rawTextareaBody($value));
37*39904235SAndreas Gohr    }
38*39904235SAndreas Gohr}
39