Skip to content

Commit

Permalink
fix: sample attribute lat lon regex and migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alli83 committed Nov 12, 2024
1 parent ea1a37c commit f25ddc3
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

- Fix #831: Check if longitude and lagitude match the WGS84 format for sample attributes
- Feat #1867: Update the gitlab static application security testing (SAST) job using the Semgrep-based analyzer
- Fix #2066: Max length for attribute value set to 1000 in file admin form
- Feat #1968: Add curators manual for operating tools on bastion server
Expand Down
2 changes: 2 additions & 0 deletions data/dev/attribute.csv
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ id,attribute_name,definition,model,structured_comment_name,value_syntax,allowed_
347,last_modified,the date the file was last modified,modified,last_modified,date,"",1,"",""
356,Description,"description of the sample (may include many attributes as human readable text, but these should also be added as independent attribute:value pairs).","",description,text,"",1,"",""
376,estimated genome size,"An estimate of the size of the genome of the species being studied, in basepairs (Gb, Mb or Kb)","",est_genome_size,text,"",1,"",""
391,geographic location (latitude),"The geographical latitudinal origin of the sample, the value should be reported in decimal degrees and in WGS84 system","",latitude,number,"","","",""
392,geographic location (longitude),"The geographical longitudinal origin of the sample, the value should be reported in decimal degrees and in WGS84 system","",longitude,number,"","","",""
448,alternative accession-biosample,"","",alt_acc_biosample,"","","","",""
455,keyword,"","",keywords,"","","","",""
472,camera parameters,"","",camera_parameters,text,"",m,"",""
Expand Down
58 changes: 48 additions & 10 deletions protected/controllers/AdminSampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function accessRules()
{
return array(
array('allow', // admin only
'actions'=>array('admin','delete','index','view','create','update'),
'actions'=>array('admin','delete','index','view','create','update', 'checkAttribute'),
'roles'=>array('admin'),
),
array('allow', 'actions' => array('create1', 'choose'), 'users' => array('@')),
Expand Down Expand Up @@ -315,6 +315,43 @@ protected function performAjaxValidation($model)
}
}

