diff --git a/modules/helfi_media_map/src/Entity/HelMap.php b/modules/helfi_media_map/src/Entity/HelMap.php index 67253f0fd..e5a774e9d 100644 --- a/modules/helfi_media_map/src/Entity/HelMap.php +++ b/modules/helfi_media_map/src/Entity/HelMap.php @@ -4,7 +4,6 @@ namespace Drupal\helfi_media_map\Entity; -use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\helfi_media\Entity\MediaEntityBundle; use Drupal\media\MediaInterface; @@ -16,10 +15,10 @@ class HelMap extends MediaEntityBundle implements MediaInterface { /** * Get service provider url. * - * @return string|null + * @return string * Url of the service provider. */ - public function getServiceUrl(): ?string { + public function getServiceUrl(): string { $map_url = $this->get('field_media_hel_map')->first()->getString(); $url_parts = parse_url($map_url); return $url_parts['scheme'] . "://" . $url_parts['host']; @@ -28,14 +27,16 @@ public function getServiceUrl(): ?string { /** * Get the title of map. * - * @return \Drupal\Core\StringTranslation\TranslatableMarkup|null + * @return string|null * The title of the map. */ - public function getMediaTitle(): ?TranslatableMarkup { - return $this->get('field_media_hel_map') + public function getMediaTitle(): ?string { + $title = (string) $this->get('field_media_hel_map') ->first() ->get('title') ->getValue(); + + return empty($title) ? NULL : $title; } } diff --git a/modules/helfi_media_map/tests/src/Kernel/Entity/HelpMapTest.php b/modules/helfi_media_map/tests/src/Kernel/Entity/HelpMapTest.php new file mode 100644 index 000000000..1e4b70ddb --- /dev/null +++ b/modules/helfi_media_map/tests/src/Kernel/Entity/HelpMapTest.php @@ -0,0 +1,78 @@ +installConfig(['system', 'media', 'media_library']); + $this->installEntitySchema('user'); + $this->installEntitySchema('media'); + $this->installEntitySchema('file'); + $this->installSchema('file', ['file_usage']); + $this->installConfig('helfi_media_map'); + } + + /** + * Tests Hel Map bundle class. + */ + public function testBundleClass() : void { + /** @var \Drupal\media\MediaStorage $storage */ + $storage = $this->container->get('entity_type.manager') + ->getStorage('media'); + + $data = [ + 'uri' => 'https://kartta.hel.fi/embed?&setlanguage=fi&link=eptE2g', + ]; + $entity = $storage->create([ + 'name' => 'test', + 'bundle' => 'hel_map', + 'field_media_hel_map' => $data, + ]); + $entity->save(); + $this->assertInstanceOf(HelMap::class, $entity); + + $this->assertEquals($entity->getServiceUrl(), 'https://kartta.hel.fi'); + $this->assertNull($entity->getMediaTitle()); + + $data['title'] = 'Test title'; + $entity->set('field_media_hel_map', $data); + $entity->save(); + + $this->assertEquals($data['title'], $entity->getMediaTitle()); + } + +}