Skip to content

Commit

Permalink
Merge pull request #6542 from seadowg/v2024.3.3-changes
Browse files Browse the repository at this point in the history
Changes for v2024.3.3
  • Loading branch information
lognaturel authored Dec 10, 2024
2 parents f0ce2a6 + 2ccb27f commit d3c5c67
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 36 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.odk.collect.androidshared.utils
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.Test
import org.odk.collect.shared.PathUtils
import org.odk.collect.shared.TempFiles
import java.io.File

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.database.Cursor
import android.provider.BaseColumns
import org.odk.collect.android.database.forms.DatabaseFormColumns
import org.odk.collect.android.database.instances.DatabaseInstanceColumns
import org.odk.collect.androidshared.utils.PathUtils.getAbsoluteFilePath
import org.odk.collect.forms.Form
import org.odk.collect.forms.instances.Instance
import org.odk.collect.shared.PathUtils.getAbsoluteFilePath
import org.odk.collect.shared.PathUtils.getRelativeFilePath
import java.lang.Boolean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep

val missingColumns = entity.properties
.map { EntitiesTable.getPropertyColumn(it.first) }
.filterNot { columnNames.contains(it) }
.distinctBy { it.lowercase() }
.filterNot { columnName -> columnNames.any { it.equals(columnName, ignoreCase = true) } }

if (missingColumns.isNotEmpty()) {
databaseConnection.resetTransaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import org.odk.collect.android.database.DatabaseConstants.SAVEPOINTS_DATABASE_VE
import org.odk.collect.android.database.DatabaseConstants.SAVEPOINTS_TABLE_NAME
import org.odk.collect.android.database.savepoints.DatabaseSavepointsColumns.FORM_DB_ID
import org.odk.collect.android.database.savepoints.DatabaseSavepointsColumns.INSTANCE_DB_ID
import org.odk.collect.androidshared.utils.PathUtils.getAbsoluteFilePath
import org.odk.collect.db.sqlite.CursorExt.foldAndClose
import org.odk.collect.db.sqlite.DatabaseConnection
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.delete
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.query
import org.odk.collect.forms.savepoints.Savepoint
import org.odk.collect.forms.savepoints.SavepointsRepository
import org.odk.collect.shared.PathUtils
import org.odk.collect.shared.PathUtils.getAbsoluteFilePath
import java.io.File

class DatabaseSavepointsRepository(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.odk.collect.android.fastexternalitemset;

import static org.odk.collect.androidshared.utils.PathUtils.getAbsoluteFilePath;
import static org.odk.collect.shared.PathUtils.getAbsoluteFilePath;

import android.content.ContentValues;
import android.database.Cursor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ abstract class EntitiesRepositoryTest {
}

@Test
fun `#save ignores case-insensitive duplicate properties`() {
fun `#save ignores case-insensitive duplicate new properties`() {
val repository = buildSubject()
val entity = Entity.New(
"1",
Expand All @@ -749,4 +749,24 @@ abstract class EntitiesRepositoryTest {
assertThat(savedEntities[0].properties.size, equalTo(1))
assertThat(savedEntities[0].properties[0].first, equalTo("prop"))
}

@Test
fun `#save ignores case-insensitive duplicate properties if one of them has already been saved`() {
val repository = buildSubject()
val entity = Entity.New(
"1",
"One",
properties = listOf(Pair("prop", "value"))
)

repository.save("things", entity)
var savedEntities = repository.getEntities("things")
assertThat(savedEntities[0].properties.size, equalTo(1))
assertThat(savedEntities[0].properties[0].first, equalTo("prop"))

repository.save("things", entity.copy(properties = listOf(Pair("Prop", "value"))))
savedEntities = repository.getEntities("things")
assertThat(savedEntities[0].properties.size, equalTo(1))
assertThat(savedEntities[0].properties[0].first, equalTo("prop"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.utilities.FileUtils.read;
import static org.odk.collect.androidshared.utils.PathUtils.getAbsoluteFilePath;
import static org.odk.collect.formstest.FormUtils.buildForm;
import static org.odk.collect.formstest.FormUtils.createXFormBody;
import static org.odk.collect.shared.PathUtils.getAbsoluteFilePath;
import static java.util.Arrays.asList;

import com.google.common.io.Files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,15 @@ class InMemEntitiesRepository : EntitiesRepository {

private fun updateLists(list: String, entity: Entity) {
lists.add(list)
listProperties.getOrPut(list) {
val properties = listProperties.getOrPut(list) {
mutableSetOf()
}.addAll(
}
properties.addAll(
entity
.properties
.distinctBy { it.first.lowercase() }
.map { it.first }
.distinctBy { it.lowercase() }
.filterNot { properties.any { property -> property.equals(it, ignoreCase = true) } }
)
}

Expand Down
15 changes: 15 additions & 0 deletions shared/src/main/java/org/odk/collect/shared/PathUtils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.odk.collect.shared

import org.odk.collect.shared.files.FileExt.sanitizedCanonicalPath
import java.io.File

object PathUtils {

@JvmStatic
Expand All @@ -10,4 +13,16 @@ object PathUtils {
// https://stackoverflow.com/questions/2679699/what-characters-allowed-in-file-names-on-android
@JvmStatic
fun getPathSafeFileName(fileName: String) = fileName.replace("[\"*/:<>?\\\\|]".toRegex(), "_")

@JvmStatic
fun getAbsoluteFilePath(dirPath: String, filePath: String): String {
val absoluteFilePath =
if (filePath.startsWith(dirPath)) filePath else dirPath + File.separator + filePath

if (File(absoluteFilePath).sanitizedCanonicalPath().startsWith(File(dirPath).sanitizedCanonicalPath())) {
return absoluteFilePath
} else {
throw SecurityException("Contact [email protected]. Attempt to access file outside of Collect directory: $absoluteFilePath")
}
}
}

0 comments on commit d3c5c67

Please sign in to comment.