/tests/phpunit/tests/term/wpSetObjectTerms.php

https://gitlab.com/morganestes/wordpress-develop · PHP · 423 lines · 289 code · 86 blank · 48 comment · 3 complexity · 46750f15753ad9b8e47138d01fa3e2a6 MD5 · raw file

  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase {
  6. protected $taxonomy = 'category';
  7. protected static $post_ids = array();
  8. public static function wpSetUpBeforeClass( $factory ) {
  9. self::$post_ids = $factory->post->create_many( 5 );
  10. }
  11. /**
  12. * @ticket 26570
  13. */
  14. function test_set_object_terms() {
  15. $non_hier = rand_str( 10 );
  16. $hier = rand_str( 10 );
  17. // Register taxonomies
  18. register_taxonomy( $non_hier, array() );
  19. register_taxonomy( $hier, array( 'hierarchical' => true ) );
  20. // Create a post.
  21. $post_id = self::$post_ids[0];
  22. /*
  23. * Set a single term (non-hierarchical) by ID.
  24. */
  25. $tag = wp_insert_term( 'Foo', $non_hier );
  26. $this->assertFalse( has_term( $tag['term_id'], $non_hier, $post_id ) );
  27. wp_set_object_terms( $post_id, $tag['term_id'], $non_hier );
  28. $this->assertTrue( has_term( $tag['term_id'], $non_hier, $post_id ) );
  29. /*
  30. * Set a single term (non-hierarchical) by slug.
  31. */
  32. $tag = wp_insert_term( 'Bar', $non_hier );
  33. $tag = get_term( $tag['term_id'], $non_hier );
  34. $this->assertFalse( has_term( $tag->slug, $non_hier, $post_id ) );
  35. wp_set_object_terms( $post_id, $tag->slug, $non_hier );
  36. $this->assertTrue( has_term( $tag->slug, $non_hier, $post_id ) );
  37. /*
  38. * Set a single term (hierarchical) by ID.
  39. */
  40. $cat = wp_insert_term( 'Baz', $hier );
  41. $this->assertFalse( has_term( $cat['term_id'], $hier, $post_id ) );
  42. wp_set_object_terms( $post_id, $cat['term_id'], $hier );
  43. $this->assertTrue( has_term( $cat['term_id'], $hier, $post_id ) );
  44. /*
  45. * Set a single term (hierarchical) by slug.
  46. */
  47. $cat = wp_insert_term( 'Qux', $hier );
  48. $cat = get_term( $cat['term_id'], $hier );
  49. $this->assertFalse( has_term( $cat->slug, $hier, $post_id ) );
  50. wp_set_object_terms( $post_id, $cat->slug, $hier );
  51. $this->assertTrue( has_term( $cat->slug, $hier, $post_id ) );
  52. /*
  53. * Set an array of term IDs (non-hierarchical) by ID.
  54. */
  55. $tag1 = wp_insert_term( '_tag1', $non_hier );
  56. $this->assertFalse( has_term( $tag1['term_id'], $non_hier, $post_id ) );
  57. $tag2 = wp_insert_term( '_tag2', $non_hier );
  58. $this->assertFalse( has_term( $tag2['term_id'], $non_hier, $post_id ) );
  59. $tag3 = wp_insert_term( '_tag3', $non_hier );
  60. $this->assertFalse( has_term( $tag3['term_id'], $non_hier, $post_id ) );
  61. wp_set_object_terms( $post_id, array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier );
  62. $this->assertTrue( has_term( array( $tag1['term_id'], $tag2['term_id'], $tag3['term_id'] ), $non_hier, $post_id ) );
  63. /*
  64. * Set an array of term slugs (hierarchical) by slug.
  65. */
  66. $cat1 = wp_insert_term( '_cat1', $hier );
  67. $cat1 = get_term( $cat1['term_id'], $hier );
  68. $this->assertFalse( has_term( $cat1->slug, $hier, $post_id ) );
  69. $cat2 = wp_insert_term( '_cat2', $hier );
  70. $cat2 = get_term( $cat2['term_id'], $hier );
  71. $this->assertFalse( has_term( $cat2->slug, $hier, $post_id ) );
  72. $cat3 = wp_insert_term( '_cat3', $hier );
  73. $cat3 = get_term( $cat3['term_id'], $hier );
  74. $this->assertFalse( has_term( $cat3->slug, $hier, $post_id ) );
  75. wp_set_object_terms( $post_id, array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier );
  76. $this->assertTrue( has_term( array( $cat1->slug, $cat2->slug, $cat3->slug ), $hier, $post_id ) );
  77. }
  78. function test_set_object_terms_by_id() {
  79. $ids = self::$post_ids;
  80. $terms = array();
  81. for ( $i = 0; $i < 3; $i++ ) {
  82. $term = "term_{$i}";
  83. $result = wp_insert_term( $term, $this->taxonomy );
  84. $this->assertInternalType( 'array', $result );
  85. $term_id[ $term ] = $result['term_id'];
  86. }
  87. foreach ( $ids as $id ) {
  88. $tt = wp_set_object_terms( $id, array_values( $term_id ), $this->taxonomy );
  89. // should return three term taxonomy ids
  90. $this->assertEquals( 3, count( $tt ) );
  91. }
  92. // each term should be associated with every post
  93. foreach ( $term_id as $term => $id ) {
  94. $actual = get_objects_in_term( $id, $this->taxonomy );
  95. $this->assertEquals( $ids, array_map( 'intval', $actual ) );
  96. }
  97. // each term should have a count of 5
  98. foreach ( array_keys( $term_id ) as $term ) {
  99. $t = get_term_by( 'name', $term, $this->taxonomy );
  100. $this->assertEquals( 5, $t->count );
  101. }
  102. }
  103. function test_set_object_terms_by_name() {
  104. $ids = self::$post_ids;
  105. $terms = array(
  106. rand_str(),
  107. rand_str(),
  108. rand_str(),
  109. );
  110. foreach ( $ids as $id ) {
  111. $tt = wp_set_object_terms( $id, $terms, $this->taxonomy );
  112. // should return three term taxonomy ids
  113. $this->assertEquals( 3, count( $tt ) );
  114. // remember which term has which term_id
  115. for ( $i = 0; $i < 3; $i++ ) {
  116. $term = get_term_by( 'name', $terms[ $i ], $this->taxonomy );
  117. $term_id[ $terms[ $i ] ] = intval( $term->term_id );
  118. }
  119. }
  120. // each term should be associated with every post
  121. foreach ( $term_id as $term => $id ) {
  122. $actual = get_objects_in_term( $id, $this->taxonomy );
  123. $this->assertEquals( $ids, array_map( 'intval', $actual ) );
  124. }
  125. // each term should have a count of 5
  126. foreach ( $terms as $term ) {
  127. $t = get_term_by( 'name', $term, $this->taxonomy );
  128. $this->assertEquals( 5, $t->count );
  129. }
  130. }
  131. function test_set_object_terms_invalid() {
  132. // bogus taxonomy
  133. $result = wp_set_object_terms( self::$post_ids[0], array( rand_str() ), rand_str() );
  134. $this->assertWPError( $result );
  135. }
  136. public function test_wp_set_object_terms_append_true() {
  137. register_taxonomy( 'wptests_tax', 'post' );
  138. $p = self::$post_ids[0];
  139. $t1 = self::factory()->term->create(
  140. array(
  141. 'taxonomy' => 'wptests_tax',
  142. )
  143. );
  144. $t2 = self::factory()->term->create(
  145. array(
  146. 'taxonomy' => 'wptests_tax',
  147. )
  148. );
  149. $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
  150. $this->assertNotEmpty( $added1 );
  151. $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
  152. $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', true );
  153. $this->assertNotEmpty( $added2 );
  154. $this->assertEqualSets( array( $t1, $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
  155. _unregister_taxonomy( 'wptests_tax' );
  156. }
  157. public function test_wp_set_object_terms_append_false() {
  158. register_taxonomy( 'wptests_tax', 'post' );
  159. $p = self::$post_ids[0];
  160. $t1 = self::factory()->term->create(
  161. array(
  162. 'taxonomy' => 'wptests_tax',
  163. )
  164. );
  165. $t2 = self::factory()->term->create(
  166. array(
  167. 'taxonomy' => 'wptests_tax',
  168. )
  169. );
  170. $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
  171. $this->assertNotEmpty( $added1 );
  172. $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
  173. $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax', false );
  174. $this->assertNotEmpty( $added2 );
  175. $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
  176. _unregister_taxonomy( 'wptests_tax' );
  177. }
  178. public function test_wp_set_object_terms_append_default_to_false() {
  179. register_taxonomy( 'wptests_tax', 'post' );
  180. $p = self::$post_ids[0];
  181. $t1 = self::factory()->term->create(
  182. array(
  183. 'taxonomy' => 'wptests_tax',
  184. )
  185. );
  186. $t2 = self::factory()->term->create(
  187. array(
  188. 'taxonomy' => 'wptests_tax',
  189. )
  190. );
  191. $added1 = wp_set_object_terms( $p, array( $t1 ), 'wptests_tax' );
  192. $this->assertNotEmpty( $added1 );
  193. $this->assertEqualSets( array( $t1 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
  194. $added2 = wp_set_object_terms( $p, array( $t2 ), 'wptests_tax' );
  195. $this->assertNotEmpty( $added2 );
  196. $this->assertEqualSets( array( $t2 ), wp_get_object_terms( $p, 'wptests_tax', array( 'fields' => 'ids' ) ) );
  197. _unregister_taxonomy( 'wptests_tax' );
  198. }
  199. function test_change_object_terms_by_id() {
  200. // set some terms on an object; then change them while leaving one intact
  201. $post_id = self::$post_ids[0];
  202. // first set: 3 terms
  203. $terms_1 = array();
  204. for ( $i = 0; $i < 3; $i++ ) {
  205. $term = "term_{$i}";
  206. $result = wp_insert_term( $term, $this->taxonomy );
  207. $this->assertInternalType( 'array', $result );
  208. $terms_1[ $i ] = $result['term_id'];
  209. }
  210. // second set: one of the original terms, plus one new term
  211. $terms_2 = array();
  212. $terms_2[0] = $terms_1[1];
  213. $term = 'term';
  214. $result = wp_insert_term( $term, $this->taxonomy );
  215. $terms_2[1] = $result['term_id'];
  216. // set the initial terms
  217. $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  218. $this->assertEquals( 3, count( $tt_1 ) );
  219. // make sure they're correct
  220. $terms = wp_get_object_terms(
  221. $post_id, $this->taxonomy, array(
  222. 'fields' => 'ids',
  223. 'orderby' => 'term_id',
  224. )
  225. );
  226. $this->assertEquals( $terms_1, $terms );
  227. // change the terms
  228. $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
  229. $this->assertEquals( 2, count( $tt_2 ) );
  230. // make sure they're correct
  231. $terms = wp_get_object_terms(
  232. $post_id, $this->taxonomy, array(
  233. 'fields' => 'ids',
  234. 'orderby' => 'term_id',
  235. )
  236. );
  237. $this->assertEquals( $terms_2, $terms );
  238. // make sure the tt id for 'bar' matches
  239. $this->assertEquals( $tt_1[1], $tt_2[0] );
  240. }
  241. function test_change_object_terms_by_name() {
  242. // set some terms on an object; then change them while leaving one intact
  243. $post_id = self::$post_ids[0];
  244. $terms_1 = array( 'foo', 'bar', 'baz' );
  245. $terms_2 = array( 'bar', 'bing' );
  246. // set the initial terms
  247. $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  248. $this->assertEquals( 3, count( $tt_1 ) );
  249. // make sure they're correct
  250. $terms = wp_get_object_terms(
  251. $post_id, $this->taxonomy, array(
  252. 'fields' => 'names',
  253. 'orderby' => 'term_id',
  254. )
  255. );
  256. $this->assertEquals( $terms_1, $terms );
  257. // change the terms
  258. $tt_2 = wp_set_object_terms( $post_id, $terms_2, $this->taxonomy );
  259. $this->assertEquals( 2, count( $tt_2 ) );
  260. // make sure they're correct
  261. $terms = wp_get_object_terms(
  262. $post_id, $this->taxonomy, array(
  263. 'fields' => 'names',
  264. 'orderby' => 'term_id',
  265. )
  266. );
  267. $this->assertEquals( $terms_2, $terms );
  268. // make sure the tt id for 'bar' matches
  269. $this->assertEquals( $tt_1[1], $tt_2[0] );
  270. }
  271. public function test_should_create_term_that_does_not_exist() {
  272. register_taxonomy( 'wptests_tax', 'post' );
  273. $this->assertFalse( get_term_by( 'slug', 'foo', 'wptests_tax' ) );
  274. $tt_ids = wp_set_object_terms( self::$post_ids[0], 'foo', 'wptests_tax' );
  275. $this->assertNotEmpty( $tt_ids );
  276. $term = get_term_by( 'term_taxonomy_id', $tt_ids[0] );
  277. $this->assertInstanceOf( 'WP_Term', $term );
  278. $this->assertSame( 'foo', $term->slug );
  279. }
  280. public function test_should_find_existing_term_by_slug_match() {
  281. register_taxonomy( 'wptests_tax', 'post' );
  282. $t = self::factory()->term->create(
  283. array(
  284. 'taxonomy' => 'wptests_tax',
  285. 'slug' => 'foo',
  286. 'name' => 'Bar',
  287. )
  288. );
  289. $tt_ids = wp_set_object_terms( self::$post_ids[0], 'foo', 'wptests_tax' );
  290. $this->assertNotEmpty( $tt_ids );
  291. $term = get_term_by( 'term_taxonomy_id', $tt_ids[0] );
  292. $this->assertInstanceOf( 'WP_Term', $term );
  293. $this->assertSame( $t, $term->term_id );
  294. }
  295. public function test_should_find_existing_term_by_name_match() {
  296. register_taxonomy( 'wptests_tax', 'post' );
  297. $t = self::factory()->term->create(
  298. array(
  299. 'taxonomy' => 'wptests_tax',
  300. 'slug' => 'foo',
  301. 'name' => 'Bar',
  302. )
  303. );
  304. $tt_ids = wp_set_object_terms( self::$post_ids[0], 'Bar', 'wptests_tax' );
  305. $this->assertNotEmpty( $tt_ids );
  306. $term = get_term_by( 'term_taxonomy_id', $tt_ids[0] );
  307. $this->assertInstanceOf( 'WP_Term', $term );
  308. $this->assertSame( $t, $term->term_id );
  309. }
  310. public function test_should_give_precedence_to_slug_match_over_name_match() {
  311. register_taxonomy( 'wptests_tax', 'post' );
  312. $t1 = self::factory()->term->create(
  313. array(
  314. 'taxonomy' => 'wptests_tax',
  315. 'slug' => 'foo',
  316. 'name' => 'Bar',
  317. )
  318. );
  319. $t2 = self::factory()->term->create(
  320. array(
  321. 'taxonomy' => 'wptests_tax',
  322. 'slug' => 'bar',
  323. 'name' => 'Foo',
  324. )
  325. );
  326. $tt_ids = wp_set_object_terms( self::$post_ids[0], 'Bar', 'wptests_tax' );
  327. $this->assertNotEmpty( $tt_ids );
  328. $term = get_term_by( 'term_taxonomy_id', $tt_ids[0] );
  329. $this->assertInstanceOf( 'WP_Term', $term );
  330. $this->assertSame( $t2, $term->term_id );
  331. }
  332. public function test_non_existent_integers_should_be_ignored() {
  333. register_taxonomy( 'wptests_tax', 'post' );
  334. $tt_ids = wp_set_object_terms( self::$post_ids[0], 12345, 'wptests_tax' );
  335. $this->assertSame( array(), $tt_ids );
  336. }
  337. }