<?php
/**
 * Test Walker_Category Class
 *
 * @internal Dual license under BSD and GPLv2, pick one.
 *
 * @license BSD
 * @license GPL version 2
 * @author Jacob Santos
 */

class WPTestWalkerCategory extends WPTestCase {

	function test_start_lvl_should_just_return() {
		$obj = new Walker_Category();

		$expected = '';

		$actual = $obj->start_lvl($expected, 0, array('style' => ''));

		$this->assertEquals( $expected, $actual );
	}

	function test_start_lvl_should_return_two_tabs() {
		$obj = new Walker_Category();

		$expected = "testing\n\t\t<ul class='children'>\n";
		$output = "testing\n";

		$actual = $obj->start_lvl( $output, 2, array('style' => 'list') );
		$this->assertEquals( $expected, $actual );
	}

	function test_end_lvl_should_just_return() {
		$obj = new Walker_Category();

		$expected = '';

		$actual = $obj->end_lvl($expected, 0, array('style' => ''));

		$this->assertEquals( $expected, $actual );
	}

	function test_end_lvl_should_return_two_tabs() {
		$obj = new Walker_Category();

		$expected = "testing\n\t\t</ul>\n";
		$output = "testing\n";

		$actual = $obj->end_lvl( $output, 2, array('style' => 'list') );
		$this->assertEquals( $expected, $actual );
	}

	function test_end_el_should_just_return() {
		$obj = new Walker_Category();

		$expected = '';

		$actual = $obj->end_el($expected, null, 0, array('style' => ''));

		$this->assertEquals( $expected, $actual );
	}

	function test_end_el_does_output_not_prepend_two_tabs() {
		$obj = new Walker_Category();

		$expected = "testing\n\t\t</li>\n";
		$output = "testing\n";

		$actual = $obj->end_el( $output, null, 2, array('style' => 'list') );
		$this->assertNotEquals( $expected, $actual, "This test is supposed to fail when fix has been applied. Change to assertEquals when failure." );
	}

	function test_end_el_does_not_prepend_two_tabs() {
		$obj = new Walker_Category();

		$expected = "testing\n</li>\n";
		$output = "testing\n";

		$actual = $obj->end_el( $output, null, 2, array('style' => 'list') );
		$this->assertEquals( $expected, $actual, "This test is supposed to fail when fix has been applied. Change to assertNotEquals when failure.");
	}

	// Covers Defect #4596
	function test_start_lvl_should_use_description_as_title__control_test() {
		global $wp_filters;

		// Remove the hook to test just the single item.
		$list_cats_hook = isset($wp_filters['list_cats']) ? $wp_filters['list_cats'] : null;
		$category_description_hook = isset($wp_filters['category_description']) ? $wp_filters['category_description'] : null;
		unset($wp_filters['list_cats'], $wp_filters['category_description']);

		add_filter('category_link', array(__CLASS__, '_clear_category_link'), 0);

		$obj = new Walker_Category();
		$category = (object) array( 'name' => 'test1', 'term_id' => 0, 'description' => 'testing just a test' );
		$expected = "\t".'<a href="" title="testing just a test">test1</a>'."<br />\n";
		$actual = $obj->start_el( '', $category, 0, array('use_desc_for_title' => 1) );

		remove_filter('category_link', array(__CLASS__, '_clear_category_link'), 0);

		// Restore broken list cats, in case another test makes use of it.
		if( !is_null( $list_cats_hook ) )
			$wp_filters['list_cats'] = $list_cats_hook;

		if( !is_null( $category_description_hook ) )
			$wp_filters['category_description'] = $category_description_hook;

		$this->assertEquals( $expected, $actual, "$actual" );
	}

	// Covers Defect #4596
	function test_start_lvl_should_not_use_description_as_title__lacking_description_and_control_test() {
		global $wp_filters;

		// Remove the hook to test just the single item.
		$list_cats_hook = isset($wp_filters['list_cats']) ? $wp_filters['list_cats'] : null;
		$category_description_hook = isset($wp_filters['category_description']) ? $wp_filters['category_description'] : null;
		unset($wp_filters['list_cats'], $wp_filters['category_description']);

		add_filter('category_link', array(__CLASS__, '_clear_category_link'), 0);

		$obj = new Walker_Category();
		$category = (object) array( 'name' => 'test1', 'term_id' => 0, 'description' => '' );
		$expected = "\t".'<a href="" title="View all posts filed under test1">test1</a>'."<br />\n";
		$actual = $obj->start_el( '', $category, 0, array('use_desc_for_title' => 1) );

		remove_filter('category_link', array(__CLASS__, '_clear_category_link'), 0);

		// Restore broken list cats, in case another test makes use of it.
		if( !is_null( $list_cats_hook ) )
			$wp_filters['list_cats'] = $list_cats_hook;

		if( !is_null( $category_description_hook ) )
			$wp_filters['category_description'] = $category_description_hook;

		$this->assertEquals( $expected, $actual, "$actual" );
	}

	// Covers Defect #4596
	function test_start_lvl_should_use_description_as_title() {
		global $wp_filters;

		// Remove the hook to test just the single item.
		$list_cats_hook = isset($wp_filters['list_cats']) ? $wp_filters['list_cats'] : null;
		$category_description_hook = isset($wp_filters['category_description']) ? $wp_filters['category_description'] : null;
		unset($wp_filters['list_cats'], $wp_filters['category_description']);

		add_filter('category_link', array(__CLASS__, '_clear_category_link'), 0);

		$obj = new Walker_Category();
		$category = (object) array( 'name' => 'test1', 'term_id' => 0, 'description' => 'asdf' );
		$expected = "\t".'<a href="" title="asdf">test1</a>'."<br />\n";
		$actual = $obj->start_el( '', $category, 0, array('use_desc_for_title' => true) );

		remove_filter('category_link', array(__CLASS__, '_clear_category_link'), 0);

		// Restore broken list cats, in case another test makes use of it.
		if( !is_null( $list_cats_hook ) )
			$wp_filters['list_cats'] = $list_cats_hook;

		if( !is_null( $category_description_hook ) )
			$wp_filters['category_description'] = $category_description_hook;

		$this->assertEquals( $expected, $actual, "$actual" );
	}

	// Covers Defect #4596
	function test_start_lvl_should_not_use_description_as_title__lacking_description() {
		global $wp_filters;

		// Remove the hook to test just the single item.
		$list_cats_hook = isset($wp_filters['list_cats']) ? $wp_filters['list_cats'] : null;
		$category_description_hook = isset($wp_filters['category_description']) ? $wp_filters['category_description'] : null;
		unset($wp_filters['list_cats'], $wp_filters['category_description']);

		add_filter('category_link', array(__CLASS__, '_clear_category_link'), 0);

		$obj = new Walker_Category();
		$category = (object) array( 'name' => 'test1', 'term_id' => 0, 'description' => '' );
		$expected = "\t".'<a href="" title="View all posts filed under test1">test1</a>'."<br />\n";
		$actual = $obj->start_el( '', $category, 0, array('use_desc_for_title' => true) );

		remove_filter('category_link', array(__CLASS__, '_clear_category_link'), 0);

		// Restore broken list cats, in case another test makes use of it.
		if( !is_null( $list_cats_hook ) )
			$wp_filters['list_cats'] = $list_cats_hook;

		if( !is_null( $category_description_hook ) )
			$wp_filters['category_description'] = $category_description_hook;

		$this->assertEquals( $expected, $actual, "$actual" );
	}
	
	function _clear_category_link($link, $id=0) {
		return '';
	}
}

?>