Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add generic connected hook #92

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions src/Uplink/Auth/Admin/Connect_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,14 @@ public function maybe_store_token_data(): void {
* @param \StellarWP\Uplink\Resources\Resource $plugin The plugin that was connected.
*/
do_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $slug . '/connected', $plugin );

/**
* Fires after a plugin has been connected.
*
* @since 2.2.2
*
* @param \StellarWP\Uplink\Resources\Resource $plugin The plugin that was connected.
*/
do_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected', $plugin );
redscar marked this conversation as resolved.
Show resolved Hide resolved
}
}
19 changes: 19 additions & 0 deletions src/Uplink/Auth/Admin/Disconnect_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use StellarWP\Uplink\Notice\Notice_Handler;
use StellarWP\Uplink\Notice\Notice;
use StellarWP\Uplink\Resources\Resource;
use StellarWP\Uplink\Config;

final class Disconnect_Controller {

Expand Down Expand Up @@ -111,6 +112,24 @@ public function maybe_disconnect(): void {
true
)
);

/**
* Fires after a plugin has been disconnected.
*
* @since 2.2.2
*
* @param string $slug The plugin slug that was disconnected.
*/
do_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $_GET[ self::SLUG ] . '/disconnected', $_GET[ self::SLUG ] );

/**
* Fires after a plugin has been disconnected.
*
* @since 2.2.2
*
* @param string $slug The plugin slug that was disconnected.
*/
do_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/disconnected', $_GET[ self::SLUG ] );
} else {
$this->notice->add(
new Notice( Notice::ERROR,
Expand Down
7 changes: 7 additions & 0 deletions tests/wpunit/Auth/Admin/ConnectControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function test_it_stores_basic_token_data(): void {

$this->assertSame( $token, $this->token_manager->get( $this->plugin ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/connected' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected' ) );
}

public function test_it_sets_additional_license_key(): void {
Expand Down Expand Up @@ -113,6 +114,7 @@ public function test_it_sets_additional_license_key(): void {
$this->assertSame( $token, $this->token_manager->get( $this->plugin ) );
$this->assertSame( $this->plugin->get_license_key(), $license );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/connected' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected' ) );
}

public function test_it_does_not_store_with_an_invalid_nonce(): void {
Expand All @@ -134,6 +136,7 @@ public function test_it_does_not_store_with_an_invalid_nonce(): void {

$this->assertNull( $this->token_manager->get( $this->plugin ) );
$this->assertEmpty( did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/connected' ) );
$this->assertEmpty( did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected' ) );
}

public function test_it_does_not_store_an_invalid_token(): void {
Expand All @@ -159,6 +162,7 @@ public function test_it_does_not_store_an_invalid_token(): void {

$this->assertNull( $this->token_manager->get( $this->plugin ) );
$this->assertEmpty( did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/connected' ) );
$this->assertEmpty( did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected' ) );
}

public function test_it_does_not_stores_token_or_license_with_a_slug_that_does_not_exist(): void {
Expand Down Expand Up @@ -224,6 +228,7 @@ public function test_it_stores_token_but_not_license_without_a_license(): void {
$this->assertSame( $token, $this->token_manager->get( $this->plugin ) );
$this->assertEmpty( $this->plugin->get_license_key() );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/connected' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected' ) );
}

/**
Expand Down Expand Up @@ -266,6 +271,7 @@ public function test_it_sets_token_and_additional_license_key_on_multisite_netwo
$this->assertSame( $token, $this->token_manager->get( $this->plugin ) );
$this->assertSame( $this->plugin->get_license_key( 'network' ), $license );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/connected' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected' ) );
}

/**
Expand Down Expand Up @@ -310,6 +316,7 @@ public function test_it_stores_token_data_on_subfolder_subsite(): void {
$this->assertSame( $token, $this->token_manager->get( $this->plugin ) );
$this->assertSame( $this->plugin->get_license_key( 'network' ), $license );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/connected' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/connected' ) );
}

}
224 changes: 224 additions & 0 deletions tests/wpunit/Auth/Admin/DisconnectControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
<?php declare( strict_types=1 );

namespace StellarWP\Uplink\Tests\Auth\Admin;

use StellarWP\Uplink\Auth\Action_Manager;
use StellarWP\Uplink\Auth\Admin\Disconnect_Controller;
use StellarWP\Uplink\Auth\Nonce;
use StellarWP\Uplink\Auth\Token\Contracts\Token_Manager;
use StellarWP\Uplink\Config;
use StellarWP\Uplink\Register;
use StellarWP\Uplink\Resources\Resource;
use StellarWP\Uplink\Tests\Sample_Plugin;
use StellarWP\Uplink\Tests\UplinkTestCase;
use StellarWP\Uplink\Uplink;
use WP_Error;
use WP_Screen;

final class DisconnectControllerTest extends UplinkTestCase {

/**
* @var Token_Manager
*/
private $token_manager;

/**
* The sample plugin slug
*
* @var string
*/
private $slug = 'sample';

private $token = '53ca40ab-c6c7-4482-a1eb-14c56da31015';

/**
* @var Resource
*/
private $plugin;

protected function setUp(): void {
parent::setUp();

// Configure the token prefix.
Config::set_token_auth_prefix( 'kadence_' );

// Run init again to reload the Token/Rest Providers.
Uplink::init();

$this->assertSame(
'kadence_' . Token_Manager::TOKEN_SUFFIX,
$this->container->get( Config::TOKEN_OPTION_NAME )
);

$this->token_manager = $this->container->get( Token_Manager::class );

// Register the sample plugin as a developer would in their plugin.
$this->plugin = Register::plugin(
$this->slug,
'Lib Sample',
'1.0.10',
'uplink/index.php',
Sample_Plugin::class
);

// Set a license key.
$this->plugin->set_license_key( '123456', 'network' );

// Set a token.
$this->token_manager->store( $this->token, $this->plugin );
}

public function test_it_disconnects_data(): void {
global $_GET;

wp_set_current_user( 1 );

$this->assertEquals( $this->token, $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );

// Mock these were passed via the query string.
$_GET[ Disconnect_Controller::ARG ] = 1;
$_GET[ Disconnect_Controller::CACHE_KEY ] = 'nada';
$_GET[ Disconnect_Controller::SLUG ] = $this->slug;
$_GET['_wpnonce'] = wp_create_nonce( Disconnect_Controller::ARG );

// Mock we're an admin inside the dashboard.
$this->admin_init();

// Fire off the specification action tied to this slug.
do_action( $this->container->get( Action_Manager::class )->get_hook_name( $this->slug ) );

$this->assertNull( $this->token_manager->get( $this->plugin ) );
// see how the license is still stored!
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/disconnected' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/disconnected' ) );
}

public function test_it_does_not_disconnect_with_an_invalid_nonce(): void {
global $_GET;

wp_set_current_user( 1 );

$this->assertEquals( $this->token, $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );

// Mock these were passed via the query string.
$_GET[ Disconnect_Controller::ARG ] = 1;
$_GET[ Disconnect_Controller::CACHE_KEY ] = 'nada';
$_GET[ Disconnect_Controller::SLUG ] = $this->slug;
$_GET['_wpnonce'] = 'invalid-nonce';

// Mock we're an admin inside the dashboard.
$this->admin_init();

$this->assertEquals( $this->token, $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );
$this->assertEmpty( did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/disconnected' ) );
$this->assertEmpty( did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/disconnected' ) );
}

public function test_it_does_not_disconnect_with_a_slug_that_does_not_exist(): void {
global $_GET;

wp_set_current_user( 1 );

$this->assertEquals( $this->token, $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );

// Mock these were passed via the query string.
$_GET[ Disconnect_Controller::ARG ] = 1;
$_GET[ Disconnect_Controller::CACHE_KEY ] = 'nada';
$_GET[ Disconnect_Controller::SLUG ] = 'a-plugin-slug-that-does-not-exist';
$_GET['_wpnonce'] = wp_create_nonce( Disconnect_Controller::ARG );

// Mock we're an admin inside the dashboard.
$this->admin_init();

// Fire off the specification action tied to this slug.
do_action( $this->container->get( Action_Manager::class )->get_hook_name( $this->slug ) );

$this->assertEquals( $this->token, $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );
$this->assertEmpty( did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/a-plugin-slug-that-does-not-exist/disconnected' ) );
}

/**
* @env multisite
*/
public function test_it_disconnects_on_multisite_network(): void {
global $_GET;

// Create a subsite, but we won't use it.
$sub_site_id = wpmu_create_blog( 'wordpress.test', '/sub1', 'Test Subsite', 1 );
$this->assertNotInstanceOf( WP_Error::class, $sub_site_id );
$this->assertGreaterThan( 1, $sub_site_id );
$this->assertTrue( is_multisite() );
wp_set_current_user( 1 );

// Mock our sample plugin is network activated, otherwise license key check fails.
$this->assertTrue( update_site_option( 'active_sitewide_plugins', [
'uplink/index.php' => time(),
] ) );
$this->assertEquals( $this->token, $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );

// Mock these were passed via the query string.
$_GET[ Disconnect_Controller::ARG ] = 1;
$_GET[ Disconnect_Controller::CACHE_KEY ] = 'nada';
$_GET[ Disconnect_Controller::SLUG ] = $this->slug;
$_GET['_wpnonce'] = wp_create_nonce( Disconnect_Controller::ARG );

// Mock we're an admin inside the NETWORK dashboard.
$this->admin_init( true );

// Fire off the specification action tied to this slug.
do_action( $this->container->get( Action_Manager::class )->get_hook_name( $this->slug ) );

$this->assertNull( $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/disconnected' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/disconnected' ) );
}

/**
* @env multisite
*/
public function test_it_disconnects_on_subfolder_subsite(): void {
global $_GET;

// Create a subsite.
$sub_site_id = wpmu_create_blog( 'wordpress.test', '/sub1', 'Test Subsite', 1 );
$this->assertNotInstanceOf( WP_Error::class, $sub_site_id );
$this->assertGreaterThan( 1, $sub_site_id );
$this->assertTrue( is_multisite() );

// Use this site, which should not allow a token to be set.
switch_to_blog( $sub_site_id );
wp_set_current_user( 1 );

// Mock our sample plugin is network activated, otherwise license key check fails.
$this->mock_activate_plugin( 'uplink/index.php', true );

$this->assertEquals( $this->token, $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );

// Mock these were passed via the query string.
$_GET[ Disconnect_Controller::ARG ] = 1;
$_GET[ Disconnect_Controller::CACHE_KEY ] = 'nada';
$_GET[ Disconnect_Controller::SLUG ] = $this->slug;
$_GET['_wpnonce'] = wp_create_nonce( Disconnect_Controller::ARG );

// Mock we're in the subsite admin.
$this->admin_init();

// Fire off the specification action tied to this slug.
do_action( $this->container->get( Action_Manager::class )->get_hook_name( $this->slug ) );

$this->assertNull( $this->token_manager->get( $this->plugin ) );
$this->assertEquals( '123456', $this->plugin->get_license_key( 'network' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/' . $this->slug . '/disconnected' ) );
$this->assertEquals( 1, did_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/disconnected' ) );
}

}
Loading