-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CMM-814 hide or show taxonomies in the menu depending on the server configuration #22255
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
Open
adalpari
wants to merge
20
commits into
trunk
Choose a base branch
from
task/cmm-814-hide-or-show-taxonomies-in-the-menu-depending-on-the-server-configuration
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f00d50e
Updating library
adalpari dc07310
Porting the terms fetching
adalpari 9358243
Porting create
adalpari 9bb9e35
Porting delete term
adalpari 5e62ed7
porting update term
adalpari 726a4d3
Update term fix
adalpari 3a0639b
Creating the new isHierarchical cocal field
adalpari 6bce343
Parent fix
adalpari c39d8fb
Fixing tests
adalpari 630a7b1
detekt
adalpari 10b8ab2
Minor fix
adalpari 03b8a91
Fixing tests
adalpari 7347ee2
Adding the taxonomies menu view model
adalpari 1a3a864
Adding show mechanism
adalpari 025c3ea
Showing taxoniomies
adalpari a7de2e3
Adding a LiveData
adalpari d37d8a2
Call categories and tags screens
adalpari 6f2bcd1
detekt and style
adalpari bbdc79f
Adding tests
adalpari b3e623b
Merge branch 'trunk' into task/cmm-814-hide-or-show-taxonomies-in-the…
adalpari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ess/src/main/java/org/wordpress/android/ui/prefs/taxonomies/TaxonomiesNavMenuViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.wordpress.android.ui.prefs.taxonomies | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.launch | ||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.network.rest.wpapi.rs.WpApiClientProvider | ||
import org.wordpress.android.fluxc.utils.AppLogWrapper | ||
import org.wordpress.android.util.AppLog | ||
import rs.wordpress.api.kotlin.WpRequestResult | ||
import uniffi.wp_api.TaxonomyListParams | ||
import uniffi.wp_api.TaxonomyTypeDetailsWithEditContext | ||
import javax.inject.Inject | ||
|
||
class TaxonomiesNavMenuViewModel @Inject constructor( | ||
private val wpApiClientProvider: WpApiClientProvider, | ||
private val appLogWrapper: AppLogWrapper, | ||
) : ViewModel() { | ||
// LiveData because this is observed from Java | ||
private val _taxonomies = MutableLiveData<List<TaxonomyTypeDetailsWithEditContext>>() | ||
val taxonomies: LiveData<List<TaxonomyTypeDetailsWithEditContext>> = _taxonomies | ||
|
||
fun fetchTaxonomies(site: SiteModel) { | ||
if (!site.isUsingSelfHostedRestApi) { | ||
appLogWrapper.d( | ||
AppLog.T.API, | ||
"Taxonomies - Taxonomies cannot be fetched: Application Password not available" | ||
) | ||
return | ||
} | ||
viewModelScope.launch { | ||
val client = wpApiClientProvider.getWpApiClient(site) | ||
val response = client.request { requestBuilder -> | ||
requestBuilder.taxonomies().listWithEditContext(TaxonomyListParams()) | ||
} | ||
when (response) { | ||
is WpRequestResult.Success -> { | ||
val list = response.response.data | ||
appLogWrapper.d(AppLog.T.API, "Taxonomies - Fetched taxonomies ${list.taxonomyTypes.size}") | ||
val taxonomies = mutableListOf<TaxonomyTypeDetailsWithEditContext>() | ||
list.taxonomyTypes.forEach { type -> | ||
appLogWrapper.d(AppLog.T.API, "Taxonomies - Taxonomy ${type.value.name}") | ||
if (type.value.visibility.showInNavMenus) { | ||
taxonomies.add(type.value) | ||
} | ||
} | ||
_taxonomies.value = taxonomies | ||
} | ||
|
||
else -> { | ||
appLogWrapper.e(AppLog.T.API, "Taxonomies - Error fetching taxonomies") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
...src/test/java/org/wordpress/android/ui/prefs/taxonomies/TaxonomiesNavMenuViewModelTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package org.wordpress.android.ui.prefs.taxonomies | ||
|
||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.advanceUntilIdle | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.mockito.Mock | ||
import org.mockito.MockitoAnnotations | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.never | ||
import org.mockito.kotlin.verify | ||
import org.mockito.kotlin.whenever | ||
import org.wordpress.android.BaseUnitTest | ||
import org.wordpress.android.fluxc.model.SiteModel | ||
import org.wordpress.android.fluxc.network.rest.wpapi.rs.WpApiClientProvider | ||
import org.wordpress.android.fluxc.utils.AppLogWrapper | ||
import org.wordpress.android.util.AppLog | ||
import rs.wordpress.api.kotlin.WpApiClient | ||
import rs.wordpress.api.kotlin.WpRequestResult | ||
import uniffi.wp_api.TaxonomiesRequestListWithEditContextResponse | ||
import uniffi.wp_api.TaxonomyType | ||
import uniffi.wp_api.TaxonomyTypeDetailsWithEditContext | ||
import uniffi.wp_api.TaxonomyTypesResponseWithEditContext | ||
import uniffi.wp_api.TaxonomyTypeVisibility | ||
import uniffi.wp_api.WpNetworkHeaderMap | ||
|
||
@ExperimentalCoroutinesApi | ||
class TaxonomiesNavMenuViewModelTest : BaseUnitTest() { | ||
@Mock | ||
private lateinit var wpApiClientProvider: WpApiClientProvider | ||
|
||
@Mock | ||
private lateinit var wpApiClient: WpApiClient | ||
|
||
@Mock | ||
private lateinit var appLogWrapper: AppLogWrapper | ||
|
||
private lateinit var viewModel: TaxonomiesNavMenuViewModel | ||
|
||
private var taxonomies: List<TaxonomyTypeDetailsWithEditContext> = listOf() | ||
|
||
private val testSite = SiteModel().apply { | ||
id = 123 | ||
url = "https://test.wordpress.com" | ||
apiRestUsernamePlain = "user" | ||
apiRestPasswordPlain = "pass" | ||
setIsWPCom(false) | ||
} | ||
|
||
@Before | ||
fun setUp() { | ||
MockitoAnnotations.openMocks(this) | ||
|
||
whenever(wpApiClientProvider.getWpApiClient(testSite)).thenReturn(wpApiClient) | ||
|
||
viewModel = TaxonomiesNavMenuViewModel( | ||
wpApiClientProvider, | ||
appLogWrapper | ||
) | ||
viewModel.taxonomies.observeForever { taxonomies = it } | ||
} | ||
|
||
@Test | ||
fun `when site does not support self-hosted rest api, then taxonomies are not fetched`() = test { | ||
testSite.setIsWPCom(true) | ||
|
||
viewModel.fetchTaxonomies(testSite) | ||
advanceUntilIdle() | ||
|
||
verify(wpApiClientProvider, never()).getWpApiClient(any(), any()) | ||
verify(appLogWrapper).d( | ||
AppLog.T.API, | ||
"Taxonomies - Taxonomies cannot be fetched: Application Password not available" | ||
) | ||
assertTrue(taxonomies.isEmpty()) | ||
} | ||
|
||
@Test | ||
fun `when LiveData is observed, it starts with null value`() { | ||
assertNotNull(viewModel.taxonomies) | ||
assertEquals(null, viewModel.taxonomies.value) | ||
} | ||
|
||
@Test | ||
fun `fetch taxonomies with success response dispatches success action`() = runTest { | ||
// Create the correct response structure following the MediaRSApiRestClientTest pattern | ||
val response = TaxonomiesRequestListWithEditContextResponse( | ||
createTestTaxonomyTypesResponseWithEditContext(), | ||
mock<WpNetworkHeaderMap>(), | ||
) | ||
|
||
val successResponse: WpRequestResult<TaxonomiesRequestListWithEditContextResponse> = WpRequestResult.Success( | ||
response = response | ||
) | ||
|
||
whenever(wpApiClient.request<TaxonomiesRequestListWithEditContextResponse>(any())).thenReturn(successResponse) | ||
|
||
viewModel.fetchTaxonomies(testSite) | ||
advanceUntilIdle() | ||
|
||
val responseList: List<TaxonomyTypeDetailsWithEditContext> = response.data.taxonomyTypes.map { it.value } | ||
assertEquals(responseList, taxonomies) | ||
} | ||
|
||
@Test | ||
fun `fetch taxonomies with error response do nothing`() = runTest { | ||
// Use a concrete error type that we can create - UnknownError requires statusCode and response | ||
val errorResponse = WpRequestResult.UnknownError<Any>( | ||
statusCode = 500u, | ||
response = "Internal Server Error" | ||
) | ||
|
||
whenever(wpApiClient.request<Any>(any())).thenReturn(errorResponse) | ||
|
||
viewModel.fetchTaxonomies(testSite) | ||
|
||
verify(appLogWrapper).e(any(), any()) | ||
assertTrue(taxonomies.isEmpty()) | ||
} | ||
|
||
private fun createTestTaxonomyTypesResponseWithEditContext(): TaxonomyTypesResponseWithEditContext { | ||
val visibility = TaxonomyTypeVisibility( | ||
public = true, | ||
publiclyQueryable = true, | ||
showUi = true, | ||
showAdminColumn = true, | ||
showInNavMenus = true, | ||
showInQuickEdit = true | ||
) | ||
|
||
val categoryDetails = TaxonomyTypeDetailsWithEditContext( | ||
name = "Categories", | ||
slug = "category", | ||
description = "Test categories", | ||
visibility = visibility, | ||
restBase = "categories", | ||
restNamespace = "wp/v2", | ||
types = listOf("post"), | ||
hierarchical = true, | ||
showCloud = true, | ||
capabilities = mock(), | ||
labels = mock() | ||
) | ||
|
||
val tagDetails = TaxonomyTypeDetailsWithEditContext( | ||
name = "Tags", | ||
slug = "post_tag", | ||
description = "Test tags", | ||
visibility = visibility, | ||
restBase = "tags", | ||
restNamespace = "wp/v2", | ||
types = listOf("post"), | ||
hierarchical = false, | ||
showCloud = true, | ||
capabilities = mock(), | ||
labels = mock() | ||
) | ||
|
||
return TaxonomyTypesResponseWithEditContext( | ||
mapOf( | ||
TaxonomyType.Category to categoryDetails, | ||
TaxonomyType.PostTag to tagDetails | ||
) | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Android Lint
Nullable/NonNull annotation missing on field Note