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

Commit ca50915

Browse files
Merge pull request #3066 from wordpress-mobile/tweat-duplication-logic-to-support-idc
Update the site duplication logic to not throw when IDC occurs
2 parents cc6b2f8 + cf23c7c commit ca50915

File tree

2 files changed

+14
-62
lines changed

2 files changed

+14
-62
lines changed

example/src/test/java/org/wordpress/android/fluxc/site/SiteStoreUnitTest.java

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -604,32 +604,6 @@ public void testWPComAutomatedTransfer() throws DuplicateSiteException {
604604
assertEquals(0, mSiteStore.getWPComSitesCount());
605605
}
606606

607-
@Test
608-
public void testBatchInsertSiteDuplicateWPCom()
609-
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
610-
WellSqlTestUtils.setupWordPressComAccount();
611-
612-
List<SiteModel> siteList = new ArrayList<>();
613-
siteList.add(generateTestSite(1, "https://pony1.com", "https://pony1.com/xmlrpc.php", true, true));
614-
siteList.add(generateTestSite(2, "https://pony2.com", "https://pony2.com/xmlrpc.php", true, true));
615-
siteList.add(generateTestSite(3, "https://pony3.com", "https://pony3.com/xmlrpc.php", true, true));
616-
// duplicate with a different id, we should ignore it
617-
siteList.add(generateTestSite(4, "https://pony3.com", "https://pony3.com/xmlrpc.php", true, true));
618-
siteList.add(generateTestSite(5, "https://pony4.com", "https://pony4.com/xmlrpc.php", true, true));
619-
siteList.add(generateTestSite(6, "https://pony5.com", "https://pony5.com/xmlrpc.php", true, true));
620-
621-
SitesModel sites = new SitesModel(siteList);
622-
623-
// Use reflection to call a private Store method: equivalent to mSiteStore.updateSites(sites)
624-
Method createOrUpdateSites = SiteStore.class.getDeclaredMethod("createOrUpdateSites", SitesModel.class);
625-
createOrUpdateSites.setAccessible(true);
626-
UpdateSitesResult res = (UpdateSitesResult) createOrUpdateSites.invoke(mSiteStore, sites);
627-
628-
assertTrue(res.duplicateSiteFound);
629-
assertEquals(5, res.rowsAffected);
630-
assertEquals(5, mSiteStore.getSitesCount());
631-
}
632-
633607
@Test
634608
public void testBatchInsertSiteNoDuplicateWPCom()
635609
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
@@ -654,36 +628,6 @@ public void testBatchInsertSiteNoDuplicateWPCom()
654628
assertEquals(5, mSiteStore.getSitesCount());
655629
}
656630

657-
@Test
658-
public void testSingleInsertSiteDuplicateWPCom()
659-
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
660-
WellSqlTestUtils.setupWordPressComAccount();
661-
662-
List<SiteModel> siteList = new ArrayList<>();
663-
siteList.add(generateTestSite(1, "https://pony1.com", "https://pony1.com/xmlrpc.php", true, true));
664-
SitesModel sites = new SitesModel(siteList);
665-
666-
// Insert 1 site
667-
Method createOrUpdateSites = SiteStore.class.getDeclaredMethod("createOrUpdateSites", SitesModel.class);
668-
createOrUpdateSites.setAccessible(true);
669-
UpdateSitesResult res = (UpdateSitesResult) createOrUpdateSites.invoke(mSiteStore, sites);
670-
671-
assertFalse(res.duplicateSiteFound);
672-
assertEquals(1, res.rowsAffected);
673-
assertEquals(1, mSiteStore.getSitesCount());
674-
675-
// Insert same site with different id (considered a duplicate)
676-
List<SiteModel> siteList2 = new ArrayList<>();
677-
siteList2.add(generateTestSite(2, "https://pony1.com", "https://pony1.com/xmlrpc.php", true, true));
678-
SitesModel sites2 = new SitesModel(siteList2);
679-
createOrUpdateSites.setAccessible(true);
680-
UpdateSitesResult res2 = (UpdateSitesResult) createOrUpdateSites.invoke(mSiteStore, sites2);
681-
682-
assertTrue(res2.duplicateSiteFound);
683-
assertEquals(0, res2.rowsAffected);
684-
assertEquals(1, mSiteStore.getSitesCount());
685-
}
686-
687631
@Test
688632
public void testInsertSiteDuplicateXmlRpcTrailingSlash() throws DuplicateSiteException {
689633
// It's possible for the URL in `wp.getOptions` to be different from the URL in `wp.getUsersBlogs`,

fluxc/src/main/java/org/wordpress/android/fluxc/persistence/SiteSqlUtils.kt

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class SiteSqlUtils
107107
* 5. Exists in the DB, originally an XML-RPC site, and matches by XMLRPC_URL -> UPDATE
108108
* 6. Not matching any previous cases -> INSERT
109109
*/
110-
@Suppress("LongMethod", "ReturnCount")
110+
@Suppress("LongMethod", "ReturnCount", "ComplexMethod")
111111
@Throws(DuplicateSiteException::class)
112112
fun insertOrUpdateSite(site: SiteModel?): Int {
113113
if (site == null) {
@@ -172,12 +172,14 @@ class SiteSqlUtils
172172
.equals(SiteModelTable.XMLRPC_URL, forcedHttpXmlRpcUrl)
173173
.or().equals(SiteModelTable.XMLRPC_URL, forcedHttpsXmlRpcUrl)
174174
.endGroup()
175-
.endWhere().asModel
176-
if (!siteResult.isEmpty()) {
175+
.endWhere()
176+
.asModel
177+
if (siteResult.isNotEmpty()) {
177178
AppLog.d(DB, "Site found using XML-RPC url: " + site.xmlRpcUrl)
178179
// Four possibilities here:
179-
// 1. DB site is WP.com, new site is WP.com:
180-
// Something really weird is happening, this should have been caught earlier --> DuplicateSiteException
180+
// 1. DB site is WP.com, new site is WP.com with different siteIds:
181+
// The site could be having an "Identity Crisis", while this should be fixed on the site itself,
182+
// it shouldn't block sign-in -> proceed
181183
// 2. DB site is WP.com, new site is XML-RPC:
182184
// It looks like an existing Jetpack-connected site over the REST API was added again as an XML-RPC
183185
// Wed don't allow this --> DuplicateSiteException
@@ -186,7 +188,13 @@ class SiteSqlUtils
186188
// 4. DB site is XML-RPC, new site is XML-RPC:
187189
// An existing self-hosted site was logged-into again, and we couldn't identify it by URL or
188190
// by WP.com site ID + URL --> proceed
189-
if (siteResult[0].origin == SiteModel.ORIGIN_WPCOM_REST) {
191+
if (siteResult[0].origin == SiteModel.ORIGIN_WPCOM_REST && site.origin == SiteModel.ORIGIN_WPCOM_REST) {
192+
AppLog.d(
193+
DB,
194+
"Duplicate WPCom sites with same URLs, it could be an Identity Crisis, insert both sites"
195+
)
196+
siteResult = emptyList()
197+
} else if (siteResult[0].origin == SiteModel.ORIGIN_WPCOM_REST) {
190198
AppLog.d(DB, "Site is a duplicate")
191199
throw DuplicateSiteException
192200
}

0 commit comments

Comments
 (0)