Skip to content
Permalink
Browse files

Added methods to update last-modified timestamp to DB interface (#598)

This moves the responsibility for updating the last-modified timestamp away from the update-metadata functionality,
and makes it into an explicit action performed by the database-operations event-listener

This is in reference to #530, where the behaviour between the Doctrine and Mongo implementation were different.
  • Loading branch information...
fangel authored and christeredvartsen committed May 24, 2018
1 parent ffa6ab9 commit 1a0a9684f421f6eab307add6d1acca348c66e25e
@@ -131,6 +131,28 @@ function getImageProperties($user, $imageIdentifier);
*/
function getLastModified(array $users, $imageIdentifier = null);
/**
* Update the last modified timestamp for a given image to now.
*
* @param string $user The user the image belongs to
* @param string $imageIdentifier The image identifier
* @return DateTime The date the timestamp was updated to
*/
function setLastModifiedNow($user, $imageIdentifier);
/**
* Update the last modified timestamp for a given image
*
* Will find and modify the last modified timestamp for an image belonging
* to a certain user to the given timestamp.
*
* @param string $user The user the image belongs to
* @param string $imageIdentifier The image identifier
* @param DateTime $time The timestamp to set last modified to
* @return DateTime The date the timestamp was updated to
*/
function setLastModifiedTime($user, $imageIdentifier, DateTime $time);
/**
* Fetch the number of images, optionally filtered by a given user
*
@@ -449,6 +449,35 @@ public function getLastModified(array $users, $imageIdentifier = null) {
return new DateTime('@' . $row['updated'], new DateTimeZone('UTC'));
}
/**
* {@inheritdoc}
*/
public function setLastModifiedNow($user, $imageIdentifier) {
return $this->setLastModifiedTime($user, $imageIdentifier, new DateTime('@' . time(), new DateTimeZone('UTC')));
}
/**
* {@inheritdoc}
*/
public function setLastModifiedTime($user, $imageIdentifier, DateTime $time) {
// Fetch the current connection
$connection = $this->getConnection();
if (!$imageId = $this->getImageId($user, $imageIdentifier)) {
throw new DatabaseException('Image not found', 404);
}
$update = $connection->createQueryBuilder();
$update->update($this->tableNames['imageinfo'])
->set('updated', $time->getTimestamp())
->where('id = :id')
->setParameters([
':id' => $imageId,
])->execute();
return $time;
}
/**
* {@inheritdoc}
*/
@@ -209,7 +209,7 @@ public function updateMetadata($user, $imageIdentifier, array $metadata) {
$this->getImageCollection()->updateOne(
['user' => $user, 'imageIdentifier' => $imageIdentifier],
['$set' => ['updated' => time(), 'metadata' => $updatedMetadata]]
['$set' => ['metadata' => $updatedMetadata]]
);
} catch (MongoException $e) {
throw new DatabaseException('Unable to update meta data', 500, $e);
@@ -435,6 +435,34 @@ public function getLastModified(array $users, $imageIdentifier = null) {
return new DateTime('@' . $data['updated'], new DateTimeZone('UTC'));
}
/**
* {@inheritdoc}
*/
public function setLastModifiedNow($user, $imageIdentifier) {
return $this->setLastModifiedTime($user, $imageIdentifier, new DateTime('@' . time(), new DateTimeZone('UTC')));
}
/**
* {@inheritdoc}
*/
public function setLastModifiedTime($user, $imageIdentifier, DateTime $time) {
$data = $this->getImageCollection()->findOne([
'user' => $user,
'imageIdentifier' => $imageIdentifier,
]);
if ($data === null) {
throw new DatabaseException('Image not found', 404);
}
$this->getImageCollection()->updateOne(
['user' => $user, 'imageIdentifier' => $imageIdentifier],
['$set' => ['updated' => $time->getTimestamp()]]
);
return $time;
}
/**
* {@inheritdoc}
*/
@@ -132,6 +132,11 @@ public function deleteMetadata(EventInterface $event) {
$request->getUser(),
$request->getImageIdentifier()
);
$event->getDatabase()->setLastModifiedNow(
$request->getUser(),
$request->getImageIdentifier()
);
}
/**
@@ -147,6 +152,11 @@ public function updateMetadata(EventInterface $event) {
$request->getImageIdentifier(),
$event->getArgument('metadata')
);
$event->getDatabase()->setLastModifiedNow(
$request->getUser(),
$request->getImageIdentifier()
);
}
/**
@@ -335,6 +335,75 @@ public function testCanGetLastModifiedDate(array $images, array $users, $imageId
}
}
/**
* @covers ::setLastModifiedNow
*/
public function testCanSetLastModifiedDateToNow() {
$user = 'user';
$imageIdentifier = 'id';
$original = $this->getImage();
$added = time() - 10;
$original->setAddedDate(new DateTime('@' . $added, new DateTimeZone('UTC')));
$original->setUpdatedDate(new DateTime('@' . $added, new DateTimeZone('UTC')));
$this->assertTrue(
$this->adapter->insertImage($user, $imageIdentifier, $original),
'Could not insert image'
);
$now = $this->adapter->setLastModifiedNow($user, $imageIdentifier);
$this->assertEquals(time(), $now->getTimestamp(), 'Returned timestamp should be around now', 1);
$image = new Image();
$this->assertTrue($this->adapter->load($user, $imageIdentifier, $image));
$this->assertEquals($added, $image->getAddedDate()->getTimestamp(), 'Added timestamp should not be modified');
$this->assertEquals($now->getTimestamp(), $image->getUpdatedDate()->getTimestamp(), 'Updated timestamp should have updated');
$lastModified = $this->adapter->getLastModified([$user], $imageIdentifier);
$this->assertEquals($now->getTimestamp(), $lastModified->getTimestamp(), 'Last timestamp should have updated');
}
/**
* @covers ::setLastModifiedTime
*/
public function testCanSetLastModifiedDateToTimestamp() {
$user = 'user';
$imageIdentifier = 'id';
$this->assertTrue(
$this->adapter->insertImage($user, $imageIdentifier, $this->getImage()),
'Could not insert image'
);
$desired = new DateTime('@' . (time() + 10), new DateTimeZone('UTC'));
$returned = $this->adapter->setLastModifiedTime($user, $imageIdentifier, $desired);
$this->assertEquals($desired->getTimestamp(), $returned->getTimestamp(), 'Returned timestamp should be around now');
$image = new Image();
$this->assertTrue($this->adapter->load($user, $imageIdentifier, $image));
$this->assertEquals($desired->getTimestamp(), $image->getUpdatedDate()->getTimestamp(), 'Updated timestamp should have updated');
$lastModified = $this->adapter->getLastModified([$user], $imageIdentifier);
$this->assertEquals($desired->getTimestamp(), $lastModified->getTimestamp(), 'Last timestamp should have updated');
}
/**
* @expectedException Imbo\Exception\DatabaseException
* @expectedExceptionCode 404
* @expectedExceptionMessage Image not found
* @covers ::setLastModifiedTime
*/
public function testCannotSetLastModifiedDateForMissingImage() {
$user = 'user';
$imageIdentifier = 'id';
$this->adapter->setLastModifiedNow($user, $imageIdentifier);
}
/**
* @covers ::getNumImages
*/
@@ -112,6 +112,7 @@ public function testCanLoadImage() {
*/
public function testCanDeleteMetadata() {
$this->database->expects($this->once())->method('deleteMetadata')->with($this->user, $this->imageIdentifier);
$this->database->expects($this->once())->method('setLastModifiedNow')->with($this->user, $this->imageIdentifier);
$this->listener->deleteMetadata($this->event);
}
@@ -122,6 +123,7 @@ public function testCanDeleteMetadata() {
public function testCanUpdateMetadata() {
$this->event->expects($this->once())->method('getArgument')->with('metadata')->will($this->returnValue(['key' => 'value']));
$this->database->expects($this->once())->method('updateMetadata')->with($this->user, $this->imageIdentifier, ['key' => 'value']);
$this->database->expects($this->once())->method('setLastModifiedNow')->with($this->user, $this->imageIdentifier);
$this->listener->updateMetadata($this->event);
}

0 comments on commit 1a0a968

Please sign in to comment.
You can’t perform that action at this time.