Skip to content

Commit

Permalink
Merge pull request #10322 from wordpress-mobile/issue/self-hosted-rem…
Browse files Browse the repository at this point in the history
…ote-preview-should-work-for-drafts

Make remote preview works for drafts on self hosted sites
  • Loading branch information
shiki authored Aug 6, 2019
2 parents e13e99e + d1ab054 commit 6a3a0e9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,47 @@ class RemotePreviewLogicHelper @Inject constructor(
post: PostModel,
helperFunctions: RemotePreviewHelperFunctions
): PreviewLogicOperationResult {
if (!site.isUsingWpComRestApi) {
activityLauncherWrapper.showActionableEmptyView(
activity,
WPWebViewUsageCategory.REMOTE_PREVIEW_NOT_AVAILABLE,
post.title
)
return PreviewLogicOperationResult.PREVIEW_NOT_AVAILABLE
} else if (!networkUtilsWrapper.isNetworkAvailable()) {
if (!networkUtilsWrapper.isNetworkAvailable()) {
activityLauncherWrapper.showActionableEmptyView(
activity,
WPWebViewUsageCategory.REMOTE_PREVIEW_NO_NETWORK,
post.title
)
return PreviewLogicOperationResult.NETWORK_NOT_AVAILABLE
} else {
if (helperFunctions.notifyUploadInProgress(post)) {
return PreviewLogicOperationResult.MEDIA_UPLOAD_IN_PROGRESS
}
}

// If a media upload is currently in progress, we can't upload / auto-save or preview until it's finished.
if (helperFunctions.notifyUploadInProgress(post)) {
return PreviewLogicOperationResult.MEDIA_UPLOAD_IN_PROGRESS
}

val updatedPost = helperFunctions.updatePostIfNeeded() ?: post
// Update the post object (copy title/content from the editor to the post object) when it's needed
// (eg. during and editing session)
val updatedPost = helperFunctions.updatePostIfNeeded() ?: post

if (shouldUpload(updatedPost)) {
return when {
shouldUpload(updatedPost) -> {
// We can't upload an unpublishable post (empty), we'll let the user know we can't preview it.
if (!postUtilsWrapper.isPublishable(updatedPost)) {
helperFunctions.notifyEmptyDraft()
return PreviewLogicOperationResult.CANNOT_SAVE_EMPTY_DRAFT
}
helperFunctions.startUploading(false, updatedPost)
} else if (shouldRemoteAutoSave(updatedPost)) {
PreviewLogicOperationResult.GENERATING_PREVIEW
}
shouldRemoteAutoSave(updatedPost) -> {
// We don't support remote auto-save for self hosted sites (accessed via XMLRPC),
// we make the preview unavailable in that case.
if (!site.isUsingWpComRestApi) {
activityLauncherWrapper.showActionableEmptyView(
activity,
WPWebViewUsageCategory.REMOTE_PREVIEW_NOT_AVAILABLE,
post.title
)
return PreviewLogicOperationResult.PREVIEW_NOT_AVAILABLE
}

// We can't remote auto-save an unpublishable post (empty), we'll let the user know we can't preview it.
if (!postUtilsWrapper.isPublishable(updatedPost)) {
helperFunctions.notifyEmptyPost()
return PreviewLogicOperationResult.CANNOT_REMOTE_AUTO_SAVE_EMPTY_POST
Expand All @@ -90,17 +103,19 @@ class RemotePreviewLogicHelper @Inject constructor(
return PreviewLogicOperationResult.PREVIEW_NOT_AVAILABLE
}
helperFunctions.startUploading(true, updatedPost)
} else {
PreviewLogicOperationResult.GENERATING_PREVIEW
}
else -> {
// If we don't need upload or auto save the post (eg. post not modified), open the preview directly.
activityLauncherWrapper.previewPostOrPageForResult(
activity,
site,
updatedPost,
RemotePreviewType.REMOTE_PREVIEW
)
return PreviewLogicOperationResult.OPENING_PREVIEW
PreviewLogicOperationResult.OPENING_PREVIEW
}
}
return PreviewLogicOperationResult.GENERATING_PREVIEW
}

private fun shouldUpload(post: PostModel): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ class RemotePreviewLogicHelperTest {
}

@Test
fun `preview not available for self hosted sites not using WPComRestApi`() {
fun `preview not available for self hosted sites not using WPComRestApi on published post with modifications`() {
// Given
doReturn(false).whenever(site).isUsingWpComRestApi
doReturn(PostStatus.PUBLISHED.toString()).whenever(post).status
doReturn(true).whenever(post).isLocallyChanged

// When
val result = remotePreviewLogicHelper.runPostPreviewLogic(activity, site, post, mock())
Expand All @@ -85,6 +87,20 @@ class RemotePreviewLogicHelperTest {
)
}

@Test
fun `preview available for self hosted sites not using WPComRestApi on drafts`() {
// Given
// next stub not used (made lenient) in case we update future logic.
lenient().doReturn(false).whenever(site).isUsingWpComRestApi

// When
val result = remotePreviewLogicHelper.runPostPreviewLogic(activity, site, post, helperFunctions)

// Then
assertThat(result).isEqualTo(RemotePreviewLogicHelper.PreviewLogicOperationResult.GENERATING_PREVIEW)
verify(helperFunctions, times(1)).startUploading(false, post)
}

@Test
fun `preview not available if network not available`() {
// Given
Expand Down

0 comments on commit 6a3a0e9

Please sign in to comment.