tainacan/tests/test-api-roles.php

166 lines
4.4 KiB
PHP
Raw Normal View History

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-12 02:14:18 +00:00
/**
* just while we dont refactor capabilities
*/
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
$role = get_role('administrator');
$role->add_cap('edit_tainacan_users');
global $current_user;
$current_user->get_role_caps();
}
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-10-15 20:15:17 +00:00
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/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',
'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'];
$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
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/new-role');
$request->set_query_params(
[
'add_cap' => 'manage_options'
]
);
$response = $this->server->dispatch($request);
$this->assertEquals( 400, $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']);
}
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() );
$request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/new-role');
$request->set_query_params(
[
'name' => 'Changed name',
'add_cap' => 'tnc_col_12_edit'
]
);
$response = $this->server->dispatch($request);
$this->assertEquals( 200, $response->get_status() );
$role = \wp_roles()->roles['tainacan-new-role'];
$this->assertArrayHasKey('tnc_col_12_edit', $role['capabilities']);
$this->assertTrue($role['capabilities']['tnc_col_12_edit']);
$this->assertArrayHasKey('upload_files', $role['capabilities']);
$this->assertTrue($role['capabilities']['upload_files']);
2019-10-12 02:14:18 +00:00
}
}
?>