|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for term_description(). |
| 5 | + * |
| 6 | + * @group taxonomy |
| 7 | + * |
| 8 | + * @covers ::term_description |
| 9 | + */ |
| 10 | +class Tests_Term_TermDescription extends WP_UnitTestCase { |
| 11 | + |
| 12 | + /** |
| 13 | + * Category object with a description. |
| 14 | + * |
| 15 | + * @var WP_Term |
| 16 | + */ |
| 17 | + private static $category; |
| 18 | + |
| 19 | + /** |
| 20 | + * Tag object with a description. |
| 21 | + * |
| 22 | + * @var WP_Term |
| 23 | + */ |
| 24 | + private static $tag; |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates shared category and tag fixtures. |
| 28 | + * |
| 29 | + * @param WP_UnitTest_Factory $factory Test factory. |
| 30 | + */ |
| 31 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 32 | + self::$category = $factory->category->create_and_get( |
| 33 | + array( |
| 34 | + 'name' => 'Described Category', |
| 35 | + 'slug' => 'described-category', |
| 36 | + 'description' => 'Category description text', |
| 37 | + ) |
| 38 | + ); |
| 39 | + |
| 40 | + self::$tag = $factory->tag->create_and_get( |
| 41 | + array( |
| 42 | + 'name' => 'Described Tag', |
| 43 | + 'slug' => 'described-tag', |
| 44 | + 'description' => 'Tag description text', |
| 45 | + ) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Tests that term_description() returns the formatted description for a given term ID. |
| 51 | + * |
| 52 | + * @ticket 65819 |
| 53 | + */ |
| 54 | + public function test_returns_description_for_term_id() { |
| 55 | + $this->assertSame( |
| 56 | + "<p>Category description text</p>\n", |
| 57 | + term_description( self::$category->term_id ) |
| 58 | + ); |
| 59 | + |
| 60 | + $this->assertSame( |
| 61 | + "<p>Tag description text</p>\n", |
| 62 | + term_description( self::$tag->term_id ) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tests that term_description() defaults to the queried object on category, tag, and custom taxonomy archives. |
| 68 | + * |
| 69 | + * @ticket 65819 |
| 70 | + */ |
| 71 | + public function test_defaults_to_queried_object_on_term_archives() { |
| 72 | + $this->go_to( get_category_link( self::$category->term_id ) ); |
| 73 | + $this->assertTrue( is_category() ); |
| 74 | + $this->assertSame( "<p>Category description text</p>\n", term_description() ); |
| 75 | + |
| 76 | + $this->go_to( get_tag_link( self::$tag->term_id ) ); |
| 77 | + $this->assertTrue( is_tag() ); |
| 78 | + $this->assertSame( "<p>Tag description text</p>\n", term_description() ); |
| 79 | + |
| 80 | + register_taxonomy( 'wptests_tax', 'post' ); |
| 81 | + $custom_term = self::factory()->term->create_and_get( |
| 82 | + array( |
| 83 | + 'taxonomy' => 'wptests_tax', |
| 84 | + 'description' => 'Custom taxonomy description text', |
| 85 | + ) |
| 86 | + ); |
| 87 | + |
| 88 | + $this->go_to( get_term_link( $custom_term, 'wptests_tax' ) ); |
| 89 | + $this->assertTrue( is_tax() ); |
| 90 | + $this->assertSame( "<p>Custom taxonomy description text</p>\n", term_description() ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Tests that term_description() returns an empty string when the term does not exist |
| 95 | + * or when no term is passed outside a term archive. |
| 96 | + * |
| 97 | + * @ticket 65819 |
| 98 | + */ |
| 99 | + public function test_returns_empty_string_for_invalid_term_or_non_term_archive() { |
| 100 | + $this->assertSame( '', term_description( 99999999 ) ); |
| 101 | + |
| 102 | + $this->go_to( home_url( '/' ) ); |
| 103 | + $this->assertSame( '', term_description() ); |
| 104 | + } |
| 105 | +} |
0 commit comments