add validation to date metadata type

This commit is contained in:
Leo Germani 2018-09-18 10:20:15 -03:00
parent 9ddf07ccd7
commit ace115837a
3 changed files with 99 additions and 1 deletions

View File

@ -2,6 +2,8 @@
namespace Tainacan\Metadata_Types; namespace Tainacan\Metadata_Types;
use Tainacan\Entities\Item_Metadata_Entity;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
use Tainacan\Helpers; use Tainacan\Helpers;
@ -28,4 +30,19 @@ class Date extends Metadata_Type {
value=\''.json_encode( $itemMetadata->get_value() ).'\' value=\''.json_encode( $itemMetadata->get_value() ).'\'
name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-date>'; name="'.$itemMetadata->get_metadatum()->get_name().'"></tainacan-date>';
} }
public function validate( Item_Metadata_Entity $item_metadata) {
$value = $item_metadata->get_value();
$format = 'Y-m-d';
$d = \DateTime::createFromFormat($format, $value);
return $d && $d->format($format) === $value;
}
} }

View File

@ -56,7 +56,7 @@ class Test_Importer extends Importer {
]; ];
protected $date_values = [ protected $date_values = [
'03/04/1993', '03/12/1998', '10/09/2001', '03/01/2018', '13/11/2016', '22/04/1993' '1993-12-03', '1998-04-03', '2001-09-10', '2018-01-03', '2016-11-13', '1993-04-22'
]; ];
protected $numeric_values = [ protected $numeric_values = [

View File

@ -0,0 +1,81 @@
<?php
namespace Tainacan\Tests;
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
use Tainacan\Entities;
/**
* Sample test case.
*/
class DateMetadatumTypes extends TAINACAN_UnitTestCase {
function test_date_metadata_types() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Date',
'status' => 'publish',
),
true
);
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'item test',
'description' => 'adasdasdsa',
'collection' => $collection,
'status' => 'publish',
),
true
);
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($i, $metadatum);
$item_metadata->set_value('2010-01-01');
$this->assertTrue($item_metadata->validate());
$item_metadata->set_value('2010-12-01');
$this->assertTrue($item_metadata->validate());
$item_metadata->set_value('2010-12-31');
$this->assertTrue($item_metadata->validate());
$item_metadata->set_value('2010-22-01');
$this->assertFalse($item_metadata->validate());
$item_metadata->set_value('3/3/1202');
$this->assertFalse($item_metadata->validate());
$item_metadata->set_value('2010-02-30');
$this->assertFalse($item_metadata->validate());
}
}