From 5da2353d04c99d660b4a253618643ee1d211cff0 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 20 Sep 2018 01:23:39 +0200 Subject: [PATCH] add tests for profile endpoints --- .../test-wiaas-rest-customer-api.php | 609 ++++++++++++++++++ 1 file changed, 609 insertions(+) create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php new file mode 100644 index 0000000..85844e4 --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-rest-customer-api.php @@ -0,0 +1,609 @@ +server = $wp_rest_server = new \WP_REST_Server; + do_action( 'rest_api_init' ); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_profile_addresses + */ + function test_update_customer_profile_addresses_as_guest() { + wp_set_current_user(0); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/0/profile-addresses'); + $request->set_body_params(array( + 'profile_address' => $dummy_address + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_profile_addresses + */ + function test_add_customer_profile_addresses_with_valid_address() { + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/profile-addresses'); + $request->set_body_params(array( + 'profile_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('profile_addresses', $profile_info); + $this->assertEquals($profile_info['profile_addresses'][0]['city'], $dummy_address['city']); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'PROFILE_ADDRESS_UPDATED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_profile_addresses + */ + function test_add_customer_profile_addresses_with_invalid_address() { + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + $dummy_address['zip_code'] = 'this is not a zip code'; + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/profile-addresses'); + $request->set_body_params(array( + 'profile_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('profile_addresses', $profile_info); + $this->assertEquals($profile_info['profile_addresses'][0], NULL); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'PROFILE_ADDRESS_NOT_CHANGED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_billing_addresses + */ + function test_update_customer_billing_addresses_as_guest() { + wp_set_current_user(0); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/0/billing-addresses'); + $request->set_body_params(array( + 'billing_address' => $dummy_address + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_billing_addresses + */ + function test_add_customer_billing_addresses_with_valid_address() { + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'billing_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('billing_addresses', $profile_info); + $this->assertEquals($profile_info['billing_addresses'][0]['city'], $dummy_address['city']); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'BILLING_ADDRESS_UPDATED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_billing_addresses + */ + function test_add_customer_billing_addresses_with_invalid_address() { + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + $dummy_address['zip_code'] = 'this is not a zip code'; + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'billing_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('billing_addresses', $profile_info); + $this->assertEquals($profile_info['billing_addresses'][0], NULL); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'BILLING_ADDRESS_NOT_CHANGED'); + } + + /** + * @covers Wiass_REST_Customer_API::delete_customer_billing_addresses + */ + function test_delete_customer_billing_addresses_as_guest() { + $address_id = self::insert_dummy_billing_address(); + wp_set_current_user(0); + + $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'address_id' => $address_id + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::delete_customer_billing_addresses + */ + function test_delete_customer_billing_addresses() { + $address_id = self::insert_dummy_billing_address(); + wp_set_current_user(1); + + $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'address_id' => $address_id + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('billing_addresses', $profile_info); + $this->assertEquals($profile_info['billing_addresses'][0], NULL); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADDRESS_REMOVED'); + } + + /** + * @covers Wiass_REST_Customer_API::delete_customer_profile_addresses + */ + function test_delete_customer_delivery_addresses_as_guest() { + $address_id = self::insert_dummy_profile_address(); + wp_set_current_user(0); + + $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/profile-addresses'); + $request->set_body_params(array( + 'address_id' => $address_id + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::delete_customer_profile_addresses + */ + function test_delete_customer_profile_addresses() { + $address_id = self::insert_dummy_profile_address(); + wp_set_current_user(1); + + $request = new WP_REST_Request( 'POST', '/wiaas/customer/1/profile-addresses'); + $request->set_body_params(array( + 'address_id' => $address_id + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('profile_addresses', $profile_info); + $this->assertEquals($profile_info['profile_addresses'][0], NULL); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADDRESS_REMOVED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_personal_info + */ + function test_update_customer_personal_info_as_guest() { + wp_set_current_user(0); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info'); + $request->set_body_params(array( + 'first_name' => 'Trunks', + 'last_name' => 'Goten', + 'phone' => 11111 + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_personal_info + */ + function test_update_customer_personal_info_with_empty_phone() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info'); + $request->set_body_params(array( + 'first_name' => 'Trunks', + 'last_name' => 'Goten', + 'phone' => '' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('phone', $profile_info); + $this->assertEquals($profile_info['phone'], ''); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADD_PHONE_NUMBER'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_personal_info + */ + function test_update_customer_personal_info_with_empty_name() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info'); + $request->set_body_params(array( + 'first_name' => '', + 'last_name' => '', + 'phone' => '23232' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('phone', $profile_info); + $this->assertEquals($profile_info['phone'], ''); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADD_NAME'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_personal_info + */ + function test_update_customer_personal_info() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/personal-info'); + $request->set_body_params(array( + 'first_name' => 'Trunks', + 'last_name' => 'Goten', + 'phone' => '3434' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('phone', $profile_info); + $this->assertArrayHasKey('name', $profile_info); + $this->assertEquals($profile_info['phone'], '3434'); + $this->assertEquals($profile_info['name'], 'Trunks Goten'); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'PROFILE_UPDATED'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_company_info + */ + function test_update_customer_company_info_as_guest() { + wp_set_current_user(0); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info'); + $request->set_body_params(array( + 'company_name' => 'Saburly', + 'vat_code' => '12345' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertTrue($response->is_error()); + $this->assertEquals($response->get_status(), 401); + + $error_data = $response->as_error(); + $this->assertEquals($error_data->get_error_message(), 'Sorry, you are not allowed to do that.'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_company_info + */ + function test_update_customer_company_info_with_empty_company_name() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info'); + $request->set_body_params(array( + 'company_name' => '', + 'vat_code' => '12345' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('company_name', $profile_info); + $this->assertArrayHasKey('vat_code', $profile_info); + $this->assertEquals($profile_info['company_name'], ''); + $this->assertEquals($profile_info['vat_code'], ''); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADD_COMPANY_NAME'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_company_info + */ + function test_update_customer_company_info_with_empty_vat() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info'); + $request->set_body_params(array( + 'company_name' => 'Saburly', + 'vat_code' => '' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('company_name', $profile_info); + $this->assertArrayHasKey('vat_code', $profile_info); + $this->assertEquals($profile_info['company_name'], ''); + $this->assertEquals($profile_info['vat_code'], ''); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'ADD_VAT'); + } + + /** + * @covers Wiass_REST_Customer_API::update_customer_company_info + */ + function test_update_customer_company_info() { + wp_set_current_user(1); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/company-info'); + $request->set_body_params(array( + 'company_name' => 'Saburly', + 'vat_code' => '123' + )); + $response = $this->server->dispatch( $request ); + + $this->assertNotNull($response); + $this->assertInstanceOf('WP_REST_Response',$response); + $this->assertFalse($response->is_error()); + $this->assertEquals($response->get_status(), 200); + + $data = $response->get_data(); + + $this->assertArrayHasKey('data', $data); + $this->assertArrayHasKey('messages', $data); + + $profile_info = $data['data']; + $messages = $data['messages'][0]; + + $this->assertArrayHasKey('company_name', $profile_info); + $this->assertArrayHasKey('vat_code', $profile_info); + $this->assertEquals($profile_info['company_name'], 'Saburly'); + $this->assertEquals($profile_info['vat_code'], '123'); + + $this->assertArrayHasKey('message', $messages); + $this->assertEquals($messages['message'], 'COMPANY_UPDATED'); + } + + + + //Helper functions + private function create_dummy_address(){ + return array( + 'country_name' => 'Sweden', + 'delivery_mail' => 'delivery@email.com', + 'id_country_selected' => 1, + 'city' => 'Stockholm', + 'detailed_address' => 'Dummy Street 7', + 'zip_code' => 124443, + 'first_name' => 'Tester', + 'last_name' => 'Retset', + 'invoice_mail' => 'invoice@mail.com' + ); + } + + private function insert_dummy_billing_address(){ + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/billing-addresses'); + $request->set_body_params(array( + 'billing_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + return $response->get_data()['data']['billing_addresses'][0]['id']; + } + + private function insert_dummy_profile_address(){ + wp_set_current_user(1); + + $dummy_address = self::create_dummy_address(); + + $request = new WP_REST_Request( 'PUT', '/wiaas/customer/1/delivery-addresses'); + $request->set_body_params(array( + 'delivery_address' => json_encode($dummy_address) + )); + $response = $this->server->dispatch( $request ); + + return $response->get_data()['data']['delivery_addresses'][0]['id']; + } +}