Skip to:
Content

BuddyPress.org

Ticket #6503: 6503.fix-bp-nav-w-test.patch

File 6503.fix-bp-nav-w-test.patch, 3.9 KB (added by dcavins, 9 years ago)

Fix error introduced in r10003 that prevents access-protected nav items from being added to the bp_nav array. The access protection should happen at access time in bp_core_register_nav_screen_function().

  • src/bp-core/bp-core-buddybar.php

    diff --git src/bp-core/bp-core-buddybar.php src/bp-core/bp-core-buddybar.php
    index aba16c5..08c3bca 100644
    function bp_core_new_nav_item( $args = '' ) { 
    5656        }
    5757
    5858        // Then, hook the screen function for the added nav item.
    59         bp_core_register_nav_screen_function( $r );
     59        $hooked = bp_core_register_nav_screen_function( $r );
     60        if ( false === $hooked ){
     61                return false;
     62        }
    6063
    6164        /**
    6265         * Fires after adding an item to the main BuddyPress navigation array.
    function bp_core_create_nav_link( $args = '' ) { 
    121124                return false;
    122125        }
    123126
    124         /**
    125          * If this nav item is hidden for the displayed user, and
    126          * the logged in user is not the displayed user
    127          * looking at their own profile, don't create the nav item.
    128          */
    129         if ( empty( $r['show_for_displayed_user'] ) && ! bp_user_has_access() ) {
    130                 return false;
    131         }
    132 
    133127        if ( empty( $r['item_css_id'] ) ) {
    134128                $r['item_css_id'] = $r['slug'];
    135129        }
    function bp_core_new_subnav_item( $args = '' ) { 
    414408        }
    415409
    416410        // Then, hook the screen function for the added subnav item.
    417         bp_core_register_subnav_screen_function( $args );
    418 
     411        $hooked = bp_core_register_subnav_screen_function( $args );
     412        if ( false === $hooked ) {
     413                return false;
     414        }
    419415}
    420416
    421417/**
  • tests/phpunit/testcases/core/nav/bpCoreNewNavItem.php

    diff --git tests/phpunit/testcases/core/nav/bpCoreNewNavItem.php tests/phpunit/testcases/core/nav/bpCoreNewNavItem.php
    index 16b6034..b6f6439 100644
    class BP_Tests_Core_Nav_BpCoreNewNavItem extends BP_UnitTestCase { 
    124124                $this->assertFalse( $retval );
    125125        }
    126126
     127        public function test_existence_of_access_protected_user_nav() {
     128                $bp_nav = buddypress()->bp_nav;
     129
     130                $u = $this->factory->user->create();
     131                $u2 = $this->factory->user->create();
     132                $old_current_user = get_current_user_id();
     133                $this->set_current_user( $u2 );
     134
     135                $this->go_to( bp_core_get_user_domain( $u ) );
     136
     137                $expected = array(
     138                        'name'                    => 'Settings',
     139                        'slug'                    => 'settings',
     140                        'link'                    => trailingslashit( bp_loggedin_user_domain() . 'settings' ),
     141                        'css_id'                  => 'settings',
     142                        'show_for_displayed_user' => false,
     143                        'position'                => 100,
     144                        'screen_function'         => 'bp_settings_screen_general',
     145                        'default_subnav_slug'     => 'general'
     146                );
     147
     148                $this->assertSame( buddypress()->bp_nav['settings'], $expected );
     149
     150                // Clean up
     151                buddypress()->bp_nav = $bp_nav;
     152                $this->set_current_user( $old_current_user );
     153        }
     154
     155        public function test_creation_of_access_protected_user_nav() {
     156                // The nav item must be added to bp_nav, even if the current user
     157                // can't visit that nav item.
     158                $bp_nav = buddypress()->bp_nav;
     159
     160                $u = $this->factory->user->create();
     161                $u2 = $this->factory->user->create();
     162                $old_current_user = get_current_user_id();
     163                $this->set_current_user( $u2 );
     164
     165                $this->go_to( bp_core_get_user_domain( $u ) );
     166
     167                bp_core_new_nav_item( array(
     168                        'name'                    => 'Woof',
     169                        'slug'                    => 'woof',
     170                        'show_for_displayed_user' => false,
     171                        'position'                => 35,
     172                        'screen_function'         => 'woof_screen_function',
     173                        'default_subnav_slug'     => 'woof-one'
     174                ) );
     175
     176                $expected = array(
     177                        'name'                    => 'Woof',
     178                        'slug'                    => 'woof',
     179                        'link'                    => trailingslashit( bp_loggedin_user_domain() . 'woof' ),
     180                        'css_id'                  => 'woof',
     181                        'show_for_displayed_user' => false,
     182                        'position'                => 35,
     183                        'screen_function'         => 'woof_screen_function',
     184                        'default_subnav_slug'     => 'woof-one'
     185                );
     186
     187                $this->assertSame( buddypress()->bp_nav['woof'], $expected );
     188
     189                // Clean up
     190                buddypress()->bp_nav = $bp_nav;
     191                $this->set_current_user( $old_current_user );
     192        }
    127193}