diff --git a/src/mongo/delegates/Tables.class.php b/src/mongo/delegates/Tables.class.php index c951e5fa..8df1da74 100644 --- a/src/mongo/delegates/Tables.class.php +++ b/src/mongo/delegates/Tables.class.php @@ -926,6 +926,11 @@ protected function isFunction($value) */ protected function castValueType($value, $type=null) { + // If value is a UTCDateTime, turn into a DateTime object in order to perform comparison + if ($value instanceof \MongoDB\BSON\UTCDateTime) { + $value = $value->toDateTime(); + } + switch($type) { case 'string': diff --git a/test/unit/mongo/MongoTripodComputedFieldsTest.php b/test/unit/mongo/MongoTripodComputedFieldsTest.php index 7f8b7b0c..3c6d6129 100644 --- a/test/unit/mongo/MongoTripodComputedFieldsTest.php +++ b/test/unit/mongo/MongoTripodComputedFieldsTest.php @@ -23,6 +23,65 @@ public function tearDown() parent::tearDown(); } + public function testConditionalComputedFieldWithDates() { + $tableSpec = array( + "_id"=> "t_conditional_creators", + "type"=> array("bibo:Document"), + "from"=>"CBD_testing", + "fields"=> array( + array( + "fieldName" => "dateUpdated", + "predicates" => array(array( + "date" => array( + "predicates" => array("dct:updated") + ) + )) + ), + array( + "fieldName" => "datePublished", + "predicates" => array(array( + "date" => array( + "predicates" => array("dct:published") + ) + )) + ) + ), + "computed_fields"=>array( + array( + "fieldName" => "status", + "value" => array( + "conditional" => array( + "if" => array('$dateUpdated', '>', '$datePublished'), + "then" => 'Updated', + "else" => 'Published' + ) + ) + ) + ) + ); + + $oldConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig = \Tripod\Mongo\Config::getConfig(); + $newConfig['stores']['tripod_php_testing']['table_specifications'][] = $tableSpec; + \Tripod\Mongo\Config::setConfig($newConfig); + \Tripod\Mongo\Config::getInstance(); + $this->tripod = new \Tripod\Mongo\Driver('CBD_testing', 'tripod_php_testing'); + $this->loadDatesDataViaTripod(); + $this->tripod->generateTableRows('t_conditional_creators'); + + $collection = \Tripod\Mongo\Config::getInstance()->getCollectionForTable('tripod_php_testing', 't_conditional_creators'); + + $tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_creators', '_id.r' => 'baseData:foo1234')); + $this->assertEquals('Updated', $tableDoc['value']['status']); + + $tableDoc = $collection->findOne(array('_id.type'=>'t_conditional_creators', '_id.r' => 'baseData:foo12345')); + $this->assertEquals('Published', $tableDoc['value']['status']); + + \Tripod\Mongo\Config::setConfig($oldConfig); + \Tripod\Mongo\Config::getInstance(); + $collection->drop(); + } + public function testConditionalComputedField() { $tableSpec = array( diff --git a/test/unit/mongo/MongoTripodTestBase.php b/test/unit/mongo/MongoTripodTestBase.php index 54d33a95..e1ff84dd 100644 --- a/test/unit/mongo/MongoTripodTestBase.php +++ b/test/unit/mongo/MongoTripodTestBase.php @@ -51,6 +51,9 @@ protected function loadResourceData() } } + protected function loadDatesDataViaTripod() { + $this->loadDataViaTripod('/data/dates.json'); + } protected function loadResourceDataViaTripod() { $this->loadDataViaTripod('/data/resources.json'); diff --git a/test/unit/mongo/data/dates.json b/test/unit/mongo/data/dates.json new file mode 100644 index 00000000..f8f6730c --- /dev/null +++ b/test/unit/mongo/data/dates.json @@ -0,0 +1,52 @@ +[ + { + "_id": + { + "r":"baseData:foo1234", + "c":"http://talisaspire.com/" + }, + "rdf:type": + [ + { + "u":"bibo:Document" + } + ], + "dct:title" : { + "l" : "A document title" + }, + "dct:isVersionOf" : { + "u" : "http://talisaspire.com/works/4d101f63c10a6" + }, + "dct:updated" : { + "l" : "2017-02-23T14:49:02+00:00" + }, + "dct:published" : { + "l" : "2017-02-22T14:49:02+00:00" + } + }, + { + "_id": + { + "r":"baseData:foo12345", + "c":"http://talisaspire.com/" + }, + "rdf:type": + [ + { + "u":"bibo:Document" + } + ], + "dct:title" : { + "l" : "A document title" + }, + "dct:isVersionOf" : { + "u" : "http://talisaspire.com/works/4d101f63c10a6" + }, + "dct:updated" : { + "l" : "2017-02-23T14:49:02+00:00" + }, + "dct:published" : { + "l" : "2017-02-23T14:49:02+00:00" + } + } +] \ No newline at end of file