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

Closes #2883: Imagify Plugin - Replace the documentation link with the upgrade one for Free users #900

Merged
Show file tree
Hide file tree
Changes from 9 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
26 changes: 26 additions & 0 deletions Tests/Unit/inc/classes/ImagifyUser/getError.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,43 @@ public function testShouldReturnFalseWhenFetchedUserData() {
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );

$this->assertFalse( ( new User() )->get_error() );
}

/**
* Test \Imagify\User\User() should return cached user data if available.
*/
public function testShouldReturnFromCachedUserDataIfAvailable() {
$userData = (object) [
'id' => 1,
'email' => '[email protected]',
'plan_id' => '1',
'plan_label' => 'free',
'quota' => 456,
'extra_quota' => 0,
'extra_quota_consumed' => 0,
'consumed_current_month_quota' => 123,
'next_date_update' => '',
'is_active' => 1,
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( $userData );
Functions\expect( 'get_imagify_user' )->never();

$this->assertSame( '[email protected]', ( new User() )->email );
}

/**
* Test \Imagify\User\User->get_error() should return a WP_Error object when couldn’t fetch user account data.
*/
public function testShouldReturnErrorWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );

$this->assertSame( $wp_error, ( new User() )->get_error() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Test_GetPercentConsumedQuota extends TestCase {
public function testShouldReturnZeroWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );
Functions\expect( 'imagify_round_half_five' )->never();

Expand All @@ -47,6 +48,7 @@ public function testShouldReturnQuotaWhenFetchedUserData() {
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );
Functions\expect( 'imagify_round_half_five' )
->twice()
Expand Down
3 changes: 3 additions & 0 deletions Tests/Unit/inc/classes/ImagifyUser/isOverQuota.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Test_IsOverQuota extends TestCase {
public function testShouldReturnFalseWhenCouldNotFetchUserData() {
$wp_error = new WP_Error( 'error_id', 'Error Message' );

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $wp_error );

$this->assertFalse( ( new User() )->is_over_quota() );
Expand All @@ -45,6 +46,7 @@ public function testShouldReturnFalseWhenPaidAccount() {
'is_monthly' => true,
];

Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );

$this->assertFalse( ( new User() )->is_over_quota() );
Expand Down Expand Up @@ -97,6 +99,7 @@ public function testShouldReturnTrueWhenFreeOverQuota() {
}

private function createMocks( $userData, $dataPreviousQuotaPercent ) {
Functions\when( 'imagify_get_cached_user' )->justReturn( false );
Functions\when( 'get_imagify_user' )->justReturn( $userData );
Functions\expect( 'imagify_round_half_five' )
->once()
Expand Down
70 changes: 70 additions & 0 deletions Tests/Unit/inc/classes/classImagifyViews/pluginActionLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace Imagify\Tests\Unit\inc\classes\classImagifyViews;

use Brain\Monkey\Functions;
use Imagify\Tests\Unit\TestCase;
use Imagify_Views;

/**
* Tests for \Imagify_Views->plugin_action_links().
*
* @covers \Imagify_Views::plugin_action_links
* @group ImagifyAPI
*/
class Test_PluginActionLinks extends TestCase {
protected $imagify_views;
protected $user_data = [
'id' => 1,
'email' => '[email protected]',
'plan_id' => '2',
'plan_label' => 'free',
'quota' => 456,
'extra_quota' => 0,
'extra_quota_consumed' => 0,
'consumed_current_month_quota' => 123,
'next_date_update' => '',
'is_active' => 1,
'is_monthly' => true,
];

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

// Create Imagify_Views instance without calling the constructor
$reflection = new \ReflectionClass(Imagify_Views::class);
$this->imagify_views = $reflection->newInstanceWithoutConstructor();
}

/**
* Test \Imagify_Views->plugin_action_links() should return Documentation link if plan label is not starter.
*/
public function testShouldReturnDocumentationLinkAmongPluginLinksIfPlanLabelIsNotStarter() {
Functions\when( 'imagify_get_cached_user' )->justReturn( (object) $this->user_data );
Functions\when( 'get_imagify_user' )->justReturn( (object) $this->user_data );
Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' );
Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' );

$plugin_action_links = $this->imagify_views->plugin_action_links([]);
$plugin_action_links = implode( '|', $plugin_action_links );

$this->assertStringContainsString( 'Documentation', $plugin_action_links );
}

/**
* Test \Imagify_Views->plugin_action_links() should return Upgrade link if plan label is starter.
*/
public function testShouldReturnUpgradeLinkAmongPluginLinksIfPlanLabelIsStarter() {
$this->user_data['plan_id'] = '1';

Functions\when( 'imagify_get_cached_user' )->justReturn( (object) $this->user_data );
Functions\when( 'get_imagify_user' )->justReturn( (object) $this->user_data );
Functions\when( 'imagify_get_external_url' )->justReturn( 'https://example.org' );
Functions\when( 'get_imagify_admin_url' )->justReturn( 'https://example.org' );

$plugin_action_links = $this->imagify_views->plugin_action_links([]);
$plugin_action_links = implode( '|', $plugin_action_links );

$this->assertStringContainsString( 'Upgrade', $plugin_action_links );
$this->assertStringContainsString( 'class="imagify-plugin-upgrade"', $plugin_action_links );
}
}
4 changes: 4 additions & 0 deletions assets/css/admin-bar.css
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,7 @@
content: "\2715";
color: #fff;
}
.imagify-plugin-upgrade {
color: #8BC34A;
font-weight: 600;
}
2 changes: 1 addition & 1 deletion assets/css/admin-bar.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion classes/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class User {
* @return void
*/
public function __construct() {
$user = get_imagify_user();
$user = imagify_get_cached_user() ?: get_imagify_user();

if ( is_wp_error( $user ) ) {
$this->error = $user;
Expand Down
12 changes: 11 additions & 1 deletion inc/classes/class-imagify-views.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,17 @@ public function add_network_menus() {
* @return array
*/
public function plugin_action_links( $actions ) {
jeawhanlee marked this conversation as resolved.
Show resolved Hide resolved
array_unshift( $actions, sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( imagify_get_external_url( 'documentation' ) ), __( 'Documentation', 'imagify' ) ) );
$user = new User();
$text = 1 !== $user->plan_id ? __( 'Documentation', 'imagify' ) : __( 'Upgrade', 'imagify' );
$url = 1 !== $user->plan_id ? 'documentation' : 'subscription';
$class = 1 !== $user->plan_id ? '' : ' class="imagify-plugin-upgrade"';

array_unshift( $actions, sprintf( '<a href="%s" target="_blank"%s>%s</a>',
esc_url( imagify_get_external_url( $url ) ),
$class,
$text
) );

array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url( 'bulk-optimization' ) ), __( 'Bulk Optimization', 'imagify' ) ) );
array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url() ), __( 'Settings', 'imagify' ) ) );
return $actions;
Expand Down
Loading
Loading