Skip to content

Commit

Permalink
Merge pull request #15006 from wordpress-mobile/feature/14856-enable-…
Browse files Browse the repository at this point in the history
…plugins-for-self-hosted-sites-updated

Enable plugins for self-hosted sites
  • Loading branch information
ravishanker authored Jul 9, 2021
2 parents 63c0faa + 2a050cf commit feeacbc
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class PluginUtils {
public static boolean isPluginFeatureAvailable(SiteModel site) {
if (site.isUsingWpComRestApi() && site.isJetpackConnected()) {
return SiteUtils.checkMinimalJetpackVersion(site, "5.6");
} else if (site.isSelfHostedAdmin()) {
return SiteUtils.checkMinimalWordPressVersion(site, "5.5");
}

// If the site has business plan we can do an Automated Transfer
Expand Down
26 changes: 8 additions & 18 deletions WordPress/src/main/java/org/wordpress/android/util/SiteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.wordpress.android.ui.reader.utils.SiteVisibility;
import org.wordpress.android.util.analytics.AnalyticsUtils;
import org.wordpress.android.util.analytics.AnalyticsUtils.BlockEditorEnabledSource;
import org.wordpress.android.util.helpers.Version;
import org.wordpress.android.util.image.BlavatarShape;
import org.wordpress.android.util.image.ImageType;

Expand Down Expand Up @@ -322,29 +321,20 @@ public static ArrayList<Integer> getCurrentSiteIds(SiteStore siteStore, boolean
public static boolean checkMinimalJetpackVersion(SiteModel site, String limitVersion) {
String jetpackVersion = site.getJetpackVersion();
if (site.isUsingWpComRestApi() && site.isJetpackConnected() && !TextUtils.isEmpty(jetpackVersion)) {
try {
// strip any trailing "-beta" or "-alpha" from the version
int index = jetpackVersion.lastIndexOf("-");
if (index > 0) {
jetpackVersion = jetpackVersion.substring(0, index);
}
// Jetpack version field is sometimes "false" instead of a number on self-hosted sites that are no
// longer active.
if (jetpackVersion.equals("false")) {
return false;
}
Version siteJetpackVersion = new Version(jetpackVersion);
Version minVersion = new Version(limitVersion);
return siteJetpackVersion.compareTo(minVersion) >= 0;
} catch (IllegalArgumentException e) {
String errorStr = "Invalid site jetpack version " + jetpackVersion + ", expected " + limitVersion;
AppLog.e(AppLog.T.UTILS, errorStr, e);
// Jetpack version field is sometimes "false" instead of a number on self-hosted sites that are no
// longer active.
if (jetpackVersion.equals("false")) {
return false;
}
return VersionUtils.checkMinimalVersion(jetpackVersion, limitVersion);
}
return false;
}

public static boolean checkMinimalWordPressVersion(SiteModel site, String minVersion) {
return VersionUtils.checkMinimalVersion(site.getSoftwareVersion(), minVersion);
}

public static boolean supportsStoriesFeature(SiteModel site) {
return site != null && (site.isWPCom() || checkMinimalJetpackVersion(site, WP_STORIES_JETPACK_VERSION));
}
Expand Down
29 changes: 29 additions & 0 deletions WordPress/src/main/java/org/wordpress/android/util/VersionUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.wordpress.android.util

import org.wordpress.android.util.AppLog.T.UTILS
import org.wordpress.android.util.helpers.Version

object VersionUtils {
/**
* Checks if a given version [String] is equal to or higher than another given minimal version [String].
*
* Note: This method ignores "-beta", "-alpha" or "-RC" versions, meaning that this will return `true` for
* a version "5.5-beta1" and `minVersion` "5.5", for example.
*
* @param version The version [String] to check.
* @param minVersion A minimal acceptable version [String].
* @return `true` if the version is equal to or higher than the `minVersion`; `false` otherwise.
*/
@JvmStatic fun checkMinimalVersion(version: String?, minVersion: String?) =
if (!version.isNullOrEmpty() && !minVersion.isNullOrEmpty()) {
try {
Version(stripVersionSuffixes(version)) >= Version(stripVersionSuffixes(minVersion))
} catch (e: IllegalArgumentException) {
AppLog.e(UTILS, "Invalid version $version, expected $minVersion", e)
false
}
} else false

// Strip any trailing "-beta", "-alpha" or "-RC" suffixes from the version
private fun stripVersionSuffixes(version: String) = version.substringBefore("-")
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,42 @@ class SiteUtilsTest {
assertThat(hasMinimalJetpackVersion).isFalse()
}

@Test
fun `checkMinimalWordPressVersion returns true when software version is higher than the minimal version`() {
val minVersion = "5.5"

val site = SiteModel()
site.softwareVersion = "5.6"

val hasMinimalWordPressVersion = SiteUtils.checkMinimalWordPressVersion(site, minVersion)

assertThat(hasMinimalWordPressVersion).isTrue
}

@Test
fun `checkMinimalWordPressVersion returns true when software version is equal to the minimal version`() {
val minVersion = "5.5"

val site = SiteModel()
site.softwareVersion = "5.5"

val hasMinimalWordPressVersion = SiteUtils.checkMinimalWordPressVersion(site, minVersion)

assertThat(hasMinimalWordPressVersion).isTrue
}

@Test
fun `checkMinimalWordPressVersion returns false when software version is lower than the minimal version`() {
val minVersion = "5.5"

val site = SiteModel()
site.softwareVersion = "5.4"

val hasMinimalWordPressVersion = SiteUtils.checkMinimalWordPressVersion(site, minVersion)

assertThat(hasMinimalWordPressVersion).isFalse
}

@Test
fun `isAccessedViaWPComRest return false when origin is not wpcom rest`() {
val site = SiteModel()
Expand Down
159 changes: 159 additions & 0 deletions WordPress/src/test/java/org/wordpress/android/util/VersionUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package org.wordpress.android.util

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class VersionUtilsTest {
@Test
fun `checkMinimalVersion returns true when the major part of the version is higher than the minimal version`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "6.0",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion returns true when the minor part of the version is higher than the minimal version`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.6",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion returns true when the patch part of the version is higher than the minimal version`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5.1",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion returns true when the version is equal to the minimal version`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion returns false when the major part of the version is lower than the minimal version`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "4.0",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isFalse
}

@Test
fun `checkMinimalVersion returns false when the minor part of the version is lower than the minimal version`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.4",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isFalse
}

@Test
fun `checkMinimalVersion returns false when the patch part of the version is lower than the minimal version`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5",
minVersion = "5.5.1"
)
assertThat(hasMinimalVersion).isFalse
}

@Test
fun `checkMinimalVersion ignores suffixes on the version string`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5-beta1",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion ignores suffixes on the minimal version string`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5-beta1",
minVersion = "5.5-beta2"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion ignores double suffixes on the version string`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5-alpha-51379",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion ignores double suffixes on the minimal version string`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5-alpha-51370",
minVersion = "5.5-alpha-51380"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion ignores zero on the patch part of the version string`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5.0",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion ignores zero on the patch part of the minimal version string`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5",
minVersion = "5.5.0"
)
assertThat(hasMinimalVersion).isTrue
}

@Test
fun `checkMinimalVersion returns false when the version string is null`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = null,
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isFalse
}

@Test
fun `checkMinimalVersion returns false when the version string is empty`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "",
minVersion = "5.5"
)
assertThat(hasMinimalVersion).isFalse
}

@Test
fun `checkMinimalVersion returns false when the minimal version string is null`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5",
minVersion = null
)
assertThat(hasMinimalVersion).isFalse
}

@Test
fun `checkMinimalVersion returns false when the minimal version string is empty`() {
val hasMinimalVersion = VersionUtils.checkMinimalVersion(
version = "5.5",
minVersion = ""
)
assertThat(hasMinimalVersion).isFalse
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ext {
androidxWorkVersion = "2.4.0"

daggerVersion = '2.29.1'
fluxCVersion = '1.21.0-beta-5'
fluxCVersion = '1.21.0-beta-6'

appCompatVersion = '1.0.2'
coreVersion = '1.3.2'
Expand Down

0 comments on commit feeacbc

Please sign in to comment.