Skip to content

Commit

Permalink
Merge pull request #39 from newfold-labs/update/use-cli-option-update
Browse files Browse the repository at this point in the history
Update/use cli option update
  • Loading branch information
circlecube authored Nov 8, 2024
2 parents edd90b3 + 396133a commit 386ec40
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 56 deletions.
35 changes: 9 additions & 26 deletions includes/Solutions.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,7 @@ public function init_entitilements_apis(): void {
*/
public static function add_my_plugins_and_tools_tab( array $tabs ) {
$capability = new SiteCapabilities();

$has_solutions = $capability->get( 'hasSolution' );

if ( ! $has_solutions ) {
return $tabs;
}
$hiive = new HiiveConnection();
$api = new EntitlementsApi( $hiive );
$entitlements = $api->get_items();
if ( is_array( $entitlements->data ) ? $entitlements->data['entitlements'] : $entitlements->data->entitlements ) {
if ( $capability->get( 'hasSolution' ) ) {
$tabs['nfd_my_plugins_and_tools'] = __( 'My Plugins & Tools', 'wp-module-solutions' );
}
return $tabs;
Expand All @@ -117,22 +108,14 @@ public static function add_my_plugins_and_tools_tab( array $tabs ) {
*/
public static function add_plugins_and_tools_menu_link() {
$capability = new SiteCapabilities();

$has_solutions = $capability->get( 'hasSolution' );

if ( $has_solutions ) {
$hiive = new HiiveConnection();
$api = new EntitlementsApi( $hiive );
$entitlements = $api->get_items();
if ( is_array( $entitlements->data ) ? $entitlements->data['entitlements'] : $entitlements->data->entitlements ) {
add_submenu_page(
'plugins.php',
'nfd_my_plugins_and_tools',
'My Plugins & Tools',
'manage_options',
'plugin-install.php?tab=nfd_my_plugins_and_tools'
);
}
if ( $capability->get( 'hasSolution' ) ) {
add_submenu_page(
'plugins.php',
'nfd_my_plugins_and_tools',
'My Plugins & Tools',
'manage_options',
'plugin-install.php?tab=nfd_my_plugins_and_tools'
);
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/cypress/integration/solutions-app.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {

// check that my plugins and tools displays when capabilities.hasSolution is true
it( 'My Plugins & Tools displays with Solution', () => {
// set up the intercept for entitlements
cy.intercept(
{
method: 'GET',
Expand Down Expand Up @@ -58,11 +59,12 @@ describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {
);
} );

cy.wait( '@getEntitlements' );

cy.get( 'a.wppbh-app-navitem[href="#/my_plugins_and_tools"]' ).should(
'be.visible'
);

cy.wait( '@getEntitlements' );

cy.get( 'a.wppbh-app-navitem[href="#/my_plugins_and_tools"]' ).should(
'be.visible'
Expand Down
47 changes: 20 additions & 27 deletions tests/cypress/integration/solutions-plugins.cy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// <reference types="Cypress" />
import { wpLogin, wpCli } from '../wp-module-support/utils.cy';
import { wpLogin, wpCli, setCapability } from '../wp-module-support/utils.cy';

// path to the entitlements json to load into transient
const entitlementsjson =
'vendor/newfold-labs/wp-module-solutions/tests/cypress/fixtures/entitlements.json';
const entitlementsFixture = require( '../fixtures/entitlements.json' );

describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {
beforeEach( () => {
Expand All @@ -13,14 +11,11 @@ describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {

after( () => {
wpCli( `transient delete nfd_site_capabilities` );
wpCli( `transient delete newfold_solutions` );
wpCli( `option delete nfd_data_token` );
} );

// check that it does not display when capabilities.hasSolution is false
it( 'My Plugins & Tools tab does not display without solution', () => {
wpCli( `transient delete nfd_site_capabilities` );
wpCli( `transient delete newfold_solutions` );

// need a cli command to set a capability before a test
cy.visit( '/wp-admin/plugin-install.php' );
Expand All @@ -34,24 +29,19 @@ describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {
} );

it( 'My Plugins & Tools exists', () => {
// set transient expiration to one hour from now
const expiry = Math.floor( new Date().getTime() / 1000.0 ) + 3600;

// spoof hiive connection
wpCli( `option set nfd_data_token 'xyc123'` );
// Set hasSolution:true in capabilities
wpCli(
`option set _transient_nfd_site_capabilities '{"hasSolution": true}' --format=json`
);
// add test entitlements into transient
wpCli(
`option set _transient_newfold_solutions --format=json < ${ entitlementsjson }`
);
// manually set expiration for the transients
wpCli(
`option set _transient_timeout_nfd_site_capabilities ${ expiry }`
);
wpCli( `option set _transient_timeout_newfold_solutions ${ expiry }` );
setCapability( { hasSolution: true } );

cy.intercept(
{
method: 'GET',
url: /newfold-solutions(\/|%2F)v1(\/|%2F)entitlements/,
},
{
body: entitlementsFixture,
delay: 100,
}
).as( 'getEntitlements' );

// load plugin install page
cy.visit( '/wp-admin/plugin-install.php' );
Expand All @@ -65,11 +55,14 @@ describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {
// check that my plugins and tools tab displays when capabilities.hasSolution is true
cy.get(
'#adminmenu a[href="plugin-install.php?tab=nfd_my_plugins_and_tools"]'
).should( 'be.visible' );
)
.should( 'be.visible' )
.click();

cy.wait( '@getEntitlements' );

// check that entitlement plugins load
cy.visit( '/wp-admin/plugin-install.php?tab=nfd_my_plugins_and_tools' );
cy.reload( true );
cy.url().should( 'contain', 'nfd_my_plugins_and_tools' );

cy.get( '.plugin-install-nfd_my_plugins_and_tools' ).should(
'be.visible'
Expand Down
34 changes: 32 additions & 2 deletions tests/cypress/wp-module-support/utils.cy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// login
/**
* Loginto WordPress.
*/
export const wpLogin = () => {
cy.login( Cypress.env( 'wpUsername' ), Cypress.env( 'wpPassword' ) );
};

// wp cli wrapper
/**
* wp-cli helper
*
* This wraps the command in the required npx wp-env run cli wp
*
* @param {string} cmd the command to send to wp-cli
*/
export const wpCli = ( cmd ) => {
cy.exec( `npx wp-env run cli wp ${ cmd }`, {
env: {
Expand All @@ -15,3 +23,25 @@ export const wpCli = ( cmd ) => {
}
} );
};

/**
* Set capability helper
*
* This calls performs a cli command to set a specific capability
*
* @param {*} capJSON json of capabilities
* @param {number} expiration seconds for transient to expire, defualt 3600 (1 hour)
*/
export const setCapability = ( capJSON, expiration = 3600 ) => {
wpCli(
`option update _transient_nfd_site_capabilities '${ JSON.stringify(
capJSON
) }' --format=json`
);
// set transient expiration to one hour (default) from now
const expiry = Math.floor( new Date().getTime() / 1000.0 ) + expiration;
// manually set expiration for the transients
wpCli(
`option update _transient_timeout_nfd_site_capabilities ${ expiry }`
);
};

0 comments on commit 386ec40

Please sign in to comment.