Add unit tests for admin notes REST API
This commit is contained in:
parent
ca0165ff5d
commit
40376c23e1
|
@ -0,0 +1,129 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Admin notes REST API Test
|
||||||
|
*
|
||||||
|
* @package WooCommerce\Tests\API
|
||||||
|
* @since 3.5.0
|
||||||
|
*/
|
||||||
|
class WC_Tests_API_Admin_Notes extends WC_REST_Unit_Test_Case {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoints.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $endpoint = '/wc/v3/admin/notes';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup test admin notes data. Called before every test.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*/
|
||||||
|
public function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->user = $this->factory->user->create(
|
||||||
|
array(
|
||||||
|
'role' => 'administrator',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
WC_Helper_Admin_Notes::reset_notes_dbs();
|
||||||
|
WC_Helper_Admin_Notes::add_notes_for_tests();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test route registration.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*/
|
||||||
|
public function test_register_routes() {
|
||||||
|
$routes = $this->server->get_routes();
|
||||||
|
|
||||||
|
$this->assertArrayHasKey( $this->endpoint, $routes );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test getting a single note.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*/
|
||||||
|
public function test_get_note() {
|
||||||
|
wp_set_current_user( $this->user );
|
||||||
|
|
||||||
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint . '/1' ) );
|
||||||
|
$note = $response->get_data();
|
||||||
|
|
||||||
|
$this->assertEquals( 200, $response->get_status() );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test getting a single note without permission. It should fail.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*/
|
||||||
|
public function test_get_note_without_permission() {
|
||||||
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint . '/1' ) );
|
||||||
|
$this->assertEquals( 401, $response->get_status() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test getting lots of notes.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*/
|
||||||
|
public function test_get_notes() {
|
||||||
|
wp_set_current_user( $this->user );
|
||||||
|
|
||||||
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||||
|
$notes = $response->get_data();
|
||||||
|
|
||||||
|
$this->assertEquals( 200, $response->get_status() );
|
||||||
|
$this->assertEquals( 2, count( $notes ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test getting lots of notes without permission. It should fail.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*/
|
||||||
|
public function test_get_notes_without_permission() {
|
||||||
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', $this->endpoint ) );
|
||||||
|
$this->assertEquals( 401, $response->get_status() );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test getting the notes schema.
|
||||||
|
*
|
||||||
|
* @since 3.5.0
|
||||||
|
*/
|
||||||
|
public function test_get_notes_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'];
|
||||||
|
|
||||||
|
$this->assertEquals( 15, count( $properties ) );
|
||||||
|
|
||||||
|
$this->assertArrayHasKey( 'id', $properties );
|
||||||
|
$this->assertArrayHasKey( 'name', $properties );
|
||||||
|
$this->assertArrayHasKey( 'type', $properties );
|
||||||
|
$this->assertArrayHasKey( 'locale', $properties );
|
||||||
|
$this->assertArrayHasKey( 'title', $properties );
|
||||||
|
|
||||||
|
$this->assertArrayHasKey( 'content', $properties );
|
||||||
|
$this->assertArrayHasKey( 'icon', $properties );
|
||||||
|
$this->assertArrayHasKey( 'content_data', $properties );
|
||||||
|
$this->assertArrayHasKey( 'status', $properties );
|
||||||
|
$this->assertArrayHasKey( 'source', $properties );
|
||||||
|
|
||||||
|
$this->assertArrayHasKey( 'date_created', $properties );
|
||||||
|
$this->assertArrayHasKey( 'date_created_gmt', $properties );
|
||||||
|
$this->assertArrayHasKey( 'date_reminder', $properties );
|
||||||
|
$this->assertArrayHasKey( 'date_reminder_gmt', $properties );
|
||||||
|
$this->assertArrayHasKey( 'actions', $properties );
|
||||||
|
}
|
||||||
|
}
|
|
@ -113,5 +113,4 @@ wc_test_includes();
|
||||||
|
|
||||||
// Include wc-admin helpers.
|
// Include wc-admin helpers.
|
||||||
require_once dirname( __FILE__ ) . '/framework/helpers/class-wc-helper-reports.php';
|
require_once dirname( __FILE__ ) . '/framework/helpers/class-wc-helper-reports.php';
|
||||||
|
require_once dirname( __FILE__ ) . '/framework/helpers/class-wc-helper-admin-notes.php';
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class WC_Helper_Admin_Notes.
|
||||||
|
*
|
||||||
|
* This helper class should ONLY be used for unit tests!.
|
||||||
|
*/
|
||||||
|
class WC_Helper_Admin_Notes {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete everything in the notes tables.
|
||||||
|
*/
|
||||||
|
public static function reset_notes_dbs() {
|
||||||
|
global $wpdb;
|
||||||
|
$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_admin_notes" ); // @codingStandardsIgnoreLine.
|
||||||
|
$wpdb->query( "TRUNCATE TABLE {$wpdb->prefix}woocommerce_admin_note_actions" ); // @codingStandardsIgnoreLine.
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create two notes that we can use for notes REST API tests
|
||||||
|
*/
|
||||||
|
public static function add_notes_for_tests() {
|
||||||
|
$data_store = WC_Data_Store::load( 'admin-note' );
|
||||||
|
|
||||||
|
$note_1 = new WC_Admin_Note();
|
||||||
|
$note_1->set_title( 'PHPUNIT_TEST_NOTE_1_TITLE' );
|
||||||
|
$note_1->set_content( 'PHPUNIT_TEST_NOTE_1_CONTENT' );
|
||||||
|
$note_1->set_content_data( (object) array( 'amount' => 1.23 ) );
|
||||||
|
$note_1->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
|
||||||
|
$note_1->set_icon( 'info' );
|
||||||
|
$note_1->set_name( 'PHPUNIT_TEST_NOTE_NAME' );
|
||||||
|
$note_1->set_source( 'PHPUNIT_TEST' );
|
||||||
|
$note_1->add_action(
|
||||||
|
'PHPUNIT_TEST_NOTE_1_ACTION_1_SLUG',
|
||||||
|
'PHPUNIT_TEST_NOTE_1_ACTION_1_LABEL',
|
||||||
|
'?s=PHPUNIT_TEST_NOTE_1_ACTION_1_URL'
|
||||||
|
);
|
||||||
|
$note_1->add_action(
|
||||||
|
'PHPUNIT_TEST_NOTE_1_ACTION_2_SLUG',
|
||||||
|
'PHPUNIT_TEST_NOTE_1_ACTION_2_LABEL',
|
||||||
|
'?s=PHPUNIT_TEST_NOTE_1_ACTION_2_URL'
|
||||||
|
);
|
||||||
|
$note_1->save();
|
||||||
|
|
||||||
|
$note_2 = new WC_Admin_Note();
|
||||||
|
$note_2->set_title( 'PHPUNIT_TEST_NOTE_2_TITLE' );
|
||||||
|
$note_2->set_content( 'PHPUNIT_TEST_NOTE_2_CONTENT' );
|
||||||
|
$note_2->set_content_data( (object) array( 'amount' => 4.56 ) );
|
||||||
|
$note_2->set_type( WC_Admin_Note::E_WC_ADMIN_NOTE_WARNING );
|
||||||
|
$note_2->set_icon( 'info' );
|
||||||
|
$note_2->set_name( 'PHPUNIT_TEST_NOTE_NAME' );
|
||||||
|
$note_2->set_source( 'PHPUNIT_TEST' );
|
||||||
|
// This note has no actions.
|
||||||
|
$note_2->save();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue