From dda32d4c7c87d09383c71fc4a34503ede8eb59ea Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Tue, 1 Jun 2021 18:43:21 +1000 Subject: [PATCH 1/7] Add test content for refactored gallery format --- .../TestContent.kt | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/TestContent.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/TestContent.kt index 54f321ce8a82..6ad93f7ee72f 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/TestContent.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/TestContent.kt @@ -261,6 +261,61 @@ object TestContent { +""" + + const val oldRefactoredGalleryBlockInnerBlocks = """ +
+ +
Gutenberg on web
+
+ + + +
+ +
Gutenberg on web
+
+ + + +
+ +
Gutenberg on web
+
+ +""" + const val oldRefactoredGalleryBlock = """ + + +""" + + const val newRefactoredGalleryBlockInnerBlocks = """ +
+ +
Gutenberg on web
+
+ + + +
+ +
Gutenberg on web
+
+ + + +
+ +
Gutenberg on web
+
+ +""" + + const val newRefactoredGalleryBlock = """ + + """ const val paragraphBlock = """ @@ -595,4 +650,8 @@ object TestContent { const val newPostWithJsonNullId = paragraphBlock + newImageBlock + imageBlockWithJsonNullId + newMediaTextBlock const val oldPostWithGalleryJsonNullId = paragraphBlock + oldImageBlock + galleryBlockWithJsonNullId const val newPostWithGalleryJsonNullId = paragraphBlock + newImageBlock + galleryBlockWithJsonNullId + const val oldPostWithMixedGalleriesOriginal = paragraphBlock + oldGalleryBlock + newRefactoredGalleryBlock + const val newPostWithMixedGalleriesOriginal = paragraphBlock + newGalleryBlock + newRefactoredGalleryBlock + const val oldPostWithMixedGalleriesRefactored = paragraphBlock + newGalleryBlock + oldRefactoredGalleryBlock + const val newPostWithMixedGalleriesRefactored = paragraphBlock + newGalleryBlock + newRefactoredGalleryBlock } From 5ce0446e99d58af5d8ca7384bff5f1e9fbbf44ea Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Tue, 1 Jun 2021 18:43:43 +1000 Subject: [PATCH 2/7] Add gallery processor tests for refactored gallery --- .../GalleryBlockProcessorTest.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/GalleryBlockProcessorTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/GalleryBlockProcessorTest.kt index 310d17f8f381..8cbd56204251 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/GalleryBlockProcessorTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/GalleryBlockProcessorTest.kt @@ -2,6 +2,7 @@ package org.wordpress.android.ui.posts.mediauploadcompletionprocessors import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.whenever import org.assertj.core.api.Assertions import org.junit.Test @@ -11,6 +12,7 @@ import org.wordpress.android.util.helpers.MediaFile class GalleryBlockProcessorTest { private val mediaFile: MediaFile = mock() + private val mediaUploadCompletionProcessor: MediaUploadCompletionProcessor = mock() private lateinit var processor: GalleryBlockProcessor @Before @@ -18,7 +20,8 @@ class GalleryBlockProcessorTest { whenever(mediaFile.mediaId).thenReturn(TestContent.remoteMediaId) whenever(mediaFile.fileURL).thenReturn(TestContent.remoteImageUrl) whenever(mediaFile.getAttachmentPageURL(any())).thenReturn(TestContent.attachmentPageUrl) - processor = GalleryBlockProcessor(TestContent.localMediaId, mediaFile, TestContent.siteUrl) + processor = GalleryBlockProcessor(TestContent.localMediaId, mediaFile, TestContent.siteUrl, + mediaUploadCompletionProcessor) } @Test @@ -56,4 +59,10 @@ class GalleryBlockProcessorTest { val processedBlock = processor.processBlock(TestContent.oldGalleryBlockLinkToAttachmentPage) Assertions.assertThat(processedBlock).isEqualTo(TestContent.newGalleryBlockLinkToAttachmentPage) } + + @Test + fun `processBlock defers the refactored gallery to inner blocks recursion`() { + processor.processBlock(TestContent.oldRefactoredGalleryBlock) + verify(mediaUploadCompletionProcessor).processContent(TestContent.oldRefactoredGalleryBlockInnerBlocks) + } } From 68a2e70660a5e53da69abaa9381b9b1eafa8c625 Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Tue, 1 Jun 2021 18:44:10 +1000 Subject: [PATCH 3/7] Add post processor tests for refactored gallery --- .../MediaUploadCompletionProcessorTest.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/MediaUploadCompletionProcessorTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/MediaUploadCompletionProcessorTest.kt index acdb9ce48da6..c10bb66fb50e 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/MediaUploadCompletionProcessorTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/MediaUploadCompletionProcessorTest.kt @@ -94,4 +94,16 @@ class MediaUploadCompletionProcessorTest { val processedContent = processor.processContent(TestContent.oldPostWithGalleryJsonNullId) Assertions.assertThat(processedContent).isEqualTo(TestContent.newPostWithGalleryJsonNullId) } + + @Test + fun `processPost can handle original galleries with refactored galleries present`() { + val processedContent = processor.processContent(TestContent.oldPostWithMixedGalleriesOriginal) + Assertions.assertThat(processedContent).isEqualTo(TestContent.newPostWithMixedGalleriesOriginal) + } + + @Test + fun `processPost can handle refactored galleries with original galleries present`() { + val processedContent = processor.processContent(TestContent.oldPostWithMixedGalleriesRefactored) + Assertions.assertThat(processedContent).isEqualTo(TestContent.newPostWithMixedGalleriesRefactored) + } } From 411196a794155b87eb47274dd7547a56837db597 Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Tue, 1 Jun 2021 20:42:57 +1000 Subject: [PATCH 4/7] Implement processor for refactored gallery format This detects the new format via the presence of the `imageCount` property in the block's json and defers processing to the image block processor (recursively through the `processContent` method). --- .../BlockProcessorFactory.java | 3 +- .../GalleryBlockProcessor.java | 38 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/BlockProcessorFactory.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/BlockProcessorFactory.java index 4258b5a3ee48..f4e9058f87c9 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/BlockProcessorFactory.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/BlockProcessorFactory.java @@ -36,7 +36,8 @@ BlockProcessorFactory init(String localId, MediaFile mediaFile, String siteUrl) mMediaBlockTypeBlockProcessorMap.put(IMAGE, new ImageBlockProcessor(localId, mediaFile)); mMediaBlockTypeBlockProcessorMap.put(VIDEO, new VideoBlockProcessor(localId, mediaFile)); mMediaBlockTypeBlockProcessorMap.put(MEDIA_TEXT, new MediaTextBlockProcessor(localId, mediaFile)); - mMediaBlockTypeBlockProcessorMap.put(GALLERY, new GalleryBlockProcessor(localId, mediaFile, siteUrl)); + mMediaBlockTypeBlockProcessorMap.put(GALLERY, new GalleryBlockProcessor(localId, mediaFile, siteUrl, + mMediaUploadCompletionProcessor)); mMediaBlockTypeBlockProcessorMap.put(COVER, new CoverBlockProcessor(localId, mediaFile, mMediaUploadCompletionProcessor)); mMediaBlockTypeBlockProcessorMap.put(FILE, new FileBlockProcessor(localId, mediaFile)); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/GalleryBlockProcessor.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/GalleryBlockProcessor.java index 44425bec409c..81fb0f171377 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/GalleryBlockProcessor.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/mediauploadcompletionprocessors/GalleryBlockProcessor.java @@ -9,7 +9,11 @@ import org.jsoup.nodes.Element; import org.wordpress.android.util.helpers.MediaFile; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class GalleryBlockProcessor extends BlockProcessor { + private final MediaUploadCompletionProcessor mMediaUploadCompletionProcessor; private String mAttachmentPageUrl; private String mLinkTo; @@ -18,8 +22,18 @@ public class GalleryBlockProcessor extends BlockProcessor { */ private String mGalleryImageQuerySelector; - public GalleryBlockProcessor(String localId, MediaFile mediaFile, String siteUrl) { + /** + * Template pattern used to match and splice inner image blocks in the refactored gallery format + */ + private static final Pattern PATTERN_GALLERY_INNER = Pattern.compile(new StringBuilder() + .append("(^.*?
\\s*)") + .append("(.*)") // inner block contents + .append("(\\s*
\\s*.*)").toString(), Pattern.DOTALL); + + public GalleryBlockProcessor(String localId, MediaFile mediaFile, String siteUrl, MediaUploadCompletionProcessor + mediaUploadCompletionProcessor) { super(localId, mediaFile); + mMediaUploadCompletionProcessor = mediaUploadCompletionProcessor; mGalleryImageQuerySelector = new StringBuilder() .append("img[data-id=\"") .append(localId) @@ -67,6 +81,11 @@ public GalleryBlockProcessor(String localId, MediaFile mediaFile, String siteUrl } @Override boolean processBlockJsonAttributes(JsonObject jsonAttributes) { + // The presence of `imageCount` means we have the new format, so return early to defer to recursive processing + if (jsonAttributes.has("imageCount")) { + return false; + } + JsonArray ids = jsonAttributes.getAsJsonArray("ids"); if (ids == null || ids.isJsonNull()) { return false; @@ -84,4 +103,21 @@ public GalleryBlockProcessor(String localId, MediaFile mediaFile, String siteUrl } return false; } + + @Override String processInnerBlock(String block) { + Matcher innerMatcher = PATTERN_GALLERY_INNER.matcher(block); + boolean innerCapturesFound = innerMatcher.find(); + + // process inner contents recursively + if (innerCapturesFound) { + String innerProcessed = mMediaUploadCompletionProcessor.processContent(innerMatcher.group(2)); // + return new StringBuilder() + .append(innerMatcher.group(1)) + .append(innerProcessed) + .append(innerMatcher.group(3)) + .toString(); + } + + return block; + } } From 53dc97a59b3881d340038b71015b1a731a7c746c Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Thu, 3 Jun 2021 17:56:29 +1000 Subject: [PATCH 5/7] Update gutenberg-mobile reference --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 23c3b7fe8eea..9c1d52d8b458 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { ext.kotlin_ktx_version = '1.2.0' ext.wordPressUtilsVersion = 'develop-bb54ee34c5fec5fa7375ce90a356adb5adbdcae0' ext.detektVersion = '1.15.0' - ext.gutenbergMobileVersion = 'v1.54.0' + ext.gutenbergMobileVersion = '3579-68087ae8070536a4ab4354787b4f103cb191dc4f' repositories { google() From 88260809afe3c58413d0fddd1a23b310291cbd65 Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Fri, 4 Jun 2021 15:06:19 +1000 Subject: [PATCH 6/7] Update gutenberg-mobile reference --- build.gradle | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9c1d52d8b458..fbd60384bba9 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,8 @@ buildscript { ext.kotlin_ktx_version = '1.2.0' ext.wordPressUtilsVersion = 'develop-bb54ee34c5fec5fa7375ce90a356adb5adbdcae0' ext.detektVersion = '1.15.0' - ext.gutenbergMobileVersion = '3579-68087ae8070536a4ab4354787b4f103cb191dc4f' + ext.gutenbergMobileVersion = '3579-c4f1c84c139e69ece449997cb129d1371781e7ea' + repositories { google() From a0383a7daa52a43f0aab007851d683b04cbb4d3b Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Wed, 16 Jun 2021 14:00:24 +1000 Subject: [PATCH 7/7] Update gutenberg-mobile reference --- build.gradle | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index fbd60384bba9..102167a82b49 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,8 @@ buildscript { ext.kotlin_ktx_version = '1.2.0' ext.wordPressUtilsVersion = 'develop-bb54ee34c5fec5fa7375ce90a356adb5adbdcae0' ext.detektVersion = '1.15.0' - ext.gutenbergMobileVersion = '3579-c4f1c84c139e69ece449997cb129d1371781e7ea' + ext.gutenbergMobileVersion = '3579-0fb5bf9c5fbfb263cec9325f5b0622138a076544' + repositories {