Skip to content

Commit

Permalink
Merge pull request #14460 from wordpress-mobile/isssue/13678-iob-empt…
Browse files Browse the repository at this point in the history
…y-story-block

Stories: add bounds check in saving, don't process an empty Story block
  • Loading branch information
mzorz authored Apr 15, 2021
2 parents bf68458 + 7ec080a commit 5ecc625
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,19 @@ class SaveStoryGutenbergBlockUseCase @Inject constructor(
// --> remove mediaFiles entirely
// set start index and go up.
var storyBlockStartIndex = 0
while (storyBlockStartIndex > -1 && storyBlockStartIndex < content.length) {
var hasMediaFiles = true
while (storyBlockStartIndex > -1 && storyBlockStartIndex < content.length && hasMediaFiles) {
storyBlockStartIndex = content.indexOf(HEADING_START, storyBlockStartIndex)
if (storyBlockStartIndex > -1) {
val storyBlockEndIndex = content.indexOf(HEADING_END_NO_NEW_LINE, storyBlockStartIndex)
val mediaFilesStartIndex = storyBlockStartIndex + HEADING_START.length
hasMediaFiles = mediaFilesStartIndex < storyBlockEndIndex
if (!hasMediaFiles) {
break
}
try {
val jsonString: String = content.substring(
storyBlockStartIndex + HEADING_START.length,
mediaFilesStartIndex,
storyBlockEndIndex
)
content = listener.doWithMediaFilesJson(content, jsonString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.wordpress.android.fluxc.store.PostStore
import org.wordpress.android.ui.posts.EditPostRepository
import org.wordpress.android.ui.posts.mediauploadcompletionprocessors.TestContent
import org.wordpress.android.ui.stories.SaveStoryGutenbergBlockUseCase.Companion.TEMPORARY_ID_PREFIX
import org.wordpress.android.ui.stories.SaveStoryGutenbergBlockUseCase.DoWithMediaFilesListener
import org.wordpress.android.ui.stories.SaveStoryGutenbergBlockUseCase.StoryMediaFileData
import org.wordpress.android.ui.stories.prefs.StoriesPrefs
import org.wordpress.android.util.CrashLogging
Expand Down Expand Up @@ -217,6 +218,25 @@ class SaveStoryGutenbergBlockUseCaseTest : BaseUnitTest() {
Assertions.assertThat(postModel.content).isEqualTo(TestContent.storyBlockWithLocalIdsAndUrls)
}

@Test
fun `post with a Story block with no mediaFiles is not taken into account for processing`() {
// Given
val siteModel = SiteModel()
val postModel = PostModel()
val listener: DoWithMediaFilesListener = mock()
postModel.setContent(BLOCK_LACKING_MEDIA_FILES_ARRAY)

// When
saveStoryGutenbergBlockUseCase.findAllStoryBlocksInPostAndPerformOnEachMediaFilesJson(
postModel,
siteModel,
mock()
)

// Then
verify(listener, times(0)).doWithMediaFilesJson(any(), any())
}

private fun setupFluxCMediaFiles(
emptyList: Boolean
): ArrayList<MediaFile> {
Expand Down Expand Up @@ -280,6 +300,9 @@ class SaveStoryGutenbergBlockUseCaseTest : BaseUnitTest() {
}

companion object {
private const val BLOCK_LACKING_MEDIA_FILES_ARRAY = "<!-- wp:jetpack/story -->\n" +
"<div class=\"wp-story wp-block-jetpack-story\"></div>\n" +
"<!-- /wp:jetpack/story -->"
private const val BLOCK_WITH_EMPTY_MEDIA_FILES = "<!-- wp:jetpack/story {\"mediaFiles\":[]} -->\n" +
"<div class=\"wp-story wp-block-jetpack-story\"></div>\n" +
"<!-- /wp:jetpack/story -->"
Expand Down

0 comments on commit 5ecc625

Please sign in to comment.