Skip to content

Commit

Permalink
Add module loader initial tests
Browse files Browse the repository at this point in the history
also add LLMS_Unit_Test_Util->is_module_loaded($name) as a way to
check if a module has been loaded
  • Loading branch information
eri-trabiccolo committed Oct 31, 2019
1 parent cbf476e commit 0a1a007
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
33 changes: 26 additions & 7 deletions includes/modules/class-llms-module-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,8 @@ private function load() {

foreach ( $to_load as $module ) {

// define the constant as true if it hasn't been defined explicitly.
if ( ! defined( $module['constant_name'] ) ) {
define( $module['constant_name'], true );
}

// bail, if the constant's value is explcitly defined to false.
if ( constant( $module['constant_name'] ) === false ) {
// bail, if the constant's value is explicitly defined to false.
if ( defined( $module['constant_name'] ) && constant( $module['constant_name'] ) === false ) {
continue;
}

Expand Down Expand Up @@ -162,6 +157,30 @@ private function load() {

}

/**
* Checks whether a module has been loaded.
*
* @since [version]
*
* @param string $name Module name.
* @return bool Whether or not the given module has been loaded.
*/
public function is_module_loaded( $name ) {

$loaded = in_array( $name, wp_list_pluck( $this->loaded, 'name' ), true );

/**
* Filters whether or not a module has been loaded.
*
* @since [version]
*
* @param bool $loaded Whether or not the given module has been loaded.
* @param LLMS_Module_Loader $request This LLMS_Module_Loader instance.
*/
return apply_filters( 'llms_is_module_loaded', $loaded, $this );

}

/**
* Loads Module Information.
*
Expand Down
66 changes: 66 additions & 0 deletions tests/unit-tests/modules/class-llms-test-module-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Tests for LifterLMS Module loader
*
* @group modules
*
* @since [version]
* @version [version]
*/
class LLMS_Test_Module_Loader extends LLMS_UnitTestCase {

/**
* Test the modules loading.
*
* @since [version]
*
* @return void
*/
public function test_load() {

// get modules to be loades.
$modules = $this->_get_load_info();

// check they've been loaded.
foreach ( $modules as &$module ) {
$module['actions'] = did_action( "lifterlms_module_{$module['name']}_loaded" );
$this->assertEquals( 1, $module['actions'] );
LLMS_Module_Loader::instance()->is_module_loaded( $module['name'] );
}

// define module constants to false so to skip the loading.
foreach ( $modules as $module ) {
! defined( $module['constant_name'] ) && define( $module['constant_name'], FALSE );
}

// Fire the loading once again.
$loaded = LLMS_Unit_Test_Util::call_method( LLMS_Module_Loader::instance(), 'load' );

// check they've not been loaded this time.
$this->assertEquals( array(), $loaded );

foreach ( $modules as &$module ) {
$actions_counter = $module['actions'];
$module['actions'] = did_action( "lifterlms_module_{$module['name']}_loaded" );
$this->assertEquals( $actions_counter, $module['actions'] );
}

}

/**
* Get the array of modules to load.
* Of the type:
* $module = array(
* 'name' => 'module-name',
* 'file_path' => 'lifterlms/includes/modules/module-name/class-llms-module-name.php',
* 'constant_name' => 'LLMS_MODULE_NAME',
* );
*
* @since [version]
*
* @return array
*/
private function _get_load_info() {
return LLMS_Unit_Test_Util::call_method( LLMS_Module_Loader::instance(), 'load_info' );
}
}

0 comments on commit 0a1a007

Please sign in to comment.