Browse Source

[timestampable] allow manual user dates to be forced on timestampable properties + test cases

gediminasm 14 years ago
parent
commit
409cd7d293

+ 15 - 8
lib/Gedmo/Timestampable/AbstractTimestampableListener.php

@@ -45,19 +45,24 @@ abstract class AbstractTimestampableListener extends MappedEventSubscriber
             $objectClass = get_class($object);
             if ($config = $this->getConfiguration($om, $objectClass)) {
                 $meta = $om->getClassMetadata($objectClass);
+                $changeSet = $this->getObjectChangeSet($uow, $object);
                 $needChanges = false;
 
                 if (isset($config['update'])) {
-                    $needChanges = true;
                     foreach ($config['update'] as $field) {
-                        $meta->getReflectionProperty($field)
-                            ->setValue($object, $this->getDateValue($meta, $field));
+                        if (!isset($changeSet[$field])) { // let manual values
+                            $needChanges = true;
+                            $meta->getReflectionProperty($field)->setValue($object, $this->getDateValue($meta, $field));
+                        }
                     }
                 }
                 
                 if (isset($config['change'])) {
-                    $changeSet = $this->getObjectChangeSet($uow, $object);
                     foreach ($config['change'] as $options) {
+                        if (isset($changeSet[$options['field']])) {
+                            continue; // value was set manually
+                        }
+                        
                         $tracked = $options['trackedField'];
                         $trackedChild = null;
                         $parts = explode('.', $tracked);
@@ -112,15 +117,17 @@ abstract class AbstractTimestampableListener extends MappedEventSubscriber
         if ($config = $this->getConfiguration($om, $meta->name)) {
             if (isset($config['update'])) {
                 foreach ($config['update'] as $field) {
-                    $meta->getReflectionProperty($field)
-                        ->setValue($object, $this->getDateValue($meta, $field));
+                    if ($meta->getReflectionProperty($field)->getValue($object) === null) { // let manual values
+                        $meta->getReflectionProperty($field)->setValue($object, $this->getDateValue($meta, $field));
+                    }
                 }
             }
             
             if (isset($config['create'])) {
                 foreach ($config['create'] as $field) {
-                    $meta->getReflectionProperty($field)
-                        ->setValue($object, $this->getDateValue($meta, $field));
+                    if ($meta->getReflectionProperty($field)->getValue($object) === null) { // let manual values
+                        $meta->getReflectionProperty($field)->setValue($object, $this->getDateValue($meta, $field));
+                    }
                 }
             }
         }

+ 15 - 0
tests/Gedmo/Timestampable/Fixture/Article.php

@@ -91,11 +91,21 @@ class Article implements Timestampable
         return $this->created;
     }
     
+    public function setCreated(\DateTime $created)
+    {
+        $this->created = $created;
+    }
+    
     public function getPublished()
     {
         return $this->published;
     }
     
+    public function setPublished(\DateTime $published)
+    {
+        $this->published = $published;
+    }
+    
     /**
      * Get updated
      *
@@ -105,4 +115,9 @@ class Article implements Timestampable
     {
         return $this->updated;
     }
+    
+    public function setUpdated(\DateTime $updated)
+    {
+        $this->updated = $updated;
+    }
 }

+ 15 - 0
tests/Gedmo/Timestampable/Fixture/Document/Article.php

@@ -83,4 +83,19 @@ class Article
     {
         return $this->type;
     }
+    
+    public function setCreated($created)
+    {
+        $this->created = $created;
+    }
+    
+    public function setPublished(\DateTime $published)
+    {
+        $this->published = $published;
+    }
+    
+    public function setUpdated(\DateTime $updated)
+    {
+        $this->updated = $updated;
+    }
 }

+ 41 - 0
tests/Gedmo/Timestampable/TimestampableDocumentTest.php

@@ -101,6 +101,47 @@ class TimestampableDocumentTest extends \PHPUnit_Framework_TestCase
         );
     }
     
+    public function testForcedValues()
+    {
+        $sport = new Article();
+        $sport->setTitle('sport forced');
+        $created = strtotime('2000-01-01 12:00:00');
+        $sport->setCreated($created);
+        $sport->setUpdated(new \DateTime('2000-01-01 12:00:00'));
+        
+        $this->dm->persist($sport);
+        $this->dm->flush();
+        $this->dm->clear();
+        
+        $repo = $this->dm->getRepository(self::TEST_CLASS_ARTICLE);
+        $sport = $repo->findOneByTitle('sport forced');
+        $this->assertEquals(
+            $created, 
+            (string)$sport->getCreated()
+        );
+        $this->assertEquals(
+            '2000-01-01 12:00:00', 
+            $sport->getUpdated()->format('Y-m-d H:i:s')
+        );
+        
+        $published = new Type;
+        $published->setIdentifier('published');
+        $published->setTitle('Published');
+        
+        $sport->setType($published);
+        $sport->setPublished(new \DateTime('2000-01-01 12:00:00'));
+        $this->dm->persist($sport);
+        $this->dm->persist($published);
+        $this->dm->flush();
+        $this->dm->clear();
+        
+        $sport = $repo->findOneByTitle('sport forced');
+        $this->assertEquals(
+            '2000-01-01 12:00:00', 
+            $sport->getPublished()->format('Y-m-d H:i:s')
+        );
+    }
+    
     private function populate()
     {
         $qb = $this->dm->createQueryBuilder(self::TEST_CLASS_ARTICLE);

+ 40 - 1
tests/Gedmo/Timestampable/TimestampableTest.php

@@ -121,4 +121,43 @@ class TimestampableTest extends \PHPUnit_Framework_TestCase
             $sport->getPublished()->format('Y-m-d H:i:s')
         );
     }
-}
+
+    public function testForcedValues()
+    {
+        $sport = new Article();
+        $sport->setTitle('sport forced');
+        $sport->setCreated(new \DateTime('2000-01-01'));
+        $sport->setUpdated(new \DateTime('2000-01-01 12:00:00'));
+        
+        $this->em->persist($sport);
+        $this->em->flush();
+        $this->em->clear();
+        
+        $repo = $this->em->getRepository(self::TEST_ENTITY_ARTICLE);
+        $sport = $repo->findOneByTitle('sport forced');
+        $this->assertEquals(
+            '2000-01-01', 
+            $sport->getCreated()->format('Y-m-d')
+        );
+        $this->assertEquals(
+            '2000-01-01 12:00:00', 
+            $sport->getUpdated()->format('Y-m-d H:i:s')
+        );
+        
+        $published = new Type();
+        $published->setTitle('Published');
+        
+        $sport->setType($published);
+        $sport->setPublished(new \DateTime('2000-01-01 12:00:00'));
+        $this->em->persist($sport);
+        $this->em->persist($published);
+        $this->em->flush();
+        $this->em->clear();
+        
+        $sport = $repo->findOneByTitle('sport forced');
+        $this->assertEquals(
+            '2000-01-01 12:00:00', 
+            $sport->getPublished()->format('Y-m-d H:i:s')
+        );
+    }
+}