|
|
@ -3,12 +3,17 @@ |
|
|
|
namespace Drupal\Tests\islandora\Kernel; |
|
|
|
namespace Drupal\Tests\islandora\Kernel; |
|
|
|
|
|
|
|
|
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
|
|
use Drupal\Core\Url; |
|
|
|
use Drupal\file\FileInterface; |
|
|
|
|
|
|
|
use Drupal\media\MediaInterface; |
|
|
|
use Drupal\islandora\GeminiLookup; |
|
|
|
use Drupal\islandora\GeminiLookup; |
|
|
|
|
|
|
|
use Drupal\islandora\MediaSource\MediaSourceService; |
|
|
|
use Drupal\jwt\Authentication\Provider\JwtAuth; |
|
|
|
use Drupal\jwt\Authentication\Provider\JwtAuth; |
|
|
|
|
|
|
|
use GuzzleHttp\Client; |
|
|
|
|
|
|
|
use GuzzleHttp\Psr7\Response; |
|
|
|
use Islandora\Crayfish\Commons\Client\GeminiClient; |
|
|
|
use Islandora\Crayfish\Commons\Client\GeminiClient; |
|
|
|
use Prophecy\Argument; |
|
|
|
use Prophecy\Argument; |
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Class GeminiLookupTest. |
|
|
|
* Class GeminiLookupTest. |
|
|
@ -18,104 +23,233 @@ use Psr\Log\LoggerInterface; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class GeminiLookupTest extends IslandoraKernelTestBase { |
|
|
|
class GeminiLookupTest extends IslandoraKernelTestBase { |
|
|
|
|
|
|
|
|
|
|
|
private $geminiLookup; |
|
|
|
private $jwtAuth; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $logger; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $guzzle; |
|
|
|
|
|
|
|
|
|
|
|
private $geminiClient; |
|
|
|
private $geminiClient; |
|
|
|
|
|
|
|
|
|
|
|
private $jwtAuth; |
|
|
|
private $mediaSource; |
|
|
|
|
|
|
|
|
|
|
|
private $logger; |
|
|
|
private $entity; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $media; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* {@inheritdoc} |
|
|
|
* {@inheritdoc} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function setUp() { |
|
|
|
public function setUp() { |
|
|
|
parent::setUp(); |
|
|
|
parent::setUp(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mock up dummy objects by default. |
|
|
|
$prophecy = $this->prophesize(JwtAuth::class); |
|
|
|
$prophecy = $this->prophesize(JwtAuth::class); |
|
|
|
$prophecy->generateToken()->willReturn("islandora"); |
|
|
|
|
|
|
|
$this->jwtAuth = $prophecy->reveal(); |
|
|
|
$this->jwtAuth = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
$prophecy = $this->prophesize(LoggerInterface::class); |
|
|
|
$prophecy = $this->prophesize(LoggerInterface::class); |
|
|
|
$this->logger = $prophecy->reveal(); |
|
|
|
$this->logger = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prophecy = $this->prophesize(MediaSourceService::class); |
|
|
|
|
|
|
|
$this->mediaSource = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prophecy = $this->prophesize(GeminiClient::class); |
|
|
|
|
|
|
|
$this->geminiClient = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prophecy = $this->prophesize(Client::class); |
|
|
|
|
|
|
|
$this->guzzle = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mock up an entity to use (node in this case). |
|
|
|
|
|
|
|
$prophecy = $this->prophesize(EntityInterface::class); |
|
|
|
|
|
|
|
$prophecy->id()->willReturn(1); |
|
|
|
|
|
|
|
$prophecy->getEntityTypeId()->willReturn('node'); |
|
|
|
|
|
|
|
$prophecy->uuid()->willReturn('abc123'); |
|
|
|
|
|
|
|
$this->entity = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mock up a media to use. |
|
|
|
|
|
|
|
$prophecy = $this->prophesize(MediaInterface::class); |
|
|
|
|
|
|
|
$prophecy->id()->willReturn(1); |
|
|
|
|
|
|
|
$prophecy->getEntityTypeId()->willReturn('media'); |
|
|
|
|
|
|
|
$prophecy->uuid()->willReturn('abc123'); |
|
|
|
|
|
|
|
$this->media = $prophecy->reveal(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Mocks up a gemini client that fails its lookup. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function mockGeminiClientForFail() { |
|
|
|
|
|
|
|
$prophecy = $this->prophesize(GeminiClient::class); |
|
|
|
|
|
|
|
$prophecy->getUrls(Argument::any(), Argument::any()) |
|
|
|
|
|
|
|
->willReturn([]); |
|
|
|
|
|
|
|
$this->geminiClient = $prophecy->reveal(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Mocks up a gemini client that finds a fedora url. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function mockGeminiClientForSuccess() { |
|
|
|
$prophecy = $this->prophesize(GeminiClient::class); |
|
|
|
$prophecy = $this->prophesize(GeminiClient::class); |
|
|
|
$prophecy->findByUri(Argument::any(), Argument::any())->willReturn(NULL); |
|
|
|
$prophecy->getUrls(Argument::any(), Argument::any()) |
|
|
|
|
|
|
|
->willReturn(['drupal' => '', 'fedora' => 'http://localhost:8080/fcrepo/rest/abc123']); |
|
|
|
$this->geminiClient = $prophecy->reveal(); |
|
|
|
$this->geminiClient = $prophecy->reveal(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Mocks up a media source service that finds the source file for a media. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function mockMediaSourceForSuccess() { |
|
|
|
|
|
|
|
$prophecy = $this->prophesize(FileInterface::class); |
|
|
|
|
|
|
|
$prophecy->uuid()->willReturn('abc123'); |
|
|
|
|
|
|
|
$file = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prophecy = $this->prophesize(MediaSourceService::class); |
|
|
|
|
|
|
|
$prophecy->getSourceFile(Argument::any()) |
|
|
|
|
|
|
|
->willReturn($file); |
|
|
|
|
|
|
|
$this->mediaSource = $prophecy->reveal(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Make the gemini lookup out of class variables. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private function createGeminiLookup() { |
|
|
|
|
|
|
|
return new GeminiLookup( |
|
|
|
|
|
|
|
$this->geminiClient, |
|
|
|
|
|
|
|
$this->jwtAuth, |
|
|
|
|
|
|
|
$this->mediaSource, |
|
|
|
|
|
|
|
$this->guzzle, |
|
|
|
|
|
|
|
$this->logger |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @covers ::lookup |
|
|
|
* @covers ::lookup |
|
|
|
* @covers ::__construct |
|
|
|
* @covers ::__construct |
|
|
|
* @throws \Drupal\Core\Entity\EntityMalformedException |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function testEntityNotSaved() { |
|
|
|
public function testEntityNotSaved() { |
|
|
|
|
|
|
|
// Mock an entity that returns a null id. |
|
|
|
|
|
|
|
// That means it's not saved in the db yet. |
|
|
|
$prophecy = $this->prophesize(EntityInterface::class); |
|
|
|
$prophecy = $this->prophesize(EntityInterface::class); |
|
|
|
$prophecy->id()->willReturn(NULL); |
|
|
|
$prophecy->id()->willReturn(NULL); |
|
|
|
$entity = $prophecy->reveal(); |
|
|
|
$this->entity = $prophecy->reveal(); |
|
|
|
$this->geminiLookup = new GeminiLookup( |
|
|
|
|
|
|
|
$this->geminiClient, |
|
|
|
$gemini_lookup = $this->createGeminiLookup(); |
|
|
|
$this->jwtAuth, |
|
|
|
|
|
|
|
$this->logger |
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
|
NULL, |
|
|
|
|
|
|
|
$gemini_lookup->lookup($this->entity) |
|
|
|
); |
|
|
|
); |
|
|
|
$this->assertEquals(NULL, $this->geminiLookup->lookup($entity)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @covers ::lookup |
|
|
|
* @covers ::lookup |
|
|
|
* @covers ::__construct |
|
|
|
* @covers ::__construct |
|
|
|
* @throws \Drupal\Core\Entity\EntityMalformedException |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function testEntityNotFound() { |
|
|
|
public function testEntityNotFound() { |
|
|
|
$prop1 = $this->prophesize(Url::class); |
|
|
|
$this->mockGeminiClientForFail(); |
|
|
|
$prop1->toString()->willReturn("http://localhost:8000/node/456"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prop2 = $this->prophesize(Url::class); |
|
|
|
|
|
|
|
$prop2->setAbsolute()->willReturn($prop1->reveal()); |
|
|
|
|
|
|
|
$url = $prop2->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prophecy = $this->prophesize(EntityInterface::class); |
|
|
|
$gemini_lookup = $this->createGeminiLookup(); |
|
|
|
$prophecy->id()->willReturn(456); |
|
|
|
|
|
|
|
$prophecy->toUrl()->willReturn($url); |
|
|
|
|
|
|
|
$entity = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->geminiLookup = new GeminiLookup( |
|
|
|
$this->assertEquals( |
|
|
|
$this->geminiClient, |
|
|
|
NULL, |
|
|
|
$this->jwtAuth, |
|
|
|
$gemini_lookup->lookup($this->entity) |
|
|
|
$this->logger |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(NULL, $this->geminiLookup->lookup($entity)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @covers ::lookup |
|
|
|
* @covers ::lookup |
|
|
|
* @covers ::__construct |
|
|
|
* @covers ::__construct |
|
|
|
* @throws \Drupal\Core\Entity\EntityMalformedException |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function testEntityFound() { |
|
|
|
public function testEntityFound() { |
|
|
|
$prop1 = $this->prophesize(Url::class); |
|
|
|
$this->mockGeminiClientForSuccess(); |
|
|
|
$prop1->toString()->willReturn("http://localhost:8000/node/456"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prop2 = $this->prophesize(Url::class); |
|
|
|
$gemini_lookup = $this->createGeminiLookup(); |
|
|
|
$prop2->setAbsolute()->willReturn($prop1->reveal()); |
|
|
|
|
|
|
|
$url = $prop2->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$prophecy = $this->prophesize(EntityInterface::class); |
|
|
|
$this->assertEquals( |
|
|
|
$prophecy->id()->willReturn(456); |
|
|
|
'http://localhost:8080/fcrepo/rest/abc123', |
|
|
|
$prophecy->toUrl()->willReturn($url); |
|
|
|
$gemini_lookup->lookup($this->entity) |
|
|
|
$entity = $prophecy->reveal(); |
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$prophecy = $this->prophesize(GeminiClient::class); |
|
|
|
/** |
|
|
|
$prophecy->findByUri(Argument::any(), Argument::any())->willReturn(["http://fedora:8080/some/uri"]); |
|
|
|
* @covers ::lookup |
|
|
|
$this->geminiClient = $prophecy->reveal(); |
|
|
|
* @covers ::__construct |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function testMediaHasNoSourceFile() { |
|
|
|
|
|
|
|
// Mock a media source service that fails to find |
|
|
|
|
|
|
|
// the source file for a media. |
|
|
|
|
|
|
|
$prophecy = $this->prophesize(MediaSourceService::class); |
|
|
|
|
|
|
|
$prophecy->getSourceFile(Argument::any()) |
|
|
|
|
|
|
|
->willThrow(new NotFoundHttpException("Media has no source")); |
|
|
|
|
|
|
|
$this->mediaSource = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$gemini_lookup = $this->createGeminiLookup(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
|
NULL, |
|
|
|
|
|
|
|
$gemini_lookup->lookup($this->media) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->geminiLookup = new GeminiLookup( |
|
|
|
/** |
|
|
|
$this->geminiClient, |
|
|
|
* @covers ::lookup |
|
|
|
$this->jwtAuth, |
|
|
|
* @covers ::__construct |
|
|
|
$this->logger |
|
|
|
*/ |
|
|
|
|
|
|
|
public function testMediaNotFound() { |
|
|
|
|
|
|
|
$this->mockMediaSourceForSuccess(); |
|
|
|
|
|
|
|
$this->mockGeminiClientForFail(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$gemini_lookup = $this->createGeminiLookup(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
|
NULL, |
|
|
|
|
|
|
|
$gemini_lookup->lookup($this->media) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @covers ::lookup |
|
|
|
|
|
|
|
* @covers ::__construct |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function testFileFoundButNoDescribedby() { |
|
|
|
|
|
|
|
$this->mockMediaSourceForSuccess(); |
|
|
|
|
|
|
|
$this->mockGeminiClientForSuccess(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mock up a guzzle client that does not return |
|
|
|
|
|
|
|
// the describedby header. |
|
|
|
|
|
|
|
$prophecy = $this->prophesize(Client::class); |
|
|
|
|
|
|
|
$prophecy->head(Argument::any(), Argument::any()) |
|
|
|
|
|
|
|
->willReturn(new Response(200, [])); |
|
|
|
|
|
|
|
$this->guzzle = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$gemini_lookup = $this->createGeminiLookup(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
|
NULL, |
|
|
|
|
|
|
|
$gemini_lookup->lookup($this->media) |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals("http://fedora:8080/some/uri", $this->geminiLookup->lookup($entity)); |
|
|
|
/** |
|
|
|
|
|
|
|
* @covers ::lookup |
|
|
|
|
|
|
|
* @covers ::__construct |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function testMediaFound() { |
|
|
|
|
|
|
|
$this->mockMediaSourceForSuccess(); |
|
|
|
|
|
|
|
$this->mockGeminiClientForSuccess(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mock up a guzzle client that returns |
|
|
|
|
|
|
|
// the describedby header. |
|
|
|
|
|
|
|
$prophecy = $this->prophesize(Client::class); |
|
|
|
|
|
|
|
$prophecy->head(Argument::any(), Argument::any()) |
|
|
|
|
|
|
|
->willReturn(new Response(200, ['Link' => '<http://localhost:8080/fcrepo/rest/abc123/fcr:metadata>; rel="describedby"'])); |
|
|
|
|
|
|
|
$this->guzzle = $prophecy->reveal(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$gemini_lookup = $this->createGeminiLookup(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
|
'http://localhost:8080/fcrepo/rest/abc123/fcr:metadata', |
|
|
|
|
|
|
|
$gemini_lookup->lookup($this->media) |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|