summaryrefslogtreecommitdiff
path: root/tests/FunctionsTest.php
blob: 163e985d0b3fe69cb1df3ab4a40c1375c83ccb9a (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
require_once dirname(__FILE__) . '/../functions.php';
/**
 * Unit tests for functions.php
 *
 * @author Christian Weiske <[email protected]>
 */
class FunctionsTest extends PHPUnit_Framework_TestCase
{
    protected $tmpFile = null;
    public function __construct()
    {
        $this->tmpFile = sys_get_temp_dir() . '/tt-rss-unittest.dat';
    }

    public function tearDown()
    {
        if (file_exists($this->tmpFile)) {
            unlink($this->tmpFile);
        }
    }

    /**
     * Test fix_url with feed:// urls
     */
    public function testFixUrlFeed()
    {
        $this->assertEquals('http://tt-rss.org/', fix_url('feed://tt-rss.org'));
        $this->assertEquals('http://tt-rss.org/', fix_url('feed://tt-rss.org/'));
    }

    /**
     * Test fix_url with non-http protocols
     */
    public function testFixUrlProtocols()
    {
        $this->assertEquals('https://tt-rss.org/', fix_url('https://tt-rss.org'));
        $this->assertEquals('ftp://tt-rss.org/', fix_url('ftp://tt-rss.org/'));
        $this->assertEquals(
            'reallylongprotocolisthat://tt-rss.org/', 
            fix_url('reallylongprotocolisthat://tt-rss.org')
        );
    }

    /**
     * Test fix_url with domain names only
     */
    public function testFixUrlDomainOnly()
    {
        $this->assertEquals('http://tt-rss.org/', fix_url('tt-rss.org'));
        $this->assertEquals('http://tt-rss.org/', fix_url('tt-rss.org/'));
        $this->assertEquals('http://tt-rss.org/', fix_url('http://tt-rss.org'));
        $this->assertEquals('http://tt-rss.org/', fix_url('http://tt-rss.org/'));
    }

    /**
     * Test fix_url with domain + paths
     */
    public function testFixUrlWithPaths()
    {
        $this->assertEquals('http://tt-rss.org/foo', fix_url('tt-rss.org/foo'));

        $this->assertEquals(
            'http://tt-rss.org/foo/bar/baz',
            fix_url('tt-rss.org/foo/bar/baz')
        );
        $this->assertEquals(
            'http://tt-rss.org/foo/bar/baz/',
            fix_url('tt-rss.org/foo/bar/baz/')
        );
    }


    /**
     * Test url_is_html() on html with a doctype
     */
    public function testUrlIsHtmlNormalHtmlWithDoctype()
    {
        file_put_contents(
            $this->tmpFile, <<<HTM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
HTM
        );
        $this->assertTrue(url_is_html($this->tmpFile));

        file_put_contents(
            $this->tmpFile, <<<HTM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
HTM
        );
        $this->assertTrue(url_is_html($this->tmpFile));
    }

    /**
     * Test url_is_html() on html with a doctype and xml header
     */
    public function testUrlIsHtmlNormalHtmlWithDoctypeAndXml()
    {
        file_put_contents(
            $this->tmpFile, <<<HTM
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
HTM
        );
        $this->assertTrue(url_is_html($this->tmpFile));
    }

    /**
     * Test url_is_html() on html without a doctype
     */
    public function testUrlIsHtmlNormalHtmlWithoutDoctype()
    {
        file_put_contents(
            $this->tmpFile, <<<HTM
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
HTM
        );
        $this->assertTrue(url_is_html($this->tmpFile));
    }

    /**
     * Test url_is_html() on UPPERCASE HTML
     */
    public function testUrlIsHtmlNormalHtmlUppercase()
    {
        file_put_contents(
            $this->tmpFile, <<<HTM
<HTML XMLNS="http://www.w3.org/1999/xhtml" XML:LANG="en">
<HEAD>
HTM
        );
        $this->assertTrue(url_is_html($this->tmpFile));

        file_put_contents(
            $this->tmpFile, <<<HTM
<HTML>
<HEAD>
HTM
        );
        $this->assertTrue(url_is_html($this->tmpFile));
    }

    /**
     * Test url_is_html() on atom
     */
    public function testUrlIsHtmlAtom()
    {
        file_put_contents(
            $this->tmpFile, <<<HTM
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 <title>Christians Tagebuch</title>
HTM
        );
        $this->assertFalse(url_is_html($this->tmpFile));
    }

    /**
     * Test url_is_html() on RSS
     */
    public function testUrlIsHtmlRss()
    {
        file_put_contents(
            $this->tmpFile, <<<HTM
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title><![CDATA[Planet-PEAR]]></title>
HTM
        );
        $this->assertFalse(url_is_html($this->tmpFile));
    }
}

?>