/tests/Main_Category_Picker_Test.php

https://bitbucket.org/ltumedei/wp-plugin-main-category-picker · PHP · 232 lines · 169 code · 28 blank · 35 comment · 9 complexity · f4497090f05fb46f9a9eab5eb66a557b MD5 · raw file

  1. <?php
  2. require_once ROOT . '/main_category_picker.php';
  3. require_once ROOT . '/tests/libs/utils.php';
  4. require_once ROOT . '/tests/libs/Term_Factory.php';
  5. class Main_Category_Picker_Test extends WP_UnitTestCase
  6. {
  7. public $plugin;
  8. public $factory;
  9. public function setUp()
  10. {
  11. parent::setUp();
  12. $this->factory = new WP_UnitTest_Factory();
  13. $this->term_factory = new Term_Factory();
  14. $this->plugin = $GLOBALS['main-category-picker'];
  15. }
  16. function test_has_initialized(){
  17. $this->assertFalse($this->plugin == null );
  18. }
  19. function test_get_selected_post_types_from_options_returns_an_array(){
  20. $this->assertTrue(is_array($this->plugin->get_selected_post_types_from_options() ) );
  21. }
  22. function test_hooks_into_add_meta_boxes(){
  23. $this->assertTrue(has_filter( 'add_meta_boxes', array($this->plugin, 'add')) != false);
  24. }
  25. function test_hooks_into_save_post(){
  26. $this->assertTrue(has_filter( 'save_post', array($this->plugin, 'save') ) != false);
  27. }
  28. function spoof_nonce_fields(){
  29. wp_nonce_field( $this->plugin->_get_filename, 'mcp_class_nonce' );
  30. }
  31. function post_types_provider(){
  32. return array(
  33. array(1, array('post'),
  34. array(2,array('post', 'page')),
  35. array(4,array('post', 'page', 'foo', 'baz')),
  36. array(3,array('$page', 'foo', 'bar')),
  37. array(0, array()),
  38. array(0, null)
  39. )
  40. );
  41. }
  42. /**
  43. * @dataProvider post_types_provider
  44. */
  45. function test_adds_right_number_of_meta_boxes($count, $pages){
  46. global $wp_meta_boxes;
  47. $this->plugin->_selected_post_types = $pages;
  48. $this->plugin->add();
  49. $matches = 0;
  50. $matches = array_key_count_r('mcp_meta_box', $wp_meta_boxes);
  51. $this->assertEquals( $count , $matches );
  52. }
  53. function test_update_adds_post_meta(){
  54. $post_id = $this->factory->post->create();
  55. $meta_keys = array( '_main_category' , '_main_sub_category');
  56. foreach ($meta_keys as $key) {
  57. $_POST[$key] = 'new_foo';
  58. $this->plugin->_update($post_id, get_post( $post_id) );
  59. // update_post_meta( $post_id, $key, $_POST[$key]);
  60. $this->assertEquals('new_foo', get_post_meta( $post_id, $key,true ), 'meta-key is: ' . $key);
  61. }
  62. }
  63. function test_update_updates_post_meta(){
  64. $post_id = $this->factory->post->create();
  65. $meta_keys = array( '_main_category' , '_main_sub_category');
  66. foreach ($meta_keys as $key) {
  67. //set a post meta
  68. add_post_meta( $post_id, $key, 'saved_foo', true);
  69. }
  70. foreach ($meta_keys as $key) {
  71. $_POST[$key] = 'updated_foo';
  72. $this->plugin->_update($post_id, get_post( $post_id) );
  73. // update_post_meta( $post_id, $key, $_POST[$key]);
  74. $this->assertEquals('updated_foo', get_post_meta( $post_id, $key,true ), 'meta-key is: ' . $key);
  75. }
  76. }
  77. function test_update_deletes_post_meta(){
  78. $post_id = $this->factory->post->create();
  79. $meta_keys = array( '_main_category' , '_main_sub_category');
  80. foreach ( $meta_keys as $key) {
  81. add_post_meta( $post_id, $key, 'saved_foo', $unique = true);
  82. }
  83. foreach ($meta_keys as $key) {
  84. $_POST[$key] = '';
  85. $this->plugin->_update($post_id, get_post( $post_id) );
  86. $this->assertEquals('', get_post_meta( $post->ID, $key,true ), 'meta-key is: ' . $key);
  87. }
  88. }
  89. function test_update_works_for_pages(){
  90. $post_id = $this->factory->post->create(array('post_type' => 'page'));
  91. $meta_keys = array( '_main_category' , '_main_sub_category');
  92. foreach ( $meta_keys as $key) {
  93. add_post_meta( $post_id, $key, 'baz', $unique = true);
  94. }
  95. foreach ($meta_keys as $key) {
  96. $_POST[$key] = 'foo';
  97. $this->plugin->_update($post_id, get_post( $post_id) );
  98. $this->assertEquals('foo', get_post_meta( $post_id, $key,true ), 'meta-key is: ' . $key);
  99. }
  100. }
  101. function tests_categories_are_properly_populted(){
  102. global $post;
  103. $post_id = $this->factory->post->create();
  104. $post = get_post($post_id);
  105. $this->term_factory->populate_categories_for_post($post_id);
  106. $cat_ids = wp_get_post_categories( $post_id );
  107. $parents = array();
  108. $children = array();
  109. foreach ($cat_ids as $c_id) {
  110. $c = get_category( $c_id );
  111. if ( $c->taxonomy == 'category'){
  112. if ($c->parent) {
  113. $children[] = $c;
  114. }
  115. else {
  116. $parents[] = $c;
  117. }
  118. }
  119. }
  120. $this->assertTrue(count($parents) == 20 );
  121. $this->assertTrue(count($children) == 40 );
  122. }
  123. function test_get_main_category(){
  124. global $post;
  125. $post_id = $this->factory->post->create();
  126. $post = get_post($post_id);
  127. // if no main cats and no cats
  128. // then it returns Uncategorized
  129. $this->assertNotNull(Main_Category_Picker::get_main_category($post_id));
  130. $this->assertEquals( 'Uncategorized', Main_Category_Picker::get_main_category($post_id)->name);
  131. // if no main category is found
  132. // then the function returns the last one
  133. // and not Uncategorized
  134. $this->term_factory->populate_categories_for_post($post_id);
  135. $main_cat = $this->term_factory->get_last_main_category($post_id);
  136. $this->assertNotNull(Main_Category_Picker::get_main_category($post_id));
  137. $this->assertEquals( $main_cat->name, Main_Category_Picker::get_main_category($post_id)->name);
  138. // if a main category is set
  139. // but it's not assigned to the post anymore
  140. // and the post has categories assigned to it
  141. // then returns the last parent category
  142. // and not Uncategorized
  143. $this->term_factory->populate_categories_for_post($post_id);
  144. $main_cat = $this->term_factory->get_last_main_category($post_id);
  145. $meta_key = '_main_category';
  146. $meta_value = 'foo';
  147. add_post_meta( $post_id, $meta_key, $meta_value, true );
  148. $this->assertNotNull(Main_Category_Picker::get_main_category($post_id));
  149. $this->assertEquals($main_cat->name, Main_Category_Picker::get_main_category($post_id)->name);
  150. // if a main category exists
  151. // and is an actual category
  152. // then return it
  153. $this->term_factory->populate_categories_for_post($post_id);
  154. $main_cat = $this->term_factory->get_random_main_category($post_id);
  155. $meta_value = $main_cat->slug;
  156. delete_post_meta( $post_id, $meta_key, 'foo' );
  157. add_post_meta( $post_id, $meta_key, $meta_value, true );
  158. $this->assertNotNull(Main_Category_Picker::get_main_category($post_id));
  159. $this->assertEquals($main_cat->name, Main_Category_Picker::get_main_category($post_id)->name);
  160. }
  161. function test_get_main_sub_category(){
  162. global $post;
  163. $post_id = $this->factory->post->create();
  164. $post = get_post($post_id);
  165. // given no main sub category set
  166. // and no sub categories assigned to the post
  167. // then it returns null
  168. $this->assertNull( Main_Category_Picker::get_main_sub_category($post_id) );
  169. // given no main sub category set
  170. // and the post has sub categories assigned to it
  171. // then the function returns the last sub category
  172. // and not Uncategorized
  173. $this->term_factory->populate_categories_for_post($post_id);
  174. $sub_cat = $this->term_factory->get_last_main_category($post_id, true);
  175. $this->assertNotNull(Main_Category_Picker::get_main_sub_category($post_id));
  176. $this->assertEquals( $sub_cat->name, Main_Category_Picker::get_main_sub_category($post_id)->name);
  177. // given a main sub category is set
  178. // and the assigned sub category is not assigned to the post anymore
  179. // and the post has sub categories assigned to it
  180. // then returns the last sub category
  181. // and not Uncategorized
  182. $this->term_factory->populate_categories_for_post($post_id);
  183. $sub_cat = $this->term_factory->get_last_main_category( $post_id , true );
  184. $meta_key = '_main_sub_category';
  185. $meta_value = 'foo';
  186. add_post_meta( $post_id, $meta_key, $meta_value, true );
  187. $this->assertEquals( $sub_cat->name, Main_Category_Picker::get_main_sub_category($post_id)->name);
  188. // given a main sub category exists
  189. // and is a sub category assigned to the post
  190. // then return it
  191. $this->term_factory->populate_categories_for_post($post_id);
  192. $main_cat = $this->term_factory->get_random_main_category($post_id, true);
  193. $meta_value = $main_cat->slug;
  194. delete_post_meta( $post_id, $meta_key, 'foo' );
  195. add_post_meta( $post_id, $meta_key, $meta_value, true );
  196. $this->assertNotNull(Main_Category_Picker::get_main_sub_category($post_id));
  197. $this->assertEquals($main_cat->name, Main_Category_Picker::get_main_sub_category($post_id)->name);
  198. }
  199. public function tearDown()
  200. {
  201. // your code here
  202. }
  203. }
  204. ?>