summaryrefslogtreecommitdiffstats
path: root/core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php
diff options
context:
space:
mode:
authorNathaniel Catchpole2017-05-19 12:57:59 (GMT)
committerNathaniel Catchpole2017-05-19 12:57:59 (GMT)
commit01621e5880b7e0efd335cb0bae243c25fc3b1c2e (patch)
tree28fcea3833aced2c323208dd9f1617ab1437f385 /core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php
parentf186fa6cf0c0618329028d5ced2c273279fa5095 (diff)
Issue #2831274 by slashrsm, seanB, Wim Leers, chr.fritsch, phenaproxima, naveenvalecha, marcoscano, webflo, Gábor Hojtsy, amateescu, Boobaa, iMiksu, mtodor, effulgentsia, xjm, Berdir, tim.plunkett, dawehner, tkoleary, tstoeckler, tedbow, alexpott, yoroy, catch, Bojhan, andypost, jhedstrom, jibran, aspilicious, boztek, cbr, bigbaldy, alex0412, dagmar, blueminds, ekes, Dave Reid, Sam152, bojanz, pixelmord, jonathanshaw, CTaPByK, webchick, samuel.mortenson, dbt102, dishabhadra, proweb.ua, rakesh.gectcr, rasikap, paranojik, pameeela, neardark, NormySan, Primsi, nicolas.rafaelli, romainj, royal121, vladan.me, vpeltot, woprrr, vilepickle, toni04, scheban, tduong, temkin, tim-e, mbovan, mashermike, felribeiro, giancarlosotelo, hctom, euphoric_mv, eric.duran7@gmail.com, edurenye, eelkeblok, H1ghlander, Jaesin, hkirsman, ja_ca, NickWilde, joachim, joshi.rohit100, marcingy, NerOcrO, Mixologic, jcisio, jfrederick, Lukas von Blarer, Maouna: Bring Media entity module to core as Media module
Diffstat (limited to 'core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php')
-rw-r--r--core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php67
1 files changed, 67 insertions, 0 deletions
diff --git a/core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php b/core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php
new file mode 100644
index 0000000..2d60c9b
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace Drupal\Tests\media\Functional;
+
+use Drupal\media\Entity\MediaType;
+
+/**
+ * Trait with helpers for Media functional tests.
+ */
+trait MediaFunctionalTestCreateMediaTypeTrait {
+
+ /**
+ * Creates a media type.
+ *
+ * @param array $values
+ * The media type values.
+ * @param string $source
+ * (optional) The media source plugin that is responsible for additional
+ * logic related to this media type. Defaults to 'test'.
+ *
+ * @return \Drupal\media\MediaTypeInterface
+ * A newly created media type.
+ */
+ protected function createMediaType(array $values = [], $source = 'test') {
+ if (empty($values['bundle'])) {
+ $id = strtolower($this->randomMachineName());
+ }
+ else {
+ $id = $values['bundle'];
+ }
+ $values += [
+ 'id' => $id,
+ 'label' => $id,
+ 'source' => $source,
+ 'source_configuration' => [],
+ 'field_map' => [],
+ 'new_revision' => FALSE,
+ ];
+
+ $media_type = MediaType::create($values);
+ $status = $media_type->save();
+
+ // @todo Rename to assertSame() when #1945040 is done.
+ // @see https://www.drupal.org/node/1945040
+ $this->assertIdentical(SAVED_NEW, $status, 'Media type was created successfully.');
+
+ // Ensure that the source field exists.
+ $source = $media_type->getSource();
+ $source_field = $source->getSourceFieldDefinition($media_type);
+ if (!$source_field) {
+ $source_field = $source->createSourceField($media_type);
+ /** @var \Drupal\field\FieldStorageConfigInterface $storage */
+ $storage = $source_field->getFieldStorageDefinition();
+ $storage->setLocked(TRUE)->save();
+ $source_field->save();
+
+ $media_type
+ ->set('source_configuration', [
+ 'source_field' => $source_field->getName(),
+ ])
+ ->save();
+ }
+
+ return $media_type;
+ }
+
+}