Browse Source
* Flysystem adapter and REST API accepts Content-Location with a stream wrapper * First pass is working now * Config dump * typo in composer.json * Testing install * Tests. Coding standards * Adding token as a depenedency * Unneccessary config * Adding drupal/token to composer.json * Applying the file's uri using the mapping predicate alter. * Tests vs coding standards whack-a-mole * Sneaky image resize bug * Test refactor to quiesce phpcpd. How you like that @bradspry? * _slightly_ better error reporting for tests * Moar test updates * phpcbf to the rescue * Config export * Fixing the Canoncial typo once and for all * Better trimming * Collapsing 'Is' style conditions into a singular 'ContentEntityType' condition * Coding standards * Was referencing is_node in a test * Unbreaking core block placement * Copy pasta fix and removing conditions I obliterated from the schema file.pull/756/head
dannylamb
6 years ago
committed by
Jared Whiklo
67 changed files with 2102 additions and 556 deletions
@ -0,0 +1,19 @@ |
|||||||
|
langcode: en |
||||||
|
status: true |
||||||
|
dependencies: |
||||||
|
config: |
||||||
|
- media.type.audio |
||||||
|
id: media.audio.name |
||||||
|
field_name: name |
||||||
|
entity_type: media |
||||||
|
bundle: audio |
||||||
|
label: Name |
||||||
|
description: '' |
||||||
|
required: true |
||||||
|
translatable: true |
||||||
|
default_value: |
||||||
|
- |
||||||
|
value: '' |
||||||
|
default_value_callback: '' |
||||||
|
settings: { } |
||||||
|
field_type: string |
@ -0,0 +1,19 @@ |
|||||||
|
langcode: en |
||||||
|
status: true |
||||||
|
dependencies: |
||||||
|
config: |
||||||
|
- media.type.file |
||||||
|
id: media.file.name |
||||||
|
field_name: name |
||||||
|
entity_type: media |
||||||
|
bundle: file |
||||||
|
label: Name |
||||||
|
description: '' |
||||||
|
required: true |
||||||
|
translatable: true |
||||||
|
default_value: |
||||||
|
- |
||||||
|
value: '' |
||||||
|
default_value_callback: '' |
||||||
|
settings: { } |
||||||
|
field_type: string |
@ -0,0 +1,19 @@ |
|||||||
|
langcode: en |
||||||
|
status: true |
||||||
|
dependencies: |
||||||
|
config: |
||||||
|
- media.type.image |
||||||
|
id: media.image.name |
||||||
|
field_name: name |
||||||
|
entity_type: media |
||||||
|
bundle: image |
||||||
|
label: Name |
||||||
|
description: '' |
||||||
|
required: true |
||||||
|
translatable: true |
||||||
|
default_value: |
||||||
|
- |
||||||
|
value: '' |
||||||
|
default_value_callback: '' |
||||||
|
settings: { } |
||||||
|
field_type: string |
@ -0,0 +1,19 @@ |
|||||||
|
langcode: en |
||||||
|
status: true |
||||||
|
dependencies: |
||||||
|
config: |
||||||
|
- media.type.video |
||||||
|
id: media.video.name |
||||||
|
field_name: name |
||||||
|
entity_type: media |
||||||
|
bundle: video |
||||||
|
label: Name |
||||||
|
description: '' |
||||||
|
required: true |
||||||
|
translatable: true |
||||||
|
default_value: |
||||||
|
- |
||||||
|
value: '' |
||||||
|
default_value_callback: '' |
||||||
|
settings: { } |
||||||
|
field_type: string |
@ -0,0 +1,5 @@ |
|||||||
|
algos: |
||||||
|
sha1: sha1 |
||||||
|
md5: '0' |
||||||
|
sha256: '0' |
||||||
|
dedupe: false |
@ -0,0 +1,46 @@ |
|||||||
|
langcode: en |
||||||
|
status: true |
||||||
|
dependencies: |
||||||
|
module: |
||||||
|
- islandora |
||||||
|
name: external_media |
||||||
|
label: 'External Media' |
||||||
|
group: Islandora |
||||||
|
description: 'Reactions for media whose source are outside of Fedora' |
||||||
|
requireAllConditions: true |
||||||
|
disabled: false |
||||||
|
conditions: |
||||||
|
media_uses_filesystem: |
||||||
|
id: media_uses_filesystem |
||||||
|
filesystems: |
||||||
|
fedora: fedora |
||||||
|
negate: 1 |
||||||
|
uuid: 4f3c414a-8c94-464c-a4b9-5d3eb2b35e92 |
||||||
|
context_mapping: |
||||||
|
media: '@islandora.media_route_context_provider:media' |
||||||
|
content_entity_type: |
||||||
|
id: content_entity_type |
||||||
|
types: |
||||||
|
media: media |
||||||
|
negate: 0 |
||||||
|
uuid: c10985ef-16ef-4571-89ad-1a0926c83b83 |
||||||
|
context_mapping: |
||||||
|
media: '@islandora.media_route_context_provider:media' |
||||||
|
reactions: |
||||||
|
index: |
||||||
|
id: index |
||||||
|
actions: |
||||||
|
index_media_in_triplestore: index_media_in_triplestore |
||||||
|
index_node_in_fedora: index_node_in_fedora |
||||||
|
saved: false |
||||||
|
delete: |
||||||
|
id: delete |
||||||
|
actions: |
||||||
|
delete_media_from_triplestore: delete_media_from_triplestore |
||||||
|
delete_node_from_fedora: delete_node_from_fedora |
||||||
|
saved: false |
||||||
|
islandora_map_uri_predicate: |
||||||
|
id: islandora_map_uri_predicate |
||||||
|
drupal_uri_predicate: 'schema:sameAs' |
||||||
|
saved: false |
||||||
|
weight: 0 |
@ -1,22 +1,21 @@ |
|||||||
langcode: en |
langcode: en |
||||||
status: true |
status: true |
||||||
dependencies: |
dependencies: |
||||||
enforced: |
|
||||||
module: |
|
||||||
- islandora_demo_feature |
|
||||||
module: |
module: |
||||||
- islandora |
- islandora |
||||||
name: files |
name: files_in_fedora |
||||||
label: Files |
label: 'Fedora Files' |
||||||
group: Islandora |
group: Islandora |
||||||
description: 'All repository files' |
description: 'Files in Fedora' |
||||||
requireAllConditions: false |
requireAllConditions: false |
||||||
disabled: false |
disabled: false |
||||||
conditions: |
conditions: |
||||||
is_file: |
file_uses_filesystem: |
||||||
id: is_file |
id: file_uses_filesystem |
||||||
|
filesystems: |
||||||
|
fedora: fedora |
||||||
negate: 0 |
negate: 0 |
||||||
uuid: 19b1b4e1-ea50-41f8-ab5b-8afb83eb5588 |
uuid: ea9d2661-2dc1-4480-bc9b-3fedeceba5f9 |
||||||
context_mapping: |
context_mapping: |
||||||
file: '@islandora.file_route_context_provider:file' |
file: '@islandora.file_route_context_provider:file' |
||||||
reactions: |
reactions: |
@ -0,0 +1,19 @@ |
|||||||
|
langcode: en |
||||||
|
status: true |
||||||
|
dependencies: |
||||||
|
config: |
||||||
|
- node.type.islandora_object |
||||||
|
id: node.islandora_object.menu_link |
||||||
|
field_name: menu_link |
||||||
|
entity_type: node |
||||||
|
bundle: islandora_object |
||||||
|
label: 'Menu link' |
||||||
|
description: 'Computed menu link for the node (only available during node saving).' |
||||||
|
required: false |
||||||
|
translatable: false |
||||||
|
default_value: { } |
||||||
|
default_value_callback: '' |
||||||
|
settings: |
||||||
|
handler: default |
||||||
|
handler_settings: { } |
||||||
|
field_type: entity_reference |
@ -0,0 +1,17 @@ |
|||||||
|
langcode: en |
||||||
|
status: true |
||||||
|
dependencies: |
||||||
|
config: |
||||||
|
- node.type.islandora_object |
||||||
|
id: node.islandora_object.title |
||||||
|
field_name: title |
||||||
|
entity_type: node |
||||||
|
bundle: islandora_object |
||||||
|
label: Title |
||||||
|
description: '' |
||||||
|
required: true |
||||||
|
translatable: true |
||||||
|
default_value: { } |
||||||
|
default_value_callback: '' |
||||||
|
settings: { } |
||||||
|
field_type: string |
@ -0,0 +1,324 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Flysystem\Adapter; |
||||||
|
|
||||||
|
use Islandora\Chullo\IFedoraApi; |
||||||
|
use League\Flysystem\AdapterInterface; |
||||||
|
use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; |
||||||
|
use League\Flysystem\Adapter\Polyfill\StreamedCopyTrait; |
||||||
|
use League\Flysystem\Config; |
||||||
|
use GuzzleHttp\Psr7; |
||||||
|
use GuzzleHttp\Psr7\Response; |
||||||
|
use GuzzleHttp\Psr7\StreamWrapper; |
||||||
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Fedora adapter for Flysystem. |
||||||
|
*/ |
||||||
|
class FedoraAdapter implements AdapterInterface { |
||||||
|
|
||||||
|
use StreamedCopyTrait; |
||||||
|
use NotSupportingVisibilityTrait; |
||||||
|
|
||||||
|
protected $fedora; |
||||||
|
protected $mimeTypeGuesser; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a Fedora adapter for Flysystem. |
||||||
|
* |
||||||
|
* @param \Islandora\Chullo\IFedoraApi $fedora |
||||||
|
* Fedora client. |
||||||
|
* @param \Symfony\Component\HttpFoundation\File\Mimetype\MimeTypeGuesserInterface $mime_type_guesser |
||||||
|
* Mimetype guesser. |
||||||
|
*/ |
||||||
|
public function __construct(IFedoraApi $fedora, MimeTypeGuesserInterface $mime_type_guesser) { |
||||||
|
$this->fedora = $fedora; |
||||||
|
$this->mimeTypeGuesser = $mime_type_guesser; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function has($path) { |
||||||
|
$response = $this->fedora->getResourceHeaders($path); |
||||||
|
return $response->getStatusCode() == 200; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function read($path) { |
||||||
|
$meta = $this->readStream($path); |
||||||
|
|
||||||
|
if (!$meta) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
if (isset($meta['stream'])) { |
||||||
|
$meta['contents'] = stream_get_contents($meta['stream']); |
||||||
|
fclose($meta['stream']); |
||||||
|
unset($meta['stream']); |
||||||
|
} |
||||||
|
|
||||||
|
return $meta; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function readStream($path) { |
||||||
|
$response = $this->fedora->getResource($path); |
||||||
|
|
||||||
|
if ($response->getStatusCode() != 200) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
$meta = $this->getMetadataFromHeaders($response); |
||||||
|
$meta['path'] = $path; |
||||||
|
|
||||||
|
if ($meta['type'] == 'file') { |
||||||
|
$meta['stream'] = StreamWrapper::getResource($response->getBody()); |
||||||
|
} |
||||||
|
|
||||||
|
return $meta; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function getMetadata($path) { |
||||||
|
$response = $this->fedora->getResourceHeaders($path); |
||||||
|
|
||||||
|
if ($response->getStatusCode() != 200) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
$meta = $this->getMetadataFromHeaders($response); |
||||||
|
$meta['path'] = $path; |
||||||
|
return $meta; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function getSize($path) { |
||||||
|
return $this->getMetadata($path); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function getMimetype($path) { |
||||||
|
return $this->getMetadata($path); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function getTimestamp($path) { |
||||||
|
return $this->getMetadata($path); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets metadata from response headers. |
||||||
|
* |
||||||
|
* @param \GuzzleHttp\Psr7\Response $response |
||||||
|
* Response. |
||||||
|
*/ |
||||||
|
protected function getMetadataFromHeaders(Response $response) { |
||||||
|
$last_modified = \DateTime::createFromFormat( |
||||||
|
\DateTime::RFC1123, |
||||||
|
$response->getHeader('Last-Modified')[0] |
||||||
|
); |
||||||
|
|
||||||
|
// NonRDFSource's are considered files. Everything else is a |
||||||
|
// directory. |
||||||
|
$type = 'dir'; |
||||||
|
$links = Psr7\parse_header($response->getHeader('Link')); |
||||||
|
foreach ($links as $link) { |
||||||
|
if ($link['rel'] == 'type' && $link[0] == '<http://www.w3.org/ns/ldp#NonRDFSource>') { |
||||||
|
$type = 'file'; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
$meta = [ |
||||||
|
'type' => $type, |
||||||
|
'timestamp' => $last_modified->getTimestamp(), |
||||||
|
]; |
||||||
|
|
||||||
|
if ($type == 'file') { |
||||||
|
$meta['size'] = $response->getHeader('Content-Length')[0]; |
||||||
|
$meta['mimetype'] = $response->getHeader('Content-Type')[0]; |
||||||
|
} |
||||||
|
|
||||||
|
return $meta; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function listContents($directory = '', $recursive = FALSE) { |
||||||
|
// Strip leading and trailing whitespace and /'s. |
||||||
|
$normalized = trim($directory, ' \t\n\r\0\x0B/'); |
||||||
|
|
||||||
|
// Exit early if it's a file. |
||||||
|
$meta = $this->getMetadata($normalized); |
||||||
|
if ($meta['type'] == 'file') { |
||||||
|
return []; |
||||||
|
} |
||||||
|
// Get the resource from Fedora. |
||||||
|
$response = $this->fedora->getResource($normalized, ['Accept' => 'application/ld+json']); |
||||||
|
$jsonld = (string) $response->getBody(); |
||||||
|
$graph = json_decode($jsonld, TRUE); |
||||||
|
|
||||||
|
$uri = $this->fedora->getBaseUri() . $normalized; |
||||||
|
|
||||||
|
// Hack it out of the graph. |
||||||
|
// There may be more than one resource returned. |
||||||
|
$resource = []; |
||||||
|
foreach ($graph as $elem) { |
||||||
|
if (isset($elem['@id']) && $elem['@id'] == $uri) { |
||||||
|
$resource = $elem; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Exit early if resource doesn't contain other resources. |
||||||
|
if (!isset($resource['http://www.w3.org/ns/ldp#contains'])) { |
||||||
|
return []; |
||||||
|
} |
||||||
|
|
||||||
|
// Collapse uris to a single array. |
||||||
|
$contained = array_map( |
||||||
|
function ($elem) { |
||||||
|
return $elem['@id']; |
||||||
|
}, |
||||||
|
$resource['http://www.w3.org/ns/ldp#contains'] |
||||||
|
); |
||||||
|
|
||||||
|
// Exit early if not recursive. |
||||||
|
if (!$recursive) { |
||||||
|
// Transform results to their flysystem metadata. |
||||||
|
return array_map( |
||||||
|
[$this, 'transformToMetadata'], |
||||||
|
$contained |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
// Recursively get containment for ancestors. |
||||||
|
$ancestors = []; |
||||||
|
|
||||||
|
foreach ($contained as $child_uri) { |
||||||
|
$child_directory = explode($this->fedora->getBaseUri(), $child_uri)[1]; |
||||||
|
$ancestors = array_merge($this->listContents($child_directory, $recursive), $ancestors); |
||||||
|
} |
||||||
|
|
||||||
|
// // Transform results to their flysystem metadata. |
||||||
|
return array_map( |
||||||
|
[$this, 'transformToMetadata'], |
||||||
|
array_merge($ancestors, $contained) |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Normalizes data for listContents(). |
||||||
|
* |
||||||
|
* @param string $uri |
||||||
|
* Uri. |
||||||
|
*/ |
||||||
|
protected function transformToMetadata($uri) { |
||||||
|
if (is_array($uri)) { |
||||||
|
return $uri; |
||||||
|
} |
||||||
|
$exploded = explode($this->fedora->getBaseUri(), $uri); |
||||||
|
return $this->getMetadata($exploded[1]); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function write($path, $contents, Config $config) { |
||||||
|
$headers = [ |
||||||
|
'Content-Type' => $this->mimeTypeGuesser->guess($path), |
||||||
|
]; |
||||||
|
|
||||||
|
$response = $this->fedora->saveResource( |
||||||
|
$path, |
||||||
|
$contents, |
||||||
|
$headers |
||||||
|
); |
||||||
|
|
||||||
|
$code = $response->getStatusCode(); |
||||||
|
if (!in_array($code, [201, 204])) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
return $this->getMetadata($path); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function writeStream($path, $contents, Config $config) { |
||||||
|
return $this->write($path, $contents, $config); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function update($path, $contents, Config $config) { |
||||||
|
return $this->write($path, $contents, $config); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function updateStream($path, $contents, Config $config) { |
||||||
|
return $this->write($path, $contents, $config); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function delete($path) { |
||||||
|
$response = $this->fedora->deleteResource($path); |
||||||
|
|
||||||
|
$code = $response->getStatusCode(); |
||||||
|
return in_array($code, [204, 404]); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function deleteDir($dirname) { |
||||||
|
return $this->delete($dirname); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function rename($path, $newpath) { |
||||||
|
if ($this->copy($path, $newpath)) { |
||||||
|
return $this->delete($path); |
||||||
|
} |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function createDir($dirname, Config $config) { |
||||||
|
$response = $this->fedora->saveResource( |
||||||
|
$dirname |
||||||
|
); |
||||||
|
|
||||||
|
$code = $response->getStatusCode(); |
||||||
|
if (!in_array($code, [201, 204])) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
return $this->getMetadata($dirname); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,124 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Flysystem; |
||||||
|
|
||||||
|
use Drupal\Core\Logger\RfcLogLevel; |
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
||||||
|
use Drupal\flysystem\Plugin\FlysystemPluginInterface; |
||||||
|
use Drupal\flysystem\Plugin\FlysystemUrlTrait; |
||||||
|
use Drupal\islandora\Flysystem\Adapter\FedoraAdapter; |
||||||
|
use GuzzleHttp\HandlerStack; |
||||||
|
use GuzzleHttp\Client; |
||||||
|
use Islandora\Chullo\IFedoraApi; |
||||||
|
use Islandora\Chullo\FedoraApi; |
||||||
|
use Psr\Http\Message\RequestInterface; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Drupal plugin for the Fedora Flysystem adapter. |
||||||
|
* |
||||||
|
* @Adapter(id = "fedora") |
||||||
|
*/ |
||||||
|
class Fedora implements FlysystemPluginInterface, ContainerFactoryPluginInterface { |
||||||
|
|
||||||
|
use FlysystemUrlTrait; |
||||||
|
|
||||||
|
protected $fedora; |
||||||
|
|
||||||
|
protected $mimeTypeGuesser; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a Fedora plugin for Flysystem. |
||||||
|
* |
||||||
|
* @param \Islandora\Chullo\IFedoraApi $fedora |
||||||
|
* Fedora client. |
||||||
|
* @param \Symfony\Component\HttpFoundation\File\Mimetype\MimeTypeGuesserInterface $mime_type_guesser |
||||||
|
* Mimetype guesser. |
||||||
|
*/ |
||||||
|
public function __construct( |
||||||
|
IFedoraApi $fedora, |
||||||
|
MimeTypeGuesserInterface $mime_type_guesser |
||||||
|
) { |
||||||
|
$this->fedora = $fedora; |
||||||
|
$this->mimeTypeGuesser = $mime_type_guesser; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||||
|
// Construct Authorization header using jwt token. |
||||||
|
$jwt = $container->get('jwt.authentication.jwt'); |
||||||
|
$auth = 'Bearer ' . $jwt->generateToken(); |
||||||
|
|
||||||
|
// Construct guzzle client to middleware that adds the header. |
||||||
|
$stack = HandlerStack::create(); |
||||||
|
$stack->push(static::addHeader('Authorization', $auth)); |
||||||
|
$client = new Client([ |
||||||
|
'handler' => $stack, |
||||||
|
'base_uri' => $configuration['root'], |
||||||
|
]); |
||||||
|
$fedora = new FedoraApi($client); |
||||||
|
|
||||||
|
// Return it. |
||||||
|
return new static( |
||||||
|
$fedora, |
||||||
|
$container->get('file.mime_type.guesser') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Guzzle middleware to add a header to outgoing requests. |
||||||
|
* |
||||||
|
* @param string $header |
||||||
|
* Header name. |
||||||
|
* @param string $value |
||||||
|
* Header value. |
||||||
|
*/ |
||||||
|
public static function addHeader($header, $value) { |
||||||
|
return function (callable $handler) use ($header, $value) { |
||||||
|
return function ( |
||||||
|
RequestInterface $request, |
||||||
|
array $options |
||||||
|
) use ( |
||||||
|
$handler, |
||||||
|
$header, |
||||||
|
$value |
||||||
|
) { |
||||||
|
$request = $request->withHeader($header, $value); |
||||||
|
return $handler($request, $options); |
||||||
|
}; |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function getAdapter() { |
||||||
|
return new FedoraAdapter($this->fedora, $this->mimeTypeGuesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function ensure($force = FALSE) { |
||||||
|
// Check fedora root for sanity. |
||||||
|
$response = $this->fedora->getResourceHeaders(''); |
||||||
|
|
||||||
|
if ($response->getStatusCode() != 200) { |
||||||
|
return [[ |
||||||
|
'severity' => RfcLogLevel::ERROR, |
||||||
|
'message' => '%url returned %status', |
||||||
|
'context' => [ |
||||||
|
'%url' => $this->fedora->getBaseUri(), |
||||||
|
'%status' => $response->getStatusCode(), |
||||||
|
], |
||||||
|
], |
||||||
|
]; |
||||||
|
} |
||||||
|
|
||||||
|
return []; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,105 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Plugin\Condition; |
||||||
|
|
||||||
|
use Drupal\Core\Condition\ConditionPluginBase; |
||||||
|
use Drupal\Core\Form\FormStateInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides a 'Content Entity Type' condition. |
||||||
|
* |
||||||
|
* @Condition( |
||||||
|
* id = "content_entity_type", |
||||||
|
* label = @Translation("Content Entity Type"), |
||||||
|
* context = { |
||||||
|
* "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node")), |
||||||
|
* "media" = @ContextDefinition("entity:media", required = FALSE, label = @Translation("Media")), |
||||||
|
* "file" = @ContextDefinition("entity:file", required = FALSE, label = @Translation("File")), |
||||||
|
* "taxonomy_term" = @ContextDefinition("entity:taxonomy_term", required = FALSE, label = @Translation("Term")) |
||||||
|
* } |
||||||
|
* ) |
||||||
|
*/ |
||||||
|
class ContentEntityType extends ConditionPluginBase { |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||||||
|
$form['types'] = [ |
||||||
|
'#title' => $this->t('Content Entity Types'), |
||||||
|
'#type' => 'checkboxes', |
||||||
|
'#options' => [ |
||||||
|
'node' => $this->t('Node'), |
||||||
|
'media' => $this->t('Media'), |
||||||
|
'file' => $this->t('File'), |
||||||
|
'taxonomy_term' => $this->t('Taxonomy Term'), |
||||||
|
], |
||||||
|
'#default_value' => $this->configuration['types'], |
||||||
|
]; |
||||||
|
return parent::buildConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
||||||
|
$this->configuration['types'] = array_filter($form_state->getValue('types')); |
||||||
|
parent::submitConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function evaluate() { |
||||||
|
if (empty($this->configuration['types']) && !$this->isNegated()) { |
||||||
|
return TRUE; |
||||||
|
} |
||||||
|
|
||||||
|
foreach ($this->configuration['types'] as $type) { |
||||||
|
if ($this->getContext($type)->hasContextValue()) { |
||||||
|
$entity = $this->getContextValue($type); |
||||||
|
if ($entity && $entity->getEntityTypeId() == $type) { |
||||||
|
return TRUE; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function summary() { |
||||||
|
if (count($this->configuration['types']) > 1) { |
||||||
|
$types = $this->configuration['types']; |
||||||
|
$last = array_pop($types); |
||||||
|
$types = implode(', ', $types); |
||||||
|
return $this->t( |
||||||
|
'The content entity is a @types or @last', |
||||||
|
[ |
||||||
|
'@types' => $types, |
||||||
|
'@last' => $last, |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
$type = reset($this->configuration['types']); |
||||||
|
return $this->t( |
||||||
|
'The content entity is a @type', |
||||||
|
[ |
||||||
|
'@type' => $type, |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function defaultConfiguration() { |
||||||
|
return array_merge( |
||||||
|
['types' => []], |
||||||
|
parent::defaultConfiguration() |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,168 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Plugin\Condition; |
||||||
|
|
||||||
|
use Drupal\Core\Condition\ConditionPluginBase; |
||||||
|
use Drupal\Core\File\FileSystem; |
||||||
|
use Drupal\Core\Form\FormStateInterface; |
||||||
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
||||||
|
use Drupal\file\FileInterface; |
||||||
|
use Drupal\islandora\IslandoraUtils; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides a condition to filter media based on where its source file resides. |
||||||
|
* |
||||||
|
* @Condition( |
||||||
|
* id = "file_uses_filesystem", |
||||||
|
* label = @Translation("File uses filesystem"), |
||||||
|
* context = { |
||||||
|
* "file" = @ContextDefinition("entity:file", required = TRUE , label = @Translation("file")) |
||||||
|
* } |
||||||
|
* ) |
||||||
|
*/ |
||||||
|
class FileUsesFilesystem extends ConditionPluginBase implements ContainerFactoryPluginInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Islandora utility functions. |
||||||
|
* |
||||||
|
* @var \Drupal\islandora\IslandoraUtils |
||||||
|
*/ |
||||||
|
protected $utils; |
||||||
|
|
||||||
|
/** |
||||||
|
* File system service. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\File\FileSystem |
||||||
|
*/ |
||||||
|
protected $fileSystem; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructor. |
||||||
|
* |
||||||
|
* @param array $configuration |
||||||
|
* The plugin configuration, i.e. an array with configuration values keyed |
||||||
|
* by configuration option name. The special key 'context' may be used to |
||||||
|
* initialize the defined contexts by setting it to an array of context |
||||||
|
* values keyed by context names. |
||||||
|
* @param string $plugin_id |
||||||
|
* The plugin_id for the plugin instance. |
||||||
|
* @param mixed $plugin_definition |
||||||
|
* The plugin implementation definition. |
||||||
|
* @param \Drupal\islandora\IslandoraUtils $utils |
||||||
|
* Islandora utility functions. |
||||||
|
* @param \Drupal\Core\File\FileSystem $file_system |
||||||
|
* File system service. |
||||||
|
*/ |
||||||
|
public function __construct( |
||||||
|
array $configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
IslandoraUtils $utils, |
||||||
|
FileSystem $file_system |
||||||
|
) { |
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition); |
||||||
|
$this->utils = $utils; |
||||||
|
$this->fileSystem = $file_system; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||||
|
return new static( |
||||||
|
$configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
$container->get('islandora.utils'), |
||||||
|
$container->get('file_system') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||||||
|
$schemes = $this->utils->getFilesystemSchemes(); |
||||||
|
$options = array_combine($schemes, $schemes); |
||||||
|
|
||||||
|
$form['filesystems'] = [ |
||||||
|
'#title' => $this->t('Filesystems'), |
||||||
|
'#type' => 'checkboxes', |
||||||
|
'#options' => $options, |
||||||
|
'#default_value' => $this->configuration['filesystems'], |
||||||
|
]; |
||||||
|
return parent::buildConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { |
||||||
|
$this->configuration['filesystems'] = array_filter($form_state->getValue('filesystems')); |
||||||
|
parent::submitConfigurationForm($form, $form_state); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function summary() { |
||||||
|
if (count($this->configuration['filesystems']) > 1) { |
||||||
|
$filesystems = $this->configuration['filesystems']; |
||||||
|
$last = array_pop($filesystems); |
||||||
|
$filesystems = implode(', ', $filesystems); |
||||||
|
return $this->t( |
||||||
|
'The file uses @filesystems or @last', |
||||||
|
[ |
||||||
|
'@filesystems' => $filesystems, |
||||||
|
'@last' => $last, |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
$filesystem = reset($this->configuration['filesystems']); |
||||||
|
return $this->t( |
||||||
|
'The file uses @filesystem', |
||||||
|
[ |
||||||
|
'@filesystem' => $filesystem, |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function evaluate() { |
||||||
|
if (empty($this->configuration['filesystems']) && !$this->isNegated()) { |
||||||
|
return TRUE; |
||||||
|
} |
||||||
|
|
||||||
|
$file = $this->getContextValue('file'); |
||||||
|
return $this->evaluateFile($file); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* The actual evaluate function. |
||||||
|
* |
||||||
|
* @param \Drupal\file\FileInterface $file |
||||||
|
* File. |
||||||
|
* |
||||||
|
* @return bool |
||||||
|
* TRUE on success. |
||||||
|
*/ |
||||||
|
protected function evaluateFile(FileInterface $file) { |
||||||
|
$uri = $file->getFileUri(); |
||||||
|
$scheme = $this->fileSystem->uriScheme($uri); |
||||||
|
return !empty($this->configuration['filesystems'][$scheme]); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function defaultConfiguration() { |
||||||
|
return array_merge( |
||||||
|
['filesystems' => []], |
||||||
|
parent::defaultConfiguration() |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,35 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\islandora\Plugin\Condition; |
|
||||||
|
|
||||||
use Drupal\Core\Condition\ConditionPluginBase; |
|
||||||
use Drupal\file\FileInterface; |
|
||||||
|
|
||||||
/** |
|
||||||
* Provides an 'Is File' condition. |
|
||||||
* |
|
||||||
* @Condition( |
|
||||||
* id = "is_file", |
|
||||||
* label = @Translation("Is File"), |
|
||||||
* context = { |
|
||||||
* "file" = @ContextDefinition("entity:file", label = @Translation("File")) |
|
||||||
* } |
|
||||||
* ) |
|
||||||
*/ |
|
||||||
class IsFile extends ConditionPluginBase { |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function summary() { |
|
||||||
return $this->t('The entity is a File'); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function evaluate() { |
|
||||||
return $this->getContextValue('file') instanceof FileInterface; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,35 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\islandora\Plugin\Condition; |
|
||||||
|
|
||||||
use Drupal\Core\Condition\ConditionPluginBase; |
|
||||||
use Drupal\media\MediaInterface; |
|
||||||
|
|
||||||
/** |
|
||||||
* Provides an 'Is Media' condition. |
|
||||||
* |
|
||||||
* @Condition( |
|
||||||
* id = "is_media", |
|
||||||
* label = @Translation("Is Media"), |
|
||||||
* context = { |
|
||||||
* "media" = @ContextDefinition("entity:media", label = @Translation("Media")) |
|
||||||
* } |
|
||||||
* ) |
|
||||||
*/ |
|
||||||
class IsMedia extends ConditionPluginBase { |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function summary() { |
|
||||||
return $this->t('The entity is a Media'); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function evaluate() { |
|
||||||
return $this->getContextValue('media') instanceof MediaInterface; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,35 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\islandora\Plugin\Condition; |
|
||||||
|
|
||||||
use Drupal\Core\Condition\ConditionPluginBase; |
|
||||||
use Drupal\node\NodeInterface; |
|
||||||
|
|
||||||
/** |
|
||||||
* Provides an 'Is Node' condition. |
|
||||||
* |
|
||||||
* @Condition( |
|
||||||
* id = "is_node", |
|
||||||
* label = @Translation("Is Node"), |
|
||||||
* context = { |
|
||||||
* "node" = @ContextDefinition("entity:node", label = @Translation("Node")) |
|
||||||
* } |
|
||||||
* ) |
|
||||||
*/ |
|
||||||
class IsNode extends ConditionPluginBase { |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function summary() { |
|
||||||
return $this->t('The entity is a Node'); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function evaluate() { |
|
||||||
return $this->getContextValue('node') instanceof NodeInterface; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,43 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\islandora\Plugin\Condition; |
|
||||||
|
|
||||||
use Drupal\Core\Condition\ConditionPluginBase; |
|
||||||
use Drupal\taxonomy\TermInterface; |
|
||||||
|
|
||||||
/** |
|
||||||
* Provides an 'Is Term' condition. |
|
||||||
* |
|
||||||
* @Condition( |
|
||||||
* id = "is_term", |
|
||||||
* label = @Translation("Is Term"), |
|
||||||
* context = { |
|
||||||
* "taxonomy_term" = @ContextDefinition("entity:taxonomy_term", label = @Translation("Term")) |
|
||||||
* } |
|
||||||
* ) |
|
||||||
*/ |
|
||||||
class IsTerm extends ConditionPluginBase { |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function summary() { |
|
||||||
return $this->t('The entity is a Taxonomy Term'); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function getContextMapping() { |
|
||||||
$this->configuration['context_mapping'] = ['taxonomy_term' => '@islandora.taxonomy_term_route_context_provider:taxonomy_term']; |
|
||||||
return parent::getContextMapping(); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function evaluate() { |
|
||||||
return $this->getContextValue('taxonomy_term') instanceof TermInterface; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,116 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\Plugin\Condition; |
||||||
|
|
||||||
|
use Drupal\Core\File\FileSystem; |
||||||
|
use Drupal\islandora\MediaSource\MediaSourceService; |
||||||
|
use Drupal\islandora\IslandoraUtils; |
||||||
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides a condition to filter media based on where its source file resides. |
||||||
|
* |
||||||
|
* @Condition( |
||||||
|
* id = "media_uses_filesystem", |
||||||
|
* label = @Translation("Media uses filesystem"), |
||||||
|
* context = { |
||||||
|
* "media" = @ContextDefinition("entity:media", required = TRUE , label = @Translation("media")) |
||||||
|
* } |
||||||
|
* ) |
||||||
|
*/ |
||||||
|
class MediaUsesFilesystem extends FileUsesFilesystem { |
||||||
|
|
||||||
|
/** |
||||||
|
* Media source service. |
||||||
|
* |
||||||
|
* @var \Drupal\islandora\MediaSource\MediaSourceService |
||||||
|
*/ |
||||||
|
protected $mediaSource; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructor. |
||||||
|
* |
||||||
|
* @param array $configuration |
||||||
|
* The plugin configuration, i.e. an array with configuration values keyed |
||||||
|
* by configuration option name. The special key 'context' may be used to |
||||||
|
* initialize the defined contexts by setting it to an array of context |
||||||
|
* values keyed by context names. |
||||||
|
* @param string $plugin_id |
||||||
|
* The plugin_id for the plugin instance. |
||||||
|
* @param mixed $plugin_definition |
||||||
|
* The plugin implementation definition. |
||||||
|
* @param \Drupal\islandora\IslandoraUtils $utils |
||||||
|
* Islandora utility functions. |
||||||
|
* @param \Drupal\Core\File\FileSystem $file_system |
||||||
|
* File system service. |
||||||
|
* @param \Drupal\islandora\MediaSource\MediaSourceService $media_source |
||||||
|
* Media source service. |
||||||
|
*/ |
||||||
|
public function __construct( |
||||||
|
array $configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
IslandoraUtils $utils, |
||||||
|
FileSystem $file_system, |
||||||
|
MediaSourceService $media_source |
||||||
|
) { |
||||||
|
parent::__construct($configuration, $plugin_id, $plugin_definition, $utils, $file_system); |
||||||
|
$this->mediaSource = $media_source; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||||
|
return new static( |
||||||
|
$configuration, |
||||||
|
$plugin_id, |
||||||
|
$plugin_definition, |
||||||
|
$container->get('islandora.utils'), |
||||||
|
$container->get('file_system'), |
||||||
|
$container->get('islandora.media_source_service') |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function summary() { |
||||||
|
if (count($this->configuration['filesystems']) > 1) { |
||||||
|
$filesystems = $this->configuration['filesystems']; |
||||||
|
$last = array_pop($filesystems); |
||||||
|
$filesystems = implode(', ', $filesystems); |
||||||
|
return $this->t( |
||||||
|
'The media uses @filesystems or @last', |
||||||
|
[ |
||||||
|
'@filesystems' => $filesystems, |
||||||
|
'@last' => $last, |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
$filesystem = reset($this->configuration['filesystems']); |
||||||
|
return $this->t( |
||||||
|
'The media uses @filesystem', |
||||||
|
[ |
||||||
|
'@filesystem' => $filesystem, |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function evaluate() { |
||||||
|
if (empty($this->configuration['filesystems']) && !$this->isNegated()) { |
||||||
|
return TRUE; |
||||||
|
} |
||||||
|
|
||||||
|
$media = $this->getContextValue('media'); |
||||||
|
$file = $this->mediaSource->getSourceFile($media); |
||||||
|
if (!$file) { |
||||||
|
return FALSE; |
||||||
|
} |
||||||
|
return $this->evaluateFile($file); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -1,63 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\Tests\islandora\Functional; |
|
||||||
|
|
||||||
/** |
|
||||||
* Tests the IsFile condition. |
|
||||||
* |
|
||||||
* @group islandora |
|
||||||
*/ |
|
||||||
class IsFileTest extends IslandoraFunctionalTestBase { |
|
||||||
|
|
||||||
/** |
|
||||||
* @covers \Drupal\islandora\ContextProvider\FileContextProvider::__construct |
|
||||||
* @covers \Drupal\islandora\ContextProvider\FileContextProvider::getRuntimeContexts |
|
||||||
* @covers \Drupal\islandora\ContextProvider\MediaContextProvider::__construct |
|
||||||
* @covers \Drupal\islandora\ContextProvider\MediaContextProvider::getRuntimeContexts |
|
||||||
* @covers \Drupal\islandora\IslandoraContextManager::evaluateContexts |
|
||||||
* @covers \Drupal\islandora\IslandoraContextManager::applyContexts |
|
||||||
* @covers \Drupal\islandora\Plugin\Condition\IsFile::evaluate |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::buildConfigurationForm |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::submitConfigurationForm |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::execute |
|
||||||
* @covers \Drupal\islandora\IslandoraServiceProvider::alter |
|
||||||
*/ |
|
||||||
public function testIsFile() { |
|
||||||
// Create a test user. |
|
||||||
$account = $this->drupalCreateUser([ |
|
||||||
'administer contexts', |
|
||||||
'view media', |
|
||||||
'create media', |
|
||||||
'update media', |
|
||||||
]); |
|
||||||
$this->drupalLogin($account); |
|
||||||
|
|
||||||
// Set it up. |
|
||||||
$this->createContext('Test', 'test'); |
|
||||||
$this->addCondition('test', 'is_file'); |
|
||||||
$this->addPresetReaction('test', 'index', 'hello_world'); |
|
||||||
|
|
||||||
// Add a new media and confirm Hello World! is printed to the |
|
||||||
// screen for the file upload. |
|
||||||
$file = current($this->getTestFiles('file')); |
|
||||||
$values = [ |
|
||||||
'name[0][value]' => 'Test Media', |
|
||||||
'files[field_media_file_0]' => __DIR__ . '/../../fixtures/test_file.txt', |
|
||||||
]; |
|
||||||
$this->drupalPostForm('media/add/' . $this->testMediaType->id(), $values, t('Save')); |
|
||||||
$this->assertSession()->pageTextContains("Hello World!"); |
|
||||||
|
|
||||||
// Stash the media's url. |
|
||||||
$url = $this->getUrl(); |
|
||||||
|
|
||||||
// Edit the media, not touching the file this time. |
|
||||||
$values = [ |
|
||||||
'name[0][value]' => 'Test Media Changed', |
|
||||||
]; |
|
||||||
$this->postEntityEditForm($url, $values, 'Save'); |
|
||||||
|
|
||||||
// Confirm Hello World! is not printed to the screen. |
|
||||||
$this->assertSession()->pageTextNotContains("Hello World!"); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,56 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\Tests\islandora\Functional; |
|
||||||
|
|
||||||
/** |
|
||||||
* Tests the IsMedia condition. |
|
||||||
* |
|
||||||
* @group islandora |
|
||||||
*/ |
|
||||||
class IsMediaTest extends IslandoraFunctionalTestBase { |
|
||||||
|
|
||||||
/** |
|
||||||
* @covers \Drupal\islandora\ContextProvider\NodeContextProvider::__construct |
|
||||||
* @covers \Drupal\islandora\ContextProvider\NodeContextProvider::getRuntimeContexts |
|
||||||
* @covers \Drupal\islandora\ContextProvider\MediaContextProvider::__construct |
|
||||||
* @covers \Drupal\islandora\ContextProvider\MediaContextProvider::getRuntimeContexts |
|
||||||
* @covers \Drupal\islandora\IslandoraContextManager::evaluateContexts |
|
||||||
* @covers \Drupal\islandora\IslandoraContextManager::applyContexts |
|
||||||
* @covers \Drupal\islandora\Plugin\Condition\IsMedia::evaluate |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::buildConfigurationForm |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::submitConfigurationForm |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::execute |
|
||||||
* @covers \Drupal\islandora\IslandoraServiceProvider::alter |
|
||||||
*/ |
|
||||||
public function testIsMedia() { |
|
||||||
// Create a test user. |
|
||||||
$account = $this->drupalCreateUser([ |
|
||||||
'bypass node access', |
|
||||||
'administer contexts', |
|
||||||
'view media', |
|
||||||
'create media', |
|
||||||
'update media', |
|
||||||
]); |
|
||||||
$this->drupalLogin($account); |
|
||||||
|
|
||||||
$this->createContext('Test', 'test'); |
|
||||||
$this->addCondition('test', 'is_media'); |
|
||||||
$this->addPresetReaction('test', 'index', 'hello_world'); |
|
||||||
|
|
||||||
// Add a new media and confirm Hello World! is printed to the |
|
||||||
// screen. |
|
||||||
$values = [ |
|
||||||
'name[0][value]' => 'Test Media', |
|
||||||
'files[field_media_file_0]' => __DIR__ . '/../../fixtures/test_file.txt', |
|
||||||
]; |
|
||||||
$this->drupalPostForm('media/add/' . $this->testMediaType->id(), $values, t('Save')); |
|
||||||
$this->assertSession()->pageTextContains("Hello World!"); |
|
||||||
|
|
||||||
// Create a new node. |
|
||||||
$this->postNodeAddForm('test_type', ['title[0][value]' => 'Test Node'], 'Save'); |
|
||||||
|
|
||||||
// Confirm Hello World! is not printed to the screen. |
|
||||||
$this->assertSession()->pageTextNotContains("Hello World!"); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,51 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace Drupal\Tests\islandora\Functional; |
|
||||||
|
|
||||||
/** |
|
||||||
* Tests the IsTerm condition. |
|
||||||
* |
|
||||||
* @group islandora |
|
||||||
*/ |
|
||||||
class IsTermTest extends IslandoraFunctionalTestBase { |
|
||||||
|
|
||||||
/** |
|
||||||
* @covers \Drupal\islandora\ContextProvider\NodeContextProvider::__construct |
|
||||||
* @covers \Drupal\islandora\ContextProvider\NodeContextProvider::getRuntimeContexts |
|
||||||
* @covers \Drupal\islandora\ContextProvider\MediaContextProvider::__construct |
|
||||||
* @covers \Drupal\islandora\ContextProvider\MediaContextProvider::getRuntimeContexts |
|
||||||
* @covers \Drupal\islandora\IslandoraContextManager::evaluateContexts |
|
||||||
* @covers \Drupal\islandora\IslandoraContextManager::applyContexts |
|
||||||
* @covers \Drupal\islandora\Plugin\Condition\IsTerm::evaluate |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::buildConfigurationForm |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::submitConfigurationForm |
|
||||||
* @covers \Drupal\islandora\PresetReaction\PresetReaction::execute |
|
||||||
* @covers \Drupal\islandora\IslandoraServiceProvider::alter |
|
||||||
*/ |
|
||||||
public function testIsTerm() { |
|
||||||
// Create a test user. |
|
||||||
$account = $this->drupalCreateUser([ |
|
||||||
'bypass node access', |
|
||||||
'administer contexts', |
|
||||||
'administer taxonomy', |
|
||||||
]); |
|
||||||
$this->drupalLogin($account); |
|
||||||
|
|
||||||
$this->createContext('Test', 'test'); |
|
||||||
$this->addCondition('test', 'is_term'); |
|
||||||
$this->addPresetReaction('test', 'index', 'hello_world'); |
|
||||||
|
|
||||||
// Create a new term and confirm Hello World! is printed to the screen. |
|
||||||
$this->drupalPostForm( |
|
||||||
'admin/structure/taxonomy/manage/' . $this->testVocabulary->id() . '/add', |
|
||||||
['name[0][value]' => 'Test Term'], |
|
||||||
t('Save') |
|
||||||
); |
|
||||||
$this->assertSession()->pageTextContains("Hello World!"); |
|
||||||
|
|
||||||
// Create a new node and confirm Hello World! is not printed to the screen. |
|
||||||
$this->postNodeAddForm('test_type', ['title[0][value]' => 'Test Node'], 'Save'); |
|
||||||
$this->assertSession()->pageTextNotContains("Hello World!"); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,549 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\Tests\islandora\Kernel; |
||||||
|
|
||||||
|
use GuzzleHttp\Psr7; |
||||||
|
use GuzzleHttp\Psr7\Response; |
||||||
|
use Drupal\islandora\Flysystem\Adapter\FedoraAdapter; |
||||||
|
use Islandora\Chullo\IFedoraApi; |
||||||
|
use League\Flysystem\Config; |
||||||
|
use Prophecy\Argument; |
||||||
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the Fedora adapter for Flysystem. |
||||||
|
* |
||||||
|
* @group islandora |
||||||
|
* @coversDefaultClass \Drupal\islandora\Flysystem\Adapter\FedoraAdapter |
||||||
|
*/ |
||||||
|
class FedoraAdapterTest extends IslandoraKernelTestBase { |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up an adapter for Fedora calls that return 404. |
||||||
|
*/ |
||||||
|
protected function createAdapterForFail() { |
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(404); |
||||||
|
$response = $prophecy->reveal(); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
$prophecy->getResourceHeaders('')->willReturn($response); |
||||||
|
$prophecy->getResource('')->willReturn($response); |
||||||
|
$api = $prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new FedoraAdapter($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up an adapter for Fedora LDP-NR response. |
||||||
|
*/ |
||||||
|
protected function createAdapterForFile() { |
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(200); |
||||||
|
$prophecy->getHeader('Last-Modified')->willReturn(["Wed, 25 Jul 2018 17:42:04 GMT"]); |
||||||
|
$prophecy->getHeader('Link')->willReturn(['<http://www.w3.org/ns/ldp#Resource>;rel="type"', '<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"']); |
||||||
|
$prophecy->getHeader('Content-Type')->willReturn(['text/plain']); |
||||||
|
$prophecy->getHeader('Content-Length')->willReturn([strlen("DERP")]); |
||||||
|
$prophecy->getBody()->willReturn(PSR7\stream_for("DERP")); |
||||||
|
$response = $prophecy->reveal(); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
$prophecy->getResourceHeaders('')->willReturn($response); |
||||||
|
$prophecy->getResource('')->willReturn($response); |
||||||
|
$api = $prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new FedoraAdapter($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up an adapter for Fedora LDP-RS response. |
||||||
|
*/ |
||||||
|
protected function createAdapterForDirectory() { |
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(200); |
||||||
|
$prophecy->getHeader('Last-Modified')->willReturn(["Wed, 25 Jul 2018 17:42:04 GMT"]); |
||||||
|
$prophecy->getHeader('Link')->willReturn(['<http://www.w3.org/ns/ldp#Resource>;rel="type"', '<http://www.w3.org/ns/ldp#RDFSource>;rel="type"']); |
||||||
|
$response = $prophecy->reveal(); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
$prophecy->getResourceHeaders('')->willReturn($response); |
||||||
|
$api = $prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new FedoraAdapter($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up an adapter for Fedora write requests. |
||||||
|
*/ |
||||||
|
protected function createAdapterForWrite() { |
||||||
|
$fedora_prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(201); |
||||||
|
|
||||||
|
$fedora_prophecy->saveResource('', '', Argument::any())->willReturn($prophecy->reveal()); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(200); |
||||||
|
$prophecy->getHeader('Last-Modified')->willReturn(["Wed, 25 Jul 2018 17:42:04 GMT"]); |
||||||
|
$prophecy->getHeader('Link')->willReturn(['<http://www.w3.org/ns/ldp#Resource>;rel="type"', '<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"']); |
||||||
|
$prophecy->getHeader('Content-Type')->willReturn(['text/plain']); |
||||||
|
$prophecy->getHeader('Content-Length')->willReturn([strlen("DERP")]); |
||||||
|
$prophecy->getBody()->willReturn(PSR7\stream_for("DERP")); |
||||||
|
|
||||||
|
$fedora_prophecy->getResourceHeaders('')->willReturn($prophecy->reveal()); |
||||||
|
|
||||||
|
$api = $fedora_prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new FedoraAdapter($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up an adapter for Fedora write requests that fail. |
||||||
|
*/ |
||||||
|
protected function createAdapterForWriteFail() { |
||||||
|
$fedora_prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(500); |
||||||
|
|
||||||
|
$fedora_prophecy->saveResource('', '', Argument::any())->willReturn($prophecy->reveal()); |
||||||
|
|
||||||
|
$api = $fedora_prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new FedoraAdapter($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up an adapter for creating directories requests. |
||||||
|
*/ |
||||||
|
protected function createAdapterForCreateDir() { |
||||||
|
$fedora_prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(201); |
||||||
|
|
||||||
|
$fedora_prophecy->saveResource('')->willReturn($prophecy->reveal()); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(200); |
||||||
|
$prophecy->getHeader('Last-Modified')->willReturn(["Wed, 25 Jul 2018 17:42:04 GMT"]); |
||||||
|
$prophecy->getHeader('Link')->willReturn(['<http://www.w3.org/ns/ldp#Resource>;rel="type"', '<http://www.w3.org/ns/ldp#RDFSource>;rel="type"']); |
||||||
|
|
||||||
|
$fedora_prophecy->getResourceHeaders('')->willReturn($prophecy->reveal()); |
||||||
|
|
||||||
|
$api = $fedora_prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new FedoraAdapter($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up an adapter for Delete requests. |
||||||
|
*/ |
||||||
|
protected function createAdapterForDelete() { |
||||||
|
$fedora_prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(204); |
||||||
|
|
||||||
|
$fedora_prophecy->deleteResource('')->willReturn($prophecy->reveal()); |
||||||
|
$fedora_prophecy->getResourceHeaders('')->willReturn($prophecy->reveal()); |
||||||
|
$api = $fedora_prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new FedoraAdapter($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up an adapter for Delete requests that fail. |
||||||
|
*/ |
||||||
|
protected function createAdapterForDeleteFail() { |
||||||
|
$fedora_prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(500); |
||||||
|
|
||||||
|
$fedora_prophecy->deleteResource('')->willReturn($prophecy->reveal()); |
||||||
|
|
||||||
|
$api = $fedora_prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new FedoraAdapter($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Asserts the stucture/contents of a metadata response for a file. |
||||||
|
*/ |
||||||
|
protected function assertFileMetadata(array $metadata) { |
||||||
|
$this->assertTrue($metadata['type'] == 'file', "Expecting 'type' of 'file', received '" . $metadata['type'] . "'"); |
||||||
|
$this->assertTrue($metadata['timestamp'] == '1532540524', "Expecting 'timestamp' of '1532540524', received '" . $metadata['timestamp'] . "'"); |
||||||
|
$this->assertTrue($metadata['size'] == strlen("DERP"), "Expecting 'size' of '" . strlen("DERP") . "', received '" . $metadata['size'] . "'"); |
||||||
|
$this->assertTrue($metadata['mimetype'] == 'text/plain', "Expecting 'mimetype' of 'image/png', received '" . $metadata['mimetype'] . "'"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Asserts the stucture/contents of a metadata response for a directory. |
||||||
|
*/ |
||||||
|
protected function assertDirMetadata(array $metadata) { |
||||||
|
$this->assertTrue($metadata['type'] == 'dir', "Expecting 'type' of 'dir', received '" . $metadata['type'] . "'"); |
||||||
|
$this->assertTrue($metadata['timestamp'] == '1532540524', "Expecting 'timestamp' of '1532540524', received '" . $metadata['timestamp'] . "'"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getMetadata |
||||||
|
*/ |
||||||
|
public function testGetMetadataFail() { |
||||||
|
$adapter = $this->createAdapterForFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->getMetadata('') == FALSE, "getMetadata() must return FALSE on non-200"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getMetadata |
||||||
|
*/ |
||||||
|
public function testGetMetadataForFile() { |
||||||
|
$adapter = $this->createAdapterForFile(); |
||||||
|
|
||||||
|
$metadata = $adapter->getMetadata(''); |
||||||
|
$this->assertFileMetadata($metadata); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getMetadata |
||||||
|
*/ |
||||||
|
public function testGetMetadataForDirectory() { |
||||||
|
$adapter = $this->createAdapterForDirectory(); |
||||||
|
|
||||||
|
$metadata = $adapter->getMetadata(''); |
||||||
|
$this->assertDirMetadata($metadata); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::readStream |
||||||
|
*/ |
||||||
|
public function testReadStream() { |
||||||
|
$adapter = $this->createAdapterForFile(); |
||||||
|
|
||||||
|
$metadata = $adapter->readStream(''); |
||||||
|
$this->assertFileMetadata($metadata); |
||||||
|
$this->assertTrue(is_resource($metadata['stream']), "Expecting a 'stream' that is a resource"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::readStream |
||||||
|
*/ |
||||||
|
public function testReadStreamFail() { |
||||||
|
$adapter = $this->createAdapterForFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->readStream('') == FALSE, "readStream() should return FALSE on non-200 from Fedora"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::read |
||||||
|
*/ |
||||||
|
public function testRead() { |
||||||
|
$adapter = $this->createAdapterForFile(); |
||||||
|
|
||||||
|
$metadata = $adapter->read(''); |
||||||
|
$this->assertFileMetadata($metadata); |
||||||
|
$this->assertTrue($metadata['contents'] == "DERP", "Expecting 'content' of 'DERP', received '${metadata['contents']}'"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::read |
||||||
|
*/ |
||||||
|
public function testReadFail() { |
||||||
|
$adapter = $this->createAdapterForFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->read('') == FALSE, "readStream() should return FALSE on non-200 from Fedora"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::has |
||||||
|
*/ |
||||||
|
public function testHasFail() { |
||||||
|
$adapter = $this->createAdapterForFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->has('') == FALSE, "has() must return FALSE on non-200"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::has |
||||||
|
*/ |
||||||
|
public function testHasSuccess() { |
||||||
|
$adapter = $this->createAdapterForFile(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->has('') == TRUE, "has() must return TRUE on 200"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getSize |
||||||
|
*/ |
||||||
|
public function testGetSizeFail() { |
||||||
|
$adapter = $this->createAdapterForFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->getSize('') == FALSE, "getSize() must return FALSE on non-200"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getSize |
||||||
|
*/ |
||||||
|
public function testGetSizeSuccess() { |
||||||
|
$adapter = $this->createAdapterForFile(); |
||||||
|
|
||||||
|
$metadata = $adapter->getSize(''); |
||||||
|
$this->assertTrue($metadata['size'] == strlen("DERP"), "Expecting 'size' of '" . strlen("DERP") . "', received '" . $metadata['size'] . "'"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getMimetype |
||||||
|
*/ |
||||||
|
public function testGetMimetypeFail() { |
||||||
|
$adapter = $this->createAdapterForFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->getMimetype('') == FALSE, "getMimetype() must return FALSE on non-200"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getMimetype |
||||||
|
*/ |
||||||
|
public function testGetMimetypeSuccess() { |
||||||
|
$adapter = $this->createAdapterForFile(); |
||||||
|
|
||||||
|
$metadata = $adapter->getMimetype(''); |
||||||
|
$this->assertTrue($metadata['mimetype'] == 'text/plain', "Expecting 'mimetype' of 'text/plain', received '" . $metadata['mimetype'] . "'"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getTimestamp |
||||||
|
*/ |
||||||
|
public function testGetTimestampFail() { |
||||||
|
$adapter = $this->createAdapterForFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->getTimestamp('') == FALSE, "getTimestamp() must return FALSE on non-200"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::getTimestamp |
||||||
|
*/ |
||||||
|
public function testGetTimestampSuccess() { |
||||||
|
$adapter = $this->createAdapterForFile(); |
||||||
|
|
||||||
|
$metadata = $adapter->getTimestamp(''); |
||||||
|
$this->assertTrue($metadata['timestamp'] == '1532540524', "Expecting 'timestamp' of '1532540524', received '" . $metadata['timestamp'] . "'"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::write |
||||||
|
*/ |
||||||
|
public function testWriteFail() { |
||||||
|
$adapter = $this->createAdapterForWriteFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->write('', '', $this->prophesize(Config::class)->reveal()) == FALSE, "write() must return FALSE on non-201 or non-204"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::write |
||||||
|
*/ |
||||||
|
public function testWrite() { |
||||||
|
$adapter = $this->createAdapterForWrite(); |
||||||
|
|
||||||
|
$metadata = $adapter->write('', '', $this->prophesize(Config::class)->reveal()); |
||||||
|
$this->assertFileMetadata($metadata); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::writeStream |
||||||
|
*/ |
||||||
|
public function testWriteStreamFail() { |
||||||
|
$adapter = $this->createAdapterForWriteFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->writeStream('', '', $this->prophesize(Config::class)->reveal()) == FALSE, "writeStream() must return FALSE on non-201 or non-204"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::writeStream |
||||||
|
*/ |
||||||
|
public function testWriteStream() { |
||||||
|
$adapter = $this->createAdapterForWrite(); |
||||||
|
|
||||||
|
$metadata = $adapter->writeStream('', '', $this->prophesize(Config::class)->reveal()); |
||||||
|
$this->assertFileMetadata($metadata); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::update |
||||||
|
*/ |
||||||
|
public function testUpdateFail() { |
||||||
|
$adapter = $this->createAdapterForWriteFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->update('', '', $this->prophesize(Config::class)->reveal()) == FALSE, "write() must return FALSE on non-201 or non-204"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::update |
||||||
|
*/ |
||||||
|
public function testUpdate() { |
||||||
|
$adapter = $this->createAdapterForWrite(); |
||||||
|
|
||||||
|
$metadata = $adapter->update('', '', $this->prophesize(Config::class)->reveal()); |
||||||
|
$this->assertFileMetadata($metadata); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::updateStream |
||||||
|
*/ |
||||||
|
public function testUpdateStreamFail() { |
||||||
|
$adapter = $this->createAdapterForWriteFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->updateStream('', '', $this->prophesize(Config::class)->reveal()) == FALSE, "writeStream() must return FALSE on non-201 or non-204"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::updateStream |
||||||
|
*/ |
||||||
|
public function testUpdateStream() { |
||||||
|
$adapter = $this->createAdapterForWrite(); |
||||||
|
|
||||||
|
$metadata = $adapter->updateStream('', '', $this->prophesize(Config::class)->reveal()); |
||||||
|
$this->assertFileMetadata($metadata); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::delete |
||||||
|
*/ |
||||||
|
public function testDeleteFail() { |
||||||
|
$adapter = $this->createAdapterForDeleteFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->delete('') == FALSE, "delete() must return FALSE on non-204 or non-404"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::delete |
||||||
|
*/ |
||||||
|
public function testDelete() { |
||||||
|
$adapter = $this->createAdapterForDelete(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->delete('') == TRUE, "delete() must return TRUE on 204 or 404"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::deleteDir |
||||||
|
*/ |
||||||
|
public function testDeleteDirFail() { |
||||||
|
$adapter = $this->createAdapterForDeleteFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->deleteDir('') == FALSE, "deleteDir() must return FALSE on non-204 or non-404"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::deleteDir |
||||||
|
*/ |
||||||
|
public function testDeleteDir() { |
||||||
|
$adapter = $this->createAdapterForDelete(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->deleteDir('') == TRUE, "deleteDir() must return TRUE on 204 or 404"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::rename |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::copy |
||||||
|
*/ |
||||||
|
public function testRenameFail() { |
||||||
|
$adapter = $this->createAdapterForFail(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->rename('', '') == FALSE, "rename() must return FALSE on Fedora error"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::rename |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::copy |
||||||
|
*/ |
||||||
|
public function testRename() { |
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(200); |
||||||
|
$prophecy->getHeader('Last-Modified')->willReturn(["Wed, 25 Jul 2018 17:42:04 GMT"]); |
||||||
|
$prophecy->getHeader('Link')->willReturn(['<http://www.w3.org/ns/ldp#Resource>;rel="type"', '<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"']); |
||||||
|
$prophecy->getHeader('Content-Type')->willReturn(['text/plain']); |
||||||
|
$prophecy->getHeader('Content-Length')->willReturn([strlen("DERP")]); |
||||||
|
$prophecy->getBody()->willReturn(PSR7\stream_for("DERP")); |
||||||
|
$response = $prophecy->reveal(); |
||||||
|
|
||||||
|
$fedora_prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
$fedora_prophecy->getResource(Argument::any())->willReturn($response); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(201); |
||||||
|
$response = $prophecy->reveal(); |
||||||
|
|
||||||
|
$fedora_prophecy->saveResource(Argument::any(), Argument::any(), Argument::any())->willReturn($response); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(200); |
||||||
|
$prophecy->getHeader('Last-Modified')->willReturn(["Wed, 25 Jul 2018 17:42:04 GMT"]); |
||||||
|
$prophecy->getHeader('Link')->willReturn(['<http://www.w3.org/ns/ldp#Resource>;rel="type"', '<http://www.w3.org/ns/ldp#NonRDFSource>;rel="type"']); |
||||||
|
$prophecy->getHeader('Content-Type')->willReturn(['text/plain']); |
||||||
|
$prophecy->getHeader('Content-Length')->willReturn([strlen("DERP")]); |
||||||
|
$response = $prophecy->reveal(); |
||||||
|
|
||||||
|
$fedora_prophecy->getResourceHeaders(Argument::any())->willReturn($response); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(204); |
||||||
|
$response = $prophecy->reveal(); |
||||||
|
|
||||||
|
$fedora_prophecy->deleteResource(Argument::any())->willReturn($response); |
||||||
|
|
||||||
|
$api = $fedora_prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
$adapter = new FedoraAdapter($api, $mime_guesser); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->rename('', '') == TRUE, "rename() must return TRUE on success"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::createDir |
||||||
|
*/ |
||||||
|
public function testCreateDirFail() { |
||||||
|
$prophecy = $this->prophesize(Response::class); |
||||||
|
$prophecy->getStatusCode()->willReturn(500); |
||||||
|
|
||||||
|
$fedora_prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
$fedora_prophecy->saveResource('')->willReturn($prophecy->reveal()); |
||||||
|
|
||||||
|
$api = $fedora_prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
$adapter = new FedoraAdapter($api, $mime_guesser); |
||||||
|
|
||||||
|
$this->assertTrue($adapter->createDir('', $this->prophesize(Config::class)->reveal()) == FALSE, "createDir() must return FALSE on fail"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\Flysystem\Adapter\FedoraAdapter::createDir |
||||||
|
*/ |
||||||
|
public function testCreateDir() { |
||||||
|
$adapter = $this->createAdapterForCreateDir(); |
||||||
|
|
||||||
|
$metadata = $adapter->createDir('', $this->prophesize(Config::class)->reveal()); |
||||||
|
$this->assertDirMetadata($metadata); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,62 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\Tests\islandora\Kernel; |
||||||
|
|
||||||
|
use Drupal\islandora\Flysystem\Fedora; |
||||||
|
use League\Flysystem\AdapterInterface; |
||||||
|
use Islandora\Chullo\IFedoraApi; |
||||||
|
use Psr\Http\Message\ResponseInterface; |
||||||
|
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the Fedora plugin for Flysystem. |
||||||
|
* |
||||||
|
* @group islandora |
||||||
|
* @coversDefaultClass \Drupal\islandora\Flysystem\Fedora |
||||||
|
*/ |
||||||
|
class FedoraPluginTest extends IslandoraKernelTestBase { |
||||||
|
|
||||||
|
/** |
||||||
|
* Mocks up a plugin. |
||||||
|
*/ |
||||||
|
protected function createPlugin($return_code) { |
||||||
|
$prophecy = $this->prophesize(ResponseInterface::class); |
||||||
|
$prophecy->getStatusCode()->willReturn($return_code); |
||||||
|
$response = $prophecy->reveal(); |
||||||
|
|
||||||
|
$prophecy = $this->prophesize(IFedoraApi::class); |
||||||
|
$prophecy->getResourceHeaders('')->willReturn($response); |
||||||
|
$prophecy->getBaseUri()->willReturn(""); |
||||||
|
$api = $prophecy->reveal(); |
||||||
|
|
||||||
|
$mime_guesser = $this->prophesize(MimeTypeGuesserInterface::class)->reveal(); |
||||||
|
|
||||||
|
return new Fedora($api, $mime_guesser); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the getAdapter() method. |
||||||
|
* |
||||||
|
* @covers \Drupal\islandora\Flysystem\Fedora::getAdapter |
||||||
|
*/ |
||||||
|
public function testGetAdapter() { |
||||||
|
$plugin = $this->createPlugin(200); |
||||||
|
$adapter = $plugin->getAdapter(); |
||||||
|
|
||||||
|
$this->assertTrue($adapter instanceof AdapterInterface, "getAdapter() must return an AdapterInterface"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the ensure() method. |
||||||
|
* |
||||||
|
* @covers \Drupal\islandora\Flysystem\Fedora::ensure |
||||||
|
*/ |
||||||
|
public function testEnsure() { |
||||||
|
$plugin = $this->createPlugin(200); |
||||||
|
$this->assertTrue(empty($plugin->ensure()), "ensure() must return an empty array on success"); |
||||||
|
|
||||||
|
$plugin = $this->createPlugin(404); |
||||||
|
$this->assertTrue(!empty($plugin->ensure()), "ensure() must return a non-empty array on fail"); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue