Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c3170fe
Added: Mechanism to not load module if core version is available
mukeshpanchal27 Jun 22, 2022
6219d96
Added: New line
mukeshpanchal27 Jun 22, 2022
42a82fc
Fix: Code linting
mukeshpanchal27 Jun 22, 2022
5294f83
Fix: Docblock for functions
mukeshpanchal27 Jun 22, 2022
eef08c5
Update some tests
mukeshpanchal27 Jun 22, 2022
1ee0028
Fix: PHP lint
mukeshpanchal27 Jun 22, 2022
8750d5a
Simplify boolean check for function existence.
felixarntz Jun 22, 2022
f06ba7d
Fix: Text related changes
mukeshpanchal27 Jun 23, 2022
073d872
Update: logic for perflab_can_load_module
mukeshpanchal27 Jun 24, 2022
a0235c8
Fix: Feedback related to disabled checkbox
mukeshpanchal27 Jun 29, 2022
ca1cd0f
Fix: Lint white space issue
mukeshpanchal27 Jun 29, 2022
3f6244d
Perflab_get_active_and_valid_modules() is introduced, and review is i…
mukeshpanchal27 Jun 29, 2022
7c5365c
Fix: Admin settings review
mukeshpanchal27 Jun 30, 2022
7dda6e8
Fix: review of the new function
mukeshpanchal27 Jun 30, 2022
41b2834
Fix: review of tests
mukeshpanchal27 Jun 30, 2022
4d1e220
Added: Unit test for new functions
mukeshpanchal27 Jun 30, 2022
2371a46
Fix: Lint
mukeshpanchal27 Jun 30, 2022
e432764
Unit test for perflab_can_load_module()
mukeshpanchal27 Jul 1, 2022
acd3ed5
Fix: Lint
mukeshpanchal27 Jul 1, 2022
110656c
Feedback implemented
mukeshpanchal27 Jul 4, 2022
d403b7a
Update unit tests in response to the review
mukeshpanchal27 Jul 4, 2022
232d462
Correct lint
mukeshpanchal27 Jul 4, 2022
3388874
Update function name for test
mukeshpanchal27 Jul 4, 2022
a156b1a
Address code review feedback
mukeshpanchal27 Jul 7, 2022
ac6163a
Fix minor lint issue
mukeshpanchal27 Jul 7, 2022
b759790
Address review feedback
mukeshpanchal27 Jul 8, 2022
09c507f
Fix: Minor lind error
mukeshpanchal27 Jul 8, 2022
88eca52
Merge branch 'trunk' into fix/293-mechanism-not-load-module
felixarntz Jul 8, 2022
e54b7d7
Add test case for file_exists check in perflab_is_valid_module().
felixarntz Jul 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address code review feedback
  • Loading branch information