public function actionCheckAttribute()
{
if (!Yii::app()->request->isAjaxRequest) {
throw new CHttpException(400,'Invalid request. An Error occurred');
}

$errorMessage = [];
$attributesList = $_POST['attr'];

foreach (explode('",', $attributesList) as $attributes) {
$attributes = str_replace('"', '', $attributes);
$attributeData = explode('=', $attributes);
if (count($attributeData) === 2) {
$attributeData[0] = trim($attributeData[0]);
if ($attributeData[0] !== 'longitude' && $attributeData[0] !== 'latitude') {
continue;
}

if(preg_match('/^[^0-9]*$/', $attributeData[1])) {
$errorMessage[] = sprintf('Attribute value for %s doesn\'t match WGS84 decimal format.
For geographic location (country, sea, region) use another attribute name', $attributeData[0]);
}

if ($attributeData[0] === 'latitude' && !preg_match('/^[-+]?(?:90(?:\.0+)?|[1-8]?\d(?:\.\d+)?)$/', $attributeData[1])) {
$errorMessage[] = sprintf('Attribute value for %s doesn\'t match WGS84 decimal format', $attributeData[0]);
}

if ($attributeData[0] === 'longitude' && !preg_match('/^[-+]?(?:180(?:\.0+)?|1[0-7]\d(?:\.\d+)?|\d{1,2}(?:\.\d+)?)$/', $attributeData[1])) {
$errorMessage[] = sprintf('Attribute value for %s doesn\'t match WGS84 decimal format', $attributeData[0]);
}
}
}

echo CJSON::encode(['messages' => $errorMessage]);
Yii::app()->end();
}

/**
* Upate sample attribute
*
Expand All @@ -337,15 +374,16 @@ private function updateSampleAttributes($model)
$attribute = Attributes::model()->findByAttributes(array('structured_comment_name' => trim($attributeData[0])));
if (!$attribute) {
$model->addError('error', 'Attribute name for the input ' . $attributeData[0] . "=" . $attributeData[1] . ' is not valid - please select a valid attribute name!');
} else {
// Let's save the new sample attribute
$sampleAttribute->value = trim($attributeData[1]);
$sampleAttribute->attribute_id = $attribute->id;
if (!$sampleAttribute->save(true)) {
foreach ($sampleAttribute->getErrors() as $errors) {
foreach ($errors as $errorMessage) {
$model->addError('error', $errorMessage);
}

continue;
}
// Let's save the new sample attribute
$sampleAttribute->value = trim($attributeData[1]);
$sampleAttribute->attribute_id = $attribute->id;
if (!$sampleAttribute->save()) {
foreach ($sampleAttribute->getErrors() as $errors) {
foreach ($errors as $errorMessage) {
$model->addError('error', $errorMessage);
}
}
}
Expand Down
80 changes: 77 additions & 3 deletions protected/views/adminSample/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php if ($model->hasErrors()) : ?>
<div class="alert alert-danger">
<div id="sample_error" class="alert alert-danger">
<?php echo $form->errorSummary($model); ?>
</div>
<?php endif; ?>

<div id="ajax-error" class='alert alert-danger' style="display: none;">An error occurred</div>

<div class="form-group">
<?php echo $form->labelEx($model, 'species_id', array('class' => 'control-label')); ?>
<?php
Expand Down Expand Up @@ -65,10 +67,82 @@

<div class="pull-right btns-row">
<a href="/adminSample/admin" class="btn background-btn-o">Cancel</a>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class' => 'btn background-btn')); ?>
<button id='checkAttribute' type='button' class="btn btn-primary"><?php echo $model->isNewRecord ? 'Create' : 'Save'?></button>

</div>

<div class='modal fade' id='confirmation_sample_modal' role='dialog'>
<div class='modal-dialog modal-lg'>
<div class='modal-content'>
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal'>&times;</button>
<h4 class='modal-title'>Important</h4>
</div>
<div class='modal-body'>
<div id='check-attribute-warning' class='alert alert-warning' style='display: none;'>
</div>
<div id="check-attribute-confirmation" class="mt-4">
</div>
</div>
<div class='modal-footer'>
<a id="hideModal" class='btn background-btn-o'>Cancel</a>
<?php echo CHtml::submitButton('Confirm', array('class' => 'btn background-btn')); ?>
</div>
</div>

</div>
</div>
<?php $this->endWidget(); ?>
</div>

</div>
</div>
<script>
$(document).ready(function() {
$('#hideModal').click(function(e) {
$('#confirmation_sample_modal').modal('hide');
})

$('#checkAttribute').click(function (e) {
e.preventDefault()

let myWarning = $('#check-attribute-warning')[0];
let myConfirmation = $('#check-attribute-confirmation')[0];
myWarning.innerHTML = '';
myWarning.style.display = 'none'
myConfirmation.innerHTML = '';
$('#ajax-error')[0].style.display = 'none';

$.ajax({
url: "<?php echo Yii::app()->createUrl('adminSample/checkAttribute') ?> ",
type: 'POST',
data: {
attr: $('.form').find("textarea[name='Sample[attributesList]']").val()
},
dataType: 'json',
success: function(response) {
$('#confirmation_sample_modal').modal('show');

if (0 < response.messages.length) {
myWarning.style.display = 'block'
}

response.messages.forEach((message) => {
let el = document.createElement('li');
el.textContent = message;

myWarning.appendChild(el);
})

let el = document.createElement('div');
el.textContent= 'Are you sure you want to continue?'
el.className = 'mt-4'
myConfirmation.appendChild(el);

},
error: function(xhr, status, error) {
$('#ajax-error')[0].style.display = 'block';
}
});
});
});
</script>
112 changes: 112 additions & 0 deletions tests/acceptance/AdminSample.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Feature: admin page for samples
When I fill in the field of "name" "Sample[species_id]" with ":Foxtail millet"
And I press the button "Save"
And I wait "1" seconds
Then I should see "Are you sure you want to continue?"
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "Please fix the following input errors:"
And I should see "Taxon ID is empty!"

Expand All @@ -31,6 +34,8 @@ Feature: admin page for samples
When I fill in the field of "name" "Sample[attributesList]" with "source_mat_id=\"David Lambert & BGI\",est_genome_size=\"1.32\",alternative_names=\"PYGAD\",animal=\"tiger\""
And I press the button "Save"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "Please fix the following input errors:"
And I should see "Attribute name for the input animal=\tiger\ is not valid - please select a valid attribute name!"
And I should see "David Lambert"
Expand All @@ -43,6 +48,8 @@ Feature: admin page for samples
And I should see "lat_lon"
And I press the button "Save"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
And I should see "David Lambert"
And I should see "1.32"
And I should see "PYGAD"
Expand All @@ -54,6 +61,8 @@ Feature: admin page for samples
When I fill in the field of "name" "Sample[species_id]" with "4555=Foxtail millet"
And I press the button "Save"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "Please fix the following input errors:"
And I should see "The input format is wrong, should be tax_id:common_name"

Expand All @@ -64,6 +73,8 @@ Feature: admin page for samples
When I fill in the field of "name" "Sample[attributesList]" with "animal=\"tiger\",plant=\"rose\""
And I press the button "Save"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "Please fix the following input errors:"
And I should see "Attribute name for the input animal=\tiger\ is not valid - please select a valid attribute name!"
And I should see "Attribute name for the input plant=\rose\ is not valid - please select a valid attribute name!"
Expand All @@ -75,6 +86,8 @@ Feature: admin page for samples
When I fill in the field of "name" "Sample[species_id]" with "Human"
And I press the button "Create"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "Please fix the following input errors:"
And I should see "Taxon ID Human is not numeric!"

Expand All @@ -85,6 +98,8 @@ Feature: admin page for samples
When I fill in the field of "name" "Sample[species_id]" with "789123"
And I press the button "Create"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "Please fix the following input errors:"
And I should see "Taxon ID 789123 is not found!"

Expand All @@ -96,9 +111,102 @@ Feature: admin page for samples
And I fill in the field of "name" "Sample[attributesList]" with "animal=\"tiger\""
And I press the button "Create"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "Please fix the following input errors:"
And I should see "Attribute name for the input animal=\tiger\ is not valid - please select a valid attribute name!"

Scenario: display 1 warning message when create with wrong latitude beyond 90
Given I am on "/adminSample/create"
And I should see "Create"
When I fill in the field of "name" "Sample[species_id]" with "87676:Eucalyptus pauciflora"
And I fill in the field of "name" "Sample[attributesList]" with "latitude=\"95.1234\""
And I press the button "Create"
And I wait "1" seconds
Then I should see "Are you sure you want to continue?"
And I should see "Attribute value for latitude doesn't match WGS84 decimal format"
And I press the button "Confirm"
And I wait "1" seconds
Then I should not see "Please fix the following input errors:"

Scenario: display 1 warning message when create with wrong latitude below -90
Given I am on "/adminSample/create"
And I should see "Create"
When I fill in the field of "name" "Sample[species_id]" with "87676:Eucalyptus pauciflora"
And I fill in the field of "name" "Sample[attributesList]" with "latitude=\"-95.1234\""
And I press the button "Create"
And I wait "1" seconds
Then I should see "Are you sure you want to continue?"
And I should see "Attribute value for latitude doesn't match WGS84 decimal format"
And I press the button "Confirm"
And I wait "1" seconds
Then I should not see "Please fix the following input errors:"

Scenario: display 1 warning message when create with wrong longitude beyond 180
Given I am on "/adminSample/create"
And I should see "Create"
When I fill in the field of "name" "Sample[species_id]" with "87676:Eucalyptus pauciflora"
And I fill in the field of "name" "Sample[attributesList]" with "longitude=\"200.1234\""
And I press the button "Create"
And I wait "1" seconds
Then I should see "Are you sure you want to continue?"
And I should see "Attribute value for longitude doesn't match WGS84 decimal format"
And I press the button "Confirm"
And I wait "1" seconds
Then I should not see "Please fix the following input errors:"

Scenario: display 1 warning message when create with wrong longitude below -180
Given I am on "/adminSample/create"
And I should see "Create"
When I fill in the field of "name" "Sample[species_id]" with "87676:Eucalyptus pauciflora"
And I fill in the field of "name" "Sample[attributesList]" with "longitude=\"-200.1234\""
And I press the button "Create"
And I wait "1" seconds
Then I should see "Attribute value for longitude doesn't match WGS84 decimal format"
And I should see "Are you sure you want to continue?"
And I press the button "Confirm"
And I wait "1" seconds
Then I should not see "Please fix the following input errors:"

Scenario: display 1 warning message when create with incorrect formatting
Given I am on "/adminSample/create"
And I should see "Create"
When I fill in the field of "name" "Sample[species_id]" with "87676:Eucalyptus pauciflora"
And I fill in the field of "name" "Sample[attributesList]" with "longitude=\"123.456.789\""
And I press the button "Create"
And I wait "1" seconds
Then I should see "Are you sure you want to continue?"
And I should see "Attribute value for longitude doesn't match WGS84 decimal format"
And I press the button "Confirm"
And I wait "1" seconds
Then I should not see "Please fix the following input errors:"

Scenario: display 1 extra message when create with extra symbol
Given I am on "/adminSample/create"
And I should see "Create"
When I fill in the field of "name" "Sample[species_id]" with "87676:Eucalyptus pauciflora"
And I fill in the field of "name" "Sample[attributesList]" with "longitude=\"123.4567°\""
And I press the button "Create"
And I wait "1" seconds
Then I should see "Are you sure you want to continue?"
And I should see "Attribute value for longitude doesn't match WGS84 decimal format"
And I press the button "Confirm"
And I wait "1" seconds
Then I should not see "Please fix the following input errors:"

Scenario: display 1 warning message when create with region for lat_lon
Given I am on "/adminSample/create"
And I should see "Create"
When I fill in the field of "name" "Sample[species_id]" with "87676:Eucalyptus pauciflora"
And I fill in the field of "name" "Sample[attributesList]" with "latitude=something"
And I press the button "Create"
And I wait "1" seconds
Then I should see "Are you sure you want to continue?"
And I should see "Attribute value for latitude doesn't match WGS84 decimal format. For geographic location (country, sea, region) use another attribute name"
And I press the button "Confirm"
And I wait "1" seconds
Then I should not see "Please fix the following input errors:"

@ok
Scenario: display 2 input error messages when create
Given I am on "/adminSample/create"
Expand All @@ -107,6 +215,8 @@ Feature: admin page for samples
And I fill in the field of "name" "Sample[attributesList]" with "animal=\"tiger\",plant=\"rose\""
And I press the button "Create"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "Please fix the following input errors:"
And I should see "Attribute name for the input animal=\tiger\ is not valid - please select a valid attribute name!"
And I should see "Attribute name for the input plant=\rose\ is not valid - please select a valid attribute name!"
Expand All @@ -119,6 +229,8 @@ Feature: admin page for samples
And I fill in the field of "name" "Sample[attributesList]" with "sex=\"male\",alternative_names=\"Alternative name here\""
And I press the button "Create"
And I wait "1" seconds
And I press the button "Confirm"
And I wait "1" seconds
Then I should see "View Sample #451"
And I should see "male"
And I should see "Alternative name here"

0 comments on commit f25ddc3

Please sign in to comment.