xref: /webtrees/tests/app/Elements/AbstractElementTestCase.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
1*b66f5d1aSGreg Roach<?php
2*b66f5d1aSGreg Roach
3*b66f5d1aSGreg Roach/**
4*b66f5d1aSGreg Roach * webtrees: online genealogy
5*b66f5d1aSGreg Roach * Copyright (C) 2023 webtrees development team
6*b66f5d1aSGreg Roach * This program is free software: you can redistribute it and/or modify
7*b66f5d1aSGreg Roach * it under the terms of the GNU General Public License as published by
8*b66f5d1aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*b66f5d1aSGreg Roach * (at your option) any later version.
10*b66f5d1aSGreg Roach * This program is distributed in the hope that it will be useful,
11*b66f5d1aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*b66f5d1aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*b66f5d1aSGreg Roach * GNU General Public License for more details.
14*b66f5d1aSGreg Roach * You should have received a copy of the GNU General Public License
15*b66f5d1aSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*b66f5d1aSGreg Roach */
17*b66f5d1aSGreg Roach
18*b66f5d1aSGreg Roachdeclare(strict_types=1);
19*b66f5d1aSGreg Roach
20*b66f5d1aSGreg Roachnamespace Fisharebest\Webtrees\Elements;
21*b66f5d1aSGreg Roach
22*b66f5d1aSGreg Roachuse Fisharebest\Webtrees\Contracts\ElementInterface;
23*b66f5d1aSGreg Roachuse Fisharebest\Webtrees\TestCase;
24*b66f5d1aSGreg Roachuse Fisharebest\Webtrees\Tree;
25*b66f5d1aSGreg Roach
26*b66f5d1aSGreg Roachabstract class AbstractElementTestCase extends TestCase
27*b66f5d1aSGreg Roach{
28*b66f5d1aSGreg Roach    private const EVIL_VALUE = '<script>evil()</script>';
29*b66f5d1aSGreg Roach    private const TEST_VALUE = '01 JAN 1970';
30*b66f5d1aSGreg Roach
31*b66f5d1aSGreg Roach    protected static ElementInterface $element;
32*b66f5d1aSGreg Roach
33*b66f5d1aSGreg Roach    public function testCanonical(): void
34*b66f5d1aSGreg Roach    {
35*b66f5d1aSGreg Roach        self::assertSame('Foo bAr baZ', self::$element->canonical('Foo  bAr  baZ'));
36*b66f5d1aSGreg Roach        self::assertSame('Foo bAr baZ', self::$element->canonical("\t Foo\t bAr \tbaZ\t "));
37*b66f5d1aSGreg Roach        self::assertSame('Foo bAr baZ', self::$element->canonical("\nFoo \n\r bAr \r\n baZ\r"));
38*b66f5d1aSGreg Roach    }
39*b66f5d1aSGreg Roach
40*b66f5d1aSGreg Roach    public function testEscapeAtSigns(): void
41*b66f5d1aSGreg Roach    {
42*b66f5d1aSGreg Roach        if (static::$element instanceof AbstractXrefElement) {
43*b66f5d1aSGreg Roach            self::assertSame('@X123@', static::$element->escape('@X123@'));
44*b66f5d1aSGreg Roach        } else {
45*b66f5d1aSGreg Roach            self::assertSame('@@X123@@', static::$element->escape('@X123@'));
46*b66f5d1aSGreg Roach        }
47*b66f5d1aSGreg Roach    }
48*b66f5d1aSGreg Roach
49*b66f5d1aSGreg Roach    public function testXssInValue(): void
50*b66f5d1aSGreg Roach    {
51*b66f5d1aSGreg Roach        $tree    = $this->createMock(Tree::class);
52*b66f5d1aSGreg Roach        $html    = static::$element->value(self::EVIL_VALUE, $tree);
53*b66f5d1aSGreg Roach        $message = 'XSS vulnerability in value()';
54*b66f5d1aSGreg Roach
55*b66f5d1aSGreg Roach        self::assertStringNotContainsStringIgnoringCase(self::EVIL_VALUE, $html, $message);
56*b66f5d1aSGreg Roach    }
57*b66f5d1aSGreg Roach
58*b66f5d1aSGreg Roach    public function testXssInLabelValue(): void
59*b66f5d1aSGreg Roach    {
60*b66f5d1aSGreg Roach        $tree    = $this->createMock(Tree::class);
61*b66f5d1aSGreg Roach        $html    = static::$element->labelValue(self::EVIL_VALUE, $tree);
62*b66f5d1aSGreg Roach        $message = 'XSS vulnerability in labelValue()';
63*b66f5d1aSGreg Roach
64*b66f5d1aSGreg Roach        self::assertStringNotContainsStringIgnoringCase(self::EVIL_VALUE, $html, $message);
65*b66f5d1aSGreg Roach    }
66*b66f5d1aSGreg Roach
67*b66f5d1aSGreg Roach    public function testXssInEdit(): void
68*b66f5d1aSGreg Roach    {
69*b66f5d1aSGreg Roach        $tree    = $this->createMock(Tree::class);
70*b66f5d1aSGreg Roach        $html    = static::$element->edit('id', 'name', self::EVIL_VALUE, $tree);
71*b66f5d1aSGreg Roach        $message = 'XSS vulnerability in edit()';
72*b66f5d1aSGreg Roach
73*b66f5d1aSGreg Roach        self::assertStringNotContainsStringIgnoringCase(self::EVIL_VALUE, $html, $message);
74*b66f5d1aSGreg Roach    }
75*b66f5d1aSGreg Roach
76*b66f5d1aSGreg Roach    public function testValidHtmlInValue(): void
77*b66f5d1aSGreg Roach    {
78*b66f5d1aSGreg Roach        $tree = $this->createMock(Tree::class);
79*b66f5d1aSGreg Roach        $html = static::$element->value(self::TEST_VALUE, $tree);
80*b66f5d1aSGreg Roach
81*b66f5d1aSGreg Roach        $this->validateHtml($html);
82*b66f5d1aSGreg Roach    }
83*b66f5d1aSGreg Roach
84*b66f5d1aSGreg Roach    public function testValidHtmlInEdit(): void
85*b66f5d1aSGreg Roach    {
86*b66f5d1aSGreg Roach        $tree = $this->createMock(Tree::class);
87*b66f5d1aSGreg Roach        $html = static::$element->edit('id', 'name', self::TEST_VALUE, $tree);
88*b66f5d1aSGreg Roach
89*b66f5d1aSGreg Roach        $this->validateHtml($html);
90*b66f5d1aSGreg Roach    }
91*b66f5d1aSGreg Roach}
92