Skip to content

Commit

Permalink
First draft admin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Bardo committed Dec 17, 2013
1 parent 9573d9b commit 8f402cf
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 71 deletions.
14 changes: 9 additions & 5 deletions classes/connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ public static function action_links( $links, $record ) {

/**
* Log handler
*
* @param string $message sprintf-ready error message string
* @param array $args sprintf (and extra) arguments to use
* @param int $object_id Target object id
* @param string $action Action performed (stream_action)
* @param int $user_id User responsible for the action
* @param array $contexts Contexts of the action
* @param int $user_id User responsible for the action
*
* @internal param string $action Action performed (stream_action)
* @return void
*/
public static function log( $message, $args, $object_id, $contexts, $user_id = null ) {
Expand All @@ -86,9 +88,11 @@ public static function log( $message, $args, $object_id, $contexts, $user_id = n

/**
* Save log data till shutdown, so other callbacks would be able to override
*
* @param string $handle Special slug to be shared with other actions
* @param mixed $arg1 Extra arguments to sent to log()
* @param mixed $arg2, etc..
*
* @internal param mixed $arg1 Extra arguments to sent to log()
* @internal param mixed $arg2 , etc..
* @return void
*/
public static function delayed_log( $handle ) {
Expand All @@ -109,4 +113,4 @@ public static function delayed_log_commit() {
}
}

}
}
16 changes: 14 additions & 2 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public static function load() {
* @return void
*/
public static function register_menu() {
global $menu;

self::$screen_id['main'] = add_menu_page(
__( 'Stream', 'stream' ),
__( 'Stream', 'stream' ),
Expand Down Expand Up @@ -78,6 +76,9 @@ public static function register_menu() {
* Enqueue scripts/styles for admin screen
*
* @action admin_enqueue_scripts
*
* @param $hook
*
* @return void
*/
public static function admin_enqueue_scripts( $hook ) {
Expand Down Expand Up @@ -273,6 +274,12 @@ private static function _role_can_view_stream( $role ) {
* Filter user caps to dynamically grant our view cap based on allowed roles
*
* @filter user_has_cap
*
* @param $allcaps
* @param $caps
* @param $args
* @param $user
*
* @return array
*/
public static function _filter_user_caps( $allcaps, $caps, $args, $user ) {
Expand All @@ -294,6 +301,11 @@ public static function _filter_user_caps( $allcaps, $caps, $args, $user ) {
* Filter role caps to dynamically grant our view cap based on allowed roles
*
* @filter role_has_cap
*
* @param $allcaps
* @param $cap
* @param $role
*
* @return array
*/
public static function _filter_role_caps( $allcaps, $cap, $role ) {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
>
<testsuites>
<testsuite>
<directory prefix="test-" suffix=".php">./tests/includes/</directory>
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class WP_Stream {
/**
* Class constructor
*/
public function __construct() {
private function __construct() {

if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
add_action( 'all_admin_notices', array( __CLASS__, 'php_version_notice' ) );
Expand Down Expand Up @@ -128,5 +128,5 @@ public static function php_version_notice() {

}

$GLOBALS['wp_stream'] = new WP_Stream;
$GLOBALS['wp_stream'] = WP_Stream::get_instance();
register_activation_hook( __FILE__, array( 'WP_Stream', 'install' ) );
8 changes: 6 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
* @author Jonathan Bardo <jonathan.bardo@x-team.com>
*/

//Create our own test case to prevent repeating ourself
require_once getenv( 'WP_TESTS_DIR' ) . '/includes/functions.php';

tests_add_filter(
'muplugins_loaded',
function() {
// Manually load plugin
require dirname( dirname( __FILE__ ) ) . '/stream.php';

//Call Activate plugin function
WP_Stream::install();
}
);

// Removes sql tables on shutdown
// Removes all sql tables on shutdown
// Do this action last
tests_add_filter(
'shutdown',
function() {
//@Todo Trigger uninstall function
//Empty all tables so we don't deal with leftovers
drop_tables();
},
999999
);

require getenv( 'WP_TESTS_DIR' ) . '/includes/bootstrap.php';
require dirname( __FILE__ ) . '/testcase.php';
59 changes: 0 additions & 59 deletions tests/includes/test-stream.php

This file was deleted.

73 changes: 73 additions & 0 deletions tests/testcase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Tests stream main class
*
* @author X-Team
* @author Jonathan Bardo <jonathan.bardo@x-team.com>
*/
class WP_StreamTestCase extends WP_UnitTestCase {

/**
* Holds the plugin base class
*
* @return void
*/
protected $plugin;

/**
* PHP unit setup function
*
* @return void
*/
function setUp() {
parent::setUp();
$this->plugin = $GLOBALS['wp_stream'];
}

/**
* Make sure the plugin is initialized with it's global variable
*
* @return void
*/
public function test_plugin_initialized() {
$this->assertFalse( null == $this->plugin );
}

/**
* Helper function to check validity of action
*
* @param array $tests
* @param string $function_call
*/
protected function do_action_validation( array $tests = array(), $function_call = 'has_action' ){
foreach ( $tests as $test ) {
list( $action, $class, $function ) = $test;

//Default WP priority
$priority = isset( $test[3] ) ? $test[3] : 10;

//Default function call
$function_call = ( in_array( $function_call, array( 'has_action', 'has_filter' ) ) ) ? $function_call : 'has_action';

//Run assertion here
$this->assertEquals(
$priority,
$function_call( $action, array( $class, $function ) ),
"$action $function_call is not attached to $class::$function. It might also have the wrong priority (validated priority: $priority)"
);
$this->assertTrue(
method_exists( $class, $function ),
"Class '$class' doesn't implement the '$function' function"
);
}
}

/**
* Helper function to check validity of filters
* @param array $tests
*/
protected function do_filter_validation( array $tests = array() ){
$this->do_action_validation( $tests, 'has_filter' );
}

}
108 changes: 108 additions & 0 deletions tests/tests/test-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Tests stream main class
*
* @author X-Team
* @author Jonathan Bardo <jonathan.bardo@x-team.com>
*
* @ToDo Test all remaining function of the admin class
* - erase_stream_records
* - purge_schedule_setup
*/
class Test_WP_Stream_Admin extends WP_StreamTestCase {

//Name of the class to test
const CLASSNAME = 'WP_Stream_Admin';

/**
* Change to admin page
*/
public function setUp() {
parent::setUp();

//Add admin user to test caps
// We need to change user to verify editing option as admin or editor
$administrator_id = $this->factory->user->create(
array(
'role' => 'administrator',
'user_login' => 'test_admin',
'email' => 'test@land.com',
)
);
wp_set_current_user( $administrator_id );

//Load the class manually like if we were in the admin
require_once WP_STREAM_INC_DIR . 'admin.php';
call_user_func( array( self::CLASSNAME, 'load' ) );
}

/**
* Check constructor composition
*
* @return void
*/
public function test_constructor() {
//Check all constructor action
$test_actions = array(
array( 'admin_menu', self::CLASSNAME, 'register_menu' ),
array( 'admin_enqueue_scripts', self::CLASSNAME, 'admin_enqueue_scripts' ),
array( 'admin_enqueue_scripts', self::CLASSNAME, 'admin_menu_css' ),
array( 'wp_ajax_wp_stream_reset', self::CLASSNAME, 'wp_ajax_reset' ),
array( 'init', self::CLASSNAME, 'purge_schedule_setup' ),
array( 'stream_auto_purge', self::CLASSNAME, 'purge_scheduled_action' ),
);

$this->do_action_validation( $test_actions );

//Check all constuctor filters
$test_actions = array(
array( 'user_has_cap', self::CLASSNAME, '_filter_user_caps' ),
array( 'role_has_cap', self::CLASSNAME, '_filter_role_caps' ),
array( 'plugin_action_links', self::CLASSNAME, 'plugin_action_links' ),
);

$this->do_filter_validation( $test_actions );
}

/**
* Check is menu is present in menu array
*/
public function test_register_menu(){
do_action( 'admin_menu' );
global $menu, $submenu;

//Check main menu
$this->assertTrue( in_array( 'wp_stream', reset( $menu ) ) );

//Check submenu
$this->assertQueryTrue( 'wp_stream_settings', $submenu['wp_stream'][1] );
}

/**
* Check if script are enqueued properly if on wp_stream page
*/
public function test_admin_enqueue_script() {
set_current_screen( 'toplevel_page_wp_stream' );
do_action( 'admin_enqueue_scripts', 'toplevel_page_wp_stream' );

//Check if chosen is present
$this->assertTrue( wp_script_is( 'wp-stream-chosen', 'enqueued' ), 'jQuery Chosen js is not enqueud' );
$this->assertTrue( wp_style_is( 'wp-stream-chosen', 'enqueued' ), 'jQuery Chosen css is not enqueud' );
$this->assertTrue( wp_script_is( 'jquery-core', 'registered' ), 'jQuery is not enqueud' );
$this->assertTrue( wp_script_is( 'wp-stream-admin', 'enqueued' ), 'wp-stream-admin is not enqueud' );
$this->assertTrue( wp_style_is( 'wp-stream-admin', 'enqueued' ), 'wp-stream-admin is not enqueud' );

//Check wp localise script
$localize_data = $GLOBALS['wp_scripts']->get_data( 'wp-stream-admin', 'data' );
$this->assertTrue( strrpos( $localize_data, 'i18n' ) > 0 );
}

/**
* Check the output of the plugin action links function
*/
public function test_plugin_action_links() {
$filter_output = apply_filters( 'plugin_action_links', array(), plugin_basename( WP_STREAM_DIR . 'stream.php' ) );
$this->assertTrue( strrpos( $filter_output[0], '/wp-admin/admin.php?page=wp_stream_settings' ) >= 0 );
}

}
Loading

0 comments on commit 8f402cf

Please sign in to comment.