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/support uplink licensing #3123

Merged
merged 26 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cdf3fa6
Fix issue with workflows definition (typo)
borkweb Jun 3, 2024
6d609b3
Add Uplink library controller, setup logic to add SLR field.
redscar Jul 9, 2024
e5c2bbc
Code cleanup.
redscar Jul 9, 2024
9885f32
Code cleanup.
redscar Jul 9, 2024
0afd707
Update registration of SLR License
dpanta94 Jul 15, 2024
e71730f
Tests.
redscar Jul 18, 2024
b416590
Fixing tests
dpanta94 Jul 18, 2024
8736e66
Added new tests for Uplink logic.
redscar Jul 18, 2024
3177229
Added new test when user doesn't have correct permissions.
redscar Jul 18, 2024
013753a
:fast_forward: https://github.com/the-events-calendar/tribe-common/co…
tec-bot Jul 22, 2024
347c323
:fast_forward: https://github.com/the-events-calendar/tribe-common/co…
tec-bot Jul 22, 2024
95378ca
Fixing snapshot
dpanta94 Jul 23, 2024
067716d
:fast_forward: https://github.com/the-events-calendar/tribe-common/co…
tec-bot Jul 23, 2024
cf6b403
:fast_forward: https://github.com/the-events-calendar/tribe-common/co…
tec-bot Jul 23, 2024
06aec90
:fast_forward: https://github.com/the-events-calendar/tribe-common/co…
tec-bot Jul 23, 2024
b98d419
Moved SLR license controller inside SLR
dpanta94 Jul 23, 2024
9b91c5b
:fast_forward: https://github.com/the-events-calendar/tribe-common/co…
tec-bot Jul 24, 2024
6412806
:fast_forward: https://github.com/the-events-calendar/tribe-common/co…
tec-bot Jul 29, 2024
eb788b0
:fast_forward: https://github.com/the-events-calendar/tribe-common/co…
tec-bot Jul 29, 2024
895cdf2
Update src/Tickets/Seating/Controller.php
dpanta94 Jul 29, 2024
84717ab
Update src/Tickets/Libraries/Uplink/SLRController.php
dpanta94 Jul 29, 2024
68282bb
Fix phpcs and tests
dpanta94 Jul 29, 2024
43995f2
amend cr comments - move uplink controllers location
dpanta94 Jul 29, 2024
91181c9
Move tests
dpanta94 Jul 29, 2024
c6a948e
phpcs - remove a space
dpanta94 Jul 29, 2024
46b5415
Merge remote-tracking branch 'origin/feat/slr-support' into feat/supp…
dpanta94 Jul 30, 2024
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
2 changes: 1 addition & 1 deletion .puprc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"npm ci",
"npm run build"
],
"workflow": {
"workflows": {
"build-common": [
"echo 'building common (no-dev) in: '$(pwd)",
"cd common && pup build"
Expand Down
2 changes: 1 addition & 1 deletion common
Submodule common updated 0 files
2 changes: 1 addition & 1 deletion src/Tickets/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function register() {
$this->container->register( Blocks\Controller::class );

// Seating.
$this->container->register( Seating\Controller::class );
$this->container->register( Seating\Controller::class );

$this->has_registered = true;

Expand Down
2 changes: 2 additions & 0 deletions src/Tickets/Seating/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ function () {
$this->container->register( Tables::class );
$this->container->register( Assets::class );

$this->container->register( Libraries\Uplink::class );

// Manage Order and Attendee data.
$this->container->register( Orders\Controller::class );

Expand Down
101 changes: 101 additions & 0 deletions src/Tickets/Seating/Libraries/Uplink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* The Controller to set up the Uplink library.
*
* @since TBD
* @package TEC\Tickets\Libraries\Uplink
*/

namespace TEC\Tickets\Seating\Libraries;

use TEC\Common\Contracts\Provider\Controller as Controller_Contract;
use TEC\Common\StellarWP\Uplink\Register;
use Tribe__Tickets__Main as Main;

/**
* Controller for setting up the stellarwp/uplink library.
*
* @since TBD
*
* @package TEC\Tickets\Libraries\Uplink
*/
class Uplink extends Controller_Contract {
/**
* Plugin slug.
*
* @var string
*/
protected string $et_slr_plugin_slug = 'tec-seating';

/**
* Plugin name.
*
* @var string
*/
protected string $et_slr_plugin_name = 'Seat Layouts & Reservations';

/**
* Main plugin object.
*
* @var object
*/
protected object $et_main;

/**
* Register the controller.
*
* @since TBD
*/
public function do_register(): void {
$this->et_main = tribe( 'tickets.main' );
$this->add_actions();
}

/**
* Unregister the controller.
*
* @since TBD
*
* @return void
*/
public function unregister(): void {
$this->remove_actions();
}

/**
* Add the action hooks.
*
* @since TBD
*/
public function add_actions(): void {
add_action( 'init', [ $this, 'register_plugin' ] );
}

/**
* Remove the action hooks.
*
* @since TBD
*/
public function remove_actions(): void {
remove_action( 'init', [ $this, 'register_plugin' ] );
}

/**
* Register the plugin in the uplink library.
*
* @since TBD
*
* @return void
*/
public function register_plugin(): void {
Register::plugin(
$this->et_slr_plugin_slug,
$this->et_slr_plugin_name,
Main::VERSION,
"{$this->et_main->plugin_path}",
$this->et_main,
null,
true
);
}
}
239 changes: 239 additions & 0 deletions tests/wpunit/Tickets/Seating/Libraries/UplinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
<?php

namespace TEC\Tickets\Seating\Libraries;

use Spatie\Snapshots\MatchesSnapshots;
use TEC\Common\StellarWP\Uplink\Resources\Collection;
use TEC\Common\StellarWP\Uplink\Auth\Token\Contracts\Token_Manager;
use Tribe\Tests\Traits\With_Uopz;

class UplinkTest extends \Codeception\TestCase\WPTestCase {

use MatchesSnapshots;
use With_Uopz;

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

/**
* Collection instance.
*
* @var Collection
*/
protected $collection;

/**
* Plugin slug.
*
* @var string
*/
protected string $et_slr_plugin_slug = 'tec-seating';

/**
* Resource instance.
*
* @var object
*/
protected $resource;

public function setUp(): void {
parent::setUp();
set_current_user( 1 );
$this->collection = tribe( Collection::class );
$this->resource = $this->collection->get( $this->et_slr_plugin_slug );
$this->token_manager = tribe( Token_Manager::class );
}

/**
* @test
*/
public function it_should_have_the_fields_for_seating_licenses_with_incorrect_permissions_snapshot(): void {
set_current_user( 0 );
$license_fields = apply_filters( 'tribe_license_fields', [], 0, 999 );
$this->assertIsArray( $license_fields );

$relevant_fields = $this->get_relevant_license_fields( $license_fields );

// Assert that the relevant keys exist.
$this->assertArrayHasKey( 'stellarwp-uplink_tec-seating-heading', $license_fields );
$this->assertArrayHasKey( 'stellarwp-uplink_tec-seating', $license_fields );

$this->assertStringContainsString( 'Contact your network administrator to connect', $relevant_fields['stellarwp-uplink_tec-seating']['html'] );

// Snapshot test only the relevant fields.
$this->assertMatchesSnapshot( $relevant_fields );
}

/**
* @test
*/
public function it_should_have_the_fields_for_seating_licenses_with_no_license_snapshot(): void {
$license_fields = apply_filters( 'tribe_license_fields', [], 0, 999 );
$this->assertIsArray( $license_fields );

$relevant_fields = $this->get_relevant_license_fields( $license_fields );

// Assert that the relevant keys exist.
$this->assertArrayHasKey( 'stellarwp-uplink_tec-seating-heading', $license_fields );
$this->assertArrayHasKey( 'stellarwp-uplink_tec-seating', $license_fields );

// Snapshot test only the relevant fields.
$this->assertMatchesSnapshot( $relevant_fields );
}

/**
* @test
*/
public function it_should_have_the_fields_for_seating_licenses_with_valid_license_snapshot(): void {
$this->set_valid_license();
$license_fields = apply_filters( 'tribe_license_fields', [], 0, 999 );
$this->assertIsArray( $license_fields );

$relevant_fields = $this->get_relevant_license_fields( $license_fields );

// Assert that the relevant keys exist.
$this->assertArrayHasKey( 'stellarwp-uplink_tec-seating-heading', $license_fields );
$this->assertArrayHasKey( 'stellarwp-uplink_tec-seating', $license_fields );

// Snapshot test only the relevant fields.
$this->assertMatchesSnapshot( $relevant_fields );
}

/**
* @test
*/
public function it_should_have_the_fields_for_seating_licenses_with_valid_license(): void {
$this->set_valid_license();
$this->verify_license_fields_contain_class( 'authorized' );
}

/**
* @test
*/
public function it_should_toggle_license_and_check_authorization_status(): void {
$this->set_valid_license();
$this->verify_license_fields_contain_class( 'authorized' );

// Disconnect the license
$this->resource->set_license_key( '' );
$this->assertEquals( '', $this->resource->get_license_key() );
$this->assertEquals( '', get_option( $this->resource->get_license_object()->get_key_option_name() ) );
$this->assertTrue( $this->token_manager->delete( $this->et_slr_plugin_slug ) );

// Confirm lack of authorization
$this->verify_license_fields_contain_class( 'not-authorized' );

// Reconnect the license
$this->set_valid_license();
$this->verify_license_fields_contain_class( 'authorized' );
}

/**
* @test
*/
public function it_should_handle_invalid_license(): void {
$this->set_invalid_license();
$this->verify_license_fields_contain_class( 'not-authorized' );
}

/**
* @test
*/
public function it_should_handle_expired_license(): void {
$this->set_expired_license();
$this->verify_license_fields_contain_class( 'not-authorized' );
}

/**
* @test
*/
public function it_should_handle_no_license_key(): void {
$this->resource->set_license_key( '' );
$this->assertEquals( '', $this->resource->get_license_key() );
$this->assertEquals( '', get_option( $this->resource->get_license_object()->get_key_option_name() ) );
$this->verify_license_fields_contain_class( 'not-authorized' );
}

/**
* Sets a valid license key for testing.
*/
private function set_valid_license(): void {
$this->set_license_key( '22222222222222222', true );
}

/**
* Sets an invalid license key for testing.
*/
private function set_invalid_license(): void {
$this->set_license_key( 'invalid_key', false );
}

/**
* Sets an expired license key for testing.
*/
private function set_expired_license(): void {
$this->set_license_key( 'expired_key', false );
}

/**
* Sets a license key and optionally mark it as valid for testing.
*
* @param string $key The license key to set.
* @param bool $valid Whether the license key should be considered valid.
*/
private function set_license_key( string $key, bool $valid ): void {
$this->resource->set_license_key( $key );
$this->assertEquals( $key, $this->resource->get_license_key() );
$this->assertEquals( $key, get_option( $this->resource->get_license_object()->get_key_option_name() ) );

if ( $valid ) {
$this->assertTrue( $this->resource->is_using_oauth() );
$this->token_manager->store( $key, $this->resource );
}

$this->set_fn_return( 'wp_create_nonce', '12345678' );
}

/**
* Helper method to verify that license fields contain a specific class.
*
* @param string $class The class to check for.
*/
private function verify_license_fields_contain_class( string $class ): void {
$license_fields = apply_filters( 'tribe_license_fields', [], 0, 999 );
$this->assertIsArray( $license_fields );

$relevant_fields = $this->get_relevant_license_fields( $license_fields );

$this->assertStringContainsString( $class, $relevant_fields['stellarwp-uplink_tec-seating']['html'] );
}

/**
* Extracts and modifies the relevant license fields for snapshot testing.
*
* @param array $license_fields The license fields.
*
* @return array The modified relevant license fields.
*/
private function get_relevant_license_fields( array $license_fields ): array {
$relevant_keys = [
'stellarwp-uplink_tec-seating-heading',
'stellarwp-uplink_tec-seating',
];

$relevant_fields = array_intersect_key( $license_fields, array_flip( $relevant_keys ) );

// Modify the `html` field to replace the dynamic part with a static placeholder.
if ( isset( $relevant_fields['stellarwp-uplink_tec-seating']['html'] ) ) {
$relevant_fields['stellarwp-uplink_tec-seating']['html'] = preg_replace(
'/uplink_callback=[^"]+/',
'uplink_callback={STATIC_CALLBACK}',
$relevant_fields['stellarwp-uplink_tec-seating']['html']
);
}

return $relevant_fields;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php return array (
'stellarwp-uplink_tec-seating-heading' =>
array (
'type' => 'heading',
'label' => 'Seat Layouts & Reservations',
),
'stellarwp-uplink_tec-seating' =>
array (
'type' => 'html',
'label' => '',
'html' => '
<div class="uplink-authorize-container">
<a href="http://wordpress.test/wp-admin/network/"
target="_self"
class="button uplink-authorize not-authorized" >
Contact your network administrator to connect </a>
</div>

',
),
);
Loading
Loading