wpInsertTerm.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. <?php
  2. /**
  3. * @group taxonomy
  4. */
  5. class Tests_Term_WpInsertTerm extends WP_UnitTestCase {
  6. public function setUp() {
  7. parent::setUp();
  8. _clean_term_filters();
  9. // Insert one term into every post taxonomy.
  10. // Otherwise term_ids and term_taxonomy_ids might be identical, which could mask bugs.
  11. $term = 'seed_term';
  12. foreach ( get_object_taxonomies( 'post' ) as $tax ) {
  13. wp_insert_term( $term, $tax );
  14. }
  15. }
  16. public function test_wp_insert_delete_term() {
  17. $taxonomy = 'wptests_tax';
  18. register_taxonomy( $taxonomy, 'post' );
  19. // A new unused term.
  20. $term = 'term';
  21. $this->assertNull( term_exists( $term ) );
  22. $initial_count = wp_count_terms( array( 'taxonomy' => $taxonomy ) );
  23. $t = wp_insert_term( $term, $taxonomy );
  24. $this->assertInternalType( 'array', $t );
  25. $this->assertNotWPError( $t );
  26. $this->assertTrue( $t['term_id'] > 0 );
  27. $this->assertTrue( $t['term_taxonomy_id'] > 0 );
  28. $this->assertEquals( $initial_count + 1, wp_count_terms( array( 'taxonomy' => $taxonomy ) ) );
  29. // Make sure the term exists.
  30. $this->assertTrue( term_exists( $term ) > 0 );
  31. $this->assertTrue( term_exists( $t['term_id'] ) > 0 );
  32. // Now delete it.
  33. add_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 );
  34. $this->assertTrue( wp_delete_term( $t['term_id'], $taxonomy ) );
  35. remove_filter( 'delete_term', array( $this, 'deleted_term_cb' ), 10, 5 );
  36. $this->assertNull( term_exists( $term ) );
  37. $this->assertNull( term_exists( $t['term_id'] ) );
  38. $this->assertSame( $initial_count, wp_count_terms( array( 'taxonomy' => $taxonomy ) ) );
  39. }
  40. public function test_wp_insert_term_taxonomy_does_not_exist() {
  41. $found = wp_insert_term( 'foo', 'bar' );
  42. $this->assertWPError( $found );
  43. $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
  44. }
  45. public function test_wp_insert_term_pre_insert_term_filter_returns_wp_error() {
  46. add_filter( 'pre_insert_term', array( $this, '_pre_insert_term_callback' ) );
  47. $found = wp_insert_term( 'foo', 'post_tag' );
  48. remove_filter( 'pre_insert_term', array( $this, '_pre_insert_term_callback' ) );
  49. $this->assertWPError( $found );
  50. $this->assertSame( 'custom_error', $found->get_error_code() );
  51. }
  52. public function test_wp_insert_term_term_0() {
  53. $found = wp_insert_term( 0, 'post_tag' );
  54. $this->assertWPError( $found );
  55. $this->assertSame( 'invalid_term_id', $found->get_error_code() );
  56. }
  57. public function test_wp_insert_term_term_trims_to_empty_string() {
  58. $found = wp_insert_term( ' ', 'post_tag' );
  59. $this->assertWPError( $found );
  60. $this->assertSame( 'empty_term_name', $found->get_error_code() );
  61. }
  62. public function test_wp_insert_term_parent_does_not_exist() {
  63. $found = wp_insert_term(
  64. 'foo',
  65. 'post_tag',
  66. array(
  67. 'parent' => 999999,
  68. )
  69. );
  70. $this->assertWPError( $found );
  71. $this->assertSame( 'missing_parent', $found->get_error_code() );
  72. }
  73. public function test_wp_insert_term_unslash_name() {
  74. register_taxonomy( 'wptests_tax', 'post' );
  75. $found = wp_insert_term( 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy', 'wptests_tax' );
  76. $term = get_term( $found['term_id'], 'wptests_tax' );
  77. _unregister_taxonomy( 'wptests_tax' );
  78. $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->name );
  79. }
  80. public function test_wp_insert_term_unslash_description() {
  81. register_taxonomy( 'wptests_tax', 'post' );
  82. $found = wp_insert_term(
  83. 'Quality',
  84. 'wptests_tax',
  85. array(
  86. 'description' => 'Let\\\'s all say \\"Hooray\\" for WordPress taxonomy',
  87. )
  88. );
  89. $term = get_term( $found['term_id'], 'wptests_tax' );
  90. _unregister_taxonomy( 'wptests_tax' );
  91. $this->assertSame( 'Let\'s all say "Hooray" for WordPress taxonomy', $term->description );
  92. }
  93. public function test_wp_insert_term_parent_string() {
  94. register_taxonomy( 'wptests_tax', 'post' );
  95. $found = wp_insert_term(
  96. 'Quality',
  97. 'wptests_tax',
  98. array(
  99. 'parent' => 'foo1',
  100. )
  101. );
  102. $term = get_term( $found['term_id'], 'wptests_tax' );
  103. _unregister_taxonomy( 'wptests_tax' );
  104. // 'foo1' is cast to 0 in sanitize_term().
  105. $this->assertSame( 0, $term->parent );
  106. }
  107. public function test_wp_insert_term_slug_empty_string() {
  108. register_taxonomy( 'wptests_tax', 'post' );
  109. $found = wp_insert_term(
  110. 'Quality',
  111. 'wptests_tax',
  112. array(
  113. 'slug' => '',
  114. )
  115. );
  116. $term = get_term( $found['term_id'], 'wptests_tax' );
  117. _unregister_taxonomy( 'wptests_tax' );
  118. $this->assertSame( 'quality', $term->slug );
  119. }
  120. public function test_wp_insert_term_slug_whitespace_string() {
  121. register_taxonomy( 'wptests_tax', 'post' );
  122. $found = wp_insert_term(
  123. 'Quality',
  124. 'wptests_tax',
  125. array(
  126. 'slug' => ' ',
  127. )
  128. );
  129. $term = get_term( $found['term_id'], 'wptests_tax' );
  130. _unregister_taxonomy( 'wptests_tax' );
  131. $this->assertSame( 'quality', $term->slug );
  132. }
  133. public function test_wp_insert_term_slug_0() {
  134. register_taxonomy( 'wptests_tax', 'post' );
  135. $found = wp_insert_term(
  136. 'Quality',
  137. 'wptests_tax',
  138. array(
  139. 'slug' => 0,
  140. )
  141. );
  142. $term = get_term( $found['term_id'], 'wptests_tax' );
  143. _unregister_taxonomy( 'wptests_tax' );
  144. $this->assertSame( 'quality', $term->slug );
  145. }
  146. /**
  147. * @ticket 17689
  148. */
  149. public function test_wp_insert_term_duplicate_name() {
  150. $term = self::factory()->tag->create_and_get( array( 'name' => 'Bozo' ) );
  151. $this->assertNotWPError( $term );
  152. $this->assertTrue( empty( $term->errors ) );
  153. // Test existing term name with unique slug.
  154. $term1 = self::factory()->tag->create(
  155. array(
  156. 'name' => 'Bozo',
  157. 'slug' => 'bozo1',
  158. )
  159. );
  160. $this->assertNotWPError( $term1 );
  161. // Test an existing term name.
  162. $term2 = self::factory()->tag->create( array( 'name' => 'Bozo' ) );
  163. $this->assertWPError( $term2 );
  164. $this->assertNotEmpty( $term2->errors );
  165. // Test named terms ending in special characters.
  166. $term3 = self::factory()->tag->create( array( 'name' => 'T$' ) );
  167. $term4 = self::factory()->tag->create( array( 'name' => 'T$$' ) );
  168. $term5 = self::factory()->tag->create( array( 'name' => 'T$$$' ) );
  169. $term6 = self::factory()->tag->create( array( 'name' => 'T$$$$' ) );
  170. $term7 = self::factory()->tag->create( array( 'name' => 'T$$$$' ) );
  171. $this->assertWPError( $term7 );
  172. $this->assertNotEmpty( $term7->errors );
  173. $this->assertSame( $term6, $term7->error_data['term_exists'] );
  174. $terms = array_map( 'get_tag', array( $term3, $term4, $term5, $term6 ) );
  175. $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) );
  176. // Test named terms with only special characters.
  177. $term8 = self::factory()->tag->create( array( 'name' => '$' ) );
  178. $term9 = self::factory()->tag->create( array( 'name' => '$$' ) );
  179. $term10 = self::factory()->tag->create( array( 'name' => '$$$' ) );
  180. $term11 = self::factory()->tag->create( array( 'name' => '$$$$' ) );
  181. $term12 = self::factory()->tag->create( array( 'name' => '$$$$' ) );
  182. $this->assertWPError( $term12 );
  183. $this->assertNotEmpty( $term12->errors );
  184. $this->assertSame( $term11, $term12->error_data['term_exists'] );
  185. $terms = array_map( 'get_tag', array( $term8, $term9, $term10, $term11 ) );
  186. $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) );
  187. $term13 = self::factory()->tag->create( array( 'name' => 'A' ) );
  188. $this->assertNotWPError( $term13 );
  189. $term14 = self::factory()->tag->create( array( 'name' => 'A' ) );
  190. $this->assertWPError( $term14 );
  191. $term15 = self::factory()->tag->create(
  192. array(
  193. 'name' => 'A+',
  194. 'slug' => 'a',
  195. )
  196. );
  197. $this->assertNotWPError( $term15 );
  198. $term16 = self::factory()->tag->create( array( 'name' => 'A+' ) );
  199. $this->assertWPError( $term16 );
  200. $term17 = self::factory()->tag->create( array( 'name' => 'A++' ) );
  201. $this->assertNotWPError( $term17 );
  202. $term18 = self::factory()->tag->create(
  203. array(
  204. 'name' => 'A-',
  205. 'slug' => 'a',
  206. )
  207. );
  208. $this->assertNotWPError( $term18 );
  209. $term19 = self::factory()->tag->create( array( 'name' => 'A-' ) );
  210. $this->assertWPError( $term19 );
  211. $term20 = self::factory()->tag->create( array( 'name' => 'A--' ) );
  212. $this->assertNotWPError( $term20 );
  213. }
  214. /**
  215. * @ticket 31328
  216. */
  217. public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_the_same_term_in_non_hierarchical_taxonomy() {
  218. register_taxonomy( 'wptests_tax', 'post' );
  219. $t1 = self::factory()->term->create(
  220. array(
  221. 'name' => 'Foo',
  222. 'slug' => 'foo',
  223. 'taxonomy' => 'wptests_tax',
  224. )
  225. );
  226. $t2 = wp_insert_term(
  227. 'Foo',
  228. 'wptests_tax',
  229. array(
  230. 'slug' => 'foo',
  231. )
  232. );
  233. $this->assertWPError( $t2 );
  234. $this->assertSame( 'term_exists', $t2->get_error_code() );
  235. }
  236. /**
  237. * @ticket 31328
  238. */
  239. public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_a_different_term_in_non_hierarchical_taxonomy() {
  240. register_taxonomy( 'wptests_tax', 'post' );
  241. $t1 = self::factory()->term->create(
  242. array(
  243. 'name' => 'Foo',
  244. 'slug' => 'foo',
  245. 'taxonomy' => 'wptests_tax',
  246. )
  247. );
  248. $t2 = self::factory()->term->create(
  249. array(
  250. 'name' => 'Bar',
  251. 'slug' => 'bar',
  252. 'taxonomy' => 'wptests_tax',
  253. )
  254. );
  255. $t3 = wp_insert_term(
  256. 'Foo',
  257. 'wptests_tax',
  258. array(
  259. 'slug' => 'bar',
  260. )
  261. );
  262. $this->assertWPError( $t3 );
  263. $this->assertSame( 'term_exists', $t3->get_error_code() );
  264. }
  265. /**
  266. * @ticket 31328
  267. */
  268. public function test_wp_insert_term_should_allow_duplicate_names_when_a_unique_slug_has_been_provided_in_non_hierarchical_taxonomy() {
  269. register_taxonomy( 'wptests_tax', 'post' );
  270. $t1 = self::factory()->term->create(
  271. array(
  272. 'name' => 'Foo',
  273. 'slug' => 'foo',
  274. 'taxonomy' => 'wptests_tax',
  275. )
  276. );
  277. $t2 = wp_insert_term(
  278. 'Foo',
  279. 'wptests_tax',
  280. array(
  281. 'slug' => 'foo-unique',
  282. )
  283. );
  284. $this->assertNotWPError( $t2 );
  285. $t2_term = get_term( $t2['term_id'], 'wptests_tax' );
  286. $this->assertSame( 'foo-unique', $t2_term->slug );
  287. $this->assertSame( 'Foo', $t2_term->name );
  288. }
  289. /**
  290. * @ticket 31328
  291. */
  292. public function test_wp_insert_term_should_not_allow_duplicate_names_when_the_slug_is_not_provided_in_non_hierarchical_taxonomy() {
  293. register_taxonomy( 'wptests_tax', 'post' );
  294. $t1 = self::factory()->term->create(
  295. array(
  296. 'name' => 'Foo',
  297. 'slug' => 'foo',
  298. 'taxonomy' => 'wptests_tax',
  299. )
  300. );
  301. $t2 = wp_insert_term( 'Foo', 'wptests_tax' );
  302. $this->assertWPError( $t2 );
  303. $this->assertSame( 'term_exists', $t2->get_error_code() );
  304. }
  305. /**
  306. * @ticket 31328
  307. */
  308. public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_the_same_term_in_hierarchical_taxonomy() {
  309. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  310. $t1 = self::factory()->term->create(
  311. array(
  312. 'name' => 'Foo',
  313. 'slug' => 'foo',
  314. 'taxonomy' => 'wptests_tax',
  315. )
  316. );
  317. $t2 = wp_insert_term(
  318. 'Foo',
  319. 'wptests_tax',
  320. array(
  321. 'slug' => 'foo',
  322. )
  323. );
  324. $this->assertWPError( $t2 );
  325. $this->assertSame( 'term_exists', $t2->get_error_code() );
  326. }
  327. /**
  328. * @ticket 31328
  329. */
  330. public function test_wp_insert_term_should_not_allow_duplicate_names_when_slug_is_a_duplicate_of_a_different_term_at_same_hierarchy_level_in_hierarchical_taxonomy() {
  331. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  332. $t1 = self::factory()->term->create(
  333. array(
  334. 'name' => 'Foo',
  335. 'slug' => 'foo',
  336. 'taxonomy' => 'wptests_tax',
  337. )
  338. );
  339. $t2 = self::factory()->term->create(
  340. array(
  341. 'name' => 'Bar',
  342. 'slug' => 'bar',
  343. 'taxonomy' => 'wptests_tax',
  344. )
  345. );
  346. $t3 = wp_insert_term(
  347. 'Foo',
  348. 'wptests_tax',
  349. array(
  350. 'slug' => 'bar',
  351. )
  352. );
  353. $this->assertWPError( $t3 );
  354. $this->assertSame( 'term_exists', $t3->get_error_code() );
  355. }
  356. /**
  357. * @ticket 31328
  358. */
  359. public function test_wp_insert_term_should_allow_duplicate_names_when_slug_is_a_duplicate_of_a_term_at_different_hierarchy_level_in_hierarchical_taxonomy() {
  360. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  361. $t1 = self::factory()->term->create(
  362. array(
  363. 'name' => 'Foo',
  364. 'slug' => 'foo',
  365. 'taxonomy' => 'wptests_tax',
  366. )
  367. );
  368. $t2 = self::factory()->term->create();
  369. $t3 = self::factory()->term->create(
  370. array(
  371. 'name' => 'Bar',
  372. 'slug' => 'bar',
  373. 'parent' => $t2,
  374. 'taxonomy' => 'wptests_tax',
  375. )
  376. );
  377. $t4 = wp_insert_term(
  378. 'Foo',
  379. 'wptests_tax',
  380. array(
  381. 'slug' => 'bar',
  382. )
  383. );
  384. $this->assertNotWPError( $t4 );
  385. $t4_term = get_term( $t4['term_id'], 'wptests_tax' );
  386. // `wp_unique_term_slug()` allows term creation but iterates the slug.
  387. $this->assertSame( 'bar-2', $t4_term->slug );
  388. $this->assertSame( 'Foo', $t4_term->name );
  389. }
  390. /**
  391. * @ticket 39984
  392. */
  393. public function test_duplicate_name_check_should_fail_when_no_slug_is_provided_even_when_slugs_would_not_clash() {
  394. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  395. $t1 = self::factory()->term->create(
  396. array(
  397. 'name' => 'Foo',
  398. 'slug' => 'foo-no-conflict',
  399. 'taxonomy' => 'wptests_tax',
  400. )
  401. );
  402. $error = wp_insert_term( 'Foo', 'wptests_tax' );
  403. $this->assertWPError( $error );
  404. $this->assertSame( 'term_exists', $error->get_error_code() );
  405. $this->assertSame( $t1, $error->get_error_data() );
  406. }
  407. /**
  408. * @ticket 39984
  409. */
  410. public function test_error_should_reference_correct_term_when_rejected_as_duplicate() {
  411. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  412. $t1 = self::factory()->term->create(
  413. array(
  414. 'name' => 'Foo',
  415. 'slug' => 'foo',
  416. 'taxonomy' => 'wptests_tax',
  417. )
  418. );
  419. $t2 = self::factory()->term->create(
  420. array(
  421. 'name' => 'Bar',
  422. 'slug' => 'bar',
  423. 'taxonomy' => 'wptests_tax',
  424. )
  425. );
  426. $t1_child = wp_insert_term(
  427. 'Child',
  428. 'wptests_tax',
  429. array(
  430. 'parent' => $t1,
  431. )
  432. );
  433. $t2_child = wp_insert_term(
  434. 'Child',
  435. 'wptests_tax',
  436. array(
  437. 'parent' => $t2,
  438. )
  439. );
  440. $error = wp_insert_term(
  441. 'Child',
  442. 'wptests_tax',
  443. array(
  444. 'parent' => $t2,
  445. )
  446. );
  447. $this->assertWPError( $error );
  448. $this->assertSame( 'term_exists', $error->get_error_code() );
  449. $this->assertSame( $t2_child['term_id'], $error->get_error_data() );
  450. }
  451. /**
  452. * @ticket 31328
  453. */
  454. public function test_wp_insert_term_should_allow_duplicate_names_when_a_unique_slug_has_been_provided_in_hierarchical_taxonomy() {
  455. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  456. $t1 = self::factory()->term->create(
  457. array(
  458. 'name' => 'Foo',
  459. 'slug' => 'foo',
  460. 'taxonomy' => 'wptests_tax',
  461. )
  462. );
  463. $t2 = wp_insert_term(
  464. 'Foo',
  465. 'wptests_tax',
  466. array(
  467. 'slug' => 'foo-unique',
  468. )
  469. );
  470. $this->assertNotWPError( $t2 );
  471. $t2_term = get_term( $t2['term_id'], 'wptests_tax' );
  472. $this->assertSame( 'foo-unique', $t2_term->slug );
  473. $this->assertSame( 'Foo', $t2_term->name );
  474. }
  475. /**
  476. * @ticket 31328
  477. */
  478. public function test_wp_insert_term_should_not_allow_duplicate_names_when_the_slug_is_not_provided_in_hierarchical_taxonomy() {
  479. register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
  480. $t1 = self::factory()->term->create(
  481. array(
  482. 'name' => 'Foo',
  483. 'slug' => 'foo',
  484. 'taxonomy' => 'wptests_tax',
  485. )
  486. );
  487. $t2 = wp_insert_term( 'Foo', 'wptests_tax' );
  488. $this->assertWPError( $t2 );
  489. $this->assertSame( 'term_exists', $t2->get_error_code() );
  490. }
  491. /**
  492. * @ticket 5809
  493. */
  494. public function test_wp_insert_term_duplicate_slug_same_taxonomy() {
  495. register_taxonomy( 'wptests_tax', 'post' );
  496. $t = self::factory()->term->create(
  497. array(
  498. 'name' => 'Foo',
  499. 'slug' => 'foo',
  500. 'taxonomy' => 'wptests_tax',
  501. )
  502. );
  503. $term = get_term( $t, 'wptests_tax' );
  504. $created = wp_insert_term(
  505. 'Foo 2',
  506. 'wptests_tax',
  507. array(
  508. 'slug' => 'foo',
  509. )
  510. );
  511. $created_term = get_term( $created['term_id'], 'wptests_tax' );
  512. $this->assertSame( 'foo-2', $created_term->slug );
  513. _unregister_taxonomy( 'wptests_tax', 'post' );
  514. }
  515. /**
  516. * @ticket 5809
  517. */
  518. public function test_wp_insert_term_duplicate_slug_different_taxonomy() {
  519. register_taxonomy( 'wptests_tax', 'post' );
  520. register_taxonomy( 'wptests_tax_2', 'post' );
  521. $t = self::factory()->term->create(
  522. array(
  523. 'name' => 'Foo',
  524. 'slug' => 'foo',
  525. 'taxonomy' => 'wptests_tax',
  526. )
  527. );
  528. $term = get_term( $t, 'wptests_tax' );
  529. $created = wp_insert_term(
  530. 'Foo 2',
  531. 'wptests_tax_2',
  532. array(
  533. 'slug' => 'foo',
  534. )
  535. );
  536. $this->assertNotWPError( $created );
  537. $new_term = get_term( $created['term_id'], 'wptests_tax_2' );
  538. $this->assertSame( 'foo', $new_term->slug );
  539. _unregister_taxonomy( 'wptests_tax', 'post' );
  540. }
  541. /**
  542. * @ticket 5809
  543. */
  544. public function test_wp_insert_term_duplicate_slug_different_taxonomy_before_410_schema_change() {
  545. $old_db_version = 30055;
  546. update_option( 'db_version', $old_db_version );
  547. register_taxonomy( 'wptests_tax', 'post' );
  548. register_taxonomy( 'wptests_tax_2', 'post' );
  549. $t = self::factory()->term->create(
  550. array(
  551. 'name' => 'Foo',
  552. 'slug' => 'foo',
  553. 'taxonomy' => 'wptests_tax',
  554. )
  555. );
  556. $term = get_term( $t, 'wptests_tax' );
  557. $created = wp_insert_term(
  558. 'Foo 2',
  559. 'wptests_tax_2',
  560. array(
  561. 'slug' => 'foo',
  562. )
  563. );
  564. $this->assertNotWPError( $created );
  565. $new_term = get_term( $created['term_id'], 'wptests_tax_2' );
  566. /*
  567. * As of 4.1, we no longer create a shared term, but we also do not
  568. * allow for duplicate slugs.
  569. */
  570. $this->assertSame( 'foo-2', $new_term->slug );
  571. $this->assertNotEquals( $new_term->term_id, $term->term_id );
  572. _unregister_taxonomy( 'wptests_tax', 'post' );
  573. }
  574. public function test_wp_insert_term_alias_of_no_term_group() {
  575. register_taxonomy( 'wptests_tax', 'post' );
  576. $t1 = self::factory()->term->create(
  577. array(
  578. 'taxonomy' => 'wptests_tax',
  579. )
  580. );
  581. $term_1 = get_term( $t1, 'wptests_tax' );
  582. $created_term_ids = wp_insert_term(
  583. 'Foo',
  584. 'wptests_tax',
  585. array(
  586. 'alias_of' => $term_1->slug,
  587. )
  588. );
  589. $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
  590. $updated_term_1 = get_term( $term_1->term_id, 'wptests_tax' );
  591. $term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
  592. _unregister_taxonomy( 'wptests_tax' );
  593. $this->assertSame( 0, $term_1->term_group );
  594. $this->assertNotEmpty( $created_term->term_group );
  595. $this->assertSame( $created_term->term_group, $updated_term_1->term_group );
  596. }
  597. public function test_wp_insert_term_alias_of_existing_term_group() {
  598. register_taxonomy( 'wptests_tax', 'post' );
  599. $t1 = self::factory()->term->create(
  600. array(
  601. 'taxonomy' => 'wptests_tax',
  602. )
  603. );
  604. $term_1 = get_term( $t1, 'wptests_tax' );
  605. $t2 = self::factory()->term->create(
  606. array(
  607. 'taxonomy' => 'wptests_tax',
  608. 'alias_of' => $term_1->slug,
  609. )
  610. );
  611. $term_2 = get_term( $t2, 'wptests_tax' );
  612. $created_term_ids = wp_insert_term(
  613. 'Foo',
  614. 'wptests_tax',
  615. array(
  616. 'alias_of' => $term_2->slug,
  617. )
  618. );
  619. $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
  620. _unregister_taxonomy( 'wptests_tax' );
  621. $this->assertNotEmpty( $created_term->term_group );
  622. $this->assertSame( $created_term->term_group, $term_2->term_group );
  623. }
  624. public function test_wp_insert_term_alias_of_nonexistent_term() {
  625. register_taxonomy( 'wptests_tax', 'post' );
  626. $created_term_ids = wp_insert_term(
  627. 'Foo',
  628. 'wptests_tax',
  629. array(
  630. 'alias_of' => 'foo',
  631. )
  632. );
  633. $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
  634. _unregister_taxonomy( 'wptests_tax' );
  635. $this->assertSame( 0, $created_term->term_group );
  636. }
  637. /**
  638. * @ticket 5809
  639. */
  640. public function test_wp_insert_term_should_not_create_shared_term() {
  641. register_taxonomy( 'wptests_tax', 'post' );
  642. register_taxonomy( 'wptests_tax_2', 'post' );
  643. $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
  644. $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
  645. $this->assertNotEquals( $t1['term_id'], $t2['term_id'] );
  646. }
  647. public function test_wp_insert_term_should_return_term_id_and_term_taxonomy_id() {
  648. register_taxonomy( 'wptests_tax', 'post' );
  649. $found = wp_insert_term( 'foo', 'wptests_tax' );
  650. $term_by_id = get_term( $found['term_id'], 'wptests_tax' );
  651. $term_by_slug = get_term_by( 'slug', 'foo', 'wptests_tax' );
  652. $term_by_ttid = get_term_by( 'term_taxonomy_id', $found['term_taxonomy_id'], 'wptests_tax' );
  653. _unregister_taxonomy( 'wptests_tax' );
  654. $this->assertInternalType( 'array', $found );
  655. $this->assertNotEmpty( $found['term_id'] );
  656. $this->assertNotEmpty( $found['term_taxonomy_id'] );
  657. $this->assertNotEmpty( $term_by_id );
  658. $this->assertEquals( $term_by_id, $term_by_slug );
  659. $this->assertEquals( $term_by_id, $term_by_ttid );
  660. }
  661. public function test_wp_insert_term_should_clean_term_cache() {
  662. register_taxonomy(
  663. 'wptests_tax',
  664. 'post',
  665. array(
  666. 'hierarchical' => true,
  667. )
  668. );
  669. $t = self::factory()->term->create(
  670. array(
  671. 'taxonomy' => 'wptests_tax',
  672. )
  673. );
  674. /**
  675. * It doesn't appear that WordPress itself ever sets these
  676. * caches, but we should ensure that they're being cleared for
  677. * compatibility with third-party addons. Prime the caches
  678. * manually.
  679. */
  680. wp_cache_set( 'all_ids', array( 1, 2, 3 ), 'wptests_tax' );
  681. wp_cache_set( 'get', array( 1, 2, 3 ), 'wptests_tax' );
  682. $found = wp_insert_term(
  683. 'foo',
  684. 'wptests_tax',
  685. array(
  686. 'parent' => $t,
  687. )
  688. );
  689. _unregister_taxonomy( 'wptests_tax' );
  690. $this->assertFalse( wp_cache_get( 'all_ids', 'wptests_tax' ) );
  691. $this->assertFalse( wp_cache_get( 'get', 'wptests_tax' ) );
  692. $cached_children = get_option( 'wptests_tax_children' );
  693. $this->assertNotEmpty( $cached_children[ $t ] );
  694. $this->assertTrue( in_array( $found['term_id'], $cached_children[ $t ], true ) );
  695. }
  696. /**
  697. * @ticket 33864
  698. */
  699. public function test_wp_insert_term_with_and_without_accents() {
  700. register_taxonomy( 'wptests_tax', 'post' );
  701. $t1 = self::factory()->term->create(
  702. array(
  703. 'name' => 'Foó',
  704. 'taxonomy' => 'wptests_tax',
  705. )
  706. );
  707. $t2 = self::factory()->term->create(
  708. array(
  709. 'name' => 'Foo',
  710. 'taxonomy' => 'wptests_tax',
  711. )
  712. );
  713. $this->assertInternalType( 'int', $t1 );
  714. $this->assertInternalType( 'int', $t2 );
  715. $this->assertNotEquals( $t1, $t2 );
  716. $term_2 = get_term( $t2, 'wptests_tax' );
  717. $this->assertSame( $t2, $term_2->term_id );
  718. $this->assertSame( 'Foo', $term_2->name );
  719. }
  720. /**
  721. * @ticket 37009
  722. */
  723. public function test_term_whose_slug_matches_existing_term_but_name_does_not_should_get_suffixed_slug() {
  724. register_taxonomy( 'wptests_tax', 'post' );
  725. $t1 = self::factory()->term->create(
  726. array(
  727. 'name' => 'Foo#bar',
  728. 'taxonomy' => 'wptests_tax',
  729. )
  730. );
  731. $created = wp_insert_term( 'Foo$bar', 'wptests_tax' );
  732. $this->assertArrayHasKey( 'term_id', $created );
  733. $created_term = get_term( $created['term_id'] );
  734. $this->assertSame( 'Foo$bar', $created_term->name );
  735. $this->assertSame( 'foobar-2', $created_term->slug );
  736. }
  737. /**
  738. * @ticket 35321
  739. */
  740. public function test_wp_insert_term_with_null_description() {
  741. register_taxonomy( 'wptests_tax', 'post' );
  742. $term = wp_insert_term(
  743. 'foo',
  744. 'wptests_tax',
  745. array(
  746. 'description' => null,
  747. )
  748. );
  749. $term_object = get_term( $term['term_id'] );
  750. $this->assertInstanceOf( 'WP_Term', $term_object );
  751. $this->assertSame( '', $term_object->description );
  752. }
  753. /** Helpers */
  754. public function deleted_term_cb( $term, $tt_id, $taxonomy, $deleted_term, $object_ids ) {
  755. $this->assertInternalType( 'object', $deleted_term );
  756. $this->assertInternalType( 'int', $term );
  757. $this->assertInternalType( 'array', $object_ids );
  758. // Pesky string $this->assertInternalType( 'int', $tt_id );
  759. $this->assertSame( $term, $deleted_term->term_id );
  760. $this->assertSame( $taxonomy, $deleted_term->taxonomy );
  761. $this->assertEquals( $tt_id, $deleted_term->term_taxonomy_id );
  762. $this->assertEmpty( $object_ids );
  763. }
  764. public function _pre_insert_term_callback() {
  765. return new WP_Error( 'custom_error' );
  766. }
  767. }