Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions modules/checklists/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ public function parse_lists_in_comment( $content, $comment = null ) {
return $this->parse_lists( $content );
}

function current_user_can_edit_checklist( $object_type, $object_ID ) {
public function current_user_can_edit_checklist( $object_type, $object_id ) {
// no logged out edits, evar
$current_user_id = get_current_user_id();
if ( 0 == $current_user_id ) {
return false;
}

// try default post and comment capabilities
if ( ( 'post' == $object_type ) && ( current_user_can( 'edit_post', $object_ID ) ) ) {
return true;
} else if ( ( 'comment' == $object_type ) && ( current_user_can( 'edit_comment', $object_ID ) ) ) {
if ( ( 'post' == $object_type ) && ( current_user_can( 'edit_post', $object_id ) ) ) {
return true;
} else if ( 'comment' == $object_type ) {
// Apply o2's comment rule: authors always edit their own comments
add_filter( 'map_meta_cap', array( 'o2_Write_API', 'restrict_comment_editing' ), 10, 4 );
$user_can_edit_comment = current_user_can( 'edit_comment', $object_id );
remove_filter( 'map_meta_cap', array( 'o2_Write_API', 'restrict_comment_editing' ), 10 );
return $user_can_edit_comment;
}

return false;
Expand Down
79 changes: 79 additions & 0 deletions tests/phpunit/modules/test-checklists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

class o2ChecklistsTest extends WP_UnitTestCase {

private $list_creator;

function setUp(): void {
parent::setUp();
$this->list_creator = new o2_List_Creator();
}

private function create_comment_on_another_authors_post( $commenter_id ) {
$post_owner_id = $this->factory->user->create( array( 'role' => 'author' ) );
$post_id = $this->factory->post->create( array( 'post_author' => $post_owner_id ) );

return $this->factory->comment->create( array(
'comment_post_ID' => $post_id,
'user_id' => $commenter_id,
'comment_content' => 'o my task',
) );
}

function test_author_can_edit_checklist_in_own_comment_on_another_authors_post() {
$author_id = $this->factory->user->create( array( 'role' => 'author' ) );
$comment_id = $this->create_comment_on_another_authors_post( $author_id );

wp_set_current_user( $author_id );

$this->assertTrue(
$this->list_creator->current_user_can_edit_checklist( 'comment', $comment_id ),
'An author should be able to edit checklists in their own comment'
);

$rendered = $this->list_creator->parse_lists_in_comment( 'o my task', get_comment( $comment_id ) );

$this->assertStringNotContainsString( 'disabled', $rendered, 'The checkbox should be enabled' );
$this->assertStringContainsString( 'o2-task-tools', $rendered, 'The task tools should be rendered' );
}

function test_author_cannot_edit_checklist_in_another_users_comment() {
$author_id = $this->factory->user->create( array( 'role' => 'author' ) );
$other_author_id = $this->factory->user->create( array( 'role' => 'author' ) );
$comment_id = $this->create_comment_on_another_authors_post( $other_author_id );

wp_set_current_user( $author_id );

$this->assertFalse(
$this->list_creator->current_user_can_edit_checklist( 'comment', $comment_id ),
'An author should not be able to edit checklists in another user\'s comment'
);

$rendered = $this->list_creator->parse_lists_in_comment( 'o my task', get_comment( $comment_id ) );

$this->assertStringContainsString( 'disabled', $rendered, 'The checkbox should be disabled' );
$this->assertStringNotContainsString( 'o2-task-tools', $rendered, 'The task tools should not be rendered' );
}

function test_editor_can_edit_checklist_in_another_users_comment() {
$editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
$author_id = $this->factory->user->create( array( 'role' => 'author' ) );
$comment_id = $this->create_comment_on_another_authors_post( $author_id );

wp_set_current_user( $editor_id );

$this->assertTrue(
$this->list_creator->current_user_can_edit_checklist( 'comment', $comment_id ),
'An editor should be able to edit checklists in any comment'
);
}

function test_logged_out_user_cannot_edit_checklist_in_comment() {
$author_id = $this->factory->user->create( array( 'role' => 'author' ) );
$comment_id = $this->create_comment_on_another_authors_post( $author_id );

wp_set_current_user( 0 );

$this->assertFalse( $this->list_creator->current_user_can_edit_checklist( 'comment', $comment_id ) );
}
}
Loading