Skip to content

Commit

Permalink
see #607: add autocomplete tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyan0v committed Sep 6, 2017
1 parent 199927d commit f5c02ae
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/test-ajax-autocomplete.php
@@ -0,0 +1,93 @@
<?php
/**
* Tests: Ajax Autocomplete Test.
*
* @since 3.15.0
* @package Wordlift
*/
require_once 'functions.php';

/**
* Class AjaxAutocompleteTest
* Extend Wordlift_Ajax_Unit_Test_Case
*
* @since 3.15.0
* @package Wordlift
*/
class AjaxAutocompleteTest extends Wordlift_Ajax_Unit_Test_Case {
/**
* A {@link Wordlift_Configuration_Service} instance.
*
* @since 3.15.0
* @access private
* @var \Wordlift_Configuration_Service $configuration_service A {@link Wordlift_Configuration_Service} instance.
*/
private $configuration_service;

/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();

$wordlift = new Wordlift_Test();
$this->configuration_service = $wordlift->get_configuration_service();
}

public function test_autocomplete_without_nonce() {
// Spoof the nonce in the POST superglobal
$_POST['query'] = 'test';

try {
$this->_handleAjax( 'wl_autocomplete' );
} catch ( WPAjaxDieContinueException $e ) {}

$response = json_decode( $this->_last_response );


$this->assertInternalType( 'object', $response );
$this->assertObjectHasAttribute( 'success', $response );
$this->assertFalse( $response->success );
$this->assertObjectHasAttribute( 'data', $response );
$this->assertObjectHasAttribute( 'message', $response->data );
$this->assertEquals( 'Nonce field doens\'t match', $response->data->message );
}

public function test_autocomplete_with_nonce_without_query_param() {
// Spoof the nonce in the POST superglobal
$_POST['_wpnonce'] = wp_create_nonce( 'wordlift_autocomplete' );

try {
$this->_handleAjax( 'wl_autocomplete' );
} catch ( WPAjaxDieContinueException $e ) {}

$response = json_decode( $this->_last_response );

$this->assertInternalType( 'object', $response );
$this->assertObjectHasAttribute( 'success', $response );
$this->assertFalse( $response->success );
$this->assertObjectHasAttribute( 'data', $response );
$this->assertObjectHasAttribute( 'message', $response->data );
$this->assertEquals( 'The query param is empty!', $response->data->message );

}

public function test_autocomplete() {
// Spoof the nonce in the POST superglobal
$_POST['_wpnonce'] = wp_create_nonce( 'wordlift_autocomplete' );
$_POST['query'] = 'test';


try {
$this->_handleAjax( 'wl_autocomplete' );
} catch ( WPAjaxDieContinueException $e ) {}

$response = json_decode( $this->_last_response );

$this->assertInternalType( 'object', $response );
$this->assertObjectHasAttribute( 'success', $response );
$this->assertTrue( $response->success );
$this->assertObjectHasAttribute( 'data', $response );
$this->assertObjectHasAttribute( 'suggestions', $response->data );
}
}

0 comments on commit f5c02ae

Please sign in to comment.