Fixes #4942 Change category extraction algorithm (#4943)

* updated addCategory()method

* update addCategory()method

* add unit test for none category

* add comments to addCategory()method

* update comments

* update comments for addCategory()method
This commit is contained in:
HCH 2022-04-28 22:21:45 +08:00 committed by GitHub
parent 79086fe942
commit b2a901b9b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 7 deletions

View file

@ -62,16 +62,27 @@ public class CategoryEditHelper {
final String wikiText) {
Timber.d("thread is category adding %s", Thread.currentThread().getName());
String summary = "Adding categories";
final StringBuilder buffer = new StringBuilder();
final String wikiTextWithoutCategory
= wikiText.substring(0, wikiText.indexOf("[[Category"));
if (categories != null && categories.size() != 0) {
final String wikiTextWithoutCategory;
//If the picture was uploaded without a category, the wikitext will contain "Uncategorized" instead of "[[Category"
if (wikiText.contains("Uncategorized")) {
wikiTextWithoutCategory = wikiText.substring(0, wikiText.indexOf("Uncategorized"));
} else if (wikiText.contains("[[Category")) {
wikiTextWithoutCategory = wikiText.substring(0, wikiText.indexOf("[[Category"));
} else {
wikiTextWithoutCategory = "";
}
if (categories != null && !categories.isEmpty()) {
//If the categories list is empty, when reading the categories of a picture,
// the code will add "None selected" to categories list in order to see in picture's categories with "None selected".
// So that after selected some category,"None selected" should be removed from list
for (int i = 0; i < categories.size(); i++) {
buffer.append("[[Category:").append(categories.get(i)).append("]]\n");
if (!categories.get(i).equals("None selected")//Not to add "None selected" as category to wikiText
|| !wikiText.contains("Uncategorized")) {
buffer.append("[[Category:").append(categories.get(i)).append("]]\n");
}
}
categories.remove("None selected");
} else {
buffer.append("{{subst:unc}}");
}

View file

@ -80,4 +80,32 @@ class CategoryEditHelperUnitTests {
ArgumentMatchers.anyString()
)
}
@Test
@Throws(Exception::class)
fun testMakeUnCategoryEdit() {
helper.makeCategoryEdit(context, media, listOf("Test"), "== {{int:filedesc}} ==\n" +
"{{Information\n" +
"|description=\n" +
"\n" +
"|source={{own}}\n" +
"|author=[[User:UserForthBeta|UserForthBeta]]\n" +
"|date={{According to Exif data|2022-03-06}}\n" +
"}}\n" +
"{{Location|22.538830555555556|114.08847222222222}}\n" +
"== {{int:license-header}} ==\n" +
"{{self|cc-zero}}\n" +
"\n" +
"{{Uploaded from Mobile|platform=Android|version=3.1.1-debug-master~e7a9ba9ad}}\n" +
"{{Uncategorized|year=2022|month=April|day=23}}")
Mockito.verify(viewUtilWrapper, Mockito.times(1)).showShortToast(
context,
context.getString(R.string.category_edit_helper_make_edit_toast)
)
Mockito.verify(pageEditClient, Mockito.times(1)).edit(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString()
)
}
}