[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @group core 4 * @group nav 5 */ 6 class BP_Tests_Core_Nav_BpCoreNewSubnavItem extends BP_UnitTestCase { 7 8 /** 9 * @expectedIncorrectUsage bp_nav 10 */ 11 public function test_user_subnav() { 12 $bp_options_nav = buddypress()->bp_options_nav; 13 14 $u = self::factory()->user->create(); 15 $old_current_user = get_current_user_id(); 16 $this->set_current_user( $u ); 17 18 $user_domain = bp_core_get_user_domain( $u ); 19 20 $this->go_to( $user_domain ); 21 22 bp_core_new_nav_item( array( 23 'name' => 'Foo Parent', 24 'slug' => 'foo-parent', 25 'link' => trailingslashit( $user_domain . 'foo-parent' ), 26 'screen_function' => 'foo_screen_function', 27 'position' => 10, 28 ) ); 29 30 bp_core_new_subnav_item( array( 31 'name' => 'Foo', 32 'slug' => 'foo', 33 'parent_url' => trailingslashit( $user_domain . 'foo-parent' ), 34 'parent_slug' => 'foo-parent', 35 'screen_function' => 'foo_screen_function', 36 'position' => 10 37 ) ); 38 39 $expected = array( 40 'name' => 'Foo', 41 'link' => trailingslashit( $user_domain . 'foo-parent/foo' ), 42 'slug' => 'foo', 43 'css_id' => 'foo', 44 'position' => 10, 45 'user_has_access' => true, 46 'no_access_url' => '', 47 'screen_function' => 'foo_screen_function', 48 'show_in_admin_bar' => false, 49 ); 50 51 foreach ( $expected as $k => $v ) { 52 $this->assertSame( $v, buddypress()->bp_options_nav['foo-parent']['foo'][ $k ] ); 53 } 54 55 // Clean up 56 buddypress()->bp_options_nav = $bp_options_nav; 57 $this->set_current_user( $old_current_user ); 58 } 59 60 public function test_required_params() { 61 // 'name' 62 $this->assertFalse( bp_core_new_subnav_item( array( 63 'slug' => 'foo', 64 'parent_slug' => 'foo', 65 'parent_url' => 'foo', 66 'screen_function' => 'foo', 67 ) ) ); 68 69 // 'slug' 70 $this->assertFalse( bp_core_new_subnav_item( array( 71 'name' => 'foo', 72 'parent_slug' => 'foo', 73 'parent_url' => 'foo', 74 'screen_function' => 'foo', 75 ) ) ); 76 77 // 'parent_slug' 78 $this->assertFalse( bp_core_new_subnav_item( array( 79 'name' => 'foo', 80 'slug' => 'foo', 81 'parent_url' => 'foo', 82 'screen_function' => 'foo', 83 ) ) ); 84 85 // 'parent_url' 86 $this->assertFalse( bp_core_new_subnav_item( array( 87 'name' => 'foo', 88 'slug' => 'foo', 89 'parent_slug' => 'foo', 90 'screen_function' => 'foo', 91 ) ) ); 92 93 // 'screen_function' 94 $this->assertFalse( bp_core_new_subnav_item( array( 95 'name' => 'foo', 96 'slug' => 'foo', 97 'parent_slug' => 'foo', 98 'parent_url' => 'foo', 99 ) ) ); 100 } 101 102 public function test_site_admin_only() { 103 $old_current_user = get_current_user_id(); 104 $this->set_current_user( 0 ); 105 106 $this->assertFalse( bp_core_new_subnav_item( array( 107 'name' => 'foo', 108 'slug' => 'foo', 109 'parent_slug' => 'foo', 110 'parent_url' => 'foo', 111 'screen_function' => 'foo', 112 'site_admin_only' => true, 113 ) ) ); 114 115 $this->set_current_user( $old_current_user ); 116 } 117 118 /** 119 * @expectedIncorrectUsage bp_nav 120 */ 121 public function test_link_provided() { 122 $bp_options_nav = buddypress()->bp_options_nav; 123 124 bp_core_new_nav_item( array( 125 'name' => 'Foo', 126 'slug' => 'foo', 127 'screen_function' => 'foo', 128 'link' => 'https://buddypress.org/', 129 ) ); 130 131 bp_core_new_subnav_item( array( 132 'name' => 'bar', 133 'slug' => 'bar', 134 'parent_slug' => 'foo', 135 'parent_url' => 'foo', 136 'screen_function' => 'foo', 137 'link' => 'https://buddypress.org/', 138 ) ); 139 140 $this->assertSame( 'https://buddypress.org/', buddypress()->bp_options_nav['foo']['bar']['link'] ); 141 142 buddypress()->bp_options_nav = $bp_options_nav; 143 } 144 145 /** 146 * @expectedIncorrectUsage bp_nav 147 */ 148 public function test_link_built_from_parent_url_and_slug() { 149 $bp_options_nav = buddypress()->bp_options_nav; 150 151 bp_core_new_nav_item( array( 152 'name' => 'Foo', 153 'slug' => 'foo', 154 'screen_function' => 'foo', 155 'link' => 'https://buddypress.org/', 156 ) ); 157 158 bp_core_new_subnav_item( array( 159 'name' => 'bar', 160 'slug' => 'bar', 161 'parent_slug' => 'foo', 162 'parent_url' => 'http://example.com/foo/', 163 'screen_function' => 'foo', 164 ) ); 165 166 $this->assertSame( 'http://example.com/foo/bar/', buddypress()->bp_options_nav['foo']['bar']['link'] ); 167 168 buddypress()->bp_options_nav = $bp_options_nav; 169 } 170 171 /** 172 * @expectedIncorrectUsage bp_nav 173 */ 174 public function test_link_built_from_parent_url_and_slug_where_slug_is_default() { 175 $bp_nav = buddypress()->bp_nav; 176 $bp_options_nav = buddypress()->bp_options_nav; 177 178 bp_core_new_nav_item( array( 179 'name' => 'Foo', 180 'slug' => 'foo', 181 'screen_function' => 'foo', 182 'default_subnav_slug' => 'bar', 183 ) ); 184 185 bp_core_new_subnav_item( array( 186 'name' => 'bar', 187 'slug' => 'bar', 188 'parent_slug' => 'foo', 189 'parent_url' => 'http://example.com/foo/', 190 'screen_function' => 'foo', 191 ) ); 192 193 $this->assertSame( 'http://example.com/foo/', buddypress()->bp_options_nav['foo']['bar']['link'] ); 194 195 // clean up 196 buddypress()->bp_nav = $bp_nav; 197 buddypress()->bp_options_nav = $bp_options_nav; 198 } 199 200 /** 201 * @expectedIncorrectUsage bp_nav 202 */ 203 public function test_should_trailingslash_link_when_link_is_autogenerated_using_slug() { 204 bp_core_new_nav_item( array( 205 'name' => 'Foo', 206 'slug' => 'foo', 207 'screen_function' => 'foo', 208 'link' => 'https://buddypress.org/', 209 ) ); 210 211 bp_core_new_subnav_item( array( 212 'name' => 'bar', 213 'slug' => 'bar', 214 'parent_slug' => 'foo', 215 'parent_url' => bp_get_root_domain() . 'foo/', 216 'screen_function' => 'foo', 217 ) ); 218 219 $expected = bp_get_root_domain() . 'foo/bar/'; 220 $this->assertSame( $expected, buddypress()->bp_options_nav['foo']['bar']['link'] ); 221 } 222 223 /** 224 * @expectedIncorrectUsage bp_nav 225 */ 226 public function test_should_trailingslash_link_when_link_is_autogenerated_not_using_slug() { 227 bp_core_new_nav_item( array( 228 'name' => 'foo', 229 'slug' => 'foo-parent', 230 'link' => bp_get_root_domain() . 'foo-parent/', 231 'default_subnav_slug' => 'bar', 232 'screen_function' => 'foo', 233 ) ); 234 235 bp_core_new_subnav_item( array( 236 'name' => 'bar', 237 'slug' => 'bar', 238 'parent_slug' => 'foo-parent', 239 'parent_url' => bp_get_root_domain() . '/foo-parent/', 240 'screen_function' => 'bar', 241 ) ); 242 243 $expected = bp_get_root_domain() . '/foo-parent/'; 244 $this->assertSame( $expected, buddypress()->bp_options_nav['foo-parent']['bar']['link'] ); 245 } 246 247 /** 248 * @ticket BP6353 249 * @expectedIncorrectUsage bp_nav 250 */ 251 public function test_link_should_not_trailingslash_link_explicit_link() { 252 $link = 'http://example.com/foo/bar/blah/?action=edit&id=30'; 253 254 bp_core_new_nav_item( array( 255 'name' => 'Foo', 256 'slug' => 'foo', 257 'screen_function' => 'foo', 258 'link' => 'http://example.com/foo/', 259 ) ); 260 261 bp_core_new_subnav_item( array( 262 'name' => 'bar', 263 'slug' => 'bar', 264 'parent_slug' => 'foo', 265 'parent_url' => 'http://example.com/foo/', 266 'screen_function' => 'foo', 267 'link' => $link, 268 ) ); 269 270 $this->assertSame( $link, buddypress()->bp_options_nav['foo']['bar']['link'] ); 271 } 272 273 public function test_should_return_false_if_site_admin_only_and_current_user_cannot_bp_moderate() { 274 bp_core_new_nav_item( array( 275 'name' => 'Foo', 276 'slug' => 'foo', 277 'screen_function' => 'foo', 278 ) ); 279 280 // Should already be set to a 0 user. 281 $this->assertFalse( bp_current_user_can( 'bp_moderate' ) ); 282 $args = array( 283 'name' => 'Foo', 284 'slug' => 'foo', 285 'parent_slug' => 'parent', 286 'parent_url' => bp_get_root_domain() . '/parent/', 287 'screen_function' => 'foo', 288 'site_admin_only' => true, 289 ); 290 291 $this->assertFalse( bp_core_new_subnav_item( $args ) ); 292 } 293 294 /** 295 * @expectedIncorrectUsage bp_nav 296 */ 297 public function test_css_id_should_fall_back_on_slug() { 298 bp_core_new_nav_item( array( 299 'name' => 'Parent', 300 'slug' => 'parent', 301 'screen_function' => 'foo', 302 ) ); 303 304 $args = array( 305 'name' => 'Foo', 306 'slug' => 'foo', 307 'parent_slug' => 'parent', 308 'parent_url' => bp_get_root_domain() . '/parent/', 309 'screen_function' => 'foo', 310 ); 311 bp_core_new_subnav_item( $args ); 312 313 $this->assertSame( 'foo', buddypress()->bp_options_nav['parent']['foo']['css_id'] ); 314 } 315 316 /** 317 * @expectedIncorrectUsage bp_nav 318 */ 319 public function test_css_id_should_be_respected() { 320 bp_core_new_nav_item( array( 321 'name' => 'Parent', 322 'slug' => 'parent', 323 'screen_function' => 'foo', 324 ) ); 325 326 $args = array( 327 'name' => 'Foo', 328 'slug' => 'foo', 329 'parent_slug' => 'parent', 330 'parent_url' => bp_get_root_domain() . '/parent/', 331 'screen_function' => 'foo', 332 'item_css_id' => 'bar', 333 ); 334 bp_core_new_subnav_item( $args ); 335 336 $this->assertSame( 'bar', buddypress()->bp_options_nav['parent']['foo']['css_id'] ); 337 } 338 339 /** 340 * @ticket BP7931 341 */ 342 public function test_subnav_should_not_404_on_early_bp_setup_nav_priority() { 343 $u = self::factory()->user->create(); 344 $old_current_user = get_current_user_id(); 345 $this->set_current_user( $u ); 346 347 $user_domain = bp_core_get_user_domain( $u ); 348 349 // Register a subnav on 'bp_setup_nav' hook early (at priority zero). 350 add_action( 'bp_setup_nav', function() use ( $user_domain ) { 351 /* 352 * Emulate a subnav screen. 353 * 354 * The bp_core_load_template() call is imperative for our 404 check to work! 355 */ 356 $screen = function() { 357 bp_core_load_template ('members/single/plugins'); 358 }; 359 360 // Register the subnav. 361 bp_core_new_subnav_item( array ( 362 'name' => 'Testing', 363 'slug' => 'testing', 364 'parent_url' => $user_domain . bp_get_profile_slug (). '/', 365 'parent_slug' => bp_get_profile_slug (), 366 'screen_function' => $screen, 367 'position' => 20 368 ) ); 369 }, 0 ); 370 371 // Emulate visit to our new subnav page. 372 $this->go_to( $user_domain . bp_get_profile_slug () . '/testing/' ); 373 374 // Assert that subnav page does not 404. 375 $this->assertFalse( is_404() ); 376 377 $this->set_current_user( $old_current_user ); 378 } 379 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Dec 22 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |