Skip to content

Commit

Permalink
Users: Add basic event caps for users with the 'subscriber' or 'contr…
Browse files Browse the repository at this point in the history
…ibutor' role.

Commit also includes unit tests.

See #5.
  • Loading branch information
r-a-y committed Apr 6, 2015
1 parent 8cce0ca commit 946cfd9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
27 changes: 27 additions & 0 deletions includes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@ function bpeo_get_my_calendar_event_ids( $user_id ) {
return $event_ids;
}

/**
* Add EO capabilities for subscribers and contributors.
*
* By default, subscribers and contributors do not have caps to post, edit or
* delete events. This function injects these caps for users with these roles.
*
* @param array $allcaps An array of all the user's capabilities.
* @param array $caps Actual capabilities for meta capability.
* @param array $args Optional parameters passed to has_cap(), typically object ID.
* @param WP_User $user The user object.
*/
function bpeo_user_has_cap( $allcaps, $caps, $args, $user ) {
// check if current user has the 'subscriber' or 'contributor' role
$is_role = array_intersect_key( array( 'subscriber' => 1, 'contributor' => 1 ), $user->caps );
if ( empty( $is_role ) ) {
return $allcaps;
}

// add our basic event caps
$allcaps['publish_events'] = 1;
$allcaps['edit_events'] = 1;
$allcaps['delete_events'] = 1;

return $allcaps;
}
add_filter( 'user_has_cap', 'bpeo_user_has_cap', 20, 4 );

/**
* Modify `WP_Query` requests for the 'bp_displayed_user_id' param.
*
Expand Down
45 changes: 45 additions & 0 deletions tests/phpunit/tests/user/bpeoEventMetaCap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @group user
* @group cap
*/
class BPEO_Tests_User_BpeoEventMetaCap extends BPEO_UnitTestCase {
protected $current_user;
protected $user;

public function setUp() {
parent::setUp();
$this->current_user = bp_loggedin_user_id();
}

public function tearDown() {
$this->set_current_user( $this->current_user );
}

public function test_non_loggedin_user_can_publish_events() {
$this->set_current_user( 0 );
$this->assertFalse( current_user_can( 'publish_events' ) );
}

public function test_loggedin_user_can_publish_events() {
$this->user = $this->factory->user->create();
$this->set_current_user( $this->user );

$this->assertTrue( current_user_can( 'publish_events' ) );
}

public function test_loggedin_user_can_edit_events() {
$this->user = $this->factory->user->create();
$this->set_current_user( $this->user );

$this->assertTrue( current_user_can( 'edit_events' ) );
}

public function test_loggedin_user_can_delete_events() {
$this->user = $this->factory->user->create();
$this->set_current_user( $this->user );

$this->assertTrue( current_user_can( 'delete_events' ) );
}
}

0 comments on commit 946cfd9

Please sign in to comment.