Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versioning #75

Merged
merged 18 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ matrix:
branches:
only:
- /master/
- /^dev/

before_install:
- export SCRIPT_DIR=$HOME/CLAW/.scripts
Expand Down
81 changes: 81 additions & 0 deletions src/FedoraApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
namespace Islandora\Chullo;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response;
use \RuntimeException;

/**
* Default implementation of IFedoraApi using Guzzle.
Expand Down Expand Up @@ -305,4 +307,83 @@ public function getGraph(ResponseInterface $response)
}
return $graph;
}

/**
* Creates version in Fedora.
* @param string $uri Fedora Resource URI
* @param string $timestamp Timestamp for Memento version
* @param string $content String or binary content
* @param array $header HTTP Headers
*
* @return ResponseInterface
*/
public function createVersion(
$uri = '',
$timestamp = '',
$content = null,
$headers = []
) {
$timemap_uri = $this->getTimemapURI($uri, $headers);
if ($timemap_uri == null) {
throw new \RuntimeException('Timemap URI is null, cannot create version');
}
$options = ['http_errors' => false];
if ($timestamp != '' && $content != null) {
$headers['Memento-Datetime'] = $timestamp;
$options['body'] = $content;
}
$options['headers'] = $headers;

return $this->client->request(
'POST',
$timemap_uri,
$options
);
}

/**
* Gets list of versions in Fedora.
* @param string $uri Fedora Resource URI
* @param array $header HTTP Headers
*
* @return ResponseInterface
*/
public function getVersions(
$uri = '',
$headers = []
) {
$timemap_uri = $this->getTimemapURI($uri, $headers);
if ($timemap_uri == null) {
throw new \RuntimeException('Timemap URI is null, cannot create version');
}
$options = ['http_errors' => false, 'headers' => $headers];
return $this->client->request(
'GET',
$timemap_uri,
$options
);
}

/**
* Helper method to get the Headers for a resource
* and parse the timemap header from it
* @param string $uri Fedora Resource URI
* @param array $header HTTP Headers
*
* @return string
*/
public function getTimemapURI(
$uri = '',
$headers = []
) {
$resource_headers = $this->getResourceHeaders($uri, $headers);
$parsed_link_headers = Psr7\parse_header($resource_headers->getHeader('Link'));
$timemap_uri = null;
$timemap_index = array_search('timemap', array_column($parsed_link_headers, 'rel'));
if (is_int($timemap_index)) {
$timemap_uri = $parsed_link_headers[$timemap_index][0];
$timemap_uri = trim($timemap_uri, "<> \t\n\r\0\x0B");
}
return $timemap_uri;
}
}
27 changes: 27 additions & 0 deletions src/IFedoraApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,31 @@ public function saveGraph(
$uri = "",
$headers = []
);

/**
* Creates a version of the resource in Fedora.
*
* @param string $uri Resource URI
* @param string $timestamp Timestamp in RFC-1123 format
* @param array $headers HTTP Headers
* @return \Psr\Http\Message\ResponseInterface
*/
public function createVersion(
$uri = "",
$timestamp = "",
$content = null,
$headers = []
);

/**
* Creates a version of the resource in Fedora.
*
* @param string $uri Resource URI
* @param array $headers HTTP Headers
* @return \Psr\Http\Message\ResponseInterface
*/
public function getVersions(
$uri = "",
$headers = []
);
}
52 changes: 52 additions & 0 deletions test/CreateVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Islandora\Chullo;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Islandora\Chullo\FedoraApi;
use \RuntimeException;

class CreateVersionTest extends \PHPUnit_Framework_TestCase
{

/**
* @covers Islandora\Chullo\FedoraApi::modifyResource
* @uses GuzzleHttp\Client
*/
public function testReturns201withVersions()
{
$mock = new MockHandler(
[
new Response(200, ['Link' => '<http://localhost:8080/rest/path/to/resource/fcr:versions>;rel="timemap"']),
new Response(201, ['Location' => "SOME URI"])
]
);

$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler]);
$api = new FedoraApi($guzzle);

$result = $api->createVersion('');
$this->assertEquals(201, $result->getStatusCode());
}

public function testThrowsExceptionWithoutTimemapUri()
{
$mock = new MockHandler(
[
new Response(200, []),
new Response(201, ['Location' => "SOME URI"])
]
);

$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler]);
$api = new FedoraApi($guzzle);

$this->expectException(\RuntimeException::class);
$result = $api->createVersion('');
}
}
44 changes: 44 additions & 0 deletions test/GetTimemapURITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Islandora\Chullo;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Islandora\Chullo\FedoraApi;

class GetTimemapURITest extends \PHPUnit_Framework_TestCase
{

/**
* @covers Islandora\Chullo\FedoraApi::getTimemapURI
* @uses GuzzleHttp\Client
*/
public function testReturnsTimemapHeaderOn200()
{

$headers = [
'Status' => '200 OK',
'ETag' => "bbdd92e395800153a686773f773bcad80a51f47b",
'Last-Modified' => 'Wed, 28 May 2014 18:31:36 GMT',
'Link' => '<http://www.w3.org/ns/ldp#Resource>;rel="type"',
'Link' => '<http://www.w3.org/ns/ldp#Container>;rel="type"',
'Link' => '<http://localhost:8080/rest/path/to/resource/fcr:versions>;rel="timemap"',
];

$mock = new MockHandler(
[
new Response(200, $headers)
]
);

$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler]);
$api = new FedoraApi($guzzle);

$timemapuri = $api->getTimemapURI("");

$this->assertEquals("http://localhost:8080/rest/path/to/resource/fcr:versions", $timemapuri);
}
}
61 changes: 61 additions & 0 deletions test/GetVersionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Islandora\Chullo;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use Islandora\Chullo\FedoraApi;
use \RuntimeException;

class GetVersionsTest extends \PHPUnit_Framework_TestCase
{

/**
* @covers Islandora\Chullo\FedoraApi::getVersions
* @uses GuzzleHttp\Client
*/
public function testReturnsVersionsOn200()
{

$headers = [
'Status' => '200 OK',
'Link' => '<http://localhost:8080/rest/path/to/resource/fcr:versions>;rel="timemap"'
];

$mock = new MockHandler(
[
new Response(200, $headers),
new Response(200, $headers)
]
);

$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler]);
$api = new FedoraApi($guzzle);

$result = $api->getVersions();

$this->assertEquals(200, $result->getStatusCode());
}

public function testThrowErrorWithNoTimemapURI()
{
$headers = [
'Status' => '200 OK'
];

$mock = new MockHandler(
[
new Response(200, $headers)
]
);
$handler = HandlerStack::create($mock);
$guzzle = new Client(['handler' => $handler]);
$api = new FedoraApi($guzzle);

$this->expectException(\RuntimeException::class);
$result = $api->getVersions();
}
}