mukeshpanchal27 committed Jul 7, 2022
commit a156b1aa74293fcfeae71a0c905d890b72c1046f
39 changes: 16 additions & 23 deletions load.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,27 @@ function( $module_settings ) {
*
* @since n.e.x.t
*
* @param array $modules List of active module slugs.
* @return array List of active valid module slugs.
* @param string $module Slug of the module.
* @return null|string Module slug name on success, null on failure.
*/
function perflab_get_valid_modules( array $modules = array() ) {
function perflab_is_valid_module( $module ) {

if ( ! is_array( $modules ) ) {
return array();
if ( empty( $module ) ) {
return;
}

$valid_modules = array();
foreach ( $modules as $module ) {

// Do not load module if no longer exists.
$module_file = plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/load.php';
if ( ! file_exists( $module_file ) ) {
continue;
}

// Do not load module if it cannot be loaded, e.g. if it was already merged and is available in WordPress core.
if ( ! perflab_can_load_module( $module ) ) {
continue;
}
// Do not load module if no longer exists.
$module_file = plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/load.php';
if ( ! file_exists( $module_file ) ) {
return;
}

$valid_modules[] = $module;
// Do not load module if it cannot be loaded, e.g. if it was already merged and is available in WordPress core.
if ( ! perflab_can_load_module( $module ) ) {
return;
}

return $valid_modules;
return $module;
}

/**
Expand All @@ -179,7 +173,7 @@ function perflab_get_valid_modules( array $modules = array() ) {
* @since 1.1.0
*/
function perflab_get_generator_content() {
$active_and_valid_modules = perflab_get_valid_modules( perflab_get_active_modules() );
$active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' );

return sprintf(
'Performance Lab %1$s; modules: %2$s',
Expand Down Expand Up @@ -237,8 +231,7 @@ function perflab_can_load_module( $module ) {
* @since n.e.x.t Renamed to perflab_load_active_and_valid_modules().
*/
function perflab_load_active_and_valid_modules() {
$active_and_valid_modules = perflab_get_valid_modules( perflab_get_active_modules() );

$active_and_valid_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' );;
if ( empty( $active_and_valid_modules ) ) {
return;
}
Expand Down
71 changes: 31 additions & 40 deletions tests/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ function() use ( $active_modules ) {
return $active_modules;
}
);
$expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $active_modules );
$content = perflab_get_generator_content();
$active_modules = array_filter( perflab_get_active_modules(), 'perflab_is_valid_module' );
$expected = 'Performance Lab ' . PERFLAB_VERSION . '; modules: ' . implode( ', ', $active_modules );
$content = perflab_get_generator_content();
$this->assertSame( $expected, $content );
}

Expand All @@ -150,50 +151,40 @@ public function test_perflab_render_generator() {
$this->assertContains( $expected, $output );
}

public function test_perflab_get_valid_modules() {
global $wp_version;

// Assert that it returns the empty array for dummy modules.
$dummy_active_modules = array(
'../tests/testdata/demo-modules/javascript/demo-module-1',
'../tests/testdata/demo-modules/something/demo-module-2',
'../tests/testdata/demo-modules/images/demo-module-3',
);

$output = perflab_get_valid_modules( $dummy_active_modules );
$this->assertIsArray( $output );
$this->assertSame( array( '../tests/testdata/demo-modules/something/demo-module-2', '../tests/testdata/demo-modules/images/demo-module-3' ), $output );

// Assert that it returns the array for modules.
if ( $wp_version >= '6.1' ) {
$active_modules = require plugin_dir_path( PERFLAB_MAIN_FILE ) . 'default-enabled-modules.php';
add_filter(
'perflab_active_modules',
function() use ( $active_modules ) {
return $active_modules;
}
);
$output = perflab_get_valid_modules( perflab_get_active_modules() );
public function test_empty_module_for_perflab_is_valid_module() {
// Assert that it return null for empty module.
$this->assertEmpty( perflab_is_valid_module( '' ) );
}

// Remove Image module as it will marge in 6.1.
$remove_marge_modules = array_shift( $active_modules );
$this->assertSame( $active_modules, $output );
}
/**
* @dataProvider provider_dummy_valid_modules
*/
public function test_perflab_is_valid_module( $dummy_module ) {
$output = perflab_is_valid_module( $dummy_module );
$this->assertNotEmpty( $output );
$this->assertIsString( $output );
}

public function test_perflab_can_load_module() {
// Assert that it validates the ability to load modules that are not in the core.
$demo_modules = array(
'javascript/demo-module-1' => false,
'something/demo-module-2' => true,
'images/demo-module-3' => true,
public function provider_dummy_valid_modules() {
return array(
array( '../tests/testdata/demo-modules/something/demo-module-2' ),
array( '../tests/testdata/demo-modules/images/demo-module-3' ),
);
}

foreach ( $demo_modules as $module => $can_load ) {
$output = perflab_can_load_module( '../tests/testdata/demo-modules/' . $module );
$this->assertSame( $can_load, $output );
}
/**
* @dataProvider provider_dummy_can_load_modules
*/
public function test_perflab_can_load_module( $dummy_can_load_modules, $module_status ) {
$this->assertSame( $module_status, perflab_can_load_module( $dummy_can_load_modules ) );
}

public function provider_dummy_can_load_modules() {
return array(
array( '../tests/testdata/demo-modules/javascript/demo-module-1', false ),
array( '../tests/testdata/demo-modules/something/demo-module-2', true ),
array( '../tests/testdata/demo-modules/images/demo-module-3', true ),
);
}

private function get_expected_default_option() {
Expand Down