aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/http/UrlUtils/CleanupUrlTest.php
blob: 45690ecfb196e46b2aa90b110d8877d63611fb7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
 * Unitary tests for cleanup_url()
 */

namespace Shaarli\Http;

require_once 'application/http/UrlUtils.php';

class CleanupUrlTest extends \Shaarli\TestCase
{
    /**
     * @var string reference URL
     */
    protected $ref = 'http://domain.tld:3000';


    /**
     * Clean empty URL
     */
    public function testCleanupUrlEmpty()
    {
        $this->assertEquals('', cleanup_url(''));
    }

    /**
     * Clean an already cleaned URL
     */
    public function testCleanupUrlAlreadyClean()
    {
        $this->assertEquals($this->ref, cleanup_url($this->ref));
        $this->ref2 = $this->ref.'/path/to/dir/';
        $this->assertEquals($this->ref2, cleanup_url($this->ref2));
    }

    /**
     * Clean URL fragments
     */
    public function testCleanupUrlFragment()
    {
        $this->assertEquals($this->ref, cleanup_url($this->ref.'#tk.rss_all'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-U3ht0tkc4b'));
    }

    /**
     * Clean URL query - single annoying parameter
     */
    public function testCleanupUrlQuerySingle()
    {
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_object_map=junk'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_ref_map=Cr4p!'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_type_map=g4R84g3'));

        $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb_stuff=v41u3'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb=71m3w4573'));

        $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_campaign=zomg'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_medium=numnum'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_source=c0d3'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_term=1n4l'));

        $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url'));

        $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_name=junk'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_start=junk'));
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_item_index=junk'));
    }

    /**
     * Clean URL query - multiple annoying parameters
     */
    public function testCleanupUrlQueryMultiple()
    {
        $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url&fb=som3th1ng'));

        $this->assertEquals($this->ref, cleanup_url(
            $this->ref.'?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
        ));

        $this->assertEquals($this->ref, cleanup_url(
            $this->ref.'?campaign_start=zomg&campaign_name=numnum'
        ));
    }

    /**
     * Clean URL query - multiple annoying parameters and fragment
     */
    public function testCleanupUrlQueryFragment()
    {
        $this->assertEquals($this->ref, cleanup_url(
            $this->ref.'?xtor=some-url&fb=som3th1ng#tk.rss_all'
        ));

        // ditch annoying query params and fragment, keep useful params
        $this->assertEquals(
            $this->ref.'?my=stuff&is=kept',
            cleanup_url(
                $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
            )
        );

        // ditch annoying query params, keep useful params and fragment
        $this->assertEquals(
            $this->ref.'?my=stuff&is=kept#again',
            cleanup_url(
                $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
            )
        );
    }
}