FunctionsTest.php 6.9 KB

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