]> git.wh0rd.org - tt-rss.git/blame - tests/FunctionsTest.php
add unit tests for rewrite_relative_url and fix a number of bugs in it
[tt-rss.git] / tests / FunctionsTest.php
CommitLineData
44453773
CW
1<?php
2require_once dirname(__FILE__) . '/../functions.php';
3/**
4 * Unit tests for functions.php
5 *
6 * @author Christian Weiske <cweiske@php.net>
7 */
8class FunctionsTest extends PHPUnit_Framework_TestCase
9{
24eb4c78
CW
10 protected $tmpFile = null;
11 public function __construct()
12 {
13 $this->tmpFile = sys_get_temp_dir() . '/tt-rss-unittest.dat';
14 }
15
16 public function tearDown()
17 {
18 if (file_exists($this->tmpFile)) {
19 unlink($this->tmpFile);
20 }
21 }
22
44453773
CW
23 /**
24 * Test fix_url with feed:// urls
25 */
26 public function testFixUrlFeed()
27 {
28 $this->assertEquals('http://tt-rss.org/', fix_url('feed://tt-rss.org'));
29 $this->assertEquals('http://tt-rss.org/', fix_url('feed://tt-rss.org/'));
30 }
31
32 /**
33 * Test fix_url with non-http protocols
34 */
35 public function testFixUrlProtocols()
36 {
37 $this->assertEquals('https://tt-rss.org/', fix_url('https://tt-rss.org'));
38 $this->assertEquals('ftp://tt-rss.org/', fix_url('ftp://tt-rss.org/'));
39 $this->assertEquals(
40 'reallylongprotocolisthat://tt-rss.org/',
41 fix_url('reallylongprotocolisthat://tt-rss.org')
42 );
43 }
44
45 /**
46 * Test fix_url with domain names only
47 */
48 public function testFixUrlDomainOnly()
49 {
50 $this->assertEquals('http://tt-rss.org/', fix_url('tt-rss.org'));
51 $this->assertEquals('http://tt-rss.org/', fix_url('tt-rss.org/'));
52 $this->assertEquals('http://tt-rss.org/', fix_url('http://tt-rss.org'));
53 $this->assertEquals('http://tt-rss.org/', fix_url('http://tt-rss.org/'));
54 }
55
56 /**
57 * Test fix_url with domain + paths
58 */
59 public function testFixUrlWithPaths()
60 {
61 $this->assertEquals('http://tt-rss.org/foo', fix_url('tt-rss.org/foo'));
62
63 $this->assertEquals(
64 'http://tt-rss.org/foo/bar/baz',
65 fix_url('tt-rss.org/foo/bar/baz')
66 );
67 $this->assertEquals(
68 'http://tt-rss.org/foo/bar/baz/',
69 fix_url('tt-rss.org/foo/bar/baz/')
70 );
71 }
24eb4c78
CW
72
73
74 /**
75 * Test url_is_html() on html with a doctype
76 */
77 public function testUrlIsHtmlNormalHtmlWithDoctype()
78 {
79 file_put_contents(
80 $this->tmpFile, <<<HTM
81<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
82<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
83<head>
84HTM
85 );
86 $this->assertTrue(url_is_html($this->tmpFile));
87
88 file_put_contents(
89 $this->tmpFile, <<<HTM
90<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
91<html>
92<head>
93HTM
94 );
95 $this->assertTrue(url_is_html($this->tmpFile));
96 }
97
98 /**
99 * Test url_is_html() on html with a doctype and xml header
100 */
101 public function testUrlIsHtmlNormalHtmlWithDoctypeAndXml()
102 {
103 file_put_contents(
104 $this->tmpFile, <<<HTM
105<?xml version="1.0" encoding="utf-8"?>
106<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
107<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
108<head>
109HTM
110 );
111 $this->assertTrue(url_is_html($this->tmpFile));
112 }
113
114 /**
115 * Test url_is_html() on html without a doctype
116 */
117 public function testUrlIsHtmlNormalHtmlWithoutDoctype()
118 {
119 file_put_contents(
120 $this->tmpFile, <<<HTM
121<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
122<head>
123HTM
124 );
125 $this->assertTrue(url_is_html($this->tmpFile));
126 }
127
128 /**
129 * Test url_is_html() on UPPERCASE HTML
130 */
131 public function testUrlIsHtmlNormalHtmlUppercase()
132 {
133 file_put_contents(
134 $this->tmpFile, <<<HTM
135<HTML XMLNS="http://www.w3.org/1999/xhtml" XML:LANG="en">
136<HEAD>
137HTM
138 );
139 $this->assertTrue(url_is_html($this->tmpFile));
140
141 file_put_contents(
142 $this->tmpFile, <<<HTM
143<HTML>
144<HEAD>
145HTM
146 );
147 $this->assertTrue(url_is_html($this->tmpFile));
148 }
149
150 /**
151 * Test url_is_html() on atom
152 */
153 public function testUrlIsHtmlAtom()
154 {
155 file_put_contents(
156 $this->tmpFile, <<<HTM
157<?xml version="1.0" encoding="utf-8"?>
158<feed xmlns="http://www.w3.org/2005/Atom">
159 <title>Christians Tagebuch</title>
160HTM
161 );
162 $this->assertFalse(url_is_html($this->tmpFile));
163 }
164
165 /**
166 * Test url_is_html() on RSS
167 */
168 public function testUrlIsHtmlRss()
169 {
170 file_put_contents(
171 $this->tmpFile, <<<HTM
172<?xml version="1.0" encoding="UTF-8"?>
173<?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">
174 <channel>
175 <title><![CDATA[Planet-PEAR]]></title>
176HTM
177 );
178 $this->assertFalse(url_is_html($this->tmpFile));
179 }
f679105c
CW
180
181
182
183 /**
184 * Test rewrite_relative_url() with a relative path
185 */
186 public function testRewriteRelativeUrlRelative()
187 {
188 $this->assertEquals(
189 'http://tt-rss.org/foo/bar',
190 rewrite_relative_url('http://tt-rss.org', 'foo/bar')
191 );
192 $this->assertEquals(
193 'http://tt-rss.org/foo/bar',
194 rewrite_relative_url('http://tt-rss.org/', 'foo/bar')
195 );
196 $this->assertEquals(
197 'http://tt-rss.org/bar',
198 rewrite_relative_url('http://tt-rss.org/foo', 'bar')
199 );
200 $this->assertEquals(
201 'http://tt-rss.org/foo/bar',
202 rewrite_relative_url('http://tt-rss.org/foo/', 'bar')
203 );
204 $this->assertEquals(
205 'http://tt-rss.org/f/o/bar',
206 rewrite_relative_url('http://tt-rss.org/f/o/o', 'bar')
207 );
208 $this->assertEquals(
209 'http://tt-rss.org/f/o/o/bar',
210 rewrite_relative_url('http://tt-rss.org/f/o/o/', 'bar')
211 );
212 }
213
214 /**
215 * Test rewrite_relative_url() with an absolute path
216 */
217 public function testRewriteRelativeUrlAbsolutePath()
218 {
219 $this->assertEquals(
220 'http://tt-rss.org/bar/',
221 rewrite_relative_url('http://tt-rss.org/foo/', '/bar/')
222 );
223 $this->assertEquals(
224 'http://tt-rss.org/bar/',
225 rewrite_relative_url('http://tt-rss.org/so/what/is/next', '/bar/')
226 );
227 $this->assertEquals(
228 'http://tt-rss.org/bar/',
229 rewrite_relative_url('http://tt-rss.org/so/what/is/next/', '/bar/')
230 );
231 }
232
233 /**
234 * Test rewrite_relative_url() with an absolute URL
235 */
236 public function testRewriteRelativeUrlAbsoluteUrl()
237 {
238 $this->assertEquals(
239 'http://example.org/bar/',
240 rewrite_relative_url('http://tt-rss.org/foo/', 'http://example.org/bar/')
241 );
242 }
44453773
CW
243}
244
245?>