Skip to content

Commit

Permalink
Versioning (#75)
Browse files Browse the repository at this point in the history
* add createVersion method and interface

* add a getVersions method, instead of passing in the versioning URI, pass in the resource URI

* get timemap uri from headers

* move getting the timemap uri from the headers so a separate function so we don't have to repeat it in createVersion and getVersion

* allow for content to be passed for createversion method

* update docs to specify datetime format

* simplifying/improving to use trim instead of str_replace and array_search instead of a loop and if statement

* phpcs fixes, adding test for getTimemapURI, and making it public

* other phpcs fixes

* move trim inside if, makes more sense

* check for null timemapURI and check body and timestamp together

* rename test

* add getVersions test

* add createVersion test

* add dev branch to travis

* update createVersion test

* adding test for getBaseUri and adding doc comments

* adding optional params for createVersion test to be sure that it tests that scenario also
  • Loading branch information
elizoller authored and whikloj committed Jun 20, 2019
1 parent fc60e9c commit f2fb0f5
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 0 deletions.
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 = []
);
}
59 changes: 59 additions & 0 deletions test/CreateVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Islandora\Chullo;

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

class CreateVersionTest extends \PHPUnit_Framework_TestCase
{

/**
* @covers Islandora\Chullo\FedoraApi::createVersion
* @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);
$date = new DateTime();
$timestamp = $date->format("D, d M Y H:i:s O");
$content = "test";
$result = $api->createVersion('', $timestamp, $content);
$this->assertEquals(201, $result->getStatusCode());
}

/**
* @covers Islandora\Chullo\FedoraApi::createVersion Exception
* @uses GuzzleHttp\Client
*/
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('');
}
}
26 changes: 26 additions & 0 deletions test/GetBaseUriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Islandora\Chullo;

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

class GetBaseUriTest extends \PHPUnit_Framework_TestCase
{

/**
* @covers Islandora\Chullo\FedoraApi::getBaseUri
* @uses GuzzleHttp\Client
*/
public function testReturnsUri()
{
$guzzle = new Client(['base_uri'=>'http://localhost:8080/fcrepo/rest']);
$api = new FedoraApi($guzzle);

$baseUri = $api->getBaseUri();
$this->assertEquals($baseUri, 'http://localhost:8080/fcrepo/rest');
}
}
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);
}
}
65 changes: 65 additions & 0 deletions test/GetVersionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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());
}

/**
* @covers Islandora\Chullo\FedoraApi::getVersions Exception
* @uses GuzzleHttp\Client
*/
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();
}
}

0 comments on commit f2fb0f5

Please sign in to comment.