Skip to content

Commit

Permalink
Configurable domain (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannylamb authored Dec 2, 2020
1 parent 937e394 commit afdb8d0
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 14 deletions.
6 changes: 5 additions & 1 deletion Gemini/cfg/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

debug: false

fedora_base_url: http://localhost:8080/fcrepo/rest
# Leave these blank unless you know what you're doing. If you're moving
# data from one server to another and the domain changes, set these
# to your new domain.
fedora_domain:
drupal_domain:

db.options:
driver: pdo_mysql
Expand Down
6 changes: 3 additions & 3 deletions Gemini/src/Controller/GeminiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class GeminiController

/**
* GeminiController constructor.
* @param \Islandora\Gemini\UrlMapper\UrlMapperInterface
* @param \Islandora\Gemini\UrlMinter\UrlMinterInterface
* @param \Symfony\Component\Routing\Generator\UrlGenerator
* @param \Islandora\Gemini\UrlMapper\UrlMapperInterface $urlMapper
* @param \Islandora\Gemini\UrlMinter\UrlMinterInterface $urlMinter
* @param \Symfony\Component\Routing\Generator\UrlGenerator $urlGenerator
*/
public function __construct(
UrlMapperInterface $urlMapper,
Expand Down
69 changes: 62 additions & 7 deletions Gemini/src/UrlMapper/UrlMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,30 @@ class UrlMapper implements UrlMapperInterface
*/
protected $connection;

/**
* @var string
*/
protected $drupalDomain;

/**
* @var string
*/
protected $fedoraDomain;

/**
* UrlMapper constructor.
* @param \Doctrine\DBAL\Connection $connection
* @param string $drupalDomain
* @param string $fedoraDomain
*/
public function __construct(Connection $connection)
{
public function __construct(
Connection $connection,
$drupalDomain = "",
$fedoraDomain = ""
) {
$this->connection = $connection;
$this->drupalDomain = $drupalDomain;
$this->fedoraDomain = $fedoraDomain;
}

/**
Expand All @@ -32,10 +49,26 @@ public function __construct(Connection $connection)
public function getUrls($uuid)
{
$sql = 'SELECT drupal_uri as drupal, fedora_uri as fedora FROM Gemini WHERE uuid = :uuid';
return $this->connection->fetchAssoc(
$result = $this->connection->fetchAssoc(
$sql,
['uuid' => $uuid]
);

if (!empty($this->drupalDomain) && isset($result['drupal'])) {
$result['drupal'] = $this->replaceDomain($result['drupal'], $this->drupalDomain);
}

if (!empty($this->fedoraDomain) && isset($result['fedora'])) {
$result['fedora'] = $this->replaceDomain($result['fedora'], $this->fedoraDomain);
}

return $result;
}

protected function replaceDomain($url, $domain)
{
$parts = parse_url($url);
return "$parts[scheme]://$domain$parts[path]";
}

/**
Expand Down Expand Up @@ -108,12 +141,34 @@ public function deleteUrls($uuid)
*/
public function findUrls($uri)
{
$parts = parse_url($uri);
$path = $parts['path'];

$query =
'SELECT fedora_uri as uri FROM Gemini WHERE drupal_uri = :uri union
SELECT drupal_uri as uri FROM Gemini WHERE fedora_uri = :uri';
return $this->connection->fetchAssoc(
'SELECT fedora_uri FROM Gemini WHERE drupal_uri LIKE :path union
SELECT drupal_uri FROM Gemini WHERE fedora_uri LIKE :path';

$result = $this->connection->fetchAssoc(
$query,
['uri' => $uri]
['path' => "%$path"]
);

if (isset($result['fedora_uri'])) {
if (!empty($this->fedoraDomain)) {
$result['fedora_uri'] = $this->replaceDomain($result['fedora_uri'], $this->fedoraDomain);
}
$result['uri'] = $result['fedora_uri'];
unset($result['fedora_uri']);
}

if (isset($result['drupal_uri'])) {
if (!empty($this->drupalDomain)) {
$result['drupal_uri'] = $this->replaceDomain($result['drupal_uri'], $this->drupalDomain);
}
$result['uri'] = $result['drupal_uri'];
unset($result['drupal_uri']);
}

return $result;
}
}
6 changes: 5 additions & 1 deletion Gemini/src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
$app->register(new YamlConfigServiceProvider(__DIR__ . '/../cfg/config.yaml'));
$app['debug'] = $app['crayfish.debug'];
$app['gemini.mapper'] = function ($app) {
return new UrlMapper($app['db']);
return new UrlMapper(
$app['db'],
$app['crayfish.drupal_domain'],
$app['crayfish.fedora_domain']
);
};
$app['gemini.minter'] = function ($app) {
return new UrlMinter();
Expand Down
91 changes: 89 additions & 2 deletions Gemini/tests/Islandora/Gemini/Tests/UrlMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UrlMapperTest extends \PHPUnit_Framework_TestCase
* @covers ::__construct
* @covers ::getUrls
*/
public function testGetUrlsReturnsUnmodifiedResults()
public function testGetUrlsReturnsUnmodifiedResultsIfNotConfigured()
{
// Simulate a record being returned.
$connection = $this->prophesize(Connection::class);
Expand Down Expand Up @@ -56,6 +56,49 @@ public function testGetUrlsReturnsUnmodifiedResults()
);
}

/**
* @covers ::__construct
* @covers ::getUrls
*/
public function testGetUrlsReturnsModifiedResultsIfConfigured()
{
// Simulate a record being returned.
$connection = $this->prophesize(Connection::class);
$connection->fetchAssoc(Argument::any(), Argument::any())
->willReturn(['fedora' => 'http://exapmle.org/foo', 'drupal' => 'http://example.org/bar']);
$connection = $connection->reveal();

$mapper = new UrlMapper($connection, 'drupal.example.org', 'fcrepo.example.org');

$results = $mapper->getUrls("abc");

$this->assertTrue(
$results['fedora'] == 'http://fcrepo.example.org/foo',
"getUrls() disobeyed configuration. Actual: ${results['fedora']}. Expected: " .
"http://fcrepo.example.org/foo"
);
$this->assertTrue(
$results['drupal'] == 'http://drupal.example.org/bar',
"getUrls() modified connection results. Actual: ${results['drupal']}. Expected: " .
"http://drupal.example.org/bar"
);

// Simulate when no record is found.
$connection = $this->prophesize(Connection::class);
$connection->fetchAssoc(Argument::any(), Argument::any())
->willReturn([]);
$connection = $connection->reveal();

$mapper = new UrlMapper($connection);

$results = $mapper->getUrls("abc");

$this->assertTrue(
empty($results),
"getUrls() modified connection results. Expected empty array, received " . json_encode($results)
);
}

/**
* @covers ::__construct
* @covers ::saveUrls
Expand Down Expand Up @@ -187,7 +230,7 @@ public function testDeleteUrlsRollsBackOnException()
/**
* @covers ::findUrls
*/
public function testFindUrls()
public function testFindUrlsReturnsUnmodifiedIfNotConfigured()
{
// Simulate a record being returned.
$connection = $this->prophesize(Connection::class);
Expand All @@ -212,4 +255,48 @@ public function testFindUrls()
"getUrls() modified connection results. Expected empty array, received " . json_encode($results)
);
}

/**
* @covers ::findUrls
*/
public function testFindUrlsReturnsModifiedIfConfigured()
{
// Simulate a record being returned for Fedora.
$connection = $this->prophesize(Connection::class);
$connection->fetchAssoc(Argument::any(), Argument::any())
->willReturn(['fedora_uri' => 'http://example.org/foo']);
$connection = $connection->reveal();
$mapper = new UrlMapper($connection, 'drupal.example.org', 'fcrepo.example.org');
$results = $mapper->findUrls("abc");
$this->assertTrue(
$results['uri'] == 'http://fcrepo.example.org/foo',
"getUrls() did not modify connection results. Actual: ${results['uri']}. Expected: " .
"http://fcrepo.example.org/foo"
);

// Simulate a record being returned for Drupal.
$connection = $this->prophesize(Connection::class);
$connection->fetchAssoc(Argument::any(), Argument::any())
->willReturn(['drupal_uri' => 'http://example.org/bar']);
$connection = $connection->reveal();
$mapper = new UrlMapper($connection, 'drupal.example.org', 'fcrepo.example.org');
$results = $mapper->findUrls("abc");
$this->assertTrue(
$results['uri'] == 'http://drupal.example.org/bar',
"getUrls() did not modify connection results. Actual: ${results['uri']}. Expected: " .
"http://drupal.example.org/bar"
);

// Simulate when no record is found.
$connection = $this->prophesize(Connection::class);
$connection->fetchAssoc(Argument::any(), Argument::any())
->willReturn([]);
$connection = $connection->reveal();
$mapper = new UrlMapper($connection, 'drupal.example.org', 'fcrepo.example.org');
$results = $mapper->findUrls("abc");
$this->assertTrue(
empty($results),
"getUrls() modified connection results. Expected empty array, received " . json_encode($results)
);
}
}

0 comments on commit afdb8d0

Please sign in to comment.