Repository: wordpress-mobile/WordPress-Android (language: kotlin)

Issue:
Jetpack Social section is visible on private posts
Refs pctCYC-W4-p2#comment-989

### Expected behavior
The Jetpack Social section should be hidden from the pre-publishing sheet and post settings when the post is set to private.

### Actual behavior
The Jetpack Social section is shown for private posts, but the post is not auto-shared.

### Steps to reproduce the behavior

- Launch the Jetpack app.
- Create a new blog post.
- Set it to private.
- Observe that the Jetpack Social section is visible in post settings and pre-publishing sheet.

##### Tested on Jetpack Android 23.0-rc-1


Discussion hints:

Pull request: [Jetpack Social] Hide Jetpack Social container if PostStatus is PRIVATE
Fixes #18927

To test:
You should not be able to reproduce the bug anymore. Steps to reproduce can be found in https://github.com/wordpress-mobile/WordPress-Android/issues/18927

## Regression Notes
1. Potential unintended areas of impact
Jetpack Social containers should continue to work as expected.

2. What I did to test those areas of impact (or what existing automated tests I relied on)
Manual testing + unit testing.

3. What automated tests I added (or what prevented me from doing so)
Updated `EditorJetpackSocialViewModelTest.kt`.

PR submission checklist:

- [x] I have completed the Regression Notes.
- [x] I have considered adding accessibility improvements for my changes.
- [x] I have considered if this change warrants user-facing release notes and have added them to `RELEASE-NOTES.txt` if necessary.

UI Changes testing checklist:

- [ ] Portrait and landscape orientations.
- [ ] Light and dark modes.
- [ ] Fonts: Larger, smaller and bold text.
- [ ] High contrast.
- [ ] Talkback.
- [ ] Languages with large words or with letters/accents not frequently used in English.
- [ ] Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
- [ ] Large and small screen sizes. (Tablet and smaller phones)
- [ ] Multi-tasking: Split screen and Pop-up view. (Android 10 or higher)


Test command: ./gradlew :WordPress:testWordpressVanillaDebugUnitTest --tests "org.wordpress.android.ui.posts.EditorJetpackSocialViewModelTest"

Fix patch:
diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostSettingsFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostSettingsFragment.java
index fcc44f840f07..17a68c9c050c 100644
--- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostSettingsFragment.java
+++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostSettingsFragment.java
@@ -938,6 +938,7 @@ void updatePostStatus(PostStatus postStatus) {
                     postImmutableModel -> {
                         updatePostStatusRelatedViews(postImmutableModel);
                         updateSaveButton();
+                        mJetpackSocialViewModel.onPostStatusChanged();
                         return null;
                     });
         }
diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditorJetpackSocialViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditorJetpackSocialViewModel.kt
index a2eaf0135a5e..8716b4e72dff 100644
--- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditorJetpackSocialViewModel.kt
+++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditorJetpackSocialViewModel.kt
@@ -85,7 +85,11 @@ class EditorJetpackSocialViewModel @Inject constructor(
         this.siteModel = siteModel
         this.editPostRepository = editPostRepository
 
-        if (jetpackSocialFeatureConfig.isEnabled() && !editPostRepository.isPage) {
+        startJetpackSocial()
+    }
+
+    private fun startJetpackSocial() {
+        if (shouldShowJetpackSocial()) {
             launch {
                 shareLimit = getJetpackSocialShareLimitStatusUseCase.execute(siteModel)
                 loadConnections()
@@ -164,8 +168,7 @@ class EditorJetpackSocialViewModel @Inject constructor(
     }
 
     private suspend fun loadJetpackSocialIfSupported() {
-        val showJetpackSocial = jetpackSocialFeatureConfig.isEnabled() && siteModel.supportsPublicize()
-        if (!showJetpackSocial) {
+        if (!shouldShowJetpackSocial()) {
             _jetpackSocialContainerVisibility.postValue(JetpackSocialContainerVisibility.ALL_HIDDEN)
             return
         }
@@ -187,6 +190,11 @@ class EditorJetpackSocialViewModel @Inject constructor(
         }
     }
 
+    private fun shouldShowJetpackSocial() = jetpackSocialFeatureConfig.isEnabled()
+            && !editPostRepository.isPage
+            && siteModel.supportsPublicize()
+            && currentPost?.status != PostStatus.PRIVATE.toString()
+
     private suspend fun mapLoaded(): JetpackSocialUiState.Loaded {
         val shareMessage = jetpackSocialShareMessage.ifEmpty {
             getJetpackSocialShareMessageUseCase.execute(currentPost?.id ?: -1)
@@ -383,6 +391,10 @@ class EditorJetpackSocialViewModel @Inject constructor(
         if (trackEvent) jetpackSocialSharingTracker.trackAddConnectionCtaDisplayed(jetpackSocialFlow)
     }
 
+    fun onPostStatusChanged() {
+        startJetpackSocial()
+    }
+
     data class JetpackSocialContainerVisibility(
         val showInPrepublishingSheet: Boolean,
         val showInPostSettings: Boolean,


Test patch:
diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/EditorJetpackSocialViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/EditorJetpackSocialViewModelTest.kt
index 68e92a6e3a22..1e05b7d4f486 100644
--- a/WordPress/src/test/java/org/wordpress/android/ui/posts/EditorJetpackSocialViewModelTest.kt
+++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/EditorJetpackSocialViewModelTest.kt
@@ -16,7 +16,9 @@ import org.wordpress.android.R
 import org.wordpress.android.datasets.wrappers.PublicizeTableWrapper
 import org.wordpress.android.fluxc.Dispatcher
 import org.wordpress.android.fluxc.model.AccountModel
+import org.wordpress.android.fluxc.model.PostModel
 import org.wordpress.android.fluxc.model.SiteModel
+import org.wordpress.android.fluxc.model.post.PostStatus
 import org.wordpress.android.fluxc.store.AccountStore
 import org.wordpress.android.fluxc.store.SiteStore
 import org.wordpress.android.models.PublicizeConnection
@@ -116,6 +118,27 @@ class EditorJetpackSocialViewModelTest : BaseUnitTest() {
         verify(getPublicizeConnectionsForUserUseCase, never()).execute(any(), any(), any())
     }
 
+    @Test
+    fun `Should NOT load jetpack social if post is page`() = test {
+        whenever(jetpackSocialFeatureConfig.isEnabled())
+            .thenReturn(true)
+        whenever(editPostRepository.isPage).thenReturn(true)
+        classToTest.start(FAKE_SITE_MODEL, editPostRepository)
+        verify(getPublicizeConnectionsForUserUseCase, never()).execute(any(), any(), any())
+    }
+
+    @Test
+    fun `Should NOT load jetpack social if PostStatus is PRIVATE`() = test {
+        whenever(jetpackSocialFeatureConfig.isEnabled())
+            .thenReturn(true)
+        whenever(editPostRepository.isPage).thenReturn(false)
+        whenever(editPostRepository.getPost()).thenReturn(PostModel().apply {
+            setStatus(PostStatus.PRIVATE.toString())
+        })
+        classToTest.start(FAKE_SITE_MODEL, editPostRepository)
+        verify(getPublicizeConnectionsForUserUseCase, never()).execute(any(), any(), any())
+    }
+
     @Test
     fun `Should hide jetpack social container if FF is disabled`() = test {
         whenever(jetpackSocialFeatureConfig.isEnabled())
@@ -525,6 +548,12 @@ class EditorJetpackSocialViewModelTest : BaseUnitTest() {
         verify(jetpackSocialSharingTracker).trackConnectionToggled(JetpackSocialFlow.PRE_PUBLISHING, false)
     }
 
+    @Test
+    fun `Should start jetpack social if onPostVisibilityChanged is called`() {
+        classToTest.onPostStatusChanged()
+        verify(showJetpackSocialContainerObserver).onChanged(any())
+    }
+
     private fun fakeSiteModel(supportsPublicize: Boolean = false): SiteModel = FAKE_SITE_MODEL.apply {
         siteId = FAKE_REMOTE_SITE_ID
         url = "mysite.blog"