@@ -57,14 +60,14 @@ function printTranslationsTable($translations, $type, $server_type) {
echo "
- ";
for ($col_index = 1; $col_index <= $column_count; $col_index++) {
- $name = trim($row[$col_index-1]);
+ $name = trim($row[$col_index - 1]);
$name = str_replace("\\", "", $name);
echo " | ";
}
@@ -91,7 +94,7 @@ function printProjectCard($project_info) {
$translations = json_decode($project_info["$settings_prefix$type-translations"] ?? '[]');
printTranslationsTable($translations, $type, $project_info['server-type']);
}
- ?>
+ ?>
getUrl('js/config_translations.js') ?>'>
#api-sync-module-wrapper .top-button-container{
margin-top: 20px;
margin-bottom: 50px;
@@ -63,4 +62,3 @@
#api-sync-module-log-entries{
width: 100%;
}
-
diff --git a/delete-request-content-logs.php b/delete-request-content-logs.php
index cc4d339..b5d0325 100644
--- a/delete-request-content-logs.php
+++ b/delete-request-content-logs.php
@@ -1,4 +1,4 @@
-removeLogs('message in (?,?)', ['API Request', 'API Response']);
-echo 'success';
\ No newline at end of file
+removeLogs('message in (?,?)', ['API Request', 'API Response']);
+echo 'success';
diff --git a/export-all-records-now.php b/export-all-records-now.php
index 72fe052..9efe48a 100644
--- a/export-all-records-now.php
+++ b/export-all-records-now.php
@@ -1,3 +1,4 @@
setProjectSetting('export-all-records', true);
-require_once __DIR__ . '/export-now.php';
\ No newline at end of file
+require_once __DIR__ . '/export-now.php';
diff --git a/export-now.php b/export-now.php
index 2a1df19..e0c205d 100644
--- a/export-now.php
+++ b/export-now.php
@@ -2,4 +2,4 @@
$module->setProjectSetting('export-now', true);
-echo 'success';
\ No newline at end of file
+echo 'success';
diff --git a/get-log-details.php b/get-log-details.php
index 7252748..f10b918 100644
--- a/get-log-details.php
+++ b/get-log-details.php
@@ -1,21 +1,19 @@
-queryLogs('select details where log_id = ?', $logId);
-$row = $result->fetch_assoc();
-
-if($row === false){
- echo "Log id not found.";
-}
-else{
- $details = $row['details'];
-
- if(empty($details)){
- echo "No details were found.";
- }
- else{
- echo htmlentities($details, ENT_QUOTES);
- }
-}
\ No newline at end of file
+queryLogs('select details where log_id = ?', $logId);
+$row = $result->fetch_assoc();
+
+if ($row === false) {
+ echo "Log id not found.";
+} else {
+ $details = $row['details'];
+
+ if (empty($details)) {
+ echo "No details were found.";
+ } else {
+ echo htmlentities($details, ENT_QUOTES);
+ }
+}
diff --git a/get-log-digest.php b/get-log-digest.php
index 1614a26..ae835ac 100644
--- a/get-log-digest.php
+++ b/get-log-digest.php
@@ -1,12 +1,14 @@
requireDateParameter('start', 'Y-m-d');
$end = $module->requireDateParameter('end', 'Y-m-d');
-if(version_compare(REDCAP_VERSION, '10.8.2', '<')){
+if (version_compare(REDCAP_VERSION, '10.8.2', '<')) {
// This REDCap version does not support functions or comparisons in select log queries.
// Just always show the details button on older versions.
$hasDetailsClause = 1;
@@ -30,7 +32,7 @@
AND timestamp >= ? and timestamp < DATE_ADD(?, INTERVAL 1 DAY)
", [$this_module_id, $module->getProjectId(), $start, $end]);
-while($row = $results->fetch_assoc()){
+while ($row = $results->fetch_assoc()) {
$start_log_id = $row['max'];
$min_log_id = $row['min'];
$end_log_id = $start_log_id - LOG_WINDOW_SIZE;
@@ -47,7 +49,7 @@
// NOTE: probably don't need EM id or project_id
// NOTE: $message_subtrings can't be included as a ? parameter
$all_accounted = false;
-do {
+do {
$results = $module->queryLogs("
select log_id, timestamp, message, failure, $hasDetailsClause as hasDetails
where external_module_id = ? and project_id = ?
@@ -56,7 +58,7 @@
order by log_id desc
", [$this_module_id, $module->getProjectId(), $end_log_id, $start_log_id]);
- while($row = $results->fetch_assoc()){
+ while ($row = $results->fetch_assoc()) {
$digestLog->parseLogRow($row);
}
@@ -64,7 +66,9 @@
$start_log_id = $end_log_id;
$end_log_id = max($end_log_id - LOG_WINDOW_SIZE, $min_log_id);
- if ($start_log_id == $end_log_id) { $all_accounted = true; }
+ if ($start_log_id == $end_log_id) {
+ $all_accounted = true;
+ }
} while (!$all_accounted);
$min_url_statuses = $digestLog->getDigest();
diff --git a/get-logs.php b/get-logs.php
index bc9b14b..d5bc3a4 100644
--- a/get-logs.php
+++ b/get-logs.php
@@ -5,7 +5,7 @@
$end = date('Y-m-d', strtotime($end . ' + 1 day'));
$hasDetailsClause = "details != ''";
-if(version_compare(REDCAP_VERSION, '10.8.2', '<')){
+if (version_compare(REDCAP_VERSION, '10.8.2', '<')) {
// This REDCap version does not support functions or comparisons in select log queries.
// Just always show the details button on older versions.
$hasDetailsClause = 1;
@@ -29,14 +29,14 @@
];
$rows = [];
-while($row = $results->fetch_assoc()){
+while ($row = $results->fetch_assoc()) {
$escapedRow = [];
- foreach($row as $key=>$value){
+ foreach ($row as $key => $value) {
$escapedRow[$key] = htmlentities($value, ENT_QUOTES);
}
- foreach($allowedHtml as $s){
- $escapedRow['message'] = str_replace(htmlentities($s, ENT_QUOTES), html_entity_decode($s, ENT_QUOTES), $escapedRow['message']);
+ foreach ($allowedHtml as $s) {
+ $escapedRow['message'] = str_replace(htmlentities($s, ENT_QUOTES), $s, $escapedRow['message']);
}
$rows[] = $escapedRow;
@@ -46,4 +46,4 @@
{
"data": =json_encode($rows, JSON_PRETTY_PRINT)?>
-}
\ No newline at end of file
+}
diff --git a/log_digest.php b/log_digest.php
index 5ce9563..7600fd3 100644
--- a/log_digest.php
+++ b/log_digest.php
@@ -2,7 +2,7 @@
- $module->includeCss("css/pages.css"); ?>
+includeCss("css/pages.css"); ?>
=$module->initializeJavascriptModuleObject()?>
diff --git a/simulate-cron.php b/simulate-cron.php
index bf53584..ccaee3e 100644
--- a/simulate-cron.php
+++ b/simulate-cron.php
@@ -1,10 +1,11 @@
-cron([
- 'cron_name' => $type
-]);
\ No newline at end of file
+cron([
+ 'cron_name' => $type
+]);
diff --git a/sync-now.php b/sync-now.php
index 358cc8f..cac18a4 100644
--- a/sync-now.php
+++ b/sync-now.php
@@ -1,13 +1,14 @@
-isImportInProgress()){
- $result = $module->queryLogs("select progress where log_id = ?", [$retryLogId]);
- $row = $result->fetch_assoc();
-
- $module->log("Retrying last failed import from where it left off");
- $module->setImportProgress($row['progress']);
-}
-
-$module->setProjectSetting('sync-now', true);
-
-header('Location: ' . $module->getUrl('api-sync.php'));
+isImportInProgress()) {
+ $result = $module->queryLogs("select progress where log_id = ?", [$retryLogId]);
+ $row = $result->fetch_assoc();
+
+ $module->log("Retrying last failed import from where it left off");
+ $module->setImportProgress($row['progress']);
+}
+
+$module->setProjectSetting('sync-now', true);
+
+header('Location: ' . $module->getUrl('api-sync.php'));
diff --git a/tests/APISyncExternalModuleTest.php b/tests/APISyncExternalModuleTest.php
index d9a13ed..0ef17fb 100644
--- a/tests/APISyncExternalModuleTest.php
+++ b/tests/APISyncExternalModuleTest.php
@@ -1,149 +1,151 @@
-getChangedFieldNamesForLogRow(implode("\n", [
- "a = ''",
- "b(1) = checked",
- "c = 'one\ntwo'",
- "underscores_and_numbers123 = ''",
- ]), $allFieldNames);
-
- $this->assertSame($expected, $actual);
- }
-
- function testGetChangedFieldNamesForLogRow_dataValuesMaxLength(){
- $dataValues = '';
- while(strlen($dataValues) < APISyncExternalModule::DATA_VALUES_MAX_LENGTH){
- $dataValues .= ' ';
- }
-
- $actual = $this->getChangedFieldNamesForLogRow($dataValues, ['whatever']);
-
- $this->assertSame([], $actual);
- }
-
- function assertFilterByFieldList($typeAll, $fieldListAll, $type, $fieldList, $instance, $expected){
- $this->cacheProjectSetting('-field-list-type-all', $typeAll);
- $this->cacheProjectSetting('-field-list-all', $fieldListAll);
-
- $project = [
- "-field-list-type" => $type,
- '-field-list' => $fieldList
- ];
-
- $_GET['pid'] = ExternalModules::getTestPIDs()[0];
- $this->module->filterByFieldList($project, $instance);
-
- $this->assertSame($expected, $instance);
- }
-
- function testFilterByFieldList(){
- $assert = function($type, $fieldListNumbers, $expectedFieldNumbers){
- $assertFilterByFieldList = function($typeAll, $fieldListAll, $type, $fieldList, $expectedFields){
- $instance = [
- 'field1' => rand(),
- 'field2' => rand()
- ];
-
- $fieldNumbersToNames = function($numbers){
- if($numbers === null){
- $numbers = [];
- }
-
- $names = [];
- foreach($numbers as $number){
- $names[] = "field$number";
- }
-
- return $names;
- };
-
- $fieldListAll = $fieldNumbersToNames($fieldListAll);
- $fieldList = $fieldNumbersToNames($fieldList);
- $expectedFields = $fieldNumbersToNames($expectedFields);
-
- $expected = [];
- foreach($expectedFields as $expectedFieldName){
- if(isset($instance[$expectedFieldName])){
- $expected[$expectedFieldName] = $instance[$expectedFieldName];
- }
- }
-
- $this->assertFilterByFieldList($typeAll, $fieldListAll, $type, $fieldList, $instance, $expected);
- };
-
- $assertFilterByFieldList(null, null, $type, $fieldListNumbers, $expectedFieldNumbers);
- $assertFilterByFieldList($type, $fieldListNumbers, null, null, $expectedFieldNumbers);
-
- if(!empty($type)){
- // Make sure project settings take precedence
- $otherType = $type === 'include' ? 'exclude' : 'include';
- $assertFilterByFieldList($otherType, $fieldListNumbers, $type, $fieldListNumbers, $expectedFieldNumbers);
- }
- };
-
- $assert('include', [], []);
- $assert('include', [1], [1]);
- $assert('include', [1,2], [1,2]);
- $assert('include', [2,3], [2]);
- $assert('include', [3], []);
-
- $assert('exclude', [], [1,2]);
- $assert('exclude', [1], [2]);
- $assert('exclude', [1,3], [2]);
- $assert('exclude', [1,2], []);
- $assert('exclude', [3], [1, 2]);
-
- foreach([null, ''] as $emptyType){
- $assert($emptyType, [], [1,2]);
- $assert($emptyType, [1], [1,2]);
- $assert($emptyType, [3], [1,2]);
- }
- }
-
- function testFilterByFieldList_checkboxes(){
- $fieldName = 'some_checkbox';
- $fieldNameWithSuffix = "{$fieldName}___1";
- $otherFieldName = 'some_other_field';
- $instance = [
- $fieldNameWithSuffix => rand(),
- $otherFieldName => rand()
- ];
-
- $this->assertFilterByFieldList(null, null, 'exclude', [$fieldName], $instance, [
- $otherFieldName => $instance[$otherFieldName]
- ]);
-
- $this->assertFilterByFieldList(null, null, 'include', [$fieldName], $instance, [
- $fieldNameWithSuffix => $instance[$fieldNameWithSuffix]
- ]);
- }
-
- function testLogDetails(){
- $a = str_repeat('a', 65535);
- $b = str_repeat('b', 65535);
- $c = str_repeat('c', 65535);
-
- $logId = $this->logDetails('message', "$a$b$c");
-
- $result = $this->queryLogs('select details, details2, details3, details4 where log_id = ?', $logId);
- $actual = $result->fetch_assoc();
-
- $expected = [
- 'details' => $a,
- 'details2' => $b,
- 'details3' => $c,
- 'details4' => null,
- ];
-
- // Don't use assertSame() because the failure output is too large for the console.
- $this->assertTrue($expected === $actual);
- }
-}
\ No newline at end of file
+getChangedFieldNamesForLogRow(implode("\n", [
+ "a = ''",
+ "b(1) = checked",
+ "c = 'one\ntwo'",
+ "underscores_and_numbers123 = ''",
+ ]), $allFieldNames);
+
+ $this->assertSame($expected, $actual);
+ }
+
+ public function testGetChangedFieldNamesForLogRow_dataValuesMaxLength() {
+ $dataValues = '';
+ while (strlen($dataValues) < APISyncExternalModule::DATA_VALUES_MAX_LENGTH) {
+ $dataValues .= ' ';
+ }
+
+ $actual = $this->getChangedFieldNamesForLogRow($dataValues, ['whatever']);
+
+ $this->assertSame([], $actual);
+ }
+
+ public function assertFilterByFieldList($typeAll, $fieldListAll, $type, $fieldList, $instance, $expected) {
+ $this->cacheProjectSetting('-field-list-type-all', $typeAll);
+ $this->cacheProjectSetting('-field-list-all', $fieldListAll);
+
+ $project = [
+ "-field-list-type" => $type,
+ '-field-list' => $fieldList
+ ];
+
+ $_GET['pid'] = ExternalModules::getTestPIDs()[0];
+ $this->module->filterByFieldList($project, $instance);
+
+ $this->assertSame($expected, $instance);
+ }
+
+ public function testFilterByFieldList() {
+ $assert = function ($type, $fieldListNumbers, $expectedFieldNumbers) {
+ $assertFilterByFieldList = function ($typeAll, $fieldListAll, $type, $fieldList, $expectedFields) {
+ $instance = [
+ 'field1' => rand(),
+ 'field2' => rand()
+ ];
+
+ $fieldNumbersToNames = function ($numbers) {
+ if ($numbers === null) {
+ $numbers = [];
+ }
+
+ $names = [];
+ foreach ($numbers as $number) {
+ $names[] = "field$number";
+ }
+
+ return $names;
+ };
+
+ $fieldListAll = $fieldNumbersToNames($fieldListAll);
+ $fieldList = $fieldNumbersToNames($fieldList);
+ $expectedFields = $fieldNumbersToNames($expectedFields);
+
+ $expected = [];
+ foreach ($expectedFields as $expectedFieldName) {
+ if (isset($instance[$expectedFieldName])) {
+ $expected[$expectedFieldName] = $instance[$expectedFieldName];
+ }
+ }
+
+ $this->assertFilterByFieldList($typeAll, $fieldListAll, $type, $fieldList, $instance, $expected);
+ };
+
+ $assertFilterByFieldList(null, null, $type, $fieldListNumbers, $expectedFieldNumbers);
+ $assertFilterByFieldList($type, $fieldListNumbers, null, null, $expectedFieldNumbers);
+
+ if (!empty($type)) {
+ // Make sure project settings take precedence
+ $otherType = $type === 'include' ? 'exclude' : 'include';
+ $assertFilterByFieldList($otherType, $fieldListNumbers, $type, $fieldListNumbers, $expectedFieldNumbers);
+ }
+ };
+
+ $assert('include', [], []);
+ $assert('include', [1], [1]);
+ $assert('include', [1,2], [1,2]);
+ $assert('include', [2,3], [2]);
+ $assert('include', [3], []);
+
+ $assert('exclude', [], [1,2]);
+ $assert('exclude', [1], [2]);
+ $assert('exclude', [1,3], [2]);
+ $assert('exclude', [1,2], []);
+ $assert('exclude', [3], [1, 2]);
+
+ foreach ([null, ''] as $emptyType) {
+ $assert($emptyType, [], [1,2]);
+ $assert($emptyType, [1], [1,2]);
+ $assert($emptyType, [3], [1,2]);
+ }
+ }
+
+ public function testFilterByFieldList_checkboxes() {
+ $fieldName = 'some_checkbox';
+ $fieldNameWithSuffix = "{$fieldName}___1";
+ $otherFieldName = 'some_other_field';
+ $instance = [
+ $fieldNameWithSuffix => rand(),
+ $otherFieldName => rand()
+ ];
+
+ $this->assertFilterByFieldList(null, null, 'exclude', [$fieldName], $instance, [
+ $otherFieldName => $instance[$otherFieldName]
+ ]);
+
+ $this->assertFilterByFieldList(null, null, 'include', [$fieldName], $instance, [
+ $fieldNameWithSuffix => $instance[$fieldNameWithSuffix]
+ ]);
+ }
+
+ public function testLogDetails() {
+ $a = str_repeat('a', 65535);
+ $b = str_repeat('b', 65535);
+ $c = str_repeat('c', 65535);
+
+ $logId = $this->logDetails('message', "$a$b$c");
+
+ $result = $this->queryLogs('select details, details2, details3, details4 where log_id = ?', $logId);
+ $actual = $result->fetch_assoc();
+
+ $expected = [
+ 'details' => $a,
+ 'details2' => $b,
+ 'details3' => $c,
+ 'details4' => null,
+ ];
+
+ // Don't use assertSame() because the failure output is too large for the console.
+ $this->assertTrue($expected === $actual);
+ }
+}
diff --git a/tests/BaseTest.php b/tests/BaseTest.php
index cfe869c..bb3a36e 100644
--- a/tests/BaseTest.php
+++ b/tests/BaseTest.php
@@ -1,6 +1,9 @@
-addEvent($logId, $recordId, $event, $fields);
-
- $batch = $expected[count($expected)-1];
- $batch->add($logId, $recordId, $fields);
-
- $this->assertSame(json_encode($expected, JSON_PRETTY_PRINT), json_encode($batchBuilder->getBatches(), JSON_PRETTY_PRINT));
- };
-
- $expected[] = new Batch(APISyncExternalModule::UPDATE);
- $assert('UPDATE', ['a', 'b']);
-
- // Encountering a delete should start a new batch
- $expected[] = new Batch(APISyncExternalModule::DELETE);
- $assert('DELETE');
-
- // Each delete should be in a batch by itself
- $expected[] = new Batch(APISyncExternalModule::DELETE);
- $assert('DELETE');
-
- // A new batch should be started on an update after a delete
- $expected[] = new Batch(APISyncExternalModule::UPDATE);
- for($i=0; $i<$batchSize; $i++){
- $assert('UPDATE', ['a']);
- }
-
- // A new batch should be started when the batch size is reached
- $expected[] = new Batch(APISyncExternalModule::UPDATE);
- $assert('UPDATE', ['b']);
- }
-
- function testShouldStartNewBatch(){
- $assert = function($fields1, $fields2){
- $b = new BatchBuilder(100);
- $b->addEvent(1, 1, APISyncExternalModule::UPDATE, $fields1);
- $b->addEvent(2, 2, APISyncExternalModule::UPDATE, $fields1);
- $b->addEvent(3, 3, APISyncExternalModule::UPDATE, $fields2);
- $b->addEvent(4, 4, APISyncExternalModule::UPDATE, $fields2);
-
- $batches = $b->getBatches();
- $this->assertSame([1,2], $batches[0]->getRecordIds());
- $this->assertSame([3,4], $batches[1]->getRecordIds());
- };
-
- $assert(['a'], []);
- $assert([], ['b']);
- }
-
- function testMergeBatches(){
- $a = new BatchBuilder(100);
- $a->addEvent(1, 1, APISyncExternalModule::UPDATE, ['a']);
- $a->addEvent(3, 1, APISyncExternalModule::UPDATE, ['c']);
-
- $b = new BatchBuilder(100);
- $b->addEvent(2, 2, APISyncExternalModule::UPDATE, []);
- $b->addEvent(4, 3, APISyncExternalModule::UPDATE, []);
-
- $batches = $this->mergeBatches($a, $b);
- $this->assertSame($batches, $this->mergeBatches($b, $a), "The order of arguments shouldn't matter since we are sorting by log ID.");
-
- $this->assertSame(2, count($batches));
- $this->assertSame(['a', 'c'], $batches[0]->getFields());
- $this->assertSame([], $batches[1]->getFields());
- }
-}
\ No newline at end of file
+addEvent($logId, $recordId, $event, $fields);
+
+ $batch = $expected[count($expected) - 1];
+ $batch->add($logId, $recordId, $fields);
+
+ $this->assertSame(json_encode($expected, JSON_PRETTY_PRINT), json_encode($batchBuilder->getBatches(), JSON_PRETTY_PRINT));
+ };
+
+ $expected[] = new Batch(APISyncExternalModule::UPDATE);
+ $assert('UPDATE', ['a', 'b']);
+
+ // Encountering a delete should start a new batch
+ $expected[] = new Batch(APISyncExternalModule::DELETE);
+ $assert('DELETE');
+
+ // Each delete should be in a batch by itself
+ $expected[] = new Batch(APISyncExternalModule::DELETE);
+ $assert('DELETE');
+
+ // A new batch should be started on an update after a delete
+ $expected[] = new Batch(APISyncExternalModule::UPDATE);
+ for ($i = 0; $i < $batchSize; $i++) {
+ $assert('UPDATE', ['a']);
+ }
+
+ // A new batch should be started when the batch size is reached
+ $expected[] = new Batch(APISyncExternalModule::UPDATE);
+ $assert('UPDATE', ['b']);
+ }
+
+ public function testShouldStartNewBatch() {
+ $assert = function ($fields1, $fields2) {
+ $b = new BatchBuilder(100);
+ $b->addEvent(1, 1, APISyncExternalModule::UPDATE, $fields1);
+ $b->addEvent(2, 2, APISyncExternalModule::UPDATE, $fields1);
+ $b->addEvent(3, 3, APISyncExternalModule::UPDATE, $fields2);
+ $b->addEvent(4, 4, APISyncExternalModule::UPDATE, $fields2);
+
+ $batches = $b->getBatches();
+ $this->assertSame([1,2], $batches[0]->getRecordIds());
+ $this->assertSame([3,4], $batches[1]->getRecordIds());
+ };
+
+ $assert(['a'], []);
+ $assert([], ['b']);
+ }
+
+ public function testMergeBatches() {
+ $a = new BatchBuilder(100);
+ $a->addEvent(1, 1, APISyncExternalModule::UPDATE, ['a']);
+ $a->addEvent(3, 1, APISyncExternalModule::UPDATE, ['c']);
+
+ $b = new BatchBuilder(100);
+ $b->addEvent(2, 2, APISyncExternalModule::UPDATE, []);
+ $b->addEvent(4, 3, APISyncExternalModule::UPDATE, []);
+
+ $batches = $this->mergeBatches($a, $b);
+ $this->assertSame($batches, $this->mergeBatches($b, $a), "The order of arguments shouldn't matter since we are sorting by log ID.");
+
+ $this->assertSame(2, count($batches));
+ $this->assertSame(['a', 'c'], $batches[0]->getFields());
+ $this->assertSame([], $batches[1]->getFields());
+ }
+}
diff --git a/tests/BatchTest.php b/tests/BatchTest.php
index 0b0cb8e..37378cf 100644
--- a/tests/BatchTest.php
+++ b/tests/BatchTest.php
@@ -1,83 +1,84 @@
-add($lastLogId, $recordId, $fields);
-
- foreach(get_class_methods($batch) as $method){
- if(strpos($method, 'get') !== 0){
- continue;
- }
-
- $variableName = lcfirst(substr($method, 3));
-
- $this->assertSame($$variableName, $batch->{$method}(), "Testing Batch::$method()");
- }
- }
-
- function testAdd_duplicates(){
- $batch = new Batch(APISyncExternalModule::UPDATE);
- $recordId = rand();
-
- $assertCount = function($expected, $field) use ($batch, $recordId){
- $batch->add(rand(), $recordId, [$field]);
- $this->assertCount($expected, $batch->getFieldsByRecord()[$recordId]);
- };
-
- $assertCount(1, 'a');
- $assertCount(1, 'a');
- $assertCount(2, 'b');
- $assertCount(2, 'b');
- $assertCount(2, 'a');
- }
-
- function testAdd_nullFieldsForUpdate(){
- $this->expectExceptionMessage('array must be specified');
- $b = new Batch(APISyncExternalModule::UPDATE);
- $b->add(1, 1, null);
- }
-
- function testAdd_fieldForDelete(){
- $this->expectExceptionMessage('should be an empty array');
- $b = new Batch(APISyncExternalModule::DELETE);
- $b->add(1, 1, ['a']);
- }
-
- function testShouldStartNewBatch(){
- $assert = function($fields1, $fields2){
- $b = new Batch(APISyncExternalModule::UPDATE);
- $b->add(1, 1, $fields1);
-
- $exceptionThrown = false;
- try{
- $b->add(2, 2, $fields2);
- }
- catch(\Exception $e){
- $exceptionThrown = true;
- $this->assertStringContainsString('not allowed', $e->getMessage());
- }
-
- $this->assertTrue($exceptionThrown);
- };
-
- $assert(['a'], []);
- $assert([], ['b']);
- }
-}
\ No newline at end of file
+add($lastLogId, $recordId, $fields);
+
+ foreach (get_class_methods($batch) as $method) {
+ if (strpos($method, 'get') !== 0) {
+ continue;
+ }
+
+ $variableName = lcfirst(substr($method, 3));
+
+ $this->assertSame($$variableName, $batch->{$method}(), "Testing Batch::$method()");
+ }
+ }
+
+ public function testAdd_duplicates() {
+ $batch = new Batch(APISyncExternalModule::UPDATE);
+ $recordId = rand();
+
+ $assertCount = function ($expected, $field) use ($batch, $recordId) {
+ $batch->add(rand(), $recordId, [$field]);
+ $this->assertCount($expected, $batch->getFieldsByRecord()[$recordId]);
+ };
+
+ $assertCount(1, 'a');
+ $assertCount(1, 'a');
+ $assertCount(2, 'b');
+ $assertCount(2, 'b');
+ $assertCount(2, 'a');
+ }
+
+ public function testAdd_nullFieldsForUpdate() {
+ $this->expectExceptionMessage('array must be specified');
+ $b = new Batch(APISyncExternalModule::UPDATE);
+ $b->add(1, 1, null);
+ }
+
+ public function testAdd_fieldForDelete() {
+ $this->expectExceptionMessage('should be an empty array');
+ $b = new Batch(APISyncExternalModule::DELETE);
+ $b->add(1, 1, ['a']);
+ }
+
+ public function testShouldStartNewBatch() {
+ $assert = function ($fields1, $fields2) {
+ $b = new Batch(APISyncExternalModule::UPDATE);
+ $b->add(1, 1, $fields1);
+
+ $exceptionThrown = false;
+ try {
+ $b->add(2, 2, $fields2);
+ } catch (\Exception $e) {
+ $exceptionThrown = true;
+ $this->assertStringContainsString('not allowed', $e->getMessage());
+ }
+
+ $this->assertTrue($exceptionThrown);
+ };
+
+ $assert(['a'], []);
+ $assert([], ['b']);
+ }
+}
diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php
index 52b5fb5..67e1699 100644
--- a/tests/TranslationTest.php
+++ b/tests/TranslationTest.php
@@ -1,14 +1,16 @@
buildTranslations($project1);
$this->assertSame([], $project1);
-
+
$project2 = [
'form-translations' => '[["Instrument A"," My First Instrument"," My First Instrument_2"],["Instrument B"," Instrument 2"," Instrument 2_2"]]',
'event-translations' => '[["E_1"," E1"," EA"],["E_2"," E2"," EB"],["E_3"," E3"," EC"]]'
@@ -25,10 +27,10 @@ function test_build_translations() {
['e_3', 'e3', 'ec']
]
]);
- }
- function test_import_translate_data() {
+ }
+ public function test_import_translate_data() {
$module = new APISyncExternalModule();
-
+
$project = [
'form-translations' => [
['instrument_a', 'my_first_instrument', 'my_first_instrument_2'],
@@ -40,7 +42,7 @@ function test_import_translate_data() {
['e_3', 'e3', 'ec']
]
];
- $data = [
+ $data = [
[
'my_rid_field' => 'src1_1',
'redcap_event_name' => 'e1_arm_1',
@@ -59,10 +61,10 @@ function test_import_translate_data() {
'instrument_2_complete' => ''
]
];
-
+
$module->translateFormNames($data, $project);
$module->translateEventNames($data, $project);
-
+
$this->assertSame([
[
'my_rid_field' => 'src1_1',
@@ -82,5 +84,5 @@ function test_import_translate_data() {
'instrument_b_complete' => ''
]
], $data);
- }
-}
\ No newline at end of file
+ }
+}