Skip to content

Commit

Permalink
Add AJAX test class
Browse files Browse the repository at this point in the history
  • Loading branch information
zackkatz committed Oct 8, 2015
1 parent e815e71 commit f65637e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function __construct() {
ini_set( 'display_errors', 'on' );
error_reporting( E_ALL );

define( 'DOING_GRAVITYVIEW_TESTS', true );

$this->tests_dir = dirname( __FILE__ );
$this->plugin_dir = dirname( $this->tests_dir );
$this->wp_tests_dir = getenv( 'WP_TESTS_DIR' ) ? getenv( 'WP_TESTS_DIR' ) : $this->plugin_dir . '/tmp/wordpress-tests-lib';
Expand Down
86 changes: 83 additions & 3 deletions tests/unit-tests/GravityView_AJAX_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ class GravityView_Ajax_Test extends GV_UnitTestCase {
*/
var $AJAX;

/**
* @var GravityView_Preset_Business_Data
*/
var $GravityView_Preset_Business_Data;

function setUp() {

parent::setUp();

$this->AJAX = new GravityView_Ajax;
$this->create_test_nonce();
$this->GravityView_Preset_Business_Data = new GravityView_Preset_Business_Data;

require_once( GFCommon::get_base_path() . '/export.php' );
}
Expand All @@ -25,6 +31,82 @@ function create_test_nonce() {
$_POST['nonce'] = wp_create_nonce( 'gravityview_ajaxviews' );
}

/**
* @covers GravityView_Ajax::pre_get_form_fields()
* @group gvajax
*/
function test_get_available_fields_html() {

$_POST['template_id'] = $this->GravityView_Preset_Business_Data->template_id;

// Test form generation and default context
add_action( 'gravityview_render_available_fields', array( $this, 'get_available_fields_html_DEFAULT' ), 10, 2 );
$this->AJAX->get_available_fields_html();
remove_action( 'gravityview_render_available_fields', array( $this, 'get_available_fields_html_DEFAULT' ), 10 );

// Test SINGLE context being set
$_POST['context'] = 'single';
add_action( 'gravityview_render_available_fields', array( $this, 'get_available_fields_html_SINGLE_CONTEXT' ), 10, 2 );
$this->AJAX->get_available_fields_html();
remove_action( 'gravityview_render_available_fields', array( $this, 'get_available_fields_html_SINGLE_CONTEXT' ), 10 );

// Test EDIT context being set
$_POST['context'] = 'edit';
add_action( 'gravityview_render_available_fields', array( $this, 'get_available_fields_html_EDIT_CONTEXT' ), 10, 2 );
$this->AJAX->get_available_fields_html();
remove_action( 'gravityview_render_available_fields', array( $this, 'get_available_fields_html_EDIT_CONTEXT' ), 10 );

}



/**
* @param array $form
* @param string $context
*/
function get_available_fields_html_DEFAULT( $form, $context ) {

$this->assertEquals( GravityView_Ajax::pre_get_form_fields( $this->GravityView_Preset_Business_Data->template_id ), $form );

// When not defined, default to directory
$this->assertEquals( 'directory', $context );
}

/**
* @param array $form
* @param string $context
*/
function get_available_fields_html_SINGLE_CONTEXT( $form, $context ) {

// When not defined, default to directory
$this->assertEquals( 'single', $context );
}

/**
* @param array $form
* @param string $context
*/
function get_available_fields_html_EDIT_CONTEXT( $form, $context ) {

// When not defined, default to directory
$this->assertEquals( 'edit', $context );
}

/**
* @covers GravityView_Ajax::pre_get_form_fields()
* @group gvajax
*/
function test_pre_get_form_fields() {

$imported_form = $this->AJAX->import_form( $this->GravityView_Preset_Business_Data->settings['preset_form'] );

$not_imported_form = GravityView_Ajax::pre_get_form_fields( $this->GravityView_Preset_Business_Data->template_id );

// We don't test exact equality, since the import_form will return GF_Field objects for field items, and other
// differences. We just want to make sure most stuff matches close enough to suggest it's working!
$this->assertEquals( count( $imported_form['fields'] ), count( $not_imported_form['fields'] ) );
$this->assertEquals( $imported_form['title'], $not_imported_form['title'] );
}

/**
* @covers GravityView_Ajax::import_form()
Expand All @@ -33,9 +115,7 @@ function create_test_nonce() {
function test_import_form() {
/** @define "GRAVITYVIEW_DIR" "../../" */

$GravityView_Preset_Business_Data = new GravityView_Preset_Business_Data;

$forms = $this->AJAX->import_form( $GravityView_Preset_Business_Data->settings['preset_form'] );
$forms = $this->AJAX->import_form( $this->GravityView_Preset_Business_Data->settings['preset_form'] );

$this->assertNotEmpty( $forms );
$this->assertEquals( 'GravityView - Business Data', $forms['title'] );
Expand Down

0 comments on commit f65637e

Please sign in to comment.