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

EWPP-884: Remove field mapping. #177

Merged
merged 2 commits into from
Aug 16, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ queue_thumbnail_downloads: false
new_revision: false
source_configuration:
source_field: oe_media_js_asset_url
field_map:
title: name
field_map: {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Drupal\oe_media_js_asset\Plugin\media\Source;

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaTypeInterface;

Expand Down Expand Up @@ -36,4 +37,23 @@ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayIn
]);
}

/**
* {@inheritdoc}
*/
public function getSourceFieldValue(MediaInterface $media) {
$source_field = $this->configuration['source_field'];
if (empty($source_field)) {
throw new \RuntimeException('Source field for media source is not defined.');
}

$items = $media->get($source_field);
if ($items->isEmpty()) {
return NULL;
}

$field_item = $items->first();
$values = $field_item->getValue();
return implode(':', $values);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_media_js_asset\Functional;

/**
* Test javascript asset media.
*/
class JavaScriptAssetTest extends JavaScriptAssetTestBase {

/**
* Tests javascript asset media.
*/
public function testJavascriptAssetMedia(): void {
$user = $this->createUser([], '', TRUE);
$this->drupalLogin($user);

$this->drupalGet('/media/add/javascript_asset');
$this->assertSession()->fieldExists('Name');
$this->assertSession()->fieldExists('Environment');
$this->assertSession()->optionExists('Environment', 'Production');
$this->assertSession()->optionExists('Environment', 'Acceptance');
$this->assertSession()->pageTextContains('A relative path to the JS asset. It should always start with a "/" character.');
$this->assertSession()->fieldExists('Path');

$page = $this->getSession()->getPage();
$page->fillField('Name', 'First JavaScript asset');
$page->fillField('Environment', 'acceptance');
$page->fillField('Path', 'somejavascript.js');
$page->pressButton('Save');

$this->assertSession()->pageTextContains('Path should start with: /');
$this->assertSession()->pageTextContains('Path should start with: /');

$page->fillField('Path', '/some>javascript.js');
$page->pressButton('Save');

$this->assertSession()->pageTextContains('The entered path is not valid.');

$page->fillField('Path', '/somejavascript.js');
$page->pressButton('Save');

$this->assertSession()->pageTextContains('Javascript asset First JavaScript asset has been created.');

// Edit the media.
$media = \Drupal::entityTypeManager()->getStorage('media')->loadByProperties(['name' => 'First JavaScript asset']);
$media = reset($media);
$this->drupalGet($media->toUrl());

$assert_session = $this->assertSession();
$this->assertEquals('First JavaScript asset', $assert_session->fieldExists('Name')->getValue());
$this->assertEquals('/somejavascript.js', $assert_session->fieldExists('Path')->getValue());
$this->assertEquals('acceptance', $assert_session->fieldExists('Environment')->getValue());
$page->fillField('Environment', 'production');
$page->pressButton('Save');

$this->assertSession()->pageTextContains('Javascript asset First JavaScript asset has been updated.');
$this->drupalGet($media->toUrl());
$this->assertEquals('First JavaScript asset', $assert_session->fieldExists('Name')->getValue());
$this->assertEquals('/somejavascript.js', $assert_session->fieldExists('Path')->getValue());
$this->assertEquals('production', $assert_session->fieldExists('Environment')->getValue());
}

}

This file was deleted.