category.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Validate Category API
  4. *
  5. * Notes:
  6. * cat_is_ancestor_of is validated under test\term\term_is_ancestor_of
  7. *
  8. * @group category.php
  9. */
  10. class Tests_Category extends WP_UnitTestCase {
  11. function tearDown() {
  12. _unregister_taxonomy( 'test_tax_cat' );
  13. parent::tearDown();
  14. }
  15. /**
  16. * Validate get_all_category_ids
  17. *
  18. * @expectedDeprecated get_all_category_ids
  19. */
  20. function test_get_all_category_ids() {
  21. // Ccreate categories.
  22. self::factory()->category->create_many( 2 );
  23. // Create new taxonomy to ensure not included.
  24. register_taxonomy( 'test_tax_cat', 'post' );
  25. wp_insert_term( 'test1', 'test_tax_cat' );
  26. // Validate length is 1 + created due to uncategorized.
  27. $cat_ids = get_all_category_ids();
  28. $this->assertSame( 3, count( $cat_ids ) );
  29. }
  30. /**
  31. * Validate get_category_by_slug function
  32. */
  33. function test_get_category_by_slug() {
  34. // Create test categories.
  35. $testcat = self::factory()->category->create_and_get(
  36. array(
  37. 'slug' => 'testcat',
  38. 'name' => 'Test Category 1',
  39. )
  40. );
  41. $testcat2 = self::factory()->category->create_and_get(
  42. array(
  43. 'slug' => 'testcat2',
  44. 'name' => 'Test Category 2',
  45. )
  46. );
  47. // Validate category is returned by slug.
  48. $ret_testcat = get_category_by_slug( 'testcat' );
  49. $this->assertSame( $testcat->term_id, $ret_testcat->term_id );
  50. $ret_testcat = get_category_by_slug( 'TeStCaT' );
  51. $this->assertSame( $testcat->term_id, $ret_testcat->term_id );
  52. // Validate unknown category returns false.
  53. $this->assertFalse( get_category_by_slug( 'testcat3' ) );
  54. }
  55. /**
  56. * Validate _make_cat_compat function
  57. */
  58. function test__make_cat_compat() {
  59. // Create test categories and array representations.
  60. $testcat_array = array(
  61. 'slug' => 'testmcc',
  62. 'name' => 'Test MCC',
  63. 'description' => 'Category Test',
  64. );
  65. $testcat = self::factory()->category->create_and_get( $testcat_array );
  66. $testcat_array['term_id'] = $testcat->term_id;
  67. $testcat2_array = array(
  68. 'slug' => 'testmcc',
  69. 'name' => 'Test MCC',
  70. 'description' => 'Category Test',
  71. 'parent' => $testcat->term_id,
  72. );
  73. $testcat2 = self::factory()->category->create_and_get( $testcat2_array );
  74. $testcat2_array['term_id'] = $testcat2->term_id;
  75. // Unset properties to enable validation of object.
  76. unset( $testcat->cat_ID );
  77. unset( $testcat->category_count );
  78. unset( $testcat->category_description );
  79. unset( $testcat->cat_name );
  80. unset( $testcat->category_nicename );
  81. unset( $testcat->category_parent );
  82. unset( $testcat2->cat_ID );
  83. unset( $testcat2->category_count );
  84. unset( $testcat2->category_description );
  85. unset( $testcat2->cat_name );
  86. unset( $testcat2->category_nicename );
  87. unset( $testcat2->category_parent );
  88. // Make compatible.
  89. _make_cat_compat( $testcat );
  90. _make_cat_compat( $testcat2 );
  91. _make_cat_compat( $testcat_array );
  92. _make_cat_compat( $testcat2_array );
  93. // Validate compatibility object.
  94. $this->assertSame( $testcat->cat_ID, $testcat->term_id );
  95. $this->assertSame( $testcat->category_count, $testcat->count );
  96. $this->assertSame( $testcat->category_description, $testcat->description );
  97. $this->assertSame( $testcat->cat_name, $testcat->name );
  98. $this->assertSame( $testcat->category_nicename, $testcat->slug );
  99. $this->assertSame( $testcat->category_parent, $testcat->parent );
  100. // Validate compatibility object with parent.
  101. $this->assertSame( $testcat->cat_ID, $testcat->term_id );
  102. $this->assertSame( $testcat->category_count, $testcat->count );
  103. $this->assertSame( $testcat->category_description, $testcat->description );
  104. $this->assertSame( $testcat->cat_name, $testcat->name );
  105. $this->assertSame( $testcat->category_nicename, $testcat->slug );
  106. $this->assertSame( $testcat->category_parent, $testcat->parent );
  107. // Validate compatibility array.
  108. $this->assertSame( $testcat_array['cat_ID'], $testcat_array['term_id'] );
  109. $this->assertSame( $testcat_array['category_count'], $testcat_array['count'] );
  110. $this->assertSame( $testcat_array['category_description'], $testcat_array['description'] );
  111. $this->assertSame( $testcat_array['cat_name'], $testcat_array['name'] );
  112. $this->assertSame( $testcat_array['category_nicename'], $testcat_array['slug'] );
  113. $this->assertSame( $testcat_array['category_parent'], $testcat_array['parent'] );
  114. // Validate compatibility array with parent.
  115. $this->assertSame( $testcat_array['cat_ID'], $testcat_array['term_id'] );
  116. $this->assertSame( $testcat_array['category_count'], $testcat_array['count'] );
  117. $this->assertSame( $testcat_array['category_description'], $testcat_array['description'] );
  118. $this->assertSame( $testcat_array['cat_name'], $testcat_array['name'] );
  119. $this->assertSame( $testcat_array['category_nicename'], $testcat_array['slug'] );
  120. $this->assertSame( $testcat_array['category_parent'], $testcat_array['parent'] );
  121. }
  122. /**
  123. * Validate get_cat_name function
  124. */
  125. function test_get_cat_name() {
  126. // Create test category.
  127. $testcat = self::factory()->category->create_and_get(
  128. array(
  129. 'slug' => 'testcat',
  130. 'name' => 'Test Category 1',
  131. )
  132. );
  133. // Validate.
  134. $this->assertSame( $testcat->name, get_cat_name( $testcat->term_id ) );
  135. $this->assertSame( '', get_cat_name( -1 ) );
  136. $this->assertSame( '', get_cat_name( $testcat->term_id + 100 ) );
  137. }
  138. /**
  139. * Validate get_cat_name function
  140. */
  141. function test_get_cat_ID() {
  142. // Create test category.
  143. $testcat = self::factory()->category->create_and_get(
  144. array(
  145. 'slug' => 'testcat',
  146. 'name' => 'Test Category 1',
  147. )
  148. );
  149. // Validate.
  150. $this->assertSame( $testcat->term_id, get_cat_ID( $testcat->name ) );
  151. $this->assertSame( 0, get_cat_ID( 'NO CAT' ) );
  152. $this->assertSame( 0, get_cat_ID( 12 ) );
  153. }
  154. /**
  155. * Validate get_category_by_path function
  156. */
  157. function test_get_category_by_path() {
  158. // Create test categories.
  159. $root_id = self::factory()->category->create(
  160. array(
  161. 'slug' => 'root',
  162. )
  163. );
  164. $root_cat_id = self::factory()->category->create(
  165. array(
  166. 'slug' => 'cat',
  167. 'parent' => $root_id,
  168. )
  169. );
  170. $root_cat_cat_id = self::factory()->category->create(
  171. array(
  172. 'slug' => 'cat', // Note this is modified on create.
  173. 'parent' => $root_cat_id,
  174. )
  175. );
  176. $root_path_id = self::factory()->category->create(
  177. array(
  178. 'slug' => 'path',
  179. 'parent' => $root_id,
  180. )
  181. );
  182. $root_path_cat_id = self::factory()->category->create(
  183. array(
  184. 'slug' => 'cat', // Note this is modified on create.
  185. 'parent' => $root_path_id,
  186. )
  187. );
  188. $root_level_id = self::factory()->category->create(
  189. array(
  190. 'slug' => 'level-1',
  191. 'parent' => $root_id,
  192. )
  193. );
  194. $root_level_cat_id = self::factory()->category->create(
  195. array(
  196. 'slug' => 'cat', // Note this is modified on create.
  197. 'parent' => $root_level_id,
  198. )
  199. );
  200. // Validate full match.
  201. $ret_cat = get_category_by_path( '/root/level-1', true );
  202. $this->assertSame( $root_level_id, $ret_cat->term_id );
  203. $this->assertNull( get_category_by_path( 'level-1', true ) );
  204. $this->assertNull( get_category_by_path( 'nocat/nocat/', true ) );
  205. // Validate partial match.
  206. $ret_cat = get_category_by_path( 'level-1', false );
  207. $this->assertSame( $root_level_id, $ret_cat->term_id );
  208. $ret_cat = get_category_by_path( 'root/cat/level-1', false );
  209. $this->assertSame( $root_level_id, $ret_cat->term_id );
  210. $ret_cat = get_category_by_path( 'root$2Fcat%20%2Flevel-1', false );
  211. $this->assertSame( $root_level_id, $ret_cat->term_id );
  212. $this->assertNull( get_category_by_path( 'nocat/nocat/', false ) );
  213. }
  214. }