Skip to content

Commit

Permalink
test: add activate and uninstall tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhooks committed Apr 4, 2023
1 parent 9cf90f8 commit e099c81
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 42 deletions.
4 changes: 2 additions & 2 deletions includes/class-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static function activate() {
*
* @return void
*/
protected static function create_tables() {
public static function create_tables() {
$db_version = get_option( 'wp_notifications_db_version' );

if ( ! $db_version ) {
Expand All @@ -125,7 +125,7 @@ protected static function create_tables() {
*
* @return void
*/
protected static function create_tables_v1() {
public static function create_tables_v1() {
global $wpdb;

require_once ABSPATH . 'wp-admin/includes/upgrade.php';
Expand Down
4 changes: 2 additions & 2 deletions includes/class-uninstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function uninstall() {
*
* @return void
*/
protected static function drop_tables() {
public static function drop_tables() {
global $wpdb;

$wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'notifications_messages' );
Expand All @@ -76,7 +76,7 @@ protected static function drop_tables() {
*
* @return void
*/
protected static function delete_options() {
public static function delete_options() {
delete_option( 'wp_notifications_db_version' );
delete_option( 'wp_notifications_options' );
}
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ function handle_wp_setup_failure( $message ) {

require dirname( __FILE__ ) . '/class-dummy-message.php';
require dirname( __FILE__ ) . '/class-wp-notify-test-case.php';
require dirname( __FILE__ ) . '/class-db-test-case.php';
17 changes: 17 additions & 0 deletions tests/phpunit/includes/class-db-test-case.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use PHPUnit\Framework\TestCase;

class WP_Notifications_DB_TestCase extends TestCase {
protected function table_exists( $table_name ): bool {
global $wpdb;
$expected = $wpdb->prefix . $table_name;
$result = $wpdb->get_var(
$wpdb->prepare(
'SHOW TABLES LIKE %s',
$wpdb->esc_like( $wpdb->prefix . $table_name )
)
);
return $expected == $result;
}
}
46 changes: 46 additions & 0 deletions tests/phpunit/tests/test-activator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use WP\Notifications\Activator;
use WP\Notifications\Uninstaller;

class Test_WP_Notify_Activator extends WP_Notifications_DB_TestCase {

/**
* Remove the plugin to test installation.
*/
public static function setUpBeforeClass(): void {
Uninstaller::drop_tables();
}

// Sanity checks to ensure the database table initially does not exist.

public function test_it_should_initially_not_have_messages_table() {
$this->assertFalse( $this->table_exists( 'notifications_messages' ) );
}

public function test_it_should_initially_not_have_channels_table() {
$this->assertFalse( $this->table_exists( 'notifications_channels' ) );
}

public function test_it_should_initially_not_have_subscriptions_table() {
$this->assertFalse( $this->table_exists( 'notifications_subscriptions' ) );
}

public function test_it_should_initially_not_have_queue_table() {
$this->assertFalse( $this->table_exists( 'notifications_queue' ) );
}

// Installation procedure tests.

/**
* Test to ensure the uninstall procedure drops the correct tables.
*/
public function test_it_should_create_tables() {
Activator::create_tables_v1();

$this->assertTrue( $this->table_exists( 'notifications_messages' ) );
$this->assertTrue( $this->table_exists( 'notifications_channels' ) );
$this->assertTrue( $this->table_exists( 'notifications_subscriptions' ) );
$this->assertTrue( $this->table_exists( 'notifications_queue' ) );
}
}
44 changes: 44 additions & 0 deletions tests/phpunit/tests/test-uninstaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use WP\Notifications\Activator;
use WP\Notifications\Uninstaller;

class Test_WP_Notify_Uninstaller extends WP_Notifications_DB_TestCase {

/**
* Ensure tables are created before tests.
*/
public static function setUpBeforeClass(): void {
Activator::create_tables_v1();
}

// Sanity checks to ensure the database tables initially do exist.

public function test_it_should_initially_have_messages_table() {
$this->assertTrue( $this->table_exists( 'notifications_messages' ) );
}

public function test_it_should_initially_have_channels_table() {
$this->assertTrue( $this->table_exists( 'notifications_channels' ) );
}

public function test_it_should_initially_have_subscriptions_table() {
$this->assertTrue( $this->table_exists( 'notifications_subscriptions' ) );
}

public function test_it_should_initially_have_queue_table() {
$this->assertTrue( $this->table_exists( 'notifications_queue' ) );
}

/**
* Test to ensure the uninstall procedure drops the correct tables.
*/
public function test_it_should_drops_tables() {
Uninstaller::uninstall();

$this->assertFalse( $this->table_exists( 'notifications_messages' ) );
$this->assertFalse( $this->table_exists( 'notifications_channels' ) );
$this->assertFalse( $this->table_exists( 'notifications_subscriptions' ) );
$this->assertFalse( $this->table_exists( 'notifications_queue' ) );
}
}
44 changes: 6 additions & 38 deletions wp-feature-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
* @package wp-feature-notifications
*/

use WP\Notifications\REST;
use WP\Notifications\Activator;
use WP\Notifications\Uninstaller;

define( 'WP_NOTIFICATIONS_LOG_PREFIX', 'WP_NOTIFICATIONS: ' );
use WP\Notifications\REST\Notification_Controller;

if ( ! defined( 'WP_NOTIFICATION_CENTER_PLUGIN_VERSION' ) ) {
define( 'WP_NOTIFICATION_CENTER_PLUGIN_VERSION', '0.0.1' );
Expand All @@ -28,9 +24,6 @@
define( 'WP_NOTIFICATION_CENTER_DB_VERSION', '1' );
}

if ( ! defined( 'WP_NOTIFICATIONS_DEBUG' ) ) {
define( 'WP_NOTIFICATIONS_DEBUG', false );
}

if ( ! defined( 'WP_NOTIFICATION_CENTER_PLUGIN_DIR' ) ) {
define( 'WP_NOTIFICATION_CENTER_PLUGIN_DIR', dirname( __FILE__ ) );
Expand All @@ -40,6 +33,8 @@
define( 'WP_NOTIFICATION_CENTER_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) );
}

require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/vendor/autoload.php';

// Require interface/class declarations..
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/exceptions/interface-wp-notify-exception.php';
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/exceptions/class-wp-notify-runtime-exception.php';
Expand Down Expand Up @@ -72,34 +67,7 @@
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/persistence/class-wp-notify-wpdb-notification-repository.php';
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/demo.php';
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/restapi/class-notification-controller.php';
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/class-activator.php';
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/class-uninstaller.php';

// TODO: Standardise structure and/or autoloading.
new REST\Notification_Controller();

/**
* Activation hook function of the WP Notification plugin.
*
* @return void
*
* @package WP_Notifications
*/
function wp_notifications_activation_hook() {
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/class-activator.php';
Activator::activate();
}

register_activation_hook( __FILE__, 'wp_notifications_activation_hook' );

/**
* Uninstall hook function of the WP Notification plugin.
*
* @return void
*
* @package WP_Notifications
*/
function wp_notifications_uninstall_hook() {
require_once WP_NOTIFICATION_CENTER_PLUGIN_DIR . '/includes/class-uninstaller.php';
Uninstaller::uninstall();
}

register_uninstall_hook( __FILE__, 'wp_notifications_uninstall_hook' );
new Notification_Controller();

0 comments on commit e099c81

Please sign in to comment.