Skip to content
This repository has been archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
Merge pull request #1702 from wordpress-mobile/issue/selfhosted_block…
Browse files Browse the repository at this point in the history
…_layouts

Extends FETCH_BLOCK_LAYOUTS action for self-hosted sites
  • Loading branch information
Antonis Lilis authored Oct 12, 2020
2 parents 3b8f742 + 01ab9c6 commit d9f29f5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.wordpress.android.fluxc.store.AccountStore.AuthenticationErrorType;
import org.wordpress.android.fluxc.store.AccountStore.OnAuthenticationChanged;
import org.wordpress.android.fluxc.store.SiteStore;
import org.wordpress.android.fluxc.store.SiteStore.OnBlockLayoutsFetched;
import org.wordpress.android.fluxc.store.SiteStore.OnPostFormatsChanged;
import org.wordpress.android.fluxc.store.SiteStore.OnProfileFetched;
import org.wordpress.android.fluxc.store.SiteStore.OnSiteChanged;
Expand All @@ -30,6 +31,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
Expand All @@ -49,6 +51,7 @@ private enum TestEvents {
SITE_CHANGED,
POST_FORMATS_CHANGED,
SITE_REMOVED,
BLOCK_LAYOUTS_FETCHED,
FETCHED_PROFILE
}

Expand Down Expand Up @@ -230,6 +233,19 @@ public void testFetchPostFormats() throws InterruptedException {
assertEquals(10, postFormats.size());
}

@Test
public void testFetchBlockLayouts() throws InterruptedException {
fetchSites(BuildConfig.TEST_WPORG_USERNAME_SH_SIMPLE,
BuildConfig.TEST_WPORG_PASSWORD_SH_SIMPLE,
BuildConfig.TEST_WPORG_URL_SH_SIMPLE_ENDPOINT);

SiteModel firstSite = mSiteStore.getSites().get(0);
mNextEvent = TestEvents.BLOCK_LAYOUTS_FETCHED;
mDispatcher.dispatch(SiteActionBuilder.newFetchBlockLayoutsAction(firstSite));
mCountDownLatch = new CountDownLatch(1);
assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS));
}

@SuppressWarnings("unused")
@Subscribe
public void onSiteChanged(OnSiteChanged event) {
Expand Down Expand Up @@ -306,6 +322,19 @@ public void onPostFormatsChanged(OnPostFormatsChanged event) {
mCountDownLatch.countDown();
}

@SuppressWarnings("unused")
@Subscribe
public void onBlockLayoutsFetched(OnBlockLayoutsFetched event) {
assertEquals(mNextEvent, TestEvents.BLOCK_LAYOUTS_FETCHED);
assertFalse(event.isError());
assertEquals(TestEvents.BLOCK_LAYOUTS_FETCHED, mNextEvent);
assertNotNull(event.categories);
assertNotNull(event.layouts);
assertFalse(event.categories.isEmpty());
assertFalse(event.layouts.isEmpty());
mCountDownLatch.countDown();
}

private void fetchProfile(String username, String password, String endpointUrl) throws InterruptedException {
RefreshSitesXMLRPCPayload payload = new RefreshSitesXMLRPCPayload();
payload.username = username;
Expand Down
1 change: 1 addition & 0 deletions fluxc-processor/src/main/resources/wp-com-v2-endpoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/sites/$site/stats/top-earners

/sites/$site/block-layouts
/common-block-layouts

/users/username/suggestions/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,18 @@ public void onErrorResponse(@NonNull WPComGsonNetworkError error) {
add(request);
}

public void fetchBlockLayouts(final SiteModel site) {
Map<String, String> params = new HashMap<>();
public void fetchWpComBlockLayouts(final SiteModel site) {
String url = WPCOMV2.sites.site(site.getSiteId()).block_layouts.getUrl();
fetchBlockLayouts(site, url);
}

public void fetchSelfHostedBlockLayouts(final SiteModel site) {
String url = WPCOMV2.common_block_layouts.getUrl();
fetchBlockLayouts(site, url);
}

private void fetchBlockLayouts(final SiteModel site, String url) {
Map<String, String> params = new HashMap<>();
final WPComGsonRequest<BlockLayoutsResponse> request = WPComGsonRequest.buildGetRequest(url, params,
BlockLayoutsResponse.class,
new Listener<BlockLayoutsResponse>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,9 @@ private void fetchSiteEditors(SiteModel site) {

private void fetchBlockLayouts(SiteModel site) {
if (site.isUsingWpComRestApi()) {
mSiteRestClient.fetchBlockLayouts(site);
mSiteRestClient.fetchWpComBlockLayouts(site);
} else {
mSiteRestClient.fetchSelfHostedBlockLayouts(site);
}
}

Expand Down

0 comments on commit d9f29f5

Please sign in to comment.