2019-10-12 02:14:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tainacan\Tests;
|
|
|
|
|
|
|
|
/**
|
2019-11-01 17:35:56 +00:00
|
|
|
* @group api
|
2019-10-12 02:14:18 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase {
|
2019-11-01 17:35:56 +00:00
|
|
|
|
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
// reset WP_Roles object. Possible bug was cleaning database between tests, but not the object
|
|
|
|
global $wpdb;
|
|
|
|
wp_roles()->roles = get_option($wpdb->prefix . 'user_roles');
|
|
|
|
wp_roles()->init_roles();
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:14:18 +00:00
|
|
|
public function test_create_get_roles() {
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-12 02:14:18 +00:00
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
|
|
|
|
|
|
|
|
$request->set_query_params(['name' => 'New role']);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-12 02:14:18 +00:00
|
|
|
$create = $this->server->dispatch($request);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-12 02:14:18 +00:00
|
|
|
$this->assertEquals( 201, $create->get_status() );
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-12 02:14:18 +00:00
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/roles');
|
|
|
|
|
|
|
|
//$request->set_query_params($name_query);
|
|
|
|
|
|
|
|
$name_response = $this->server->dispatch($request);
|
|
|
|
$data = $name_response->get_data();
|
|
|
|
$this->assertArrayHasKey('tainacan-new-role', $data);
|
|
|
|
$this->assertEquals('New role', $data['tainacan-new-role']['name']);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
}
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
public function test_create_remove_roles() {
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
|
|
|
|
|
|
|
|
$request->set_query_params(['name' => 'Super role']);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$create = $this->server->dispatch($request);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$this->assertEquals( 201, $create->get_status() );
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$request = new \WP_REST_Request('DELETE', $this->namespace . '/roles/tainacan-super-role');
|
2019-10-12 02:14:18 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$delete_response = $this->server->dispatch($request);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$this->assertEquals( 200, $delete_response->get_status() );
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/roles');
|
|
|
|
|
|
|
|
$name_response = $this->server->dispatch($request);
|
|
|
|
$data = $name_response->get_data();
|
|
|
|
$this->assertArrayNotHasKey('tainacan-super-role', $data);
|
|
|
|
|
2019-11-01 17:35:56 +00:00
|
|
|
|
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
}
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
public function test_edit_role() {
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
|
|
|
|
|
|
|
|
$request->set_query_params(['name' => 'New role']);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$create = $this->server->dispatch($request);
|
|
|
|
//var_dump($create);
|
|
|
|
$this->assertEquals( 201, $create->get_status() );
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$request->set_query_params(
|
|
|
|
[
|
|
|
|
'name' => 'Changed name',
|
2019-11-01 17:49:14 +00:00
|
|
|
'add_cap' => 'tnc_rep_edit_collections'
|
2019-10-15 20:15:17 +00:00
|
|
|
]
|
|
|
|
);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$response = $this->server->dispatch($request);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$role = \wp_roles()->roles['tainacan-new-role'];
|
2019-11-01 17:49:14 +00:00
|
|
|
$this->assertArrayHasKey('tnc_rep_edit_collections', $role['capabilities']);
|
|
|
|
$this->assertTrue($role['capabilities']['tnc_rep_edit_collections']);
|
2019-10-15 20:15:17 +00:00
|
|
|
$this->assertEquals('Changed name', $role['name']);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
|
2019-11-01 17:49:14 +00:00
|
|
|
|
|
|
|
$request->set_query_params(
|
|
|
|
[
|
|
|
|
'add_cap' => 'manage_options'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
|
|
|
|
|
|
|
|
$request->set_query_params(
|
|
|
|
[
|
|
|
|
'add_cap' => 'manage_tainacan_collection_234'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
}
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
public function test_get_role() {
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/roles/administrator');
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$response = $this->server->dispatch($request);
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$data = $response->get_data();
|
2019-11-01 17:35:56 +00:00
|
|
|
|
2019-10-15 20:15:17 +00:00
|
|
|
$this->assertEquals( translate_user_role('Administrator'), $data['name'] );
|
2019-11-01 17:35:56 +00:00
|
|
|
$this->assertArrayHasKey('manage_options', $data['capabilities']);
|
|
|
|
$this->assertTrue($data['capabilities']['manage_options']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-05 14:50:57 +00:00
|
|
|
public function test_get_roles() {
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/roles');
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
foreach (\tainacan_roles()->get_tainacan_roles() as $role => $r) {
|
|
|
|
$this->assertArrayHasKey($role, $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:35:56 +00:00
|
|
|
public function test_add_dependencies() {
|
|
|
|
$request = new \WP_REST_Request('POST', $this->namespace . '/roles');
|
|
|
|
|
|
|
|
$request->set_query_params(['name' => 'New role']);
|
|
|
|
|
|
|
|
$create = $this->server->dispatch($request);
|
|
|
|
//var_dump($create);
|
|
|
|
$this->assertEquals( 201, $create->get_status() );
|
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role');
|
2019-11-01 17:35:56 +00:00
|
|
|
|
|
|
|
$request->set_query_params(
|
|
|
|
[
|
|
|
|
'name' => 'Changed name',
|
2019-11-04 15:10:47 +00:00
|
|
|
'add_cap' => 'tnc_col_12_edit_items'
|
2019-11-01 17:35:56 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
|
|
|
|
$role = \wp_roles()->roles['tainacan-new-role'];
|
|
|
|
|
2019-11-04 15:10:47 +00:00
|
|
|
$this->assertArrayHasKey('tnc_col_12_edit_items', $role['capabilities']);
|
|
|
|
$this->assertTrue($role['capabilities']['tnc_col_12_edit_items']);
|
2019-11-01 17:35:56 +00:00
|
|
|
$this->assertArrayHasKey('upload_files', $role['capabilities']);
|
|
|
|
$this->assertTrue($role['capabilities']['upload_files']);
|
2019-10-12 02:14:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
public function test_get_collection_caps() {
|
2019-11-04 18:38:04 +00:00
|
|
|
|
|
|
|
$collection = $this->tainacan_entity_factory->create_entity(
|
|
|
|
'collection',
|
|
|
|
array(
|
|
|
|
'name' => 'teste',
|
|
|
|
'status' => 'publish'
|
|
|
|
),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
$editor = get_role('editor');
|
|
|
|
$contributor = get_role('contributor');
|
|
|
|
$author = get_role('author');
|
|
|
|
|
|
|
|
$editor->add_cap('manage_tainacan_collection_' . $collection->get_id());
|
|
|
|
|
|
|
|
$author->add_cap( 'tnc_col_' . $collection->get_id() . '_edit_items' );
|
|
|
|
$author->add_cap( 'tnc_col_' . $collection->get_id() . '_edit_metadata' );
|
|
|
|
$author->add_cap( 'tnc_col_' . $collection->get_id() . '_edit_filters' );
|
|
|
|
|
|
|
|
$contributor->add_cap( 'tnc_col_all_edit_items' );
|
|
|
|
$contributor->add_cap( 'tnc_col_all_edit_published_items' );
|
|
|
|
|
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/capabilities');
|
2019-11-04 18:38:04 +00:00
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
|
|
|
|
$caps = $response->get_data()['capabilities'];
|
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$this->assertArrayHasKey('editor', $caps['manage_tainacan_collection_' . $collection->get_id()]['roles']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('author', $caps['tnc_col_' . $collection->get_id() . '_edit_items']['roles']);
|
|
|
|
$this->assertArrayHasKey('author', $caps['tnc_col_' . $collection->get_id() . '_edit_metadata']['roles']);
|
|
|
|
$this->assertArrayHasKey('author', $caps['tnc_col_' . $collection->get_id() . '_edit_filters']['roles']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('contributor', $caps['tnc_col_' . $collection->get_id() . '_edit_items']['roles_inherited']);
|
|
|
|
$this->assertArrayHasKey('contributor', $caps['tnc_col_' . $collection->get_id() . '_edit_published_items']['roles_inherited']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('administrator', $caps['tnc_col_' . $collection->get_id() . '_delete_published_items']['roles_inherited']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_get_repo_capabilities() {
|
|
|
|
|
|
|
|
$role = add_role('test', 'test', ['tnc_rep_edit_metadata'=>true]);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('GET', $this->namespace . '/capabilities');
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
|
|
|
|
$caps = $response->get_data()['capabilities'];
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('editor', $caps['manage_tainacan']['roles']);
|
|
|
|
$this->assertArrayHasKey('administrator', $caps['manage_tainacan']['roles']);
|
|
|
|
$this->assertArrayHasKey('test', $caps['tnc_rep_edit_metadata']['roles']);
|
|
|
|
$this->assertArrayHasKey('editor', $caps['tnc_rep_edit_metadata']['roles_inherited']);
|
|
|
|
|
2019-11-11 17:57:30 +00:00
|
|
|
$this->assertArrayNotHasKey('editor', $caps['manage_tainacan']['roles_inherited']);
|
|
|
|
$this->assertArrayNotHasKey('administrator', $caps['manage_tainacan']['roles_inherited']);
|
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_edit_collection_users_permission() {
|
|
|
|
|
|
|
|
global $current_user;
|
|
|
|
$subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' ));
|
|
|
|
|
|
|
|
wp_set_current_user($subscriber);
|
|
|
|
$sub_user = get_userdata( $subscriber );
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
|
|
|
|
|
|
|
|
$request->set_query_params(
|
|
|
|
[
|
|
|
|
'name' => 'Changed name',
|
|
|
|
'add_cap' => 'tnc_col_12_edit_items'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals( 403, $response->get_status(), 'should not be permitted');
|
|
|
|
|
|
|
|
$sub_user->add_cap('tnc_col_12_edit_users');
|
|
|
|
$current_user = $sub_user;
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
|
|
|
|
|
|
|
|
$request->set_query_params(
|
|
|
|
[
|
|
|
|
'name' => 'Changed name',
|
|
|
|
'add_cap' => 'tnc_col_12_edit_items'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
2019-11-04 18:38:04 +00:00
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$this->assertEquals( 403, $response->get_status(), 'should still not be permitted because edits name');
|
2019-11-04 18:38:04 +00:00
|
|
|
|
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
|
|
|
|
|
|
|
|
$request->set_query_params(
|
|
|
|
[
|
|
|
|
'add_cap' => 'tnc_rep_edit_metadata'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
|
|
|
|
|
|
|
$this->assertEquals( 403, $response->get_status(), 'should not be permitted');
|
|
|
|
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor');
|
|
|
|
|
|
|
|
$request->set_query_params(
|
|
|
|
[
|
|
|
|
'add_cap' => 'tnc_col_12_edit_items'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = $this->server->dispatch($request);
|
2019-11-04 18:38:04 +00:00
|
|
|
|
2019-11-05 20:07:38 +00:00
|
|
|
$this->assertEquals( 200, $response->get_status(), 'should be permitted');
|
2019-11-04 18:38:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|