Skip to content
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
49 changes: 19 additions & 30 deletions src/Telemetry/Opt_In/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Status {
public const OPTION_NAME_USER_INFO = 'stellarwp_telemetry_user_info';
public const STATUS_ACTIVE = 1;
public const STATUS_INACTIVE = 2;
public const STATUS_MIXED = 3;

/**
* Gets the option name used to store the opt-in status.
Expand Down Expand Up @@ -63,40 +62,32 @@ public function get_option() {
* The status is stored as an integer because there are multiple possible statuses:
* 1 = Active
* 2 = Inactive
* 3 = Mixed
*
* @since 1.0.0
* @since 2.0.1 Correct logic so it is not subject to the order of the plugins.
* @since TBD Update to remove unnecessary "mixed" status.
*
* @return integer The status value.
*/
public function get() {
$option = $this->get_option();

// If the status option is not an option, default to inactive.
if ( ! isset( $option['plugins'] ) ) {
return self::STATUS_INACTIVE;
}
$status = self::STATUS_INACTIVE;
$option = $this->get_option();
$plugins = isset( $option['plugins'] ) ? $option['plugins'] : [];

$status = array_reduce(
$option['plugins'],
function( $carry, $item ) {
// First run, ignore the default STATUS_ACTIVE.
if ( empty( $carry ) ) {
return (int) $item['optin'];
}
if ( count( $plugins ) === 0 ) {
$status = self::STATUS_INACTIVE;
}

// As long as they are the same, we keep returning the same.
if ( $carry === $item['optin'] ) {
return (int) $item['optin'];
}
foreach ( $plugins as $plugin ) {

return self::STATUS_MIXED;
// If any plugins are missing an optin status or at least one is false, set status to false.
if ( ! isset( $plugin['optin'] ) || false === $plugin['optin'] ) {
$status = self::STATUS_INACTIVE;
break;
}
);

if ( 0 === $status ) {
$status = self::STATUS_INACTIVE;
$status = self::STATUS_ACTIVE;
}

/**
Expand Down Expand Up @@ -234,7 +225,7 @@ public function get_opted_in_plugins() {
* @since 1.0.0
* @since 2.0.0 - Updated to allow defined stellar_slug.
*
* @param boolean $status The status to set (Active = 1, Inactive = 2, Mixed = 3).
* @param boolean $status The status to set.
* @param string $stellar_slug The stellar_slug to set the status of.
*
* @return boolean
Expand All @@ -260,17 +251,15 @@ public function set_status( bool $status, string $stellar_slug = '' ) {
* @return string
*/
public function get_status() {

$optin_label = '';

switch ( $this->get() ) {
case self::STATUS_ACTIVE:
$optin_label = __( 'Active', 'stellarwp-telemetry' );
break;
case self::STATUS_INACTIVE:
$optin_label = __( 'Inactive', 'stellarwp-telemetry' );
case 1:
$optin_label = esc_html__( 'Active', 'stellarwp-telemetry' );
break;
case self::STATUS_MIXED:
$optin_label = __( 'Mixed', 'stellarwp-telemetry' );
case 2:
$optin_label = esc_html__( 'Inactive', 'stellarwp-telemetry' );
break;
}

Expand Down
185 changes: 152 additions & 33 deletions tests/wpunit/StatusTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php

use Codeception\TestCase\WPTestCase;
use StellarWP\Telemetry\Config;
use StellarWP\Telemetry\Core;
use StellarWP\Telemetry\Opt_In\Status;
use StellarWP\Telemetry\Tests\Container;
use StellarWP\Telemetry\Tests\Support\Traits\With_Test_Container;
use StellarWP\Telemetry\Tests\Support\Traits\With_Uopz;

Expand Down Expand Up @@ -77,9 +74,9 @@ public function get_opted_in_plugins_data_provider(): array {
[
[
'slug' => 'acme-commerce',
'version' => '1.2.3'
]
]
'version' => '1.2.3',
],
],
],
'one plugin is missing opt-in key' => [
[
Expand All @@ -96,9 +93,9 @@ public function get_opted_in_plugins_data_provider(): array {
[
[
'slug' => 'acme-commerce',
'version' => '1.2.3'
]
]
'version' => '1.2.3',
],
],
],
'not all plugins opt-in' => [
[
Expand All @@ -120,12 +117,12 @@ public function get_opted_in_plugins_data_provider(): array {
[
[
'slug' => 'acme-commerce',
'version' => '1.2.3'
'version' => '1.2.3',
],
[
'slug' => 'acme-learn',
'version' => '5.6.7'
]
'version' => '5.6.7',
],
],
],
'all plugins opt-out' => [
Expand Down Expand Up @@ -167,16 +164,16 @@ public function get_opted_in_plugins_data_provider(): array {
[
[
'slug' => 'acme-commerce',
'version' => '1.2.3'
'version' => '1.2.3',
],
[
'slug' => 'acme-tickets',
'version' => '3.4.5'
'version' => '3.4.5',
],
[
'slug' => 'acme-learn',
'version' => '5.6.7'
]
'version' => '5.6.7',
],
],
],
];
Expand All @@ -186,31 +183,153 @@ public function get_opted_in_plugins_data_provider(): array {
* @dataProvider get_opted_in_plugins_data_provider
*/
public function test_get_opted_in_plugins( $option_value, $expected ): void {
$this->set_fn_return( 'get_plugin_data', static function ( string $plugin ) {
if ( strpos( $plugin, 'acme-commerce', true ) ) {
return [
'Name' => 'Acme Commerce',
'Version' => '1.2.3',
];
}
$this->set_fn_return(
'get_plugin_data',
static function ( string $plugin ) {
if ( strpos( $plugin, 'acme-commerce', true ) ) {
return [
'Name' => 'Acme Commerce',
'Version' => '1.2.3',
];
}

if ( strpos( $plugin, 'acme-tickets', true ) ) {
return [
'Name' => 'Acme Tickets',
'Version' => '3.4.5',
];
}

if ( strpos( $plugin, 'acme-tickets', true ) ) {
return [
'Name' => 'Acme Tickets',
'Version' => '3.4.5',
'Name' => 'Acme Learn',
'Version' => '5.6.7',
];
}

return [
'Name' => 'Acme Learn',
'Version' => '5.6.7',
];
}, true );
},
true
);
$status = new Status();

update_option( $status->get_option_name(), $option_value );

$this->assertIsArray( $status->get_opted_in_plugins() );
$this->assertEquals( $expected, $status->get_opted_in_plugins() );
}

public function get_status_data_provider(): array {
return [
'empty' => [ [], 2 ],
'missing plugins key' => [ [ 'token' => 'foo' ], 2 ],
'empty plugins' => [ [ 'plugins' => [] ], 2 ],
'one plugin is missing opt-in key' => [
[
'plugins' => [
'acme-commerce' => [
'wp_slug' => 'acme-commerce/acme-commerce.php',
'optin' => true,
],
'acme-tickets' => [
'wp_slug' => 'acme-tickets/acme-tickets.php',
],
],
],
2,
],
'not all plugins opt-in' => [
[
'plugins' => [
'acme-commerce' => [
'wp_slug' => 'acme-commerce/acme-commerce.php',
'optin' => true,
],
'acme-tickets' => [
'wp_slug' => 'acme-tickets/acme-tickets.php',
'optin' => false,
],
'acme-learn' => [
'wp_slug' => 'acme-learn/acme-learn.php',
'optin' => true,
],
],
],
2,
],
'all plugins opt-out' => [
[
'plugins' => [
'acme-commerce' => [
'wp_slug' => 'acme-commerce/acme-commerce.php',
'optin' => false,
],
'acme-tickets' => [
'wp_slug' => 'acme-tickets/acme-tickets.php',
'optin' => false,
],
'acme-learn' => [
'wp_slug' => 'acme-learn/acme-learn.php',
'optin' => false,
],
],
],
2,
],
'all plugins opt-in' => [
[
'plugins' => [
'acme-commerce' => [
'wp_slug' => 'acme-commerce/acme-commerce.php',
'optin' => true,
],
'acme-tickets' => [
'wp_slug' => 'acme-tickets/acme-tickets.php',
'optin' => true,
],
'acme-learn' => [
'wp_slug' => 'acme-learn/acme-learn.php',
'optin' => true,
],
],
],
1,
],
];
}

/**
* @dataProvider get_status_data_provider
*/
public function test_get_status( $option_value, $expected ): void {
$status = new Status();

update_option( $status->get_option_name(), $option_value );

$this->assertIsInt( $status->get() );
$this->assertEquals( $expected, $status->get() );
}

/**
* @dataProvider get_status_data_provider
*/
public function test_get_status_label( $option_value, $expected ): void {
$status = new Status();

update_option( $status->get_option_name(), $option_value );

$label = 1 === $expected ? 'Active' : 'Inactive';

$this->assertEquals( $label, $status->get_status() );
}

/**
* @dataProvider get_status_data_provider
*/
public function test_is_active( $option_value, $expected ): void {
$status = new Status();

update_option( $status->get_option_name(), $option_value );

$is_active = 1 === $expected ? true : false;

$this->assertIsBool( $status->is_active() );
$this->assertEquals( $is_active, $status->is_active() );
}
}