Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

DDST-454: Update tests to create models #8

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Includes an abstract kernel test class that can be extended in tests and a conte
- [x] Create a file.
- [x] Create a media which references an input node in media of field.
- [x] Create access tagged db queries for node, media and files.
- [x] Create an Islandora models taxonomy.
- [x] Create a field on Islandora content type that references the Islandora models taxonomy.
- [ ] Create a member of field
- [ ] Create node with member of field
- [ ] Create original file
Expand Down
4 changes: 4 additions & 0 deletions tests/src/Kernel/AbstractIslandoraKernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ abstract class AbstractIslandoraKernelTestBase extends KernelTestBase {
'media',
'file',
'user',
'taxonomy_term',
'media_type',
];

Expand Down Expand Up @@ -56,11 +57,14 @@ public function setUp(): void {
'image',
'system',
'text',
'taxonomy',
'link',
]);

$this->installConfig([
'node',
'user',
'taxonomy',
]);

// Install schemas for node, file and user.
Expand Down
88 changes: 79 additions & 9 deletions tests/src/Traits/IslandoraContentTypeTestTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

namespace Drupal\Tests\islandora_test_support\Traits;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\FileInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\Tests\test_support\Traits\Installs\InstallsModules;
use Drupal\Tests\test_support\Traits\Support\InteractsWithEntities;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
* Useful test traits for Islandora. Creates Islanodra node, media and files.
* Useful test traits for Islandora. Creates Islandora node, media and files.
*/
trait IslandoraContentTypeTestTraits {
use EntityReferenceFieldCreationTrait;
Expand All @@ -26,11 +31,12 @@ trait IslandoraContentTypeTestTraits {
use InteractsWithEntities;
use InstallsModules;
use UserCreationTrait;
use TaxonomyTestTrait;

/**
* Node type for node creation.
*
* @var \Drupal\node\NodeTypeInterface|\Drupal\node\Entity\NodeType
* @var \Drupal\node\NodeTypeInterface
*/
protected NodeTypeInterface $contentType;

Expand All @@ -42,9 +48,14 @@ trait IslandoraContentTypeTestTraits {
protected MediaTypeInterface $mediaType;

/**
* {@inheritDoc}
* Vocabulary for islandora models.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* @var \Drupal\taxonomy\VocabularyInterface
*/
protected VocabularyInterface $modelsVocabulary;

/**
* Creates a node type, media type, and vocabulary for Islandora.
*/
protected function prepareIslandoraContentType() : void {
// Create content required for creating islandora-esque data.
Expand All @@ -53,15 +64,26 @@ protected function prepareIslandoraContentType() : void {
$this->createEntityReferenceField('media',
$this->mediaType->id(), IslandoraUtils::MEDIA_OF_FIELD,
"Media Of", $this->contentType->getEntityType()->getBundleOf());

// Create islandora_models vocabulary.
$this->modelsVocabulary = $this->createModelsVocabulary();
$this->createEntityReferenceField(
'node',
'page',
IslandoraUtils::MODEL_FIELD,
'Model',
'taxonomy_term',
'default',
['target_bundles' => ['islandora_models']]
);

}

/**
* Helper; create a node.
*
* @return \Drupal\node\NodeInterface
* A created (and saved) node entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createNode() : NodeInterface {
if (empty($this->contentType)) {
Expand All @@ -80,8 +102,6 @@ protected function createNode() : NodeInterface {
*
* @return \Drupal\file\FileInterface
* A created (and saved) file entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createFile() : FileInterface {
/** @var \Drupal\file\FileInterface $entity */
Expand All @@ -97,7 +117,7 @@ protected function createFile() : FileInterface {
* @return string
* File URI.
*/
protected function createUri() {
protected function createUri(): string {
$filepath = 'test file ' . $this->randomMachineName();
$scheme = 'public';
$filepath = $scheme . '://' . $filepath;
Expand Down Expand Up @@ -149,4 +169,54 @@ protected function getMediaFieldName() : string {
return $this->mediaType->getSource()->getSourceFieldDefinition($this->mediaType)->getName();
}

/**
* Helper; Creates the islandora models vocabulary.
*
* @return \Drupal\taxonomy\VocabularyInterface
* The created vocabulary.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createModelsVocabulary() : Vocabulary {
$vocabulary = $this->createVocabulary(['vid' => 'islandora_models']);

// Create link type field on vocabulary for external uri.
$field_name = IslandoraUtils::EXTERNAL_URI_FIELD;

// Create a field with settings to validate.
$storage_definition = [
'field_name' => $field_name,
'entity_type' => 'taxonomy_term',
'type' => 'link',
'cardinality' => 1,
];

FieldStorageConfig::create($storage_definition)->save();

$field_definition = [
'field_name' => $field_name,
'entity_type' => 'taxonomy_term',
'bundle' => $vocabulary->id(),
'label' => $this->randomMachineName() . '_label',
];

FieldConfig::create($field_definition)->save();

return $vocabulary;
}

/**
* Creates a non-islandora node.
*
* @return \Drupal\node\NodeInterface
* A created (and saved) node entity.
*/
public function createNonIslandoraNode(): NodeInterface {
/** @var \Drupal\node\NodeInterface $entity */
return $this->createEntity('node', [
'type' => $this->contentType->id(),
'title' => $this->randomString(),
]);
}

}
Loading