public function categories() { if (!$this->categories) { $this->categories = $this->post->categories(); } return $this->categories; }
function testPostCategories() { $cats = array('News', 'Sports', 'Obits'); foreach ($cats as &$cat) { $cat = wp_insert_term($cat, 'category'); } $pid = $this->factory->post->create(); foreach ($cats as $cat) { wp_set_object_terms($pid, $cat['term_id'], 'category', true); } $post = new TimberPost($pid); $this->assertEquals(3, count($post->categories())); }
function testPostTerms() { $pid = $this->factory->post->create(); $post = new TimberPost($pid); // create a new tag and associate it with the post $dummy_tag = wp_insert_term('whatever', 'post_tag'); wp_set_object_terms($pid, $dummy_tag['term_id'], 'post_tag', true); // test expected tags $timber_tags = $post->terms('post_tag'); $dummy_timber_tag = new TimberTerm($dummy_tag['term_id'], 'post_tag'); $this->assertEquals('whatever', $timber_tags[0]->slug); $this->assertEquals($dummy_timber_tag, $timber_tags[0]); // register a custom taxonomy, create some terms in it and associate to post register_taxonomy('team', 'post'); $team_names = array('Patriots', 'Bills', 'Dolphins', 'Jets'); foreach ($team_names as $team_name) { $team_term = wp_insert_term($team_name, 'team'); wp_set_object_terms($pid, $team_term['term_id'], 'team', true); } $this->assertEquals(count($team_names), count($post->terms('team'))); // check presence of specific terms $this->assertTrue($post->has_term('Uncategorized')); $this->assertTrue($post->has_term('whatever')); $this->assertTrue($post->has_term('Dolphins')); $this->assertTrue($post->has_term('Patriots', 'team')); // 4 teams + 1 tag + default category (Uncategorized) $this->assertEquals(6, count($post->terms())); // test tags method - wrapper for $this->get_terms('tags') $this->assertEquals($post->tags(), $post->terms('tag')); $this->assertEquals($post->tags(), $post->terms('tags')); $this->assertEquals($post->tags(), $post->terms('post_tag')); // test categories method - wrapper for $this->get_terms('category') $this->assertEquals($post->categories(), $post->terms('category')); $this->assertEquals($post->categories(), $post->terms('categories')); // test using an array of taxonomies $post_tag_terms = $post->terms(array('post_tag')); $this->assertEquals(1, count($post_tag_terms)); $post_team_terms = $post->terms(array('team')); $this->assertEquals(count($team_names), count($post_team_terms)); // test multiple taxonomies $post_tag_and_team_terms = $post->terms(array('post_tag', 'team')); $this->assertEquals(count($post_tag_terms) + count($post_team_terms), count($post_tag_and_team_terms)); }