X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Ffilefield_sources%2Fsrc%2FTests%2FReferenceSourceTest.php;fp=web%2Fmodules%2Fcontrib%2Ffilefield_sources%2Fsrc%2FTests%2FReferenceSourceTest.php;h=9b392783873efd256e229cd0f64640d2638aa558;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/filefield_sources/src/Tests/ReferenceSourceTest.php b/web/modules/contrib/filefield_sources/src/Tests/ReferenceSourceTest.php new file mode 100644 index 000000000..9b3927838 --- /dev/null +++ b/web/modules/contrib/filefield_sources/src/Tests/ReferenceSourceTest.php @@ -0,0 +1,109 @@ +createPermanentFileEntity(); + + $this->enableSources(array( + 'reference' => TRUE, + )); + + // Upload a file by 'Reference' source. + $this->uploadFileByReferenceSource($file->id(), $file->getFilename(), 0); + + // We can only refer one file on single value field. + $this->assertNoFieldByXPath('//input[@type="submit"]', t('Select'), t('After uploading a file, "Select" button is no longer displayed.')); + + // Remove uploaded file. + $this->removeFile($file->getFileName(), 0); + + // Can select file again. + $this->assertFieldByXpath('//input[@type="submit"]', t('Select'), 'After clicking the "Remove" button, the "Select" button is displayed.'); + } + + /** + * Test autocompletion. + */ + public function testAutocompletion() { + // Create test file. + $file = $this->createPermanentFileEntity(); + $filename = $file->getFileName(); + $first_character = substr($filename, 0, 1); + $second_character = substr($filename, 1, 1); + + // Switch to 'Starts with' match type. + $this->updateFilefieldSourcesSettings('source_reference', 'autocomplete', '0'); + + // STARTS_WITH: empty results. + $query = $this->findCharacterNotInString($first_character); + $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query))); + $this->assertEqual($autocomplete_result, array(), "No files that have name starts with '$query'"); + + // STARTS_WITH: not empty results. + $query = $first_character; + $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('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.'); + + // Switch to 'Contains' match type. + $this->updateFilefieldSourcesSettings('source_reference', 'autocomplete', '1'); + + // CONTAINS: empty results. + $query = $this->findCharacterNotInString($filename); + $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('q' => $query))); + $this->assertEqual($autocomplete_result, array(), "No files that have name contains '$query'"); + + // CONTAINS: not empty results. + $query = $second_character; + $autocomplete_result = $this->drupalGetJSON('file/reference/node/' . $this->typeName . '/' . $this->fieldName, array('query' => array('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.'); + } + + /** + * Find the first character that is not in string. + * + * Only find for lower case character. + * + * @param string $string + * String to check. + * + * @return string + * First character that is not in the string. + */ + protected function findCharacterNotInString($string) { + // Only check for lower case string. + $string = Unicode::strtolower($string); + + // Lower case characters and numbers generated by + // \Drupal\simpletest\TestBase::randomMachineName(). + $values = array_merge(range(97, 122), range(48, 57)); + foreach ($values as $value) { + $character = chr($value); + if (strpos($string, $character) === FALSE) { + return $character; + } + } + } + +}