|
| 1 | +<?php |
| 2 | + |
| 3 | +class WP_MS_Test_REST_Networks_Controller extends WP_Test_REST_Controller_Testcase { |
| 4 | + protected static $superadmin_id; |
| 5 | + protected static $total_networks = 30; |
| 6 | + protected static $per_page = 50; |
| 7 | + protected static $network_id; |
| 8 | + |
| 9 | + public static function wpSetUpBeforeClass( $factory ) { |
| 10 | + self::$superadmin_id = $factory->user->create( |
| 11 | + array( |
| 12 | + 'role' => 'administrator', |
| 13 | + 'user_login' => 'superadmin', |
| 14 | + ) |
| 15 | + ); |
| 16 | + grant_super_admin( self::$superadmin_id ); |
| 17 | + |
| 18 | + // Set up networks for pagination tests. |
| 19 | + for ( $i = 0; $i < self::$total_networks - 1; $i++ ) { |
| 20 | + $network_ids[] = $factory->network->create(); |
| 21 | + } |
| 22 | + |
| 23 | + self::$network_id = $factory->network->create(); |
| 24 | + } |
| 25 | + |
| 26 | + public static function wpTearDownAfterClass() { |
| 27 | + self::delete_user( self::$superadmin_id ); |
| 28 | + } |
| 29 | + |
| 30 | + public function test_register_routes() { |
| 31 | + $routes = rest_get_server()->get_routes(); |
| 32 | + |
| 33 | + $this->assertArrayHasKey( '/wpmn/v1/networks', $routes ); |
| 34 | + $this->assertCount( 2, $routes['/wpmn/v1/networks'] ); |
| 35 | + $this->assertArrayHasKey( '/wpmn/v1/networks/(?P<id>[\d]+)', $routes ); |
| 36 | + $this->assertCount( 3, $routes['/wpmn/v1/networks/(?P<id>[\d]+)'] ); |
| 37 | + } |
| 38 | + |
| 39 | + public function test_context_param() { |
| 40 | + // Collection. |
| 41 | + $request = new WP_REST_Request( 'OPTIONS', '/wpmn/v1/networks' ); |
| 42 | + $response = rest_get_server()->dispatch( $request ); |
| 43 | + $data = $response->get_data(); |
| 44 | + $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); |
| 45 | + $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); |
| 46 | + // Single. |
| 47 | + $network = $this->factory->network->create(); |
| 48 | + $request = new WP_REST_Request( 'OPTIONS', '/wpmn/v1/networks/' . $network ); |
| 49 | + $response = rest_get_server()->dispatch( $request ); |
| 50 | + $data = $response->get_data(); |
| 51 | + $this->assertEquals( 'view', $data['endpoints'][0]['args']['context']['default'] ); |
| 52 | + $this->assertEquals( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); |
| 53 | + } |
| 54 | + |
| 55 | + public function test_registered_query_params() { |
| 56 | + $request = new WP_REST_Request( 'OPTIONS', '/wpmn/v1/networks' ); |
| 57 | + $response = rest_get_server()->dispatch( $request ); |
| 58 | + $data = $response->get_data(); |
| 59 | + $keys = array_keys( $data['endpoints'][0]['args'] ); |
| 60 | + sort( $keys ); |
| 61 | + $this->assertEquals( |
| 62 | + array( |
| 63 | + 'context', |
| 64 | + 'domain', |
| 65 | + 'domain_exclude', |
| 66 | + 'exclude', |
| 67 | + 'include', |
| 68 | + 'offset', |
| 69 | + 'order', |
| 70 | + 'orderby', |
| 71 | + 'page', |
| 72 | + 'path', |
| 73 | + 'path_exclude', |
| 74 | + 'per_page', |
| 75 | + 'search', |
| 76 | + ), |
| 77 | + $keys |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public function test_get_items() { |
| 83 | + wp_set_current_user( self::$superadmin_id ); |
| 84 | + $request = new WP_REST_Request( 'GET', '/wpmn/v1/networks' ); |
| 85 | + $request->set_param( 'per_page', self::$per_page ); |
| 86 | + |
| 87 | + $response = rest_get_server()->dispatch( $request ); |
| 88 | + $this->assertEquals( 200, $response->get_status() ); |
| 89 | + |
| 90 | + $networks = $response->get_data(); |
| 91 | + $this->assertCount( self::$total_networks + 1, $networks ); |
| 92 | + } |
| 93 | + |
| 94 | + public function test_get_item() { |
| 95 | + wp_set_current_user( self::$superadmin_id ); |
| 96 | + $request = new WP_REST_Request( 'GET', sprintf( '/wpmn/v1/networks/%d', self::$network_id ) ); |
| 97 | + |
| 98 | + $response = rest_get_server()->dispatch( $request ); |
| 99 | + $this->assertEquals( 200, $response->get_status() ); |
| 100 | + |
| 101 | + $data = $response->get_data(); |
| 102 | + $this->check_network_data( $data ); |
| 103 | + } |
| 104 | + |
| 105 | + public function test_prepare_item() { |
| 106 | + wp_set_current_user( self::$superadmin_id ); |
| 107 | + |
| 108 | + $request = new WP_REST_Request( 'GET', sprintf( '/wpmn/v1/networks/%d', self::$network_id ) ); |
| 109 | + $request->set_query_params( |
| 110 | + array( |
| 111 | + 'context' => 'edit', |
| 112 | + ) |
| 113 | + ); |
| 114 | + |
| 115 | + $response = rest_get_server()->dispatch( $request ); |
| 116 | + $this->assertEquals( 200, $response->get_status() ); |
| 117 | + |
| 118 | + $data = $response->get_data(); |
| 119 | + $this->check_network_data( $data ); |
| 120 | + } |
| 121 | + |
| 122 | + public function test_create_item() { |
| 123 | + wp_set_current_user( self::$superadmin_id ); |
| 124 | + |
| 125 | + $request = new WP_REST_Request( 'POST', '/wpmn/v1/networks' ); |
| 126 | + $request->set_param( 'domain', 'www.example.net' ); |
| 127 | + $request->set_param( 'path', '/network' ); |
| 128 | + $request->set_param( 'site_name', 'main-network' ); |
| 129 | + $response = rest_get_server()->dispatch( $request ); |
| 130 | + $data = $response->get_data(); |
| 131 | + |
| 132 | + $this->assertEquals( 201, $response->get_status() ); |
| 133 | + $this->check_network_data( $data ); |
| 134 | + $network = get_network( $data['id'] ); |
| 135 | + $this->assertEquals( $network->domain, 'www.example.net' ); |
| 136 | + $this->assertEquals( $network->path, '/network/' ); |
| 137 | + $this->assertEquals( $network->site_name, 'main-network' ); |
| 138 | + } |
| 139 | + |
| 140 | + public function test_update_item() { |
| 141 | + $network_id = $this->factory->network->create(); |
| 142 | + wp_set_current_user( self::$superadmin_id ); |
| 143 | + $request = new WP_REST_Request( 'POST', '/wpmn/v1/networks/' . $network_id ); |
| 144 | + $request->set_param( 'domain', 'www.example.co' ); |
| 145 | + $request->set_param( 'path', '/update' ); |
| 146 | + $response = rest_get_server()->dispatch( $request ); |
| 147 | + $data = $response->get_data(); |
| 148 | + $this->check_network_data( $data ); |
| 149 | + $network = get_network( $data['id'] ); |
| 150 | + $this->assertEquals( $network->domain, 'www.example.co' ); |
| 151 | + $this->assertEquals( $network->path, '/update/' ); |
| 152 | + } |
| 153 | + |
| 154 | + public function test_delete_item() { |
| 155 | + $network_id = $this->factory->network->create(); |
| 156 | + wp_set_current_user( self::$superadmin_id ); |
| 157 | + $request = new WP_REST_Request( 'DELETE', '/wpmn/v1/networks/' . $network_id ); |
| 158 | + $request['force'] = true; |
| 159 | + $response = rest_get_server()->dispatch( $request ); |
| 160 | + $this->assertEquals( 200, $response->get_status() ); |
| 161 | + } |
| 162 | + |
| 163 | + public function test_no_update_item() { |
| 164 | + $network_id = wp_rand(); |
| 165 | + wp_set_current_user( self::$superadmin_id ); |
| 166 | + $request = new WP_REST_Request( 'POST', '/wpmn/v1/networks/' . $network_id ); |
| 167 | + $request->set_param( 'domain', 'www.example.co' ); |
| 168 | + $request->set_param( 'path', '/update' ); |
| 169 | + $response = rest_get_server()->dispatch( $request ); |
| 170 | + $this->assertEquals( 404, $response->get_status() ); |
| 171 | + } |
| 172 | + |
| 173 | + public function test_no_delete_item() { |
| 174 | + $network_id = wp_rand(); |
| 175 | + wp_set_current_user( self::$superadmin_id ); |
| 176 | + $request = new WP_REST_Request( 'DELETE', '/wpmn/v1/networks/' . $network_id ); |
| 177 | + $request['force'] = true; |
| 178 | + $response = rest_get_server()->dispatch( $request ); |
| 179 | + $this->assertEquals( 404, $response->get_status() ); |
| 180 | + } |
| 181 | + |
| 182 | + public function test_get_item_schema() { |
| 183 | + $request = new WP_REST_Request( 'OPTIONS', '/wpmn/v1/networks' ); |
| 184 | + $response = rest_get_server()->dispatch( $request ); |
| 185 | + $data = $response->get_data(); |
| 186 | + $properties = $data['schema']['properties']; |
| 187 | + $this->assertEquals( 5, count( $properties ) ); |
| 188 | + $this->assertArrayHasKey( 'id', $properties ); |
| 189 | + $this->assertArrayHasKey( 'path', $properties ); |
| 190 | + $this->assertArrayHasKey( 'domain', $properties ); |
| 191 | + $this->assertArrayHasKey( 'site_id', $properties ); |
| 192 | + $this->assertArrayHasKey( 'link', $properties ); |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + /** |
| 198 | + * |
| 199 | + */ |
| 200 | + public function test_get_items_no_permission() { |
| 201 | + wp_set_current_user( 0 ); |
| 202 | + $request = new WP_REST_Request( 'GET', '/wpmn/v1/networks' ); |
| 203 | + $response = rest_get_server()->dispatch( $request ); |
| 204 | + $this->assertErrorResponse( 'rest_forbidden', $response, rest_authorization_required_code() ); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * |
| 209 | + */ |
| 210 | + public function test_get_item_no_permission() { |
| 211 | + wp_set_current_user( 0 ); |
| 212 | + $request = new WP_REST_Request( 'GET', '/wpmn/v1/networks/' . self::$network_id ); |
| 213 | + $response = rest_get_server()->dispatch( $request ); |
| 214 | + $this->assertErrorResponse( 'rest_forbidden', $response, rest_authorization_required_code() ); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * |
| 219 | + */ |
| 220 | + public function test_create_item_no_permission() { |
| 221 | + wp_set_current_user( 0 ); |
| 222 | + $request = new WP_REST_Request( 'POST', '/wpmn/v1/networks' ); |
| 223 | + $request->set_param( 'domain', 'www.example.net' ); |
| 224 | + $request->set_param( 'path', '/network' ); |
| 225 | + $response = rest_get_server()->dispatch( $request ); |
| 226 | + $this->assertErrorResponse( 'rest_forbidden', $response, rest_authorization_required_code() ); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * |
| 231 | + */ |
| 232 | + public function test_update_item_no_permission() { |
| 233 | + $network_id = $this->factory->network->create(); |
| 234 | + wp_set_current_user( 0 ); |
| 235 | + $request = new WP_REST_Request( 'POST', '/wpmn/v1/networks/' . $network_id ); |
| 236 | + $request->set_param( 'domain', 'www.example.co' ); |
| 237 | + $request->set_param( 'path', '/update' ); |
| 238 | + $response = rest_get_server()->dispatch( $request ); |
| 239 | + $this->assertErrorResponse( 'rest_cannot_edit', $response, rest_authorization_required_code() ); |
| 240 | + } |
| 241 | + |
| 242 | + public function test_delete_item_no_permission() { |
| 243 | + $network_id = $this->factory->network->create(); |
| 244 | + wp_set_current_user( 0 ); |
| 245 | + $request = new WP_REST_Request( 'DELETE', '/wpmn/v1/networks/' . $network_id ); |
| 246 | + $request['force'] = true; |
| 247 | + $response = rest_get_server()->dispatch( $request ); |
| 248 | + $this->assertErrorResponse( 'rest_forbidden', $response, rest_authorization_required_code() ); |
| 249 | + } |
| 250 | + |
| 251 | + protected function check_network_data( $data ) { |
| 252 | + $network = get_network( $data['id'] ); |
| 253 | + |
| 254 | + $this->assertEquals( $network->id, $data['id'] ); |
| 255 | + $this->assertEquals( $network->domain, $data['domain'] ); |
| 256 | + $this->assertEquals( $network->path, $data['path'] ); |
| 257 | + $this->assertEquals( $network->site_name, $data['site_name'] ); |
| 258 | + } |
| 259 | +} |
0 commit comments