Skip to content

Commit

Permalink
Implement create capability
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Apr 17, 2024
1 parent 034b285 commit 4464bcb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 6 additions & 2 deletions includes/event/event-capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wporg\TranslationEvents\Event;

use GP;
use WP_User;

class Event_Capabilities {
Expand All @@ -21,8 +22,11 @@ private function has_cap( string $cap, array $args, WP_User $user ): bool {
}

private function has_create( WP_User $user ): bool {
// TODO.
return true;
return $this->has_gp_crud( $user );
}

private function has_gp_crud( WP_User $user ): bool {
return apply_filters( 'gp_translation_events_can_crud_event', GP::$permission->user_can( $user, 'admin' ) );
}

public function register_hooks(): void {
Expand Down
18 changes: 17 additions & 1 deletion tests/event/event-capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ public function setUp(): void {
$this->stats_factory = new Stats_Factory();
$this->attendee_repository = new Attendee_Repository();
$this->event_repository = new Event_Repository( $this->attendee_repository );
$this->capilities = new Event_Capabilities();
}

public function test_cannot_create_if_no_crud_permission() {
$this->set_normal_user_as_current();

add_filter( 'gp_translation_events_can_crud_event', '__return_false' );

$this->assertFalse( current_user_can( 'create_translation_event' ) );

Check warning on line 26 in tests/event/event-capabilities.php

View workflow job for this annotation

GitHub Actions / phpcs

Found unknown capability "create_translation_event" in function call to current_user_can(). Please check the spelling of the capability. If this is a custom capability, please verify the capability is registered with WordPress via a call to WP_Role(s)->add_cap(). Custom capabilities can be made known to this sniff by setting the "custom_capabilities" property in the PHPCS ruleset.
}

public function test_can_create_if_crud_permission() {
$this->set_normal_user_as_current();
get_current_user_id();

add_filter( 'gp_translation_events_can_crud_event', '__return_true' );

$this->assertTrue( current_user_can( 'create_translation_event' ) );

Check warning on line 35 in tests/event/event-capabilities.php

View workflow job for this annotation

GitHub Actions / phpcs

Found unknown capability "create_translation_event" in function call to current_user_can(). Please check the spelling of the capability. If this is a custom capability, please verify the capability is registered with WordPress via a call to WP_Role(s)->add_cap(). Custom capabilities can be made known to this sniff by setting the "custom_capabilities" property in the PHPCS ruleset.
}
}

0 comments on commit 4464bcb

Please sign in to comment.