Skip to content

Commit

Permalink
test(wpunit) cover update of OAuth field
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Sep 3, 2024
1 parent 84747bd commit 3577725
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/Uplink/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public static function plugin( $slug, $name, $version, $path, $class, $license_c
* @since 1.0.0
* @since 2.0.0 Added oAuth parameter.
*
* @param string $slug Resource slug.
* @param string $name Resource name.
* @param string $version Resource version.
* @param string $path Resource path to bootstrap file.
* @param string $class Resource class.
* @param string $slug Resource slug.
* @param string $name Resource name.
* @param string $version Resource version.
* @param string $path Resource path to bootstrap file.
* @param string $class Resource class.
* @param string $license_class Resource license class.
* @param bool $is_oauth Is the plugin using OAuth?
* @param bool $oauth Whether the plugin uses OAuth (bool) or a set of OAuth options (int).
*
* @return Resources\Resource
*/
public static function service( $slug, $name, $version, $path, $class, $license_class = null, $is_oauth = false) {
return Resources\Service::register( $slug, $name, $version, $path, $class, $license_class, $is_oauth );
public static function service( $slug, $name, $version, $path, $class, $license_class = null, $oauth = false) {
return Resources\Service::register( $slug, $name, $version, $path, $class, $license_class, $oauth );
}
}
14 changes: 14 additions & 0 deletions src/views/admin/fields/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* Fires before the field.
*
* @since 2.0.0
*
* @param string $slug The slug of the field.
*/
do_action( 'stellarwp/uplink/' . Config::get_hook_prefix(). '/license_field_before_field', $field->get_slug() );
?>
Expand All @@ -35,6 +37,18 @@ class="<?php echo esc_attr( $field->get_classes() ); ?>"
>
<fieldset class="stellarwp-uplink__settings-group">
<?php settings_fields( $group ); ?>

<?php
/**
* Fires before the license key input is printed on the page.
*
* @since TBD
*
* @param string $slug The slug of the field.
*/
do_action( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/license_field_before_input', $field->get_slug() );
?>

<input
type="text"
name="<?php echo esc_attr( $field->get_field_name() ); ?>"
Expand Down
73 changes: 68 additions & 5 deletions tests/wpunit/Admin/Fields/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@

namespace StellarWP\Uplink\Admin\Fields;

use StellarWP\ContainerContract\ContainerInterface;
use StellarWP\Uplink\Admin\Group;
use StellarWP\Uplink\Auth\Token\Contracts\Token_Manager;
use StellarWP\Uplink\Auth\Token\Token_Manager as Concrete_Token_Manager;
use StellarWP\Uplink\Config;
use StellarWP\Uplink\Register;
use StellarWP\Uplink\Resources\Collection;
use StellarWP\Uplink\Tests\UplinkTestCase;
use StellarWP\Uplink\Resources\Resource;
use StellarWP\Uplink\Tests\TestUtils;
use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
use StellarWP\Uplink\Tests\Traits\With_Uopz;
use StellarWP\Uplink\Tests\UplinkTestCase;
use StellarWP\Uplink\Uplink;
use tad\Codeception\SnapshotAssertions\SnapshotAssertions;

class FieldTest extends UplinkTestCase {

use TestUtils;
use SnapshotAssertions;
use With_Uopz;

/**
* @var ContainerInterface|null
*/
private $original_container = null;

public function resourceProvider() {
$resources = $this->get_test_resources();

Expand All @@ -39,6 +49,23 @@ public function setup_container_get_slug( $resource ) {
return $collection->get( $resource['slug'] );
}

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

// Clone the original container used in all tests to avoid polluting the global container with custom bindings.
$this->original_container = $this->container;
$this->container = clone $this->container;
// Bind the token manager to the cloned container.
$this->container->singleton( Token_Manager::class, static function ( $c ) {
return new Concrete_Token_Manager( 'something' );
} );
}

public function tearDown(): void {
$this->container = $this->original_container;
parent::tearDown();
}

/**
* @test
* @dataProvider resourceProvider
Expand All @@ -63,8 +90,11 @@ public function it_should_get_fields_with_slug( $resource ) {
$this->assertEquals( $current_resource->get_path(), $field->get_product() );
$this->assertEquals( $field_name, $field->get_field_name() );
$this->assertEquals( 'stellarwp_uplink_license_key_' . $slug, $field->get_field_id() );
$this->assertEquals( $license_key, $field->get_field_value(), 'Field value should be equal to the license key' );
$this->assertStringContainsString( 'A valid license key is required for support and updates', $field->get_key_status_html() );
$this->assertEquals( $license_key,
$field->get_field_value(),
'Field value should be equal to the license key' );
$this->assertStringContainsString( 'A valid license key is required for support and updates',
$field->get_key_status_html() );
$this->assertEquals( 'License key', $field->get_placeholder() );
$this->assertEquals( 'stellarwp-uplink-license-key-field', $field->get_classes() );
}
Expand Down Expand Up @@ -145,7 +175,9 @@ public function it_should_get_nonce_action_and_field( $resource ) {
$nonce_value = $matches[1];

$this->assertNotEmpty( $nonce_action, 'Nonce action should not be empty.' );
$this->assertStringContainsString( 'stellarwp-uplink-license-key-nonce__' . $field->get_slug(), $nonce_field, 'Nonce field should contain the correct action slug.' );
$this->assertStringContainsString( 'stellarwp-uplink-license-key-nonce__' . $field->get_slug(),
$nonce_field,
'Nonce field should contain the correct action slug.' );

// Validate the nonce
$is_valid_nonce = wp_verify_nonce( $nonce_value, $nonce_action );
Expand Down Expand Up @@ -213,4 +245,35 @@ public function it_should_handle_empty_label( $resource ) {

$this->assertEmpty( $field->get_label(), 'Label should be empty by default' );
}

/**
* @test
*/
public function it_should_render_correct_html_for_oauth_resource_with_license_key() {
$this->set_fn_return( 'wp_create_nonce', '535281edcd' );
$this->set_fn_return( 'wp_generate_password', '535281edcd535281ed' );
$slug = 'service-oauth-with-license-key-field-1';
Register::service(
$slug,
'Service OAuth With License Key Field 1',
'1.0.0',
$this->get_base() . '/service-oauth-with-license-key-field-1.php',
Uplink::class,
Uplink::class,
Resource::OAUTH_REQUIRES_LICENSE_KEY
);
add_filter( 'stellarwp/uplink/test/auth/can_auth', '__return_true' );
update_option( 'test_storage', [
'stellarwp_auth_url_service_oauth_with_license_key_field_1' => [
'expiration' => 0,
'value' => 'https://licensing.stellarwp.com/account-auth?uplink_domain=&uplink_slug=service-oauth-with-license-key-field-1&_uplink_nonce=535281edcd',
]
] );
$collection = $this->container->get( Collection::class );

$field = $this->container->get( Field::class )->set_resource( $collection->get( $slug ) );
$html = $field->get_render_html();

$this->assertMatchesHtmlSnapshot( $html );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

<div class="stellarwp-uplink__license-field">
<div
class="stellarwp-uplink-license-key-field"
id="/var/www/html/wp-content/plugins/uplink/tests/service-oauth-with-license-key-field-1.php"
data-slug="/var/www/html/wp-content/plugins/uplink/tests/service-oauth-with-license-key-field-1.php"
data-plugin="/var/www/html/wp-content/plugins/uplink/tests/service-oauth-with-license-key-field-1.php"
data-plugin-slug="service-oauth-with-license-key-field-1"
data-action="test"
>
<fieldset class="stellarwp-uplink__settings-group">
<input type='hidden' name='option_page' value='stellarwp_uplink_group_service-oauth-with-license-key-field-1' /><input type="hidden" name="action" value="update" /><input type="hidden" id="_wpnonce" name="_wpnonce" value="535281edcd" /><input type="hidden" name="_wp_http_referer" value="" />

<input
type="text"
name=""
value=""
placeholder="License key"
class="regular-text stellarwp-uplink__settings-field"
/>
<p class="tooltip description">
A valid license key is required for support and updates</p>
<div class="license-test-results">
<img src="http://wordpress.test/wp-admin/images/wpspin_light.gif" class="ajax-loading-license" alt="Loading" style="display: none"/>
<div class="key-validity"></div>
</div>
</fieldset>
<input type="hidden" class="wp-nonce-fluent" name="stellarwp-uplink-license-key-nonce__service-oauth-with-license-key-field-1" value="535281edcd" /> </div>
</div>

<div class="uplink-authorize-container">
<a href="https://licensing.stellarwp.com/account-auth?uplink_domain=&#038;uplink_slug=service-oauth-with-license-key-field-1&#038;_uplink_nonce=535281edcd?uplink_callback=aHR0cDovL3dvcmRwcmVzcy50ZXN0L3dwLWFkbWluL2luZGV4LnBocD91cGxpbmtfc2x1Zz1zZXJ2aWNlLW9hdXRoLXdpdGgtbGljZW5zZS1rZXktZmllbGQtMSZfdXBsaW5rX25vbmNlPTUzNTI4MWVkY2Q1MzUyODFlZA%3D%3D"
target="_blank"
class="button uplink-authorize not-authorized" >
Connect </a>
</div>

0 comments on commit 3577725

Please sign in to comment.