Passed
Pull Request — v3 (#685)
by Timothy
16:12
created

SstiUnitTest::testSanitizeUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 46
rs 9.488
c 2
b 0
f 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2020 nystudio107
10
 */
11
12
namespace nystudio107\seomatictests\unit;
13
14
use nystudio107\seomatic\Seomatic;
15
use nystudio107\seomatic\helpers\DynamicMeta as DynamicMetaHelper;
16
use nystudio107\seomatic\helpers\Text as TextHelper;
17
18
use Craft;
19
20
use Codeception\Test\Unit;
21
use UnitTester;
22
23
/**
24
 * ExampleUnitTest
25
 *
26
 * @author    nystudio107
27
 * @package   Seomatic
28
 * @since     3.3.9
29
 */
30
class SstiUnitTest extends Unit
31
{
32
    // Properties
33
    // =========================================================================
34
35
    /**
36
     * @var UnitTester
37
     */
38
    protected $tester;
39
40
    // Public methods
41
    // =========================================================================
42
43
    // Tests
44
    // =========================================================================
45
46
    /**
47
     * Test the sanitizeUrl() method to ensure it sanitizes URLs properly
48
     */
49
    public function testSanitizeUrl()
50
    {
51
        // Strip Twig code
52
        $this->assertSame(
53
            '',
54
            DynamicMetaHelper::sanitizeUrl('{{ craft.app.config.general.actionTrigger }}')
55
        );
56
        // Strip object syntax Twig code
57
        $this->assertSame(
58
            '',
59
            DynamicMetaHelper::sanitizeUrl('{ craft.app.config.general.actionTrigger }')
60
        );
61
        // Strip URL-encoded Twig code
62
        $this->assertSame(
63
            '',
64
            DynamicMetaHelper::sanitizeUrl('%7B%7B%20craft.app.config.general.actionTrigger%20%7D%7D')
65
        );
66
        // Strip URL-encoded object syntax Twig code
67
        $this->assertSame(
68
            '',
69
            DynamicMetaHelper::sanitizeUrl('%7B%20craft.app.config.general.actionTrigger%20%7D')
70
        );
71
        // Strip HTML entity-encoded Twig code
72
        $this->assertSame(
73
            '',
74
            DynamicMetaHelper::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;')
75
        );
76
        // Strip HTML entity-encoded object syntax Twig code
77
        $this->assertSame(
78
            '',
79
            DynamicMetaHelper::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;')
80
        );
81
        // Strip query strings
82
        $this->assertSame(
83
            '/woof',
84
            DynamicMetaHelper::sanitizeUrl('/woof?q=bark')
85
        );
86
        // Strip HTML
87
        $this->assertSame(
88
            '/woof/hello',
89
            DynamicMetaHelper::sanitizeUrl('/woof/<blockquote>hello</blockquote>')
90
        );
91
        // Proper text is returned, without the Twig code
92
        $this->assertSame(
93
            'The sum is: ',
94
            TextHelper::sanitizeFieldData('The sum is: {{ 2 + 2 }}')
95
        );
96
    }
97
98
    /**
99
     * Test the testSanitizeFieldData() method to ensure it sanitizes pulled field data
100
     */
101
    public function testSanitizeFieldData()
102
    {
103
        // Strip Twig code
104
        $this->assertSame(
105
            '',
106
            TextHelper::sanitizeFieldData('{{ craft.app.config.general.actionTrigger }}')
107
        );
108
        // Strip object syntax Twig code
109
        $this->assertSame(
110
            '( craft.app.config.general.actionTrigger )',
111
            TextHelper::sanitizeFieldData('{ craft.app.config.general.actionTrigger }')
112
        );
113
        // Strip URL-encoded Twig code
114
        $this->assertSame(
115
            '',
116
            TextHelper::sanitizeFieldData('%7B%7B%20craft.app.config.general.actionTrigger%20%7D%7D')
117
        );
118
        // Strip URL-encoded object syntax Twig code
119
        $this->assertSame(
120
            '( craft.app.config.general.actionTrigger )',
121
            TextHelper::sanitizeFieldData('%7B%20craft.app.config.general.actionTrigger%20%7D')
122
        );
123
        // Strip HTML entity-encoded Twig code
124
        $this->assertSame(
125
            '',
126
            TextHelper::sanitizeFieldData('&#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;')
127
        );
128
        // Strip HTML entity-encoded object syntax Twig code
129
        $this->assertSame(
130
            '( craft.app.config.general.actionTrigger )',
131
            TextHelper::sanitizeFieldData('&#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;')
132
        );
133
        // Strip HTML
134
        $this->assertSame(
135
            'hello',
136
            DynamicMetaHelper::sanitizeUrl('<blockquote>hello</blockquote>')
137
        );
138
        // Proper text is returned, without the Twig code
139
        $this->assertSame(
140
            'The sum is: ',
141
            TextHelper::sanitizeFieldData('The sum is: {{ 2 + 2 }}')
142
        );
143
    }
144
}
145