Skip to content

Commit

Permalink
Merge pull request #3066 from wordpress-mobile/tweat-duplication-logi…
Browse files Browse the repository at this point in the history
…c-to-support-idc

Update the site duplication logic to not throw when IDC occurs
  • Loading branch information
hichamboushaba authored Aug 1, 2024
2 parents cc6b2f8 + cf23c7c commit ca50915
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -604,32 +604,6 @@ public void testWPComAutomatedTransfer() throws DuplicateSiteException {
assertEquals(0, mSiteStore.getWPComSitesCount());
}

@Test
public void testBatchInsertSiteDuplicateWPCom()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
WellSqlTestUtils.setupWordPressComAccount();

List<SiteModel> siteList = new ArrayList<>();
siteList.add(generateTestSite(1, "https://pony1.com", "https://pony1.com/xmlrpc.php", true, true));
siteList.add(generateTestSite(2, "https://pony2.com", "https://pony2.com/xmlrpc.php", true, true));
siteList.add(generateTestSite(3, "https://pony3.com", "https://pony3.com/xmlrpc.php", true, true));
// duplicate with a different id, we should ignore it
siteList.add(generateTestSite(4, "https://pony3.com", "https://pony3.com/xmlrpc.php", true, true));
siteList.add(generateTestSite(5, "https://pony4.com", "https://pony4.com/xmlrpc.php", true, true));
siteList.add(generateTestSite(6, "https://pony5.com", "https://pony5.com/xmlrpc.php", true, true));

SitesModel sites = new SitesModel(siteList);

// Use reflection to call a private Store method: equivalent to mSiteStore.updateSites(sites)
Method createOrUpdateSites = SiteStore.class.getDeclaredMethod("createOrUpdateSites", SitesModel.class);
createOrUpdateSites.setAccessible(true);
UpdateSitesResult res = (UpdateSitesResult) createOrUpdateSites.invoke(mSiteStore, sites);

assertTrue(res.duplicateSiteFound);
assertEquals(5, res.rowsAffected);
assertEquals(5, mSiteStore.getSitesCount());
}

@Test
public void testBatchInsertSiteNoDuplicateWPCom()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Expand All @@ -654,36 +628,6 @@ public void testBatchInsertSiteNoDuplicateWPCom()
assertEquals(5, mSiteStore.getSitesCount());
}

@Test
public void testSingleInsertSiteDuplicateWPCom()
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
WellSqlTestUtils.setupWordPressComAccount();

List<SiteModel> siteList = new ArrayList<>();
siteList.add(generateTestSite(1, "https://pony1.com", "https://pony1.com/xmlrpc.php", true, true));
SitesModel sites = new SitesModel(siteList);

// Insert 1 site
Method createOrUpdateSites = SiteStore.class.getDeclaredMethod("createOrUpdateSites", SitesModel.class);
createOrUpdateSites.setAccessible(true);
UpdateSitesResult res = (UpdateSitesResult) createOrUpdateSites.invoke(mSiteStore, sites);

assertFalse(res.duplicateSiteFound);
assertEquals(1, res.rowsAffected);
assertEquals(1, mSiteStore.getSitesCount());

// Insert same site with different id (considered a duplicate)
List<SiteModel> siteList2 = new ArrayList<>();
siteList2.add(generateTestSite(2, "https://pony1.com", "https://pony1.com/xmlrpc.php", true, true));
SitesModel sites2 = new SitesModel(siteList2);
createOrUpdateSites.setAccessible(true);
UpdateSitesResult res2 = (UpdateSitesResult) createOrUpdateSites.invoke(mSiteStore, sites2);

assertTrue(res2.duplicateSiteFound);
assertEquals(0, res2.rowsAffected);
assertEquals(1, mSiteStore.getSitesCount());
}

@Test
public void testInsertSiteDuplicateXmlRpcTrailingSlash() throws DuplicateSiteException {
// It's possible for the URL in `wp.getOptions` to be different from the URL in `wp.getUsersBlogs`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class SiteSqlUtils
* 5. Exists in the DB, originally an XML-RPC site, and matches by XMLRPC_URL -> UPDATE
* 6. Not matching any previous cases -> INSERT
*/
@Suppress("LongMethod", "ReturnCount")
@Suppress("LongMethod", "ReturnCount", "ComplexMethod")
@Throws(DuplicateSiteException::class)
fun insertOrUpdateSite(site: SiteModel?): Int {
if (site == null) {
Expand Down Expand Up @@ -172,12 +172,14 @@ class SiteSqlUtils
.equals(SiteModelTable.XMLRPC_URL, forcedHttpXmlRpcUrl)
.or().equals(SiteModelTable.XMLRPC_URL, forcedHttpsXmlRpcUrl)
.endGroup()
.endWhere().asModel
if (!siteResult.isEmpty()) {
.endWhere()
.asModel
if (siteResult.isNotEmpty()) {
AppLog.d(DB, "Site found using XML-RPC url: " + site.xmlRpcUrl)
// Four possibilities here:
// 1. DB site is WP.com, new site is WP.com:
// Something really weird is happening, this should have been caught earlier --> DuplicateSiteException
// 1. DB site is WP.com, new site is WP.com with different siteIds:
// The site could be having an "Identity Crisis", while this should be fixed on the site itself,
// it shouldn't block sign-in -> proceed
// 2. DB site is WP.com, new site is XML-RPC:
// It looks like an existing Jetpack-connected site over the REST API was added again as an XML-RPC
// Wed don't allow this --> DuplicateSiteException
Expand All @@ -186,7 +188,13 @@ class SiteSqlUtils
// 4. DB site is XML-RPC, new site is XML-RPC:
// An existing self-hosted site was logged-into again, and we couldn't identify it by URL or
// by WP.com site ID + URL --> proceed
if (siteResult[0].origin == SiteModel.ORIGIN_WPCOM_REST) {
if (siteResult[0].origin == SiteModel.ORIGIN_WPCOM_REST && site.origin == SiteModel.ORIGIN_WPCOM_REST) {
AppLog.d(
DB,
"Duplicate WPCom sites with same URLs, it could be an Identity Crisis, insert both sites"
)
siteResult = emptyList()
} else if (siteResult[0].origin == SiteModel.ORIGIN_WPCOM_REST) {
AppLog.d(DB, "Site is a duplicate")
throw DuplicateSiteException
}
Expand Down

0 comments on commit ca50915

Please sign in to comment.