PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/admin/includesScreen.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 207 lines | 165 code | 33 blank | 9 comment | 9 complexity | 1d7aebe2a175b286fb6f525f1bf2fbbd MD5 | raw file
  1. <?php
  2. /**
  3. * @group admin
  4. */
  5. class Tests_Admin_includesScreen extends WP_UnitTestCase {
  6. var $core_screens = array(
  7. 'index.php' => array( 'base' => 'dashboard', 'id' => 'dashboard' ),
  8. 'edit.php' => array( 'base' => 'edit', 'id' => 'edit-post', 'post_type' => 'post' ),
  9. 'post-new.php'=> array( 'action' => 'add', 'base' => 'post', 'id' => 'post', 'post_type' => 'post' ),
  10. 'edit-tags.php' => array( 'base' => 'edit-tags', 'id' => 'edit-post_tag', 'post_type' => 'post', 'taxonomy' => 'post_tag' ),
  11. 'edit-tags.php?taxonomy=post_tag' => array( 'base' => 'edit-tags', 'id' => 'edit-post_tag', 'post_type' => 'post', 'taxonomy' => 'post_tag' ),
  12. 'edit-tags.php?taxonomy=category' => array( 'base' => 'edit-tags', 'id' => 'edit-category', 'post_type' => 'post', 'taxonomy' => 'category' ),
  13. 'upload.php' => array( 'base' => 'upload', 'id' => 'upload' ),
  14. 'media-new.php' => array( 'action' => 'add', 'base' => 'media', 'id' => 'media' ),
  15. 'edit.php?post_type=page' => array( 'base' => 'edit', 'id' => 'edit-page', 'post_type' => 'page' ),
  16. 'link-manager.php' => array( 'base' => 'link-manager', 'id' => 'link-manager' ),
  17. 'link-add.php' => array( 'action' => 'add', 'base' => 'link', 'id' => 'link' ),
  18. 'edit-tags.php?taxonomy=link_category' => array( 'base' => 'edit-tags', 'id' => 'edit-link_category', 'taxonomy' => 'link_category', 'post_type' => '' ),
  19. 'edit-comments.php' => array( 'base' => 'edit-comments', 'id' => 'edit-comments' ),
  20. 'themes.php' => array( 'base' => 'themes', 'id' => 'themes' ),
  21. 'widgets.php' => array( 'base' => 'widgets', 'id' => 'widgets' ),
  22. 'nav-menus.php' => array( 'base' => 'nav-menus', 'id' => 'nav-menus' ),
  23. 'plugins.php' => array( 'base' => 'plugins', 'id' => 'plugins' ),
  24. 'users.php' => array( 'base' => 'users', 'id' => 'users' ),
  25. 'user-new.php' => array( 'action' => 'add', 'base' => 'user', 'id' => 'user' ),
  26. 'profile.php' => array( 'base' => 'profile', 'id' => 'profile' ),
  27. 'tools.php' => array( 'base' => 'tools', 'id' => 'tools' ),
  28. 'import.php' => array( 'base' => 'import', 'id' => 'import' ),
  29. 'export.php' => array( 'base' => 'export', 'id' => 'export' ),
  30. 'options-general.php' => array( 'base' => 'options-general', 'id' => 'options-general' ),
  31. 'options-writing.php' => array( 'base' => 'options-writing', 'id' => 'options-writing' ),
  32. );
  33. function setUp() {
  34. set_current_screen( 'front' );
  35. }
  36. function tearDown() {
  37. parent::tearDown();
  38. unset( $GLOBALS['wp_taxonomies']['old-or-new'] );
  39. set_current_screen( 'front' );
  40. }
  41. function test_set_current_screen_with_hook_suffix() {
  42. global $current_screen;
  43. foreach ( $this->core_screens as $hook_name => $screen ) {
  44. $_GET = $_POST = $_REQUEST = array();
  45. $GLOBALS['taxnow'] = $GLOBALS['typenow'] = '';
  46. $screen = (object) $screen;
  47. $hook = parse_url( $hook_name );
  48. if ( ! empty( $hook['query'] ) ) {
  49. $args = wp_parse_args( $hook['query'] );
  50. if ( isset( $args['taxonomy'] ) )
  51. $GLOBALS['taxnow'] = $_GET['taxonomy'] = $_POST['taxonomy'] = $_REQUEST['taxonomy'] = $args['taxonomy'];
  52. if ( isset( $args['post_type'] ) )
  53. $GLOBALS['typenow'] = $_GET['post_type'] = $_POST['post_type'] = $_REQUEST['post_type'] = $args['post_type'];
  54. else if ( isset( $screen->post_type ) )
  55. $GLOBALS['typenow'] = $_GET['post_type'] = $_POST['post_type'] = $_REQUEST['post_type'] = $screen->post_type;
  56. }
  57. $GLOBALS['hook_suffix'] = $hook['path'];
  58. set_current_screen();
  59. $this->assertEquals( $screen->id, $current_screen->id, $hook_name );
  60. $this->assertEquals( $screen->base, $current_screen->base, $hook_name );
  61. if ( isset( $screen->action ) )
  62. $this->assertEquals( $screen->action, $current_screen->action, $hook_name );
  63. if ( isset( $screen->post_type ) )
  64. $this->assertEquals( $screen->post_type, $current_screen->post_type, $hook_name );
  65. else
  66. $this->assertEmpty( $current_screen->post_type, $hook_name );
  67. if ( isset( $screen->taxonomy ) )
  68. $this->assertEquals( $screen->taxonomy, $current_screen->taxonomy, $hook_name );
  69. $this->assertTrue( $current_screen->in_admin() );
  70. $this->assertTrue( $current_screen->in_admin( 'site' ) );
  71. $this->assertFalse( $current_screen->in_admin( 'network' ) );
  72. $this->assertFalse( $current_screen->in_admin( 'user' ) );
  73. $this->assertFalse( $current_screen->in_admin( 'garbage' ) );
  74. // With convert_to_screen(), the same ID should return the exact $current_screen.
  75. $this->assertSame( $current_screen, convert_to_screen( $screen->id ), $hook_name );
  76. // With convert_to_screen(), the hook_suffix should return the exact $current_screen.
  77. // But, convert_to_screen() cannot figure out ?taxonomy and ?post_type.
  78. if ( empty( $hook['query'] ) )
  79. $this->assertSame( $current_screen, convert_to_screen( $GLOBALS['hook_suffix'] ), $hook_name );
  80. }
  81. }
  82. function test_post_type_as_hookname() {
  83. $screen = convert_to_screen( 'page' );
  84. $this->assertEquals( $screen->post_type, 'page' );
  85. $this->assertEquals( $screen->base, 'post' );
  86. $this->assertEquals( $screen->id, 'page' );
  87. }
  88. function test_post_type_with_special_suffix_as_hookname() {
  89. register_post_type( 'value-add' );
  90. $screen = convert_to_screen( 'value-add' ); // the -add part is key.
  91. $this->assertEquals( $screen->post_type, 'value-add' );
  92. $this->assertEquals( $screen->base, 'post' );
  93. $this->assertEquals( $screen->id, 'value-add' );
  94. $screen = convert_to_screen( 'edit-value-add' ); // the -add part is key.
  95. $this->assertEquals( $screen->post_type, 'value-add' );
  96. $this->assertEquals( $screen->base, 'edit' );
  97. $this->assertEquals( $screen->id, 'edit-value-add' );
  98. }
  99. function test_taxonomy_with_special_suffix_as_hookname() {
  100. register_taxonomy( 'old-or-new', 'post' );
  101. $screen = convert_to_screen( 'edit-old-or-new' ); // the -new part is key.
  102. $this->assertEquals( $screen->taxonomy, 'old-or-new' );
  103. $this->assertEquals( $screen->base, 'edit-tags' );
  104. $this->assertEquals( $screen->id, 'edit-old-or-new' );
  105. }
  106. function test_post_type_with_edit_prefix() {
  107. register_post_type( 'edit-some-thing' );
  108. $screen = convert_to_screen( 'edit-some-thing' );
  109. $this->assertEquals( $screen->post_type, 'edit-some-thing' );
  110. $this->assertEquals( $screen->base, 'post' );
  111. $this->assertEquals( $screen->id, 'edit-some-thing' );
  112. $screen = convert_to_screen( 'edit-edit-some-thing' );
  113. $this->assertEquals( $screen->post_type, 'edit-some-thing' );
  114. $this->assertEquals( $screen->base, 'edit' );
  115. $this->assertEquals( $screen->id, 'edit-edit-some-thing' );
  116. }
  117. function test_post_type_edit_collisions() {
  118. register_post_type( 'comments' );
  119. register_post_type( 'tags' );
  120. // Sorry, core wins here.
  121. $screen = convert_to_screen( 'edit-comments' );
  122. $this->assertEquals( $screen->base, 'edit-comments' );
  123. // The post type wins here. convert_to_screen( $post_type ) is only relevant for meta boxes anyway.
  124. $screen = convert_to_screen( 'comments' );
  125. $this->assertEquals( $screen->base, 'post' );
  126. // Core wins.
  127. $screen = convert_to_screen( 'edit-tags' );
  128. $this->assertEquals( $screen->base, 'edit-tags' );
  129. $screen = convert_to_screen( 'tags' );
  130. $this->assertEquals( $screen->base, 'post' );
  131. }
  132. function test_help_tabs() {
  133. $tab = rand_str();
  134. $tab_args = array(
  135. 'id' => $tab,
  136. 'title' => 'Help!',
  137. 'content' => 'Some content',
  138. 'callback' => false,
  139. );
  140. $screen = get_current_screen();
  141. $screen->add_help_tab( $tab_args );
  142. $this->assertEquals( $screen->get_help_tab( $tab ), $tab_args );
  143. $tabs = $screen->get_help_tabs();
  144. $this->assertArrayHasKey( $tab, $tabs );
  145. $screen->remove_help_tab( $tab );
  146. $this->assertNull( $screen->get_help_tab( $tab ) );
  147. $screen->remove_help_tabs();
  148. $this->assertEquals( $screen->get_help_tabs(), array() );
  149. }
  150. function test_in_admin() {
  151. $screen = get_current_screen();
  152. set_current_screen( 'edit.php' );
  153. $this->assertTrue( get_current_screen()->in_admin() );
  154. $this->assertTrue( get_current_screen()->in_admin( 'site' ) );
  155. $this->assertFalse( get_current_screen()->in_admin( 'network' ) );
  156. $this->assertFalse( get_current_screen()->in_admin( 'user' ) );
  157. set_current_screen( 'dashboard-network' );
  158. $this->assertTrue( get_current_screen()->in_admin() );
  159. $this->assertFalse( get_current_screen()->in_admin( 'site' ) );
  160. $this->assertTrue( get_current_screen()->in_admin( 'network' ) );
  161. $this->assertFalse( get_current_screen()->in_admin( 'user' ) );
  162. set_current_screen( 'dashboard-user' );
  163. $this->assertTrue( get_current_screen()->in_admin() );
  164. $this->assertFalse( get_current_screen()->in_admin( 'site' ) );
  165. $this->assertFalse( get_current_screen()->in_admin( 'network' ) );
  166. $this->assertTrue( get_current_screen()->in_admin( 'user' ) );
  167. set_current_screen( 'front' );
  168. $this->assertFalse( get_current_screen()->in_admin() );
  169. $this->assertFalse( get_current_screen()->in_admin( 'site' ) );
  170. $this->assertFalse( get_current_screen()->in_admin( 'network' ) );
  171. $this->assertFalse( get_current_screen()->in_admin( 'user' ) );
  172. $GLOBALS['current_screen'] = $screen;
  173. }
  174. }