categoryExists.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. class Tests_Term_CategoryExists extends WP_UnitTestCase {
  3. /**
  4. * @ticket 30975
  5. */
  6. public function test_category_exists_should_return_only_top_level_categories_when_parent_is_0() {
  7. $c1 = self::factory()->category->create();
  8. $c2 = self::factory()->category->create(
  9. array(
  10. 'name' => 'Foo',
  11. 'parent' => $c1,
  12. )
  13. );
  14. $c3 = self::factory()->category->create(
  15. array(
  16. 'name' => 'Foo',
  17. )
  18. );
  19. $found = category_exists( 'Foo', 0 );
  20. $this->assertEquals( $found, $c3 );
  21. }
  22. /**
  23. * @ticket 30975
  24. */
  25. public function test_category_exists_should_select_oldest_matching_category_when_no_parent_is_specified_1() {
  26. // Foo child of c1 is created first.
  27. $c1 = self::factory()->category->create();
  28. $c2 = self::factory()->category->create(
  29. array(
  30. 'name' => 'Foo',
  31. 'parent' => $c1,
  32. )
  33. );
  34. $c3 = self::factory()->category->create(
  35. array(
  36. 'name' => 'Foo',
  37. )
  38. );
  39. $found = category_exists( 'Foo' );
  40. $this->assertEquals( $found, $c2 );
  41. }
  42. /**
  43. * @ticket 30975
  44. */
  45. public function test_category_exists_should_select_oldest_matching_category_when_no_parent_is_specified_2() {
  46. // Top-level Foo is created first.
  47. $c1 = self::factory()->category->create();
  48. $c2 = self::factory()->category->create(
  49. array(
  50. 'name' => 'Foo',
  51. )
  52. );
  53. $c3 = self::factory()->category->create(
  54. array(
  55. 'name' => 'Foo',
  56. 'parent' => $c1,
  57. )
  58. );
  59. $found = category_exists( 'Foo' );
  60. $this->assertEquals( $found, $c2 );
  61. }
  62. /**
  63. * @ticket 30975
  64. */
  65. public function test_category_exists_should_respect_nonempty_parent() {
  66. $c1 = self::factory()->category->create();
  67. $c2 = self::factory()->category->create(
  68. array(
  69. 'name' => 'Foo',
  70. 'parent' => $c1,
  71. )
  72. );
  73. $c3 = self::factory()->category->create(
  74. array(
  75. 'name' => 'Foo',
  76. )
  77. );
  78. $found = category_exists( 'Foo', $c1 );
  79. $this->assertEquals( $found, $c2 );
  80. }
  81. }