Skip to content

Commit

Permalink
Add unit testing for uninstall script
Browse files Browse the repository at this point in the history
  • Loading branch information
zackkatz committed Oct 2, 2015
1 parent a2f10cc commit 280b8b2
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 19 deletions.
238 changes: 219 additions & 19 deletions tests/unit-tests/GravityView_Uninstall_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,248 @@
class GravityView_Uninstall_Test extends GV_UnitTestCase {

/**
* @var int
* @since 1.15
* @group uninstall
* @covers GravityView_Uninstall::fire_everything()
*/
function test_fire_everything() {

$create_count = 10;

$form = $this->factory->form->create_and_get();

$all_forms = GFAPI::get_forms();

$views = $this->factory->view->create_many( $create_count, array(), array( 'form_id' => $form['id'] ) );

$entry_ids = $this->factory->entry->create_many( $create_count, array(), array( 'form_id' => $form['id'] ) );

$connected = gravityview_get_connected_views( $form['id'] );

$entry_count = GFAPI::count_entries( $form['id'] );

// Make sure the objects were created and connected
$this->assertEquals( $create_count, count( array_filter( $views ) ) );
$this->assertEquals( $create_count, count( array_filter( $connected ) ) );
$this->assertEquals( $create_count, count( array_filter( $entry_ids ) ) );

$this->_set_up_expected_options();

### DO NOT DELETE WHEN IT IS NOT SET OR SET TO FALSE

// TRY deleting when the settings aren't configured.
$this->_set_up_gravityview_settings( NULL );
$this->uninstall();
$this->_check_deleted_options( false );

// TRY deleting when the Delete setting is set to No
$this->_set_up_gravityview_settings( '0' );
$this->uninstall();
$this->_check_deleted_options( false );

### REALLY DELETE NOW

// Create the items
$this->_set_up_gravityview_settings( 'delete' );
$this->_set_up_notes( $entry_ids );
$this->_set_up_entry_meta( $entry_ids, $form );

$this->uninstall();

// No Forms should be deleted
$this->assertEquals( $all_forms, GFAPI::get_forms() );

$this->_check_posts();
$this->_check_entries( $form, $entry_count );
$this->_check_deleted_options();
$this->_check_deleted_entry_notes( $entry_ids );
$this->_check_deleted_entry_meta( $entry_ids );

}

/**
* Make sure that the GV approval entry meta has been deleted, but not other meta
* @since 1.15
* @param $entry_ids
*/
function _check_deleted_entry_meta( $entry_ids ) {

$values = gform_get_meta_values_for_entries( $entry_ids, array( 'is_approved', 'do_not_delete' ) );

foreach ( $values as $value ) {
$this->assertFalse( $value->is_approved );
$this->assertEquals( "DO NOT DELETE", $value->do_not_delete );
}
}

/**
* @since 1.15
*/
var $form_id = 0;
function _check_posts() {
// All the Views should be deleted
$views = get_posts( array(
'post_type' => 'gravityview',
));
$this->assertEquals( array(), $views );
}

/**
* @var array GF Form array
* No entries should be deleted
* @since 1.15
* @param $form
* @param $create_count
*/
var $form = array();
function _check_entries( $form, $create_count ) {
$entries = GFAPI::get_entries( $form['id'] );
$this->assertEquals( sizeof( $entries ), $create_count );
}

/**
* @var int
* There should only be ONE NOTE not deleted
* @since 1.15
* @param $entry_ids
*/
var $entry_id = 0;
function _check_deleted_entry_notes( $entry_ids ) {

foreach( $entry_ids as $entry_id ) {

$notes = GravityView_Entry_Notes::get_notes( $entry_id );

$this->assertEquals( sizeof( $notes ), 1 );
$this->assertEquals( 'NOT DELETED', $notes[0]->value );
}
}

/**
* Make sure the settings and transients have been deleted
* @since 1.15
*/
function _check_deleted_options( $should_be_empty = true ) {

$options = array(
'gravityformsaddon_gravityview_app_settings',
'gravityformsaddon_gravityview_version',
'gravityview_cache_blacklist',
);

foreach( $options as $option ) {
if( $should_be_empty ) {
$this->assertEmpty( get_option( $option ) );
} else {
$this->assertNotEmpty( get_option( $option ) );
}
}

$transients = array(
'gravityview_edd-activate_valid',
'gravityview_edd-deactivate_valid',
'gravityview_dismissed_notices',
);

foreach( $transients as $transient ) {
if( $should_be_empty ) {
$this->assertEmpty( get_transient( $transient ) );
} else {
$this->assertNotEmpty( get_transient( $transient ) );
}
}
}

/**
* @since 1.15
* @param $entry_ids
* @param $form
*/
function _set_up_entry_meta( $entry_ids, $form ) {

foreach( $entry_ids as $entry_id ) {
GravityView_Admin_ApproveEntries::update_approved( $entry_id, 1, $form['id'] );
$this->assertEquals( gform_get_meta( $entry_id, 'is_approved' ), 1 );
gform_add_meta( $entry_id, 'do_not_delete', 'DO NOT DELETE' );
}
}

/**
* @var array GF Entry array
* @since 1.15
* @param $entry_ids
*/
var $entry = array();
function _set_up_notes( $entry_ids ) {

function setUp() {
parent::setUp();
$disapproved = __('Disapproved the Entry for GravityView', 'gravityview');
$approved = __('Approved the Entry for GravityView', 'gravityview');

define( 'WP_UNINSTALL_PLUGIN', true );
foreach( $entry_ids as $entry_id ) {

require_once GV_Unit_Tests_Bootstrap::instance()->plugin_dir . '/uninstall.php';
$added_notes = 0;

// Deleted because it's "gravityview" note type
GravityView_Entry_Notes::add_note( $entry_id, -1, new WP_UnitTest_Generator_Sequence( 'To be deleted %s' ), 'NOTE!', 'gravityview' ); // TO BE DELETED
$added_notes++;

$this->form = GV_Unit_Tests_Bootstrap::instance()->get_form();
$this->form_id = GV_Unit_Tests_Bootstrap::instance()->get_form_id();
// Deleted because it's the same value as $approved
GravityView_Entry_Notes::add_note( $entry_id, -1, new WP_UnitTest_Generator_Sequence( 'To be deleted %s' ), $approved, 'note' );
$added_notes++;

$this->entry = GV_Unit_Tests_Bootstrap::instance()->get_entry();
$this->entry_id = GV_Unit_Tests_Bootstrap::instance()->get_entry_id();
// Deleted because it's the same value as $disapproved
GravityView_Entry_Notes::add_note( $entry_id, -1, new WP_UnitTest_Generator_Sequence( 'To be deleted %s' ), $disapproved, 'note' );
$added_notes++;

do_action( 'deactivate_gravityview/gravityview.php' );
// NOT DELETED
GravityView_Entry_Notes::add_note( $entry_id, -1, new WP_UnitTest_Generator_Sequence( 'NOT DELETED %s' ), 'NOT DELETED', 'note' ); // NOT DELETED ("note" type)
$added_notes++;

$notes = GravityView_Entry_Notes::get_notes( $entry_id );

$this->assertEquals( sizeof( $notes ), $added_notes );
}
}

/**
* @group uninstall
* Get the script and process uninstall
* @since 1.15
*/
function uninstall() {
if( ! defined('WP_UNINSTALL_PLUGIN') ) {
define( 'WP_UNINSTALL_PLUGIN', true );
}
if( ! class_exists('GravityView_Uninstall' ) ) {
require_once GV_Unit_Tests_Bootstrap::instance()->plugin_dir . '/uninstall.php';
} else {
new GravityView_Uninstall;
}
}

/**
* Set delete to true
* @since 1.15
*/
function test_gravityview_has_shortcode_r() {
function _set_up_gravityview_settings( $delete_on_uninstall ) {

$defaults = GravityView_Settings::get_instance()->get_app_settings();

if( NULL === $delete_on_uninstall ) {
unset( $defaults['delete-on-uninstall'] );
} else {
$defaults['delete-on-uninstall'] = $delete_on_uninstall;
}

update_option( 'gravityformsaddon_gravityview_app_settings', $defaults );

if( NULL !== $delete_on_uninstall ) {
$this->assertEquals( $delete_on_uninstall, GravityView_Settings::get_instance()->get_app_setting( 'delete-on-uninstall' ) );
}
}

/**
* @since 1.15
*/
function _set_up_expected_options() {
update_option( 'gravityformsaddon_gravityview_version', 1 );
update_option( 'gravityview_cache_blacklist', 1 );

set_transient( 'gravityview_edd-activate_valid', 1 );
set_transient( 'gravityview_edd-deactivate_valid', 1 );
set_transient( 'gravityview_dismissed_notices', 1 );
}

}
1 change: 1 addition & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct() {
* Delete GravityView Views, settings, roles, caps, etc.
* @see https://youtu.be/FXy_DO6IZOA?t=35s
* @since 1.14
* @return void
*/
private function fire_everything() {
$this->delete_options();
Expand Down

0 comments on commit 280b8b2

Please sign in to comment.