]> git.wh0rd.org Git - tt-rss.git/commitdiff
add unit tests for url_is_html and fix a bug with UPPERCASE <HTML> in it
authorChristian Weiske <cweiske@cweiske.de>
Wed, 10 Nov 2010 21:48:16 +0000 (22:48 +0100)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Thu, 11 Nov 2010 06:43:18 +0000 (09:43 +0300)
functions.php
tests/FunctionsTest.php

index e73c0d16e277efaf29788ac4e0ef7914e93bafee..96fa19224375c5c68339d4b1ece8d0051436a06e 100644 (file)
         */
        function url_is_html($url) {
                $content = substr(fetch_file_contents($url, false), 0, 1000);
-               if (strpos($content, '<html>') === false
-                       && strpos($content, '<html ') === false
+               if (stripos($content, '<html>') === false
+                       && stripos($content, '<html ') === false
                ) {
                        return false;
                }
index 61264ac50e53e297514b98f8a130558b69ecb787..163e985d0b3fe69cb1df3ab4a40c1375c83ccb9a 100644 (file)
@@ -7,6 +7,19 @@ require_once dirname(__FILE__) . '/../functions.php';
  */
 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
      */
@@ -56,6 +69,114 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
             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));
+    }
 }
 
 ?>
\ No newline at end of file