RetourUnitTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 34
dl 0
loc 71
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A testSanitizeUrl() 0 52 1
1
<?php
2
/**
3
 * Retour plugin for Craft CMS 3.x
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
9
 * @copyright Copyright (c) 2018 nystudio107
10
 */
11
12
namespace nystudio107\retourtests\unit;
13
14
use nystudio107\retour\Retour;
15
use nystudio107\retour\helpers\UrlHelper;
16
17
use Craft;
18
19
use Codeception\Test\Unit;
20
use UnitTester;
21
22
/**
23
 * ExampleUnitTest
24
 *
25
 * @author    nystudio107
26
 * @package   Retour
27
 * @since     3.1.40
28
 */
29
class RetourUnitTest extends Unit
30
{
31
    // Properties
32
    // =========================================================================
33
34
    /**
35
     * @var UnitTester
36
     */
37
    protected $tester;
38
39
    // Public methods
40
    // =========================================================================
41
42
    // Tests
43
    // =========================================================================
44
45
    /**
46
     * Test the sanitizeUrl() method to ensure it sanitizes URLs properly
47
     */
48
    public function testSanitizeUrl()
49
    {
50
        // Strip line feeds
51
        $this->assertSame(
52
            'One fish, Two fish, Red fish, Blue fish.',
53
            UrlHelper::sanitizeUrl(implode(
54
                "",
55
                [
56
                    "One fish, ",
57
                    PHP_EOL,
58
                    "Two fish, ",
59
                    "\r",
60
                    "Red fish, ",
61
                    "\n",
62
                    "Blue fish.",
63
                ]
64
            ))
65
        );
66
        // Strip Twig code
67
        $this->assertSame(
68
            '',
69
            UrlHelper::sanitizeUrl('{{ craft.app.config.general.actionTrigger }}')
70
        );
71
        // Strip object syntax Twig code
72
        $this->assertSame(
73
            '',
74
            UrlHelper::sanitizeUrl('{ craft.app.config.general.actionTrigger }')
75
        );
76
        // Strip URL-encoded Twig code
77
        $this->assertSame(
78
            '',
79
            UrlHelper::sanitizeUrl('%7B%7B%20craft.app.config.general.actionTrigger%20%7D%7D')
80
        );
81
        // Strip URL-encoded object syntax Twig code
82
        $this->assertSame(
83
            '',
84
            UrlHelper::sanitizeUrl('%7B%20craft.app.config.general.actionTrigger%20%7D')
85
        );
86
        // Strip HTML entity-encoded Twig code
87
        $this->assertSame(
88
            '',
89
            UrlHelper::sanitizeUrl('&#x7B;&#x7B;&#x20;&#x63;&#x72;&#x61;&#x66;&#x74;&#x2E;&#x61;&#x70;&#x70;&#x2E;&#x63;&#x6F;&#x6E;&#x66;&#x69;&#x67;&#x2E;&#x67;&#x65;&#x6E;&#x65;&#x72;&#x61;&#x6C;&#x2E;&#x61;&#x63;&#x74;&#x69;&#x6F;&#x6E;&#x54;&#x72;&#x69;&#x67;&#x67;&#x65;&#x72;&#x20;&#x7D;&#x7D;')
90
        );
91
        // Strip HTML entity-encoded object syntax Twig code
92
        $this->assertSame(
93
            '',
94
            UrlHelper::sanitizeUrl('&#x7B;&#x20;&#x63;&#x72;&#x61;&#x66;&#x74;&#x2E;&#x61;&#x70;&#x70;&#x2E;&#x63;&#x6F;&#x6E;&#x66;&#x69;&#x67;&#x2E;&#x67;&#x65;&#x6E;&#x65;&#x72;&#x61;&#x6C;&#x2E;&#x61;&#x63;&#x74;&#x69;&#x6F;&#x6E;&#x54;&#x72;&#x69;&#x67;&#x67;&#x65;&#x72;&#x20;&#x7D;')
95
        );
96
        // Strip HTML
97
        $this->assertSame(
98
            '/woof/hello',
99
            UrlHelper::sanitizeUrl('/woof/<blockquote>hello</blockquote>')
100
        );
101
    }
102
}
103