comparison core/modules/media/tests/src/Kernel/MediaKernelTestBase.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
3 namespace Drupal\Tests\media\Kernel; 3 namespace Drupal\Tests\media\Kernel;
4 4
5 use Drupal\file\Entity\File; 5 use Drupal\file\Entity\File;
6 use Drupal\KernelTests\KernelTestBase; 6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\media\Entity\Media; 7 use Drupal\media\Entity\Media;
8 use Drupal\media\Entity\MediaType;
9 use Drupal\media\MediaTypeInterface; 8 use Drupal\media\MediaTypeInterface;
9 use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
10 use Drupal\user\Entity\User; 10 use Drupal\user\Entity\User;
11 use org\bovigo\vfs\vfsStream; 11 use org\bovigo\vfs\vfsStream;
12 12
13 /** 13 /**
14 * Base class for Media kernel tests. 14 * Base class for Media kernel tests.
15 */ 15 */
16 abstract class MediaKernelTestBase extends KernelTestBase { 16 abstract class MediaKernelTestBase extends KernelTestBase {
17
18 use MediaTypeCreationTrait;
17 19
18 /** 20 /**
19 * Modules to install. 21 * Modules to install.
20 * 22 *
21 * @var array 23 * @var array
76 $this->user->save(); 78 $this->user->save();
77 $this->container->get('current_user')->setAccount($this->user); 79 $this->container->get('current_user')->setAccount($this->user);
78 } 80 }
79 81
80 /** 82 /**
81 * Create a media type for a source plugin.
82 *
83 * @param string $media_source_name
84 * The name of the media source.
85 *
86 * @return \Drupal\media\MediaTypeInterface
87 * A media type.
88 */
89 protected function createMediaType($media_source_name) {
90 $id = strtolower($this->randomMachineName());
91 $media_type = MediaType::create([
92 'id' => $id,
93 'label' => $id,
94 'source' => $media_source_name,
95 'new_revision' => FALSE,
96 ]);
97 $media_type->save();
98 $source_field = $media_type->getSource()->createSourceField($media_type);
99 // The media type form creates a source field if it does not exist yet. The
100 // same must be done in a kernel test, since it does not use that form.
101 // @see \Drupal\media\MediaTypeForm::save()
102 $source_field->getFieldStorageDefinition()->save();
103 // The source field storage has been created, now the field can be saved.
104 $source_field->save();
105 $media_type->set('source_configuration', [
106 'source_field' => $source_field->getName(),
107 ])->save();
108 return $media_type;
109 }
110
111 /**
112 * Helper to generate a media item. 83 * Helper to generate a media item.
113 * 84 *
114 * @param string $filename 85 * @param string $filename
115 * String filename with extension. 86 * String filename with extension.
116 * @param \Drupal\media\MediaTypeInterface $media_type 87 * @param \Drupal\media\MediaTypeInterface $media_type