From 8d40956f7a11753e647b13a4704a821e44d6ca91 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 6 Jun 2016 21:34:22 +0000 Subject: [PATCH] Tests: Add tests demonstrating `wp_set_object_terms()` behavior when matching `$terms`. See #37009. git-svn-id: https://develop.svn.wordpress.org/trunk@37647 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/term/wpSetObjectTerms.php | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/tests/phpunit/tests/term/wpSetObjectTerms.php b/tests/phpunit/tests/term/wpSetObjectTerms.php index a86459142a..2dde5fd72f 100644 --- a/tests/phpunit/tests/term/wpSetObjectTerms.php +++ b/tests/phpunit/tests/term/wpSetObjectTerms.php @@ -309,4 +309,82 @@ class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { $this->assertEquals( $tt_1[1], $tt_2[0] ); } + + public function test_should_create_term_that_does_not_exist() { + register_taxonomy( 'wptests_tax', 'post' ); + + $this->assertFalse( get_term_by( 'slug', 'foo', 'wptests_tax' ) ); + + $tt_ids = wp_set_object_terms( self::$post_ids[0], 'foo', 'wptests_tax' ); + + $this->assertNotEmpty( $tt_ids ); + $term = get_term( $tt_ids[0] ); + $this->assertInstanceOf( 'WP_Term', $term ); + $this->assertSame( 'foo', $term->slug ); + } + + public function test_should_find_existing_term_by_slug_match() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t = self::factory()->term->create( array( + 'taxonomy' => 'wptests_tax', + 'slug' => 'foo', + 'name' => 'Bar', + ) ); + + $tt_ids = wp_set_object_terms( self::$post_ids[0], 'foo', 'wptests_tax' ); + + $this->assertNotEmpty( $tt_ids ); + $term = get_term( $tt_ids[0] ); + $this->assertInstanceOf( 'WP_Term', $term ); + $this->assertSame( $t, $term->term_id ); + } + + public function test_should_find_existing_term_by_name_match() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t = self::factory()->term->create( array( + 'taxonomy' => 'wptests_tax', + 'slug' => 'foo', + 'name' => 'Bar', + ) ); + + $tt_ids = wp_set_object_terms( self::$post_ids[0], 'Bar', 'wptests_tax' ); + + $this->assertNotEmpty( $tt_ids ); + $term = get_term( $tt_ids[0] ); + $this->assertInstanceOf( 'WP_Term', $term ); + $this->assertSame( $t, $term->term_id ); + } + + public function test_should_give_precedence_to_slug_match_over_name_match() { + register_taxonomy( 'wptests_tax', 'post' ); + + $t1 = self::factory()->term->create( array( + 'taxonomy' => 'wptests_tax', + 'slug' => 'foo', + 'name' => 'Bar', + ) ); + + $t2 = self::factory()->term->create( array( + 'taxonomy' => 'wptests_tax', + 'slug' => 'bar', + 'name' => 'Foo', + ) ); + + $tt_ids = wp_set_object_terms( self::$post_ids[0], 'Bar', 'wptests_tax' ); + + $this->assertNotEmpty( $tt_ids ); + $term = get_term( $tt_ids[0] ); + $this->assertInstanceOf( 'WP_Term', $term ); + $this->assertSame( $t2, $term->term_id ); + } + + public function test_non_existent_integers_should_be_ignored() { + register_taxonomy( 'wptests_tax', 'post' ); + + $tt_ids = wp_set_object_terms( self::$post_ids[0], 12345, 'wptests_tax' ); + + $this->assertSame( array(), $tt_ids ); + } }