5 * Definition of Drupal\filefield_sources\Tests\ReferenceSourceTest.
8 namespace Drupal\filefield_sources\Tests;
10 use Drupal\Component\Utility\Unicode;
13 * Tests the reference source.
15 * @group filefield_sources
17 class ReferenceSourceTest extends FileFieldSourcesTestBase {
20 * Tests reference source enabled.
22 public function testReferenceSourceEnabled() {
25 $file = $this->createPermanentFileEntity();
27 $this->enableSources(array(
31 // Upload a file by 'Reference' source.
32 $this->uploadFileByReferenceSource($file->id(), $file->getFilename(), 0);
34 // We can only refer one file on single value field.
35 $this->assertNoFieldByXPath('//input[@type="submit"]', t('Select'), t('After uploading a file, "Select" button is no longer displayed.'));
37 // Remove uploaded file.
38 $this->removeFile($file->getFileName(), 0);
40 // Can select file again.
41 $this->assertFieldByXpath('//input[@type="submit"]', t('Select'), 'After clicking the "Remove" button, the "Select" button is displayed.');
45 * Test autocompletion.
47 public function testAutocompletion() {
49 $file = $this->createPermanentFileEntity();
50 $filename = $file->getFileName();
51 $first_character = substr($filename, 0, 1);
52 $second_character = substr($filename, 1, 1);
54 // Switch to 'Starts with' match type.
55 $this->updateFilefieldSourcesSettings('source_reference', 'autocomplete', '0');
57 // STARTS_WITH: empty results.
58 $query = $this->findCharacterNotInString($first_character);
59 $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query)));
60 $this->assertEqual($autocomplete_result, array(), "No files that have name starts with '$query'");
62 // STARTS_WITH: not empty results.
63 $query = $first_character;
64 $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query)));
65 $this->assertEqual($autocomplete_result[0]['label'], $filename, 'Autocompletion return correct label.');
66 $this->assertEqual($autocomplete_result[0]['value'], $filename . ' [fid:' . $file->id() . ']', 'Autocompletion return correct value.');
68 // Switch to 'Contains' match type.
69 $this->updateFilefieldSourcesSettings('source_reference', 'autocomplete', '1');
71 // CONTAINS: empty results.
72 $query = $this->findCharacterNotInString($filename);
73 $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query)));
74 $this->assertEqual($autocomplete_result, array(), "No files that have name contains '$query'");
76 // CONTAINS: not empty results.
77 $query = $second_character;
78 $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query)));
79 $this->assertEqual($autocomplete_result[0]['label'], $filename, 'Autocompletion return correct label.');
80 $this->assertEqual($autocomplete_result[0]['value'], $filename . ' [fid:' . $file->id() . ']', 'Autocompletion return correct value.');
84 * Find the first character that is not in string.
86 * Only find for lower case character.
88 * @param string $string
92 * First character that is not in the string.
94 protected function findCharacterNotInString($string) {
95 // Only check for lower case string.
96 $string = Unicode::strtolower($string);
98 // Lower case characters and numbers generated by
99 // \Drupal\simpletest\TestBase::randomMachineName().
100 $values = array_merge(range(97, 122), range(48, 57));
101 foreach ($values as $value) {
102 $character = chr($value);
103 if (strpos($string, $character) === FALSE) {