woocommerce/plugins/woocommerce-admin/tests/api/onboarding-profile.php

181 lines
5.6 KiB
PHP
Raw Normal View History

<?php
/**
* Onboarding Profile REST API Test
*
* @package WooCommerce\Admin\Tests\API
*/
2019-08-02 18:54:09 +00:00
use \Automattic\WooCommerce\Admin\API\OnboardingProfile;
/**
* WC Tests API Onboarding Profile
*/
class WC_Tests_API_Onboarding_Profiles extends WC_REST_Unit_Test_Case {
/**
* Endpoints.
*
* @var string
*/
protected $endpoint = '/wc-admin/onboarding/profile';
/**
* Setup test data. Called before every test.
*/
public function setUp() {
parent::setUp();
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
}
/**
* Test that profile data is returned by the endpoint.
*/
public function test_get_profile_items() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'GET', $this->endpoint );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
2019-08-02 18:54:09 +00:00
$properties = OnboardingProfile::get_profile_properties();
foreach ( $properties as $key => $property ) {
$this->assertArrayHasKey( $key, $properties );
}
}
/**
* Test that profile date is updated by the endpoint.
*/
public function test_update_profile_items() {
wp_set_current_user( $this->user );
// Test updating 2 fields separately.
$request = new WP_REST_Request( 'POST', $this->endpoint );
$request->set_headers( array( 'content-type' => 'application/json' ) );
$request->set_body( wp_json_encode( array( 'industry' => 'health-beauty' ) ) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'success', $data['status'] );
// Test that the update works.
$request = new WP_REST_Request( 'POST', $this->endpoint );
$request->set_headers( array( 'content-type' => 'application/json' ) );
$request->set_body( wp_json_encode( array( 'theme' => 'Storefront' ) ) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'success', $data['status'] );
// Make sure the original field value wasn't overwritten.
$request = new WP_REST_Request( 'GET', $this->endpoint );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'health-beauty', $data['industry'][0] );
$this->assertEquals( 'storefront', $data['theme'] );
}
/**
* Test schema.
*
* @since 3.5.0
*/
public function test_schema() {
wp_set_current_user( $this->user );
$request = new WP_REST_Request( 'OPTIONS', $this->endpoint );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
Added skip profiler functionality (https://github.com/woocommerce/woocommerce-admin/pull/4721) * Added skip profiler functionality This commit adds the skip profiler functionality, leaving the Home screen `Store details` as incomplete. # Conflicts: # client/task-list/tasks.js * Unit tests repaiared * Added OBW page This commit makes the necessary changes to add an OBW page # Conflicts: # client/profile-wizard/index.js * Event name changed This commit renames the event name we were recording since it was wrong. * Redirection modified This commit modifies the way we do redirection # Conflicts: # client/profile-wizard/index.js * Removed `profiler` feature flag * Removed `profiler` feature flag This commit removes the `profiler` feature flag * Added error handling for `updateProfileItems` This commit adds error handling for the method `updateProfileItems` * Update client/profile-wizard/index.js Co-authored-by: Matt Sherman <matt.sherman@automattic.com> * Removed "throw" from "then" * Update client/dashboard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Update client/profile-wizard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Modified redirection URL This commit modifies the redirection URL. # Conflicts: # client/task-list/tasks.js # Conflicts: # client/profile-wizard/index.js * Removed the variable "lastStep" and the related logic. * Removed change of state for variable "skipped" * Added error trowing to onboarding actions This commit adds error trowing to onboarding actions * Removed response handling for `updateProfileItems` * Added default value for "profileItems" in Homescreen and Dashboard * Removed `step` from OnboardingProfile and test This commit removes the `step` from OnboardingProfile.php and from tests Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com> Co-authored-by: Matt Sherman <matt.sherman@automattic.com> Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
2020-07-16 15:17:10 +00:00
$this->assertCount( 16, $properties );
$this->assertArrayHasKey( 'completed', $properties );
Added skip profiler functionality (https://github.com/woocommerce/woocommerce-admin/pull/4721) * Added skip profiler functionality This commit adds the skip profiler functionality, leaving the Home screen `Store details` as incomplete. # Conflicts: # client/task-list/tasks.js * Unit tests repaiared * Added OBW page This commit makes the necessary changes to add an OBW page # Conflicts: # client/profile-wizard/index.js * Event name changed This commit renames the event name we were recording since it was wrong. * Redirection modified This commit modifies the way we do redirection # Conflicts: # client/profile-wizard/index.js * Removed `profiler` feature flag * Removed `profiler` feature flag This commit removes the `profiler` feature flag * Added error handling for `updateProfileItems` This commit adds error handling for the method `updateProfileItems` * Update client/profile-wizard/index.js Co-authored-by: Matt Sherman <matt.sherman@automattic.com> * Removed "throw" from "then" * Update client/dashboard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Update client/profile-wizard/index.js Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Modified redirection URL This commit modifies the redirection URL. # Conflicts: # client/task-list/tasks.js # Conflicts: # client/profile-wizard/index.js * Removed the variable "lastStep" and the related logic. * Removed change of state for variable "skipped" * Added error trowing to onboarding actions This commit adds error trowing to onboarding actions * Removed response handling for `updateProfileItems` * Added default value for "profileItems" in Homescreen and Dashboard * Removed `step` from OnboardingProfile and test This commit removes the `step` from OnboardingProfile.php and from tests Co-authored-by: Fernando Marichal <contacto@fernandomarichal.com> Co-authored-by: Matt Sherman <matt.sherman@automattic.com> Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
2020-07-16 15:17:10 +00:00
$this->assertArrayHasKey( 'skipped', $properties );
$this->assertArrayHasKey( 'account_type', $properties );
$this->assertArrayHasKey( 'industry', $properties );
$this->assertArrayHasKey( 'product_types', $properties );
$this->assertArrayHasKey( 'product_count', $properties );
$this->assertArrayHasKey( 'selling_venues', $properties );
$this->assertArrayHasKey( 'revenue', $properties );
$this->assertArrayHasKey( 'other_platform', $properties );
$this->assertArrayHasKey( 'other_platform_name', $properties );
$this->assertArrayHasKey( 'business_extensions', $properties );
$this->assertArrayHasKey( 'theme', $properties );
$this->assertArrayHasKey( 'wccom_connected', $properties );
$this->assertArrayHasKey( 'items_purchased', $properties );
$this->assertArrayHasKey( 'plugins', $properties );
$this->assertArrayHasKey( 'setup_client', $properties );
}
/**
* Test that profiles response changes based on applied filters.
*/
public function test_profile_extensibility() {
wp_set_current_user( $this->user );
add_filter(
'woocommerce_rest_onboarding_profile_properties',
function( $properties ) {
$properties['test_profile_datum'] = array(
'type' => 'array',
'description' => __( 'Test onboarding profile extensibility.', 'woocommerce-admin' ),
'context' => array( 'view' ),
'readonly' => true,
);
return $properties;
}
);
// Test that the update works.
$request = new WP_REST_Request( 'POST', $this->endpoint );
$request->set_headers( array( 'content-type' => 'application/json' ) );
$request->set_body( wp_json_encode( array( 'test_profile_datum' => 'woo' ) ) );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'success', $data['status'] );
// Test that the new field is retrieved.
$request = new WP_REST_Request( 'GET', $this->endpoint );
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'woo', $data['test_profile_datum'] );
}
/**
* Ensure that every REST controller works with their defaults.
*/
public function test_default_params() {
$endpoints = array(
'/wc-admin/onboarding/profile',
'/wc-admin/plugins',
);
foreach ( $endpoints as $endpoint ) {
$request = new WP_REST_Request( 'GET', $endpoint );
$response = $this->server->dispatch( $request );
// Surface any errors for easier debugging.
if ( is_wp_error( $response ) ) {
$this->fail( $response->get_error_message() );
}
$this->assertTrue( is_array( $response->get_data() ) );
}
}
}