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 @@ -160,7 +160,11 @@ abstract class BaseWPV2MediaRestClient constructor(
if (response.isSuccessful) {
try {
val res = gson.fromJson(response.body!!.string(), MediaWPRESTResponse::class.java)
val uploadedMedia = res.toMediaModel(site.id)
val uploadedMedia = res.toMediaModel(site.id).apply {
id = media.id
localPostId = media.localPostId
markedLocallyAsFeatured = media.markedLocallyAsFeatured
}
val payload = ProgressPayload(uploadedMedia, 1f, true, false)
try {
trySendBlocking(payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class MediaRSApiRestClient @Inject constructor(
dispatcher.dispatch(MediaActionBuilder.newDeletedMediaAction(payload))
}

@Suppress("LongMethod")
fun uploadMedia(site: SiteModel, media: MediaModel?) {
if (media == null || media.id == 0) {
// we can't have a MediaModel without an ID - otherwise we can't keep track of them.
Expand Down Expand Up @@ -277,6 +278,8 @@ class MediaRSApiRestClient @Inject constructor(
val responseMedia: MediaModel = mediaResponse.response.data.toMediaModel(site.id).apply {
id = media.id // be sure we are using the same local id when getting the remote response
localSiteId = site.id
localPostId = media.localPostId
markedLocallyAsFeatured = media.markedLocallyAsFeatured
}
notifyMediaUploaded(
media = responseMedia,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,39 @@ class MediaRsApiRestClientTest {
assertNull(payload.error)
}

@Test
fun `uploadMedia preserves local metadata from original media model`() = runTest {
val testSite = createTestSite()
val localPostId = 42
val testMedia = createTestMedia().apply {
filePath = "/valid/path/file.jpg"
this.localPostId = localPostId
markedLocallyAsFeatured = true
}
val mediaWithEditContext = createTestMediaWithEditContext(testMedia.id.toLong())
val mediaRequestResult: WpRequestResult<MediaRequestCreateResponse> =
WpRequestResult.Success(
response = MediaRequestCreateResponse(
mediaWithEditContext, mock<WpNetworkHeaderMap>()
)
)

whenever(fileCheckWrapper.canReadFile(any())).thenReturn(true)
whenever(wpApiClient.request<MediaRequestCreateResponse>(any()))
.thenReturn(mediaRequestResult)

restClient.uploadMedia(testSite, testMedia)

val actionCaptor = ArgumentCaptor.forClass(Action::class.java)
verify(dispatcher).dispatch(actionCaptor.capture())

val capturedAction = actionCaptor.value
val payload = capturedAction.payload as ProgressPayload
assertEquals(testMedia.id, payload.media?.id)
assertEquals(localPostId, payload.media?.localPostId)
assertTrue(payload.media?.markedLocallyAsFeatured == true)
}

@Test
fun `uploadMedia with error response dispatches error action`() = runTest {
val testSite = createTestSite()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,45 @@ class WPComV2MediaRestClientTest {
}
}

@Test
fun `upload preserves local metadata from original media model`() {
createFileThenRunTest {
whenever(okHttpClient.newCall(any())).thenReturn(mockedCall)
whenever(mockedCall.enqueue(any())).then {
(it.arguments.first() as Callback).onResponse(
mockedCall,
mock {
on { body } doReturn UnitTestUtils.getStringFromResourceFile(
this::class.java,
"media/media-upload-wp-api-success.json"
).toResponseBody("application/json".toMediaType())
on { isSuccessful } doReturn true
}
)
countDownLatch.countDown()
}

val media = MediaTestUtils.generateMediaFromPath(0, 0L, "./image.jpg").apply {
localPostId = 42
markedLocallyAsFeatured = true
}

countDownLatch = CountDownLatch(1)
restClient.uploadMedia(SiteModel(), media)

countDownLatch.await()

verify(dispatcher).dispatch(argThat {
val payload = payload as ProgressPayload
type == UPLOADED_MEDIA
&& payload.completed
&& payload.media?.id == media.id
&& payload.media?.localPostId == 42
&& payload.media?.markedLocallyAsFeatured == true
})
}
}

@Test
fun `emit failure action when upload fails`() {
createFileThenRunTest {
Expand Down
Loading