Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ public static String replaceMediaFileWithUrlInGutenbergPost(@NonNull String post
public static boolean isMediaInGutenbergPostBody(@NonNull String postContent,
String localMediaId) {
List<String> patterns = new ArrayList<>();
// Regex for Image, Video, and File blocks
patterns.add("<!-- wp:(?:image|video|file){1} \\{[^\\}]*\"id\":%s([^\\d\\}][^\\}]*)*\\} -->");
// Regex for Image, Video, Audio and File blocks
patterns.add("<!-- wp:(?:image|video|audio|file){1} \\{[^\\}]*\"id\":%s([^\\d\\}][^\\}]*)*\\} -->");
// Regex for Media&Text block
patterns.add("<!-- wp:media-text \\{[^\\}]*\"mediaId\":%s([^\\d\\}][^\\}]*)*\\} -->");
// Regex for Gallery block
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.wordpress.android.ui.posts.mediauploadcompletionprocessors

import com.google.gson.JsonObject
import org.jsoup.nodes.Document
import org.wordpress.android.util.helpers.MediaFile

class AudioBlockProcessor(localId: String?, mediaFile: MediaFile?) : BlockProcessor(localId, mediaFile) {
override fun processBlockContentDocument(document: Document?): Boolean {
val audioElements = document?.select(AUDIO_TAG)

audioElements?.let { elements ->
for (element in elements) {
// replaces the src attribute's local url with the remote counterpart.
element.attr(SRC_ATTRIBUTE, mRemoteUrl)
}
return true
}
return false
}

override fun processBlockJsonAttributes(jsonAttributes: JsonObject?): Boolean {
val id = jsonAttributes?.get(ID_ATTRIBUTE)

return if (id != null && !id.isJsonNull && id.asString == mLocalId) {
jsonAttributes.apply {
addProperty(ID_ATTRIBUTE, Integer.parseInt(mRemoteId))
}
true
} else {
false
}
}

companion object {
const val AUDIO_TAG = "audio"
const val SRC_ATTRIBUTE = "src"
const val ID_ATTRIBUTE = "id"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.wordpress.android.ui.posts.mediauploadcompletionprocessors.MediaBlockType.AUDIO;
import static org.wordpress.android.ui.posts.mediauploadcompletionprocessors.MediaBlockType.COVER;
import static org.wordpress.android.ui.posts.mediauploadcompletionprocessors.MediaBlockType.FILE;
import static org.wordpress.android.ui.posts.mediauploadcompletionprocessors.MediaBlockType.GALLERY;
Expand Down Expand Up @@ -39,6 +40,7 @@ BlockProcessorFactory init(String localId, MediaFile mediaFile, String siteUrl)
mMediaBlockTypeBlockProcessorMap.put(COVER, new CoverBlockProcessor(localId, mediaFile,
mMediaUploadCompletionProcessor));
mMediaBlockTypeBlockProcessorMap.put(FILE, new FileBlockProcessor(localId, mediaFile));
mMediaBlockTypeBlockProcessorMap.put(AUDIO, new AudioBlockProcessor(localId, mediaFile));

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ enum MediaBlockType {
MEDIA_TEXT("media-text"),
GALLERY("gallery"),
COVER("cover"),
FILE("file");
FILE("file"),
AUDIO("audio");

private static final Map<String, MediaBlockType> MAP = new HashMap<>();
private static final String MATCHING_GROUP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ class PostUtilsUnitTest {
assertThat(PostUtils.isMediaInGutenbergPostBody(postContent, mediaId)).isTrue()
}

@Test
fun `isMediaInGutenberg returns true when an Audio Block is found in the post content`() {
val mediaId = "999"
val postContent = "<!-- wp:audio {\"id\":$mediaId} --> ...... <!-- /wp:audio -->"
assertThat(PostUtils.isMediaInGutenbergPostBody(postContent, mediaId)).isTrue()
}

@Test
fun `isMediaInGutenberg returns false when an imageBlock with provided id is NOT found in the post content`() {
val imgId = "123"
Expand Down Expand Up @@ -158,6 +165,13 @@ class PostUtilsUnitTest {
assertThat(PostUtils.isMediaInGutenbergPostBody(postContent, "999")).isFalse()
}

@Test
fun `isMediaInGutenberg returns false when an Audio Block with provided id is NOT found in the post content`() {
val mediaId = "123"
val postContent = "<!-- wp:audio {\"id\":$mediaId} --> ...... <!-- /wp:audio -->"
assertThat(PostUtils.isMediaInGutenbergPostBody(postContent, "999")).isFalse()
}

@Test
fun `isMediaInGutenberg returns false for an imageBlock when only part of the id matches`() {
val imgId = "12345"
Expand Down Expand Up @@ -186,6 +200,13 @@ class PostUtilsUnitTest {
assertThat(PostUtils.isMediaInGutenbergPostBody(postContent, "123")).isFalse()
}

@Test
fun `isMediaInGutenberg returns false for an Audio Block when only part of the id matches`() {
val mediaId = "12345"
val postContent = "<!-- wp:audio {\"id\":$mediaId} --> ...... <!-- /wp:audio -->"
assertThat(PostUtils.isMediaInGutenbergPostBody(postContent, "123")).isFalse()
}

@Test
fun `isMediaInGutenberg returns false when a galleryBlock with provided id is NOT found in the post content`() {
val imgId = "123"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.wordpress.android.ui.posts.mediauploadcompletionprocessors

import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import org.assertj.core.api.Assertions
import org.junit.Before
import org.junit.Test
import org.wordpress.android.util.helpers.MediaFile

class AudioBlockProcessorTest {
private val mediaFile: MediaFile = mock()
private lateinit var processor: AudioBlockProcessor

@Before
fun before() {
whenever(mediaFile.mediaId).thenReturn(TestContent.remoteMediaId)
whenever(mediaFile.fileURL).thenReturn(TestContent.remoteAudioUrl)
processor = AudioBlockProcessor(TestContent.localMediaId, mediaFile)
}

@Test
fun `processBlock replaces id and src in matching block`() {
val processedBlock = processor.processBlock(TestContent.oldAudioBlock)
Assertions.assertThat(processedBlock).isEqualTo(TestContent.newAudioBlock)
}

@Test
fun `processBlock leaves non-matching block unchanged`() {
val nonMatchingId = "123"
val processor = AudioBlockProcessor(nonMatchingId, mediaFile)
val processedBlock = processor.processBlock(TestContent.oldFileBlock)
Assertions.assertThat(processedBlock).isEqualTo(TestContent.oldFileBlock)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ object TestContent {
const val siteUrl = "https://wordpress.org"
const val localImageUrl = "file://Screenshot-1-1.png"
const val localImageUrl2 = "file://Screenshot-1-2.png"
const val localAudioUrl = "file://test-song.mp3"
const val remoteImageUrl = "https://onetwoonetwothisisjustatesthome.files.wordpress.com/2019/11/pexels-photo-1671668.jpg"
const val remoteImageUrl2 = "https://onetwoonetwothisisjustatesthome.files.wordpress.com/2019/12/img_20191202_094944-19.jpg"
const val remoteAudioUrl = "https://onetwoonetwothisisjustatesthome.files.wordpress.com/2020/11/test-song.mp3"
private const val remoteImageUrlBlogLink = "http://onetwoonetwothisisjustatest.home.blog/pexels-photo-1671668/"
private const val remoteImageUrlWithSize = "https://onetwoonetwothisisjustatesthome.files.wordpress.com/2019/11/pexels-photo-1671668.jpg?w=1024"
private const val remoteImageUrl2BlogLink = "http://onetwoonetwothisisjustatest.home.blog/?attachment_id=369"
Expand Down Expand Up @@ -571,6 +573,13 @@ object TestContent {
<div class="wp-block-file"><a href="$remoteImageUrl">Test image</a><a href="$remoteImageUrl" class="wp-block-file__button" download>Download</a></div>
<!-- /wp:file -->
"""
const val oldAudioBlock = """<!-- wp:audio {"id":${localMediaId}} -->
<figure class="wp-block-audio"><audio controls src="$localAudioUrl"></audio></figure>
<!-- /wp:audio -->"""

const val newAudioBlock = """<!-- wp:audio {"id":${remoteMediaId}} -->
<figure class="wp-block-audio"><audio controls src="$remoteAudioUrl"></audio></figure>
<!-- /wp:audio -->"""

const val oldPostImage = paragraphBlock + oldImageBlock + newVideoBlock + newMediaTextBlock + newGalleryBlock
const val newPostImage = paragraphBlock + newImageBlock + newVideoBlock + newMediaTextBlock + newGalleryBlock
Expand Down