Fixes #3464, App posts deletion request notifications ({{subst:idw}}) on the wrong user's talk page (#3495)

* Fixed #3464: App posts deletion request notifications ({{subst:idw}}) on the wrong user's talk page

* Fixed DeleteHelperTest.kt

* Fixed DeleteHelper tests and null-pointer exception

* Modified DeleteHelper makeDeletion() test

* Reverted unintentionally modified Project.xml

* Raising exception when nominating for deletion with empty creator name

* Fixed code style

* Fixed code style
This commit is contained in:
Vitaly V. Pinchuk 2020-03-17 14:39:28 +03:00 committed by GitHub
parent cd2d530175
commit e55b5495a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View file

@ -98,6 +98,12 @@ public class DeleteHelper {
String userPageString = "\n{{subst:idw|" + media.getFilename() +
"}} ~~~~";
String creator = media.getCreator();
if (creator == null || creator.isEmpty()) {
throw new RuntimeException("Failed to nominate for deletion");
}
String creatorName = creator.replace(" (page does not exist)", "");
return pageEditClient.prependEdit(media.filename, fileDeleteString + "\n", summary)
.flatMap(result -> {
if (result) {
@ -111,7 +117,7 @@ public class DeleteHelper {
throw new RuntimeException("Failed to nominate for deletion");
}).flatMap(result -> {
if (result) {
return pageEditClient.appendEdit("User_Talk:" + username, userPageString + "\n", summary);
return pageEditClient.appendEdit("User_Talk:" + creatorName, userPageString + "\n", summary);
}
throw new RuntimeException("Failed to nominate for deletion");
});

View file

@ -1,6 +1,8 @@
package fr.free.nrw.commons.delete
import android.content.Context
import com.nhaarman.mockitokotlin2.eq
import com.nhaarman.mockitokotlin2.verify
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.actions.PageEditClient
import fr.free.nrw.commons.notification.NotificationHelper
@ -14,7 +16,6 @@ import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.wikipedia.AppAdapter
import javax.inject.Inject
import javax.inject.Named
@ -65,9 +66,13 @@ class DeleteHelperTest {
`when`(media?.displayTitle).thenReturn("Test file")
media?.filename="Test file.jpg"
val creatorName = "Creator"
`when`(media?.getCreator()).thenReturn("$creatorName (page does not exist)")
val makeDeletion = deleteHelper?.makeDeletion(context, media, "Test reason")?.blockingGet()
assertNotNull(makeDeletion)
assertTrue(makeDeletion!!)
verify(pageEditClient)?.appendEdit(eq("User_Talk:$creatorName"), ArgumentMatchers.anyString(), ArgumentMatchers.anyString())
}
/**
@ -83,6 +88,7 @@ class DeleteHelperTest {
.thenReturn(Observable.just(true))
`when`(media?.displayTitle).thenReturn("Test file")
`when`(media?.filename).thenReturn("Test file.jpg")
`when`(media?.creator).thenReturn("Creator (page does not exist)")
deleteHelper?.makeDeletion(context, media, "Test reason")?.blockingGet()
}
@ -97,6 +103,7 @@ class DeleteHelperTest {
.thenReturn(Observable.just(false))
`when`(media?.displayTitle).thenReturn("Test file")
`when`(media?.filename).thenReturn("Test file.jpg")
`when`(media?.creator).thenReturn("Creator (page does not exist)")
deleteHelper?.makeDeletion(context, media, "Test reason")?.blockingGet()
}
@ -111,6 +118,24 @@ class DeleteHelperTest {
.thenReturn(Observable.just(true))
`when`(media?.displayTitle).thenReturn("Test file")
`when`(media?.filename).thenReturn("Test file.jpg")
`when`(media?.creator).thenReturn("Creator (page does not exist)")
deleteHelper?.makeDeletion(context, media, "Test reason")?.blockingGet()
}
@Test(expected = RuntimeException::class)
fun makeDeletionForEmptyCreatorName() {
`when`(pageEditClient?.prependEdit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(true))
`when`(pageEditClient?.appendEdit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(true))
`when`(pageEditClient?.edit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(true))
`when`(media?.displayTitle).thenReturn("Test file")
media?.filename="Test file.jpg"
`when`(media?.getCreator()).thenReturn(null)
deleteHelper?.makeDeletion(context, media, "Test reason")?.blockingGet()
}