View source
<?php
namespace Drupal\Tests\file_url\FunctionalJavascript;
use Behat\Mink\Exception\ElementNotFoundException;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use Drupal\Tests\TestFileCreationTrait;
class FileUrlWidgetTest extends JavascriptTestBase {
use TestFileCreationTrait;
protected $files;
protected static $fields = [
'single' => 1,
'multiple' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
];
protected static $modules = [
'entity_test',
'file_url',
];
protected function setUp() {
parent::setUp();
$display = EntityFormDisplay::load('entity_test.entity_test.default');
foreach (static::$fields as $field_name => $cardinality) {
FieldStorageConfig::create([
'type' => 'file_url',
'entity_type' => 'entity_test',
'field_name' => $field_name,
'cardinality' => $cardinality,
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'field_type' => 'file_url',
'field_name' => $field_name,
'label' => $field_name,
])
->save();
$display
->setComponent($field_name, [
'type' => 'file_url_generic',
'region' => 'content',
]);
}
$display
->save();
$this->files = $this
->getTestFiles('text');
$this
->drupalLogin($this
->createUser([
'administer entity_test content',
]));
}
public function testFileUrlWidget() {
$this
->drupalGet('/entity_test/add');
$page = $this
->getSession()
->getPage();
$this
->addFileUrlItem('single', 'upload', 0);
$page
->pressButton('Save');
$this
->removeFileUrlItem('single');
$this
->assertSession()
->fieldExists('single[0][file_url_type]');
$url1 = $this
->randomUrl();
$this
->addFileUrlItem('single', 'remote', $url1);
$page
->pressButton('Save');
$this
->assertSession()
->linkExists($url1);
$this
->addFileUrlItem('multiple', 'upload', 1);
$this
->addFileUrlItem('multiple', 'upload', 2);
$url2 = $this
->randomUrl();
$this
->addFileUrlItem('multiple', 'remote', $url2);
$this
->assertOrderInPageText([
$this->files[1]->filename,
$this->files[2]->filename,
$url2,
]);
$dragged = $this
->xpath("//details[@data-drupal-selector='edit-multiple']//table//tr[1]//a[@class='tabledrag-handle']")[0];
$target = $this
->xpath("//details[@data-drupal-selector='edit-multiple']//table//tr[2]//a[@class='tabledrag-handle']")[0];
$dragged
->dragTo($target);
$this
->assertOrderInPageText([
$this->files[2]->filename,
$this->files[1]->filename,
$url2,
]);
$dragged = $this
->xpath("//details[@data-drupal-selector='edit-multiple']//table//tr[2]//a[@class='tabledrag-handle']")[0];
$target = $this
->xpath("//details[@data-drupal-selector='edit-multiple']//table//tr[3]//a[@class='tabledrag-handle']")[0];
$dragged
->dragTo($target);
$this
->assertOrderInPageText([
$this->files[2]->filename,
$url2,
$this->files[1]->filename,
]);
$page
->pressButton('Save');
$this
->assertOrderInPageText([
$this->files[2]->filename,
$url2,
$this->files[1]->filename,
]);
$url3 = $this
->randomUrl();
$this
->addFileUrlItem('multiple', 'remote', $url3);
$this
->assertOrderInPageText([
$this->files[2]->filename,
$url2,
$this->files[1]->filename,
$url3,
]);
$page
->pressButton('Save');
$this
->assertOrderInPageText([
$this->files[2]->filename,
$url2,
$this->files[1]->filename,
$url3,
]);
$this
->drupalGet('/entity_test/add');
$url4 = $this
->randomUrl();
$this
->addFileUrlItem('multiple', 'remote', $url4);
$url5 = 'invalid url';
$this
->addFileUrlItem('multiple', 'remote', $url5, FALSE);
$this
->assertSession()
->pageTextContains("The URL {$url5} is not valid.");
}
protected function addFileUrlItem($field_name, $file_mode, $value, $check_presence = TRUE) {
$session = $this
->getSession();
$page = $session
->getPage();
$wrapper = $page
->find('xpath', "//div[@data-drupal-selector='edit-{$field_name}-wrapper']");
if (!$wrapper) {
throw new ElementNotFoundException($session, $field_name);
}
$radio = $wrapper
->find('xpath', "//input[@type='radio' and @value='{$file_mode}']");
if (!$radio || $radio
->getTagName() !== 'input' || $radio
->getAttribute('type') !== 'radio') {
throw new ElementNotFoundException($session, $field_name);
}
$radio
->click();
if ($file_mode === 'upload') {
$file_system = $this->container
->get('file_system');
$wrapper
->attachFileToField('Choose a file', $file_system
->realpath($this->files[$value]->uri));
$this
->assertSession()
->assertWaitOnAjaxRequest();
if ($check_presence) {
$this
->assertSession()
->linkExists($this->files[$value]->filename);
}
}
elseif ($file_mode === 'remote') {
$wrapper
->fillField('Remote URL', $value);
$this
->assertSession()
->assertWaitOnAjaxRequest();
if ($check_presence) {
$this
->assertSession()
->linkExists($value);
}
}
}
protected function removeFileUrlItem($field_name, $delta = 0) {
$session = $this
->getSession();
$page = $session
->getPage();
$wrapper = $page
->find('css', "div.form-item-{$field_name}-{$delta}");
if (!$wrapper) {
throw new ElementNotFoundException($session, $field_name);
}
$wrapper
->pressButton('Remove');
$this
->assertSession()
->assertWaitOnAjaxRequest();
}
protected function randomUrl() {
return 'http://example.com/' . $this
->randomMachineName();
}
protected function assertOrderInPageText(array $items) {
$session = $this
->getSession();
$text = $session
->getPage()
->getText();
$strings = $not_found = [];
foreach ($items as $item) {
if (($pos = strpos($text, $item)) === FALSE) {
if (!in_array($item, $not_found)) {
$not_found[] = $item;
}
}
else {
$strings[$pos] = $item;
}
}
$quote_string_list = function (array $list) {
return implode(', ', array_map(function ($string) {
return "'{$string}'";
}, $list));
};
if ($not_found) {
$not_found = $quote_string_list($not_found);
throw new ElementNotFoundException($session
->getDriver(), "Cannot find item(s): {$not_found}.");
}
ksort($strings);
$ordered = $quote_string_list($items);
$this
->assertTrue($items === array_values($strings), "Strings correctly ordered as: {$ordered}.");
}
}