View source
<?php
namespace Drupal\soundcloudfield\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\Display\EntityDisplayRepositoryInterface;
class SoundCloudWidgetValidationTest extends WebTestBase {
public static $modules = [
'entity_test',
'node',
'soundcloudfield',
];
protected $fieldName;
protected $fieldStorage;
protected $field;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'view test entity',
'administer entity_test content',
'link to any page',
]));
}
function testSoundCloudUrlValid() {
$field = $this
->getField();
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$this->fieldName}[0][url]", '', 'SoundCloud URL');
$this
->assertRaw('value=""');
\Drupal::service('path.alias_storage')
->save('/admin', '/a/path/alias');
$node = $this
->drupalCreateNode();
$validEntries = [
'https://soundcloud.com/dioxidmusic/soncha-gona-the-ground-20-tribute-to-the-100',
];
$this
->assertValidEntries($validEntries);
}
function testSoundCloudUrlInvalid() {
$field = $this
->getField();
$this
->drupalGet('entity_test/add');
$this
->assertFieldByName("{$this->fieldName}[0][url]", '', 'SoundCloud URL');
$this
->assertRaw('value=""');
\Drupal::service('path.alias_storage')
->save('/admin', '/a/path/alias');
$node = $this
->drupalCreateNode();
$validationError = 'Please insert a valid SoundCloud url.';
$invalidEntries = [
'only a string' => $validationError,
'https://lmgtfy.com/' => $validationError,
];
$this
->assertInvalidEntries($invalidEntries);
}
protected function assertValidEntries(array $validEntries) {
foreach ($validEntries as $key => $validEntry) {
$edit = [
"{$this->fieldName}[0][url]" => $validEntry,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
$id = $match[1];
$this
->assertText(t('entity_test @id has been created.', [
'@id' => $id,
]));
$this
->assertRaw($validEntry);
}
}
protected function assertInvalidEntries(array $invalidEntries) {
foreach ($invalidEntries as $invalidValue => $errorMessage) {
$edit = [
"{$this->fieldName}[0][url]" => $invalidValue,
];
$this
->drupalPostForm('entity_test/add', $edit, t('Save'));
$this
->assertText(t($errorMessage, [
'@link_path' => $invalidValue,
]));
}
}
private function getField() {
$this->fieldName = mb_strtolower($this
->randomMachineName());
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'soundcloud',
'cardinality' => 1,
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'settings' => [
'title' => DRUPAL_DISABLED,
],
]);
$this->field
->save();
EntityStorageInterface::load('entity_test.entity_test.default')
->setComponent($this->fieldName, [
'type' => 'soundcloud_url',
])
->save();
EntityDisplayRepositoryInterface::getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($this->fieldName, [
'type' => 'soundcloud_default',
'settings' => [
'soundcloud_player_type' => 'classic',
'soundcloud_player_width' => 100,
'soundcloud_player_height' => 166,
'soundcloud_player_height_sets' => 450,
'soundcloud_player_visual_height' => 450,
'soundcloud_player_autoplay' => '',
'soundcloud_player_color' => 'ff7700',
'soundcloud_player_hiderelated' => '',
'soundcloud_player_showartwork' => '',
'soundcloud_player_showcomments' => TRUE,
'soundcloud_player_showplaycount' => '',
],
])
->save();
}
}