Skip to content

Commit

Permalink
[NDB_Page] Add default maxYear for date elements (#9498)
Browse files Browse the repository at this point in the history
Changed NDB_Page to have a default maxYear that is set for all date
elements to '9999' if the page does not override that field with its own
dateOptions

* Resolves #9497
  • Loading branch information
skarya22 authored Dec 10, 2024
1 parent 009297f commit 4eb8f6c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
14 changes: 0 additions & 14 deletions php/libraries/NDB_BVL_Instrument.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ abstract class NDB_BVL_Instrument extends NDB_Page
*/
protected $jsonData = false;

/**
* The form object for this instrument.
*
* @var LorisForm
*/
public $form;

/**
* Variable defining the type of form in use
*
Expand All @@ -40,13 +33,6 @@ abstract class NDB_BVL_Instrument extends NDB_Page
*/
public $formType;

/**
* Array holding the date format
*
* @var array
*/
public $dateOptions;

/**
* Type of instrument. Can be 'normal' or 'DirectEntry'
*
Expand Down
14 changes: 7 additions & 7 deletions php/libraries/NDB_Page.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ class NDB_Page extends \LORIS\Http\Endpoint implements RequestHandlerInterface
* The default dateOptions to send to LorisForm to render elements
* on this page. This is usually set by subclasses of NDB_Page which
* need to modify the options to show on a date (ie. min year, max year,
* etc.)
* etc.). Default values can be added here, which can be overriden.
*
* @var array
*/
public $dateOptions;
public $dateOptions = [
'maxYear' => '9999'
];

/**
* Does the setup required for this page. By default, sets up elements
Expand Down Expand Up @@ -315,9 +317,7 @@ class NDB_Page extends \LORIS\Http\Endpoint implements RequestHandlerInterface
'style' => 'max-width:33%; display:inline-block;',
]
) {
if ($options === [] && !empty($this->dateOptions)) {
$options = $this->dateOptions;
}
$options = ($options ?? []) + $this->dateOptions;
$this->form->addElement('date', $field, $label, $options, $attribs);
}

Expand Down Expand Up @@ -592,7 +592,7 @@ class NDB_Page extends \LORIS\Http\Endpoint implements RequestHandlerInterface
function createDate(
$field,
$label,
$dateOptions=null,
$dateOptions=[],
$attribs=[
'class' => 'form-control input-sm',
'style' => 'max-width:33%; display:inline-block;',
Expand All @@ -602,7 +602,7 @@ class NDB_Page extends \LORIS\Http\Endpoint implements RequestHandlerInterface
"date",
$field,
$label,
$dateOptions,
($dateOptions ?? []) + $this->dateOptions,
$attribs
);
}
Expand Down
5 changes: 4 additions & 1 deletion test/unittests/NDB_BVL_Instrument_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ function testAddCustomDateElement()
'type' => 'date',
'html' => $this->_instrument->form
->renderElement($groupEl['elements'][0]),
'options' => ['value' => 'Option']
'options' => [
'value' => 'Option',
'maxYear' => '9999'
]
],
[
'label' => null,
Expand Down
22 changes: 16 additions & 6 deletions test/unittests/NDB_PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function testAddBasicTextArea()
/**
* Test that addBasicDate calls addElement from LorisForm and properly adds
* an element to the page's form when dateOptions is not set. Since
* dateOptions is not set, the options array should remain empty.
* dateOptions is not set, the options array should have the default maxYear.
*
* @covers NDB_Page::addBasicDate
* @return void
Expand All @@ -256,7 +256,9 @@ public function testAddBasicDateWithNoDateOptions()
'label' => 'test_label',
'type' => 'date',
'class' => 'form-control input-sm',
'options' => []
'options' => [
'maxYear' => '9999'
]
],
$this->_page->form->form['test_name']
);
Expand All @@ -265,21 +267,27 @@ public function testAddBasicDateWithNoDateOptions()
/**
* Test that addBasicDate calls addElement from LorisForm and properly adds
* an element to the page's form when dateOptions is set. Since
* dateOptions is set, the options array should have information in it.
* dateOptions is set, the options array should have that information in it
*
* @covers NDB_Page::addBasicDate
* @return void
*/
public function testAddBasicDateWithDateOptionsSet()
{
$this->_page->dateOptions = ['someOption' => 'true'];
$this->_page->dateOptions = [
'someOption' => 'true',
'maxYear' => '2028'
];
$this->_page->addBasicDate("test_name", "test_label");
$this->assertEquals(
['name' => 'test_name',
'label' => 'test_label',
'type' => 'date',
'class' => 'form-control input-sm',
'options' => ['someOption' => 'true']
'options' => [
'someOption' => 'true',
'maxYear' => '2028'
]
],
$this->_page->form->form['test_name']
);
Expand Down Expand Up @@ -603,7 +611,9 @@ public function testCreateDate()
'label' => 'test_label',
'type' => 'date',
'class' => 'form-control input-sm',
'options' => null
'options' => [
'maxYear' => '9999'
]
],
$this->_page->createDate("test_field", "test_label")
);
Expand Down

0 comments on commit 4eb8f6c

Please sign in to comment.