Skip to content

Commit

Permalink
Mock installables app during PHPUnit tests
Browse files Browse the repository at this point in the history
Fixes issues resulting from the installables app not being mocked during
some tests, leading to the installable remaining registered with the
app, and perpetuating its version in the database. In order to fully fix
this, we needed to also allow the installable to be registered more than
once, so that it would have its app registered again. Some issues with
the tests were also revealed by this, and fixed.

We also realized that we use 'extension' as the type for the
installables registered in the new code, but the legacy code was still
using 'module', so we've standardized on 'extension'.

See #404
  • Loading branch information
JDGrimes committed Aug 22, 2017
1 parent 9ebf246 commit 2e516a7
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 20 deletions.
6 changes: 1 addition & 5 deletions src/classes/installables.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,13 @@ public static function register( $type, $slug, $data ) {
, 'WordPoints_Installables_App::register'
);

if ( isset( self::$registered[ $type ][ $slug ] ) ) {
return false;
}

self::$registered[ $type ][ $slug ] = $data;

/** @var WordPoints_Installables_App $installables */
$installables = wordpoints_apps()->get_sub_app( 'installables' );
$installables->register(
$type
, $slug
, 'module' === $slug ? 'extension' : $slug
, 'WordPoints_Installables::installer_loader'
, $data['version']
, ! empty( $data['network_wide'] )
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/classes/installable/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class WordPoints_Installable_Extension_Test extends WordPoints_PHPUnit_TestCase
*/
public function test_get_version() {

$this->mock_apps();

WordPoints_Modules::register(
'
Extension Name: Demo Module
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/classes/updater/core/extension/merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class WordPoints_Updater_Core_Extension_Merge_Test extends WordPoints_PHPUnit_Te
*/
public function test_deactivates_extension() {

$this->mock_apps();

add_filter( 'wordpoints_extensions_dir', 'wordpoints_phpunit_extensions_dir' );

$extension = 'wordpointsorg/wordpointsorg.php';
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/tests/components.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ public function test_register_registers_installable() {
*
* @covers WordPoints_Components::activate
* @covers WordPoints_Components::is_active
*
* @expectedDeprecated WordPoints_Installables::register
*/
public function test_activation() {

$this->mock_apps();

$components = WordPoints_Components::instance();
$components->register(
array(
Expand Down
68 changes: 54 additions & 14 deletions tests/phpunit/tests/installables.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class WordPoints_Installables_Test extends WordPoints_PHPUnit_TestCase_Admin {
*/
public function register_installables() {

$this->mock_apps();

WordPoints_Installables::register(
'module'
, 'test'
Expand Down Expand Up @@ -83,6 +85,8 @@ public function test_install() {
*/
public function test_install_network_wide() {

$this->mock_apps();

$installer = WordPoints_Installables::install( 'module', 'network_test', true );

/** @var WordPoints_Un_Installer_Module_Mock $installer */
Expand Down Expand Up @@ -118,6 +122,8 @@ public function test_install_not_registered() {
*/
public function test_install_no_un_installer() {

$this->mock_apps();

WordPoints_Installables::register(
'module'
, __METHOD__
Expand Down Expand Up @@ -180,6 +186,8 @@ public function test_uninstall_not_registered() {
*/
public function test_uninstall_no_un_installer() {

$this->mock_apps();

WordPoints_Installables::register(
'module'
, __METHOD__
Expand All @@ -200,16 +208,19 @@ public function test_uninstall_no_un_installer() {
*
* @since 2.0.0
*
* @expectedDeprecated WordPoints_Installables::register
* @expectedDeprecated WordPoints_Installables::maybe_do_updates
*/
public function test_maybe_do_updates() {

$this->set_module_db_version( 'test', '0.9.2' );
$this->assertSame( '0.9.2', $this->get_module_db_version( 'test' ) );
$this->register_installables();

$this->set_extension_db_version( 'test', '0.9.2' );
$this->assertSame( '0.9.2', $this->get_extension_db_version( 'test' ) );

WordPoints_Installables::maybe_do_updates();

$this->assertSame( '1.0.0', $this->get_module_db_version( 'test' ) );
$this->assertSame( '1.0.0', $this->get_extension_db_version( 'test' ) );
}

/**
Expand All @@ -219,16 +230,19 @@ public function test_maybe_do_updates() {
*
* @requires WordPoints network-active
*
* @expectedDeprecated WordPoints_Installables::register
* @expectedDeprecated WordPoints_Installables::maybe_do_updates
*/
public function test_maybe_do_updates_network() {

$this->set_module_db_version( 'network_test', '0.9.2', true );
$this->assertSame( '0.9.2', $this->get_module_db_version( 'network_test', true ) );
$this->register_installables();

$this->set_extension_db_version( 'network_test', '0.9.2', true );
$this->assertSame( '0.9.2', $this->get_extension_db_version( 'network_test', true ) );

WordPoints_Installables::maybe_do_updates();

$this->assertSame( '1.0.0', $this->get_module_db_version( 'network_test', true ) );
$this->assertSame( '1.0.0', $this->get_extension_db_version( 'network_test', true ) );
}

/**
Expand All @@ -240,11 +254,11 @@ public function test_maybe_do_updates_network() {
*/
public function test_maybe_do_updates_not_installed() {

$this->assertSame( '', $this->get_module_db_version( 'test' ) );
$this->assertSame( '', $this->get_extension_db_version( 'test' ) );

WordPoints_Installables::maybe_do_updates();

$this->assertSame( '1.0.0', $this->get_module_db_version( 'test' ) );
$this->assertSame( '', $this->get_extension_db_version( 'test' ) );
}

/**
Expand All @@ -256,20 +270,44 @@ public function test_maybe_do_updates_not_installed() {
*/
public function test_maybe_do_updates_wordpoints_not_installed() {

$this->mock_apps();

wordpoints_delete_maybe_network_option( 'wordpoints_installable_versions' );

wordpoints_update_maybe_network_option(
'wordpoints_data'
, array( 'a' => 'b' )
);

/** @var WordPoints_Installables_App $installables */
$installables = wordpoints_apps()->get_sub_app( 'installables' );
$installables->register(
'plugin'
, 'wordpoints'
, array( $this, 'wordpoints_installable_loader' )
, WORDPOINTS_VERSION
);

$this->assertSame( '', $this->wordpoints_get_db_version() );

WordPoints_Installables::maybe_do_updates();

$this->assertSame( WORDPOINTS_VERSION, $this->wordpoints_get_db_version() );
}

/**
* Returns a mock installable for WordPoints.
*
* This avoids using the core installable itself, which lead to issues.
*
* @since 2.4.0
*
* @return WordPoints_Installable_Basic The installable object.
*/
public function wordpoints_installable_loader() {
return new WordPoints_Installable_Basic( 'plugin', 'wordpoints', WORDPOINTS_VERSION );
}

/**
* Test updating an entity that is up to date.
*
Expand All @@ -279,12 +317,12 @@ public function test_maybe_do_updates_wordpoints_not_installed() {
*/
public function test_maybe_do_updates_up_to_date() {

$this->set_module_db_version( 'test', '1.0.0' );
$this->assertSame( '1.0.0', $this->get_module_db_version( 'test' ) );
$this->set_extension_db_version( 'test', '1.0.0' );
$this->assertSame( '1.0.0', $this->get_extension_db_version( 'test' ) );

WordPoints_Installables::maybe_do_updates();

$this->assertSame( '1.0.0', $this->get_module_db_version( 'test' ) );
$this->assertSame( '1.0.0', $this->get_extension_db_version( 'test' ) );
}

/**
Expand All @@ -296,12 +334,12 @@ public function test_maybe_do_updates_up_to_date() {
*/
public function test_maybe_do_updates_newer_db_version() {

$this->set_module_db_version( 'test', '1.1.0' );
$this->assertSame( '1.1.0', $this->get_module_db_version( 'test' ) );
$this->set_extension_db_version( 'test', '1.1.0' );
$this->assertSame( '1.1.0', $this->get_extension_db_version( 'test' ) );

WordPoints_Installables::maybe_do_updates();

$this->assertSame( '1.1.0', $this->get_module_db_version( 'test' ) );
$this->assertSame( '1.1.0', $this->get_extension_db_version( 'test' ) );
}

/**
Expand Down Expand Up @@ -448,6 +486,8 @@ public function test_get_installer_nonexistent() {
*/
public function test_get_installer_nonexistent_file() {

$this->mock_apps();

WordPoints_Installables::register(
'module'
, __METHOD__
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/tests/modules/class-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class WordPoints_Modules_Class_Test extends WordPoints_PHPUnit_TestCase {
*/
public function test_parsing_header() {

$this->mock_apps();

WordPoints_Modules::register(
'
Extension Name: Demo Module
Expand Down Expand Up @@ -80,6 +82,8 @@ public function test_parsing_header() {
*/
public function test_get_data() {

$this->mock_apps();

WordPoints_Modules::register(
'
Extension Name: Demo Module
Expand Down
12 changes: 11 additions & 1 deletion tests/phpunit/tests/modules/module-headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public function test_module_header_parsing_with_markup() {
*/
public function test_uses_wordpoints_modules_data_if_available() {

$this->mock_apps();

$mock_filter = new WordPoints_PHPUnit_Mock_Filter();

add_filter( 'extra_wordpoints_module_headers', array( $mock_filter, 'filter' ) );
Expand Down Expand Up @@ -137,6 +139,8 @@ public function test_uses_wordpoints_modules_data_if_available() {
*/
public function test_update_api_header_deprecated() {

$this->mock_apps();

WordPoints_Modules::register(
'
Extension Name: Test 3
Expand Down Expand Up @@ -173,6 +177,8 @@ public function test_update_api_header_deprecated() {
*/
public function test_channel_header_deprecated() {

$this->mock_apps();

WordPoints_Modules::register(
'
Extension Name: Test 3
Expand Down Expand Up @@ -209,6 +215,8 @@ public function test_channel_header_deprecated() {
*/
public function test_module_name_header_deprecated() {

$this->mock_apps();

WordPoints_Modules::register(
'
Module Name: Test 3
Expand Down Expand Up @@ -241,7 +249,9 @@ public function test_module_name_header_deprecated() {
*
* @expectedDeprecated wordpoints_get_module_data
*/
public function test_module_uir_header_deprecated() {
public function test_module_uri_header_deprecated() {

$this->mock_apps();

WordPoints_Modules::register(
'
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/modules/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function test_module_basename() {
*/
public function test_get_modules() {

$this->mock_apps();

// This module uses the 2.0 API, and for a more realistic test, it helps if
// it is pre-loaded (which the modules aren't during the tests).
include_once( wordpoints_extensions_dir() . '/test-6/main-file.php' );
Expand Down

0 comments on commit 2e516a7

Please sign in to comment.