View source
<?php
namespace Drupal\Tests\filefield_sources\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Unicode;
class ReferenceSourceTest extends FileFieldSourcesTestBase {
public function testReferenceSourceEnabled() {
$file = $this
->createPermanentFileEntity();
$this
->enableSources([
'reference' => TRUE,
]);
$this
->uploadFileByReferenceSource($file
->id(), $file
->getFilename(), 0);
$this
->assertNoFieldByXPath('//input[@type="submit"]', t('Select'), t('After uploading a file, "Select" button is no longer displayed.'));
$this
->removeFile($file
->getFileName(), 0);
$this
->assertFieldByXpath('//input[@type="submit"]', t('Select'), 'After clicking the "Remove" button, the "Select" button is displayed.');
}
public function testAutocompletion() {
$file = $this
->createPermanentFileEntity();
$filename = $file
->getFileName();
$first_character = substr($filename, 0, 1);
$second_character = substr($filename, 1, 1);
$default_options = [
'query' => [
'_format' => 'json',
],
];
$options = $default_options;
$this
->updateFilefieldSourcesSettings('source_reference', 'autocomplete', '0');
$query = $this
->findCharacterNotInString($first_character);
$options['query']['q'] = $query;
$autocomplete_result = Json::decode($this
->drupalGet('file/reference/node/' . $this->typeName . '/' . $this->fieldName, $options));
$this
->assertEqual($autocomplete_result, [], "No files that have name starts with '{$query}'");
$query = $first_character;
$options['query']['q'] = $query;
$autocomplete_result = Json::decode($this
->drupalGet('file/reference/node/' . $this->typeName . '/' . $this->fieldName, $options + [
'query' => [
'q' => $query,
],
]));
$this
->assertEqual($autocomplete_result[0]['label'], $filename, 'Autocompletion return correct label.');
$this
->assertEqual($autocomplete_result[0]['value'], $filename . ' [fid:' . $file
->id() . ']', 'Autocompletion return correct value.');
$this
->updateFilefieldSourcesSettings('source_reference', 'autocomplete', '1');
$query = $this
->findCharacterNotInString($filename);
$options['query']['q'] = $query;
$autocomplete_result = Json::decode($this
->drupalGet('file/reference/node/' . $this->typeName . '/' . $this->fieldName, $options + [
'query' => [
'q' => $query,
],
]));
$this
->assertEqual($autocomplete_result, [], "No files that have name contains '{$query}'");
$query = $second_character;
$options['query']['q'] = $query;
$autocomplete_result = Json::decode($this
->drupalGet('file/reference/node/' . $this->typeName . '/' . $this->fieldName, $options + [
'query' => [
'q' => $query,
],
]));
$this
->assertEqual($autocomplete_result[0]['label'], $filename, 'Autocompletion return correct label.');
$this
->assertEqual($autocomplete_result[0]['value'], $filename . ' [fid:' . $file
->id() . ']', 'Autocompletion return correct value.');
}
protected function findCharacterNotInString($string) {
$string = mb_strtolower($string);
$values = array_merge(range(97, 122), range(48, 57));
foreach ($values as $value) {
$character = chr($value);
if (strpos($string, $character) === FALSE) {
return $character;
}
}
}
}