<?php

	lt_include( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
	lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
	lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );

	/**
	 * \ingroup Test
	 *
	 * Test cases for the Textfilter class
	 */
	class Textfilter_Test extends LifeTypeTestCase
	{
		function setup()
		{
			$this->tf = new Textfilter();
		}
		
		/**
		 * Verifies the 'slugify' method
		 */
		function testSlugify()
		{
			// load the value of the default separator
			$config =& Config::getConfig();
            $sep = $config->getValue( "urlize_word_separator", URLIZE_WORD_SEPARATOR_DEFAULT );			
			
			$tests = Array(  // associative array where the key is the input and the value is the expected output
				"simple" => "simple",
				"two words" => "two{$sep}words",
				"two  spaces" => "two{$sep}spaces",
				"   leadingblanks" => "leadingblanks",
				"trailingblanks  " => "trailingblanks",
				"!@#extraseparators'''" => "extraseparators",
				"<a>html</a><b>is</b><h1>not</h1><p>allowed</p>"
                 => "htmlisnotallowed",
				"unclosed < html</a><b >shouldn't </b>be<h1> <p>stripped</p>"
                 => "unclosed{$sep}htmlshouldn{$sep}t{$sep}be{$sep}stripped",
                "SOME uppercase CHARAcTERS" => "some{$sep}uppercase{$sep}characters" );
				
			// process each one of them
			foreach( $tests as $input => $output ) {
				$result = $this->tf->slugify( $input );
				$this->assertEquals( $output, $result );
			}
		}
		
		/**
		 * Verifies the domainize() method
		 */
		function testDomainize()
		{
			// load the value of the default separator
			$config =& Config::getConfig();
            $sep = $config->getValue( "urlize_word_separator", URLIZE_WORD_SEPARATOR_DEFAULT );			
			
			// set of input values and their expected output
			$tests = Array(
				"TesT BlOg" => "test{$sep}blog",
				"test-blog" => "test{$sep}blog",
				"test_blog" => "test{$sep}blog",
				"test.blog" => "test.blog",
				"??test//blog" => "test{$sep}blog",
				"==================test blog" => "test{$sep}blog",
				"this.has.dots_and-hyphens----and   spaces		    " => "this.has.dots{$sep}and{$sep}hyphens{$sep}and{$sep}spaces"
			);
			
			foreach( $tests as $input => $output ) {
				$result = $this->tf->domainize( $input );
				$this->assertEquals( $output, $result, "input was: $input" );
			}
		}

		/**
		 * Verifies the urlize() method
		 */
		function testUrlize()
		{
			// load the value of the default separator
			$config =& Config::getConfig();
            $sep = $config->getValue( "urlize_word_separator", URLIZE_WORD_SEPARATOR_DEFAULT );			
			
			// set of input values and their expected output
			$tests = Array(
				"teSt blog" => "test{$sep}blog",
				"test-blog" => "test-blog",
				"test_blog" => "test_blog",
				"test.blog" => "test.blog",
				"??test//blog" => "test{$sep}blog",
				"==================test blog" => "test{$sep}blog",
				"this.has.dots_and-hyphens----and   spaces		    " => "this.has.dots_and-hyphens{$sep}and{$sep}spaces",
				"multiple__underscores______" => "multiple__underscores______"
			);
			
			foreach( $tests as $input => $output ) {
				$result = $this->tf->urlize( $input );
				$this->assertEquals( $output, $result, "input was: $input" );
			}
		}

        
		/**
		 * tests the htmlDecode() method
		 */
		function testHtmlDecode()
		{
			// array with strings and the expected result, the key is the
			// input and the value is the expected output, add more if needed
			$tests = Array(
				"&amp;" => "&",
				"test" => "test",
				"&aacute;&eacute;" => "αι",
				"&auml;&Uuml;" => "δά"
			);
			
			foreach( $tests as $input => $output ) {
				// check that the input is equal to the output after processing it with TextFilter::htmlDecode
				$this->assertEquals( $output, TextFilter::htmlDecode( $input ), "Error htmlDecode()-ing string: $input" );
				// and that htmlDecode and filterHTMLEntities are really the opposite of each other
				$this->assertEquals( $output, Textfilter::htmlDecode( TextFilter::filterHTMLEntities( $output )));
			}
		}
	}
?>