Skip to content

Commit

Permalink
DDST-454: Update tests to create models
Browse files Browse the repository at this point in the history
  • Loading branch information
bibliophileaxe committed Aug 15, 2024
1 parent 05ab32e commit b451511
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
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
73 changes: 70 additions & 3 deletions tests/src/Traits/IslandoraContentTypeTestTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace Drupal\Tests\islandora_test_support\Traits;

use Drupal\Core\Entity\EntityInterface;
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;
Expand All @@ -26,11 +32,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 +49,14 @@ trait IslandoraContentTypeTestTraits {
protected MediaTypeInterface $mediaType;

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

/**
* {@inheritDoc}
*/
protected function prepareIslandoraContentType() : void {
// Create content required for creating islandora-esque data.
Expand All @@ -53,6 +65,19 @@ 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']]
);

}

/**
Expand Down Expand Up @@ -149,4 +174,46 @@ protected function getMediaFieldName() : string {
return $this->mediaType->getSource()->getSourceFieldDefinition($this->mediaType)->getName();
}

/**
* Helper; Creates the islandora models vocabulary.
*/
protected function createModelsVocabulary() : Vocabulary {
$vocabulary = $this->createVocabulary(['vid' => 'islandora_models']);

// Create link type field on vocabulary for external uri.
$field_name = 'field_external_uri';

// 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.
*/
public function createNonIslandoraNode(): NodeInterface {
/** @var \Drupal\node\NodeInterface $entity */
return $this->createEntity('node', [
'type' => $this->contentType->id(),
'title' => $this->randomString(),
]);
}

}

0 comments on commit b451511

Please sign in to comment.